From 720ddd8615a30c7545fe59a8c5cd4983af044522 Mon Sep 17 00:00:00 2001 From: Paul Hebble Date: Sat, 23 Dec 2023 18:13:05 -0600 Subject: [PATCH] Fix compatible popup messing with max game version column --- Core/Games/KerbalSpaceProgram.cs | 25 +++++--- Core/Repositories/AvailableModule.cs | 2 +- GUI/Dialogs/CompatibleGameVersionsDialog.cs | 67 ++++++++------------- GUI/Model/GUIMod.cs | 6 ++ 4 files changed, 49 insertions(+), 51 deletions(-) diff --git a/Core/Games/KerbalSpaceProgram.cs b/Core/Games/KerbalSpaceProgram.cs index 9a4879a210..e0a41d4e67 100644 --- a/Core/Games/KerbalSpaceProgram.cs +++ b/Core/Games/KerbalSpaceProgram.cs @@ -213,20 +213,29 @@ public void RefreshVersions() private List versions; + private readonly object versionMutex = new object(); + public List KnownVersions { get { - // There's a lot of duplicate real versions with different build IDs, - // skip all those extra checks when we use these if (versions == null) { - versions = ServiceLocator.Container - .Resolve() - .KnownVersions - .Select(v => v.WithoutBuild) - .Distinct() - .ToList(); + lock (versionMutex) + { + if (versions == null) + { + // There's a lot of duplicate real versions with different build IDs, + // skip all those extra checks when we use these + versions = ServiceLocator.Container + .Resolve() + .KnownVersions + .Select(v => v.WithoutBuild) + .Distinct() + .OrderBy(v => v) + .ToList(); + } + } } return versions; } diff --git a/Core/Repositories/AvailableModule.cs b/Core/Repositories/AvailableModule.cs index f11f6dec27..85c564e2bd 100644 --- a/Core/Repositories/AvailableModule.cs +++ b/Core/Repositories/AvailableModule.cs @@ -183,7 +183,7 @@ public GameVersion LatestCompatibleGameVersion(List realVersions) { // Cheat slightly for performance: // Find the CkanModule with the highest ksp_version_max, - // then get the real lastest compatible of just that one mod + // then get the real latest compatible of just that one mod GameVersion best = null; CkanModule bestMod = null; foreach (var mod in module_version.Values) diff --git a/GUI/Dialogs/CompatibleGameVersionsDialog.cs b/GUI/Dialogs/CompatibleGameVersionsDialog.cs index 24f133baa8..64a9d7205f 100644 --- a/GUI/Dialogs/CompatibleGameVersionsDialog.cs +++ b/GUI/Dialogs/CompatibleGameVersionsDialog.cs @@ -35,14 +35,15 @@ public CompatibleGameVersionsDialog(GameInstance inst, bool centerScreen) StartPosition = FormStartPosition.CenterScreen; } - List compatibleVersions = inst.GetCompatibleVersions(); + var compatibleVersions = inst.GetCompatibleVersions(); GameVersionLabel.Text = inst.Version()?.ToString() ?? Properties.Resources.CompatibleGameVersionsDialogNone; GameLocationLabel.Text = inst.GameDir().Replace('/', Path.DirectorySeparatorChar); - List knownVersions = inst.game.KnownVersions; - List majorVersionsList = CreateMajorVersionsList(knownVersions); - List compatibleVersionsLeftOthers = new List(compatibleVersions); - compatibleVersionsLeftOthers.RemoveAll((el)=>knownVersions.Contains(el) || majorVersionsList.Contains(el)); + var knownVersions = inst.game.KnownVersions; + var majorVersionsList = CreateMajorVersionsList(knownVersions); + var compatibleVersionsLeftOthers = compatibleVersions.Except(knownVersions) + .Except(majorVersionsList) + .ToList(); SortAndAddVersionsToList(compatibleVersionsLeftOthers, compatibleVersions); SortAndAddVersionsToList(majorVersionsList, compatibleVersions); @@ -74,37 +75,23 @@ private void CompatibleGameVersionsDialog_Shown(object sender, EventArgs e) GameVersionLabel.Text = string.Format( Properties.Resources.CompatibleGameVersionsDialogVersionDetails, _inst.Version(), - _inst.GameVersionWhenCompatibleVersionsWereStored - ); + _inst.GameVersionWhenCompatibleVersionsWereStored); GameVersionLabel.ForeColor = Color.Red; } } private static List CreateMajorVersionsList(List knownVersions) - { - Dictionary majorVersions = new Dictionary(); - foreach (var version in knownVersions) - { - GameVersion fullKnownVersion = version.ToVersionRange().Lower.Value; - GameVersion toAdd = new GameVersion(fullKnownVersion.Major, fullKnownVersion.Minor); - if (!majorVersions.ContainsKey(toAdd)) - { - majorVersions.Add(toAdd, true); - } - } - return new List(majorVersions.Keys); - } + => knownVersions.Select(v => v.ToVersionRange().Lower.Value) + .Select(v => new GameVersion(v.Major, v.Minor)) + .Distinct() + .ToList(); private void SortAndAddVersionsToList(List versions, List compatibleVersions) { - versions.Sort(); - versions.Reverse(); - foreach (GameVersion version in versions) + foreach (var version in versions.Where(v => v != _inst.Version()) + .Reverse()) { - if (!version.Equals(_inst.Version())) - { - SelectedVersionsCheckedListBox.Items.Add(version, compatibleVersions.Contains(version)); - } + SelectedVersionsCheckedListBox.Items.Add(version, compatibleVersions.Contains(version)); } } @@ -116,12 +103,10 @@ private void AddVersionToListButton_Click(object sender, EventArgs e) } if (AddVersionToListTextBox.Text.ToLower() == "any") { - MessageBox.Show( - Properties.Resources.CompatibleGameVersionsDialogInvalidFormat, - Properties.Resources.CompatibleGameVersionsDialogErrorTitle, - MessageBoxButtons.OK, - MessageBoxIcon.Error - ); + MessageBox.Show(Properties.Resources.CompatibleGameVersionsDialogInvalidFormat, + Properties.Resources.CompatibleGameVersionsDialogErrorTitle, + MessageBoxButtons.OK, + MessageBoxIcon.Error); return; } try @@ -131,12 +116,10 @@ private void AddVersionToListButton_Click(object sender, EventArgs e) } catch (FormatException) { - MessageBox.Show( - Properties.Resources.CompatibleGameVersionsDialogInvalidFormat, - Properties.Resources.CompatibleGameVersionsDialogErrorTitle, - MessageBoxButtons.OK, - MessageBoxIcon.Error - ); + MessageBox.Show(Properties.Resources.CompatibleGameVersionsDialogInvalidFormat, + Properties.Resources.CompatibleGameVersionsDialogErrorTitle, + MessageBoxButtons.OK, + MessageBoxIcon.Error); } } @@ -156,9 +139,9 @@ private void CancelButton_Click(object sender, EventArgs e) private void SaveButton_Click(object sender, EventArgs e) { - _inst.SetCompatibleVersions( - SelectedVersionsCheckedListBox.CheckedItems.Cast().ToList() - ); + _inst.SetCompatibleVersions(SelectedVersionsCheckedListBox.CheckedItems + .Cast() + .ToList()); DialogResult = DialogResult.OK; Close(); diff --git a/GUI/Model/GUIMod.cs b/GUI/Model/GUIMod.cs index 0ca98f70e1..557815f0f6 100644 --- a/GUI/Model/GUIMod.cs +++ b/GUI/Model/GUIMod.cs @@ -200,6 +200,12 @@ public GUIMod(CkanModule mod, if (GameCompatibilityVersion == null) { GameCompatibilityVersion = mod.LatestCompatibleGameVersion(); + if (GameCompatibilityVersion.IsAny) + { + GameCompatibilityVersion = mod.LatestCompatibleRealGameVersion( + Main.Instance?.Manager.CurrentInstance?.game.KnownVersions + ?? new List() {}); + } } UpdateIsCached();