From 4b3a593941bf9880404171c4bef9a3b3f60b6dca Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 7 Sep 2019 16:38:17 -0400 Subject: track texture asset keys, fix NPC portrait propagation --- .../ContentManagers/BaseContentManager.cs | 6 +++++ src/SMAPI/Metadata/CoreAssetPropagator.cs | 28 +++++++--------------- 2 files changed, 14 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs index de39dbae..5283340e 100644 --- a/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs +++ b/src/SMAPI/Framework/ContentManagers/BaseContentManager.cs @@ -6,6 +6,7 @@ using System.Globalization; using System.IO; using System.Linq; using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; using StardewModdingAPI.Framework.Content; using StardewModdingAPI.Framework.Exceptions; using StardewModdingAPI.Framework.Reflection; @@ -264,6 +265,11 @@ namespace StardewModdingAPI.Framework.ContentManagers /// The language code for which to inject the asset. protected virtual void Inject(string assetName, T value, LanguageCode language) { + // track asset key + if (value is Texture2D texture) + texture.Name = assetName; + + // cache asset assetName = this.AssertAndNormalizeAssetName(assetName); this.Cache[assetName] = value; } diff --git a/src/SMAPI/Metadata/CoreAssetPropagator.cs b/src/SMAPI/Metadata/CoreAssetPropagator.cs index 66e6ed14..c0d57f4b 100644 --- a/src/SMAPI/Metadata/CoreAssetPropagator.cs +++ b/src/SMAPI/Metadata/CoreAssetPropagator.cs @@ -507,6 +507,7 @@ namespace StardewModdingAPI.Metadata // find matches TAnimal[] animals = this.GetCharacters() .OfType() + .Where(p => key == this.GetNormalizedPath(p.Sprite?.Texture?.Name)) .ToArray(); if (!animals.Any()) return false; @@ -587,7 +588,7 @@ namespace StardewModdingAPI.Metadata let locCritters = this.Reflection.GetField>(location, "critters").GetValue() where locCritters != null from Critter critter in locCritters - where this.GetNormalizedPath(critter.sprite.textureName) == key + where this.GetNormalizedPath(critter.sprite?.Texture?.Name) == key select critter ) .ToArray(); @@ -673,7 +674,7 @@ namespace StardewModdingAPI.Metadata // get NPCs HashSet lookup = new HashSet(keys, StringComparer.InvariantCultureIgnoreCase); NPC[] characters = this.GetCharacters() - .Where(npc => npc.Sprite != null && lookup.Contains(this.GetNormalizedPath(npc.Sprite.textureName.Value))) + .Where(npc => npc.Sprite != null && lookup.Contains(this.GetNormalizedPath(npc.Sprite?.Texture?.Name))) .ToArray(); if (!characters.Any()) return 0; @@ -697,36 +698,23 @@ namespace StardewModdingAPI.Metadata { // get NPCs HashSet lookup = new HashSet(keys, StringComparer.InvariantCultureIgnoreCase); - var villagers = - ( - from npc in this.GetCharacters() - where npc.isVillager() - let textureKey = this.GetNormalizedPath($"Portraits\\{this.getTextureName(npc)}") - where lookup.Contains(textureKey) - select new { npc, textureKey } - ) + var villagers = this + .GetCharacters() + .Where(npc => npc.isVillager() && lookup.Contains(this.GetNormalizedPath(npc.Portrait?.Name))) .ToArray(); if (!villagers.Any()) return 0; // update portrait int reloaded = 0; - foreach (var entry in villagers) + foreach (NPC npc in villagers) { - entry.npc.resetPortrait(); - entry.npc.Portrait = content.Load(entry.textureKey); + npc.Portrait = content.Load(npc.Portrait.Name); reloaded++; } return reloaded; } - 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. /// The content manager through which to reload the asset. /// The asset key to reload. -- cgit