From 1fa2632289134c39f268c374bb290549f28751d5 Mon Sep 17 00:00:00 2001 From: Dan Volchek Date: Sun, 10 Jun 2018 14:19:05 -0700 Subject: seperate IModMetadata methods, call them correctly when updating --- src/SMAPI/Framework/IModMetadata.cs | 13 ++++++----- src/SMAPI/Framework/ModLoading/ModMetadata.cs | 18 ++++++++++------ src/SMAPI/Program.cs | 31 +++++++++++++++++++-------- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/src/SMAPI/Framework/IModMetadata.cs b/src/SMAPI/Framework/IModMetadata.cs index 673c5b2e..7bf7d98c 100644 --- a/src/SMAPI/Framework/IModMetadata.cs +++ b/src/SMAPI/Framework/IModMetadata.cs @@ -80,14 +80,17 @@ namespace StardewModdingAPI.Framework /// The mod-provided API. IModMetadata SetApi(object api); - /// Set the update status, indicating no errors happened. + /// Set the update version. /// The latest version. + IModMetadata SetUpdateVersion(ISemanticVersion latestVersion); + + /// Set the preview update version. /// The latest preview version. - IModMetadata SetUpdateStatus(ISemanticVersion latestVersion, ISemanticVersion latestPreviewVersion); + IModMetadata SetPreviewUpdateVersion(ISemanticVersion latestPreviewVersion); - /// Set the update status, indicating an error happened. - /// The error checking for updates, if any. - IModMetadata SetUpdateStatus(string updateCheckError); + /// Set the error that occured while checking for updates. + /// The error checking for updates. + IModMetadata SetUpdateError(string updateCheckError); /// Whether the mod manifest was loaded (regardless of whether the mod itself was loaded). bool HasManifest(); diff --git a/src/SMAPI/Framework/ModLoading/ModMetadata.cs b/src/SMAPI/Framework/ModLoading/ModMetadata.cs index 8f544da3..1ead1387 100644 --- a/src/SMAPI/Framework/ModLoading/ModMetadata.cs +++ b/src/SMAPI/Framework/ModLoading/ModMetadata.cs @@ -124,19 +124,25 @@ namespace StardewModdingAPI.Framework.ModLoading return this; } - /// Set the update status. + /// Set the update version. /// The latest version. - /// The latest preview version. - public IModMetadata SetUpdateStatus(ISemanticVersion latestVersion, ISemanticVersion latestPreviewVersion) + public IModMetadata SetUpdateVersion(ISemanticVersion latestVersion) { this.LatestVersion = latestVersion; + return this; + } + + /// Set the preview update version. + /// The latest preview version. + public IModMetadata SetPreviewUpdateVersion(ISemanticVersion latestPreviewVersion) + { this.LatestPreviewVersion = latestPreviewVersion; return this; } - // Set the update status, indicating an error happened. - /// The error checking for updates, if any. - public IModMetadata SetUpdateStatus(string updateCheckError) + /// Set the error that occured while checking for updates. + /// The error checking for updates. + public IModMetadata SetUpdateError(string updateCheckError) { this.UpdateCheckError = updateCheckError; return this; diff --git a/src/SMAPI/Program.cs b/src/SMAPI/Program.cs index dc034331..e6aafb2d 100644 --- a/src/SMAPI/Program.cs +++ b/src/SMAPI/Program.cs @@ -671,34 +671,47 @@ namespace StardewModdingAPI // handle error if (remoteInfo.Error != null) { - mod.SetUpdateStatus(remoteInfo.Error); + if(mod.LatestVersion == null && mod.LatestPreviewVersion == null) + mod.SetUpdateError(remoteInfo.Error); this.Monitor.Log($" {mod.DisplayName} ({result.Key}): update error: {remoteInfo.Error}", LogLevel.Trace); continue; } // normalise versions ISemanticVersion localVersion = mod.DataRecord?.GetLocalVersionForUpdateChecks(mod.Manifest.Version) ?? mod.Manifest.Version; - if (!SemanticVersion.TryParse(mod.DataRecord?.GetRemoteVersionForUpdateChecks(remoteInfo.Version) ?? remoteInfo.Version, out ISemanticVersion remoteVersion)) + + bool validVersion = SemanticVersion.TryParse(mod.DataRecord?.GetRemoteVersionForUpdateChecks(remoteInfo.Version) ?? remoteInfo.Version, out ISemanticVersion remoteVersion); + bool validPreviewVersion = SemanticVersion.TryParse(remoteInfo.PreviewVersion, out ISemanticVersion remotePreviewVersion); + + if (!validVersion && !validPreviewVersion) { - string errorInfo = $"Mod has invalid version {remoteInfo.Version}"; + string errorInfo = $"Mod has invalid versions. version: {remoteInfo.Version}, preview version: {remoteInfo.PreviewVersion}"; - mod.SetUpdateStatus(errorInfo); + if (mod.LatestVersion == null && mod.LatestPreviewVersion == null) + mod.SetUpdateError(errorInfo); this.Monitor.Log($" {mod.DisplayName} ({result.Key}): update error: {errorInfo}", LogLevel.Trace); continue; } - SemanticVersion.TryParse(remoteInfo.PreviewVersion, out ISemanticVersion remotePreviewVersion); - mod.SetUpdateStatus(remoteVersion, remotePreviewVersion); - // compare versions - bool isNonPreviewUpdate = remoteVersion.IsNewerThan(localVersion); + bool isNonPreviewUpdate = validVersion && remoteVersion.IsNewerThan(localVersion); + bool isUpdate = isNonPreviewUpdate || - (localVersion.IsNewerThan(remoteVersion) && remotePreviewVersion.IsNewerThan(localVersion)); + (validPreviewVersion && localVersion.IsNewerThan(remoteVersion) && remotePreviewVersion.IsNewerThan(localVersion)); this.VerboseLog($" {mod.DisplayName} ({result.Key}): {(isUpdate ? $"{mod.Manifest.Version}{(!localVersion.Equals(mod.Manifest.Version) ? $" [{localVersion}]" : "")} => {(isNonPreviewUpdate ? remoteInfo.Version : remoteInfo.PreviewVersion)}" : "okay")}."); if (isUpdate) { if (!updatesByMod.TryGetValue(mod, out Tuple other) || (isNonPreviewUpdate ? remoteVersion : remotePreviewVersion).IsNewerThan(other.Item2 ? other.Item1.PreviewVersion : other.Item1.Version)) + { updatesByMod[mod] = new Tuple(remoteInfo, !isNonPreviewUpdate); + + if (isNonPreviewUpdate) + mod.SetUpdateVersion(remoteVersion); + else + mod.SetPreviewUpdateVersion(remotePreviewVersion); + + mod.SetUpdateError(null); + } } } -- cgit