From 4c9510890a83f0502bfb3ac02687a1072dc16fef Mon Sep 17 00:00:00 2001 From: sr55 Date: Sun, 12 May 2019 10:58:35 +0100 Subject: WinGui: Clean-up the preset archive code. New filename format for archive files is presets.json.version.archive to make it easier to identify what preset version it came from. i.e. presets.json.36_0_0.archive --- .../HandBrakeWPF/Services/Presets/PresetService.cs | 63 +++++++++------------- 1 file changed, 24 insertions(+), 39 deletions(-) diff --git a/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs b/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs index ad8c6b7e7..37926a352 100644 --- a/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs +++ b/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs @@ -619,62 +619,47 @@ namespace HandBrakeWPF.Services.Presets } /// - /// Recover from a corrupted preset file - /// Add .old to the current filename, and delete the current file. + /// Archive the presets file without deleting it. /// /// - /// The broken presets file. + /// The filename to archive + /// + /// + /// True will delete the current presets file. /// /// - /// The . + /// The archived filename /// - private string RecoverFromCorruptedPresetFile(string file) + private string ArchivePresetFile(string file, bool delete) { try { // Recover from Error. - string disabledFile = string.Format("{0}.{1}", file, GeneralUtilities.ProcessId); + PresetVersion version = HandBrakePresetService.GetCurrentPresetVersion(); + string archiveFile = string.Format("{0}.{1}_{2}_{3}.archive", file, version.Major, version.Minor, version.Micro); if (File.Exists(file)) { - File.Move(file, disabledFile); - if (File.Exists(file)) + int counter = 0; + while (File.Exists(archiveFile)) { - File.Delete(file); + counter = counter + 1; + string appendedNumber = string.Format("({0})", counter); + archiveFile = string.Format("{0}.{1}_{2}_{3} {4}.archive", file, version.Major, version.Minor, version.Micro, appendedNumber); } - } - - return disabledFile; - } - catch (IOException e) - { - this.ServiceLogMessage(e.ToString()); - // Give up - } - return "Sorry, the archiving failed."; - } - - /// - /// Archive the presets file without deleting it. - /// - /// The filename to archive - /// The archived filename - private string ArchivePresetFile(string file) - { - try - { - // Recover from Error. - string archiveFile = string.Format("{0}.{1}", file, GeneralUtilities.ProcessId); - if (File.Exists(file)) - { File.Copy(file, archiveFile); + + if (delete) + { + File.Delete(file); + } } return archiveFile; } - catch (IOException) + catch (IOException e) { - // Give up + this.ServiceLogMessage(e.ToString()); } return "Sorry, the archiving failed."; @@ -713,7 +698,7 @@ namespace HandBrakeWPF.Services.Presets if (container?.PresetList == null) { this.ServiceLogMessage("Attempting Preset Recovery ..."); - string filename = this.RecoverFromCorruptedPresetFile(this.presetFile); + string filename = this.ArchivePresetFile(this.presetFile, true); this.errorService.ShowMessageBox( Resources.PresetService_UnableToLoadPresets + filename, Resources.PresetService_UnableToLoad, @@ -730,7 +715,7 @@ namespace HandBrakeWPF.Services.Presets { this.userSettingService.SetUserSetting(UserSettingConstants.ForcePresetReset, ForcePresetReset); - string fileName = this.ArchivePresetFile(this.presetFile); + string fileName = this.ArchivePresetFile(this.presetFile, true); this.errorService.ShowMessageBox( Resources.Presets_PresetForceReset + Environment.NewLine + Environment.NewLine + Resources.PresetService_ArchiveFile + fileName, @@ -746,7 +731,7 @@ namespace HandBrakeWPF.Services.Presets catch (Exception ex) { this.ServiceLogMessage(ex.ToString()); - this.RecoverFromCorruptedPresetFile(this.presetFile); + this.ArchivePresetFile(this.presetFile, true); this.UpdateBuiltInPresets(); } } -- cgit v1.2.3