From 610e2722c6687591faacb942d6f752c5f3c620d7 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 29 Jan 2023 16:37:23 -0500 Subject: rename & validate format version --- .../ResponseModels/UpdateManifestModel.cs | 10 +++++----- .../Clients/UpdateManifest/UpdateManifestClient.cs | 21 ++++++++++++++++++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/SMAPI.Web/Framework/Clients/UpdateManifest/ResponseModels/UpdateManifestModel.cs b/src/SMAPI.Web/Framework/Clients/UpdateManifest/ResponseModels/UpdateManifestModel.cs index e213fbab..ff3dccbc 100644 --- a/src/SMAPI.Web/Framework/Clients/UpdateManifest/ResponseModels/UpdateManifestModel.cs +++ b/src/SMAPI.Web/Framework/Clients/UpdateManifest/ResponseModels/UpdateManifestModel.cs @@ -9,8 +9,8 @@ namespace StardewModdingAPI.Web.Framework.Clients.UpdateManifest.ResponseModels /********* ** Accessors *********/ - /// The manifest format version. - public string? ManifestVersion { get; } + /// The manifest format version. This is equivalent to the SMAPI version, and is used to parse older manifests correctly if later versions of SMAPI change the expected format. + public string Format { get; } /// The mod info in this update manifest. public IDictionary? Mods { get; } @@ -20,11 +20,11 @@ namespace StardewModdingAPI.Web.Framework.Clients.UpdateManifest.ResponseModels ** Public methods *********/ /// Construct an instance. - /// The manifest format version. + /// The manifest format version. /// The mod info in this update manifest. - public UpdateManifestModel(string manifestVersion, IDictionary mods) + public UpdateManifestModel(string format, IDictionary mods) { - this.ManifestVersion = manifestVersion; + this.Format = format; this.Mods = mods; } } diff --git a/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestClient.cs b/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestClient.cs index 0d19900e..cd102ec5 100644 --- a/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestClient.cs +++ b/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestClient.cs @@ -1,8 +1,12 @@ // Copyright 2022 Jamie Taylor + +using System; +using System.Diagnostics.CodeAnalysis; using System.Net; using System.Net.Http.Headers; using System.Threading.Tasks; using Pathoschild.Http.Client; +using StardewModdingAPI.Toolkit; using StardewModdingAPI.Toolkit.Framework.UpdateData; using StardewModdingAPI.Web.Framework.Clients.UpdateManifest.ResponseModels; @@ -45,21 +49,32 @@ namespace StardewModdingAPI.Web.Framework.Clients.UpdateManifest } /// + [SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract", Justification = "This is the method which ensures the annotations are correct.")] public async Task GetModData(string id) { + // get raw update manifest UpdateManifestModel? manifest; try { manifest = await this.Client.GetAsync(id).As(); + if (manifest is null) + return new GenericModPage(this.SiteKey, id).SetError(RemoteModStatus.InvalidData, $"The update manifest at {id} is empty"); } catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound) { return new GenericModPage(this.SiteKey, id).SetError(RemoteModStatus.DoesNotExist, $"No update manifest found at {id}"); } + catch (Exception ex) + { + return new GenericModPage(this.SiteKey, id).SetError(RemoteModStatus.InvalidData, $"The update manifest at {id} has an invalid format: {ex.Message}"); + } + + // 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}'"); - return manifest is not null - ? new UpdateManifestModPage(id, manifest) - : new GenericModPage(this.SiteKey, id).SetError(RemoteModStatus.DoesNotExist, $"The update manifest at {id} has an invalid format"); + // build model + return new UpdateManifestModPage(id, manifest); } } } -- cgit