From ba7f5701def491f66b3e14aa5990eba521dfcf7c Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 30 May 2022 01:22:50 -0400 Subject: [PATCH] simplify asset type validaiton --- .../ContentManagers/ModContentManager.cs | 67 +++++-------------- 1 file 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 *********/ - /// Validates that the provided type is compatible with . - /// Type to validate compatibility of. - /// Type to validate compatibility against. - /// The asset name relative to the loader root directory. - /// The file being loaded. - /// The exception to throw if the type validation fails, otherwise . - /// if the type validation succeeds, otherwise - private bool ValidateType(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; - } - - /// Validates that the provided type is compatible with or . - /// Type to validate compatibility of. - /// First type to validate compatibility against. - /// /// Second type to validate compatibility against. - /// The asset name relative to the loader root directory. - /// The file being loaded. - /// The exception to throw if the type validation fails, otherwise . - /// if the type validation succeeds, otherwise - private bool ValidateType(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; - } - - /// Load an unpacked font file (.fnt). /// The type of asset to load. /// The asset name relative to the loader root directory. /// The file to load. private T LoadFont(IAssetName assetName, FileInfo file) { - // validate - if (!this.ValidateType(assetName, file, out var exception)) - throw exception; + this.AssertValidType(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 /// The file to load. private T LoadImageFile(IAssetName assetName, FileInfo file) { - // validate type - if (!this.ValidateType(assetName, file, out var exception)) - throw exception; - + this.AssertValidType(assetName, file, typeof(Texture2D), typeof(IRawTextureData)); bool asRawData = typeof(T).IsAssignableTo(typeof(IRawTextureData)); // load @@ -302,11 +256,8 @@ namespace StardewModdingAPI.Framework.ContentManagers /// The file to load. private T LoadMapFile(IAssetName assetName, FileInfo file) { - // validate - if (!this.ValidateType(assetName, file, out var exception)) - throw exception; + this.AssertValidType(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'."); } + /// Assert that the asset type is compatible with one of the allowed types. + /// The actual asset type. + /// The asset name relative to the loader root directory. + /// The file being loaded. + /// The allowed asset types. + /// The is not compatible with any of the . + private void AssertValidType(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))}'."); + } + /// Get an error which indicates that an asset couldn't be loaded. /// Why loading an asset through the content pipeline failed. /// The asset name that failed to load.