diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2022-06-16 22:14:44 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2022-06-16 22:14:44 -0400 |
commit | 8e9237bdd7ec179975c9be5e28c811b42007e707 (patch) | |
tree | 665ca44f40e77e8f4a3af55ee442e2b3acba6434 /src/SMAPI/Framework/Content/ContentCache.cs | |
parent | e10147e7bda94a8fbc58684246628a6520d2c6b8 (diff) | |
parent | 011aa4c9d07d6fc313d6d1ee107651778bb3c665 (diff) | |
download | SMAPI-8e9237bdd7ec179975c9be5e28c811b42007e707.tar.gz SMAPI-8e9237bdd7ec179975c9be5e28c811b42007e707.tar.bz2 SMAPI-8e9237bdd7ec179975c9be5e28c811b42007e707.zip |
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Framework/Content/ContentCache.cs')
-rw-r--r-- | src/SMAPI/Framework/Content/ContentCache.cs | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/src/SMAPI/Framework/Content/ContentCache.cs b/src/SMAPI/Framework/Content/ContentCache.cs index 736ee5da..bf42812b 100644 --- a/src/SMAPI/Framework/Content/ContentCache.cs +++ b/src/SMAPI/Framework/Content/ContentCache.cs @@ -14,7 +14,7 @@ namespace StardewModdingAPI.Framework.Content ** Fields *********/ /// <summary>The underlying asset cache.</summary> - private readonly IDictionary<string, object> Cache; + private readonly Dictionary<string, object> Cache; /********* @@ -29,7 +29,7 @@ namespace StardewModdingAPI.Framework.Content } /// <summary>The current cache keys.</summary> - public IEnumerable<string> Keys => this.Cache.Keys; + public Dictionary<string, object>.KeyCollection Keys => this.Cache.Keys; /********* @@ -89,33 +89,36 @@ namespace StardewModdingAPI.Framework.Content /// <returns>Returns the removed key (if any).</returns> public bool Remove(string key, bool dispose) { - // get entry - if (!this.Cache.TryGetValue(key, out object? value)) + // remove and get entry + if (!this.Cache.Remove(key, out object? value)) return false; // dispose & remove entry if (dispose && value is IDisposable disposable) disposable.Dispose(); - return this.Cache.Remove(key); + return true; } - /// <summary>Purge matched assets from the cache.</summary> + /// <summary>Purge assets matching <paramref name="predicate"/> from the cache.</summary> /// <param name="predicate">Matches the asset keys to invalidate.</param> - /// <param name="dispose">Whether to dispose invalidated assets. This should only be <c>true</c> when they're being invalidated as part of a dispose, to avoid crashing the game.</param> - /// <returns>Returns the removed keys (if any).</returns> + /// <param name="dispose">Whether to dispose invalidated assets. This should only be <see langword="true"/> when they're being invalidated as part of a <see cref="IDisposable.Dispose"/>, to avoid crashing the game.</param> + /// <returns>Returns any removed keys.</returns> public IEnumerable<string> Remove(Func<string, object, bool> predicate, bool dispose) { - List<string> removed = new List<string>(); - foreach (string key in this.Cache.Keys.ToArray()) + List<string> removed = new(); + foreach ((string key, object value) in this.Cache) { - if (predicate(key, this.Cache[key])) - { - this.Remove(key, dispose); + if (predicate(key, value)) removed.Add(key); - } } - return removed; + + foreach (string key in removed) + this.Remove(key, dispose); + + return removed.Count == 0 + ? Enumerable.Empty<string>() // let GC collect the list in gen0 instead of potentially living longer + : removed; } } } |