From 1ddf70697eda8b721617c023cd827fa6ac0759c4 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 18 May 2022 20:13:09 -0400 Subject: simplify asset propagation a bit to prepare for the upcoming SDV 1.6 --- src/SMAPI/Framework/ContentCoordinator.cs | 7 +++++- .../ContentManagers/BaseContentManager.cs | 27 ++++++++++------------ .../Framework/ContentManagers/IContentManager.cs | 11 +++++---- 3 files changed, 25 insertions(+), 20 deletions(-) (limited to 'src/SMAPI') diff --git a/src/SMAPI/Framework/ContentCoordinator.cs b/src/SMAPI/Framework/ContentCoordinator.cs index da8f0da9..dd3d2917 100644 --- a/src/SMAPI/Framework/ContentCoordinator.cs +++ b/src/SMAPI/Framework/ContentCoordinator.cs @@ -394,9 +394,14 @@ namespace StardewModdingAPI.Framework // cached assets foreach (IContentManager contentManager in this.ContentManagers) { - foreach ((string key, object asset) in contentManager.InvalidateCache((key, type) => predicate(contentManager, key, type), dispose)) + foreach ((string key, object asset) in contentManager.GetCachedAssets()) { + if (!predicate(contentManager, key, asset.GetType())) + continue; + AssetName assetName = this.ParseAssetName(key, allowLocales: true); + contentManager.InvalidateCache(assetName, dispose); + if (!invalidatedAssets.ContainsKey(assetName)) invalidatedAssets[assetName] = asset.GetType(); } diff --git a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs index 575d252e..ddc02a8c 100644 --- a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs @@ -231,24 +231,21 @@ namespace StardewModdingAPI.Framework.ContentManagers ** Cache invalidation ****/ /// - public IDictionary InvalidateCache(Func predicate, bool dispose = false) + public IEnumerable> GetCachedAssets() { - IDictionary removeAssets = new Dictionary(StringComparer.OrdinalIgnoreCase); - this.Cache.Remove((key, asset) => - { - string baseAssetName = this.Coordinator.ParseAssetName(key, allowLocales: this.TryLocalizeKeys).BaseName; + foreach (string key in this.Cache.Keys) + yield return new(key, this.Cache[key]); + } - // check if asset should be removed - bool remove = removeAssets.ContainsKey(baseAssetName); - if (!remove && predicate(baseAssetName, asset.GetType())) - { - removeAssets[baseAssetName] = asset; - remove = true; - } - return remove; - }, dispose); + /// + public bool InvalidateCache(IAssetName assetName, bool dispose = false) + { + if (!this.Cache.ContainsKey(assetName.Name)) + return false; - return removeAssets; + // remove from cache + this.Cache.Remove(assetName.Name, dispose); + return true; } /// diff --git a/src/SMAPI/Framework/ContentManagers/IContentManager.cs b/src/SMAPI/Framework/ContentManagers/IContentManager.cs index ac67cad5..f2e3b9f0 100644 --- a/src/SMAPI/Framework/ContentManagers/IContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/IContentManager.cs @@ -32,7 +32,7 @@ namespace StardewModdingAPI.Framework.ContentManagers /// The expected asset type. /// The normalized asset name. bool DoesAssetExist(IAssetName assetName) - where T: notnull; + where T : notnull; /// Load an asset through the content pipeline, using a localized variant of the if available. /// The type of asset to load. @@ -65,10 +65,13 @@ namespace StardewModdingAPI.Framework.ContentManagers /// The asset path relative to the loader root directory, not including the .xnb extension. bool IsLoaded(IAssetName assetName); + /// Get all assets in the cache. + IEnumerable> GetCachedAssets(); + /// Purge matched assets from the cache. - /// Matches the asset keys to invalidate. + /// The asset name to dispose. /// Whether to dispose invalidated assets. This should only be true when they're being invalidated as part of a dispose, to avoid crashing the game. - /// Returns the invalidated asset names and instances. - IDictionary InvalidateCache(Func predicate, bool dispose = false); + /// Returns whether the asset was in the cache. + bool InvalidateCache(IAssetName assetName, bool dispose = false); } } -- cgit