diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-01-30 22:10:16 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-01-30 22:10:16 -0500 |
commit | e5d8acf240f923a09bdaad3fb14b2c34847860dc (patch) | |
tree | c86ac2e42dfd4eb058493b6f2b9050c5c51a6c4f /src/SMAPI/Framework/SCore.cs | |
parent | 9f36b2b3d69ee0a45241bfcc45953df29f167aef (diff) | |
download | SMAPI-e5d8acf240f923a09bdaad3fb14b2c34847860dc.tar.gz SMAPI-e5d8acf240f923a09bdaad3fb14b2c34847860dc.tar.bz2 SMAPI-e5d8acf240f923a09bdaad3fb14b2c34847860dc.zip |
rework asset editor/loader tracking so they're affected by load order
Diffstat (limited to 'src/SMAPI/Framework/SCore.cs')
-rw-r--r-- | src/SMAPI/Framework/SCore.cs | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/src/SMAPI/Framework/SCore.cs b/src/SMAPI/Framework/SCore.cs index 9139b371..7e1f8770 100644 --- a/src/SMAPI/Framework/SCore.cs +++ b/src/SMAPI/Framework/SCore.cs @@ -807,13 +807,13 @@ namespace StardewModdingAPI.Framework { // ReSharper disable SuspiciousTypeConversion.Global if (metadata.Mod is IAssetEditor editor) - helper.ObservableAssetEditors.Add(editor); + this.ContentCore.Editors.Add(new ModLinked<IAssetEditor>(metadata, editor)); if (metadata.Mod is IAssetLoader loader) - helper.ObservableAssetLoaders.Add(loader); + this.ContentCore.Loaders.Add(new ModLinked<IAssetLoader>(metadata, loader)); // ReSharper restore SuspiciousTypeConversion.Global - this.ContentCore.Editors[metadata] = helper.ObservableAssetEditors; - this.ContentCore.Loaders[metadata] = helper.ObservableAssetLoaders; + helper.ObservableAssetEditors.CollectionChanged += (sender, e) => this.OnInterceptorsChanged(metadata, e.NewItems?.Cast<IAssetEditor>(), e.OldItems?.Cast<IAssetEditor>(), this.ContentCore.Editors); + helper.ObservableAssetLoaders.CollectionChanged += (sender, e) => this.OnInterceptorsChanged(metadata, e.NewItems?.Cast<IAssetLoader>(), e.OldItems?.Cast<IAssetLoader>(), this.ContentCore.Loaders); } // call entry method @@ -862,6 +862,24 @@ namespace StardewModdingAPI.Framework this.ModRegistry.AreAllModsInitialized = true; } + /// <summary>Handle a mod adding or removing asset interceptors.</summary> + /// <typeparam name="T">The asset interceptor type (one of <see cref="IAssetEditor"/> or <see cref="IAssetLoader"/>).</typeparam> + /// <param name="mod">The mod metadata.</param> + /// <param name="added">The interceptors that were added.</param> + /// <param name="removed">The interceptors that were removed.</param> + /// <param name="list">The list to update.</param> + private void OnInterceptorsChanged<T>(IModMetadata mod, IEnumerable<T> added, IEnumerable<T> removed, IList<ModLinked<T>> list) + { + foreach (T interceptor in added ?? new T[0]) + list.Add(new ModLinked<T>(mod, interceptor)); + + foreach (T interceptor in removed ?? new T[0]) + { + foreach (ModLinked<T> entry in list.Where(p => p.Mod == mod && object.ReferenceEquals(p.Data, interceptor)).ToArray()) + list.Remove(entry); + } + } + /// <summary>Load a given mod.</summary> /// <param name="mod">The mod to load.</param> /// <param name="mods">The mods being loaded.</param> |