summaryrefslogtreecommitdiff
path: root/src/SMAPI.Toolkit/Framework/ManifestValidator.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI.Toolkit/Framework/ManifestValidator.cs')
-rw-r--r--src/SMAPI.Toolkit/Framework/ManifestValidator.cs57
1 files changed, 31 insertions, 26 deletions
diff --git a/src/SMAPI.Toolkit/Framework/ManifestValidator.cs b/src/SMAPI.Toolkit/Framework/ManifestValidator.cs
index 62cfd8e9..461dc325 100644
--- a/src/SMAPI.Toolkit/Framework/ManifestValidator.cs
+++ b/src/SMAPI.Toolkit/Framework/ManifestValidator.cs
@@ -1,4 +1,5 @@
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using StardewModdingAPI.Toolkit.Utilities;
@@ -8,40 +9,47 @@ namespace StardewModdingAPI.Toolkit.Framework
/// <summary>Validates manifest fields.</summary>
public static class ManifestValidator
{
- /// <summary>Try to validate a manifest's fields. Fails if any invalid field is found.</summary>
+ /// <summary>Validate a manifest's fields.</summary>
/// <param name="manifest">The manifest to validate.</param>
- /// <param name="error">The error message to display to the user.</param>
- /// <returns>Returns whether the manifest was validated successfully.</returns>
- public static bool TryValidate(IManifest manifest, out string error)
+ /// <param name="error">The error message indicating why validation failed, if applicable.</param>
+ /// <returns>Returns whether all manifest fields validated successfully.</returns>
+ [SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract", Justification = "This is the method that ensures those annotations are respected.")]
+ public static bool TryValidateFields(IManifest manifest, out string error)
{
- // validate DLL / content pack fields
+ //
+ // Note: SMAPI assumes that it can grammatically append the returned sentence in the
+ // form "failed loading <mod> because its <error>". Any errors returned should be valid
+ // in that format, unless the SMAPI call is adjusted accordingly.
+ //
+
bool hasDll = !string.IsNullOrWhiteSpace(manifest.EntryDll);
bool isContentPack = manifest.ContentPackFor != null;
- // validate field presence
- if (!hasDll && !isContentPack)
- {
- error = $"manifest has no {nameof(IManifest.EntryDll)} or {nameof(IManifest.ContentPackFor)} field; must specify one.";
- return false;
- }
- if (hasDll && isContentPack)
+ // validate use of EntryDll vs ContentPackFor fields
+ if (hasDll == isContentPack)
{
- error = $"manifest sets both {nameof(IManifest.EntryDll)} and {nameof(IManifest.ContentPackFor)}, which are mutually exclusive.";
+ error = hasDll
+ ? $"manifest sets both {nameof(IManifest.EntryDll)} and {nameof(IManifest.ContentPackFor)}, which are mutually exclusive."
+ : $"manifest has no {nameof(IManifest.EntryDll)} or {nameof(IManifest.ContentPackFor)} field; must specify one.";
return false;
}
- // validate DLL filename format
- if (hasDll && manifest.EntryDll!.Intersect(Path.GetInvalidFileNameChars()).Any())
+ // validate EntryDll/ContentPackFor format
+ if (hasDll)
{
- error = $"manifest has invalid filename '{manifest.EntryDll}' for the EntryDLL field.";
- return false;
+ if (manifest.EntryDll!.Intersect(Path.GetInvalidFileNameChars()).Any())
+ {
+ error = $"manifest has invalid filename '{manifest.EntryDll}' for the {nameof(IManifest.EntryDll)} field.";
+ return false;
+ }
}
-
- // validate content pack ID
- else if (isContentPack && string.IsNullOrWhiteSpace(manifest.ContentPackFor!.UniqueID))
+ else
{
- error = $"manifest declares {nameof(IManifest.ContentPackFor)} without its required {nameof(IManifestContentPackFor.UniqueID)} field.";
- return false;
+ if (string.IsNullOrWhiteSpace(manifest.ContentPackFor!.UniqueID))
+ {
+ error = $"manifest declares {nameof(IManifest.ContentPackFor)} without its required {nameof(IManifestContentPackFor.UniqueID)} field.";
+ return false;
+ }
}
// validate required fields
@@ -69,24 +77,21 @@ namespace StardewModdingAPI.Toolkit.Framework
return false;
}
- // validate dependencies
+ // validate dependency format
foreach (IManifestDependency? dependency in manifest.Dependencies)
{
- // null dependency
if (dependency == null)
{
error = $"manifest has a null entry under {nameof(IManifest.Dependencies)}.";
return false;
}
- // missing ID
if (string.IsNullOrWhiteSpace(dependency.UniqueID))
{
error = $"manifest has a {nameof(IManifest.Dependencies)} entry with no {nameof(IManifestDependency.UniqueID)} field.";
return false;
}
- // invalid ID
if (!PathUtilities.IsSlug(dependency.UniqueID))
{
error = $"manifest has a {nameof(IManifest.Dependencies)} entry with an invalid {nameof(IManifestDependency.UniqueID)} field (IDs must only contain letters, numbers, underscores, periods, or hyphens).";