From b0d8b23c2c53ea3aafd60b0597a7562ac1708a42 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 5 Mar 2022 15:31:06 -0500 Subject: migrate more internal code to IAssetName (#766) --- src/SMAPI/Metadata/CoreAssetPropagator.cs | 34 +++++++++++++++---------------- 1 file changed, 16 insertions(+), 18 deletions(-) (limited to 'src/SMAPI/Metadata') diff --git a/src/SMAPI/Metadata/CoreAssetPropagator.cs b/src/SMAPI/Metadata/CoreAssetPropagator.cs index e7fac578..f645470e 100644 --- a/src/SMAPI/Metadata/CoreAssetPropagator.cs +++ b/src/SMAPI/Metadata/CoreAssetPropagator.cs @@ -45,8 +45,8 @@ namespace StardewModdingAPI.Metadata /// Whether to enable more aggressive memory optimizations. private readonly bool AggressiveMemoryOptimizations; - /// Normalizes an asset key to match the cache key and assert that it's valid. - private readonly Func AssertAndNormalizeAssetName; + /// Parse a raw asset name. + private readonly Func ParseAssetName; /// Optimized bucket categories for batch reloading assets. private enum AssetBucket @@ -71,15 +71,15 @@ namespace StardewModdingAPI.Metadata /// Writes messages to the console. /// Simplifies access to private code. /// Whether to enable more aggressive memory optimizations. - public CoreAssetPropagator(LocalizedContentManager mainContent, GameContentManagerForAssetPropagation disposableContent, IMonitor monitor, Reflector reflection, bool aggressiveMemoryOptimizations) + /// Parse a raw asset name. + public CoreAssetPropagator(LocalizedContentManager mainContent, GameContentManagerForAssetPropagation disposableContent, IMonitor monitor, Reflector reflection, bool aggressiveMemoryOptimizations, Func parseAssetName) { this.MainContentManager = mainContent; this.DisposableContentManager = disposableContent; this.Monitor = monitor; this.Reflection = reflection; this.AggressiveMemoryOptimizations = aggressiveMemoryOptimizations; - - this.AssertAndNormalizeAssetName = disposableContent.AssertAndNormalizeAssetName; + this.ParseAssetName = parseAssetName; } /// Reload one of the game's core assets (if applicable). @@ -955,13 +955,12 @@ namespace StardewModdingAPI.Metadata private void ReloadNpcSprites(IEnumerable keys, IDictionary propagated) { // get NPCs - IDictionary lookup = keys.ToDictionary(p => p.Name, StringComparer.OrdinalIgnoreCase); var characters = ( from npc in this.GetCharacters() - let key = this.NormalizeAssetNameIgnoringEmpty(npc.Sprite?.Texture?.Name) - where key != null && lookup.ContainsKey(key) - select new { Npc = npc, AssetName = lookup[key] } + let key = this.ParseAssetNameOrNull(npc.Sprite?.Texture?.Name) + where key != null && propagated.ContainsKey(key) + select new { Npc = npc, AssetName = key } ) .ToArray(); if (!characters.Any()) @@ -981,26 +980,25 @@ namespace StardewModdingAPI.Metadata private void ReloadNpcPortraits(IEnumerable keys, IDictionary propagated) { // get NPCs - IDictionary lookup = keys.ToDictionary(p => p.Name, StringComparer.OrdinalIgnoreCase); var characters = ( from npc in this.GetCharacters() where npc.isVillager() - let key = this.NormalizeAssetNameIgnoringEmpty(npc.Portrait?.Name) - where key != null && lookup.ContainsKey(key) - select new { Npc = npc, AssetName = lookup[key] } + let key = this.ParseAssetNameOrNull(npc.Portrait?.Name) + where key != null && propagated.ContainsKey(key) + select new { Npc = npc, AssetName = key } ) .ToList(); // special case: Gil is a private NPC field on the AdventureGuild class (only used for the portrait) { - string gilKey = this.NormalizeAssetNameIgnoringEmpty("Portraits/Gil"); - if (lookup.TryGetValue(gilKey, out IAssetName assetName)) + IAssetName gilKey = this.ParseAssetName("Portraits/Gil"); + if (propagated.ContainsKey(gilKey)) { GameLocation adventureGuild = Game1.getLocationFromName("AdventureGuild"); if (adventureGuild != null) - characters.Add(new { Npc = this.Reflection.GetField(adventureGuild, "Gil").GetValue(), AssetName = assetName }); + characters.Add(new { Npc = this.Reflection.GetField(adventureGuild, "Gil").GetValue(), AssetName = gilKey }); } } @@ -1234,12 +1232,12 @@ namespace StardewModdingAPI.Metadata /// Normalize an asset key to match the cache key and assert that it's valid, but don't raise an error for null or empty values. /// The asset key to normalize. - private string NormalizeAssetNameIgnoringEmpty(string path) + private IAssetName ParseAssetNameOrNull(string path) { if (string.IsNullOrWhiteSpace(path)) return null; - return this.AssertAndNormalizeAssetName(path); + return this.ParseAssetName(path); } /// Get the segments in a path (e.g. 'a/b' is 'a' and 'b'). -- cgit