From 4ea6a4102bb69b72391334c4825bd393eff6ac97 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 23 Jul 2017 15:08:14 -0400 Subject: add support for partial cache invalidation (#335) --- src/StardewModdingAPI/Constants.cs | 68 +++++++- src/StardewModdingAPI/Framework/SContentManager.cs | 173 +++++++++++++++------ src/StardewModdingAPI/Program.cs | 16 +- 3 files changed, 202 insertions(+), 55 deletions(-) diff --git a/src/StardewModdingAPI/Constants.cs b/src/StardewModdingAPI/Constants.cs index ee57be0f..e85d7e2b 100644 --- a/src/StardewModdingAPI/Constants.cs +++ b/src/StardewModdingAPI/Constants.cs @@ -11,6 +11,9 @@ using StardewModdingAPI.AssemblyRewriters.Rewriters.Wrappers; using StardewModdingAPI.Events; using StardewModdingAPI.Framework; using StardewValley; +using StardewValley.BellsAndWhistles; +using StardewValley.Objects; +using StardewValley.Projectiles; namespace StardewModdingAPI { @@ -99,7 +102,7 @@ namespace StardewModdingAPI /********* - ** Protected methods + ** Internal methods *********/ /// Get metadata for mapping assemblies to the current platform. /// The target game platform. @@ -220,6 +223,69 @@ namespace StardewModdingAPI }; } + /// Get the game's static asset setters by (non-normalised) asset name. + /// Derived from . + internal static IDictionary> GetCoreAssetSetters() + { + return new Dictionary> + { + // from Game1.loadContent + ["LooseSprites\\daybg"] = (content, key) => Game1.daybg = content.Load(key), + ["LooseSprites\\daybg"] = (content, key) => Game1.daybg = content.Load(key), + ["LooseSprites\\nightbg"] = (content, key) => Game1.nightbg = content.Load(key), + ["Maps\\MenuTiles"] = (content, key) => Game1.menuTexture = content.Load(key), + ["LooseSprites\\Lighting\\lantern"] = (content, key) => Game1.lantern = content.Load(key), + ["LooseSprites\\Lighting\\windowLight"] = (content, key) => Game1.windowLight = content.Load(key), + ["LooseSprites\\Lighting\\sconceLight"] = (content, key) => Game1.sconceLight = content.Load(key), + ["LooseSprites\\Lighting\\greenLight"] = (content, key) => Game1.cauldronLight = content.Load(key), + ["LooseSprites\\Lighting\\indoorWindowLight"] = (content, key) => Game1.indoorWindowLight = content.Load(key), + ["LooseSprites\\shadow"] = (content, key) => Game1.shadowTexture = content.Load(key), + ["LooseSprites\\Cursors"] = (content, key) => Game1.mouseCursors = content.Load(key), + ["LooseSprites\\ControllerMaps"] = (content, key) => Game1.controllerMaps = content.Load(key), + ["TileSheets\\animations"] = (content, key) => Game1.animations = content.Load(key), + ["Data\\Achievements"] = (content, key) => Game1.achievements = content.Load>(key), + ["Data\\NPCGiftTastes"] = (content, key) => Game1.NPCGiftTastes = content.Load>(key), + ["Fonts\\SpriteFont1"] = (content, key) => Game1.dialogueFont = content.Load(key), + ["Fonts\\SmallFont"] = (content, key) => Game1.smallFont = content.Load(key), + ["Fonts\\tinyFont"] = (content, key) => Game1.tinyFont = content.Load(key), + ["Fonts\\tinyFontBorder"] = (content, key) => Game1.tinyFontBorder = content.Load(key), + ["Maps\\springobjects"] = (content, key) => Game1.objectSpriteSheet = content.Load(key), + ["TileSheets\\crops"] = (content, key) => Game1.cropSpriteSheet = content.Load(key), + ["TileSheets\\emotes"] = (content, key) => Game1.emoteSpriteSheet = content.Load(key), + ["TileSheets\\debris"] = (content, key) => Game1.debrisSpriteSheet = content.Load(key), + ["TileSheets\\Craftables"] = (content, key) => Game1.bigCraftableSpriteSheet = content.Load(key), + ["TileSheets\\rain"] = (content, key) => Game1.rainTexture = content.Load(key), + ["TileSheets\\BuffsIcons"] = (content, key) => Game1.buffsIcons = content.Load(key), + ["Data\\ObjectInformation"] = (content, key) => Game1.objectInformation = content.Load>(key), + ["Data\\BigCraftablesInformation"] = (content, key) => Game1.bigCraftablesInformation = content.Load>(key), + ["Characters\\Farmer\\hairstyles"] = (content, key) => FarmerRenderer.hairStylesTexture = content.Load(key), + ["Characters\\Farmer\\shirts"] = (content, key) => FarmerRenderer.shirtsTexture = content.Load(key), + ["Characters\\Farmer\\hats"] = (content, key) => FarmerRenderer.hatsTexture = content.Load(key), + ["Characters\\Farmer\\accessories"] = (content, key) => FarmerRenderer.accessoriesTexture = content.Load(key), + ["TileSheets\\furniture"] = (content, key) => Furniture.furnitureTexture = content.Load(key), + ["LooseSprites\\font_bold"] = (content, key) => SpriteText.spriteTexture = content.Load(key), + ["LooseSprites\\font_colored"] = (content, key) => SpriteText.coloredTexture = content.Load(key), + ["TileSheets\\weapons"] = (content, key) => Tool.weaponsTexture = content.Load(key), + ["TileSheets\\Projectiles"] = (content, key) => Projectile.projectileSheet = content.Load(key), + + // from Farmer constructor + ["Characters\\Farmer\\farmer_base"] = (content, key) => + { + if (Game1.player != null && Game1.player.isMale) + Game1.player.FarmerRenderer = new FarmerRenderer(content.Load(key)); + }, + ["Characters\\Farmer\\farmer_girl_base"] = (content, key) => + { + if (Game1.player != null && !Game1.player.isMale) + Game1.player.FarmerRenderer = new FarmerRenderer(content.Load(key)); + } + }; + } + + + /********* + ** Private methods + *********/ /// Get the name of a save directory for the current player. private static string GetSaveFolderName() { diff --git a/src/StardewModdingAPI/Framework/SContentManager.cs b/src/StardewModdingAPI/Framework/SContentManager.cs index 669b0e7a..e6b0cac2 100644 --- a/src/StardewModdingAPI/Framework/SContentManager.cs +++ b/src/StardewModdingAPI/Framework/SContentManager.cs @@ -5,14 +5,10 @@ using System.IO; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; -using Microsoft.Xna.Framework.Graphics; using StardewModdingAPI.AssemblyRewriters; using StardewModdingAPI.Framework.Content; using StardewModdingAPI.Framework.Reflection; using StardewValley; -using StardewValley.BellsAndWhistles; -using StardewValley.Objects; -using StardewValley.Projectiles; namespace StardewModdingAPI.Framework { @@ -40,6 +36,12 @@ namespace StardewModdingAPI.Framework /// The private method which generates the locale portion of an asset name. private readonly IPrivateMethod GetKeyLocale; + /// The language codes used in asset keys. + private IDictionary KeyLocales; + + /// The game's static asset setters by normalised asset name. + private readonly IDictionary CoreAssetSetters; + /********* ** Accessors @@ -86,6 +88,48 @@ namespace StardewModdingAPI.Framework } else this.NormaliseAssetNameForPlatform = key => key.Replace('\\', '/'); // based on MonoGame's ContentManager.Load logic + + // get asset key locales + this.KeyLocales = this.GetKeyLocales(reflection); + this.CoreAssetSetters = this.GetCoreAssetSetters(); + + } + + /// Get methods to reload core game assets by normalised key. + private IDictionary GetCoreAssetSetters() + { + return Constants.GetCoreAssetSetters() + .ToDictionary>, string, Action>( + p => this.NormaliseAssetName(p.Key), + p => () => p.Value(this, p.Key) + ); + } + + /// Get the locale codes (like ja-JP) used in asset keys. + /// Simplifies access to private game code. + private IDictionary GetKeyLocales(Reflector reflection) + { + // get the private code field directly to avoid changed-code logic + IPrivateField codeField = reflection.GetPrivateField(typeof(LocalizedContentManager), "_currentLangCode"); + + // remember previous settings + LanguageCode previousCode = codeField.GetValue(); + string previousOverride = this.LanguageCodeOverride; + + // create locale => code map + IDictionary map = new Dictionary(StringComparer.InvariantCultureIgnoreCase); + this.LanguageCodeOverride = null; + foreach (LanguageCode code in Enum.GetValues(typeof(LanguageCode))) + { + codeField.SetValue(code); + map[this.GetKeyLocale.Invoke()] = code; + } + + // restore previous settings + codeField.SetValue(previousCode); + this.LanguageCodeOverride = previousOverride; + + return map; } /// Normalise path separators in a file path. For asset keys, see instead. @@ -159,54 +203,55 @@ namespace StardewModdingAPI.Framework return this.GetKeyLocale.Invoke(); } + /// Get the cached asset keys. + public IEnumerable GetAssetKeys() + { + IEnumerable GetAllAssetKeys() + { + foreach (string cacheKey in this.Cache.Keys) + { + this.ParseCacheKey(cacheKey, out string assetKey, out string _); + yield return assetKey; + } + } + + return GetAllAssetKeys().Distinct(); + } + /// Reset the asset cache and reload the game's static assets. + /// Matches the asset keys to invalidate. /// This implementation is derived from . - public void Reset() + public void InvalidateCache(Func predicate) { - this.Monitor.Log("Resetting asset cache...", LogLevel.Trace); - this.Cache.Clear(); - - // from Game1.LoadContent - Game1.daybg = this.Load("LooseSprites\\daybg"); - Game1.nightbg = this.Load("LooseSprites\\nightbg"); - Game1.menuTexture = this.Load("Maps\\MenuTiles"); - Game1.lantern = this.Load("LooseSprites\\Lighting\\lantern"); - Game1.windowLight = this.Load("LooseSprites\\Lighting\\windowLight"); - Game1.sconceLight = this.Load("LooseSprites\\Lighting\\sconceLight"); - Game1.cauldronLight = this.Load("LooseSprites\\Lighting\\greenLight"); - Game1.indoorWindowLight = this.Load("LooseSprites\\Lighting\\indoorWindowLight"); - Game1.shadowTexture = this.Load("LooseSprites\\shadow"); - Game1.mouseCursors = this.Load("LooseSprites\\Cursors"); - Game1.controllerMaps = this.Load("LooseSprites\\ControllerMaps"); - Game1.animations = this.Load("TileSheets\\animations"); - Game1.achievements = this.Load>("Data\\Achievements"); - Game1.NPCGiftTastes = this.Load>("Data\\NPCGiftTastes"); - Game1.dialogueFont = this.Load("Fonts\\SpriteFont1"); - Game1.smallFont = this.Load("Fonts\\SmallFont"); - Game1.tinyFont = this.Load("Fonts\\tinyFont"); - Game1.tinyFontBorder = this.Load("Fonts\\tinyFontBorder"); - Game1.objectSpriteSheet = this.Load("Maps\\springobjects"); - Game1.cropSpriteSheet = this.Load("TileSheets\\crops"); - Game1.emoteSpriteSheet = this.Load("TileSheets\\emotes"); - Game1.debrisSpriteSheet = this.Load("TileSheets\\debris"); - Game1.bigCraftableSpriteSheet = this.Load("TileSheets\\Craftables"); - Game1.rainTexture = this.Load("TileSheets\\rain"); - Game1.buffsIcons = this.Load("TileSheets\\BuffsIcons"); - Game1.objectInformation = this.Load>("Data\\ObjectInformation"); - Game1.bigCraftablesInformation = this.Load>("Data\\BigCraftablesInformation"); - FarmerRenderer.hairStylesTexture = this.Load("Characters\\Farmer\\hairstyles"); - FarmerRenderer.shirtsTexture = this.Load("Characters\\Farmer\\shirts"); - FarmerRenderer.hatsTexture = this.Load("Characters\\Farmer\\hats"); - FarmerRenderer.accessoriesTexture = this.Load("Characters\\Farmer\\accessories"); - Furniture.furnitureTexture = this.Load("TileSheets\\furniture"); - SpriteText.spriteTexture = this.Load("LooseSprites\\font_bold"); - SpriteText.coloredTexture = this.Load("LooseSprites\\font_colored"); - Tool.weaponsTexture = this.Load("TileSheets\\weapons"); - Projectile.projectileSheet = this.Load("TileSheets\\Projectiles"); - - // from Farmer constructor - if (Game1.player != null) - Game1.player.FarmerRenderer = new FarmerRenderer(this.Load("Characters\\Farmer\\farmer_" + (Game1.player.isMale ? "" : "girl_") + "base")); + // find matching asset keys + HashSet purgeCacheKeys = new HashSet(StringComparer.InvariantCultureIgnoreCase); + HashSet purgeAssetKeys = new HashSet(StringComparer.InvariantCultureIgnoreCase); + foreach (string cacheKey in this.Cache.Keys) + { + this.ParseCacheKey(cacheKey, out string assetKey, out string localeCode); + if (predicate(assetKey)) + { + purgeAssetKeys.Add(assetKey); + purgeCacheKeys.Add(cacheKey); + } + } + + // purge from cache + foreach (string key in purgeCacheKeys) + this.Cache.Remove(key); + + // reload core game assets + int reloaded = 0; + foreach (string key in purgeAssetKeys) + { + if (this.CoreAssetSetters.TryGetValue(key, out Action reloadAsset)) + { + reloadAsset(); + reloaded++; + } + } + + this.Monitor.Log($"Invalidated {purgeCacheKeys.Count} cache entries for {purgeAssetKeys.Count} asset keys: {string.Join(", ", purgeCacheKeys.OrderBy(p => p, StringComparer.InvariantCultureIgnoreCase))}. Reloaded {reloaded} core assets.", LogLevel.Trace); } @@ -221,6 +266,33 @@ namespace StardewModdingAPI.Framework || this.Cache.ContainsKey($"{normalisedAssetName}.{this.GetKeyLocale.Invoke()}"); // translated asset } + /// Parse a cache key into its component parts. + /// The input cache key. + /// The original asset key. + /// The asset locale code (or null if not localised). + private void ParseCacheKey(string cacheKey, out string assetKey, out string localeCode) + { + // handle localised key + if (!string.IsNullOrWhiteSpace(cacheKey)) + { + int lastSepIndex = cacheKey.LastIndexOf(".", StringComparison.InvariantCulture); + if (lastSepIndex >= 0) + { + string suffix = cacheKey.Substring(lastSepIndex + 1, cacheKey.Length - lastSepIndex - 1); + if (this.KeyLocales.ContainsKey(suffix)) + { + assetKey = cacheKey.Substring(0, lastSepIndex); + localeCode = cacheKey.Substring(lastSepIndex + 1, cacheKey.Length - lastSepIndex - 1); + return; + } + } + } + + // handle simple key + assetKey = cacheKey; + localeCode = null; + } + /// Load the initial asset from the registered . /// The basic asset metadata. /// Returns the loaded asset metadata, or null if no loader matched. @@ -365,7 +437,8 @@ namespace StardewModdingAPI.Framework // can't know which assets are meant to be disposed. Here we remove current assets from // the cache, but don't dispose them to avoid crashing any code that still references // them. The garbage collector will eventually clean up any unused assets. - this.Reset(); + this.Monitor.Log("Content manager disposed, resetting cache.", LogLevel.Trace); + this.InvalidateCache(p => true); } } } diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index 50ab4e25..56c56431 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -796,7 +796,7 @@ namespace StardewModdingAPI if (this.DeprecationManager.IsVirtualMethodImplemented(mod.GetType(), typeof(Mod), nameof(Mod.Entry), new[] { typeof(object[]) })) deprecationWarnings.Add(() => this.DeprecationManager.Warn(metadata.DisplayName, $"{nameof(Mod)}.{nameof(Mod.Entry)}(object[]) instead of {nameof(Mod)}.{nameof(Mod.Entry)}({nameof(IModHelper)})", "1.0", DeprecationLevel.PendingRemoval)); #else - if (!this.DeprecationManager.IsVirtualMethodImplemented(mod.GetType(), typeof(Mod), nameof(Mod.Entry), new[] {typeof(IModHelper)})) + if (!this.DeprecationManager.IsVirtualMethodImplemented(mod.GetType(), typeof(Mod), nameof(Mod.Entry), new[] { typeof(IModHelper) })) this.Monitor.Log($"{metadata.DisplayName} doesn't implement Entry() and may not work correctly.", LogLevel.Error); #endif } @@ -812,19 +812,27 @@ namespace StardewModdingAPI { if (metadata.Mod.Helper.Content is ContentHelper helper) { + // TODO: optimise by only reloading assets the new editors/loaders can intercept helper.ObservableAssetEditors.CollectionChanged += (sender, e) => { if (e.NewItems.Count > 0) - this.ContentManager.Reset(); + { + this.Monitor.Log("Detected new asset editor, resetting cache...", LogLevel.Trace); + this.ContentManager.InvalidateCache(p => true); + } }; helper.ObservableAssetLoaders.CollectionChanged += (sender, e) => { if (e.NewItems.Count > 0) - this.ContentManager.Reset(); + { + this.Monitor.Log("Detected new asset loader, resetting cache...", LogLevel.Trace); + this.ContentManager.InvalidateCache(p => true); + } }; } } - this.ContentManager.Reset(); + this.Monitor.Log("Resetting cache to enable interception...", LogLevel.Trace); + this.ContentManager.InvalidateCache(p => true); } /// Reload translations for all mods. -- cgit