From 09c52fb3f530bb450de5a7b4ba2ca09c1035b3cd Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 6 May 2022 19:39:51 -0400 Subject: cache legacy asset operations by target type --- src/SMAPI/Framework/ContentCoordinator.cs | 50 +++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 16 deletions(-) (limited to 'src/SMAPI/Framework') diff --git a/src/SMAPI/Framework/ContentCoordinator.cs b/src/SMAPI/Framework/ContentCoordinator.cs index 8ce0dba1..4f52d57e 100644 --- a/src/SMAPI/Framework/ContentCoordinator.cs +++ b/src/SMAPI/Framework/ContentCoordinator.cs @@ -83,11 +83,11 @@ namespace StardewModdingAPI.Framework /// A cache of asset operation groups created for legacy implementations. [Obsolete] - private readonly Dictionary LegacyLoaderCache = new(ReferenceEqualityComparer.Instance); + private readonly Dictionary> LegacyLoaderCache = new(ReferenceEqualityComparer.Instance); /// A cache of asset operation groups created for legacy implementations. [Obsolete] - private readonly Dictionary LegacyEditorCache = new(ReferenceEqualityComparer.Instance); + private readonly Dictionary> LegacyEditorCache = new(ReferenceEqualityComparer.Instance); /********* @@ -606,9 +606,11 @@ namespace StardewModdingAPI.Framework } // add operation - if (!this.LegacyLoaderCache.TryGetValue(loader.Data, out AssetOperationGroup? group)) - { - this.LegacyLoaderCache[loader.Data] = group = new AssetOperationGroup( + yield return this.GetOrCreateLegacyOperationGroup( + cache: this.LegacyLoaderCache, + editor: loader.Data, + dataType: info.DataType, + createGroup: () => new AssetOperationGroup( mod: loader.Mod, loadOperations: new[] { @@ -622,10 +624,8 @@ namespace StardewModdingAPI.Framework ) }, editOperations: Array.Empty() - ); - } - - yield return group; + ) + ); } // legacy edit operations @@ -665,9 +665,11 @@ namespace StardewModdingAPI.Framework }; // add operation - if (!this.LegacyEditorCache.TryGetValue(editor.Data, out AssetOperationGroup? group)) - { - this.LegacyEditorCache[editor.Data] = group = new AssetOperationGroup( + yield return this.GetOrCreateLegacyOperationGroup( + cache: this.LegacyEditorCache, + editor: editor.Data, + dataType: info.DataType, + createGroup: () => new AssetOperationGroup( mod: editor.Mod, loadOperations: Array.Empty(), editOperations: new[] @@ -681,14 +683,30 @@ namespace StardewModdingAPI.Framework ) ) } - ); - } - - yield return group; + ) + ); } #pragma warning restore CS0612, CS0618 } + /// Get a cached asset operation group for a legacy or instance, creating it if needed. + /// The editor type (one of or ). + /// The cached operation groups for the interceptor type. + /// The legacy asset interceptor. + /// The asset data type. + /// Create the asset operation group if it's not cached yet. + private AssetOperationGroup GetOrCreateLegacyOperationGroup(Dictionary> cache, TInterceptor editor, Type dataType, Func createGroup) + where TInterceptor : class + { + if (!cache.TryGetValue(editor, out Dictionary? cacheByType)) + cache[editor] = cacheByType = new Dictionary(); + + if (!cacheByType.TryGetValue(dataType, out AssetOperationGroup? group)) + cacheByType[dataType] = group = createGroup(); + + return group; + } + /// Get an asset info compatible with legacy and instances, which always expect the base name. /// The asset info. private IAssetInfo GetLegacyAssetInfo(IAssetInfo asset) -- cgit