From 11cd923e46ba2800ee4b4660996b67c078717c38 Mon Sep 17 00:00:00 2001 From: Terry MacDonald Date: Fri, 26 Mar 2021 21:54:45 +1300 Subject: [PATCH] Changed profile matching logic Relaxed the profile matching logic to use my looser displayidentifier logic as it is more likely that connections to a display is changed rather thant the display paths, resolution or anything like that. Will use this as an experiment to see if it works or not. Also fixed the shortcut renaming errors by searching for the UUID, not the shortcut name which is the pretty much the way I should have done it in the first place! --- DisplayMagician/ShortcutRepository.cs | 14 +++-- .../UIForms/ImageListViewRenderers.cs | 2 +- DisplayMagician/UIForms/ShortcutAdaptor.cs | 4 +- .../UIForms/ShortcutLibraryForm.cs | 14 +++-- DisplayMagicianShared/ProfileItem.cs | 56 +++++++++++++++++-- DisplayMagicianShared/ProfileRepository.cs | 42 ++++++++++++-- 6 files changed, 111 insertions(+), 21 deletions(-) diff --git a/DisplayMagician/ShortcutRepository.cs b/DisplayMagician/ShortcutRepository.cs index 720cda2..7919ecf 100644 --- a/DisplayMagician/ShortcutRepository.cs +++ b/DisplayMagician/ShortcutRepository.cs @@ -441,20 +441,26 @@ namespace DisplayMagician logger.Debug($"ShortcutRepository/LoadShortcuts: Connecting Shortcut profile names to the real profile objects"); foreach (ShortcutItem updatedShortcut in _allShortcuts) { + bool foundProfile = false; foreach (ProfileItem profile in ProfileRepository.AllProfiles) { - if (profile.Equals(updatedShortcut.ProfileToUse)) + if (profile.UUID.Equals(updatedShortcut.ProfileUUID)) { // And assign the matching Profile if we find it. updatedShortcut.ProfileToUse = profile; + foundProfile = true; + logger.Debug($"ShortcutRepository/LoadShortcuts: Found the profile with UUID {updatedShortcut.ProfileUUID} and linked it to a profile!"); break; } } - // We should only get here if there isn't a profile to match to. - logger.Debug($"ShortcutRepository/LoadShortcuts: Couldn't find the profile with UUID {updatedShortcut.ProfileUUID} so couldn't link it to a profile! We can't use this shortcut."); - updatedShortcut.ProfileToUse = null; + if (!foundProfile) + { + // We should only get here if there isn't a profile to match to. + logger.Debug($"ShortcutRepository/LoadShortcuts: Couldn't find the profile with UUID {updatedShortcut.ProfileUUID} so couldn't link it to a profile! We can't use this shortcut."); + updatedShortcut.ProfileToUse = null; + } } // Sort the shortcuts alphabetically diff --git a/DisplayMagician/UIForms/ImageListViewRenderers.cs b/DisplayMagician/UIForms/ImageListViewRenderers.cs index 9689c67..e0626f2 100644 --- a/DisplayMagician/UIForms/ImageListViewRenderers.cs +++ b/DisplayMagician/UIForms/ImageListViewRenderers.cs @@ -96,7 +96,7 @@ namespace DisplayMagician.UIForms { Rectangle pos = Utility.GetSizedImageBounds(img, new Rectangle(bounds.Location + itemPadding, ImageListView.ThumbnailSize)); - ShortcutItem shortcutToRender = ShortcutRepository.GetShortcut(item.Text); + ShortcutItem shortcutToRender = ShortcutRepository.GetShortcut(item.EquipmentModel); if (shortcutToRender.IsValid == ShortcutValidity.Error) { // The shortcut is permanently invalid (game removed or profile deleted) diff --git a/DisplayMagician/UIForms/ShortcutAdaptor.cs b/DisplayMagician/UIForms/ShortcutAdaptor.cs index c92b6b7..b7bb75f 100644 --- a/DisplayMagician/UIForms/ShortcutAdaptor.cs +++ b/DisplayMagician/UIForms/ShortcutAdaptor.cs @@ -77,7 +77,7 @@ namespace DisplayMagician.UIForms ShortcutItem shortcut = (ShortcutItem) key; //return shortcut.Name; - return shortcut.Name; + return shortcut.UUID; } catch (Exception ex) { @@ -181,7 +181,7 @@ namespace DisplayMagician.UIForms details.Add(new Utility.Tuple(ColumnType.Dimensions, string.Empty, mySize)); details.Add(new Utility.Tuple(ColumnType.Resolution, string.Empty, mySizeF)); details.Add(new Utility.Tuple(ColumnType.ImageDescription, string.Empty, name ?? "")); - details.Add(new Utility.Tuple(ColumnType.EquipmentModel, string.Empty, "")); + details.Add(new Utility.Tuple(ColumnType.EquipmentModel, string.Empty, shortcut.UUID)); details.Add(new Utility.Tuple(ColumnType.DateTaken, string.Empty, now)); details.Add(new Utility.Tuple(ColumnType.Artist, string.Empty, "")); details.Add(new Utility.Tuple(ColumnType.Copyright, string.Empty, "")); diff --git a/DisplayMagician/UIForms/ShortcutLibraryForm.cs b/DisplayMagician/UIForms/ShortcutLibraryForm.cs index a6c06a0..e2ffc64 100644 --- a/DisplayMagician/UIForms/ShortcutLibraryForm.cs +++ b/DisplayMagician/UIForms/ShortcutLibraryForm.cs @@ -100,6 +100,11 @@ namespace DisplayMagician.UIForms return (from item in ShortcutRepository.AllShortcuts where item.Name == shortcutName select item).First(); } + private ShortcutItem GetShortcutFromUUID(string shortcutUUID) + { + return (from item in ShortcutRepository.AllShortcuts where item.UUID == shortcutUUID select item).First(); + } + private void btn_save_Click(object sender, EventArgs e) { //DialogResult = DialogResult.None; @@ -252,8 +257,8 @@ namespace DisplayMagician.UIForms private void btn_edit_Click(object sender, EventArgs e) { int currentIlvIndex = ilv_saved_shortcuts.SelectedItems[0].Index; - string shortcutName = ilv_saved_shortcuts.Items[currentIlvIndex].Text; - _selectedShortcut = GetShortcutFromName(shortcutName); + string shortcutUUID = ilv_saved_shortcuts.Items[currentIlvIndex].EquipmentModel; + _selectedShortcut = GetShortcutFromUUID(shortcutUUID); if (_selectedShortcut == null) return; @@ -262,9 +267,10 @@ namespace DisplayMagician.UIForms // We need to stop ImageListView redrawing things before we're ready // This stops an exception when ILV is just too keen! - ilv_saved_shortcuts.SuspendLayout(); + - var shortcutForm = new ShortcutForm(_selectedShortcut); + var shortcutForm = new ShortcutForm(_selectedShortcut); + //ilv_saved_shortcuts.SuspendLayout(); shortcutForm.ShowDialog(this); if (shortcutForm.DialogResult == DialogResult.OK) { diff --git a/DisplayMagicianShared/ProfileItem.cs b/DisplayMagicianShared/ProfileItem.cs index 918040f..5b15ba4 100644 --- a/DisplayMagicianShared/ProfileItem.cs +++ b/DisplayMagicianShared/ProfileItem.cs @@ -331,7 +331,8 @@ namespace DisplayMagicianShared // We need to exclude the name as the name is solely for saving to disk // and displaying to the user. // Two profiles are equal only when they have the same viewport data - if (Paths.SequenceEqual(other.Paths)) + //if (Paths.SequenceEqual(other.Paths)) + if (ProfileDisplayIdentifiers.SequenceEqual(other.ProfileDisplayIdentifiers)) return true; else return false; @@ -339,7 +340,7 @@ namespace DisplayMagicianShared // If Equals() returns true for this object compared to another // then GetHashCode() must return the same value for these objects. - public override int GetHashCode() + /*public override int GetHashCode() { // Get hash code for the Viewports field if it is not null. @@ -348,8 +349,18 @@ namespace DisplayMagicianShared //Calculate the hash code for the product. return hashPaths; - } + }*/ + public override int GetHashCode() + { + // Get hash code for the ProfileDisplayIdentifiers field if it is not null. + int hashPaths = ProfileDisplayIdentifiers == null ? 0 : ProfileDisplayIdentifiers.GetHashCode(); + + //Calculate the hash code for the product. + return hashPaths; + + } + public override string ToString() { @@ -462,7 +473,7 @@ namespace DisplayMagicianShared class ProfileComparer : IEqualityComparer { // Products are equal if their names and product numbers are equal. - public bool Equals(ProfileItem x, ProfileItem y) + /*public bool Equals(ProfileItem x, ProfileItem y) { //Check whether the compared objects reference the same data. @@ -480,11 +491,31 @@ namespace DisplayMagicianShared return true; else return false; + }*/ + + public bool Equals(ProfileItem x, ProfileItem y) + { + + //Check whether the compared objects reference the same data. + if (Object.ReferenceEquals(x, y)) return true; + + //Check whether any of the compared objects is null. + if (x is null || y is null) + return false; + + // Check whether the profiles' properties are equal + // We need to exclude the name as the name is solely for saving to disk + // and displaying to the user. + // Two profiles are equal only when they have the same viewport data + if (x.ProfileDisplayIdentifiers.SequenceEqual(y.ProfileDisplayIdentifiers)) + return true; + else + return false; } // If Equals() returns true for a pair of objects // then GetHashCode() must return the same value for these objects. - public int GetHashCode(ProfileItem profile) + /*public int GetHashCode(ProfileItem profile) { // Check whether the object is null @@ -496,7 +527,20 @@ namespace DisplayMagicianShared //Calculate the hash code for the product. return hashPaths; - } + }*/ + // Modified the GetHashCode to compare the displayidentifier + public int GetHashCode(ProfileItem profile) + { + // Check whether the object is null + if (profile is null) return 0; + + // Get hash code for the ProfileDisplayIdentifiers field if it is not null. + int hashPaths = profile.ProfileDisplayIdentifiers == null ? 0 : profile.ProfileDisplayIdentifiers.GetHashCode(); + + //Calculate the hash code for the product. + return hashPaths; + + } } } \ No newline at end of file diff --git a/DisplayMagicianShared/ProfileRepository.cs b/DisplayMagicianShared/ProfileRepository.cs index a26d36a..e99bb28 100644 --- a/DisplayMagicianShared/ProfileRepository.cs +++ b/DisplayMagicianShared/ProfileRepository.cs @@ -47,10 +47,10 @@ namespace DisplayMagicianShared SharedLogger.logger.Debug($"ProfileRepository/ProfileRepository: Initialising the NvAPIWrapper.NVIDIA library."); NvAPIWrapper.NVIDIA.Initialize(); - SharedLogger.logger.Debug($"ProfileRepository/ProfileRepository: Creating the Profiles storage folder {AppProfileStoragePath}."); // Create the Profile Storage Path if it doesn't exist so that it's avilable for all the program if (!Directory.Exists(AppProfileStoragePath)) { + SharedLogger.logger.Debug($"ProfileRepository/ProfileRepository: Creating the Profiles storage folder {AppProfileStoragePath}."); Directory.CreateDirectory(AppProfileStoragePath); } } @@ -495,7 +495,7 @@ namespace DisplayMagicianShared } - public static void UpdateActiveProfile() + /*public static void UpdateActiveProfile() { SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: Updating the profile currently active (in use now)."); @@ -526,9 +526,42 @@ namespace DisplayMagicianShared //IsPossibleRefresh(); - } + }*/ + public static void UpdateActiveProfile() + { + + SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: Updating the profile currently active (in use now)."); + + ProfileItem activeProfile = new ProfileItem + { + Name = "Current Display Profile", + Paths = PathInfo.GetActivePaths().Select(info => new DisplayMagicianShared.Topology.Path(info)).ToArray() + }; + + activeProfile.ProfileIcon = new ProfileIcon(activeProfile); + activeProfile.ProfileBitmap = activeProfile.ProfileIcon.ToBitmap(256, 256); + + if (_profilesLoaded && _allProfiles.Count > 0) + { + foreach (ProfileItem loadedProfile in ProfileRepository.AllProfiles) + { + if (activeProfile.Equals(loadedProfile)) + { + _currentProfile = loadedProfile; + SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The profile {loadedProfile.Name} is currently active (in use now)."); + return; + } + } + } + SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The current profile is a new profile that doesn't already exist in the Profile Repository."); + _currentProfile = activeProfile; + + //IsPossibleRefresh(); + + } + public static ProfileItem GetActiveProfile() { if (!(_currentProfile is ProfileItem)) @@ -549,7 +582,8 @@ namespace DisplayMagicianShared SharedLogger.logger.Debug($"ProfileRepository/IsActiveProfile: Checking whether the profile {profile.Name} is the currently active profile."); - if (profile.Paths.SequenceEqual(_currentProfile.Paths)) + //if (profile.Paths.SequenceEqual(_currentProfile.Paths)) + if (profile.Equals(_currentProfile)) { SharedLogger.logger.Debug($"ProfileRepository/IsActiveProfile: The profile {profile.Name} is the currently active profile."); return true;