Skip to content

Commit

Permalink
Merge #3976 Fix compatible popup messing with max game version column
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Dec 24, 2023
2 parents e45475b + 720ddd8 commit fd4180d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 51 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file.
- [Multiple] Usability improvements for adding game instance (#3964 by: HebaruSan; reviewed: JonnyOThan)
- [Core] Fix NullReferenceException in csv/tsv export (#3967 by: HebaruSan)
- [Core] Fix cache timestamp comparisons (#3974 by: HebaruSan)
- [GUI] Fix compatible popup messing with max game version column (#3976 by: HebaruSan)

### Internal

Expand Down
25 changes: 17 additions & 8 deletions Core/Games/KerbalSpaceProgram.cs
Expand Up @@ -213,20 +213,29 @@ public void RefreshVersions()

private List<GameVersion> versions;

private readonly object versionMutex = new object();

public List<GameVersion> 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<IKspBuildMap>()
.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<IKspBuildMap>()
.KnownVersions
.Select(v => v.WithoutBuild)
.Distinct()
.OrderBy(v => v)
.ToList();
}
}
}
return versions;
}
Expand Down
2 changes: 1 addition & 1 deletion Core/Repositories/AvailableModule.cs
Expand Up @@ -183,7 +183,7 @@ public GameVersion LatestCompatibleGameVersion(List<GameVersion> 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)
Expand Down
67 changes: 25 additions & 42 deletions GUI/Dialogs/CompatibleGameVersionsDialog.cs
Expand Up @@ -35,14 +35,15 @@ public CompatibleGameVersionsDialog(GameInstance inst, bool centerScreen)
StartPosition = FormStartPosition.CenterScreen;
}

List<GameVersion> compatibleVersions = inst.GetCompatibleVersions();
var compatibleVersions = inst.GetCompatibleVersions();

GameVersionLabel.Text = inst.Version()?.ToString() ?? Properties.Resources.CompatibleGameVersionsDialogNone;
GameLocationLabel.Text = inst.GameDir().Replace('/', Path.DirectorySeparatorChar);
List<GameVersion> knownVersions = inst.game.KnownVersions;
List<GameVersion> majorVersionsList = CreateMajorVersionsList(knownVersions);
List<GameVersion> compatibleVersionsLeftOthers = new List<GameVersion>(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);
Expand Down Expand Up @@ -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<GameVersion> CreateMajorVersionsList(List<GameVersion> knownVersions)
{
Dictionary<GameVersion, bool> majorVersions = new Dictionary<GameVersion, bool>();
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<GameVersion>(majorVersions.Keys);
}
=> knownVersions.Select(v => v.ToVersionRange().Lower.Value)
.Select(v => new GameVersion(v.Major, v.Minor))
.Distinct()
.ToList();

private void SortAndAddVersionsToList(List<GameVersion> versions, List<GameVersion> 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));
}
}

Expand All @@ -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
Expand All @@ -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);
}
}

Expand All @@ -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<GameVersion>().ToList()
);
_inst.SetCompatibleVersions(SelectedVersionsCheckedListBox.CheckedItems
.Cast<GameVersion>()
.ToList());

DialogResult = DialogResult.OK;
Close();
Expand Down
6 changes: 6 additions & 0 deletions GUI/Model/GUIMod.cs
Expand Up @@ -200,6 +200,12 @@ public string Version
if (GameCompatibilityVersion == null)
{
GameCompatibilityVersion = mod.LatestCompatibleGameVersion();
if (GameCompatibilityVersion.IsAny)
{
GameCompatibilityVersion = mod.LatestCompatibleRealGameVersion(
Main.Instance?.Manager.CurrentInstance?.game.KnownVersions
?? new List<GameVersion>() {});
}
}

UpdateIsCached();
Expand Down

0 comments on commit fd4180d

Please sign in to comment.