From 3eb98b565f48c26384f0e83e4012fc9b40f1d819 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 29 Jan 2023 16:37:23 -0500 Subject: simplify & validate manifest mod page URLs This avoids an issue where users are told to download it from the JSON manifest URL. --- .../ResponseModels/UpdateManifestModModel.cs | 14 ++++++++------ .../UpdateManifest/ResponseModels/UpdateManifestModel.cs | 6 +++--- .../ResponseModels/UpdateManifestVersionModel.cs | 15 +++++---------- .../Clients/UpdateManifest/UpdateManifestClient.cs | 11 +++++++++++ .../Clients/UpdateManifest/UpdateManifestModPage.cs | 15 ++++++--------- 5 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/SMAPI.Web/Framework/Clients/UpdateManifest/ResponseModels/UpdateManifestModModel.cs b/src/SMAPI.Web/Framework/Clients/UpdateManifest/ResponseModels/UpdateManifestModModel.cs index ee1fbeb6..418fb26b 100644 --- a/src/SMAPI.Web/Framework/Clients/UpdateManifest/ResponseModels/UpdateManifestModModel.cs +++ b/src/SMAPI.Web/Framework/Clients/UpdateManifest/ResponseModels/UpdateManifestModModel.cs @@ -1,4 +1,6 @@ // Copyright 2022 Jamie Taylor +using System; + namespace StardewModdingAPI.Web.Framework.Clients.UpdateManifest.ResponseModels { /// The data model for a mod in an update manifest file. @@ -11,10 +13,10 @@ namespace StardewModdingAPI.Web.Framework.Clients.UpdateManifest.ResponseModels public string? Name { get; } /// The mod page URL from which to download updates. - public string? Url { get; } + public string? ModPageUrl { get; } /// The available versions for this mod. - public UpdateManifestVersionModel[]? Versions { get; } + public UpdateManifestVersionModel[] Versions { get; } /********* @@ -22,13 +24,13 @@ namespace StardewModdingAPI.Web.Framework.Clients.UpdateManifest.ResponseModels *********/ /// Construct an instance. /// The mod's name. - /// The mod page URL from which to download updates. + /// The mod page URL from which to download updates. /// The available versions for this mod. - public UpdateManifestModModel(string? name, string? url, UpdateManifestVersionModel[]? versions) + public UpdateManifestModModel(string? name, string? modPageUrl, UpdateManifestVersionModel[]? versions) { this.Name = name; - this.Url = url; - this.Versions = versions; + this.ModPageUrl = modPageUrl; + this.Versions = versions ?? Array.Empty(); } } } diff --git a/src/SMAPI.Web/Framework/Clients/UpdateManifest/ResponseModels/UpdateManifestModel.cs b/src/SMAPI.Web/Framework/Clients/UpdateManifest/ResponseModels/UpdateManifestModel.cs index ff3dccbc..3b930ff3 100644 --- a/src/SMAPI.Web/Framework/Clients/UpdateManifest/ResponseModels/UpdateManifestModel.cs +++ b/src/SMAPI.Web/Framework/Clients/UpdateManifest/ResponseModels/UpdateManifestModel.cs @@ -13,7 +13,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.UpdateManifest.ResponseModels public string Format { get; } /// The mod info in this update manifest. - public IDictionary? Mods { get; } + public IDictionary Mods { get; } /********* @@ -22,10 +22,10 @@ namespace StardewModdingAPI.Web.Framework.Clients.UpdateManifest.ResponseModels /// Construct an instance. /// The manifest format version. /// The mod info in this update manifest. - public UpdateManifestModel(string format, IDictionary mods) + public UpdateManifestModel(string format, IDictionary? mods) { this.Format = format; - this.Mods = mods; + this.Mods = mods ?? new Dictionary(); } } } diff --git a/src/SMAPI.Web/Framework/Clients/UpdateManifest/ResponseModels/UpdateManifestVersionModel.cs b/src/SMAPI.Web/Framework/Clients/UpdateManifest/ResponseModels/UpdateManifestVersionModel.cs index 1e84501f..7cfb0cfc 100644 --- a/src/SMAPI.Web/Framework/Clients/UpdateManifest/ResponseModels/UpdateManifestVersionModel.cs +++ b/src/SMAPI.Web/Framework/Clients/UpdateManifest/ResponseModels/UpdateManifestVersionModel.cs @@ -10,11 +10,8 @@ namespace StardewModdingAPI.Web.Framework.Clients.UpdateManifest.ResponseModels /// The mod's semantic version. public string? Version { get; } - /// The URL for this version's download page (if any). - public string? DownloadPageUrl { get; } - - /// The URL for this version's direct file download (if any). - public string? DownloadFileUrl { get; } + /// The mod page URL from which to download updates, if different from . + public string? ModPageUrl { get; } /********* @@ -22,13 +19,11 @@ namespace StardewModdingAPI.Web.Framework.Clients.UpdateManifest.ResponseModels *********/ /// Construct an instance. /// The mod's semantic version. - /// This version's download page URL, if any. - /// This version's direct file download URL, if any. - public UpdateManifestVersionModel(string version, string? downloadPageUrl, string? downloadFileUrl) + /// The mod page URL from which to download updates, if different from . + public UpdateManifestVersionModel(string version, string? modPageUrl) { this.Version = version; - this.DownloadPageUrl = downloadPageUrl; - this.DownloadFileUrl = downloadFileUrl; + this.ModPageUrl = modPageUrl; } } } diff --git a/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestClient.cs b/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestClient.cs index cd102ec5..9a2887c2 100644 --- a/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestClient.cs +++ b/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestClient.cs @@ -72,6 +72,17 @@ namespace StardewModdingAPI.Web.Framework.Clients.UpdateManifest // validate if (!SemanticVersion.TryParse(manifest.Format, out _)) return new GenericModPage(this.SiteKey, id).SetError(RemoteModStatus.InvalidData, $"The update manifest at {id} has invalid format version '{manifest.Format}'"); + foreach ((string modKey, UpdateManifestModModel mod) in manifest.Mods) + { + if (string.IsNullOrWhiteSpace(mod.ModPageUrl)) + { + foreach (UpdateManifestVersionModel download in mod.Versions) + { + if (string.IsNullOrWhiteSpace(download.ModPageUrl)) + return new GenericModPage(this.SiteKey, id).SetError(RemoteModStatus.InvalidData, $"The update manifest at {id} is invalid (all mod downloads must have a mod page URL)"); + } + } + } // build model return new UpdateManifestModPage(id, manifest); diff --git a/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModPage.cs b/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModPage.cs index 251ed9ec..f4ad0500 100644 --- a/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModPage.cs +++ b/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModPage.cs @@ -26,7 +26,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.UpdateManifest : base(ModSiteKey.UpdateManifest, url) { this.RequireSubkey = true; - this.Mods = manifest.Mods ?? new Dictionary(); + this.Mods = manifest.Mods; this.SetInfo(name: url, url: url, version: null, downloads: this.ParseDownloads(manifest.Mods).ToArray()); } @@ -35,8 +35,8 @@ namespace StardewModdingAPI.Web.Framework.Clients.UpdateManifest /// The mod name for the given subkey, or if this manifest does not contain the given subkey. public override string? GetName(string? subkey) { - return subkey is not null && this.Mods.TryGetValue(subkey.TrimStart('@'), out UpdateManifestModModel? modModel) - ? modModel.Name + return subkey is not null && this.Mods.TryGetValue(subkey.TrimStart('@'), out UpdateManifestModModel? mod) + ? mod.Name : null; } @@ -45,8 +45,8 @@ namespace StardewModdingAPI.Web.Framework.Clients.UpdateManifest /// The mod URL for the given subkey, or if this manifest does not contain the given subkey. public override string? GetUrl(string? subkey) { - return subkey is not null && this.Mods.TryGetValue(subkey.TrimStart('@'), out UpdateManifestModModel? modModel) - ? modModel.Url + return subkey is not null && this.Mods.TryGetValue(subkey.TrimStart('@'), out UpdateManifestModModel? mod) + ? mod.ModPageUrl : null; } @@ -63,11 +63,8 @@ namespace StardewModdingAPI.Web.Framework.Clients.UpdateManifest foreach ((string modKey, UpdateManifestModModel mod) in mods) { - if (mod.Versions is null) - continue; - foreach (UpdateManifestVersionModel version in mod.Versions) - yield return new UpdateManifestModDownload(modKey, mod.Name ?? modKey, version.Version, version.DownloadFileUrl ?? version.DownloadPageUrl); + yield return new UpdateManifestModDownload(modKey, mod.Name ?? modKey, version.Version, version.ModPageUrl); } } -- cgit