Skip to content

Commit

Permalink
Merge #4191 Set compat of new instances based on Unity updates and al…
Browse files Browse the repository at this point in the history
…ert user
  • Loading branch information
HebaruSan committed Sep 22, 2024
2 parents 00774fe + 76c8316 commit 5986e89
Show file tree
Hide file tree
Showing 13 changed files with 226 additions and 49 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ All notable changes to this project will be documented in this file.
- [GUI] Don't hide other providing mods for installed, make installed bold (#4163 by: HebaruSan)
- [CLI] Set headless mode automatically (#4166 by: HebaruSan)
- [Core] Autogenerate spec version for modpacks (#4173 by: HebaruSan)
- [Multiple] Set compat of new instances based on Unity updates and alert user (#4191 by: HebaruSan)

### Bugfixes

Expand Down
4 changes: 2 additions & 2 deletions Core/Configuration/Win32RegistryConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ public void SetAuthToken(string host, string? token)
/// <summary>
/// Not implemented because the Windows registry is deprecated
/// </summary>
public string[] GlobalInstallFilters { get; set; } = new string[] { };
public string[] GlobalInstallFilters { get; set; } = Array.Empty<string>();

/// <summary>
/// Not implemented because the Windows registry is deprecated
/// </summary>
public string?[] PreferredHosts { get; set; } = new string?[] { };
public string?[] PreferredHosts { get; set; } = Array.Empty<string?>();

/// <summary>
/// Not implemented because the Windows registry is deprecated
Expand Down
5 changes: 5 additions & 0 deletions Core/GameInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ private void LoadCompatibleVersions()
GameVersion.TryParse(compatibleGameVersions.GameVersionWhenWritten, out GameVersion? mainVer);
GameVersionWhenCompatibleVersionsWereStored = mainVer;
}
else if (Version() is GameVersion gv)
{
_compatibleVersions = game.DefaultCompatibleVersions(gv)
.ToList();
}
}

private string CompatibleGameVersionsFile()
Expand Down
1 change: 1 addition & 0 deletions Core/Games/IGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public interface IGame
GameVersion[] EmbeddedGameVersions { get; }
GameVersion[] ParseBuildsJson(JToken json);
GameVersion? DetectVersion(DirectoryInfo where);
GameVersion[] DefaultCompatibleVersions(GameVersion installedVersion);
string CompatibleVersionsFile { get; }
string[] InstanceAnchorFiles { get; }

Expand Down
26 changes: 24 additions & 2 deletions Core/Games/KerbalSpaceProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,14 @@ is Stream s
?.Builds
?.Select(b => GameVersion.Parse(b.Value))
.ToArray()
?? new GameVersion[] { };
?? Array.Empty<GameVersion>();

public GameVersion[] ParseBuildsJson(JToken json)
=> json.ToObject<JBuilds>()
?.Builds
?.Select(b => GameVersion.Parse(b.Value))
.ToArray()
?? new GameVersion[] { };
?? Array.Empty<GameVersion>();

public GameVersion? DetectVersion(DirectoryInfo where)
=> ServiceLocator.Container
Expand All @@ -233,6 +233,28 @@ public GameVersion[] ParseBuildsJson(JToken json)
? verFromReadme
: null;

public GameVersion[] DefaultCompatibleVersions(GameVersion installedVersion)
// 1.2.9 was a prerelease that broke compatibility with previous 1.2 releases
=> installedVersion.Major == 1 && installedVersion.Minor == 2 && installedVersion.Patch == 9
? Array.Empty<GameVersion>()
: UnityBreakVersions.FirstOrDefault(brk => brk <= installedVersion)
is GameVersion latestUnityBreak
? MinorVersionsInRange(latestUnityBreak, installedVersion)
: new GameVersion[]
{
new GameVersion(installedVersion.Major,
installedVersion.Minor)
};

private static readonly GameVersion[] UnityBreakVersions =
new int[] {8, 4, 2, 1}.Select(minor => new GameVersion(1, minor))
.ToArray();

private static GameVersion[] MinorVersionsInRange(GameVersion min, GameVersion max)
=> Enumerable.Range(min.Minor, max.Minor - min.Minor + 1)
.Select(minor => new GameVersion(min.Major, minor))
.ToArray();

public string CompatibleVersionsFile => "compatible_ksp_versions.json";

public string[] InstanceAnchorFiles =>
Expand Down
10 changes: 8 additions & 2 deletions Core/Games/KerbalSpaceProgram2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ public GameVersion[] EmbeddedGameVersions
is Stream s
? JsonConvert.DeserializeObject<GameVersion[]>(new StreamReader(s).ReadToEnd())
: null)
?? new GameVersion[] { };
?? Array.Empty<GameVersion>();

public GameVersion[] ParseBuildsJson(JToken json)
=> json.ToObject<GameVersion[]>()
?? new GameVersion[] { };
?? Array.Empty<GameVersion>();

public GameVersion DetectVersion(DirectoryInfo where)
=> VersionFromAssembly(Path.Combine(where.FullName,
Expand Down Expand Up @@ -202,6 +202,12 @@ public GameVersion DetectVersion(DirectoryInfo where)
? v
: null;

public GameVersion[] DefaultCompatibleVersions(GameVersion installedVersion)
// KSP2 didn't last long enough to break compatibility :~(
=> Enumerable.Range(1, 2)
.Select(minor => new GameVersion(0, minor))
.ToArray();

public string CompatibleVersionsFile => "compatible_game_versions.json";

public string[] InstanceAnchorFiles =>
Expand Down
3 changes: 2 additions & 1 deletion GUI/Controls/ModInfoTabs/Versions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Linq;
using System.ComponentModel;
using System.Collections.Generic;
Expand Down Expand Up @@ -226,7 +227,7 @@ private ListViewItem[] getItems(GUIMod gmod, List<CkanModule> versions)

return items;
}
return new ListViewItem[] { };
return Array.Empty<ListViewItem>();
}

[ForbidGUICalls]
Expand Down
60 changes: 45 additions & 15 deletions GUI/Dialogs/CompatibleGameVersionsDialog.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 40 additions & 10 deletions GUI/Dialogs/CompatibleGameVersionsDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,51 @@ protected override void OnHelpButtonClicked(CancelEventArgs evt)
evt.Cancel = Util.TryOpenWebPage(HelpURLs.CompatibleGameVersions);
}

private void ShowMessage(string msg)
{
if (string.IsNullOrEmpty(msg))
{
MessageLabel.Text = "";
MessageLabel.Visible = false;
}
else
{
MessageLabel.Text = msg;
MessageLabel.Visible = true;
MessageLabel.Height = Util.LabelStringHeight(CreateGraphics(),
MessageLabel);
Height += MessageLabel.Height;
}
}

protected override void OnResize(EventArgs e)
{
base.OnResize(e);
if (MessageLabel.Visible)
{
MessageLabel.Height = Util.LabelStringHeight(CreateGraphics(),
MessageLabel);
}
}

private void CompatibleGameVersionsDialog_Shown(object? sender, EventArgs? e)
{
if (_inst.CompatibleVersionsAreFromDifferentGameVersion)
{
CancelChooseCompatibleVersionsButton.Visible = false;
ActualGameVersionLabel.Text = string.Format(
Properties.Resources.CompatibleGameVersionsDialogVersionDetails,
_inst.Version(),
_inst.GameVersionWhenCompatibleVersionsWereStored);
ActualGameVersionLabel.ForeColor = Color.Red;
MessageBox.Show(this,
Properties.Resources.CompatibleGameVersionsDialogGameUpdatedDetails,
Properties.Resources.CompatibleGameVersionsDialogGameUpdatedTitle,
MessageBoxButtons.OK,
MessageBoxIcon.Information);
if (_inst.GameVersionWhenCompatibleVersionsWereStored == null)
{
ShowMessage(Properties.Resources.CompatibleGameVersionsDialogDefaultedDetails);
}
else
{
ActualGameVersionLabel.Text = string.Format(
Properties.Resources.CompatibleGameVersionsDialogVersionDetails,
_inst.Version(),
_inst.GameVersionWhenCompatibleVersionsWereStored);
ActualGameVersionLabel.ForeColor = Color.Red;
ShowMessage(Properties.Resources.CompatibleGameVersionsDialogGameUpdatedDetails);
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion GUI/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@
<data name="CloneGameInstanceToolTipShareStock" xml:space="preserve"><value>Create junction points (Windows) or symbolic links (Unix) to stock directories instead of copying them.
If you choose this option, DO NOT move or delete the old instance!!</value></data>
<data name="CompatibleGameVersionsDialogNone" xml:space="preserve"><value>&lt;NONE&gt;</value></data>
<data name="CompatibleGameVersionsDialogGameUpdatedTitle" xml:space="preserve"><value>Game Version Updated</value></data>
<data name="CompatibleGameVersionsDialogDefaultedDetails" xml:space="preserve"><value>Default compatibility has been set based on your installed game version. These settings should bring the greatest number of working mods to the most users, but it's still possible that some older mods might misbehave.

If you want to be cautious, click "Clear selection" to uncheck all the boxes.</value></data>
<data name="CompatibleGameVersionsDialogGameUpdatedDetails" xml:space="preserve"><value>The game has been updated since you last reviewed your compatible game versions. Please make sure that settings are correct.</value></data>
<data name="CompatibleGameVersionsDialogVersionDetails" xml:space="preserve"><value>{0} (previous game version: {1})</value></data>
<data name="CompatibleGameVersionsDialogInvalidFormat" xml:space="preserve"><value>Version has invalid format</value></data>
Expand Down
32 changes: 16 additions & 16 deletions GUI/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,26 +306,26 @@ private static Color AddColors(Color a, Color b)
a.G + b.G,
a.B + b.B);

public static Bitmap LerpBitmaps(Bitmap a, Bitmap b, float amount)
public static Bitmap LerpBitmaps(Bitmap a, Bitmap b,
float amount)
=> amount <= 0 ? a
: amount >= 1 ? b
: MergeBitmaps(a, b,
// Note pixA and pixB are swapped because our blend function
// pretends 'amount' is the first argument's alpha channel,
// so 0 -> third param by itself
(pixA, pixB) => AlphaBlendWith(pixB, amount, pixA));

private static Bitmap MergeBitmaps(Bitmap a, Bitmap b,
Func<Color, Color, Color> how)
{
if (amount <= 0)
{
return a;
}
if (amount >= 1)
{
return b;
}
var c = new Bitmap(a);
for (int y = 0; y < c.Height; ++y)
foreach (var y in Enumerable.Range(0, c.Height))
{
for (int x = 0; x < c.Width; ++x)
foreach (var x in Enumerable.Range(0, c.Width))
{
// Note a and b are swapped because our blend function pretends 'amount'
// is the first argument's alpha channel, so 0 -> third param by itself
c.SetPixel(x, y, AlphaBlendWith(b.GetPixel(x, y),
amount,
a.GetPixel(x, y)));
c.SetPixel(x, y, how(a.GetPixel(x, y),
b.GetPixel(x, y)));
}
}
return c;
Expand Down
35 changes: 35 additions & 0 deletions Tests/Core/Games/KerbalSpaceProgram2Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using NUnit.Framework;

using CKAN.Versioning;
using CKAN.Games.KerbalSpaceProgram2;
using System.Linq;

namespace Tests.Core.Games
{
[TestFixture]
public class KerbalSpaceProgram2Tests
{
[Test, TestCase("0.1.0.0",
new string[] { "0.1", "0.2" }),
TestCase("0.1.1.0",
new string[] { "0.1", "0.2" }),
TestCase("0.2.0.0",
new string[] { "0.1", "0.2" }),
TestCase("0.2.2.0",
new string[] { "0.1", "0.2" }),
]
public void DefaultCompatibleVersions_RealVersion_CorrectRange(string installedVersion,
string[] correctCompatVersions)
{
// Arrange
var game = new KerbalSpaceProgram2();

// Act
var compat = game.DefaultCompatibleVersions(GameVersion.Parse(installedVersion));

// Assert
CollectionAssert.AreEqual(correctCompatVersions.Select(GameVersion.Parse),
compat);
}
}
}
Loading

0 comments on commit 5986e89

Please sign in to comment.