Skip to content

Commit

Permalink
Refactor "Too New" version check to "Excluded Versions"
Browse files Browse the repository at this point in the history
  • Loading branch information
Grantapher committed Jul 1, 2024
1 parent daf283f commit 6392bd4
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions ValheimPlus/ValheimPlus.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -42,10 +43,16 @@ public class ValheimPlusPlugin : BaseUnityPlugin
// The game version this version of V+ was compiled against.
private static readonly GameVersion TargetGameVersion = new(0, 218, 15);

// The last game version this will work with. If higher, the mod will not work.
// This is useful for warning when the game is running on a PTB version we know this will fail on.
// Otherwise, just keep this as null to disable the check.
private static readonly GameVersion MaxKnownWorkingGameVersion = new(0, 218, 15);
// Versions we know for sure will not work with this game version.
// Useful if a PTB is active to exclude it from the stable release.
private static readonly Dictionary<GameVersion, string> ExcludeGameVersions = new()
{
[new GameVersion(0, 218, 17)] =
$"This version of Valheim Plus ({FullVersion}) does not work with the current Valheim game version " +
$"({Version.CurrentVersion}), to use this Valheim game version, update Valheim Plus (possibly to an" +
" alpha version): https://github.com/Grantapher/ValheimPlus/releases. If you don't want to use an " +
"alpha version, update the Valheim game version to one compatible with this one."
};

internal static string newestVersion { get; private set; } = "";
internal static bool isUpToDate { get; private set; }
Expand Down Expand Up @@ -88,10 +95,9 @@ private void Awake()
var tooOld = IsGameVersionTooOld();
if (tooOld) LogTooOld();

var tooNew = IsGameVersionTooNew();
if (tooNew) LogTooNew();
var versionExcluded = CheckIsGameVersionExcluded();

if (tooOld || tooNew)
if (tooOld || versionExcluded)
{
Logger.LogFatal("Aborting loading of Valheim Plus due to incompatible version.");
return;
Expand Down Expand Up @@ -146,9 +152,12 @@ private void Awake()
private static bool IsGameVersionTooOld() => Version.CurrentVersion < MinSupportedGameVersion;
private static bool IsGameVersionNewerThanTarget() => Version.CurrentVersion > TargetGameVersion;

private static bool IsGameVersionTooNew() =>
// ReSharper disable once ConditionIsAlwaysTrueOrFalse Want to be able to set maxKnown* to null.
MaxKnownWorkingGameVersion != null && Version.CurrentVersion > MaxKnownWorkingGameVersion;
private static bool CheckIsGameVersionExcluded()
{
bool excluded = ExcludeGameVersions.TryGetValue(Version.CurrentVersion, out var errorMessage);
if (excluded) Logger.LogError(errorMessage);
return excluded;
}

private static bool IsNewVersionAvailable()
{
Expand Down Expand Up @@ -228,9 +237,8 @@ public static void PatchAll()
}
catch (Exception)
{
Logger.LogError($"Failed to apply patches.");
Logger.LogError("Failed to apply patches.");
if (IsGameVersionTooOld()) LogTooOld();
else if (IsGameVersionTooNew()) LogTooNew();
else if (IsGameVersionNewerThanTarget())
{
Logger.LogWarning(
Expand Down Expand Up @@ -266,14 +274,6 @@ private static void LogTooOld()
"https://github.com/Grantapher/ValheimPlus/blob/grantapher-development/COMPATIBILITY.md.");
}

private static void LogTooNew()
{
Logger.LogError(
$"This version of Valheim Plus ({FullVersion}) expects a maximum game version of " +
$"\"{MaxKnownWorkingGameVersion}\", but this game version is newer at \"{Version.CurrentVersion}\". " +
"Please update Valheim Plus via the releases: https://github.com/Grantapher/ValheimPlus/releases");
}

public static void UnpatchSelf()
{
Logger.LogInfo("Unpatching.");
Expand Down

0 comments on commit 6392bd4

Please sign in to comment.