diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-07-01 21:31:21 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-07-01 21:31:21 -0400 |
commit | 600ef562861fe306390b78ee8f08036f0872e92c (patch) | |
tree | 5e776378a672cb13b8f781932bbc595442431e14 /src/StardewModdingAPI | |
parent | 306427786b9ae349e3f33ca2e4be6a79b63cf6ce (diff) | |
download | SMAPI-600ef562861fe306390b78ee8f08036f0872e92c.tar.gz SMAPI-600ef562861fe306390b78ee8f08036f0872e92c.tar.bz2 SMAPI-600ef562861fe306390b78ee8f08036f0872e92c.zip |
improve error handling when mods set invalid asset value (#255)
Diffstat (limited to 'src/StardewModdingAPI')
-rw-r--r-- | src/StardewModdingAPI/Framework/SContentManager.cs | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/src/StardewModdingAPI/Framework/SContentManager.cs b/src/StardewModdingAPI/Framework/SContentManager.cs index 1ee1eae6..53afb729 100644 --- a/src/StardewModdingAPI/Framework/SContentManager.cs +++ b/src/StardewModdingAPI/Framework/SContentManager.cs @@ -219,31 +219,47 @@ namespace StardewModdingAPI.Framework // get metadata IAssetInfo info = new AssetInfo(locale, normalisedKey, typeof(T), this.NormaliseAssetName); - // load asset - T asset = getData(); // edit asset - IAssetData data = new AssetDataForObject(info.Locale, info.AssetName, asset, this.NormaliseAssetName); + IAssetData data = this.GetAssetData(info, getData()); foreach (var entry in this.GetAssetEditors()) { + // check for match IModMetadata mod = entry.Mod; IAssetEditor editor = entry.Editor; - if (!editor.CanEdit<T>(info)) continue; + // try edit this.Monitor.Log($"{mod.DisplayName} intercepted {info.AssetName}.", LogLevel.Trace); + object prevAsset = data.Data; editor.Edit<T>(data); + + // validate edit if (data.Data == null) - throw new InvalidOperationException($"{mod.DisplayName} incorrectly set asset '{normalisedKey}' to a null value."); - if (!(data.Data is T)) - throw new InvalidOperationException($"{mod.DisplayName} incorrectly set asset '{normalisedKey}' to incompatible type '{data.Data.GetType()}', expected '{typeof(T)}'."); + { + data = this.GetAssetData(info, prevAsset); + this.Monitor.Log($"{mod.DisplayName} incorrectly set asset '{normalisedKey}' to a null value; ignoring override.", LogLevel.Warn); + } + else if (!(data.Data is T)) + { + data = this.GetAssetData(info, prevAsset); + this.Monitor.Log($"{mod.DisplayName} incorrectly set asset '{normalisedKey}' to incompatible type '{data.Data.GetType()}', expected '{typeof(T)}'; ignoring override.", LogLevel.Warn); + } } // return result return (T)data.Data; } + /// <summary>Get an asset edit helper.</summary> + /// <param name="info">The asset info.</param> + /// <param name="asset">The loaded asset data.</param> + private IAssetData GetAssetData(IAssetInfo info, object asset) + { + return new AssetDataForObject(info.Locale, info.AssetName, asset, this.NormaliseAssetName); + } + /// <summary>Get all registered asset editors.</summary> private IEnumerable<(IModMetadata Mod, IAssetEditor Editor)> GetAssetEditors() { |