simplify asset type validaiton

This commit is contained in:
Jesse Plamondon-Willard 2022-05-30 01:22:50 -04:00
parent 87c7095e8d
commit ba7f5701de
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
1 changed files with 15 additions and 52 deletions

View File

@ -162,57 +162,14 @@ namespace StardewModdingAPI.Framework.ContentManagers
/********* /*********
** Private methods ** 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> /// <summary>Load an unpacked font file (<c>.fnt</c>).</summary>
/// <typeparam name="T">The type of asset to load.</typeparam> /// <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="assetName">The asset name relative to the loader root directory.</param>
/// <param name="file">The file to load.</param> /// <param name="file">The file to load.</param>
private T LoadFont<T>(IAssetName assetName, FileInfo file) private T LoadFont<T>(IAssetName assetName, FileInfo file)
{ {
// validate this.AssertValidType<T>(assetName, file, typeof(XmlSource));
if (!this.ValidateType<T, XmlSource>(assetName, file, out var exception))
throw exception;
// load
string source = File.ReadAllText(file.FullName); string source = File.ReadAllText(file.FullName);
return (T)(object)new XmlSource(source); return (T)(object)new XmlSource(source);
} }
@ -235,10 +192,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
/// <param name="file">The file to load.</param> /// <param name="file">The file to load.</param>
private T LoadImageFile<T>(IAssetName assetName, FileInfo file) private T LoadImageFile<T>(IAssetName assetName, FileInfo file)
{ {
// validate type this.AssertValidType<T>(assetName, file, typeof(Texture2D), typeof(IRawTextureData));
if (!this.ValidateType<T, Texture2D, IRawTextureData>(assetName, file, out var exception))
throw exception;
bool asRawData = typeof(T).IsAssignableTo(typeof(IRawTextureData)); bool asRawData = typeof(T).IsAssignableTo(typeof(IRawTextureData));
// load // load
@ -302,11 +256,8 @@ namespace StardewModdingAPI.Framework.ContentManagers
/// <param name="file">The file to load.</param> /// <param name="file">The file to load.</param>
private T LoadMapFile<T>(IAssetName assetName, FileInfo file) private T LoadMapFile<T>(IAssetName assetName, FileInfo file)
{ {
// validate this.AssertValidType<T>(assetName, file, typeof(Map));
if (!this.ValidateType<T, Map>(assetName, file, out var exception))
throw exception;
// load
FormatManager formatManager = FormatManager.Instance; FormatManager formatManager = FormatManager.Instance;
Map map = formatManager.LoadMap(file.FullName); Map map = formatManager.LoadMap(file.FullName);
map.assetPath = assetName.Name; 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'."); 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> /// <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="errorType">Why loading an asset through the content pipeline failed.</param>
/// <param name="assetName">The asset name that failed to load.</param> /// <param name="assetName">The asset name that failed to load.</param>