From 5a0e49827be92d19dfdda7bb15ca15fa8f269ecb Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 25 Mar 2018 00:52:37 -0400 Subject: update fence textures when changed through the content API (#459) --- docs/release-notes.md | 3 ++- src/SMAPI/Metadata/CoreAssetPropagator.cs | 44 ++++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/docs/release-notes.md b/docs/release-notes.md index e0e209a1..2a134d13 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -17,9 +17,10 @@ ## 2.5.4 * For players: - * Fixed NPC and tree textures not updated when changed through the content API. + * Fixed fence, NPC, and tree textures not updated when a mod changes them through the content API. * Fixed visual bug on Linux/Mac when mods overlay images through the content API. * Fixed error when a mod removes an asset editor/loader. + * Fixed minimum game version incorrectly changed from 1.2.30 to 1.2.33 in SMAPI 2.5.3. * For the [log parser][]: * Fixed error when log text contains certain tokens. diff --git a/src/SMAPI/Metadata/CoreAssetPropagator.cs b/src/SMAPI/Metadata/CoreAssetPropagator.cs index 4a1d6097..1702ee26 100644 --- a/src/SMAPI/Metadata/CoreAssetPropagator.cs +++ b/src/SMAPI/Metadata/CoreAssetPropagator.cs @@ -333,6 +333,9 @@ namespace StardewModdingAPI.Metadata if (key.StartsWith(this.GetNormalisedPath("Characters\\Monsters\\"))) return this.ReloadNpcSprites(content, key, monster: true); + if (key.StartsWith(this.GetNormalisedPath("LooseSprites\\Fence"))) + return this.ReloadFenceTextures(content, key); + if (key.StartsWith(this.GetNormalisedPath("Portraits\\"))) return this.ReloadNpcPortraits(content, key); @@ -372,6 +375,34 @@ namespace StardewModdingAPI.Metadata return false; } + /// Reload the sprites for a fence type. + /// The content manager through which to reload the asset. + /// The asset key to reload. + /// Returns whether any textures were reloaded. + private bool ReloadFenceTextures(LocalizedContentManager content, string key) + { + // get fence type + if (!int.TryParse(this.GetSegments(key)[1].Substring("Fence".Length), out int fenceType)) + return false; + + // get fences + Fence[] fences = + ( + from location in this.GetLocations() + from fence in location.Objects.Values.OfType() + where fenceType == 1 + ? fence.isGate + : fence.whichType == fenceType + select fence + ) + .ToArray(); + + // update fence textures + foreach (Fence fence in fences) + fence.reloadSprite(); + return true; + } + /// Reload the sprites for matching NPCs. /// The content manager through which to reload the asset. /// The asset key to reload. @@ -490,13 +521,20 @@ namespace StardewModdingAPI.Metadata } } + /// Get the segments in a path (e.g. 'a/b' is 'a' and 'b'). + /// The path to check. + private string[] GetSegments(string path) + { + if (path == null) + return new string[0]; + return path.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + } + /// Count the number of segments in a path (e.g. 'a/b' is 2). /// The path to check. private int CountSegments(string path) { - if (path == null) - return 0; - return path.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar).Length; + return this.GetSegments(path).Length; } } } -- cgit