using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using Microsoft.Xna.Framework;
using StardewModdingAPI.Framework.ModLoading;
using StardewModdingAPI.Framework.Reflection;
using StardewModdingAPI.Framework.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 localisation, loading content, etc. It assumes all keys passed in are already normalised.
internal class ContentCache
{
/*********
** Properties
*********/
/// The underlying asset cache.
private readonly IDictionary Cache;
/// Applies platform-specific asset key normalisation so it's consistent with the underlying cache.
private readonly Func NormaliseAssetNameForPlatform;
/*********
** 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 normalisation logic
if (Constants.TargetPlatform == Platform.Windows)
{
IReflectedMethod method = reflection.GetMethod(typeof(TitleContainer), "GetCleanPath");
this.NormaliseAssetNameForPlatform = path => method.Invoke(path);
}
else
this.NormaliseAssetNameForPlatform = 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);
}
/****
** Normalise
****/
/// Normalise path separators in a file path. For asset keys, see instead.
/// The file path to normalise.
[Pure]
public string NormalisePathSeparators(string path)
{
return PathUtilities.NormalisePathSeparators(path);
}
/// Normalise a cache key so it's consistent with the underlying cache.
/// The asset key.
[Pure]
public string NormaliseKey(string key)
{
key = this.NormalisePathSeparators(key);
return key.EndsWith(".xnb", StringComparison.InvariantCultureIgnoreCase)
? key.Substring(0, key.Length - 4)
: this.NormaliseAssetNameForPlatform(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;
}
}
}