From c5b06988dd0437dbf28dcef45e38945b26785a67 Mon Sep 17 00:00:00 2001 From: Terry MacDonald Date: Sat, 9 Oct 2021 18:42:50 +1300 Subject: [PATCH] Fixed broken apply profile logic Changed an AND to a NAND and now it works. --- DisplayMagician/Properties/AssemblyInfo.cs | 4 +- DisplayMagician/ShortcutRepository.cs | 51 +++++++-- DisplayMagicianShared/ProfileItem.cs | 5 +- DisplayMagicianShared/ProfileRepository.cs | 115 ++++++++++++++++++++- 4 files changed, 160 insertions(+), 15 deletions(-) diff --git a/DisplayMagician/Properties/AssemblyInfo.cs b/DisplayMagician/Properties/AssemblyInfo.cs index 05e5439..199f48f 100644 --- a/DisplayMagician/Properties/AssemblyInfo.cs +++ b/DisplayMagician/Properties/AssemblyInfo.cs @@ -26,8 +26,8 @@ using System.Resources; [assembly: Guid("e4ceaf5e-ad01-4695-b179-31168eb74c48")] // Version information -[assembly: AssemblyVersion("2.0.1.75")] -[assembly: AssemblyFileVersion("2.0.1.75")] +[assembly: AssemblyVersion("2.0.1.89")] +[assembly: AssemblyFileVersion("2.0.1.89")] [assembly: NeutralResourcesLanguageAttribute( "en" )] [assembly: CLSCompliant(true)] diff --git a/DisplayMagician/ShortcutRepository.cs b/DisplayMagician/ShortcutRepository.cs index 37d85c3..b1edbf3 100644 --- a/DisplayMagician/ShortcutRepository.cs +++ b/DisplayMagician/ShortcutRepository.cs @@ -402,15 +402,27 @@ namespace DisplayMagician #pragma warning disable IDE0059 // Unnecessary assignment of a value List shortcuts = new List(); #pragma warning restore IDE0059 // Unnecessary assignment of a value + + List jsonErrors = new List(); try { - _allShortcuts = JsonConvert.DeserializeObject>(json, new JsonSerializerSettings + + + JsonSerializerSettings mySerializerSettings = new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Ignore, NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Include, - TypeNameHandling = TypeNameHandling.Auto - }); + TypeNameHandling = TypeNameHandling.Auto, + Error = delegate (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args) + { + jsonErrors.Add($"JSON.net Error: {args.ErrorContext.Error.Source}:{args.ErrorContext.Error.StackTrace} - {args.ErrorContext.Error.Message} | InnerException:{args.ErrorContext.Error.InnerException.Source}:{args.ErrorContext.Error.InnerException.StackTrace} - {args.ErrorContext.Error.InnerException.Message}"); + args.ErrorContext.Handled = true; + }, + }; + + _allShortcuts = JsonConvert.DeserializeObject>(json, mySerializerSettings); + } catch (Exception ex) { @@ -418,6 +430,15 @@ namespace DisplayMagician throw new Exception("ShortcutRepository/LoadShortcuts: Tried to parse the JSON in the {_shortcutStorageJsonFileName} but the JsonConvert threw an exception. There is an error in the SHortcut JSON file!"); } + // If we have any JSON.net errors, then we need to records them in the logs + if (jsonErrors.Count > 0) + { + foreach (string jsonError in jsonErrors) + { + logger.Error($"ShortcutRepository/LoadShortcuts: {jsonErrors}"); + } + } + // Lookup all the Profile Names in the Saved Profiles // and link the profiles to the Shortcuts as we only // store the profile names to allow users to uodate profiles @@ -499,17 +520,24 @@ namespace DisplayMagician } + List jsonErrors = new List(); + try { logger.Debug($"ShortcutRepository/SaveShortcuts: Converting the objects to JSON format."); - var json = JsonConvert.SerializeObject(_allShortcuts, Formatting.Indented, new JsonSerializerSettings + JsonSerializerSettings mySerializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include, DefaultValueHandling = DefaultValueHandling.Populate, - TypeNameHandling = TypeNameHandling.Auto - - }); + TypeNameHandling = TypeNameHandling.Auto, + Error = delegate (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args) + { + jsonErrors.Add($"JSON.net Error: {args.ErrorContext.Error.Source}:{args.ErrorContext.Error.StackTrace} - {args.ErrorContext.Error.Message} | InnerException:{args.ErrorContext.Error.InnerException.Source}:{args.ErrorContext.Error.InnerException.StackTrace} - {args.ErrorContext.Error.InnerException.Message}"); + args.ErrorContext.Handled = true; + }, + }; + var json = JsonConvert.SerializeObject(_allShortcuts, Formatting.Indented, mySerializerSettings); if (!string.IsNullOrWhiteSpace(json)) @@ -525,6 +553,15 @@ namespace DisplayMagician logger.Error(ex, $"ShortcutRepository/SaveShortcuts: Unable to save the shortcut repository to the {_shortcutStorageJsonFileName}."); } + // If we have any JSON.net errors, then we need to records them in the logs + if (jsonErrors.Count > 0) + { + foreach (string jsonError in jsonErrors) + { + logger.Error($"ProfileRepository/SaveProfiles: {jsonErrors}"); + } + } + return false; } diff --git a/DisplayMagicianShared/ProfileItem.cs b/DisplayMagicianShared/ProfileItem.cs index 4d7946f..7842d09 100644 --- a/DisplayMagicianShared/ProfileItem.cs +++ b/DisplayMagicianShared/ProfileItem.cs @@ -681,7 +681,7 @@ namespace DisplayMagicianShared WinLibrary winLibrary = WinLibrary.GetLibrary(); if (nvidiaLibrary.IsInstalled) { - if (!nvidiaLibrary.IsActiveConfig(_nvidiaDisplayConfig) && !winLibrary.IsActiveConfig(_windowsDisplayConfig)) + if (!(nvidiaLibrary.IsActiveConfig(_nvidiaDisplayConfig) && winLibrary.IsActiveConfig(_windowsDisplayConfig))) { if (nvidiaLibrary.IsPossibleConfig(_nvidiaDisplayConfig)) { @@ -742,7 +742,7 @@ namespace DisplayMagicianShared WinLibrary winLibrary = WinLibrary.GetLibrary(); if (amdLibrary.IsInstalled) { - if (!amdLibrary.IsActiveConfig(_amdDisplayConfig) && !winLibrary.IsActiveConfig(_windowsDisplayConfig)) + if (!(amdLibrary.IsActiveConfig(_amdDisplayConfig) && winLibrary.IsActiveConfig(_windowsDisplayConfig))) { if (amdLibrary.IsPossibleConfig(_amdDisplayConfig)) { @@ -1542,6 +1542,7 @@ namespace DisplayMagicianShared { return (Name ?? Language.UN_TITLED_PROFILE); } + } } diff --git a/DisplayMagicianShared/ProfileRepository.cs b/DisplayMagicianShared/ProfileRepository.cs index 00dcd0d..ec773a1 100644 --- a/DisplayMagicianShared/ProfileRepository.cs +++ b/DisplayMagicianShared/ProfileRepository.cs @@ -11,6 +11,7 @@ using DisplayMagicianShared.AMD; using DisplayMagicianShared.NVIDIA; using DisplayMagicianShared.Windows; using System.Runtime.Serialization; +using Newtonsoft.Json.Linq; namespace DisplayMagicianShared { @@ -693,11 +694,17 @@ namespace DisplayMagicianShared SharedLogger.logger.Error(ex, $"ProfileRepository/LoadProfiles: Tried to read the JSON file {_profileStorageJsonFileName} to memory but File.ReadAllTextthrew an exception."); } + // Migrate any previous entries to the latest version of the file format to the latest one + json = MigrateJsonToLatestVersion(json); + + if (!string.IsNullOrWhiteSpace(json)) { + List jsonErrors = new List(); + try { - _allProfiles = JsonConvert.DeserializeObject>(json, new JsonSerializerSettings + JsonSerializerSettings mySerializerSettings = new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error, NullValueHandling = NullValueHandling.Include, @@ -706,13 +713,33 @@ namespace DisplayMagicianShared //DefaultValueHandling = DefaultValueHandling.Ignore, TypeNameHandling = TypeNameHandling.Auto, ObjectCreationHandling = ObjectCreationHandling.Replace, - }); + + Error = delegate (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args) + { + jsonErrors.Add($"JSON.net Error: {args.ErrorContext.Error.Source}:{args.ErrorContext.Error.StackTrace} - {args.ErrorContext.Error.Message} | InnerException:{args.ErrorContext.Error.InnerException.Source}:{args.ErrorContext.Error.InnerException.StackTrace} - {args.ErrorContext.Error.InnerException.Message}"); + args.ErrorContext.Handled = true; + }, + }; + _allProfiles = JsonConvert.DeserializeObject>(json, mySerializerSettings); + } catch (Exception ex) { SharedLogger.logger.Error(ex, $"ProfileRepository/LoadProfiles: Tried to parse the JSON in the {_profileStorageJsonFileName} but the JsonConvert threw an exception."); } + // If we have any JSON.net errors, then we need to records them in the logs + if (jsonErrors.Count > 0) + { + foreach (string jsonError in jsonErrors) + { + SharedLogger.logger.Error($"ProfileRepository/LoadProfiles: {jsonErrors}"); + } + } + + // We need to try and + + SharedLogger.logger.Debug($"ProfileRepository/LoadProfiles: Finding the current profile in the Profile Repository"); // Go through all the profiles and set up the needed structures (such as the Screens list) @@ -749,6 +776,68 @@ namespace DisplayMagicianShared IsPossibleRefresh(); return true; + } + + public static string MigrateJsonToLatestVersion(string json) + { + + bool changedJson = false; + JArray root = new JArray(); + try + { + SharedLogger.logger.Trace($"ProfileRepository/MigrateJsonToLatestVersion: Processing the Profiles json data to migrate any older feature to the latest version."); + root = JArray.Parse(json); + } + catch(JsonReaderException ex) + { + SharedLogger.logger.Error($"ProfileRepository/MigrateJsonToLatestVersion: JSONReaderException while trying to process the Profiles json data to migrate any older feature to the latest version."); + } + catch(Exception ex) + { + SharedLogger.logger.Error($"ProfileRepository/MigrateJsonToLatestVersion: Exception while trying to process the Profiles json data to migrate any older feature to the latest version."); + } + + + // Now we try and add a default NVIDIA Color Settings if there isn't one + try + { + SharedLogger.logger.Trace($"ProfileRepository/MigrateJsonToLatestVersion: Looking for missing NVIDIA Color Config."); + + // Create a default object + NVIDIA_DISPLAY_CONFIG myDefaultConfig = new NVIDIA_DISPLAY_CONFIG(); + myDefaultConfig.ColorConfig.ColorData = new Dictionary(); + JObject defaultColorConfig = (JObject)JToken.FromObject(myDefaultConfig.ColorConfig); + + for (int i=0; i < root.Count; i++) + { + JObject profile = (JObject)root[i]; + JObject result = (JObject)profile.SelectToken("NVIDIADisplayConfig.ColorConfig.ColorData"); + if (result == null) + { + + JObject NVIDIADisplayConfig = (JObject)profile.SelectToken("NVIDIADisplayConfig"); + NVIDIADisplayConfig.Add("ColorConfig",defaultColorConfig); + changedJson = true; + } + } + } + catch (JsonReaderException ex) + { + SharedLogger.logger.Error($"ProfileRepository/MigrateJsonToLatestVersion: JSONReaderException while trying to process the Profiles json data to migrate any older feature to the latest version."); + } + catch (Exception ex) + { + SharedLogger.logger.Error($"ProfileRepository/MigrateJsonToLatestVersion: Exception while trying to process the Profiles json data to migrate any older feature to the latest version."); + } + + // Now write the changed json to the json string but only if we've changed something + if (changedJson) + { + json = root.ToString(Formatting.Indented); + } + + + return json; } public static bool SaveProfiles() @@ -782,11 +871,14 @@ namespace DisplayMagicianShared { SharedLogger.logger.Debug($"ProfileRepository/SaveProfiles: Profiles folder {AppProfileStoragePath} exists."); } + + List jsonErrors = new List(); + try { SharedLogger.logger.Debug($"ProfileRepository/SaveProfiles: Converting the objects to JSON format."); - var json = JsonConvert.SerializeObject(_allProfiles, Formatting.Indented, new JsonSerializerSettings + JsonSerializerSettings mySerializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include, //NullValueHandling = NullValueHandling.Ignore, @@ -795,7 +887,13 @@ namespace DisplayMagicianShared TypeNameHandling = TypeNameHandling.Auto, MissingMemberHandling = MissingMemberHandling.Error, ObjectCreationHandling = ObjectCreationHandling.Replace, - }); + Error = delegate (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args) + { + jsonErrors.Add($"JSON.net Error: {args.ErrorContext.Error.Source}:{args.ErrorContext.Error.StackTrace} - {args.ErrorContext.Error.Message} | InnerException:{args.ErrorContext.Error.InnerException.Source}:{args.ErrorContext.Error.InnerException.StackTrace} - {args.ErrorContext.Error.InnerException.Message}"); + args.ErrorContext.Handled = true; + }, + }; + var json = JsonConvert.SerializeObject(_allProfiles, Formatting.Indented, mySerializerSettings); if (!string.IsNullOrWhiteSpace(json)) @@ -811,6 +909,15 @@ namespace DisplayMagicianShared SharedLogger.logger.Error(ex, $"ProfileRepository/SaveProfiles: Unable to save the profile repository to the {_profileStorageJsonFileName}."); } + // If we have any JSON.net errors, then we need to records them in the logs + if (jsonErrors.Count > 0) + { + foreach (string jsonError in jsonErrors) + { + SharedLogger.logger.Error($"ProfileRepository/SaveProfiles: {jsonErrors}"); + } + } + return false; }