From 0b2e46d55cb09a169c7bb64ade37c82fc8233cb3 Mon Sep 17 00:00:00 2001 From: Dan Volchek Date: Sun, 10 Jun 2018 15:05:59 -0700 Subject: refactor IModMetadata update info --- src/SMAPI/Framework/IModMetadata.cs | 28 ++++++---------- src/SMAPI/Framework/ModLoading/ModMetadata.cs | 36 ++++++++------------ .../Framework/ModUpdateChecking/ModUpdateStatus.cs | 32 ++++++++++++++++++ src/SMAPI/Program.cs | 39 +++++++++++----------- src/SMAPI/StardewModdingAPI.csproj | 1 + 5 files changed, 77 insertions(+), 59 deletions(-) create mode 100644 src/SMAPI/Framework/ModUpdateChecking/ModUpdateStatus.cs (limited to 'src/SMAPI') diff --git a/src/SMAPI/Framework/IModMetadata.cs b/src/SMAPI/Framework/IModMetadata.cs index 7bf7d98c..b71c8056 100644 --- a/src/SMAPI/Framework/IModMetadata.cs +++ b/src/SMAPI/Framework/IModMetadata.cs @@ -1,5 +1,6 @@ using StardewModdingAPI.Framework.ModData; using StardewModdingAPI.Framework.ModLoading; +using StardewModdingAPI.Framework.ModUpdateChecking; namespace StardewModdingAPI.Framework { @@ -45,14 +46,11 @@ namespace StardewModdingAPI.Framework /// Whether the mod is a content pack. bool IsContentPack { get; } - /// The latest version of the mod. - ISemanticVersion LatestVersion { get; } + /// The update status of this mod (if any). + ModUpdateStatus UpdateStatus { get; } - /// The latest preview version of the mod, if any. - ISemanticVersion LatestPreviewVersion { get; } - - /// The error checking for updates for this mod, if any. - string UpdateCheckError { get; } + /// The preview update status of this mod (if any). + ModUpdateStatus PreviewUpdateStatus { get; } /********* ** Public methods @@ -80,17 +78,13 @@ namespace StardewModdingAPI.Framework /// The mod-provided API. IModMetadata SetApi(object api); - /// Set the update version. - /// The latest version. - IModMetadata SetUpdateVersion(ISemanticVersion latestVersion); - - /// Set the preview update version. - /// The latest preview version. - IModMetadata SetPreviewUpdateVersion(ISemanticVersion latestPreviewVersion); + /// Set the update status. + /// The mod update status. + IModMetadata SetUpdateStatus(ModUpdateStatus updateStatus); - /// Set the error that occured while checking for updates. - /// The error checking for updates. - IModMetadata SetUpdateError(string updateCheckError); + /// Set the preview update status. + /// The mod preview update status. + IModMetadata SetPreviewUpdateStatus(ModUpdateStatus previewUpdateStatus); /// 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 1ead1387..88d2770c 100644 --- a/src/SMAPI/Framework/ModLoading/ModMetadata.cs +++ b/src/SMAPI/Framework/ModLoading/ModMetadata.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using StardewModdingAPI.Framework.ModData; +using StardewModdingAPI.Framework.ModUpdateChecking; namespace StardewModdingAPI.Framework.ModLoading { @@ -43,14 +44,11 @@ namespace StardewModdingAPI.Framework.ModLoading /// The mod-provided API (if any). public object Api { get; private set; } - /// The latest version of the mod. - public ISemanticVersion LatestVersion { get; private set; } + /// The update status of this mod (if any). + public ModUpdateStatus UpdateStatus { get; private set; } - /// The latest preview version of the mod, if any. - public ISemanticVersion LatestPreviewVersion { get; private set; } - - /// The error checking for updates for this mod, if any. - public string UpdateCheckError { get; private set; } + /// The preview update status of this mod (if any). + public ModUpdateStatus PreviewUpdateStatus { get; private set; } /// Whether the mod is a content pack. public bool IsContentPack => this.Manifest?.ContentPackFor != null; @@ -124,27 +122,19 @@ namespace StardewModdingAPI.Framework.ModLoading return this; } - /// Set the update version. - /// The latest version. - public IModMetadata SetUpdateVersion(ISemanticVersion latestVersion) - { - this.LatestVersion = latestVersion; - return this; - } - - /// Set the preview update version. - /// The latest preview version. - public IModMetadata SetPreviewUpdateVersion(ISemanticVersion latestPreviewVersion) + /// Set the update status. + /// The mod update status. + public IModMetadata SetUpdateStatus(ModUpdateStatus updateStatus) { - this.LatestPreviewVersion = latestPreviewVersion; + this.UpdateStatus = updateStatus; return this; } - /// Set the error that occured while checking for updates. - /// The error checking for updates. - public IModMetadata SetUpdateError(string updateCheckError) + /// Set the preview update status. + /// The mod preview update status. + public IModMetadata SetPreviewUpdateStatus(ModUpdateStatus previewUpdateStatus) { - this.UpdateCheckError = updateCheckError; + this.PreviewUpdateStatus = previewUpdateStatus; return this; } diff --git a/src/SMAPI/Framework/ModUpdateChecking/ModUpdateStatus.cs b/src/SMAPI/Framework/ModUpdateChecking/ModUpdateStatus.cs new file mode 100644 index 00000000..7f588b66 --- /dev/null +++ b/src/SMAPI/Framework/ModUpdateChecking/ModUpdateStatus.cs @@ -0,0 +1,32 @@ +namespace StardewModdingAPI.Framework.ModUpdateChecking +{ + /// Update status for a mod. + internal class ModUpdateStatus + { + /********* + ** Accessors + *********/ + /// The version that this mod can be updated to (if any). + public ISemanticVersion Version { get; } + + /// The error checking for updates of this mod (if any). + public string Error { get; } + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The version that this mod can be update to. + public ModUpdateStatus(ISemanticVersion version) + { + this.Version = version; + } + + /// Construct an instance. + /// The error checking for updates of this mod. + public ModUpdateStatus(string error) + { + this.Error = error; + } + } +} diff --git a/src/SMAPI/Program.cs b/src/SMAPI/Program.cs index e6aafb2d..96061b2a 100644 --- a/src/SMAPI/Program.cs +++ b/src/SMAPI/Program.cs @@ -24,6 +24,7 @@ using StardewModdingAPI.Framework.ModData; using StardewModdingAPI.Framework.Models; using StardewModdingAPI.Framework.ModHelpers; using StardewModdingAPI.Framework.ModLoading; +using StardewModdingAPI.Framework.ModUpdateChecking; using StardewModdingAPI.Framework.Patching; using StardewModdingAPI.Framework.Reflection; using StardewModdingAPI.Framework.Serialisation; @@ -671,46 +672,46 @@ namespace StardewModdingAPI // handle error if (remoteInfo.Error != null) { - if(mod.LatestVersion == null && mod.LatestPreviewVersion == null) - mod.SetUpdateError(remoteInfo.Error); + if (mod.UpdateStatus?.Version == null) + mod.SetUpdateStatus(new ModUpdateStatus(remoteInfo.Error)); + if (mod.PreviewUpdateStatus?.Version == null) + mod.SetUpdateStatus(new ModUpdateStatus(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; - 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 && mod.UpdateStatus?.Version == null) + mod.SetUpdateStatus(new ModUpdateStatus($"Version is invalid: {remoteInfo.Version}")); + if (!validPreviewVersion && mod.PreviewUpdateStatus?.Version == null) + mod.SetPreviewUpdateStatus(new ModUpdateStatus($"Version is invalid: {remoteInfo.PreviewVersion}")); + if (!validVersion && !validPreviewVersion) { - string errorInfo = $"Mod has invalid versions. version: {remoteInfo.Version}, preview version: {remoteInfo.PreviewVersion}"; - - if (mod.LatestVersion == null && mod.LatestPreviewVersion == null) - mod.SetUpdateError(errorInfo); - this.Monitor.Log($" {mod.DisplayName} ({result.Key}): update error: {errorInfo}", LogLevel.Trace); + this.Monitor.Log($" {mod.DisplayName} ({result.Key}): update error: Mod has invalid versions. version: {remoteInfo.Version}, preview version: {remoteInfo.PreviewVersion}", LogLevel.Trace); continue; } // compare versions - bool isNonPreviewUpdate = validVersion && remoteVersion.IsNewerThan(localVersion); + bool isPreviewUpdate = validPreviewVersion && localVersion.IsNewerThan(remoteVersion) && remotePreviewVersion.IsNewerThan(localVersion); + bool isUpdate = (validVersion && remoteVersion.IsNewerThan(localVersion)) || isPreviewUpdate; - bool isUpdate = isNonPreviewUpdate || - (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")}."); + this.VerboseLog($" {mod.DisplayName} ({result.Key}): {(isUpdate ? $"{mod.Manifest.Version}{(!localVersion.Equals(mod.Manifest.Version) ? $" [{localVersion}]" : "")} => {(isPreviewUpdate ? remoteInfo.PreviewVersion : remoteInfo.Version)}" : "okay")}."); if (isUpdate) { - if (!updatesByMod.TryGetValue(mod, out Tuple other) || (isNonPreviewUpdate ? remoteVersion : remotePreviewVersion).IsNewerThan(other.Item2 ? other.Item1.PreviewVersion : other.Item1.Version)) + if (!updatesByMod.TryGetValue(mod, out Tuple other) || (isPreviewUpdate ? remotePreviewVersion : remoteVersion).IsNewerThan(other.Item2 ? other.Item1.PreviewVersion : other.Item1.Version)) { - updatesByMod[mod] = new Tuple(remoteInfo, !isNonPreviewUpdate); + updatesByMod[mod] = new Tuple(remoteInfo, isPreviewUpdate); - if (isNonPreviewUpdate) - mod.SetUpdateVersion(remoteVersion); + if (isPreviewUpdate) + mod.SetPreviewUpdateStatus(new ModUpdateStatus(remotePreviewVersion)); else - mod.SetPreviewUpdateVersion(remotePreviewVersion); - - mod.SetUpdateError(null); + mod.SetUpdateStatus(new ModUpdateStatus(remoteVersion)); } } } diff --git a/src/SMAPI/StardewModdingAPI.csproj b/src/SMAPI/StardewModdingAPI.csproj index 67c48a57..beaad8e0 100644 --- a/src/SMAPI/StardewModdingAPI.csproj +++ b/src/SMAPI/StardewModdingAPI.csproj @@ -108,6 +108,7 @@ + -- cgit