diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-07-01 19:55:08 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-07-01 19:55:08 -0400 |
commit | 306427786b9ae349e3f33ca2e4be6a79b63cf6ce (patch) | |
tree | 8b3f6ce9121703339b5d248f5ca65ee5221be35b /src | |
parent | 3b6adf3c2676fa8f73997f9c1f8ec5f727f73690 (diff) | |
download | SMAPI-306427786b9ae349e3f33ca2e4be6a79b63cf6ce.tar.gz SMAPI-306427786b9ae349e3f33ca2e4be6a79b63cf6ce.tar.bz2 SMAPI-306427786b9ae349e3f33ca2e4be6a79b63cf6ce.zip |
let mods implement IAssetEditor for simple cases (#255)
Diffstat (limited to 'src')
-rw-r--r-- | src/StardewModdingAPI/Framework/SContentManager.cs | 46 |
1 files changed, 32 insertions, 14 deletions
diff --git a/src/StardewModdingAPI/Framework/SContentManager.cs b/src/StardewModdingAPI/Framework/SContentManager.cs index 24585963..1ee1eae6 100644 --- a/src/StardewModdingAPI/Framework/SContentManager.cs +++ b/src/StardewModdingAPI/Framework/SContentManager.cs @@ -224,25 +224,43 @@ namespace StardewModdingAPI.Framework // edit asset IAssetData data = new AssetDataForObject(info.Locale, info.AssetName, asset, this.NormaliseAssetName); - foreach (var modEditors in this.Editors) + foreach (var entry in this.GetAssetEditors()) { - IModMetadata mod = modEditors.Key; - foreach (IAssetEditor editor in modEditors.Value) - { - if (!editor.CanEdit<T>(info)) - continue; - - this.Monitor.Log($"{mod.DisplayName} intercepted {info.AssetName}.", LogLevel.Trace); - editor.Edit<T>(data); - 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)}'."); - } + IModMetadata mod = entry.Mod; + IAssetEditor editor = entry.Editor; + + if (!editor.CanEdit<T>(info)) + continue; + + this.Monitor.Log($"{mod.DisplayName} intercepted {info.AssetName}.", LogLevel.Trace); + editor.Edit<T>(data); + 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)}'."); } // return result return (T)data.Data; } + + /// <summary>Get all registered asset editors.</summary> + private IEnumerable<(IModMetadata Mod, IAssetEditor Editor)> GetAssetEditors() + { + foreach (var entry in this.Editors) + { + IModMetadata metadata = entry.Key; + IList<IAssetEditor> editors = entry.Value; + + // special case if mod implements interface + // ReSharper disable once SuspiciousTypeConversion.Global + if (metadata.Mod is IAssetEditor modAsEditor) + yield return (metadata, modAsEditor); + + // registered editors + foreach (IAssetEditor editor in editors) + yield return (metadata, editor); + } + } } } |