From 51368b8afb0c2064a70ed09f41570ca8fac5fdfa Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 23 Mar 2018 20:18:23 -0400 Subject: update tree textures when changeed through the content API (#459) --- src/SMAPI/Metadata/CoreAssetPropagator.cs | 120 ++++++++++++++++++++++-------- 1 file changed, 88 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/SMAPI/Metadata/CoreAssetPropagator.cs b/src/SMAPI/Metadata/CoreAssetPropagator.cs index 85021727..21aaeb6c 100644 --- a/src/SMAPI/Metadata/CoreAssetPropagator.cs +++ b/src/SMAPI/Metadata/CoreAssetPropagator.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using Microsoft.Xna.Framework.Graphics; using StardewModdingAPI.Framework.Reflection; @@ -45,7 +46,10 @@ namespace StardewModdingAPI.Metadata /// Returns whether an asset was reloaded. public bool Propagate(LocalizedContentManager content, string key) { - return this.PropagateImpl(content, key) != null; + object result = this.PropagateImpl(content, key); + if (result is bool b) + return b; + return result != null; } @@ -55,7 +59,7 @@ 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. - /// Returns any non-null value to indicate an asset was loaded.. + /// Returns any non-null value to indicate an asset was loaded. private object PropagateImpl(LocalizedContentManager content, string key) { Reflector reflection = this.Reflection; @@ -72,7 +76,7 @@ namespace StardewModdingAPI.Metadata { Farm farm = Game1.getFarm(); if (farm == null) - return null; + return false; return farm.houseTextures = content.Load(key); } #endif @@ -85,7 +89,7 @@ namespace StardewModdingAPI.Metadata case "characters\\farmer\\farmer_base": // Farmer if (Game1.player == null || !Game1.player.isMale) - return null; + return false; #if STARDEW_VALLEY_1_3 return Game1.player.FarmerRenderer = new FarmerRenderer(key); #else @@ -94,7 +98,7 @@ namespace StardewModdingAPI.Metadata case "characters\\farmer\\farmer_girl_base": // Farmer if (Game1.player == null || Game1.player.isMale) - return null; + return false; #if STARDEW_VALLEY_1_3 return Game1.player.FarmerRenderer = new FarmerRenderer(key); #else @@ -240,8 +244,7 @@ namespace StardewModdingAPI.Metadata reflection.GetField(Game1.activeClickableMenu, "cloudsTexture").SetValue(content.Load(key)); return true; } - - return null; + return false; case "minigames\\titlebuttons": // TitleMenu if (Game1.activeClickableMenu is TitleMenu titleMenu) @@ -256,8 +259,7 @@ namespace StardewModdingAPI.Metadata #endif return true; } - - return null; + return false; /**** ** Content\TileSheets @@ -291,48 +293,102 @@ namespace StardewModdingAPI.Metadata case "terrainfeatures\\hoedirt": // from HoeDirt return HoeDirt.lightTexture = content.Load(key); - case "Terrainfeatures\\hoedirtdark": // from HoeDirt + case "terrainfeatures\\hoedirtdark": // from HoeDirt return HoeDirt.darkTexture = content.Load(key); - case "Terrainfeatures\\hoedirtsnow": // from HoeDirt + case "terrainfeatures\\hoedirtsnow": // from HoeDirt return HoeDirt.snowTexture = content.Load(key); + + case "terrainfeatures\\mushroom_tree": // from Tree + return this.ReloadTreeTextures(content, key, Tree.mushroomTree); + + case "terrainfeatures\\tree_palm": // from Tree + return this.ReloadTreeTextures(content, key, Tree.palmTree); + + case "terrainfeatures\\tree1_fall": // from Tree + case "terrainfeatures\\tree1_spring": // from Tree + case "terrainfeatures\\tree1_summer": // from Tree + case "terrainfeatures\\tree1_winter": // from Tree + return this.ReloadTreeTextures(content, key, Tree.bushyTree); + + case "terrainfeatures\\tree2_fall": // from Tree + case "terrainfeatures\\tree2_spring": // from Tree + case "terrainfeatures\\tree2_summer": // from Tree + case "terrainfeatures\\tree2_winter": // from Tree + return this.ReloadTreeTextures(content, key, Tree.leafyTree); + + case "terrainfeatures\\tree3_fall": // from Tree + case "terrainfeatures\\tree3_spring": // from Tree + case "terrainfeatures\\tree3_winter": // from Tree + return this.ReloadTreeTextures(content, key, Tree.pineTree); } // building textures if (key.StartsWith(this.GetNormalisedPath("Buildings\\"), StringComparison.InvariantCultureIgnoreCase)) { - Building[] buildings = this.GetAllBuildings().Where(p => key.Equals(this.GetNormalisedPath($"Buildings\\{p.buildingType?.ToLower()}"), StringComparison.InvariantCultureIgnoreCase)).ToArray(); - if (buildings.Any()) - { -#if STARDEW_VALLEY_1_3 - foreach (Building building in buildings) - building.texture = new Lazy(() => content.Load(key)); -#else - Texture2D texture = content.Load(key); - foreach (Building building in buildings) - building.texture = texture; -#endif - - return true; - } - return null; + string type = Path.GetFileName(key); + return this.ReloadBuildings(content, key, type); } - return null; + return false; } /********* ** Private methods *********/ - /// Get all player-constructed buildings in the world. - private IEnumerable GetAllBuildings() + /// Reload building textures. + /// The content manager through which to reload the asset. + /// The asset key to reload. + /// The type to reload. + /// Returns whether any textures were reloaded. + private bool ReloadBuildings(LocalizedContentManager content, string key, string type) + { + Building[] buildings = Game1.locations + .OfType() + .SelectMany(p => p.buildings) + .Where(p => p.buildingType == type) + .ToArray(); + + if (buildings.Any()) + { + Lazy texture = new Lazy(() => content.Load(key)); + foreach (Building building in buildings) +#if STARDEW_VALLEY_1_3 + building.texture = texture; +#else + building.texture = texture.Value; +#endif + return true; + } + return false; + } + + /// Reload tree textures. + /// The content manager through which to reload the asset. + /// The asset key to reload. + /// The type to reload. + /// Returns whether any textures were reloaded. + private bool ReloadTreeTextures(LocalizedContentManager content, string key, int type) { - foreach (BuildableGameLocation location in Game1.locations.OfType()) + Tree[] trees = Game1.locations + .SelectMany(p => p.terrainFeatures.Values.OfType()) + .Where(tree => tree.treeType == type) + .ToArray(); + + if (trees.Any()) { - foreach (Building building in location.buildings) - yield return building; + Lazy texture = new Lazy(() => content.Load(key)); + foreach (Tree tree in trees) +#if STARDEW_VALLEY_1_3 + this.Reflection.GetField>(tree, "texture").SetValue(texture); +#else + this.Reflection.GetField(tree, "texture").SetValue(texture.Value); +#endif + return true; } + + return false; } } } -- cgit