From b105c97dda01441d503d31e8b8ac0b3fd35fef14 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 24 Sep 2017 19:55:34 -0400 Subject: add support for remapping legacy versions for update checks (#361) --- .../Framework/Models/ModDataRecord.cs | 25 ++++++++++++++++++++++ src/StardewModdingAPI/Program.cs | 9 ++++---- .../StardewModdingAPI.config.json | 5 +++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/StardewModdingAPI/Framework/Models/ModDataRecord.cs b/src/StardewModdingAPI/Framework/Models/ModDataRecord.cs index a2397fa8..0d033e82 100644 --- a/src/StardewModdingAPI/Framework/Models/ModDataRecord.cs +++ b/src/StardewModdingAPI/Framework/Models/ModDataRecord.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; using StardewModdingAPI.Framework.Serialisation; @@ -24,6 +25,12 @@ namespace StardewModdingAPI.Framework.Models [JsonConverter(typeof(SFieldConverter))] public ModCompatibility[] Compatibility { get; set; } = new ModCompatibility[0]; + /// Map local versions to a semantic version for update checks. + public IDictionary MapLocalVersions { get; set; } = new Dictionary(); + + /// Map remote versions to a semantic version for update checks. + public IDictionary MapRemoteVersions { get; set; } = new Dictionary(); + /********* ** Public methods @@ -34,5 +41,23 @@ namespace StardewModdingAPI.Framework.Models { return this.Compatibility.FirstOrDefault(p => p.MatchesVersion(version)); } + + /// Get a semantic local version for update checks. + /// The local version to normalise. + public string GetLocalVersionForUpdateChecks(string version) + { + return this.MapLocalVersions != null && this.MapLocalVersions.TryGetValue(version, out string newVersion) + ? newVersion + : version; + } + + /// Get a semantic remote version for update checks. + /// The remote version to normalise. + public string GetRemoteVersionForUpdateChecks(string version) + { + return this.MapRemoteVersions != null && this.MapRemoteVersions.TryGetValue(version, out string newVersion) + ? newVersion + : version; + } } } diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index 3e1db20c..7b3048c0 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -576,12 +576,13 @@ namespace StardewModdingAPI } // track update - ISemanticVersion version = new SemanticVersion(info.Version); - bool isUpdate = version.IsNewerThan(mod.Manifest.Version); - this.VerboseLog($" {mod.DisplayName} ({result.Key}): {(isUpdate ? $"{mod.Manifest.Version} => {info.Version}" : "OK")}."); + ISemanticVersion localVersion = new SemanticVersion(mod.DataRecord?.GetLocalVersionForUpdateChecks(mod.Manifest.Version.ToString())); + ISemanticVersion latestVersion = new SemanticVersion(mod.DataRecord?.GetRemoteVersionForUpdateChecks(info.Version)); + bool isUpdate = latestVersion.IsNewerThan(localVersion); + this.VerboseLog($" {mod.DisplayName} ({result.Key}): {(isUpdate ? $"{mod.Manifest.Version}{(!localVersion.Equals(mod.Manifest.Version) ? $" [{localVersion}]" : "")} => {info.Version}{(!latestVersion.Equals(new SemanticVersion(info.Version)) ? $" [{latestVersion}]" : "")}" : "OK")}."); if (isUpdate) { - if (!updatesByMod.TryGetValue(mod, out ModInfoModel other) || version.IsNewerThan(other.Version)) + if (!updatesByMod.TryGetValue(mod, out ModInfoModel other) || latestVersion.IsNewerThan(other.Version)) updatesByMod[mod] = info; } } diff --git a/src/StardewModdingAPI/StardewModdingAPI.config.json b/src/StardewModdingAPI/StardewModdingAPI.config.json index 3d156a15..a768c762 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.config.json +++ b/src/StardewModdingAPI/StardewModdingAPI.config.json @@ -63,6 +63,11 @@ This file contains advanced configuration for SMAPI. You generally shouldn't cha * if it detects incompatible code). * - 'ReasonPhrase' (optional) specifies a message to show to the player explaining why the * mod isn't loaded. This has no effect for AssumeCompatible. + * + * - 'MapLocalVersions' and 'MapRemoteVersions' substitute versions for update checks. For + * example, if the API returns version '1.1-1078', MapRemoteVersions can map it to '1.1' when + * comparing to the mod's current version. This is only intended to support legacy mods with + * injected update keys. */ "ModData": [ { -- cgit