using System; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Linq; using Microsoft.Xna.Framework; using StardewModdingAPI.Framework.Reflection; using StardewModdingAPI.Internal; using StardewModdingAPI.Toolkit.Utilities; using StardewValley; namespace StardewModdingAPI.Framework.Content { /// A low-level wrapper around the content cache which handles reading, writing, and invalidating entries in the cache. This doesn't handle any higher-level logic like localization, loading content, etc. It assumes all keys passed in are already normalized. internal class ContentCache { /********* ** Fields *********/ /// The underlying asset cache. private readonly IDictionary Cache; /// Applies platform-specific asset key normalization so it's consistent with the underlying cache. private readonly Func NormalizeAssetNameForPlatform; /********* ** Accessors *********/ /// Get or set the value of a raw cache entry. /// The cache key. public object this[string key] { get => this.Cache[key]; set => this.Cache[key] = value; } /// The current cache keys. public IEnumerable Keys => this.Cache.Keys; /********* ** Public methods *********/ /**** ** Constructor ****/ /// Construct an instance. /// The underlying content manager whose cache to manage. /// Simplifies access to private game code. public ContentCache(LocalizedContentManager contentManager, Reflector reflection) { // init this.Cache = reflection.GetField>(contentManager, "loadedAssets").GetValue(); // get key normalization logic if (Constants.Platform == Platform.Windows) { IReflectedMethod method = reflection.GetMethod(typeof(TitleContainer), "GetCleanPath"); this.NormalizeAssetNameForPlatform = path => method.Invoke(path); } else this.NormalizeAssetNameForPlatform = key => key.Replace('\\', '/'); // based on MonoGame's ContentManager.Load logic } /**** ** Fetch ****/ /// Get whether the cache contains a given key. /// The cache key. public bool ContainsKey(string key) { return this.Cache.ContainsKey(key); } /**** ** Normalize ****/ /// Normalize path separators in a file path. For asset keys, see instead. /// The file path to normalize. [Pure] public string NormalizePathSeparators(string path) { return PathUtilities.NormalizePathSeparators(path); } /// Normalize a cache key so it's consistent with the underlying cache. /// The asset key. [Pure] public string NormalizeKey(string key) { key = this.NormalizePathSeparators(key); return key.EndsWith(".xnb", StringComparison.InvariantCultureIgnoreCase) ? key.Substring(0, key.Length - 4) : this.NormalizeAssetNameForPlatform(key); } /**** ** Remove ****/ /// Remove an asset with the given key. /// The cache key. /// Whether to dispose the entry value, if applicable. /// Returns the removed key (if any). public bool Remove(string key, bool dispose) { // get entry if (!this.Cache.TryGetValue(key, out object value)) return false; // dispose & remove entry if (dispose && value is IDisposable disposable) disposable.Dispose(); return this.Cache.Remove(key); } /// Purge matched assets from the cache. /// Matches the asset keys to invalidate. /// 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 removed keys (if any). public IEnumerable Remove(Func predicate, bool dispose = false) { List removed = new List(); foreach (string key in this.Cache.Keys.ToArray()) { Type type = this.Cache[key].GetType(); if (predicate(key, type)) { this.Remove(key, dispose); removed.Add(key); } } return removed; } } }