Fixed broken apply profile logic

Changed an AND to a NAND and now it works.
This commit is contained in:
Terry MacDonald 2021-10-09 18:42:50 +13:00
parent 8d060edf83
commit c5b06988dd
4 changed files with 160 additions and 15 deletions

View File

@ -26,8 +26,8 @@ using System.Resources;
[assembly: Guid("e4ceaf5e-ad01-4695-b179-31168eb74c48")] [assembly: Guid("e4ceaf5e-ad01-4695-b179-31168eb74c48")]
// Version information // Version information
[assembly: AssemblyVersion("2.0.1.75")] [assembly: AssemblyVersion("2.0.1.89")]
[assembly: AssemblyFileVersion("2.0.1.75")] [assembly: AssemblyFileVersion("2.0.1.89")]
[assembly: NeutralResourcesLanguageAttribute( "en" )] [assembly: NeutralResourcesLanguageAttribute( "en" )]
[assembly: CLSCompliant(true)] [assembly: CLSCompliant(true)]

View File

@ -402,15 +402,27 @@ namespace DisplayMagician
#pragma warning disable IDE0059 // Unnecessary assignment of a value #pragma warning disable IDE0059 // Unnecessary assignment of a value
List<ShortcutItem> shortcuts = new List<ShortcutItem>(); List<ShortcutItem> shortcuts = new List<ShortcutItem>();
#pragma warning restore IDE0059 // Unnecessary assignment of a value #pragma warning restore IDE0059 // Unnecessary assignment of a value
List<string> jsonErrors = new List<string>();
try try
{ {
_allShortcuts = JsonConvert.DeserializeObject<List<ShortcutItem>>(json, new JsonSerializerSettings
JsonSerializerSettings mySerializerSettings = new JsonSerializerSettings
{ {
MissingMemberHandling = MissingMemberHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Include, 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<List<ShortcutItem>>(json, mySerializerSettings);
} }
catch (Exception ex) 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!"); 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 // Lookup all the Profile Names in the Saved Profiles
// and link the profiles to the Shortcuts as we only // and link the profiles to the Shortcuts as we only
// store the profile names to allow users to uodate profiles // store the profile names to allow users to uodate profiles
@ -499,17 +520,24 @@ namespace DisplayMagician
} }
List<string> jsonErrors = new List<string>();
try try
{ {
logger.Debug($"ShortcutRepository/SaveShortcuts: Converting the objects to JSON format."); 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, NullValueHandling = NullValueHandling.Include,
DefaultValueHandling = DefaultValueHandling.Populate, 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)) if (!string.IsNullOrWhiteSpace(json))
@ -525,6 +553,15 @@ namespace DisplayMagician
logger.Error(ex, $"ShortcutRepository/SaveShortcuts: Unable to save the shortcut repository to the {_shortcutStorageJsonFileName}."); 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; return false;
} }

View File

@ -681,7 +681,7 @@ namespace DisplayMagicianShared
WinLibrary winLibrary = WinLibrary.GetLibrary(); WinLibrary winLibrary = WinLibrary.GetLibrary();
if (nvidiaLibrary.IsInstalled) if (nvidiaLibrary.IsInstalled)
{ {
if (!nvidiaLibrary.IsActiveConfig(_nvidiaDisplayConfig) && !winLibrary.IsActiveConfig(_windowsDisplayConfig)) if (!(nvidiaLibrary.IsActiveConfig(_nvidiaDisplayConfig) && winLibrary.IsActiveConfig(_windowsDisplayConfig)))
{ {
if (nvidiaLibrary.IsPossibleConfig(_nvidiaDisplayConfig)) if (nvidiaLibrary.IsPossibleConfig(_nvidiaDisplayConfig))
{ {
@ -742,7 +742,7 @@ namespace DisplayMagicianShared
WinLibrary winLibrary = WinLibrary.GetLibrary(); WinLibrary winLibrary = WinLibrary.GetLibrary();
if (amdLibrary.IsInstalled) if (amdLibrary.IsInstalled)
{ {
if (!amdLibrary.IsActiveConfig(_amdDisplayConfig) && !winLibrary.IsActiveConfig(_windowsDisplayConfig)) if (!(amdLibrary.IsActiveConfig(_amdDisplayConfig) && winLibrary.IsActiveConfig(_windowsDisplayConfig)))
{ {
if (amdLibrary.IsPossibleConfig(_amdDisplayConfig)) if (amdLibrary.IsPossibleConfig(_amdDisplayConfig))
{ {
@ -1542,6 +1542,7 @@ namespace DisplayMagicianShared
{ {
return (Name ?? Language.UN_TITLED_PROFILE); return (Name ?? Language.UN_TITLED_PROFILE);
} }
} }
} }

View File

@ -11,6 +11,7 @@ using DisplayMagicianShared.AMD;
using DisplayMagicianShared.NVIDIA; using DisplayMagicianShared.NVIDIA;
using DisplayMagicianShared.Windows; using DisplayMagicianShared.Windows;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Newtonsoft.Json.Linq;
namespace DisplayMagicianShared 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."); 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)) if (!string.IsNullOrWhiteSpace(json))
{ {
List<string> jsonErrors = new List<string>();
try try
{ {
_allProfiles = JsonConvert.DeserializeObject<List<ProfileItem>>(json, new JsonSerializerSettings JsonSerializerSettings mySerializerSettings = new JsonSerializerSettings
{ {
MissingMemberHandling = MissingMemberHandling.Error, MissingMemberHandling = MissingMemberHandling.Error,
NullValueHandling = NullValueHandling.Include, NullValueHandling = NullValueHandling.Include,
@ -706,13 +713,33 @@ namespace DisplayMagicianShared
//DefaultValueHandling = DefaultValueHandling.Ignore, //DefaultValueHandling = DefaultValueHandling.Ignore,
TypeNameHandling = TypeNameHandling.Auto, TypeNameHandling = TypeNameHandling.Auto,
ObjectCreationHandling = ObjectCreationHandling.Replace, 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<List<ProfileItem>>(json, mySerializerSettings);
} }
catch (Exception ex) catch (Exception ex)
{ {
SharedLogger.logger.Error(ex, $"ProfileRepository/LoadProfiles: Tried to parse the JSON in the {_profileStorageJsonFileName} but the JsonConvert threw an exception."); 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"); 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) // Go through all the profiles and set up the needed structures (such as the Screens list)
@ -749,6 +776,68 @@ namespace DisplayMagicianShared
IsPossibleRefresh(); IsPossibleRefresh();
return true; 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<uint, NV_COLOR_DATA_V5>();
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() public static bool SaveProfiles()
@ -782,11 +871,14 @@ namespace DisplayMagicianShared
{ {
SharedLogger.logger.Debug($"ProfileRepository/SaveProfiles: Profiles folder {AppProfileStoragePath} exists."); SharedLogger.logger.Debug($"ProfileRepository/SaveProfiles: Profiles folder {AppProfileStoragePath} exists.");
} }
List<string> jsonErrors = new List<string>();
try try
{ {
SharedLogger.logger.Debug($"ProfileRepository/SaveProfiles: Converting the objects to JSON format."); 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.Include,
//NullValueHandling = NullValueHandling.Ignore, //NullValueHandling = NullValueHandling.Ignore,
@ -795,7 +887,13 @@ namespace DisplayMagicianShared
TypeNameHandling = TypeNameHandling.Auto, TypeNameHandling = TypeNameHandling.Auto,
MissingMemberHandling = MissingMemberHandling.Error, MissingMemberHandling = MissingMemberHandling.Error,
ObjectCreationHandling = ObjectCreationHandling.Replace, 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)) 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}."); 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; return false;
} }