diff --git a/DisplayMagician/Program.cs b/DisplayMagician/Program.cs index f5e7148..c16618f 100644 --- a/DisplayMagician/Program.cs +++ b/DisplayMagician/Program.cs @@ -261,31 +261,31 @@ namespace DisplayMagician { { if (forcedVideoLibrary.Value().Equals("NVIDIA")) { - ProfileRepository.ForcedVideoMode = FORCED_VIDEO_MODE.NVIDIA; + ProfileRepository.InitialiseRepository(FORCED_VIDEO_MODE.NVIDIA); Console.WriteLine($"Forcing NVIDIA Video Library as '--force-video-library NVIDIA' was provided on the commandline."); logger.Info($"Forcing NVIDIA Video Library as '--force-video-library NVIDIA' was provided on the commandline."); } else if (forcedVideoLibrary.Value().Equals("AMD")) { - ProfileRepository.ForcedVideoMode = FORCED_VIDEO_MODE.AMD; + ProfileRepository.InitialiseRepository(FORCED_VIDEO_MODE.AMD); Console.WriteLine($"Forcing AMD Video Library as '--force-video-library AMD' was provided on the commandline."); logger.Info($"Forcing AMD Video Library as '--force-video-library AMD' was provided on the commandline."); } else if (forcedVideoLibrary.Value().Equals("Windows")) { - ProfileRepository.ForcedVideoMode = FORCED_VIDEO_MODE.WINDOWS; + ProfileRepository.InitialiseRepository(FORCED_VIDEO_MODE.WINDOWS); Console.WriteLine($"Forcing Windows CCD Video Library as '--force-video-library Windows' was provided on the commandline."); logger.Info($"Forcing Windows CCD Video Library as '--force-video-library Windows' was provided on the commandline."); } else { - ProfileRepository.ForcedVideoMode = FORCED_VIDEO_MODE.DETECT; + ProfileRepository.InitialiseRepository(FORCED_VIDEO_MODE.DETECT); logger.Info($"Leaving DisplayMagician to detect the best Video Library to use."); } } else { - ProfileRepository.ForcedVideoMode = FORCED_VIDEO_MODE.DETECT; + ProfileRepository.InitialiseRepository(FORCED_VIDEO_MODE.DETECT); logger.Info($"Leaving DisplayMagician to detect the best Video Library to use."); } var argumentShortcut = runShortcutCmd.Argument("\"SHORTCUT_UUID\"", "(required) The UUID of the shortcut to run from those stored in the shortcut library.").IsRequired(); @@ -328,31 +328,31 @@ namespace DisplayMagician { { if (forcedVideoLibrary.Value().Equals("NVIDIA")) { - ProfileRepository.ForcedVideoMode = FORCED_VIDEO_MODE.NVIDIA; + ProfileRepository.InitialiseRepository(FORCED_VIDEO_MODE.NVIDIA); Console.WriteLine($"Forcing NVIDIA Video Library as '--force-video-library NVIDIA' was provided on the commandline."); logger.Info($"Forcing NVIDIA Video Library as '--force-video-library NVIDIA' was provided on the commandline."); } else if (forcedVideoLibrary.Value().Equals("AMD")) { - ProfileRepository.ForcedVideoMode = FORCED_VIDEO_MODE.AMD; + ProfileRepository.InitialiseRepository(FORCED_VIDEO_MODE.AMD); Console.WriteLine($"Forcing AMD Video Library as '--force-video-library AMD' was provided on the commandline."); logger.Info($"Forcing AMD Video Library as '--force-video-library AMD' was provided on the commandline."); } else if (forcedVideoLibrary.Value().Equals("Windows")) { - ProfileRepository.ForcedVideoMode = FORCED_VIDEO_MODE.WINDOWS; + ProfileRepository.InitialiseRepository(FORCED_VIDEO_MODE.WINDOWS); Console.WriteLine($"Forcing Windows CCD Video Library as '--force-video-library Windows' was provided on the commandline."); logger.Info($"Forcing Windows CCD Video Library as '--force-video-library Windows' was provided on the commandline."); } else { - ProfileRepository.ForcedVideoMode = FORCED_VIDEO_MODE.DETECT; + ProfileRepository.InitialiseRepository(FORCED_VIDEO_MODE.DETECT); logger.Info($"Leaving DisplayMagician to detect the best Video Library to use."); } } else { - ProfileRepository.ForcedVideoMode = FORCED_VIDEO_MODE.DETECT; + ProfileRepository.InitialiseRepository(FORCED_VIDEO_MODE.DETECT); logger.Info($"Leaving DisplayMagician to detect the best Video Library to use."); } @@ -403,31 +403,31 @@ namespace DisplayMagician { { if (forcedVideoLibrary.Value().Equals("NVIDIA")) { - ProfileRepository.ForcedVideoMode = FORCED_VIDEO_MODE.NVIDIA; + ProfileRepository.InitialiseRepository(FORCED_VIDEO_MODE.NVIDIA); Console.WriteLine($"Forcing NVIDIA Video Library as '--force-video-library NVIDIA' was provided on the commandline."); logger.Info($"Forcing NVIDIA Video Library as '--force-video-library NVIDIA' was provided on the commandline."); } else if (forcedVideoLibrary.Value().Equals("AMD")) { - ProfileRepository.ForcedVideoMode = FORCED_VIDEO_MODE.AMD; + ProfileRepository.InitialiseRepository(FORCED_VIDEO_MODE.AMD); Console.WriteLine($"Forcing AMD Video Library as '--force-video-library AMD' was provided on the commandline."); logger.Info($"Forcing AMD Video Library as '--force-video-library AMD' was provided on the commandline."); } else if (forcedVideoLibrary.Value().Equals("Windows")) { - ProfileRepository.ForcedVideoMode = FORCED_VIDEO_MODE.WINDOWS; + ProfileRepository.InitialiseRepository(FORCED_VIDEO_MODE.WINDOWS); Console.WriteLine($"Forcing Windows CCD Video Library as '--force-video-library Windows' was provided on the commandline."); logger.Info($"Forcing Windows CCD Video Library as '--force-video-library Windows' was provided on the commandline."); } else { - ProfileRepository.ForcedVideoMode = FORCED_VIDEO_MODE.DETECT; + ProfileRepository.InitialiseRepository(FORCED_VIDEO_MODE.DETECT); logger.Info($"Leaving DisplayMagician to detect the best Video Library to use."); } } else { - ProfileRepository.ForcedVideoMode = FORCED_VIDEO_MODE.DETECT; + ProfileRepository.InitialiseRepository(FORCED_VIDEO_MODE.DETECT); logger.Info($"Leaving DisplayMagician to detect the best Video Library to use."); } //description and help text of the command. @@ -465,25 +465,25 @@ namespace DisplayMagician { { if (forcedVideoLibrary.Value().Equals("NVIDIA")) { - ProfileRepository.ForcedVideoMode = FORCED_VIDEO_MODE.NVIDIA; + ProfileRepository.InitialiseRepository(FORCED_VIDEO_MODE.NVIDIA); Console.WriteLine($"Forcing NVIDIA Video Library as '--force-video-library NVIDIA' was provided on the commandline."); logger.Info($"Forcing NVIDIA Video Library as '--force-video-library NVIDIA' was provided on the commandline."); } else if (forcedVideoLibrary.Value().Equals("AMD")) { - ProfileRepository.ForcedVideoMode = FORCED_VIDEO_MODE.AMD; + ProfileRepository.InitialiseRepository(FORCED_VIDEO_MODE.AMD); Console.WriteLine($"Forcing AMD Video Library as '--force-video-library AMD' was provided on the commandline."); logger.Info($"Forcing AMD Video Library as '--force-video-library AMD' was provided on the commandline."); } else if (forcedVideoLibrary.Value().Equals("Windows")) { - ProfileRepository.ForcedVideoMode = FORCED_VIDEO_MODE.WINDOWS; + ProfileRepository.InitialiseRepository(FORCED_VIDEO_MODE.WINDOWS); Console.WriteLine($"Forcing Windows CCD Video Library as '--force-video-library Windows' was provided on the commandline."); logger.Info($"Forcing Windows CCD Video Library as '--force-video-library Windows' was provided on the commandline."); } else { - ProfileRepository.ForcedVideoMode = FORCED_VIDEO_MODE.DETECT; + ProfileRepository.InitialiseRepository(FORCED_VIDEO_MODE.DETECT); logger.Info($"Leaving DisplayMagician to detect the best Video Library to use."); } } diff --git a/DisplayMagicianShared/AMD/AMDProfileItem.cs b/DisplayMagicianShared/AMD/AMDProfileItem.cs index 615b720..f298eeb 100644 --- a/DisplayMagicianShared/AMD/AMDProfileItem.cs +++ b/DisplayMagicianShared/AMD/AMDProfileItem.cs @@ -12,7 +12,7 @@ using System.Drawing.Imaging; namespace DisplayMagicianShared.AMD { - public class AMDProfileItem : ProfileItem, IComparable + public class AMDProfileItem : ProfileItem { private static List _allSavedProfiles = new List(); private ProfileIcon _profileIcon; @@ -363,13 +363,13 @@ namespace DisplayMagicianShared.AMD return _screens; } - public override int CompareTo(object obj) + /*public override int CompareTo(object obj) { if (!(obj is AMDProfileItem)) throw new ArgumentException("Object to CompareTo is not a AMDProfileItem"); ; AMDProfileItem otherProfile = (AMDProfileItem)obj; return this.Name.CompareTo(otherProfile.Name); - } + }*/ // The public override for the Object.Equals public override bool Equals(object obj) diff --git a/DisplayMagicianShared/NVIDIA/NVIDIAProfileItem.cs b/DisplayMagicianShared/NVIDIA/NVIDIAProfileItem.cs index 1b70b43..cdc8d4c 100644 --- a/DisplayMagicianShared/NVIDIA/NVIDIAProfileItem.cs +++ b/DisplayMagicianShared/NVIDIA/NVIDIAProfileItem.cs @@ -14,7 +14,7 @@ using DisplayMagicianShared.Windows; namespace DisplayMagicianShared.NVIDIA { - public class NVIDIAProfileItem : ProfileItem, IComparable + public class NVIDIAProfileItem : ProfileItem { private static List _allSavedProfiles = new List(); private ProfileIcon _profileIcon; @@ -393,7 +393,7 @@ namespace DisplayMagicianShared.NVIDIA } else if (_nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos[i].DisplayCount == 1) { - // This is a single screen + // This is a single screen with an adjoining mosaic screen // Set some basics about the screen uint displayId = _nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos[i].Displays[0].DisplayId; string windowsDisplayName = _nvidiaDisplayConfig.DisplayNames[displayId]; @@ -547,14 +547,7 @@ namespace DisplayMagicianShared.NVIDIA return _screens; } - - public override int CompareTo(object obj) - { - if (!(obj is NVIDIAProfileItem)) throw new ArgumentException("Object to CompareTo is not a NVIDIAProfileItem"); ; - - NVIDIAProfileItem otherProfile = (NVIDIAProfileItem)obj; - return this.Name.CompareTo(otherProfile.Name); - } + // The public override for the Object.Equals public override bool Equals(object obj) diff --git a/DisplayMagicianShared/ProfileItem.cs b/DisplayMagicianShared/ProfileItem.cs index db21865..030cd5b 100644 --- a/DisplayMagicianShared/ProfileItem.cs +++ b/DisplayMagicianShared/ProfileItem.cs @@ -52,7 +52,7 @@ namespace DisplayMagicianShared public int Row; } - public class ProfileItem : IComparable + public class ProfileItem : IComparable { private static List _allSavedProfiles = new List(); private ProfileIcon _profileIcon; @@ -504,14 +504,74 @@ namespace DisplayMagicianShared return new List(); } - public virtual int CompareTo(object obj) + /*public virtual int CompareTo(object obj) { if (!(obj is ProfileItem)) throw new ArgumentException("Object to CompareTo is not a Shortcut"); ; ProfileItem otherProfile = (ProfileItem)obj; return this.Name.CompareTo(otherProfile.Name); + }*/ + + public int CompareTo(ProfileItem other) + { + + int result = CompareToValues(other); + + // If comparison based solely on values + // returns zero, indicating that two instances + // are equal in those fields they have in common, + // only then we break the tie by comparing + // data types of the two instances. + if (result == 0) + result = CompareTypes(other); + + return result; + } + protected virtual int CompareToValues(ProfileItem other) + { + + if (object.ReferenceEquals(other, null)) + return 1; // All instances are greater than null + + // Base class simply compares Mark properties + return Name.CompareTo(other.Name); + + } + + protected int CompareTypes(ProfileItem other) + { + + // Base type is considered less than derived type + // when two instances have the same values of + // base fields. + + // Instances of two distinct derived types are + // ordered by comparing full names of their + // types when base fields are equal. + // This is consistent comparison rule for all + // instances of the two derived types. + + int result = 0; + + Type thisType = this.GetType(); + Type otherType = other.GetType(); + + if (otherType.IsSubclassOf(thisType)) + result = -1; // other is subclass of this class + else if (thisType.IsSubclassOf(otherType)) + result = 1; // this is subclass of other class + else if (thisType != otherType) + result = thisType.FullName.CompareTo(otherType.FullName); + // cut the tie with a test that returns + // the same value for all objects + + return result; + + } + + // The public override for the Object.Equals public override bool Equals(object obj) { @@ -524,7 +584,7 @@ namespace DisplayMagicianShared { return !object.ReferenceEquals(obj, null) && obj is ProfileItem && - ((ProfileItem)obj).ProfileDisplayIdentifiers == this.ProfileDisplayIdentifiers; + ((ProfileItem)obj).ProfileDisplayIdentifiers.SequenceEqual(this.ProfileDisplayIdentifiers); } // If Equals() returns true for this object compared to another diff --git a/DisplayMagicianShared/ProfileRepository.cs b/DisplayMagicianShared/ProfileRepository.cs index 6814b4c..31e7e14 100644 --- a/DisplayMagicianShared/ProfileRepository.cs +++ b/DisplayMagicianShared/ProfileRepository.cs @@ -102,8 +102,6 @@ namespace DisplayMagicianShared { SharedLogger.logger.Warn(ex, $"ProfileRepository/ProfileRepository: Exception creating the Profiles storage folder."); } - // Load the Profiles from storage - LoadProfiles(); } #endregion @@ -221,6 +219,22 @@ namespace DisplayMagicianShared #endregion #region Class Methods + public static bool InitialiseRepository(FORCED_VIDEO_MODE forcedVideoMode = FORCED_VIDEO_MODE.DETECT) + { + if (!SetVideoCardMode(forcedVideoMode)) + { + return false; + } + + if (!LoadProfiles()) + { + return false; + } + + return true; + } + + public static bool AddProfile(ProfileItem profile) { if (!(profile is ProfileItem)) diff --git a/DisplayMagicianShared/Windows/WinProfileItem.cs b/DisplayMagicianShared/Windows/WinProfileItem.cs index 40f277e..e363ed6 100644 --- a/DisplayMagicianShared/Windows/WinProfileItem.cs +++ b/DisplayMagicianShared/Windows/WinProfileItem.cs @@ -11,7 +11,7 @@ using System.Drawing.Imaging; namespace DisplayMagicianShared.Windows { - public class WinProfileItem : ProfileItem, IComparable + public class WinProfileItem : ProfileItem { private static List _allSavedProfiles = new List(); private ProfileIcon _profileIcon; @@ -318,13 +318,13 @@ namespace DisplayMagicianShared.Windows return _screens; } - public override int CompareTo(object obj) + /*public override int CompareTo(object obj) { if (!(obj is WinProfileItem)) throw new ArgumentException("Object to CompareTo is not a WinProfileItem"); ; WinProfileItem otherProfile = (WinProfileItem)obj; return this.Name.CompareTo(otherProfile.Name); - } + }*/ // The public override for the Object.Equals public override bool Equals(object obj)