summaryrefslogtreecommitdiff
path: root/src/SMAPI
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-05-30 01:22:50 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-06-10 00:04:25 -0400
commitba7f5701def491f66b3e14aa5990eba521dfcf7c (patch)
treea0a1751181a19e15c711fb09cfb011239536e005 /src/SMAPI
parent87c7095e8d7bfc1c1819e7d17cfea3d07c65dfea (diff)
downloadSMAPI-ba7f5701def491f66b3e14aa5990eba521dfcf7c.tar.gz
SMAPI-ba7f5701def491f66b3e14aa5990eba521dfcf7c.tar.bz2
SMAPI-ba7f5701def491f66b3e14aa5990eba521dfcf7c.zip
simplify asset type validaiton
Diffstat (limited to 'src/SMAPI')
-rw-r--r--src/SMAPI/Framework/ContentManagers/ModContentManager.cs67
1 files changed, 15 insertions, 52 deletions
diff --git a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs
index d4a30f71..1f38b76b 100644
--- a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs
+++ b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs
@@ -162,57 +162,14 @@ namespace StardewModdingAPI.Framework.ContentManagers
/*********
** Private methods
*********/
- /// <summary>Validates that the provided <typeparamref name="TInput">type</typeparamref> is compatible with <typeparamref name="TExpected"/>.</summary>
- /// <typeparam name="TInput">Type to validate compatibility of.</typeparam>
- /// <typeparam name="TExpected">Type to validate compatibility against.</typeparam>
- /// <param name="assetName">The asset name relative to the loader root directory.</param>
- /// <param name="file">The file being loaded.</param>
- /// <param name="exception">The exception to throw if the type validation fails, otherwise <see langword="null"/>.</param>
- /// <returns><see langword="true"/> if the type validation succeeds, otherwise <see langword="false"/></returns>
- private bool ValidateType<TInput, TExpected>(IAssetName assetName, FileInfo file, [NotNullWhen(false)] out SContentLoadException? exception)
- {
- if (typeof(TInput).IsAssignableFrom(typeof(TExpected)))
- {
- exception = null;
- return true;
- }
-
- exception = this.GetLoadError(assetName, ContentLoadErrorType.InvalidData, $"can't read file with extension '{file.Extension}' as type '{typeof(TInput)}'; must be type '{typeof(TExpected)}'.");
- return false;
- }
-
- /// <summary>Validates that the provided <typeparamref name="TInput">type</typeparamref> is compatible with <typeparamref name="TExpected0"/> or <typeparamref name="TExpected1"/>.</summary>
- /// <typeparam name="TInput">Type to validate compatibility of.</typeparam>
- /// <typeparam name="TExpected0">First type to validate compatibility against.</typeparam>
- /// /// <typeparam name="TExpected1">Second type to validate compatibility against.</typeparam>
- /// <param name="assetName">The asset name relative to the loader root directory.</param>
- /// <param name="file">The file being loaded.</param>
- /// <param name="exception">The exception to throw if the type validation fails, otherwise <see langword="null"/>.</param>
- /// <returns><see langword="true"/> if the type validation succeeds, otherwise <see langword="false"/></returns>
- private bool ValidateType<TInput, TExpected0, TExpected1>(IAssetName assetName, FileInfo file, [NotNullWhen(false)] out SContentLoadException? exception)
- {
- if (typeof(TInput).IsAssignableFrom(typeof(TExpected0)) || typeof(TInput).IsAssignableFrom(typeof(TExpected1)))
- {
- exception = null;
- return true;
- }
-
- exception = this.GetLoadError(assetName, ContentLoadErrorType.InvalidData, $"can't read file with extension '{file.Extension}' as type '{typeof(TInput)}'; must be type '{typeof(TExpected0)}' or '{typeof(TExpected1)}'.");
- return false;
- }
-
-
/// <summary>Load an unpacked font file (<c>.fnt</c>).</summary>
/// <typeparam name="T">The type of asset to load.</typeparam>
/// <param name="assetName">The asset name relative to the loader root directory.</param>
/// <param name="file">The file to load.</param>
private T LoadFont<T>(IAssetName assetName, FileInfo file)
{
- // validate
- if (!this.ValidateType<T, XmlSource>(assetName, file, out var exception))
- throw exception;
+ this.AssertValidType<T>(assetName, file, typeof(XmlSource));
- // load
string source = File.ReadAllText(file.FullName);
return (T)(object)new XmlSource(source);
}
@@ -235,10 +192,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
/// <param name="file">The file to load.</param>
private T LoadImageFile<T>(IAssetName assetName, FileInfo file)
{
- // validate type
- if (!this.ValidateType<T, Texture2D, IRawTextureData>(assetName, file, out var exception))
- throw exception;
-
+ this.AssertValidType<T>(assetName, file, typeof(Texture2D), typeof(IRawTextureData));
bool asRawData = typeof(T).IsAssignableTo(typeof(IRawTextureData));
// load
@@ -302,11 +256,8 @@ namespace StardewModdingAPI.Framework.ContentManagers
/// <param name="file">The file to load.</param>
private T LoadMapFile<T>(IAssetName assetName, FileInfo file)
{
- // validate
- if (!this.ValidateType<T, Map>(assetName, file, out var exception))
- throw exception;
+ this.AssertValidType<T>(assetName, file, typeof(Map));
- // load
FormatManager formatManager = FormatManager.Instance;
Map map = formatManager.LoadMap(file.FullName);
map.assetPath = assetName.Name;
@@ -348,6 +299,18 @@ namespace StardewModdingAPI.Framework.ContentManagers
throw this.GetLoadError(assetName, ContentLoadErrorType.InvalidName, $"unknown file extension '{file.Extension}'; must be one of '.fnt', '.json', '.png', '.tbin', '.tmx', or '.xnb'.");
}
+ /// <summary>Assert that the asset type is compatible with one of the allowed types.</summary>
+ /// <typeparam name="TAsset">The actual asset type.</typeparam>
+ /// <param name="assetName">The asset name relative to the loader root directory.</param>
+ /// <param name="file">The file being loaded.</param>
+ /// <param name="validTypes">The allowed asset types.</param>
+ /// <exception cref="SContentLoadException">The <typeparamref name="TAsset"/> is not compatible with any of the <paramref name="validTypes"/>.</exception>
+ private void AssertValidType<TAsset>(IAssetName assetName, FileInfo file, params Type[] validTypes)
+ {
+ if (!validTypes.Any(validType => validType.IsAssignableFrom(typeof(TAsset))))
+ throw this.GetLoadError(assetName, ContentLoadErrorType.InvalidData, $"can't read file with extension '{file.Extension}' as type '{typeof(TAsset)}'; must be type '{string.Join("' or '", validTypes.Select(p => p.FullName))}'.");
+ }
+
/// <summary>Get an error which indicates that an asset couldn't be loaded.</summary>
/// <param name="errorType">Why loading an asset through the content pipeline failed.</param>
/// <param name="assetName">The asset name that failed to load.</param>