From fd2d7d714d025f7f787e14e47645078f988ae8ce Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 24 Feb 2017 15:04:28 -0500 Subject: fix game version checks not using semantic versioning This caused an issue where SMAPI didn't consider SDV 1.2.10 to pass the minimum game version of 1.2.9. This requires some workarounds for SDV 1.11's non-semantic version. --- src/StardewModdingAPI/Constants.cs | 37 ++++++++++++++++++++++++++++++------- src/StardewModdingAPI/Program.cs | 4 ++-- 2 files changed, 32 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/StardewModdingAPI/Constants.cs b/src/StardewModdingAPI/Constants.cs index 3326e43f..99bf834c 100644 --- a/src/StardewModdingAPI/Constants.cs +++ b/src/StardewModdingAPI/Constants.cs @@ -33,7 +33,7 @@ namespace StardewModdingAPI public static ISemanticVersion ApiVersion { get; } = new SemanticVersion(1, 8, 0); /// The minimum supported version of Stardew Valley. - public static string MinimumGameVersion { get; } = "1.2.9"; + public static ISemanticVersion MinimumGameVersion { get; } = new SemanticVersion("1.2.9"); /// The path to the game folder. public static string ExecutionPath { get; } = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); @@ -68,8 +68,12 @@ namespace StardewModdingAPI /// Whether a player save has been loaded. internal static bool IsSaveLoaded => Game1.hasLoadedGame && !string.IsNullOrEmpty(Game1.player.name); - /// The current game version. - internal static string GameVersion { get; } = Constants.GetGameVersion(); + /// The game's current semantic version. + internal static ISemanticVersion GameVersion { get; } = Constants.GetGameVersion(); + + /// The game's current version as it should be displayed to players. + internal static ISemanticVersion GameDisplayVersion { get; } = Constants.GetGameDisplayVersion(Constants.GameVersion); + /********* ** Protected methods @@ -142,14 +146,33 @@ namespace StardewModdingAPI return $"{prefix}_{Game1.uniqueIDForThisGame}"; } - /// Get the actual game version. - /// This uses reflection because is a constant, so SMAPI's references to it are inlined at compile-time. - private static string GetGameVersion() + /// Get the game's current semantic version. + private static ISemanticVersion GetGameVersion() { + // get raw version + // we need reflection because it's a constant, so SMAPI's references to it are inlined at compile-time FieldInfo field = typeof(Game1).GetField(nameof(Game1.version), BindingFlags.Public | BindingFlags.Static); if (field == null) throw new InvalidOperationException($"The {nameof(Game1)}.{nameof(Game1.version)} field could not be found."); - return (string)field.GetValue(null); + string version = (string)field.GetValue(null); + + // get semantic version + if (version == "1.11") + version = "1.1.1"; // The 1.1 patch was released as 1.11, which means it's out of order for semantic version checks + return new SemanticVersion(version); + } + + /// Get game current version as it should be displayed to players. + /// The semantic game version. + private static ISemanticVersion GetGameDisplayVersion(ISemanticVersion version) + { + switch (version.ToString()) + { + case "1.1.1": + return new SemanticVersion(1, 11, 0); // The 1.1 patch was released as 1.11 + default: + return version; + } } } } diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index 3431e02f..8264d0ee 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -142,9 +142,9 @@ namespace StardewModdingAPI try { // verify version - if (string.Compare(Constants.GameVersion, Constants.MinimumGameVersion, StringComparison.InvariantCultureIgnoreCase) < 0) + if (Constants.GameVersion.IsOlderThan(Constants.MinimumGameVersion)) { - this.Monitor.Log($"Oops! You're running Stardew Valley {Constants.GameVersion}, but the oldest supported version is {Constants.MinimumGameVersion}. Please update your game before using SMAPI. If you're on the Steam beta channel, note that the beta channel may not receive the latest updates.", LogLevel.Error); + this.Monitor.Log($"Oops! You're running Stardew Valley {Constants.GameDisplayVersion}, but the oldest supported version is {Constants.MinimumGameVersion}. Please update your game before using SMAPI. If you're on the Steam beta channel, note that the beta channel may not receive the latest updates.", LogLevel.Error); return; } -- cgit