summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Framework
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI.Web/Framework')
-rw-r--r--src/SMAPI.Web/Framework/AllowLargePostsAttribute.cs4
-rw-r--r--src/SMAPI.Web/Framework/Caching/Cached.cs11
-rw-r--r--src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs5
-rw-r--r--src/SMAPI.Web/Framework/Caching/Mods/ModCacheMemoryRepository.cs5
-rw-r--r--src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs9
-rw-r--r--src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMemoryRepository.cs11
-rw-r--r--src/SMAPI.Web/Framework/Caching/Wiki/WikiMetadata.cs11
-rw-r--r--src/SMAPI.Web/Framework/Clients/Chucklefish/ChucklefishClient.cs12
-rw-r--r--src/SMAPI.Web/Framework/Clients/CurseForge/CurseForgeClient.cs14
-rw-r--r--src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModFileModel.cs22
-rw-r--r--src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModModel.cs30
-rw-r--r--src/SMAPI.Web/Framework/Clients/GenericModDownload.cs13
-rw-r--r--src/SMAPI.Web/Framework/Clients/GenericModPage.cs23
-rw-r--r--src/SMAPI.Web/Framework/Clients/GitHub/GitAsset.cs26
-rw-r--r--src/SMAPI.Web/Framework/Clients/GitHub/GitHubClient.cs30
-rw-r--r--src/SMAPI.Web/Framework/Clients/GitHub/GitLicense.cs26
-rw-r--r--src/SMAPI.Web/Framework/Clients/GitHub/GitRelease.cs36
-rw-r--r--src/SMAPI.Web/Framework/Clients/GitHub/GitRepo.cs26
-rw-r--r--src/SMAPI.Web/Framework/Clients/GitHub/IGitHubClient.cs6
-rw-r--r--src/SMAPI.Web/Framework/Clients/IModSiteClient.cs4
-rw-r--r--src/SMAPI.Web/Framework/Clients/ModDrop/ModDropClient.cs20
-rw-r--r--src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/FileDataModel.cs42
-rw-r--r--src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModDataModel.cs24
-rw-r--r--src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModListModel.cs7
-rw-r--r--src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModModel.cs22
-rw-r--r--src/SMAPI.Web/Framework/Clients/Nexus/NexusClient.cs63
-rw-r--r--src/SMAPI.Web/Framework/Clients/Nexus/ResponseModels/NexusMod.cs43
-rw-r--r--src/SMAPI.Web/Framework/Clients/Pastebin/IPastebinClient.cs2
-rw-r--r--src/SMAPI.Web/Framework/Clients/Pastebin/PasteInfo.cs28
-rw-r--r--src/SMAPI.Web/Framework/Clients/Pastebin/PastebinClient.cs14
-rw-r--r--src/SMAPI.Web/Framework/Compression/GzipHelper.cs9
-rw-r--r--src/SMAPI.Web/Framework/Compression/IGzipHelper.cs5
-rw-r--r--src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs36
-rw-r--r--src/SMAPI.Web/Framework/ConfigModels/ModOverrideConfig.cs6
-rw-r--r--src/SMAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs10
-rw-r--r--src/SMAPI.Web/Framework/ConfigModels/SiteConfig.cs6
-rw-r--r--src/SMAPI.Web/Framework/ConfigModels/SmapiInfoConfig.cs8
-rw-r--r--src/SMAPI.Web/Framework/Extensions.cs8
-rw-r--r--src/SMAPI.Web/Framework/IModDownload.cs9
-rw-r--r--src/SMAPI.Web/Framework/IModPage.cs18
-rw-r--r--src/SMAPI.Web/Framework/InternalControllerFeatureProvider.cs2
-rw-r--r--src/SMAPI.Web/Framework/JobDashboardAuthorizationFilter.cs2
-rw-r--r--src/SMAPI.Web/Framework/ModInfoModel.cs33
-rw-r--r--src/SMAPI.Web/Framework/ModSiteManager.cs58
-rw-r--r--src/SMAPI.Web/Framework/RedirectRules/RedirectHostsToUrlsRule.cs10
-rw-r--r--src/SMAPI.Web/Framework/RedirectRules/RedirectMatchRule.cs6
-rw-r--r--src/SMAPI.Web/Framework/RedirectRules/RedirectPathsToUrlsRule.cs6
-rw-r--r--src/SMAPI.Web/Framework/RedirectRules/RedirectToHttpsRule.cs6
-rw-r--r--src/SMAPI.Web/Framework/Storage/IStorageProvider.cs2
-rw-r--r--src/SMAPI.Web/Framework/Storage/StorageProvider.cs52
-rw-r--r--src/SMAPI.Web/Framework/Storage/StoredFileInfo.cs41
-rw-r--r--src/SMAPI.Web/Framework/Storage/UploadResult.cs14
-rw-r--r--src/SMAPI.Web/Framework/VersionConstraint.cs6
53 files changed, 557 insertions, 385 deletions
diff --git a/src/SMAPI.Web/Framework/AllowLargePostsAttribute.cs b/src/SMAPI.Web/Framework/AllowLargePostsAttribute.cs
index 108ceff7..bd414ea2 100644
--- a/src/SMAPI.Web/Framework/AllowLargePostsAttribute.cs
+++ b/src/SMAPI.Web/Framework/AllowLargePostsAttribute.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.Filters;
@@ -42,7 +40,7 @@ namespace StardewModdingAPI.Web.Framework
public void OnAuthorization(AuthorizationFilterContext context)
{
IFeatureCollection features = context.HttpContext.Features;
- IFormFeature formFeature = features.Get<IFormFeature>();
+ IFormFeature? formFeature = features.Get<IFormFeature>();
if (formFeature?.Form == null)
{
diff --git a/src/SMAPI.Web/Framework/Caching/Cached.cs b/src/SMAPI.Web/Framework/Caching/Cached.cs
index aabbf146..b393e1e1 100644
--- a/src/SMAPI.Web/Framework/Caching/Cached.cs
+++ b/src/SMAPI.Web/Framework/Caching/Cached.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System;
namespace StardewModdingAPI.Web.Framework.Caching
@@ -12,21 +10,18 @@ namespace StardewModdingAPI.Web.Framework.Caching
** Accessors
*********/
/// <summary>The cached data.</summary>
- public T Data { get; set; }
+ public T Data { get; }
/// <summary>When the data was last updated.</summary>
- public DateTimeOffset LastUpdated { get; set; }
+ public DateTimeOffset LastUpdated { get; }
/// <summary>When the data was last requested through the mod API.</summary>
- public DateTimeOffset LastRequested { get; set; }
+ public DateTimeOffset LastRequested { get; internal set; }
/*********
** Public methods
*********/
- /// <summary>Construct an empty instance.</summary>
- public Cached() { }
-
/// <summary>Construct an instance.</summary>
/// <param name="data">The cached data.</param>
public Cached(T data)
diff --git a/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs b/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs
index 2020d747..fb74e9da 100644
--- a/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs
+++ b/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs
@@ -1,6 +1,5 @@
-#nullable disable
-
using System;
+using System.Diagnostics.CodeAnalysis;
using StardewModdingAPI.Toolkit.Framework.UpdateData;
namespace StardewModdingAPI.Web.Framework.Caching.Mods
@@ -16,7 +15,7 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods
/// <param name="id">The mod's unique ID within the <paramref name="site"/>.</param>
/// <param name="mod">The fetched mod.</param>
/// <param name="markRequested">Whether to update the mod's 'last requested' date.</param>
- bool TryGetMod(ModSiteKey site, string id, out Cached<IModPage> mod, bool markRequested = true);
+ bool TryGetMod(ModSiteKey site, string id, [NotNullWhen(true)] out Cached<IModPage>? mod, bool markRequested = true);
/// <summary>Save data fetched for a mod.</summary>
/// <param name="site">The mod site on which the mod is found.</param>
diff --git a/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMemoryRepository.cs b/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMemoryRepository.cs
index 338562d8..4ba0bd20 100644
--- a/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMemoryRepository.cs
+++ b/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMemoryRepository.cs
@@ -1,7 +1,6 @@
-#nullable disable
-
using System;
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
using System.Linq;
using StardewModdingAPI.Toolkit.Framework.UpdateData;
@@ -25,7 +24,7 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods
/// <param name="id">The mod's unique ID within the <paramref name="site"/>.</param>
/// <param name="mod">The fetched mod.</param>
/// <param name="markRequested">Whether to update the mod's 'last requested' date.</param>
- public bool TryGetMod(ModSiteKey site, string id, out Cached<IModPage> mod, bool markRequested = true)
+ public bool TryGetMod(ModSiteKey site, string id, [NotNullWhen(true)] out Cached<IModPage>? mod, bool markRequested = true)
{
// get mod
if (!this.Mods.TryGetValue(this.GetKey(site, id), out var cachedMod))
diff --git a/src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs b/src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs
index 6edafddc..b8a0df34 100644
--- a/src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs
+++ b/src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs
@@ -1,7 +1,6 @@
-#nullable disable
-
using System;
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
using StardewModdingAPI.Toolkit.Framework.Clients.Wiki;
namespace StardewModdingAPI.Web.Framework.Caching.Wiki
@@ -14,16 +13,16 @@ namespace StardewModdingAPI.Web.Framework.Caching.Wiki
*********/
/// <summary>Get the cached wiki metadata.</summary>
/// <param name="metadata">The fetched metadata.</param>
- bool TryGetWikiMetadata(out Cached<WikiMetadata> metadata);
+ bool TryGetWikiMetadata([NotNullWhen(true)] out Cached<WikiMetadata>? metadata);
/// <summary>Get the cached wiki mods.</summary>
/// <param name="filter">A filter to apply, if any.</param>
- IEnumerable<Cached<WikiModEntry>> GetWikiMods(Func<WikiModEntry, bool> filter = null);
+ IEnumerable<Cached<WikiModEntry>> GetWikiMods(Func<WikiModEntry, bool>? filter = null);
/// <summary>Save data fetched from the wiki compatibility list.</summary>
/// <param name="stableVersion">The current stable Stardew Valley version.</param>
/// <param name="betaVersion">The current beta Stardew Valley version.</param>
/// <param name="mods">The mod data.</param>
- void SaveWikiData(string stableVersion, string betaVersion, IEnumerable<WikiModEntry> mods);
+ void SaveWikiData(string? stableVersion, string? betaVersion, IEnumerable<WikiModEntry> mods);
}
}
diff --git a/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMemoryRepository.cs b/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMemoryRepository.cs
index d1ccb9c7..8b4338e2 100644
--- a/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMemoryRepository.cs
+++ b/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMemoryRepository.cs
@@ -1,7 +1,6 @@
-#nullable disable
-
using System;
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
using System.Linq;
using StardewModdingAPI.Toolkit.Framework.Clients.Wiki;
@@ -14,7 +13,7 @@ namespace StardewModdingAPI.Web.Framework.Caching.Wiki
** Fields
*********/
/// <summary>The saved wiki metadata.</summary>
- private Cached<WikiMetadata> Metadata;
+ private Cached<WikiMetadata>? Metadata;
/// <summary>The cached wiki data.</summary>
private Cached<WikiModEntry>[] Mods = Array.Empty<Cached<WikiModEntry>>();
@@ -25,7 +24,7 @@ namespace StardewModdingAPI.Web.Framework.Caching.Wiki
*********/
/// <summary>Get the cached wiki metadata.</summary>
/// <param name="metadata">The fetched metadata.</param>
- public bool TryGetWikiMetadata(out Cached<WikiMetadata> metadata)
+ public bool TryGetWikiMetadata([NotNullWhen(true)] out Cached<WikiMetadata>? metadata)
{
metadata = this.Metadata;
return metadata != null;
@@ -33,7 +32,7 @@ namespace StardewModdingAPI.Web.Framework.Caching.Wiki
/// <summary>Get the cached wiki mods.</summary>
/// <param name="filter">A filter to apply, if any.</param>
- public IEnumerable<Cached<WikiModEntry>> GetWikiMods(Func<WikiModEntry, bool> filter = null)
+ public IEnumerable<Cached<WikiModEntry>> GetWikiMods(Func<WikiModEntry, bool>? filter = null)
{
foreach (var mod in this.Mods)
{
@@ -46,7 +45,7 @@ namespace StardewModdingAPI.Web.Framework.Caching.Wiki
/// <param name="stableVersion">The current stable Stardew Valley version.</param>
/// <param name="betaVersion">The current beta Stardew Valley version.</param>
/// <param name="mods">The mod data.</param>
- public void SaveWikiData(string stableVersion, string betaVersion, IEnumerable<WikiModEntry> mods)
+ public void SaveWikiData(string? stableVersion, string? betaVersion, IEnumerable<WikiModEntry> mods)
{
this.Metadata = new Cached<WikiMetadata>(new WikiMetadata(stableVersion, betaVersion));
this.Mods = mods.Select(mod => new Cached<WikiModEntry>(mod)).ToArray();
diff --git a/src/SMAPI.Web/Framework/Caching/Wiki/WikiMetadata.cs b/src/SMAPI.Web/Framework/Caching/Wiki/WikiMetadata.cs
index 6ae42488..f53ea201 100644
--- a/src/SMAPI.Web/Framework/Caching/Wiki/WikiMetadata.cs
+++ b/src/SMAPI.Web/Framework/Caching/Wiki/WikiMetadata.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
namespace StardewModdingAPI.Web.Framework.Caching.Wiki
{
/// <summary>The model for cached wiki metadata.</summary>
@@ -9,22 +7,19 @@ namespace StardewModdingAPI.Web.Framework.Caching.Wiki
** Accessors
*********/
/// <summary>The current stable Stardew Valley version.</summary>
- public string StableVersion { get; set; }
+ public string? StableVersion { get; }
/// <summary>The current beta Stardew Valley version.</summary>
- public string BetaVersion { get; set; }
+ public string? BetaVersion { get; }
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
- public WikiMetadata() { }
-
- /// <summary>Construct an instance.</summary>
/// <param name="stableVersion">The current stable Stardew Valley version.</param>
/// <param name="betaVersion">The current beta Stardew Valley version.</param>
- public WikiMetadata(string stableVersion, string betaVersion)
+ public WikiMetadata(string? stableVersion, string? betaVersion)
{
this.StableVersion = stableVersion;
this.BetaVersion = betaVersion;
diff --git a/src/SMAPI.Web/Framework/Clients/Chucklefish/ChucklefishClient.cs b/src/SMAPI.Web/Framework/Clients/Chucklefish/ChucklefishClient.cs
index 4d041c1b..ce0f1122 100644
--- a/src/SMAPI.Web/Framework/Clients/Chucklefish/ChucklefishClient.cs
+++ b/src/SMAPI.Web/Framework/Clients/Chucklefish/ChucklefishClient.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System;
using System.Net;
using System.Threading.Tasks;
@@ -44,7 +42,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.Chucklefish
/// <summary>Get update check info about a mod.</summary>
/// <param name="id">The mod ID.</param>
- public async Task<IModPage> GetModData(string id)
+ public async Task<IModPage?> GetModData(string id)
{
IModPage page = new GenericModPage(this.SiteKey, id);
@@ -53,7 +51,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.Chucklefish
return page.SetError(RemoteModStatus.DoesNotExist, $"The value '{id}' isn't a valid Chucklefish mod ID, must be an integer ID.");
// fetch HTML
- string html;
+ string? html;
try
{
html = await this.Client
@@ -69,7 +67,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.Chucklefish
// extract mod info
string url = this.GetModUrl(parsedId);
- string version = doc.DocumentNode.SelectSingleNode("//h1/span")?.InnerText;
+ string? version = doc.DocumentNode.SelectSingleNode("//h1/span")?.InnerText;
string name = doc.DocumentNode.SelectSingleNode("//h1").ChildNodes[0].InnerText.Trim();
if (name.StartsWith("[SMAPI]"))
name = name.Substring("[SMAPI]".Length).TrimStart();
@@ -81,7 +79,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.Chucklefish
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose()
{
- this.Client?.Dispose();
+ this.Client.Dispose();
}
@@ -92,7 +90,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.Chucklefish
/// <param name="id">The mod ID.</param>
private string GetModUrl(uint id)
{
- UriBuilder builder = new(this.Client.BaseClient.BaseAddress);
+ UriBuilder builder = new(this.Client.BaseClient.BaseAddress!);
builder.Path += string.Format(this.ModPageUrlFormat, id);
return builder.Uri.ToString();
}
diff --git a/src/SMAPI.Web/Framework/Clients/CurseForge/CurseForgeClient.cs b/src/SMAPI.Web/Framework/Clients/CurseForge/CurseForgeClient.cs
index 5ef369d5..d351b42d 100644
--- a/src/SMAPI.Web/Framework/Clients/CurseForge/CurseForgeClient.cs
+++ b/src/SMAPI.Web/Framework/Clients/CurseForge/CurseForgeClient.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
@@ -42,7 +40,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.CurseForge
/// <summary>Get update check info about a mod.</summary>
/// <param name="id">The mod ID.</param>
- public async Task<IModPage> GetModData(string id)
+ public async Task<IModPage?> GetModData(string id)
{
IModPage page = new GenericModPage(this.SiteKey, id);
@@ -51,9 +49,9 @@ namespace StardewModdingAPI.Web.Framework.Clients.CurseForge
return page.SetError(RemoteModStatus.DoesNotExist, $"The value '{id}' isn't a valid CurseForge mod ID, must be an integer ID.");
// get raw data
- ModModel mod = await this.Client
+ ModModel? mod = await this.Client
.GetAsync($"addon/{parsedId}")
- .As<ModModel>();
+ .As<ModModel?>();
if (mod == null)
return page.SetError(RemoteModStatus.DoesNotExist, "Found no CurseForge mod with this ID.");
@@ -73,7 +71,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.CurseForge
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose()
{
- this.Client?.Dispose();
+ this.Client.Dispose();
}
@@ -82,9 +80,9 @@ namespace StardewModdingAPI.Web.Framework.Clients.CurseForge
*********/
/// <summary>Get a raw version string for a mod file, if available.</summary>
/// <param name="file">The file whose version to get.</param>
- private string GetRawVersion(ModFileModel file)
+ private string? GetRawVersion(ModFileModel file)
{
- Match match = this.VersionInNamePattern.Match(file.DisplayName);
+ Match match = this.VersionInNamePattern.Match(file.DisplayName ?? "");
if (!match.Success)
match = this.VersionInNamePattern.Match(file.FileName);
diff --git a/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModFileModel.cs b/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModFileModel.cs
index eabef9f0..e9adcf20 100644
--- a/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModFileModel.cs
+++ b/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModFileModel.cs
@@ -1,14 +1,28 @@
-#nullable disable
-
namespace StardewModdingAPI.Web.Framework.Clients.CurseForge.ResponseModels
{
/// <summary>Metadata from the CurseForge API about a mod file.</summary>
public class ModFileModel
{
+ /*********
+ ** Accessors
+ *********/
/// <summary>The file name as downloaded.</summary>
- public string FileName { get; set; }
+ public string FileName { get; }
/// <summary>The file display name.</summary>
- public string DisplayName { get; set; }
+ public string? DisplayName { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="fileName">The file name as downloaded.</param>
+ /// <param name="displayName">The file display name.</param>
+ public ModFileModel(string fileName, string? displayName)
+ {
+ this.FileName = fileName;
+ this.DisplayName = displayName;
+ }
}
}
diff --git a/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModModel.cs b/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModModel.cs
index a95df7f1..fd7796f2 100644
--- a/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModModel.cs
+++ b/src/SMAPI.Web/Framework/Clients/CurseForge/ResponseModels/ModModel.cs
@@ -1,20 +1,38 @@
-#nullable disable
-
namespace StardewModdingAPI.Web.Framework.Clients.CurseForge.ResponseModels
{
/// <summary>An mod from the CurseForge API.</summary>
public class ModModel
{
+ /*********
+ ** Accessors
+ *********/
/// <summary>The mod's unique ID on CurseForge.</summary>
- public int ID { get; set; }
+ public int ID { get; }
/// <summary>The mod name.</summary>
- public string Name { get; set; }
+ public string Name { get; }
/// <summary>The web URL for the mod page.</summary>
- public string WebsiteUrl { get; set; }
+ public string WebsiteUrl { get; }
/// <summary>The available file downloads.</summary>
- public ModFileModel[] LatestFiles { get; set; }
+ public ModFileModel[] LatestFiles { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="id">The mod's unique ID on CurseForge.</param>
+ /// <param name="name">The mod name.</param>
+ /// <param name="websiteUrl">The web URL for the mod page.</param>
+ /// <param name="latestFiles">The available file downloads.</param>
+ public ModModel(int id, string name, string websiteUrl, ModFileModel[] latestFiles)
+ {
+ this.ID = id;
+ this.Name = name;
+ this.WebsiteUrl = websiteUrl;
+ this.LatestFiles = latestFiles;
+ }
}
}
diff --git a/src/SMAPI.Web/Framework/Clients/GenericModDownload.cs b/src/SMAPI.Web/Framework/Clients/GenericModDownload.cs
index 919072b0..548f17c3 100644
--- a/src/SMAPI.Web/Framework/Clients/GenericModDownload.cs
+++ b/src/SMAPI.Web/Framework/Clients/GenericModDownload.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
namespace StardewModdingAPI.Web.Framework.Clients
{
/// <summary>Generic metadata about a file download on a mod page.</summary>
@@ -9,26 +7,23 @@ namespace StardewModdingAPI.Web.Framework.Clients
** Accessors
*********/
/// <summary>The download's display name.</summary>
- public string Name { get; set; }
+ public string Name { get; }
/// <summary>The download's description.</summary>
- public string Description { get; set; }
+ public string? Description { get; }
/// <summary>The download's file version.</summary>
- public string Version { get; set; }
+ public string? Version { get; }
/*********
** Public methods
*********/
- /// <summary>Construct an empty instance.</summary>
- public GenericModDownload() { }
-
/// <summary>Construct an instance.</summary>
/// <param name="name">The download's display name.</param>
/// <param name="description">The download's description.</param>
/// <param name="version">The download's file version.</param>
- public GenericModDownload(string name, string description, string version)
+ public GenericModDownload(string name, string? description, string? version)
{
this.Name = name;
this.Description = description;
diff --git a/src/SMAPI.Web/Framework/Clients/GenericModPage.cs b/src/SMAPI.Web/Framework/Clients/GenericModPage.cs
index 4788aa2a..5353c7e1 100644
--- a/src/SMAPI.Web/Framework/Clients/GenericModPage.cs
+++ b/src/SMAPI.Web/Framework/Clients/GenericModPage.cs
@@ -1,7 +1,6 @@
-#nullable disable
-
using System;
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
using System.Linq;
using StardewModdingAPI.Toolkit.Framework.UpdateData;
@@ -20,30 +19,31 @@ namespace StardewModdingAPI.Web.Framework.Clients
public string Id { get; set; }
/// <summary>The mod name.</summary>
- public string Name { get; set; }
+ public string? Name { get; set; }
/// <summary>The mod's semantic version number.</summary>
- public string Version { get; set; }
+ public string? Version { get; set; }
/// <summary>The mod's web URL.</summary>
- public string Url { get; set; }
+ public string? Url { get; set; }
/// <summary>The mod downloads.</summary>
public IModDownload[] Downloads { get; set; } = Array.Empty<IModDownload>();
/// <summary>The mod availability status on the remote site.</summary>
- public RemoteModStatus Status { get; set; } = RemoteModStatus.Ok;
+ public RemoteModStatus Status { get; set; } = RemoteModStatus.InvalidData;
/// <summary>A user-friendly error which indicates why fetching the mod info failed (if applicable).</summary>
- public string Error { get; set; }
+ public string? Error { get; set; }
+
+ /// <summary>Whether the mod data is valid.</summary>
+ [MemberNotNullWhen(true, nameof(IModPage.Name), nameof(IModPage.Url))]
+ public bool IsValid => this.Status == RemoteModStatus.Ok;
/*********
** Public methods
*********/
- /// <summary>Construct an empty instance.</summary>
- public GenericModPage() { }
-
/// <summary>Construct an instance.</summary>
/// <param name="site">The mod site containing the mod.</param>
/// <param name="id">The mod's unique ID within the site.</param>
@@ -58,12 +58,13 @@ namespace StardewModdingAPI.Web.Framework.Clients
/// <param name="version">The mod's semantic version number.</param>
/// <param name="url">The mod's web URL.</param>
/// <param name="downloads">The mod downloads.</param>
- public IModPage SetInfo(string name, string version, string url, IEnumerable<IModDownload> downloads)
+ public IModPage SetInfo(string name, string? version, string url, IEnumerable<IModDownload> downloads)
{
this.Name = name;
this.Version = version;
this.Url = url;
this.Downloads = downloads.ToArray();
+ this.Status = RemoteModStatus.Ok;
return this;
}
diff --git a/src/SMAPI.Web/Framework/Clients/GitHub/GitAsset.cs b/src/SMAPI.Web/Framework/Clients/GitHub/GitAsset.cs
index 39ebf94e..dbce9368 100644
--- a/src/SMAPI.Web/Framework/Clients/GitHub/GitAsset.cs
+++ b/src/SMAPI.Web/Framework/Clients/GitHub/GitAsset.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using Newtonsoft.Json;
namespace StardewModdingAPI.Web.Framework.Clients.GitHub
@@ -7,16 +5,34 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub
/// <summary>A GitHub download attached to a release.</summary>
internal class GitAsset
{
+ /*********
+ ** Accessors
+ *********/
/// <summary>The file name.</summary>
[JsonProperty("name")]
- public string FileName { get; set; }
+ public string FileName { get; }
/// <summary>The file content type.</summary>
[JsonProperty("content_type")]
- public string ContentType { get; set; }
+ public string ContentType { get; }
/// <summary>The download URL.</summary>
[JsonProperty("browser_download_url")]
- public string DownloadUrl { get; set; }
+ public string DownloadUrl { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="fileName">The file name.</param>
+ /// <param name="contentType">The file content type.</param>
+ /// <param name="downloadUrl">The download URL.</param>
+ public GitAsset(string fileName, string contentType, string downloadUrl)
+ {
+ this.FileName = fileName;
+ this.ContentType = contentType;
+ this.DownloadUrl = downloadUrl;
+ }
}
}
diff --git a/src/SMAPI.Web/Framework/Clients/GitHub/GitHubClient.cs b/src/SMAPI.Web/Framework/Clients/GitHub/GitHubClient.cs
index 0e68e2c2..785979a5 100644
--- a/src/SMAPI.Web/Framework/Clients/GitHub/GitHubClient.cs
+++ b/src/SMAPI.Web/Framework/Clients/GitHub/GitHubClient.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System;
using System.Linq;
using System.Net;
@@ -35,26 +33,26 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub
/// <param name="acceptHeader">The Accept header value expected by the GitHub API.</param>
/// <param name="username">The username with which to authenticate to the GitHub API.</param>
/// <param name="password">The password with which to authenticate to the GitHub API.</param>
- public GitHubClient(string baseUrl, string userAgent, string acceptHeader, string username, string password)
+ public GitHubClient(string baseUrl, string userAgent, string acceptHeader, string? username, string? password)
{
this.Client = new FluentClient(baseUrl)
.SetUserAgent(userAgent)
.AddDefault(req => req.WithHeader("Accept", acceptHeader));
if (!string.IsNullOrWhiteSpace(username))
- this.Client = this.Client.SetBasicAuthentication(username, password);
+ this.Client = this.Client.SetBasicAuthentication(username, password!);
}
/// <summary>Get basic metadata for a GitHub repository, if available.</summary>
/// <param name="repo">The repository key (like <c>Pathoschild/SMAPI</c>).</param>
/// <returns>Returns the repository info if it exists, else <c>null</c>.</returns>
- public async Task<GitRepo> GetRepositoryAsync(string repo)
+ public async Task<GitRepo?> GetRepositoryAsync(string repo)
{
this.AssertKeyFormat(repo);
try
{
return await this.Client
.GetAsync($"repos/{repo}")
- .As<GitRepo>();
+ .As<GitRepo?>();
}
catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound)
{
@@ -66,7 +64,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub
/// <param name="repo">The repository key (like <c>Pathoschild/SMAPI</c>).</param>
/// <param name="includePrerelease">Whether to return a prerelease version if it's latest.</param>
/// <returns>Returns the release if found, else <c>null</c>.</returns>
- public async Task<GitRelease> GetLatestReleaseAsync(string repo, bool includePrerelease = false)
+ public async Task<GitRelease?> GetLatestReleaseAsync(string repo, bool includePrerelease = false)
{
this.AssertKeyFormat(repo);
try
@@ -81,7 +79,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub
return await this.Client
.GetAsync($"repos/{repo}/releases/latest")
- .As<GitRelease>();
+ .As<GitRelease?>();
}
catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound)
{
@@ -91,7 +89,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub
/// <summary>Get update check info about a mod.</summary>
/// <param name="id">The mod ID.</param>
- public async Task<IModPage> GetModData(string id)
+ public async Task<IModPage?> GetModData(string id)
{
IModPage page = new GenericModPage(this.SiteKey, id);
@@ -99,15 +97,15 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub
return page.SetError(RemoteModStatus.DoesNotExist, $"The value '{id}' isn't a valid GitHub mod ID, must be a username and project name like 'Pathoschild/SMAPI'.");
// fetch repo info
- GitRepo repository = await this.GetRepositoryAsync(id);
+ GitRepo? repository = await this.GetRepositoryAsync(id);
if (repository == null)
return page.SetError(RemoteModStatus.DoesNotExist, "Found no GitHub repository for this ID.");
string name = repository.FullName;
string url = $"{repository.WebUrl}/releases";
// get releases
- GitRelease latest;
- GitRelease preview;
+ GitRelease? latest;
+ GitRelease? preview;
{
// get latest release (whether preview or stable)
latest = await this.GetLatestReleaseAsync(id, includePrerelease: true);
@@ -118,7 +116,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub
preview = null;
if (latest.IsPrerelease)
{
- GitRelease release = await this.GetLatestReleaseAsync(id, includePrerelease: false);
+ GitRelease? release = await this.GetLatestReleaseAsync(id, includePrerelease: false);
if (release != null)
{
preview = latest;
@@ -129,8 +127,8 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub
// get downloads
IModDownload[] downloads = new[] { latest, preview }
- .Where(release => release != null)
- .Select(release => (IModDownload)new GenericModDownload(release.Name, release.Body, release.Tag))
+ .Where(release => release is not null)
+ .Select(release => (IModDownload)new GenericModDownload(release!.Name, release.Body, release.Tag))
.ToArray();
// return info
@@ -140,7 +138,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose()
{
- this.Client?.Dispose();
+ this.Client.Dispose();
}
diff --git a/src/SMAPI.Web/Framework/Clients/GitHub/GitLicense.cs b/src/SMAPI.Web/Framework/Clients/GitHub/GitLicense.cs
index 275c775a..24d6c3c5 100644
--- a/src/SMAPI.Web/Framework/Clients/GitHub/GitLicense.cs
+++ b/src/SMAPI.Web/Framework/Clients/GitHub/GitLicense.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using Newtonsoft.Json;
namespace StardewModdingAPI.Web.Framework.Clients.GitHub
@@ -7,16 +5,34 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub
/// <summary>The license info for a GitHub project.</summary>
internal class GitLicense
{
+ /*********
+ ** Accessors
+ *********/
/// <summary>The license display name.</summary>
[JsonProperty("name")]
- public string Name { get; set; }
+ public string Name { get; }
/// <summary>The SPDX ID for the license.</summary>
[JsonProperty("spdx_id")]
- public string SpdxId { get; set; }
+ public string SpdxId { get; }
/// <summary>The URL for the license info.</summary>
[JsonProperty("url")]
- public string Url { get; set; }
+ public string Url { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="name">The license display name.</param>
+ /// <param name="spdxId">The SPDX ID for the license.</param>
+ /// <param name="url">The URL for the license info.</param>
+ public GitLicense(string name, string spdxId, string url)
+ {
+ this.Name = name;
+ this.SpdxId = spdxId;
+ this.Url = url;
+ }
}
}
diff --git a/src/SMAPI.Web/Framework/Clients/GitHub/GitRelease.cs b/src/SMAPI.Web/Framework/Clients/GitHub/GitRelease.cs
index 383775d2..9de6f020 100644
--- a/src/SMAPI.Web/Framework/Clients/GitHub/GitRelease.cs
+++ b/src/SMAPI.Web/Framework/Clients/GitHub/GitRelease.cs
@@ -1,5 +1,4 @@
-#nullable disable
-
+using System;
using Newtonsoft.Json;
namespace StardewModdingAPI.Web.Framework.Clients.GitHub
@@ -12,24 +11,45 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub
*********/
/// <summary>The display name.</summary>
[JsonProperty("name")]
- public string Name { get; set; }
+ public string Name { get; }
/// <summary>The semantic version string.</summary>
[JsonProperty("tag_name")]
- public string Tag { get; set; }
+ public string Tag { get; }
/// <summary>The Markdown description for the release.</summary>
- public string Body { get; set; }
+ public string Body { get; internal set; }
/// <summary>Whether this is a draft version.</summary>
[JsonProperty("draft")]
- public bool IsDraft { get; set; }
+ public bool IsDraft { get; }
/// <summary>Whether this is a prerelease version.</summary>
[JsonProperty("prerelease")]
- public bool IsPrerelease { get; set; }
+ public bool IsPrerelease { get; }
/// <summary>The attached files.</summary>
- public GitAsset[] Assets { get; set; }
+ public GitAsset[] Assets { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="name">The display name.</param>
+ /// <param name="tag">The semantic version string.</param>
+ /// <param name="body">The Markdown description for the release.</param>
+ /// <param name="isDraft">Whether this is a draft version.</param>
+ /// <param name="isPrerelease">Whether this is a prerelease version.</param>
+ /// <param name="assets">The attached files.</param>
+ public GitRelease(string name, string tag, string? body, bool isDraft, bool isPrerelease, GitAsset[]? assets)
+ {
+ this.Name = name;
+ this.Tag = tag;
+ this.Body = body ?? string.Empty;
+ this.IsDraft = isDraft;
+ this.IsPrerelease = isPrerelease;
+ this.Assets = assets ?? Array.Empty<GitAsset>();
+ }
}
}
diff --git a/src/SMAPI.Web/Framework/Clients/GitHub/GitRepo.cs b/src/SMAPI.Web/Framework/Clients/GitHub/GitRepo.cs
index 5b5ce6a6..879b5e49 100644
--- a/src/SMAPI.Web/Framework/Clients/GitHub/GitRepo.cs
+++ b/src/SMAPI.Web/Framework/Clients/GitHub/GitRepo.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using Newtonsoft.Json;
namespace StardewModdingAPI.Web.Framework.Clients.GitHub
@@ -7,16 +5,34 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub
/// <summary>Basic metadata about a GitHub project.</summary>
internal class GitRepo
{
+ /*********
+ ** Accessors
+ *********/
/// <summary>The full repository name, including the owner.</summary>
[JsonProperty("full_name")]
- public string FullName { get; set; }
+ public string FullName { get; }
/// <summary>The URL to the repository web page, if any.</summary>
[JsonProperty("html_url")]
- public string WebUrl { get; set; }
+ public string? WebUrl { get; }
/// <summary>The code license, if any.</summary>
[JsonProperty("license")]
- public GitLicense License { get; set; }
+ public GitLicense? License { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="fullName">The full repository name, including the owner.</param>
+ /// <param name="webUrl">The URL to the repository web page, if any.</param>
+ /// <param name="license">The code license, if any.</param>
+ public GitRepo(string fullName, string? webUrl, GitLicense? license)
+ {
+ this.FullName = fullName;
+ this.WebUrl = webUrl;
+ this.License = license;
+ }
}
}
diff --git a/src/SMAPI.Web/Framework/Clients/GitHub/IGitHubClient.cs b/src/SMAPI.Web/Framework/Clients/GitHub/IGitHubClient.cs
index e1961416..886e32d3 100644
--- a/src/SMAPI.Web/Framework/Clients/GitHub/IGitHubClient.cs
+++ b/src/SMAPI.Web/Framework/Clients/GitHub/IGitHubClient.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System;
using System.Threading.Tasks;
@@ -14,12 +12,12 @@ namespace StardewModdingAPI.Web.Framework.Clients.GitHub
/// <summary>Get basic metadata for a GitHub repository, if available.</summary>
/// <param name="repo">The repository key (like <c>Pathoschild/SMAPI</c>).</param>
/// <returns>Returns the repository info if it exists, else <c>null</c>.</returns>
- Task<GitRepo> GetRepositoryAsync(string repo);
+ Task<GitRepo?> GetRepositoryAsync(string repo);
/// <summary>Get the latest release for a GitHub repository.</summary>
/// <param name="repo">The repository key (like <c>Pathoschild/SMAPI</c>).</param>
/// <param name="includePrerelease">Whether to return a prerelease version if it's latest.</param>
/// <returns>Returns the release if found, else <c>null</c>.</returns>
- Task<GitRelease> GetLatestReleaseAsync(string repo, bool includePrerelease = false);
+ Task<GitRelease?> GetLatestReleaseAsync(string repo, bool includePrerelease = false);
}
}
diff --git a/src/SMAPI.Web/Framework/Clients/IModSiteClient.cs b/src/SMAPI.Web/Framework/Clients/IModSiteClient.cs
index 2cd1f635..3697ffae 100644
--- a/src/SMAPI.Web/Framework/Clients/IModSiteClient.cs
+++ b/src/SMAPI.Web/Framework/Clients/IModSiteClient.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System.Threading.Tasks;
using StardewModdingAPI.Toolkit.Framework.UpdateData;
@@ -20,6 +18,6 @@ namespace StardewModdingAPI.Web.Framework.Clients
*********/
/// <summary>Get update check info about a mod.</summary>
/// <param name="id">The mod ID.</param>
- Task<IModPage> GetModData(string id);
+ Task<IModPage?> GetModData(string id);
}
}
diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropClient.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropClient.cs
index 1a11a606..c60b2c90 100644
--- a/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropClient.cs
+++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropClient.cs
@@ -1,6 +1,5 @@
-#nullable disable
-
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Pathoschild.Http.Client;
using StardewModdingAPI.Toolkit.Framework.UpdateData;
@@ -43,9 +42,10 @@ namespace StardewModdingAPI.Web.Framework.Clients.ModDrop
/// <summary>Get update check info about a mod.</summary>
/// <param name="id">The mod ID.</param>
- public async Task<IModPage> GetModData(string id)
+ [SuppressMessage("ReSharper", "ConstantConditionalAccessQualifier", Justification = "The nullability is validated in this method.")]
+ public async Task<IModPage?> GetModData(string id)
{
- var page = new GenericModPage(this.SiteKey, id);
+ IModPage page = new GenericModPage(this.SiteKey, id);
if (!long.TryParse(id, out long parsedId))
return page.SetError(RemoteModStatus.DoesNotExist, $"The value '{id}' isn't a valid ModDrop mod ID, must be an integer ID.");
@@ -60,9 +60,11 @@ namespace StardewModdingAPI.Web.Framework.Clients.ModDrop
Mods = true
})
.As<ModListModel>();
- ModModel mod = response.Mods[parsedId];
- if (mod.Mod?.Title == null || mod.Mod.ErrorCode.HasValue)
- return null;
+
+ if (!response.Mods.TryGetValue(parsedId, out ModModel? mod) || mod?.Mod is null)
+ return page.SetError(RemoteModStatus.DoesNotExist, "Found no ModDrop page with this ID.");
+ if (mod.Mod.ErrorCode is not null)
+ return page.SetError(RemoteModStatus.InvalidData, $"ModDrop returned error code {mod.Mod.ErrorCode} for mod ID '{id}'.");
// get files
var downloads = new List<IModDownload>();
@@ -77,7 +79,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.ModDrop
}
// return info
- string name = mod.Mod?.Title;
+ string name = mod.Mod.Title;
string url = string.Format(this.ModUrlFormat, id);
return page.SetInfo(name: name, version: null, url: url, downloads: downloads);
}
@@ -85,7 +87,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.ModDrop
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose()
{
- this.Client?.Dispose();
+ this.Client.Dispose();
}
}
}
diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/FileDataModel.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/FileDataModel.cs
index dd6a95e0..31905338 100644
--- a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/FileDataModel.cs
+++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/FileDataModel.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using Newtonsoft.Json;
namespace StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels
@@ -7,27 +5,53 @@ namespace StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels
/// <summary>Metadata from the ModDrop API about a mod file.</summary>
public class FileDataModel
{
+ /*********
+ ** Accessors
+ *********/
/// <summary>The file title.</summary>
[JsonProperty("title")]
- public string Name { get; set; }
+ public string Name { get; }
/// <summary>The file description.</summary>
[JsonProperty("desc")]
- public string Description { get; set; }
+ public string Description { get; }
/// <summary>The file version.</summary>
- public string Version { get; set; }
+ public string Version { get; }
/// <summary>Whether the file is deleted.</summary>
- public bool IsDeleted { get; set; }
+ public bool IsDeleted { get; }
/// <summary>Whether the file is hidden from users.</summary>
- public bool IsHidden { get; set; }
+ public bool IsHidden { get; }
/// <summary>Whether this is the default file for the mod.</summary>
- public bool IsDefault { get; set; }
+ public bool IsDefault { get; }
/// <summary>Whether this is an archived file.</summary>
- public bool IsOld { get; set; }
+ public bool IsOld { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="name">The file title.</param>
+ /// <param name="description">The file description.</param>
+ /// <param name="version">The file version.</param>
+ /// <param name="isDeleted">Whether the file is deleted.</param>
+ /// <param name="isHidden">Whether the file is hidden from users.</param>
+ /// <param name="isDefault">Whether this is the default file for the mod.</param>
+ /// <param name="isOld">Whether this is an archived file.</param>
+ public FileDataModel(string name, string description, string version, bool isDeleted, bool isHidden, bool isDefault, bool isOld)
+ {
+ this.Name = name;
+ this.Description = description;
+ this.Version = version;
+ this.IsDeleted = isDeleted;
+ this.IsHidden = isHidden;
+ this.IsDefault = isDefault;
+ this.IsOld = isOld;
+ }
}
}
diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModDataModel.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModDataModel.cs
index 6cae16d9..0654b576 100644
--- a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModDataModel.cs
+++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModDataModel.cs
@@ -1,17 +1,33 @@
-#nullable disable
-
namespace StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels
{
/// <summary>Metadata about a mod from the ModDrop API.</summary>
public class ModDataModel
{
+ /*********
+ ** Accessors
+ *********/
/// <summary>The mod's unique ID on ModDrop.</summary>
public int ID { get; set; }
+ /// <summary>The mod name.</summary>
+ public string Title { get; set; }
+
/// <summary>The error code, if any.</summary>
public int? ErrorCode { get; set; }
- /// <summary>The mod name.</summary>
- public string Title { get; set; }
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="id">The mod's unique ID on ModDrop.</param>
+ /// <param name="title">The mod name.</param>
+ /// <param name="errorCode">The error code, if any.</param>
+ public ModDataModel(int id, string title, int? errorCode)
+ {
+ this.ID = id;
+ this.Title = title;
+ this.ErrorCode = errorCode;
+ }
}
}
diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModListModel.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModListModel.cs
index 445e25cb..cb4be35c 100644
--- a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModListModel.cs
+++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModListModel.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System.Collections.Generic;
namespace StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels
@@ -7,7 +5,10 @@ namespace StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels
/// <summary>A list of mods from the ModDrop API.</summary>
public class ModListModel
{
+ /*********
+ ** Accessors
+ *********/
/// <summary>The mod data.</summary>
- public IDictionary<long, ModModel> Mods { get; set; }
+ public IDictionary<long, ModModel> Mods { get; } = new Dictionary<long, ModModel>();
}
}
diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModModel.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModModel.cs
index 8869193e..60b818d6 100644
--- a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModModel.cs
+++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModModel.cs
@@ -1,14 +1,28 @@
-#nullable disable
-
namespace StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels
{
/// <summary>An entry in a mod list from the ModDrop API.</summary>
public class ModModel
{
+ /*********
+ ** Accessors
+ *********/
/// <summary>The available file downloads.</summary>
- public FileDataModel[] Files { get; set; }
+ public FileDataModel[] Files { get; }
/// <summary>The mod metadata.</summary>
- public ModDataModel Mod { get; set; }
+ public ModDataModel Mod { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="files">The available file downloads.</param>
+ /// <param name="mod">The mod metadata.</param>
+ public ModModel(FileDataModel[] files, ModDataModel mod)
+ {
+ this.Files = files;
+ this.Mod = mod;
+ }
}
}
diff --git a/src/SMAPI.Web/Framework/Clients/Nexus/NexusClient.cs b/src/SMAPI.Web/Framework/Clients/Nexus/NexusClient.cs
index dd0bb94f..23b25f95 100644
--- a/src/SMAPI.Web/Framework/Clients/Nexus/NexusClient.cs
+++ b/src/SMAPI.Web/Framework/Clients/Nexus/NexusClient.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System;
using System.Collections.Generic;
using System.Linq;
@@ -61,7 +59,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus
/// <summary>Get update check info about a mod.</summary>
/// <param name="id">The mod ID.</param>
- public async Task<IModPage> GetModData(string id)
+ public async Task<IModPage?> GetModData(string id)
{
IModPage page = new GenericModPage(this.SiteKey, id);
@@ -72,7 +70,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus
// adult content are hidden for anonymous users, so fall back to the API in that case.
// Note that the API has very restrictive rate limits which means we can't just use it
// for all cases.
- NexusMod mod = await this.GetModFromWebsiteAsync(parsedId);
+ NexusMod? mod = await this.GetModFromWebsiteAsync(parsedId);
if (mod?.Status == NexusModStatus.AdultContentForbidden)
mod = await this.GetModFromApiAsync(parsedId);
@@ -81,16 +79,16 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus
return page.SetError(RemoteModStatus.DoesNotExist, "Found no Nexus mod with this ID.");
// return info
- page.SetInfo(name: mod.Name, url: mod.Url, version: mod.Version, downloads: mod.Downloads);
+ page.SetInfo(name: mod.Name ?? parsedId.ToString(), url: mod.Url ?? this.GetModUrl(parsedId), version: mod.Version, downloads: mod.Downloads);
if (mod.Status != NexusModStatus.Ok)
- page.SetError(RemoteModStatus.TemporaryError, mod.Error);
+ page.SetError(RemoteModStatus.TemporaryError, mod.Error!);
return page;
}
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose()
{
- this.WebClient?.Dispose();
+ this.WebClient.Dispose();
}
@@ -100,7 +98,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus
/// <summary>Get metadata about a mod by scraping the Nexus website.</summary>
/// <param name="id">The Nexus mod ID.</param>
/// <returns>Returns the mod info if found, else <c>null</c>.</returns>
- private async Task<NexusMod> GetModFromWebsiteAsync(uint id)
+ private async Task<NexusMod?> GetModFromWebsiteAsync(uint id)
{
// fetch HTML
string html;
@@ -116,35 +114,38 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus
}
// parse HTML
- var doc = new HtmlDocument();
+ HtmlDocument doc = new();
doc.LoadHtml(html);
// handle Nexus error message
- HtmlNode node = doc.DocumentNode.SelectSingleNode("//div[contains(@class, 'site-notice')][contains(@class, 'warning')]");
+ HtmlNode? node = doc.DocumentNode.SelectSingleNode("//div[contains(@class, 'site-notice')][contains(@class, 'warning')]");
if (node != null)
{
string[] errorParts = node.InnerText.Trim().Split(new[] { '\n' }, 2, System.StringSplitOptions.RemoveEmptyEntries);
string errorCode = errorParts[0];
- string errorText = errorParts.Length > 1 ? errorParts[1] : null;
+ string? errorText = errorParts.Length > 1 ? errorParts[1] : null;
switch (errorCode.Trim().ToLower())
{
case "not found":
return null;
default:
- return new NexusMod { Error = $"Nexus error: {errorCode} ({errorText}).", Status = this.GetWebStatus(errorCode) };
+ return new NexusMod(
+ status: this.GetWebStatus(errorCode),
+ error: $"Nexus error: {errorCode} ({errorText})."
+ );
}
}
// extract mod info
string url = this.GetModUrl(id);
- string name = doc.DocumentNode.SelectSingleNode("//div[@id='pagetitle']//h1")?.InnerText.Trim();
- string version = doc.DocumentNode.SelectSingleNode("//ul[contains(@class, 'stats')]//li[@class='stat-version']//div[@class='stat']")?.InnerText.Trim();
- SemanticVersion.TryParse(version, out ISemanticVersion parsedVersion);
+ string? name = doc.DocumentNode.SelectSingleNode("//div[@id='pagetitle']//h1")?.InnerText.Trim();
+ string? version = doc.DocumentNode.SelectSingleNode("//ul[contains(@class, 'stats')]//li[@class='stat-version']//div[@class='stat']")?.InnerText.Trim();
+ SemanticVersion.TryParse(version, out ISemanticVersion? parsedVersion);
// extract files
var downloads = new List<IModDownload>();
- foreach (var fileSection in doc.DocumentNode.SelectNodes("//div[contains(@class, 'files-tabs')]"))
+ foreach (HtmlNode fileSection in doc.DocumentNode.SelectNodes("//div[contains(@class, 'files-tabs')]"))
{
string sectionName = fileSection.Descendants("h2").First().InnerText;
if (sectionName != "Main files" && sectionName != "Optional files")
@@ -154,7 +155,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus
{
string fileName = container.GetDataAttribute("name").Value;
string fileVersion = container.GetDataAttribute("version").Value;
- string description = container.SelectSingleNode("following-sibling::*[1][self::dd]//div").InnerText?.Trim(); // get text of next <dd> tag; derived from https://stackoverflow.com/a/25535623/262123
+ string? description = container.SelectSingleNode("following-sibling::*[1][self::dd]//div").InnerText?.Trim(); // get text of next <dd> tag; derived from https://stackoverflow.com/a/25535623/262123
downloads.Add(
new GenericModDownload(fileName, description, fileVersion)
@@ -163,13 +164,12 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus
}
// yield info
- return new NexusMod
- {
- Name = name,
- Version = parsedVersion?.ToString() ?? version,
- Url = url,
- Downloads = downloads.ToArray()
- };
+ return new NexusMod(
+ name: name ?? id.ToString(),
+ version: parsedVersion?.ToString() ?? version,
+ url: url,
+ downloads: downloads.ToArray()
+ );
}
/// <summary>Get metadata about a mod from the Nexus API.</summary>
@@ -182,22 +182,21 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus
ModFileList files = await this.ApiClient.ModFiles.GetModFiles("stardewvalley", (int)id, FileCategory.Main, FileCategory.Optional);
// yield info
- return new NexusMod
- {
- Name = mod.Name,
- Version = SemanticVersion.TryParse(mod.Version, out ISemanticVersion version) ? version?.ToString() : mod.Version,
- Url = this.GetModUrl(id),
- Downloads = files.Files
+ return new NexusMod(
+ name: mod.Name,
+ version: SemanticVersion.TryParse(mod.Version, out ISemanticVersion? version) ? version.ToString() : mod.Version,
+ url: this.GetModUrl(id),
+ downloads: files.Files
.Select(file => (IModDownload)new GenericModDownload(file.Name, file.Description, file.FileVersion))
.ToArray()
- };
+ );
}
/// <summary>Get the full mod page URL for a given ID.</summary>
/// <param name="id">The mod ID.</param>
private string GetModUrl(uint id)
{
- UriBuilder builder = new(this.WebClient.BaseClient.BaseAddress);
+ UriBuilder builder = new(this.WebClient.BaseClient.BaseAddress!);
builder.Path += string.Format(this.WebModUrlFormat, id);
return builder.Uri.ToString();
}
diff --git a/src/SMAPI.Web/Framework/Clients/Nexus/ResponseModels/NexusMod.cs b/src/SMAPI.Web/Framework/Clients/Nexus/ResponseModels/NexusMod.cs
index 358c4633..3155cfda 100644
--- a/src/SMAPI.Web/Framework/Clients/Nexus/ResponseModels/NexusMod.cs
+++ b/src/SMAPI.Web/Framework/Clients/Nexus/ResponseModels/NexusMod.cs
@@ -1,5 +1,4 @@
-#nullable disable
-
+using System;
using Newtonsoft.Json;
namespace StardewModdingAPI.Web.Framework.Clients.Nexus.ResponseModels
@@ -11,25 +10,53 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus.ResponseModels
** Accessors
*********/
/// <summary>The mod name.</summary>
- public string Name { get; set; }
+ public string? Name { get; }
/// <summary>The mod's semantic version number.</summary>
- public string Version { get; set; }
+ public string? Version { get; }
/// <summary>The mod's web URL.</summary>
[JsonProperty("mod_page_uri")]
- public string Url { get; set; }
+ public string? Url { get; }
/// <summary>The mod's publication status.</summary>
[JsonIgnore]
- public NexusModStatus Status { get; set; } = NexusModStatus.Ok;
+ public NexusModStatus Status { get; }
/// <summary>The files available to download.</summary>
[JsonIgnore]
- public IModDownload[] Downloads { get; set; }
+ public IModDownload[] Downloads { get; }
/// <summary>A custom user-friendly error which indicates why fetching the mod info failed (if applicable).</summary>
[JsonIgnore]
- public string Error { get; set; }
+ public string? Error { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="name">The mod name</param>
+ /// <param name="version">The mod's semantic version number.</param>
+ /// <param name="url">The mod's web URL.</param>
+ /// <param name="downloads">The files available to download.</param>
+ public NexusMod(string name, string? version, string url, IModDownload[] downloads)
+ {
+ this.Name = name;
+ this.Version = version;
+ this.Url = url;
+ this.Status = NexusModStatus.Ok;
+ this.Downloads = downloads;
+ }
+
+ /// <summary>Construct an instance.</summary>
+ /// <param name="status">The mod's publication status.</param>
+ /// <param name="error">A custom user-friendly error which indicates why fetching the mod info failed (if applicable).</param>
+ public NexusMod(NexusModStatus status, string error)
+ {
+ this.Status = status;
+ this.Error = error;
+ this.Downloads = Array.Empty<IModDownload>();
+ }
}
}
diff --git a/src/SMAPI.Web/Framework/Clients/Pastebin/IPastebinClient.cs b/src/SMAPI.Web/Framework/Clients/Pastebin/IPastebinClient.cs
index 03c78e01..431fed7b 100644
--- a/src/SMAPI.Web/Framework/Clients/Pastebin/IPastebinClient.cs
+++ b/src/SMAPI.Web/Framework/Clients/Pastebin/IPastebinClient.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System;
using System.Threading.Tasks;
diff --git a/src/SMAPI.Web/Framework/Clients/Pastebin/PasteInfo.cs b/src/SMAPI.Web/Framework/Clients/Pastebin/PasteInfo.cs
index 2d48a7ae..7f40e713 100644
--- a/src/SMAPI.Web/Framework/Clients/Pastebin/PasteInfo.cs
+++ b/src/SMAPI.Web/Framework/Clients/Pastebin/PasteInfo.cs
@@ -1,17 +1,35 @@
-#nullable disable
+using System.Diagnostics.CodeAnalysis;
namespace StardewModdingAPI.Web.Framework.Clients.Pastebin
{
/// <summary>The response for a get-paste request.</summary>
internal class PasteInfo
{
+ /*********
+ ** Accessors
+ *********/
/// <summary>Whether the log was successfully fetched.</summary>
- public bool Success { get; set; }
+ [MemberNotNullWhen(true, nameof(PasteInfo.Content))]
+ [MemberNotNullWhen(false, nameof(PasteInfo.Error))]
+ public bool Success => this.Error == null || this.Content != null;
/// <summary>The fetched paste content (if <see cref="Success"/> is <c>true</c>).</summary>
- public string Content { get; set; }
+ public string? Content { get; internal set; }
- /// <summary>The error message if saving failed.</summary>
- public string Error { get; set; }
+ /// <summary>The error message (if <see cref="Success"/> is <c>false</c>).</summary>
+ public string? Error { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="content">The fetched paste content.</param>
+ /// <param name="error">The error message, if it failed.</param>
+ public PasteInfo(string? content, string? error)
+ {
+ this.Content = content;
+ this.Error = error;
+ }
}
}
diff --git a/src/SMAPI.Web/Framework/Clients/Pastebin/PastebinClient.cs b/src/SMAPI.Web/Framework/Clients/Pastebin/PastebinClient.cs
index d0cdf374..0e00f071 100644
--- a/src/SMAPI.Web/Framework/Clients/Pastebin/PastebinClient.cs
+++ b/src/SMAPI.Web/Framework/Clients/Pastebin/PastebinClient.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System;
using System.Net;
using System.Threading.Tasks;
@@ -35,24 +33,24 @@ namespace StardewModdingAPI.Web.Framework.Clients.Pastebin
try
{
// get from API
- string content = await this.Client
+ string? content = await this.Client
.GetAsync($"raw/{id}")
.AsString();
// handle Pastebin errors
if (string.IsNullOrWhiteSpace(content))
- return new PasteInfo { Error = "Received an empty response from Pastebin." };
+ return new PasteInfo(null, "Received an empty response from Pastebin.");
if (content.StartsWith("<!DOCTYPE"))
- return new PasteInfo { Error = $"Received a captcha challenge from Pastebin. Please visit https://pastebin.com/{id} in a new window to solve it." };
- return new PasteInfo { Success = true, Content = content };
+ return new PasteInfo(null, $"Received a captcha challenge from Pastebin. Please visit https://pastebin.com/{id} in a new window to solve it.");
+ return new PasteInfo(content, null);
}
catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound)
{
- return new PasteInfo { Error = "There's no log with that ID." };
+ return new PasteInfo(null, "There's no log with that ID.");
}
catch (Exception ex)
{
- return new PasteInfo { Error = $"Pastebin error: {ex}" };
+ return new PasteInfo(null, $"Pastebin error: {ex}");
}
}
diff --git a/src/SMAPI.Web/Framework/Compression/GzipHelper.cs b/src/SMAPI.Web/Framework/Compression/GzipHelper.cs
index 843b7735..e7a2df13 100644
--- a/src/SMAPI.Web/Framework/Compression/GzipHelper.cs
+++ b/src/SMAPI.Web/Framework/Compression/GzipHelper.cs
@@ -1,6 +1,5 @@
-#nullable disable
-
using System;
+using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.IO.Compression;
using System.Text;
@@ -53,8 +52,12 @@ namespace StardewModdingAPI.Web.Framework.Compression
/// <summary>Decompress a string.</summary>
/// <param name="rawText">The compressed text.</param>
/// <remarks>Derived from <a href="https://stackoverflow.com/a/17993002/262123"/>.</remarks>
- public string DecompressString(string rawText)
+ [return: NotNullIfNotNull("rawText")]
+ public string? DecompressString(string? rawText)
{
+ if (rawText is null)
+ return rawText;
+
// get raw bytes
byte[] zipBuffer;
try
diff --git a/src/SMAPI.Web/Framework/Compression/IGzipHelper.cs b/src/SMAPI.Web/Framework/Compression/IGzipHelper.cs
index e1ec9b67..ef2d5696 100644
--- a/src/SMAPI.Web/Framework/Compression/IGzipHelper.cs
+++ b/src/SMAPI.Web/Framework/Compression/IGzipHelper.cs
@@ -1,4 +1,4 @@
-#nullable disable
+using System.Diagnostics.CodeAnalysis;
namespace StardewModdingAPI.Web.Framework.Compression
{
@@ -14,6 +14,7 @@ namespace StardewModdingAPI.Web.Framework.Compression
/// <summary>Decompress a string.</summary>
/// <param name="rawText">The compressed text.</param>
- string DecompressString(string rawText);
+ [return: NotNullIfNotNull("rawText")]
+ string? DecompressString(string? rawText);
}
}
diff --git a/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs b/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs
index 3730a9db..b582b2b0 100644
--- a/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs
+++ b/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
namespace StardewModdingAPI.Web.Framework.ConfigModels
{
/// <summary>The config settings for the API clients.</summary>
@@ -12,17 +10,17 @@ namespace StardewModdingAPI.Web.Framework.ConfigModels
** Generic
****/
/// <summary>The user agent for API clients, where {0} is the SMAPI version.</summary>
- public string UserAgent { get; set; }
+ public string UserAgent { get; set; } = null!;
/****
** Azure
****/
/// <summary>The connection string for the Azure Blob storage account.</summary>
- public string AzureBlobConnectionString { get; set; }
+ public string? AzureBlobConnectionString { get; set; }
/// <summary>The Azure Blob container in which to store temporary uploaded logs.</summary>
- public string AzureBlobTempContainer { get; set; }
+ public string AzureBlobTempContainer { get; set; } = null!;
/// <summary>The number of days since the blob's last-modified date when it will be deleted.</summary>
public int AzureBlobTempExpiryDays { get; set; }
@@ -32,65 +30,65 @@ namespace StardewModdingAPI.Web.Framework.ConfigModels
** Chucklefish
****/
/// <summary>The base URL for the Chucklefish mod site.</summary>
- public string ChucklefishBaseUrl { get; set; }
+ public string ChucklefishBaseUrl { get; set; } = null!;
/// <summary>The URL for a mod page on the Chucklefish mod site excluding the <see cref="GitHubBaseUrl"/>, where {0} is the mod ID.</summary>
- public string ChucklefishModPageUrlFormat { get; set; }
+ public string ChucklefishModPageUrlFormat { get; set; } = null!;
/****
** CurseForge
****/
/// <summary>The base URL for the CurseForge API.</summary>
- public string CurseForgeBaseUrl { get; set; }
+ public string CurseForgeBaseUrl { get; set; } = null!;
/****
** GitHub
****/
/// <summary>The base URL for the GitHub API.</summary>
- public string GitHubBaseUrl { get; set; }
+ public string GitHubBaseUrl { get; set; } = null!;
/// <summary>The Accept header value expected by the GitHub API.</summary>
- public string GitHubAcceptHeader { get; set; }
+ public string GitHubAcceptHeader { get; set; } = null!;
/// <summary>The username with which to authenticate to the GitHub API (if any).</summary>
- public string GitHubUsername { get; set; }
+ public string? GitHubUsername { get; set; }
/// <summary>The password with which to authenticate to the GitHub API (if any).</summary>
- public string GitHubPassword { get; set; }
+ public string? GitHubPassword { get; set; }
/****
** ModDrop
****/
/// <summary>The base URL for the ModDrop API.</summary>
- public string ModDropApiUrl { get; set; }
+ public string ModDropApiUrl { get; set; } = null!;
/// <summary>The URL for a ModDrop mod page for the user, where {0} is the mod ID.</summary>
- public string ModDropModPageUrl { get; set; }
+ public string ModDropModPageUrl { get; set; } = null!;
/****
** Nexus Mods
****/
/// <summary>The base URL for the Nexus Mods API.</summary>
- public string NexusBaseUrl { get; set; }
+ public string NexusBaseUrl { get; set; } = null!;
/// <summary>The URL for a Nexus mod page for the user, excluding the <see cref="NexusBaseUrl"/>, where {0} is the mod ID.</summary>
- public string NexusModUrlFormat { get; set; }
+ public string NexusModUrlFormat { get; set; } = null!;
/// <summary>The URL for a Nexus mod page to scrape for versions, excluding the <see cref="NexusBaseUrl"/>, where {0} is the mod ID.</summary>
- public string NexusModScrapeUrlFormat { get; set; }
+ public string NexusModScrapeUrlFormat { get; set; } = null!;
/// <summary>The Nexus API authentication key.</summary>
- public string NexusApiKey { get; set; }
+ public string? NexusApiKey { get; set; }
/****
** Pastebin
****/
/// <summary>The base URL for the Pastebin API.</summary>
- public string PastebinBaseUrl { get; set; }
+ public string PastebinBaseUrl { get; set; } = null!;
}
}
diff --git a/src/SMAPI.Web/Framework/ConfigModels/ModOverrideConfig.cs b/src/SMAPI.Web/Framework/ConfigModels/ModOverrideConfig.cs
index 682c97e6..e46ecf2b 100644
--- a/src/SMAPI.Web/Framework/ConfigModels/ModOverrideConfig.cs
+++ b/src/SMAPI.Web/Framework/ConfigModels/ModOverrideConfig.cs
@@ -1,17 +1,15 @@
-#nullable disable
-
namespace StardewModdingAPI.Web.Framework.ConfigModels
{
/// <summary>Override update-check metadata for a mod.</summary>
internal class ModOverrideConfig
{
/// <summary>The unique ID from the mod's manifest.</summary>
- public string ID { get; set; }
+ public string ID { get; set; } = null!;
/// <summary>Whether to allow non-standard versions.</summary>
public bool AllowNonStandardVersions { get; set; }
/// <summary>The mod page URL to use regardless of which site has the update, or <c>null</c> to use the site URL.</summary>
- public string SetUrl { get; set; }
+ public string? SetUrl { get; set; }
}
}
diff --git a/src/SMAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs b/src/SMAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs
index e525e09a..c3b136e8 100644
--- a/src/SMAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs
+++ b/src/SMAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs
@@ -1,4 +1,4 @@
-#nullable disable
+using System;
namespace StardewModdingAPI.Web.Framework.ConfigModels
{
@@ -8,16 +8,16 @@ namespace StardewModdingAPI.Web.Framework.ConfigModels
/*********
** Accessors
*********/
- /// <summary>The number of minutes successful update checks should be cached before refetching them.</summary>
+ /// <summary>The number of minutes successful update checks should be cached before re-fetching them.</summary>
public int SuccessCacheMinutes { get; set; }
- /// <summary>The number of minutes failed update checks should be cached before refetching them.</summary>
+ /// <summary>The number of minutes failed update checks should be cached before re-fetching them.</summary>
public int ErrorCacheMinutes { get; set; }
/// <summary>Update-check metadata to override.</summary>
- public ModOverrideConfig[] ModOverrides { get; set; }
+ public ModOverrideConfig[] ModOverrides { get; set; } = Array.Empty<ModOverrideConfig>();
/// <summary>The update-check config for SMAPI's own update checks.</summary>
- public SmapiInfoConfig SmapiInfo { get; set; }
+ public SmapiInfoConfig SmapiInfo { get; set; } = null!;
}
}
diff --git a/src/SMAPI.Web/Framework/ConfigModels/SiteConfig.cs b/src/SMAPI.Web/Framework/ConfigModels/SiteConfig.cs
index ef6c2659..62685e47 100644
--- a/src/SMAPI.Web/Framework/ConfigModels/SiteConfig.cs
+++ b/src/SMAPI.Web/Framework/ConfigModels/SiteConfig.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
namespace StardewModdingAPI.Web.Framework.ConfigModels
{
/// <summary>The site config settings.</summary>
@@ -9,9 +7,9 @@ namespace StardewModdingAPI.Web.Framework.ConfigModels
** Accessors
*********/
/// <summary>A message to show below the download button (e.g. for details on downloading a beta version), in Markdown format.</summary>
- public string OtherBlurb { get; set; }
+ public string? OtherBlurb { get; set; }
/// <summary>A list of supports to credit on the main page, in Markdown format.</summary>
- public string SupporterList { get; set; }
+ public string? SupporterList { get; set; }
}
}
diff --git a/src/SMAPI.Web/Framework/ConfigModels/SmapiInfoConfig.cs b/src/SMAPI.Web/Framework/ConfigModels/SmapiInfoConfig.cs
index dbf58817..a95e0048 100644
--- a/src/SMAPI.Web/Framework/ConfigModels/SmapiInfoConfig.cs
+++ b/src/SMAPI.Web/Framework/ConfigModels/SmapiInfoConfig.cs
@@ -1,4 +1,4 @@
-#nullable disable
+using System;
namespace StardewModdingAPI.Web.Framework.ConfigModels
{
@@ -6,12 +6,12 @@ namespace StardewModdingAPI.Web.Framework.ConfigModels
internal class SmapiInfoConfig
{
/// <summary>The mod ID used for SMAPI update checks.</summary>
- public string ID { get; set; }
+ public string ID { get; set; } = null!;
/// <summary>The default update key used for SMAPI update checks.</summary>
- public string DefaultUpdateKey { get; set; }
+ public string DefaultUpdateKey { get; set; } = null!;
/// <summary>The update keys to add for SMAPI update checks when the player has a beta version installed.</summary>
- public string[] AddBetaUpdateKeys { get; set; }
+ public string[] AddBetaUpdateKeys { get; set; } = Array.Empty<string>();
}
}
diff --git a/src/SMAPI.Web/Framework/Extensions.cs b/src/SMAPI.Web/Framework/Extensions.cs
index a72c12c1..62a23155 100644
--- a/src/SMAPI.Web/Framework/Extensions.cs
+++ b/src/SMAPI.Web/Framework/Extensions.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Html;
@@ -28,7 +26,7 @@ namespace StardewModdingAPI.Web.Framework
/// <param name="values">An object that contains route values.</param>
/// <param name="absoluteUrl">Get an absolute URL instead of a server-relative path/</param>
/// <returns>The generated URL.</returns>
- public static string PlainAction(this IUrlHelper helper, [AspMvcAction] string action, [AspMvcController] string controller, object values = null, bool absoluteUrl = false)
+ public static string? PlainAction(this IUrlHelper helper, [AspMvcAction] string action, [AspMvcController] string controller, object? values = null, bool absoluteUrl = false)
{
// get route values
RouteValueDictionary valuesDict = new(values);
@@ -39,7 +37,7 @@ namespace StardewModdingAPI.Web.Framework
}
// get relative URL
- string url = helper.Action(action, controller, valuesDict);
+ string? url = helper.Action(action, controller, valuesDict);
if (url == null && action.EndsWith("Async"))
url = helper.Action(action[..^"Async".Length], controller, valuesDict);
@@ -59,7 +57,7 @@ namespace StardewModdingAPI.Web.Framework
/// <param name="value">The value to serialize.</param>
/// <returns>The serialized JSON.</returns>
/// <remarks>This bypasses unnecessary validation (e.g. not allowing null values) in <see cref="IJsonHelper.Serialize"/>.</remarks>
- public static IHtmlContent ForJson(this RazorPageBase page, object value)
+ public static IHtmlContent ForJson(this RazorPageBase page, object? value)
{
string json = JsonConvert.SerializeObject(value);
return new HtmlString(json);
diff --git a/src/SMAPI.Web/Framework/IModDownload.cs b/src/SMAPI.Web/Framework/IModDownload.cs
index b8d1f62c..fe171785 100644
--- a/src/SMAPI.Web/Framework/IModDownload.cs
+++ b/src/SMAPI.Web/Framework/IModDownload.cs
@@ -1,17 +1,18 @@
-#nullable disable
-
namespace StardewModdingAPI.Web.Framework
{
/// <summary>Generic metadata about a file download on a mod page.</summary>
internal interface IModDownload
{
+ /*********
+ ** Accessors
+ *********/
/// <summary>The download's display name.</summary>
string Name { get; }
/// <summary>The download's description.</summary>
- string Description { get; }
+ string? Description { get; }
/// <summary>The download's file version.</summary>
- string Version { get; }
+ string? Version { get; }
}
}
diff --git a/src/SMAPI.Web/Framework/IModPage.cs b/src/SMAPI.Web/Framework/IModPage.cs
index 68220b49..4d0a8d61 100644
--- a/src/SMAPI.Web/Framework/IModPage.cs
+++ b/src/SMAPI.Web/Framework/IModPage.cs
@@ -1,6 +1,5 @@
-#nullable disable
-
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
using StardewModdingAPI.Toolkit.Framework.UpdateData;
namespace StardewModdingAPI.Web.Framework
@@ -18,13 +17,13 @@ namespace StardewModdingAPI.Web.Framework
string Id { get; }
/// <summary>The mod name.</summary>
- string Name { get; }
+ string? Name { get; }
/// <summary>The mod's semantic version number.</summary>
- string Version { get; }
+ string? Version { get; }
/// <summary>The mod's web URL.</summary>
- string Url { get; }
+ string? Url { get; }
/// <summary>The mod downloads.</summary>
IModDownload[] Downloads { get; }
@@ -33,7 +32,12 @@ namespace StardewModdingAPI.Web.Framework
RemoteModStatus Status { get; }
/// <summary>A user-friendly error which indicates why fetching the mod info failed (if applicable).</summary>
- string Error { get; }
+ string? Error { get; }
+
+ /// <summary>Whether the mod data is valid.</summary>
+ [MemberNotNullWhen(true, nameof(IModPage.Name), nameof(IModPage.Url))]
+ [MemberNotNullWhen(false, nameof(IModPage.Error))]
+ bool IsValid { get; }
/*********
@@ -44,7 +48,7 @@ namespace StardewModdingAPI.Web.Framework
/// <param name="version">The mod's semantic version number.</param>
/// <param name="url">The mod's web URL.</param>
/// <param name="downloads">The mod downloads.</param>
- IModPage SetInfo(string name, string version, string url, IEnumerable<IModDownload> downloads);
+ IModPage SetInfo(string name, string? version, string url, IEnumerable<IModDownload> downloads);
/// <summary>Set a mod fetch error.</summary>
/// <param name="status">The mod availability status on the remote site.</param>
diff --git a/src/SMAPI.Web/Framework/InternalControllerFeatureProvider.cs b/src/SMAPI.Web/Framework/InternalControllerFeatureProvider.cs
index 98738a82..2c24c610 100644
--- a/src/SMAPI.Web/Framework/InternalControllerFeatureProvider.cs
+++ b/src/SMAPI.Web/Framework/InternalControllerFeatureProvider.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System;
using System.Reflection;
using Microsoft.AspNetCore.Mvc;
diff --git a/src/SMAPI.Web/Framework/JobDashboardAuthorizationFilter.cs b/src/SMAPI.Web/Framework/JobDashboardAuthorizationFilter.cs
index 8db43dca..3c1405eb 100644
--- a/src/SMAPI.Web/Framework/JobDashboardAuthorizationFilter.cs
+++ b/src/SMAPI.Web/Framework/JobDashboardAuthorizationFilter.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using Hangfire.Dashboard;
namespace StardewModdingAPI.Web.Framework
diff --git a/src/SMAPI.Web/Framework/ModInfoModel.cs b/src/SMAPI.Web/Framework/ModInfoModel.cs
index 021d14fb..e70b60bf 100644
--- a/src/SMAPI.Web/Framework/ModInfoModel.cs
+++ b/src/SMAPI.Web/Framework/ModInfoModel.cs
@@ -1,4 +1,5 @@
-#nullable disable
+using System.Diagnostics.CodeAnalysis;
+using Newtonsoft.Json;
namespace StardewModdingAPI.Web.Framework
{
@@ -9,22 +10,22 @@ namespace StardewModdingAPI.Web.Framework
** Accessors
*********/
/// <summary>The mod name.</summary>
- public string Name { get; set; }
+ public string? Name { get; private set; }
+
+ /// <summary>The mod's web URL.</summary>
+ public string? Url { get; private set; }
/// <summary>The mod's latest version.</summary>
- public ISemanticVersion Version { get; set; }
+ public ISemanticVersion? Version { get; private set; }
/// <summary>The mod's latest optional or prerelease version, if newer than <see cref="Version"/>.</summary>
- public ISemanticVersion PreviewVersion { get; set; }
-
- /// <summary>The mod's web URL.</summary>
- public string Url { get; set; }
+ public ISemanticVersion? PreviewVersion { get; private set; }
/// <summary>The mod availability status on the remote site.</summary>
- public RemoteModStatus Status { get; set; } = RemoteModStatus.Ok;
+ public RemoteModStatus Status { get; private set; }
/// <summary>The error message indicating why the mod is invalid (if applicable).</summary>
- public string Error { get; set; }
+ public string? Error { get; private set; }
/*********
@@ -35,19 +36,24 @@ namespace StardewModdingAPI.Web.Framework
/// <summary>Construct an instance.</summary>
/// <param name="name">The mod name.</param>
+ /// <param name="url">The mod's web URL.</param>
/// <param name="version">The semantic version for the mod's latest release.</param>
/// <param name="previewVersion">The semantic version for the mod's latest preview release, if available and different from <see cref="Version"/>.</param>
- /// <param name="url">The mod's web URL.</param>
- public ModInfoModel(string name, ISemanticVersion version, string url, ISemanticVersion previewVersion = null)
+ /// <param name="status">The mod availability status on the remote site.</param>
+ /// <param name="error">The error message indicating why the mod is invalid (if applicable).</param>
+ [JsonConstructor]
+ public ModInfoModel(string name, string url, ISemanticVersion? version, ISemanticVersion? previewVersion = null, RemoteModStatus status = RemoteModStatus.Ok, string? error = null)
{
this
.SetBasicInfo(name, url)
- .SetVersions(version, previewVersion);
+ .SetVersions(version!, previewVersion)
+ .SetError(status, error!);
}
/// <summary>Set the basic mod info.</summary>
/// <param name="name">The mod name.</param>
/// <param name="url">The mod's web URL.</param>
+ [MemberNotNull(nameof(ModInfoModel.Name), nameof(ModInfoModel.Url))]
public ModInfoModel SetBasicInfo(string name, string url)
{
this.Name = name;
@@ -59,7 +65,8 @@ namespace StardewModdingAPI.Web.Framework
/// <summary>Set the mod version info.</summary>
/// <param name="version">The semantic version for the mod's latest release.</param>
/// <param name="previewVersion">The semantic version for the mod's latest preview release, if available and different from <see cref="Version"/>.</param>
- public ModInfoModel SetVersions(ISemanticVersion version, ISemanticVersion previewVersion = null)
+ [MemberNotNull(nameof(ModInfoModel.Version))]
+ public ModInfoModel SetVersions(ISemanticVersion version, ISemanticVersion? previewVersion = null)
{
this.Version = version;
this.PreviewVersion = previewVersion;
diff --git a/src/SMAPI.Web/Framework/ModSiteManager.cs b/src/SMAPI.Web/Framework/ModSiteManager.cs
index 2d6755d8..674b9ffc 100644
--- a/src/SMAPI.Web/Framework/ModSiteManager.cs
+++ b/src/SMAPI.Web/Framework/ModSiteManager.cs
@@ -1,7 +1,6 @@
-#nullable disable
-
using System;
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
@@ -36,12 +35,15 @@ namespace StardewModdingAPI.Web.Framework
/// <param name="updateKey">The namespaced update key.</param>
public async Task<IModPage> GetModPageAsync(UpdateKey updateKey)
{
+ if (!updateKey.LooksValid)
+ return new GenericModPage(updateKey.Site, updateKey.ID!).SetError(RemoteModStatus.DoesNotExist, $"Invalid update key '{updateKey}'.");
+
// get site
- if (!this.ModSites.TryGetValue(updateKey.Site, out IModSiteClient client))
+ if (!this.ModSites.TryGetValue(updateKey.Site, out IModSiteClient? client))
return new GenericModPage(updateKey.Site, updateKey.ID).SetError(RemoteModStatus.DoesNotExist, $"There's no mod site with key '{updateKey.Site}'. Expected one of [{string.Join(", ", this.ModSites.Keys)}].");
// fetch mod
- IModPage mod;
+ IModPage? mod;
try
{
mod = await client.GetModData(updateKey.ID);
@@ -60,39 +62,42 @@ namespace StardewModdingAPI.Web.Framework
/// <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="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, string? subkey, bool allowNonStandardVersions, ChangeDescriptor? mapRemoteVersions)
{
// get base model
- ModInfoModel model = new ModInfoModel()
- .SetBasicInfo(page.Name, page.Url)
- .SetError(page.Status, page.Error);
- if (page.Status != RemoteModStatus.Ok)
+ ModInfoModel model = new();
+ if (page.IsValid)
+ model.SetBasicInfo(page.Name, page.Url);
+ else
+ {
+ model.SetError(page.Status, page.Error);
return model;
+ }
// fetch versions
- bool hasVersions = this.TryGetLatestVersions(page, subkey, allowNonStandardVersions, mapRemoteVersions, out ISemanticVersion mainVersion, out ISemanticVersion previewVersion);
+ bool hasVersions = this.TryGetLatestVersions(page, subkey, allowNonStandardVersions, mapRemoteVersions, out ISemanticVersion? mainVersion, out ISemanticVersion? previewVersion);
if (!hasVersions && subkey != null)
hasVersions = this.TryGetLatestVersions(page, null, allowNonStandardVersions, mapRemoteVersions, out mainVersion, out previewVersion);
if (!hasVersions)
return model.SetError(RemoteModStatus.InvalidData, $"The {page.Site} mod with ID '{page.Id}' has no valid versions.");
// return info
- return model.SetVersions(mainVersion, previewVersion);
+ return model.SetVersions(mainVersion!, previewVersion);
}
/// <summary>Get a semantic local version for update checks.</summary>
/// <param name="version">The version to parse.</param>
/// <param name="map">Changes to apply to the raw version, if any.</param>
/// <param name="allowNonStandard">Whether to allow non-standard versions.</param>
- public ISemanticVersion GetMappedVersion(string version, ChangeDescriptor map, bool allowNonStandard)
+ public ISemanticVersion? GetMappedVersion(string? version, ChangeDescriptor? map, bool allowNonStandard)
{
// try mapped version
- string rawNewVersion = this.GetRawMappedVersion(version, map, allowNonStandard);
- if (SemanticVersion.TryParse(rawNewVersion, allowNonStandard, out ISemanticVersion parsedNew))
+ string? rawNewVersion = this.GetRawMappedVersion(version, map);
+ if (SemanticVersion.TryParse(rawNewVersion, allowNonStandard, out ISemanticVersion? parsedNew))
return parsedNew;
// return original version
- return SemanticVersion.TryParse(version, allowNonStandard, out ISemanticVersion parsedOld)
+ return SemanticVersion.TryParse(version, allowNonStandard, out ISemanticVersion? parsedOld)
? parsedOld
: null;
}
@@ -108,31 +113,31 @@ namespace StardewModdingAPI.Web.Framework
/// <param name="mapRemoteVersions">The changes to apply to remote versions for update checks.</param>
/// <param name="main">The main mod version.</param>
/// <param name="preview">The latest prerelease version, if newer than <paramref name="main"/>.</param>
- private bool TryGetLatestVersions(IModPage mod, string subkey, bool allowNonStandardVersions, ChangeDescriptor mapRemoteVersions, out ISemanticVersion main, out ISemanticVersion preview)
+ private bool TryGetLatestVersions(IModPage? mod, string? subkey, bool allowNonStandardVersions, ChangeDescriptor? mapRemoteVersions, [NotNullWhen(true)] out ISemanticVersion? main, out ISemanticVersion? preview)
{
main = null;
preview = null;
// parse all versions from the mod page
- IEnumerable<(string name, string description, ISemanticVersion version)> GetAllVersions()
+ IEnumerable<(string? name, string? description, ISemanticVersion? version)> GetAllVersions()
{
if (mod != null)
{
- ISemanticVersion ParseAndMapVersion(string raw)
+ ISemanticVersion? ParseAndMapVersion(string? raw)
{
raw = this.NormalizeVersion(raw);
return this.GetMappedVersion(raw, mapRemoteVersions, allowNonStandardVersions);
}
// get mod version
- ISemanticVersion modVersion = ParseAndMapVersion(mod.Version);
+ ISemanticVersion? modVersion = ParseAndMapVersion(mod.Version);
if (modVersion != null)
yield return (name: null, description: null, version: ParseAndMapVersion(mod.Version));
// get file versions
foreach (IModDownload download in mod.Downloads)
{
- ISemanticVersion cur = ParseAndMapVersion(download.Version);
+ ISemanticVersion? cur = ParseAndMapVersion(download.Version);
if (cur != null)
yield return (download.Name, download.Description, cur);
}
@@ -143,15 +148,15 @@ namespace StardewModdingAPI.Web.Framework
.ToArray();
// get main + preview versions
- void TryGetVersions(out ISemanticVersion mainVersion, out ISemanticVersion previewVersion, Func<(string name, string description, ISemanticVersion version), bool> filter = null)
+ void TryGetVersions([NotNullWhen(true)] out ISemanticVersion? mainVersion, out ISemanticVersion? previewVersion, Func<(string? name, string? description, ISemanticVersion? version), bool>? filter = null)
{
mainVersion = null;
previewVersion = null;
// get latest main + preview version
- foreach (var entry in versions)
+ foreach ((string? name, string? description, ISemanticVersion? version) entry in versions)
{
- if (filter?.Invoke(entry) == false)
+ if (entry.version is null || filter?.Invoke(entry) == false)
continue;
if (entry.version.IsPrerelease())
@@ -160,7 +165,7 @@ namespace StardewModdingAPI.Web.Framework
mainVersion ??= entry.version;
if (mainVersion != null)
- break; // any other values will be older
+ break; // any others will be older since entries are sorted by version
}
// normalize values
@@ -183,8 +188,7 @@ namespace StardewModdingAPI.Web.Framework
/// <summary>Get a semantic local version for update checks.</summary>
/// <param name="version">The version to map.</param>
/// <param name="map">Changes to apply to the raw version, if any.</param>
- /// <param name="allowNonStandard">Whether to allow non-standard versions.</param>
- private string GetRawMappedVersion(string version, ChangeDescriptor map, bool allowNonStandard)
+ private string? GetRawMappedVersion(string? version, ChangeDescriptor? map)
{
if (version == null || map?.HasChanges != true)
return version;
@@ -197,7 +201,7 @@ namespace StardewModdingAPI.Web.Framework
/// <summary>Normalize a version string.</summary>
/// <param name="version">The version to normalize.</param>
- private string NormalizeVersion(string version)
+ private string? NormalizeVersion(string? version)
{
if (string.IsNullOrWhiteSpace(version))
return null;
diff --git a/src/SMAPI.Web/Framework/RedirectRules/RedirectHostsToUrlsRule.cs b/src/SMAPI.Web/Framework/RedirectRules/RedirectHostsToUrlsRule.cs
index fe601524..7b8f0ec9 100644
--- a/src/SMAPI.Web/Framework/RedirectRules/RedirectHostsToUrlsRule.cs
+++ b/src/SMAPI.Web/Framework/RedirectRules/RedirectHostsToUrlsRule.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System;
using System.Net;
using Microsoft.AspNetCore.Rewrite;
@@ -13,7 +11,7 @@ namespace StardewModdingAPI.Web.Framework.RedirectRules
** Fields
*********/
/// <summary>Maps a lowercase hostname to the resulting redirect URL.</summary>
- private readonly Func<string, string> Map;
+ private readonly Func<string, string?> Map;
/*********
@@ -22,7 +20,7 @@ namespace StardewModdingAPI.Web.Framework.RedirectRules
/// <summary>Construct an instance.</summary>
/// <param name="statusCode">The status code to use for redirects.</param>
/// <param name="map">Hostnames mapped to the resulting redirect URL.</param>
- public RedirectHostsToUrlsRule(HttpStatusCode statusCode, Func<string, string> map)
+ public RedirectHostsToUrlsRule(HttpStatusCode statusCode, Func<string, string?> map)
{
this.StatusCode = statusCode;
this.Map = map ?? throw new ArgumentNullException(nameof(map));
@@ -35,10 +33,10 @@ namespace StardewModdingAPI.Web.Framework.RedirectRules
/// <summary>Get the new redirect URL.</summary>
/// <param name="context">The rewrite context.</param>
/// <returns>Returns the redirect URL, or <c>null</c> if the redirect doesn't apply.</returns>
- protected override string GetNewUrl(RewriteContext context)
+ protected override string? GetNewUrl(RewriteContext context)
{
// get requested host
- string host = context.HttpContext.Request.Host.Host;
+ string? host = context.HttpContext.Request.Host.Host;
// get new host
host = this.Map(host);
diff --git a/src/SMAPI.Web/Framework/RedirectRules/RedirectMatchRule.cs b/src/SMAPI.Web/Framework/RedirectRules/RedirectMatchRule.cs
index 81a265c9..b46e8f69 100644
--- a/src/SMAPI.Web/Framework/RedirectRules/RedirectMatchRule.cs
+++ b/src/SMAPI.Web/Framework/RedirectRules/RedirectMatchRule.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System;
using System.Net;
using Microsoft.AspNetCore.Http;
@@ -24,7 +22,7 @@ namespace StardewModdingAPI.Web.Framework.RedirectRules
/// <param name="context">The rewrite context.</param>
public void ApplyRule(RewriteContext context)
{
- string newUrl = this.GetNewUrl(context);
+ string? newUrl = this.GetNewUrl(context);
if (newUrl == null)
return;
@@ -41,7 +39,7 @@ namespace StardewModdingAPI.Web.Framework.RedirectRules
/// <summary>Get the new redirect URL.</summary>
/// <param name="context">The rewrite context.</param>
/// <returns>Returns the redirect URL, or <c>null</c> if the redirect doesn't apply.</returns>
- protected abstract string GetNewUrl(RewriteContext context);
+ protected abstract string? GetNewUrl(RewriteContext context);
/// <summary>Get the full request URL.</summary>
/// <param name="request">The request.</param>
diff --git a/src/SMAPI.Web/Framework/RedirectRules/RedirectPathsToUrlsRule.cs b/src/SMAPI.Web/Framework/RedirectRules/RedirectPathsToUrlsRule.cs
index cb3e53ef..e691ffba 100644
--- a/src/SMAPI.Web/Framework/RedirectRules/RedirectPathsToUrlsRule.cs
+++ b/src/SMAPI.Web/Framework/RedirectRules/RedirectPathsToUrlsRule.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System.Collections.Generic;
using System.Linq;
using System.Net;
@@ -39,9 +37,9 @@ namespace StardewModdingAPI.Web.Framework.RedirectRules
/// <summary>Get the new redirect URL.</summary>
/// <param name="context">The rewrite context.</param>
/// <returns>Returns the redirect URL, or <c>null</c> if the redirect doesn't apply.</returns>
- protected override string GetNewUrl(RewriteContext context)
+ protected override string? GetNewUrl(RewriteContext context)
{
- string path = context.HttpContext.Request.Path.Value;
+ string? path = context.HttpContext.Request.Path.Value;
if (!string.IsNullOrWhiteSpace(path))
{
diff --git a/src/SMAPI.Web/Framework/RedirectRules/RedirectToHttpsRule.cs b/src/SMAPI.Web/Framework/RedirectRules/RedirectToHttpsRule.cs
index dd7c836f..01807608 100644
--- a/src/SMAPI.Web/Framework/RedirectRules/RedirectToHttpsRule.cs
+++ b/src/SMAPI.Web/Framework/RedirectRules/RedirectToHttpsRule.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System;
using System.Net;
using Microsoft.AspNetCore.Http;
@@ -22,7 +20,7 @@ namespace StardewModdingAPI.Web.Framework.RedirectRules
*********/
/// <summary>Construct an instance.</summary>
/// <param name="except">Matches requests which should be ignored.</param>
- public RedirectToHttpsRule(Func<HttpRequest, bool> except = null)
+ public RedirectToHttpsRule(Func<HttpRequest, bool>? except = null)
{
this.Except = except ?? (_ => false);
this.StatusCode = HttpStatusCode.RedirectKeepVerb;
@@ -35,7 +33,7 @@ namespace StardewModdingAPI.Web.Framework.RedirectRules
/// <summary>Get the new redirect URL.</summary>
/// <param name="context">The rewrite context.</param>
/// <returns>Returns the redirect URL, or <c>null</c> if the redirect doesn't apply.</returns>
- protected override string GetNewUrl(RewriteContext context)
+ protected override string? GetNewUrl(RewriteContext context)
{
HttpRequest request = context.HttpContext.Request;
if (request.IsHttps || this.Except(request))
diff --git a/src/SMAPI.Web/Framework/Storage/IStorageProvider.cs b/src/SMAPI.Web/Framework/Storage/IStorageProvider.cs
index 2eca4845..dfc1fb47 100644
--- a/src/SMAPI.Web/Framework/Storage/IStorageProvider.cs
+++ b/src/SMAPI.Web/Framework/Storage/IStorageProvider.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System.Threading.Tasks;
namespace StardewModdingAPI.Web.Framework.Storage
diff --git a/src/SMAPI.Web/Framework/Storage/StorageProvider.cs b/src/SMAPI.Web/Framework/Storage/StorageProvider.cs
index 0177e602..effbbc9f 100644
--- a/src/SMAPI.Web/Framework/Storage/StorageProvider.cs
+++ b/src/SMAPI.Web/Framework/Storage/StorageProvider.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System;
using System.Collections.Generic;
using System.IO;
@@ -65,11 +63,11 @@ namespace StardewModdingAPI.Web.Framework.Storage
BlobClient blob = this.GetAzureBlobClient(id);
await blob.UploadAsync(stream);
- return new UploadResult(true, id, null);
+ return new UploadResult(id, null);
}
catch (Exception ex)
{
- return new UploadResult(false, null, ex.Message);
+ return new UploadResult(null, ex.Message);
}
}
@@ -77,10 +75,10 @@ namespace StardewModdingAPI.Web.Framework.Storage
else
{
string path = this.GetDevFilePath(id);
- Directory.CreateDirectory(Path.GetDirectoryName(path));
+ Directory.CreateDirectory(Path.GetDirectoryName(path)!);
File.WriteAllText(path, content);
- return new UploadResult(true, id, null);
+ return new UploadResult(id, null);
}
}
@@ -110,21 +108,15 @@ namespace StardewModdingAPI.Web.Framework.Storage
string content = this.GzipHelper.DecompressString(reader.ReadToEnd());
// build model
- return new StoredFileInfo
- {
- Success = true,
- Content = content,
- Expiry = expiry.UtcDateTime
- };
+ return new StoredFileInfo(content, expiry);
}
catch (RequestFailedException ex)
{
- return new StoredFileInfo
- {
- Error = ex.ErrorCode == "BlobNotFound"
+ return new StoredFileInfo(
+ error: ex.ErrorCode == "BlobNotFound"
? "There's no file with that ID."
: $"Could not fetch that file from storage ({ex.ErrorCode}: {ex.Message})."
- };
+ );
}
}
@@ -137,10 +129,7 @@ namespace StardewModdingAPI.Web.Framework.Storage
file.Delete();
if (!file.Exists)
{
- return new StoredFileInfo
- {
- Error = "There's no file with that ID."
- };
+ return new StoredFileInfo(error: "There's no file with that ID.");
}
// renew
@@ -151,13 +140,11 @@ namespace StardewModdingAPI.Web.Framework.Storage
}
// build model
- return new StoredFileInfo
- {
- Success = true,
- Content = File.ReadAllText(file.FullName),
- Expiry = DateTime.UtcNow.AddDays(this.ExpiryDays),
- Warning = "This file was saved temporarily to the local computer. This should only happen in a local development environment."
- };
+ return new StoredFileInfo(
+ content: File.ReadAllText(file.FullName),
+ expiry: DateTime.UtcNow.AddDays(this.ExpiryDays),
+ warning: "This file was saved temporarily to the local computer. This should only happen in a local development environment."
+ );
}
}
@@ -166,12 +153,7 @@ namespace StardewModdingAPI.Web.Framework.Storage
{
PasteInfo response = await this.Pastebin.GetAsync(id);
response.Content = this.GzipHelper.DecompressString(response.Content);
- return new StoredFileInfo
- {
- Success = response.Success,
- Content = response.Content,
- Error = response.Error
- };
+ return new StoredFileInfo(response.Content, null, error: response.Error);
}
}
@@ -179,8 +161,8 @@ namespace StardewModdingAPI.Web.Framework.Storage
/// <param name="id">The file ID.</param>
private BlobClient GetAzureBlobClient(string id)
{
- var azure = new BlobServiceClient(this.ClientsConfig.AzureBlobConnectionString);
- var container = azure.GetBlobContainerClient(this.ClientsConfig.AzureBlobTempContainer);
+ BlobServiceClient azure = new(this.ClientsConfig.AzureBlobConnectionString);
+ BlobContainerClient container = azure.GetBlobContainerClient(this.ClientsConfig.AzureBlobTempContainer);
return container.GetBlobClient($"uploads/{id}");
}
diff --git a/src/SMAPI.Web/Framework/Storage/StoredFileInfo.cs b/src/SMAPI.Web/Framework/Storage/StoredFileInfo.cs
index cd941c94..bbbcf2a9 100644
--- a/src/SMAPI.Web/Framework/Storage/StoredFileInfo.cs
+++ b/src/SMAPI.Web/Framework/Storage/StoredFileInfo.cs
@@ -1,25 +1,52 @@
-#nullable disable
-
using System;
+using System.Diagnostics.CodeAnalysis;
namespace StardewModdingAPI.Web.Framework.Storage
{
/// <summary>The response for a get-file request.</summary>
internal class StoredFileInfo
{
+ /*********
+ ** Accessors
+ *********/
/// <summary>Whether the file was successfully fetched.</summary>
- public bool Success { get; set; }
+ [MemberNotNullWhen(true, nameof(StoredFileInfo.Content))]
+ public bool Success => this.Content != null && this.Error == null;
/// <summary>The fetched file content (if <see cref="Success"/> is <c>true</c>).</summary>
- public string Content { get; set; }
+ public string? Content { get; }
/// <summary>When the file will no longer be available.</summary>
- public DateTime? Expiry { get; set; }
+ public DateTimeOffset? Expiry { get; }
/// <summary>The error message if saving succeeded, but a non-blocking issue was encountered.</summary>
- public string Warning { get; set; }
+ public string? Warning { get; }
/// <summary>The error message if saving failed.</summary>
- public string Error { get; set; }
+ public string? Error { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="content">The fetched file content (if <see cref="Success"/> is <c>true</c>).</param>
+ /// <param name="expiry">When the file will no longer be available.</param>
+ /// <param name="warning">The error message if saving succeeded, but a non-blocking issue was encountered.</param>
+ /// <param name="error">The error message if saving failed.</param>
+ public StoredFileInfo(string? content, DateTimeOffset? expiry, string? warning = null, string? error = null)
+ {
+ this.Content = content;
+ this.Expiry = expiry;
+ this.Warning = warning;
+ this.Error = error;
+ }
+
+ /// <summary>Construct an instance.</summary>
+ /// <param name="error">The error message if saving failed.</param>
+ public StoredFileInfo(string error)
+ {
+ this.Error = error;
+ }
}
}
diff --git a/src/SMAPI.Web/Framework/Storage/UploadResult.cs b/src/SMAPI.Web/Framework/Storage/UploadResult.cs
index b1eedd59..92993d42 100644
--- a/src/SMAPI.Web/Framework/Storage/UploadResult.cs
+++ b/src/SMAPI.Web/Framework/Storage/UploadResult.cs
@@ -1,4 +1,4 @@
-#nullable disable
+using System.Diagnostics.CodeAnalysis;
namespace StardewModdingAPI.Web.Framework.Storage
{
@@ -9,25 +9,25 @@ namespace StardewModdingAPI.Web.Framework.Storage
** Accessors
*********/
/// <summary>Whether the file upload succeeded.</summary>
- public bool Succeeded { get; }
+ [MemberNotNullWhen(true, nameof(UploadResult.ID))]
+ [MemberNotNullWhen(false, nameof(UploadResult.UploadError))]
+ public bool Succeeded => this.ID != null && this.UploadError == null;
/// <summary>The file ID, if applicable.</summary>
- public string ID { get; }
+ public string? ID { get; }
/// <summary>The upload error, if any.</summary>
- public string UploadError { get; }
+ public string? UploadError { get; }
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
- /// <param name="succeeded">Whether the file upload succeeded.</param>
/// <param name="id">The file ID, if applicable.</param>
/// <param name="uploadError">The upload error, if any.</param>
- public UploadResult(bool succeeded, string id, string uploadError)
+ public UploadResult(string? id, string? uploadError)
{
- this.Succeeded = succeeded;
this.ID = id;
this.UploadError = uploadError;
}
diff --git a/src/SMAPI.Web/Framework/VersionConstraint.cs b/src/SMAPI.Web/Framework/VersionConstraint.cs
index f230a95b..1b1abd81 100644
--- a/src/SMAPI.Web/Framework/VersionConstraint.cs
+++ b/src/SMAPI.Web/Framework/VersionConstraint.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
@@ -20,7 +18,7 @@ namespace StardewModdingAPI.Web.Framework
/// <param name="values">A dictionary that contains the parameters for the URL.</param>
/// <param name="routeDirection">An object that indicates whether the constraint check is being performed when an incoming request is being handled or when a URL is being generated.</param>
/// <returns><c>true</c> if the URL parameter contains a valid value; otherwise, <c>false</c>.</returns>
- public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
+ public bool Match(HttpContext? httpContext, IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
{
if (routeKey == null)
throw new ArgumentNullException(nameof(routeKey));
@@ -28,7 +26,7 @@ namespace StardewModdingAPI.Web.Framework
throw new ArgumentNullException(nameof(values));
return
- values.TryGetValue(routeKey, out object routeValue)
+ values.TryGetValue(routeKey, out object? routeValue)
&& routeValue is string routeStr
&& SemanticVersion.TryParse(routeStr, allowNonStandard: true, out _);
}