From 50f8adb4efdf43eba77d92d8ef86eacee21fe201 Mon Sep 17 00:00:00 2001 From: sr55 Date: Sat, 11 May 2019 20:39:00 +0100 Subject: WinGui: Use hb_presets_read_file_json to read the UI presets file. This allows us to better handle version upgrades without resetting everything back to 0. --- win/CS/HandBrake.Interop/HandBrake.Interop.csproj | 1 + .../Interop/HandBrakePresetService.cs | 22 ++++++++- .../HandBrake.Interop/Interop/HbLib/HbFunctions.cs | 3 ++ .../Json/Presets/PresetTransportContainer.cs | 8 ++-- .../Interop/Model/PresetVersion.cs | 25 +++++++++++ win/CS/HandBrakeWPF/Constants.cs | 5 --- .../HandBrakeWPF/Properties/Resources.Designer.cs | 11 ----- win/CS/HandBrakeWPF/Properties/Resources.de.resx | 41 ++++++++--------- win/CS/HandBrakeWPF/Properties/Resources.fr.resx | 41 ++++++++--------- win/CS/HandBrakeWPF/Properties/Resources.resx | 5 --- win/CS/HandBrakeWPF/Properties/Resources.zh.resx | 41 ++++++++--------- .../Presets/Factories/JsonPresetFactory.cs | 12 ++--- .../HandBrakeWPF/Services/Presets/PresetService.cs | 52 ++++++---------------- 13 files changed, 125 insertions(+), 142 deletions(-) create mode 100644 win/CS/HandBrake.Interop/Interop/Model/PresetVersion.cs (limited to 'win') diff --git a/win/CS/HandBrake.Interop/HandBrake.Interop.csproj b/win/CS/HandBrake.Interop/HandBrake.Interop.csproj index a4aff67fe..733c81248 100644 --- a/win/CS/HandBrake.Interop/HandBrake.Interop.csproj +++ b/win/CS/HandBrake.Interop/HandBrake.Interop.csproj @@ -121,6 +121,7 @@ + diff --git a/win/CS/HandBrake.Interop/Interop/HandBrakePresetService.cs b/win/CS/HandBrake.Interop/Interop/HandBrakePresetService.cs index a51a44fd1..98eb6071b 100644 --- a/win/CS/HandBrake.Interop/Interop/HandBrakePresetService.cs +++ b/win/CS/HandBrake.Interop/Interop/HandBrakePresetService.cs @@ -17,6 +17,7 @@ namespace HandBrake.Interop.Interop using HandBrake.Interop.Interop.HbLib; using HandBrake.Interop.Interop.Helpers; using HandBrake.Interop.Interop.Json.Presets; + using HandBrake.Interop.Interop.Model; using Newtonsoft.Json; @@ -50,7 +51,7 @@ namespace HandBrake.Interop.Interop /// /// The . /// - public static PresetTransportContainer GetPresetFromFile(string filename) + public static PresetTransportContainer GetPresetsFromFile(string filename) { IntPtr presetStringPointer = HBFunctions.hb_presets_read_file_json(InteropUtilities.ToUtf8PtrFromString(filename)); string presetJson = Marshal.PtrToStringAnsi(presetStringPointer); @@ -88,5 +89,24 @@ namespace HandBrake.Interop.Interop writer.Write(preset); } } + + public static PresetVersion GetCurrentPresetVersion() + { + IntPtr major = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int))); + IntPtr minor = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int))); + IntPtr micro = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int))); + + HBFunctions.hb_presets_current_version(major, minor, micro); + + int majorVersion = Marshal.ReadInt32(major); + int minorVersion = Marshal.ReadInt32(minor); + int microVersion = Marshal.ReadInt32(micro); + + Marshal.FreeHGlobal(major); + Marshal.FreeHGlobal(minor); + Marshal.FreeHGlobal(micro); + + return new PresetVersion(majorVersion, minorVersion, microVersion); + } } } diff --git a/win/CS/HandBrake.Interop/Interop/HbLib/HbFunctions.cs b/win/CS/HandBrake.Interop/Interop/HbLib/HbFunctions.cs index 689ceb937..7fc1ed500 100644 --- a/win/CS/HandBrake.Interop/Interop/HbLib/HbFunctions.cs +++ b/win/CS/HandBrake.Interop/Interop/HbLib/HbFunctions.cs @@ -469,5 +469,8 @@ namespace HandBrake.Interop.Interop.HbLib // char * hb_presets_read_file_json(const char *filename); [DllImport("hb", EntryPoint = "hb_presets_read_file_json", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr hb_presets_read_file_json(IntPtr filename); + + [DllImport("hb", EntryPoint = "hb_presets_current_version", CallingConvention = CallingConvention.Cdecl)] + public static extern IntPtr hb_presets_current_version(IntPtr major, IntPtr minor, IntPtr micro); } } diff --git a/win/CS/HandBrake.Interop/Interop/Json/Presets/PresetTransportContainer.cs b/win/CS/HandBrake.Interop/Interop/Json/Presets/PresetTransportContainer.cs index 91ce48de5..4d72e70ff 100644 --- a/win/CS/HandBrake.Interop/Interop/Json/Presets/PresetTransportContainer.cs +++ b/win/CS/HandBrake.Interop/Interop/Json/Presets/PresetTransportContainer.cs @@ -37,7 +37,7 @@ namespace HandBrake.Interop.Interop.Json.Presets /// /// The version micro. /// - public PresetTransportContainer(string versionMajor, string versionMinor, string versionMicro) + public PresetTransportContainer(int versionMajor, int versionMinor, int versionMicro) { this.VersionMajor = versionMajor; this.VersionMicro = versionMicro; @@ -52,16 +52,16 @@ namespace HandBrake.Interop.Interop.Json.Presets /// /// Gets or sets the version major. /// - public string VersionMajor { get; set; } + public int VersionMajor { get; set; } /// /// Gets or sets the version micro. /// - public string VersionMicro { get; set; } + public int VersionMicro { get; set; } /// /// Gets or sets the version minor. /// - public string VersionMinor { get; set; } + public int VersionMinor { get; set; } } } diff --git a/win/CS/HandBrake.Interop/Interop/Model/PresetVersion.cs b/win/CS/HandBrake.Interop/Interop/Model/PresetVersion.cs new file mode 100644 index 000000000..dbe5578f8 --- /dev/null +++ b/win/CS/HandBrake.Interop/Interop/Model/PresetVersion.cs @@ -0,0 +1,25 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. +// +// +// Defines the PresetVersion type. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace HandBrake.Interop.Interop.Model +{ + public class PresetVersion + { + public PresetVersion(int major, int minor, int micro) + { + this.Major = major; + this.Minor = minor; + this.Micro = micro; + } + + public int Major { get; } + public int Minor { get; } + public int Micro { get; } + } +} diff --git a/win/CS/HandBrakeWPF/Constants.cs b/win/CS/HandBrakeWPF/Constants.cs index f4a65d7d2..5ee22d03d 100644 --- a/win/CS/HandBrakeWPF/Constants.cs +++ b/win/CS/HandBrakeWPF/Constants.cs @@ -37,11 +37,6 @@ namespace HandBrakeWPF public const string SourcePath = "{source_path}"; public const string SourceFolderName = "{source_folder_name}"; - /* Preset Versions */ - public const string PresetVersionMajor = "35"; - public const string PresetVersionMinor = "0"; - public const string PresetVersionMicro = "0"; - public const string FileScanMru = "FileScanMru"; public const string FileSaveMru = "FileSaveMru"; } diff --git a/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs b/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs index a3583ef4a..8c460eca0 100644 --- a/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs +++ b/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs @@ -3737,17 +3737,6 @@ namespace HandBrakeWPF.Properties { } } - /// - /// Looks up a localized string similar to HandBrake has detected your presets file is from an older version. - ///It will try and load the file anyway. - ///If it fails, it will archive off the old file and create a new one.. - /// - public static string PresetService_PresetsOutOfDate { - get { - return ResourceManager.GetString("PresetService_PresetsOutOfDate", resourceCulture); - } - } - /// /// Looks up a localized string similar to Unable to load presets.. /// diff --git a/win/CS/HandBrakeWPF/Properties/Resources.de.resx b/win/CS/HandBrakeWPF/Properties/Resources.de.resx index 5c330036f..e0d6029c8 100644 --- a/win/CS/HandBrakeWPF/Properties/Resources.de.resx +++ b/win/CS/HandBrakeWPF/Properties/Resources.de.resx @@ -1,4 +1,4 @@ - + - + - + - - - - + + + + - - + + - - + + - - - - + + + + - + - + @@ -559,11 +559,6 @@ Bitte stellen Sie sicher, dass VLC installiert und der Pfad in den HandBrake-Ein Archivierte Datei: - - HandBrake hat festgestellt, dass die Voreinstellungsdatei von einer älteren Version stammt. -Es wird versucht die Datei trotzdem zu laden. -Schlägt dies fehl, wird die Datei archiviert und eine neue erstellt. - Laden der Voreinstellung fehlgeschlagen. @@ -1964,4 +1959,4 @@ Nicht-Echtzeit Optionen: {date} {time} {creation-date} {creation-time} {quality} Zahl anhängen - + \ No newline at end of file diff --git a/win/CS/HandBrakeWPF/Properties/Resources.fr.resx b/win/CS/HandBrakeWPF/Properties/Resources.fr.resx index ab85f8933..d8aa30d8f 100644 --- a/win/CS/HandBrakeWPF/Properties/Resources.fr.resx +++ b/win/CS/HandBrakeWPF/Properties/Resources.fr.resx @@ -1,4 +1,4 @@ - + - + - + - - - - + + + + - - + + - - + + - - - - + + + + - + - + @@ -573,11 +573,6 @@ Assurez-vous que VLC est installé et que le répertoire spécifié dans les opt Fichier archivé: - - HandBrake a détecté que votre fichier de préréglages provient d'une version antérieure. -Il va quand même essayer de charger le fichier. -Si cela échoue, il archivera l'ancien fichier et en créera un nouveau. - Impossible de charger les préréglages. @@ -1978,4 +1973,4 @@ Non-Live Options: {date} {time} {creation-date} {creation-time} {quality} {bitra Append Number - + \ No newline at end of file diff --git a/win/CS/HandBrakeWPF/Properties/Resources.resx b/win/CS/HandBrakeWPF/Properties/Resources.resx index 66a4cf97f..d7d5dedcb 100644 --- a/win/CS/HandBrakeWPF/Properties/Resources.resx +++ b/win/CS/HandBrakeWPF/Properties/Resources.resx @@ -573,11 +573,6 @@ Please make sure VLC is installed and the directory specified in HandBrake's opt Archived File: - - HandBrake has detected your presets file is from an older version. -It will try and load the file anyway. -If it fails, it will archive off the old file and create a new one. - Unable to load presets. diff --git a/win/CS/HandBrakeWPF/Properties/Resources.zh.resx b/win/CS/HandBrakeWPF/Properties/Resources.zh.resx index dda1f9d5b..a9fbe4434 100644 --- a/win/CS/HandBrakeWPF/Properties/Resources.zh.resx +++ b/win/CS/HandBrakeWPF/Properties/Resources.zh.resx @@ -1,4 +1,4 @@ - + - + - + - - - - + + + + - - + + - - + + - - - - + + + + - + - + @@ -566,11 +566,6 @@ Foreign Audio Preferred, else First - 如果存在外语轨道,则会将其烧 存档的文件: - - HandBrake 检测到预设文件来自旧版本。 -它将尝试加载文件。 -如果失败,它将存档旧文件并创建新文件。 - 无法加载预设。 @@ -1966,4 +1961,4 @@ Non-Live Options: {date} {time} {creation-date} {creation-time} {quality} {bitra Append Number - + \ No newline at end of file diff --git a/win/CS/HandBrakeWPF/Services/Presets/Factories/JsonPresetFactory.cs b/win/CS/HandBrakeWPF/Services/Presets/Factories/JsonPresetFactory.cs index c6dd9595e..91efcc7f9 100644 --- a/win/CS/HandBrakeWPF/Services/Presets/Factories/JsonPresetFactory.cs +++ b/win/CS/HandBrakeWPF/Services/Presets/Factories/JsonPresetFactory.cs @@ -483,10 +483,8 @@ namespace HandBrakeWPF.Services.Presets.Factories /// public static PresetTransportContainer ExportPreset(Preset export, HBConfiguration config) { - PresetTransportContainer container = new PresetTransportContainer(); - container.VersionMajor = Constants.PresetVersionMajor; - container.VersionMinor = Constants.PresetVersionMinor; - container.VersionMicro = Constants.PresetVersionMicro; + PresetVersion presetVersion = HandBrakePresetService.GetCurrentPresetVersion(); + PresetTransportContainer container = new PresetTransportContainer(presetVersion.Major, presetVersion.Minor, presetVersion.Micro); container.PresetList = new List { CreateHbPreset(export, config) }; @@ -501,10 +499,8 @@ namespace HandBrakeWPF.Services.Presets.Factories /// A list of JSON object presets. public static PresetTransportContainer ExportPresets(IEnumerable exportList, HBConfiguration config) { - PresetTransportContainer container = new PresetTransportContainer(); - container.VersionMajor = Constants.PresetVersionMajor; - container.VersionMinor = Constants.PresetVersionMinor; - container.VersionMicro = Constants.PresetVersionMicro; + PresetVersion presetVersion = HandBrakePresetService.GetCurrentPresetVersion(); + PresetTransportContainer container = new PresetTransportContainer(presetVersion.Major, presetVersion.Minor, presetVersion.Micro); List presets = exportList.Select(item => CreateHbPreset(item, config)).ToList(); diff --git a/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs b/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs index 52f1d5d7f..56bbbf8d0 100644 --- a/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs +++ b/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs @@ -21,6 +21,7 @@ namespace HandBrakeWPF.Services.Presets using HandBrake.Interop.Interop; using HandBrake.Interop.Interop.Json.Presets; + using HandBrake.Interop.Interop.Model; using HandBrake.Interop.Model; using HandBrake.Interop.Utilities; @@ -174,7 +175,7 @@ namespace HandBrakeWPF.Services.Presets PresetTransportContainer container = null; try { - container = HandBrakePresetService.GetPresetFromFile(filename); + container = HandBrakePresetService.GetPresetsFromFile(filename); } catch (Exception exc) { @@ -681,16 +682,13 @@ namespace HandBrakeWPF.Services.Presets // Otherwise, we already have a file, so lets try load it. PresetTransportContainer container = null; - using (StreamReader reader = new StreamReader(this.presetFile)) + try { - try - { - container = JsonConvert.DeserializeObject(reader.ReadToEnd()); - } - catch (Exception exc) - { - this.ServiceLogMessage("Corrupted Presets File Detected: " + Environment.NewLine + exc); - } + container = HandBrakePresetService.GetPresetsFromFile(this.presetFile); + } + catch (Exception exc) + { + this.ServiceLogMessage("Corrupted Presets File Detected: " + Environment.NewLine + exc); } // Sanity Check. Did the container deserialise. @@ -709,23 +707,6 @@ namespace HandBrakeWPF.Services.Presets return; // Update built-in presets stores the presets locally, so just return. } - // Version Check - // If we have old presets, or the container wasn't parseable, or we have a version mismatch, backup the user preset file - // incase something goes wrong and reset built-in presets, then re-save. - bool ignoreBuildIn = false; - if (container.VersionMajor != Constants.PresetVersionMajor || container.VersionMinor != Constants.PresetVersionMinor || container.VersionMicro != Constants.PresetVersionMicro) - { - string fileName = this.ArchivePresetFile(this.presetFile); - this.errorService.ShowMessageBox( - Resources.PresetService_PresetsOutOfDate - + Environment.NewLine + Environment.NewLine + Resources.PresetService_ArchiveFile + fileName, - Resources.PresetService_UnableToLoad, - MessageBoxButton.OK, - MessageBoxImage.Information); - this.UpdateBuiltInPresets(); // Update built-in presets stores the presets locally, so just return. - ignoreBuildIn = true; - } - // Force Upgrade of presets if (this.userSettingService.GetUserSetting(UserSettingConstants.ForcePresetReset, typeof(int)) < ForcePresetReset) { @@ -742,7 +723,7 @@ namespace HandBrakeWPF.Services.Presets return; } - this.ProcessPresetList(container, ignoreBuildIn); + this.ProcessPresetList(container); } catch (Exception ex) { @@ -752,7 +733,7 @@ namespace HandBrakeWPF.Services.Presets } } - private void ProcessPresetList(PresetTransportContainer container, bool ignoreOldBuiltIn) + private void ProcessPresetList(PresetTransportContainer container) { // The presets file loaded was OK, so process it. foreach (var item in container.PresetList) @@ -767,11 +748,6 @@ namespace HandBrakeWPF.Services.Presets { Preset preset = JsonPresetFactory.ImportPreset(hbpreset); - if (preset.IsBuildIn && ignoreOldBuiltIn) - { - continue; - } - // Migration preset.Category = category.PresetName == "User Presets" ? UserPresetCatgoryName : category.PresetName; preset.IsBuildIn = hbpreset.Type == 0; @@ -834,11 +810,9 @@ namespace HandBrakeWPF.Services.Presets // Wrap the categories in a container. JsonSerializerSettings settings = new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Ignore }; - PresetTransportContainer container = new PresetTransportContainer( - Constants.PresetVersionMajor, - Constants.PresetVersionMinor, - Constants.PresetVersionMicro) - { PresetList = new List() }; + + PresetVersion presetVersion = HandBrakePresetService.GetCurrentPresetVersion(); + PresetTransportContainer container = new PresetTransportContainer(presetVersion.Major, presetVersion.Minor, presetVersion.Micro) { PresetList = new List() }; container.PresetList.AddRange(presetCategories.Values); container.PresetList.AddRange(uncategorisedPresets); -- cgit v1.2.3