Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress incompatibility warning at game launch #3453

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions Core/GameInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ private void SetupCkanDirectories(bool scan = true)
log.InfoFormat("Initialised {0}", CkanDir());
}

#endregion

#region Settings

public void SetCompatibleVersions(List<GameVersion> compatibleVersions)
{
this._compatibleVersions = compatibleVersions.Distinct().ToList();
Expand Down Expand Up @@ -178,6 +182,19 @@ public List<GameVersion> GetCompatibleVersions()
return new List<GameVersion>(this._compatibleVersions);
}

public HashSet<string> GetSuppressedCompatWarningIdentifiers =>
SuppressedCompatWarningIdentifiers.LoadFrom(Version(), SuppressedCompatWarningIdentifiersFile).Identifiers;

public void AddSuppressedCompatWarningIdentifiers(HashSet<string> idents)
{
var scwi = SuppressedCompatWarningIdentifiers.LoadFrom(Version(), SuppressedCompatWarningIdentifiersFile);
scwi.Identifiers.UnionWith(idents);
scwi.SaveTo(SuppressedCompatWarningIdentifiersFile);
}

private string SuppressedCompatWarningIdentifiersFile =>
Path.Combine(CkanDir(), "suppressed_compat_warning_identifiers.json");

#endregion

#region KSP Directory Detection and Versioning
Expand Down
44 changes: 44 additions & 0 deletions Core/SuppressedCompatWarningIdentifiers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json;
using log4net;

using CKAN.Versioning;

namespace CKAN
{
class SuppressedCompatWarningIdentifiers
{
public GameVersion GameVersionWhenWritten;
public HashSet<string> Identifiers = new HashSet<string>();

public static SuppressedCompatWarningIdentifiers LoadFrom(GameVersion gameVer, string filename)
{
try
{
var saved = JsonConvert.DeserializeObject<SuppressedCompatWarningIdentifiers>(File.ReadAllText(filename));
// Reset warnings if e.g. Steam auto-updates the game
if (saved.GameVersionWhenWritten == gameVer)
{
return saved;
}
}
catch (Exception exc)
{
log.Debug("Failed to load", exc);
}
return new SuppressedCompatWarningIdentifiers()
{
GameVersionWhenWritten = gameVer
};
}

public void SaveTo(string filename)
{
File.WriteAllText(filename, JsonConvert.SerializeObject(this));
}

private static readonly ILog log = LogManager.GetLogger(typeof(SuppressedCompatWarningIdentifiers));
}
}
19 changes: 17 additions & 2 deletions GUI/Dialogs/YesNoDialog.Designer.cs

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

61 changes: 44 additions & 17 deletions GUI/Dialogs/YesNoDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,57 @@ public YesNoDialog()

public DialogResult ShowYesNoDialog(Form parentForm, string text, string yesText = null, string noText = null)
{
task = new TaskCompletionSource<DialogResult>();
task = new TaskCompletionSource<Tuple<DialogResult, bool>>();

Util.Invoke(parentForm, () =>
{
var height = StringHeight(text, ClientSize.Width - 25) + 2 * 54;
DescriptionLabel.Text = text;
DescriptionLabel.TextAlign = text.Contains("\n")
? HorizontalAlignment.Left
: HorizontalAlignment.Center;
DescriptionLabel.ScrollBars = height < maxHeight
? ScrollBars.None
: ScrollBars.Vertical;
YesButton.Text = yesText ?? defaultYes;
NoButton.Text = noText ?? defaultNo;
ClientSize = new Size(
ClientSize.Width,
Math.Min(maxHeight, height)
);
task.SetResult(ShowDialog(parentForm));
Setup(text, yesText, noText);
task.SetResult(new Tuple<DialogResult, bool>(ShowDialog(parentForm), SuppressCheckbox.Checked));
});

return task.Task.Result.Item1;
}

public Tuple<DialogResult, bool> ShowSuppressableYesNoDialog(Form parentForm, string text, string suppressText, string yesText = null, string noText = null)
{
task = new TaskCompletionSource<Tuple<DialogResult, bool>>();

Util.Invoke(parentForm, () =>
{
SetupSuppressable(text, yesText, noText, suppressText);
task.SetResult(new Tuple<DialogResult, bool>(ShowDialog(parentForm), SuppressCheckbox.Checked));
});

return task.Task.Result;
}

private void Setup(string text, string yesText, string noText)
{
var height = StringHeight(text, ClientSize.Width - 25) + 2 * 54;
DescriptionLabel.Text = text;
DescriptionLabel.TextAlign = text.Contains("\n")
? HorizontalAlignment.Left
: HorizontalAlignment.Center;
DescriptionLabel.ScrollBars = height < maxHeight
? ScrollBars.None
: ScrollBars.Vertical;
YesButton.Text = yesText ?? defaultYes;
NoButton.Text = noText ?? defaultNo;
SuppressCheckbox.Visible = false;
ClientSize = new Size(
ClientSize.Width,
Math.Min(maxHeight, height)
);
}

private void SetupSuppressable(string text, string yesText, string noText, string suppressText)
{
Setup(text, yesText, noText);
SuppressCheckbox.Checked = false;
SuppressCheckbox.Text = suppressText;
SuppressCheckbox.Visible = true;
}

/// <summary>
/// Simple syntactic sugar around Graphics.MeasureString
/// </summary>
Expand All @@ -59,7 +86,7 @@ public void HideYesNoDialog()
}

private const int maxHeight = 600;
private TaskCompletionSource<DialogResult> task;
private TaskCompletionSource<Tuple<DialogResult, bool>> task;
private string defaultYes;
private string defaultNo;
}
Expand Down
25 changes: 21 additions & 4 deletions GUI/Main/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Autofac;

using CKAN.Extensions;
using CKAN.Versioning;

namespace CKAN
{
Expand Down Expand Up @@ -717,20 +718,36 @@ public void LaunchGame()
return;

var registry = RegistryManager.Instance(CurrentInstance).registry;
var incomp = registry.IncompatibleInstalled(CurrentInstance.VersionCriteria())
.Where(m => !m.Module.IsDLC).ToList();

var suppressedIdentifiers = CurrentInstance.GetSuppressedCompatWarningIdentifiers;
var incomp = registry.IncompatibleInstalled(CurrentInstance.VersionCriteria())
.Where(m => !m.Module.IsDLC && !suppressedIdentifiers.Contains(m.identifier))
.ToList();
if (incomp.Any())
{
// Warn that it might not be safe to run Game with incompatible modules installed
string incompatDescrip = incomp
.Select(m => $"{m.Module} ({registry.CompatibleGameVersions(CurrentInstance.game, m.Module)})")
.Aggregate((a, b) => $"{a}{Environment.NewLine}{b}");
if (!YesNoDialog(string.Format(Properties.Resources.MainLaunchWithIncompatible, incompatDescrip),
var ver = CurrentInstance.Version();
var result = SuppressableYesNoDialog(
string.Format(Properties.Resources.MainLaunchWithIncompatible, incompatDescrip),
string.Format(Properties.Resources.MainLaunchDontShow,
CurrentInstance.game.ShortName,
new GameVersion(ver.Major, ver.Minor, ver.Patch)),
Properties.Resources.MainLaunch,
Properties.Resources.MainGoBack))
Properties.Resources.MainGoBack
);
if (result.Item1 != DialogResult.Yes)
{
return;
}
else if (result.Item2)
{
CurrentInstance.AddSuppressedCompatWarningIdentifiers(
incomp.Select(m => m.identifier).ToHashSet()
);
}
}

split = CurrentInstance.game.AdjustCommandLine(split,
Expand Down
10 changes: 10 additions & 0 deletions GUI/Main/MainDialogs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public bool YesNoDialog(string text, string yesText = null, string noText = null
return yesNoDialog.ShowYesNoDialog(this, text, yesText, noText) == DialogResult.Yes;
}

/// <summary>
/// Show a yes/no dialog with a "don't show again" checkbox
/// </summary>
/// <returns>A tuple of the dialog result and a bool indicating whether
/// the suppress-checkbox has been checked (true)</returns>
public Tuple<DialogResult, bool> SuppressableYesNoDialog(string text, string suppressText, string yesText = null, string noText = null)
{
return yesNoDialog.ShowSuppressableYesNoDialog(this, text, suppressText, yesText, noText);
}

public int SelectionDialog(string message, params object[] args)
{
return selectionDialog.ShowSelectionDialog(message, args);
Expand Down
3 changes: 3 additions & 0 deletions GUI/Properties/Resources.Designer.cs

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

1 change: 1 addition & 0 deletions GUI/Properties/Resources.de-DE.resx
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ Versuche {2} aus {3} zu verschieben und CKAN neu zu starten.</value></data>
{0}</value>
</data>
<data name="MainLaunch" xml:space="preserve"><value>Starten</value></data>
<data name="MainLaunchDontShow" xml:space="preserve"><value>Für diese Mods und {0} {1} nicht mehr anzeigen</value></data>
<data name="MainLaunchFailed" xml:space="preserve">
<value>Das Spiel konnte nicht gestartet werden.

Expand Down
1 change: 1 addition & 0 deletions GUI/Properties/Resources.fr-FR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ Essayez de déplacer {2} en dehors de {3} et redémarrez CKAN.</value></data>

{0}</value></data>
<data name="MainLaunch" xml:space="preserve"><value>Lancement</value></data>
<data name="MainLaunchDontShow" xml:space="preserve"><value>Ne plus demander pour ces mods sur {0} version {1}</value></data>
<data name="MainLaunchFailed" xml:space="preserve"><value>Impossible de démarrer le jeu.

{0}</value></data>
Expand Down
1 change: 1 addition & 0 deletions GUI/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ Try to move {2} out of {3} and restart CKAN.</value></data>
<data name="MainLaunchWithIncompatible" xml:space="preserve"><value>Some installed modules are incompatible! It might not be safe to launch the game. Really launch?

{0}</value></data>
<data name="MainLaunchDontShow" xml:space="preserve"><value>Don't show this again for these mods on {0} {1}</value></data>
<data name="MainLaunch" xml:space="preserve"><value>Launch</value></data>
<data name="MainLaunchFailed" xml:space="preserve"><value>Couldn't start game.

Expand Down