diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2023-01-29 16:37:22 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2023-01-29 16:37:22 -0500 |
commit | 25c2081d43bd4026552cda687fb56216dd3a9f8e (patch) | |
tree | 2cddef2bd5db31a5977d47b5450bfb80e813a8f0 | |
parent | 55fd4839da43e7ca205eaa85480786e3dfe8af6f (diff) | |
download | SMAPI-25c2081d43bd4026552cda687fb56216dd3a9f8e.tar.gz SMAPI-25c2081d43bd4026552cda687fb56216dd3a9f8e.tar.bz2 SMAPI-25c2081d43bd4026552cda687fb56216dd3a9f8e.zip |
encapsulate update manifest implementation details when possible
6 files changed, 37 insertions, 40 deletions
diff --git a/src/SMAPI.Web/Controllers/ModsApiController.cs b/src/SMAPI.Web/Controllers/ModsApiController.cs index 5fc4987d..2003e25f 100644 --- a/src/SMAPI.Web/Controllers/ModsApiController.cs +++ b/src/SMAPI.Web/Controllers/ModsApiController.cs @@ -301,7 +301,7 @@ namespace StardewModdingAPI.Web.Controllers } // get version info - return this.ModSites.GetPageVersions(page, updateKey.Subkey, allowNonStandardVersions, mapRemoteVersions); + return this.ModSites.GetPageVersions(page, updateKey, allowNonStandardVersions, mapRemoteVersions); } /// <summary>Get update keys based on the available mod metadata, while maintaining the precedence order.</summary> diff --git a/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModDownload.cs b/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModDownload.cs index 0a6d4736..0128fa17 100644 --- a/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModDownload.cs +++ b/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModDownload.cs @@ -22,7 +22,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.UpdateManifest public UpdateManifestModDownload(string fieldName, string name, string? version, string? url) : base(name, null, version, url) { - this.Subkey = fieldName; + this.Subkey = '@' + fieldName; } /// <summary>Get whether the subkey matches this download.</summary> diff --git a/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModPage.cs b/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModPage.cs index bbc7b5da..befad268 100644 --- a/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModPage.cs +++ b/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModPage.cs @@ -34,7 +34,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.UpdateManifest /// <returns>The mod name for the given subkey, or <see langword="null"/> if this manifest does not contain the given subkey.</returns> public override string? GetName(string? subkey) { - return subkey is not null && this.Mods.TryGetValue(subkey, out UpdateManifestModModel? modModel) + return subkey is not null && this.Mods.TryGetValue(subkey.TrimStart('@'), out UpdateManifestModModel? modModel) ? modModel.Name : null; } @@ -44,7 +44,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.UpdateManifest /// <returns>The mod URL for the given subkey, or <see langword="null"/> if this manifest does not contain the given subkey.</returns> public override string? GetUrl(string? subkey) { - return subkey is not null && this.Mods.TryGetValue(subkey, out UpdateManifestModModel? modModel) + return subkey is not null && this.Mods.TryGetValue(subkey.TrimStart('@'), out UpdateManifestModModel? modModel) ? modModel.Url : null; } diff --git a/src/SMAPI.Web/Framework/IModPage.cs b/src/SMAPI.Web/Framework/IModPage.cs index ef1513eb..84af9516 100644 --- a/src/SMAPI.Web/Framework/IModPage.cs +++ b/src/SMAPI.Web/Framework/IModPage.cs @@ -39,7 +39,7 @@ namespace StardewModdingAPI.Web.Framework [MemberNotNullWhen(false, nameof(IModPage.Error))] bool IsValid { get; } - /// <summary>Whether this mod page requires string subkey matching, in which case a subkey that isn't found will return no update instead of falling back to one without. Additionally, the leading <c>@</c> is stripped from the subkey value before searching for matches.</summary> + /// <summary>Whether this mod page requires string subkey matching, in which case a subkey that isn't found will return no update instead of falling back to one without.</summary> bool IsSubkeyStrict { get; } diff --git a/src/SMAPI.Web/Framework/ModSiteManager.cs b/src/SMAPI.Web/Framework/ModSiteManager.cs index 350159a3..f1a088d5 100644 --- a/src/SMAPI.Web/Framework/ModSiteManager.cs +++ b/src/SMAPI.Web/Framework/ModSiteManager.cs @@ -59,11 +59,13 @@ namespace StardewModdingAPI.Web.Framework /// <summary>Parse version info for the given mod page info.</summary> /// <param name="page">The mod page info.</param> - /// <param name="subkey">The optional update subkey to match in available files. (If no file names or descriptions contain the subkey, it'll be ignored.)</param> + /// <param name="updateKey">The update key to match in available files.</param> /// <param name="mapRemoteVersions">The changes to apply to remote versions for update checks.</param> /// <param name="allowNonStandardVersions">Whether to allow non-standard versions.</param> - public ModInfoModel GetPageVersions(IModPage page, string? subkey, bool allowNonStandardVersions, ChangeDescriptor? mapRemoteVersions) + public ModInfoModel GetPageVersions(IModPage page, UpdateKey updateKey, bool allowNonStandardVersions, ChangeDescriptor? mapRemoteVersions) { + bool isManifest = updateKey.Site == ModSiteKey.UpdateManifest; + // get base model ModInfoModel model = new(); if (!page.IsValid) @@ -71,23 +73,23 @@ namespace StardewModdingAPI.Web.Framework model.SetError(page.Status, page.Error); return model; } - - // trim subkey in strict mode - if (page.IsSubkeyStrict && subkey is not null) - { - if (subkey.StartsWith('@')) - subkey = subkey.Substring(1); - } + else if (!isManifest) // if this is a manifest, the 'mod page' is the JSON file + model.SetBasicInfo(page.Name, page.Url); // fetch versions - bool hasVersions = this.TryGetLatestVersions(page, subkey, allowNonStandardVersions, mapRemoteVersions, out ISemanticVersion? mainVersion, out ISemanticVersion? previewVersion, out string? mainModPageUrl, out string? previewModPageUrl); + bool hasVersions = this.TryGetLatestVersions(page, updateKey.Subkey, allowNonStandardVersions, mapRemoteVersions, out ISemanticVersion? mainVersion, out ISemanticVersion? previewVersion, out string? mainModPageUrl, out string? previewModPageUrl); if (!hasVersions) - return model.SetError(RemoteModStatus.InvalidData, $"The {page.Site} mod with ID '{page.Id}{subkey ?? ""}' has no valid versions."); + { + string displayId = isManifest + ? page.Id + updateKey.Subkey + : page.Id; + return model.SetError(RemoteModStatus.InvalidData, $"The {page.Site} mod with ID '{displayId}' has no valid versions."); + } // apply mod page info model.SetBasicInfo( - name: page.GetName(subkey) ?? page.Name, - url: page.GetUrl(subkey) ?? page.Url + name: page.GetName(updateKey.Subkey) ?? page.Name, + url: page.GetUrl(updateKey.Subkey) ?? page.Url ); // return info @@ -132,30 +134,29 @@ namespace StardewModdingAPI.Web.Framework preview = null; mainModPageUrl = null; previewModPageUrl = null; + if (mod is null) + return false; // parse all versions from the mod page IEnumerable<(IModDownload? download, ISemanticVersion? version)> GetAllVersions() { - if (mod != null) + ISemanticVersion? ParseAndMapVersion(string? raw) { - ISemanticVersion? ParseAndMapVersion(string? raw) - { - raw = this.NormalizeVersion(raw); - return this.GetMappedVersion(raw, mapRemoteVersions, allowNonStandardVersions); - } + raw = this.NormalizeVersion(raw); + return this.GetMappedVersion(raw, mapRemoteVersions, allowNonStandardVersions); + } - // get mod version - ISemanticVersion? modVersion = ParseAndMapVersion(mod.Version); - if (modVersion != null) - yield return (download: null, version: modVersion); + // get mod version + ISemanticVersion? modVersion = ParseAndMapVersion(mod.Version); + if (modVersion != null) + yield return (download: null, version: modVersion); - // get file versions - foreach (IModDownload download in mod.Downloads) - { - ISemanticVersion? cur = ParseAndMapVersion(download.Version); - if (cur != null) - yield return (download, cur); - } + // get file versions + foreach (IModDownload download in mod.Downloads) + { + ISemanticVersion? cur = ParseAndMapVersion(download.Version); + if (cur != null) + yield return (download, cur); } } var versions = GetAllVersions() @@ -213,7 +214,7 @@ namespace StardewModdingAPI.Web.Framework if (subkey is not null) { TryGetVersions(out main, out preview, out mainModPageUrl, out previewModPageUrl, filter: entry => entry.download?.MatchesSubkey(subkey) == true); - if (mod?.IsSubkeyStrict == true) + if (mod.IsSubkeyStrict) return main != null; } diff --git a/src/SMAPI.Web/SMAPI.Web.csproj b/src/SMAPI.Web/SMAPI.Web.csproj index cfec1d08..d26cb6f8 100644 --- a/src/SMAPI.Web/SMAPI.Web.csproj +++ b/src/SMAPI.Web/SMAPI.Web.csproj @@ -12,7 +12,6 @@ <None Remove="Properties\PublishProfiles\**" /> <None Remove="Properties\ServiceDependencies\**" /> <Content Remove="aws-beanstalk-tools-defaults.json" /> - <None Remove="Framework\Clients\UpdateManifest\" /> </ItemGroup> <ItemGroup> @@ -49,7 +48,4 @@ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> </ItemGroup> - <ItemGroup> - <Folder Include="Framework\Clients\UpdateManifest\" /> - </ItemGroup> </Project> |