From 2b12bb32f67422a7d25eb7e2d8ea8e1c3e335708 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 17 May 2019 19:41:26 -0400 Subject: batch reload assets in some cases --- src/SMAPI/Framework/ContentCoordinator.cs | 9 +- src/SMAPI/Metadata/CoreAssetPropagator.cs | 263 ++++++++++++++++++++---------- 2 files changed, 182 insertions(+), 90 deletions(-) (limited to 'src') diff --git a/src/SMAPI/Framework/ContentCoordinator.cs b/src/SMAPI/Framework/ContentCoordinator.cs index 25eeb2ef..15f1c163 100644 --- a/src/SMAPI/Framework/ContentCoordinator.cs +++ b/src/SMAPI/Framework/ContentCoordinator.cs @@ -258,14 +258,7 @@ namespace StardewModdingAPI.Framework } // reload core game assets - int reloaded = 0; - foreach (var pair in removedAssetNames) - { - string key = pair.Key; - Type type = pair.Value; - if (this.CoreAssets.Propagate(this.MainContentManager, key, type)) // use an intercepted content manager - reloaded++; - } + int reloaded = this.CoreAssets.Propagate(this.MainContentManager, removedAssetNames); // use an intercepted content manager // report result if (removedAssetNames.Any()) diff --git a/src/SMAPI/Metadata/CoreAssetPropagator.cs b/src/SMAPI/Metadata/CoreAssetPropagator.cs index c4086712..dbb27b14 100644 --- a/src/SMAPI/Metadata/CoreAssetPropagator.cs +++ b/src/SMAPI/Metadata/CoreAssetPropagator.cs @@ -34,6 +34,19 @@ namespace StardewModdingAPI.Metadata /// Encapsulates monitoring and logging. private readonly IMonitor Monitor; + /// Optimised bucket categories for batch reloading assets. + private enum AssetBucket + { + /// NPC overworld sprites. + Sprite, + + /// Villager dialogue portraits. + Portrait, + + /// Any other asset. + Other + }; + /********* ** Public methods @@ -51,15 +64,42 @@ namespace StardewModdingAPI.Metadata /// Reload one of the game's core assets (if applicable). /// The content manager through which to reload the asset. - /// The asset key to reload. - /// The asset type to reload. - /// Returns whether an asset was reloaded. - public bool Propagate(LocalizedContentManager content, string key, Type type) + /// The asset keys and types to reload. + /// Returns the number of reloaded assets. + public int Propagate(LocalizedContentManager content, IDictionary assets) { - object result = this.PropagateImpl(content, key, type); - if (result is bool b) - return b; - return result != null; + // group into optimised lists + var buckets = assets.GroupBy(p => + { + if (this.IsInFolder(p.Key, "Characters") || this.IsInFolder(p.Key, "Characters\\Monsters")) + return AssetBucket.Sprite; + + if (this.IsInFolder(p.Key, "Portraits")) + return AssetBucket.Portrait; + + return AssetBucket.Other; + }); + + // reload assets + int reloaded = 0; + foreach (var bucket in buckets) + { + switch (bucket.Key) + { + case AssetBucket.Sprite: + reloaded += this.ReloadNpcSprites(content, bucket.Select(p => p.Key)); + break; + + case AssetBucket.Portrait: + reloaded += this.ReloadNpcPortraits(content, bucket.Select(p => p.Key)); + break; + + default: + reloaded += bucket.Count(p => this.PropagateOther(content, p.Key, p.Value)); + break; + } + } + return reloaded; } @@ -71,7 +111,7 @@ namespace StardewModdingAPI.Metadata /// The asset key to reload. /// The asset type to reload. /// Returns whether an asset was loaded. The return value may be true or false, or a non-null value for true. - private object PropagateImpl(LocalizedContentManager content, string key, Type type) + private bool PropagateOther(LocalizedContentManager content, string key, Type type) { key = this.GetNormalisedPath(key); @@ -147,147 +187,185 @@ namespace StardewModdingAPI.Metadata ** Content\Characters\Farmer ****/ case "characters\\farmer\\accessories": // Game1.loadContent - return FarmerRenderer.accessoriesTexture = content.Load(key); + FarmerRenderer.accessoriesTexture = content.Load(key); + return true; case "characters\\farmer\\farmer_base": // Farmer if (Game1.player == null || !Game1.player.IsMale) return false; - return Game1.player.FarmerRenderer = new FarmerRenderer(key, Game1.player); + Game1.player.FarmerRenderer = new FarmerRenderer(key, Game1.player); + return true; case "characters\\farmer\\farmer_girl_base": // Farmer if (Game1.player == null || Game1.player.IsMale) return false; - return Game1.player.FarmerRenderer = new FarmerRenderer(key, Game1.player); + Game1.player.FarmerRenderer = new FarmerRenderer(key, Game1.player); + return true; case "characters\\farmer\\hairstyles": // Game1.loadContent - return FarmerRenderer.hairStylesTexture = content.Load(key); + FarmerRenderer.hairStylesTexture = content.Load(key); + return true; case "characters\\farmer\\hats": // Game1.loadContent - return FarmerRenderer.hatsTexture = content.Load(key); + FarmerRenderer.hatsTexture = content.Load(key); + return true; case "characters\\farmer\\shirts": // Game1.loadContent - return FarmerRenderer.shirtsTexture = content.Load(key); + FarmerRenderer.shirtsTexture = content.Load(key); + return true; /**** ** Content\Data ****/ case "data\\achievements": // Game1.loadContent - return Game1.achievements = content.Load>(key); + Game1.achievements = content.Load>(key); + return true; case "data\\bigcraftablesinformation": // Game1.loadContent - return Game1.bigCraftablesInformation = content.Load>(key); + Game1.bigCraftablesInformation = content.Load>(key); + return true; case "data\\cookingrecipes": // CraftingRecipe.InitShared - return CraftingRecipe.cookingRecipes = content.Load>(key); + CraftingRecipe.cookingRecipes = content.Load>(key); + return true; case "data\\craftingrecipes": // CraftingRecipe.InitShared - return CraftingRecipe.craftingRecipes = content.Load>(key); + CraftingRecipe.craftingRecipes = content.Load>(key); + return true; case "data\\npcdispositions": // NPC constructor return this.ReloadNpcDispositions(content, key); case "data\\npcgifttastes": // Game1.loadContent - return Game1.NPCGiftTastes = content.Load>(key); + Game1.NPCGiftTastes = content.Load>(key); + return true; case "data\\objectinformation": // Game1.loadContent - return Game1.objectInformation = content.Load>(key); + Game1.objectInformation = content.Load>(key); + return true; /**** ** Content\Fonts ****/ case "fonts\\spritefont1": // Game1.loadContent - return Game1.dialogueFont = content.Load(key); + Game1.dialogueFont = content.Load(key); + return true; case "fonts\\smallfont": // Game1.loadContent - return Game1.smallFont = content.Load(key); + Game1.smallFont = content.Load(key); + return true; case "fonts\\tinyfont": // Game1.loadContent - return Game1.tinyFont = content.Load(key); + Game1.tinyFont = content.Load(key); + return true; case "fonts\\tinyfontborder": // Game1.loadContent - return Game1.tinyFontBorder = content.Load(key); + Game1.tinyFontBorder = content.Load(key); + return true; /**** ** Content\Lighting ****/ case "loosesprites\\lighting\\greenlight": // Game1.loadContent - return Game1.cauldronLight = content.Load(key); + Game1.cauldronLight = content.Load(key); + return true; case "loosesprites\\lighting\\indoorwindowlight": // Game1.loadContent - return Game1.indoorWindowLight = content.Load(key); + Game1.indoorWindowLight = content.Load(key); + return true; case "loosesprites\\lighting\\lantern": // Game1.loadContent - return Game1.lantern = content.Load(key); + Game1.lantern = content.Load(key); + return true; case "loosesprites\\lighting\\sconcelight": // Game1.loadContent - return Game1.sconceLight = content.Load(key); + Game1.sconceLight = content.Load(key); + return true; case "loosesprites\\lighting\\windowlight": // Game1.loadContent - return Game1.windowLight = content.Load(key); + Game1.windowLight = content.Load(key); + return true; /**** ** Content\LooseSprites ****/ case "loosesprites\\controllermaps": // Game1.loadContent - return Game1.controllerMaps = content.Load(key); + Game1.controllerMaps = content.Load(key); + return true; case "loosesprites\\cursors": // Game1.loadContent - return Game1.mouseCursors = content.Load(key); + Game1.mouseCursors = content.Load(key); + return true; case "loosesprites\\daybg": // Game1.loadContent - return Game1.daybg = content.Load(key); + Game1.daybg = content.Load(key); + return true; case "loosesprites\\font_bold": // Game1.loadContent - return SpriteText.spriteTexture = content.Load(key); + SpriteText.spriteTexture = content.Load(key); + return true; case "loosesprites\\font_colored": // Game1.loadContent - return SpriteText.coloredTexture = content.Load(key); + SpriteText.coloredTexture = content.Load(key); + return true; case "loosesprites\\nightbg": // Game1.loadContent - return Game1.nightbg = content.Load(key); + Game1.nightbg = content.Load(key); + return true; case "loosesprites\\shadow": // Game1.loadContent - return Game1.shadowTexture = content.Load(key); + Game1.shadowTexture = content.Load(key); + return true; /**** ** Content\Critters ****/ case "tilesheets\\crops": // Game1.loadContent - return Game1.cropSpriteSheet = content.Load(key); + Game1.cropSpriteSheet = content.Load(key); + return true; case "tilesheets\\debris": // Game1.loadContent - return Game1.debrisSpriteSheet = content.Load(key); + Game1.debrisSpriteSheet = content.Load(key); + return true; case "tilesheets\\emotes": // Game1.loadContent - return Game1.emoteSpriteSheet = content.Load(key); + Game1.emoteSpriteSheet = content.Load(key); + return true; case "tilesheets\\furniture": // Game1.loadContent - return Furniture.furnitureTexture = content.Load(key); + Furniture.furnitureTexture = content.Load(key); + return true; case "tilesheets\\projectiles": // Game1.loadContent - return Projectile.projectileSheet = content.Load(key); + Projectile.projectileSheet = content.Load(key); + return true; case "tilesheets\\rain": // Game1.loadContent - return Game1.rainTexture = content.Load(key); + Game1.rainTexture = content.Load(key); + return true; case "tilesheets\\tools": // Game1.ResetToolSpriteSheet Game1.ResetToolSpriteSheet(); return true; case "tilesheets\\weapons": // Game1.loadContent - return Tool.weaponsTexture = content.Load(key); + Tool.weaponsTexture = content.Load(key); + return true; /**** ** Content\Maps ****/ case "maps\\menutiles": // Game1.loadContent - return Game1.menuTexture = content.Load(key); + Game1.menuTexture = content.Load(key); + return true; case "maps\\springobjects": // Game1.loadContent - return Game1.objectSpriteSheet = content.Load(key); + Game1.objectSpriteSheet = content.Load(key); + return true; case "maps\\walls_and_floors": // Wallpaper - return Wallpaper.wallpaperTexture = content.Load(key); + Wallpaper.wallpaperTexture = content.Load(key); + return true; /**** ** Content\Minigames @@ -315,35 +393,43 @@ namespace StardewModdingAPI.Metadata ** Content\TileSheets ****/ case "tilesheets\\animations": // Game1.loadContent - return Game1.animations = content.Load(key); + Game1.animations = content.Load(key); + return true; case "tilesheets\\buffsicons": // Game1.loadContent - return Game1.buffsIcons = content.Load(key); + Game1.buffsIcons = content.Load(key); + return true; case "tilesheets\\bushes": // new Bush() reflection.GetField>(typeof(Bush), "texture").SetValue(new Lazy(() => content.Load(key))); return true; case "tilesheets\\craftables": // Game1.loadContent - return Game1.bigCraftableSpriteSheet = content.Load(key); + Game1.bigCraftableSpriteSheet = content.Load(key); + return true; case "tilesheets\\fruittrees": // FruitTree - return FruitTree.texture = content.Load(key); + FruitTree.texture = content.Load(key); + return true; /**** ** Content\TerrainFeatures ****/ case "terrainfeatures\\flooring": // Flooring - return Flooring.floorsTexture = content.Load(key); + Flooring.floorsTexture = content.Load(key); + return true; case "terrainfeatures\\hoedirt": // from HoeDirt - return HoeDirt.lightTexture = content.Load(key); + HoeDirt.lightTexture = content.Load(key); + return true; case "terrainfeatures\\hoedirtdark": // from HoeDirt - return HoeDirt.darkTexture = content.Load(key); + HoeDirt.darkTexture = content.Load(key); + return true; case "terrainfeatures\\hoedirtsnow": // from HoeDirt - return HoeDirt.snowTexture = content.Load(key); + HoeDirt.snowTexture = content.Load(key); + return true; case "terrainfeatures\\mushroom_tree": // from Tree return this.ReloadTreeTextures(content, key, Tree.mushroomTree); @@ -376,15 +462,9 @@ namespace StardewModdingAPI.Metadata if (this.IsInFolder(key, "Buildings")) return this.ReloadBuildings(content, key); - if (this.IsInFolder(key, "Characters") || this.IsInFolder(key, "Characters\\Monsters")) - return this.ReloadNpcSprites(content, key); - if (this.KeyStartsWith(key, "LooseSprites\\Fence")) return this.ReloadFenceTextures(key); - if (this.IsInFolder(key, "Portraits")) - return this.ReloadNpcPortraits(content, key); - // dynamic data if (this.IsInFolder(key, "Characters\\Dialogue")) return this.ReloadNpcDialogue(key); @@ -536,46 +616,65 @@ namespace StardewModdingAPI.Metadata /// Reload the sprites for matching NPCs. /// The content manager through which to reload the asset. - /// The asset key to reload. - /// Returns whether any textures were reloaded. - private bool ReloadNpcSprites(LocalizedContentManager content, string key) + /// The asset keys to reload. + /// Returns the number of reloaded assets. + private int ReloadNpcSprites(LocalizedContentManager content, IEnumerable keys) { // get NPCs + HashSet lookup = new HashSet(keys, StringComparer.InvariantCultureIgnoreCase); NPC[] characters = this.GetCharacters() - .Where(npc => npc.Sprite != null && this.GetNormalisedPath(npc.Sprite.textureName.Value) == key) + .Where(npc => npc.Sprite != null && lookup.Contains(this.GetNormalisedPath(npc.Sprite.textureName.Value))) .ToArray(); if (!characters.Any()) - return false; + return 0; - // update portrait - Texture2D texture = content.Load(key); - foreach (NPC character in characters) - this.SetSpriteTexture(character.Sprite, texture); - return true; + // update sprite + int reloaded = 0; + foreach (NPC npc in characters) + { + this.SetSpriteTexture(npc.Sprite, content.Load(npc.Sprite.textureName.Value)); + reloaded++; + } + + return reloaded; } /// Reload the portraits for matching NPCs. /// The content manager through which to reload the asset. - /// The asset key to reload. - /// Returns whether any textures were reloaded. - private bool ReloadNpcPortraits(LocalizedContentManager content, string key) + /// The asset key to reload. + /// Returns the number of reloaded assets. + private int ReloadNpcPortraits(LocalizedContentManager content, IEnumerable keys) { // get NPCs - NPC[] villagers = this.GetCharacters() - .Where(npc => npc.isVillager() && this.GetNormalisedPath($"Portraits\\{this.Reflection.GetMethod(npc, "getTextureName").Invoke()}") == key) + HashSet lookup = new HashSet(keys, StringComparer.InvariantCultureIgnoreCase); + var villagers = + ( + from npc in this.GetCharacters() + where npc.isVillager() + let textureKey = this.GetNormalisedPath($"Portraits\\{this.getTextureName(npc)}") + where lookup.Contains(textureKey) + select new { npc, textureKey } + ) .ToArray(); if (!villagers.Any()) - return false; + return 0; // update portrait - Texture2D texture = content.Load(key); - foreach (NPC villager in villagers) + int reloaded = 0; + foreach (var entry in villagers) { - villager.resetPortrait(); - villager.Portrait = texture; + entry.npc.resetPortrait(); + entry.npc.Portrait = content.Load(entry.textureKey); + reloaded++; } + return reloaded; + } - return true; + private string getTextureName(NPC npc) + { + string name = npc.Name; + string str = name == "Old Mariner" ? "Mariner" : (name == "Dwarf King" ? "DwarfKing" : (name == "Mister Qi" ? "MrQi" : (name == "???" ? "Monsters\\Shadow Guy" : name))); + return str; } /// Reload tree textures. -- cgit