From eeee6b185d5438e5d44ed0da7c23cf19813b29ea Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 23 Jul 2017 19:39:17 -0400 Subject: use more flexible approach to core asset reloading (#335) --- src/StardewModdingAPI/Metadata/CoreAssets.cs | 107 +++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 src/StardewModdingAPI/Metadata/CoreAssets.cs (limited to 'src/StardewModdingAPI/Metadata') diff --git a/src/StardewModdingAPI/Metadata/CoreAssets.cs b/src/StardewModdingAPI/Metadata/CoreAssets.cs new file mode 100644 index 00000000..256a911a --- /dev/null +++ b/src/StardewModdingAPI/Metadata/CoreAssets.cs @@ -0,0 +1,107 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Xna.Framework.Graphics; +using StardewModdingAPI.Framework; +using StardewValley; +using StardewValley.BellsAndWhistles; +using StardewValley.Objects; +using StardewValley.Projectiles; + +namespace StardewModdingAPI.Metadata +{ + /// Provides metadata about core assets in the game. + internal class CoreAssets + { + /********* + ** Properties + *********/ + /// Normalises an asset key to match the cache key. + protected readonly Func GetNormalisedPath; + + /// The static asset setters. + private readonly IDictionary> StaticSetters; + + + /********* + ** Public methods + *********/ + /// Initialise the core asset data. + /// Normalises an asset key to match the cache key. + public CoreAssets(Func getNormalisedPath) + { + this.GetNormalisedPath = getNormalisedPath; + this.StaticSetters = + new Dictionary> + { + // from Game1.loadContent + ["LooseSprites\\daybg"] = (content, key) => Game1.daybg = content.Load(key), + ["LooseSprites\\daybg"] = (content, key) => Game1.daybg = content.Load(key), + ["LooseSprites\\nightbg"] = (content, key) => Game1.nightbg = content.Load(key), + ["Maps\\MenuTiles"] = (content, key) => Game1.menuTexture = content.Load(key), + ["LooseSprites\\Lighting\\lantern"] = (content, key) => Game1.lantern = content.Load(key), + ["LooseSprites\\Lighting\\windowLight"] = (content, key) => Game1.windowLight = content.Load(key), + ["LooseSprites\\Lighting\\sconceLight"] = (content, key) => Game1.sconceLight = content.Load(key), + ["LooseSprites\\Lighting\\greenLight"] = (content, key) => Game1.cauldronLight = content.Load(key), + ["LooseSprites\\Lighting\\indoorWindowLight"] = (content, key) => Game1.indoorWindowLight = content.Load(key), + ["LooseSprites\\shadow"] = (content, key) => Game1.shadowTexture = content.Load(key), + ["LooseSprites\\Cursors"] = (content, key) => Game1.mouseCursors = content.Load(key), + ["LooseSprites\\ControllerMaps"] = (content, key) => Game1.controllerMaps = content.Load(key), + ["TileSheets\\animations"] = (content, key) => Game1.animations = content.Load(key), + ["Data\\Achievements"] = (content, key) => Game1.achievements = content.Load>(key), + ["Data\\NPCGiftTastes"] = (content, key) => Game1.NPCGiftTastes = content.Load>(key), + ["Fonts\\SpriteFont1"] = (content, key) => Game1.dialogueFont = content.Load(key), + ["Fonts\\SmallFont"] = (content, key) => Game1.smallFont = content.Load(key), + ["Fonts\\tinyFont"] = (content, key) => Game1.tinyFont = content.Load(key), + ["Fonts\\tinyFontBorder"] = (content, key) => Game1.tinyFontBorder = content.Load(key), + ["Maps\\springobjects"] = (content, key) => Game1.objectSpriteSheet = content.Load(key), + ["TileSheets\\crops"] = (content, key) => Game1.cropSpriteSheet = content.Load(key), + ["TileSheets\\emotes"] = (content, key) => Game1.emoteSpriteSheet = content.Load(key), + ["TileSheets\\debris"] = (content, key) => Game1.debrisSpriteSheet = content.Load(key), + ["TileSheets\\Craftables"] = (content, key) => Game1.bigCraftableSpriteSheet = content.Load(key), + ["TileSheets\\rain"] = (content, key) => Game1.rainTexture = content.Load(key), + ["TileSheets\\BuffsIcons"] = (content, key) => Game1.buffsIcons = content.Load(key), + ["Data\\ObjectInformation"] = (content, key) => Game1.objectInformation = content.Load>(key), + ["Data\\BigCraftablesInformation"] = (content, key) => Game1.bigCraftablesInformation = content.Load>(key), + ["Characters\\Farmer\\hairstyles"] = (content, key) => FarmerRenderer.hairStylesTexture = content.Load(key), + ["Characters\\Farmer\\shirts"] = (content, key) => FarmerRenderer.shirtsTexture = content.Load(key), + ["Characters\\Farmer\\hats"] = (content, key) => FarmerRenderer.hatsTexture = content.Load(key), + ["Characters\\Farmer\\accessories"] = (content, key) => FarmerRenderer.accessoriesTexture = content.Load(key), + ["TileSheets\\furniture"] = (content, key) => Furniture.furnitureTexture = content.Load(key), + ["LooseSprites\\font_bold"] = (content, key) => SpriteText.spriteTexture = content.Load(key), + ["LooseSprites\\font_colored"] = (content, key) => SpriteText.coloredTexture = content.Load(key), + ["TileSheets\\weapons"] = (content, key) => Tool.weaponsTexture = content.Load(key), + ["TileSheets\\Projectiles"] = (content, key) => Projectile.projectileSheet = content.Load(key), + + // from Farmer constructor + ["Characters\\Farmer\\farmer_base"] = (content, key) => + { + if (Game1.player != null && Game1.player.isMale) + Game1.player.FarmerRenderer = new FarmerRenderer(content.Load(key)); + }, + ["Characters\\Farmer\\farmer_girl_base"] = (content, key) => + { + if (Game1.player != null && !Game1.player.isMale) + Game1.player.FarmerRenderer = new FarmerRenderer(content.Load(key)); + } + } + .ToDictionary(p => getNormalisedPath(p.Key), p => p.Value); + } + + /// 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 whether an asset was reloaded. + public bool ReloadForKey(SContentManager content, string key) + { + // static assets + if (this.StaticSetters.TryGetValue(key, out Action reload)) + { + reload(content, key); + return true; + } + + return false; + } + } +} -- cgit From e8648e217a3c0e45bf78e903623e6f250f92e88d Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 23 Jul 2017 20:22:33 -0400 Subject: add support for reloading wallpaper and building textures (#335) --- src/StardewModdingAPI/Metadata/CoreAssets.cs | 33 +++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'src/StardewModdingAPI/Metadata') diff --git a/src/StardewModdingAPI/Metadata/CoreAssets.cs b/src/StardewModdingAPI/Metadata/CoreAssets.cs index 256a911a..c350b0da 100644 --- a/src/StardewModdingAPI/Metadata/CoreAssets.cs +++ b/src/StardewModdingAPI/Metadata/CoreAssets.cs @@ -5,6 +5,8 @@ using Microsoft.Xna.Framework.Graphics; using StardewModdingAPI.Framework; using StardewValley; using StardewValley.BellsAndWhistles; +using StardewValley.Buildings; +using StardewValley.Locations; using StardewValley.Objects; using StardewValley.Projectiles; @@ -83,7 +85,10 @@ namespace StardewModdingAPI.Metadata { if (Game1.player != null && !Game1.player.isMale) Game1.player.FarmerRenderer = new FarmerRenderer(content.Load(key)); - } + }, + + // from Wallpaper constructor + ["Maps\\walls_and_floors"] = (content, key) => Wallpaper.wallpaperTexture = content.Load(key) } .ToDictionary(p => getNormalisedPath(p.Key), p => p.Value); } @@ -101,7 +106,33 @@ namespace StardewModdingAPI.Metadata return true; } + // building textures + if (key.StartsWith(this.GetNormalisedPath("Buildings\\"))) + { + Building[] buildings = this.GetAllBuildings().Where(p => key == this.GetNormalisedPath($"Buildings\\{p.buildingType}")).ToArray(); + if (buildings.Any()) + { + Texture2D texture = content.Load(key); + foreach (Building building in buildings) + building.texture = texture; + return true; + } + return false; + } + return false; } + + + /********* + ** Private methods + *********/ + /// Get all player-constructed buildings in the world. + private IEnumerable GetAllBuildings() + { + return Game1.locations + .OfType() + .SelectMany(p => p.buildings); + } } } -- cgit From 64facdd439b4d924d7214bb2d9f6fd72e009dd42 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 23 Jul 2017 20:44:04 -0400 Subject: add support for reloading more singleton assets (#335) --- src/StardewModdingAPI/Metadata/CoreAssets.cs | 39 +++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 7 deletions(-) (limited to 'src/StardewModdingAPI/Metadata') diff --git a/src/StardewModdingAPI/Metadata/CoreAssets.cs b/src/StardewModdingAPI/Metadata/CoreAssets.cs index c350b0da..3818314d 100644 --- a/src/StardewModdingAPI/Metadata/CoreAssets.cs +++ b/src/StardewModdingAPI/Metadata/CoreAssets.cs @@ -9,6 +9,7 @@ using StardewValley.Buildings; using StardewValley.Locations; using StardewValley.Objects; using StardewValley.Projectiles; +using StardewValley.TerrainFeatures; namespace StardewModdingAPI.Metadata { @@ -21,8 +22,8 @@ namespace StardewModdingAPI.Metadata /// Normalises an asset key to match the cache key. protected readonly Func GetNormalisedPath; - /// The static asset setters. - private readonly IDictionary> StaticSetters; + /// Setters which update static or singleton texture fields indexed by normalised asset key. + private readonly IDictionary> SingletonSetters; /********* @@ -33,12 +34,11 @@ namespace StardewModdingAPI.Metadata public CoreAssets(Func getNormalisedPath) { this.GetNormalisedPath = getNormalisedPath; - this.StaticSetters = + this.SingletonSetters = new Dictionary> { // from Game1.loadContent ["LooseSprites\\daybg"] = (content, key) => Game1.daybg = content.Load(key), - ["LooseSprites\\daybg"] = (content, key) => Game1.daybg = content.Load(key), ["LooseSprites\\nightbg"] = (content, key) => Game1.nightbg = content.Load(key), ["Maps\\MenuTiles"] = (content, key) => Game1.menuTexture = content.Load(key), ["LooseSprites\\Lighting\\lantern"] = (content, key) => Game1.lantern = content.Load(key), @@ -75,7 +75,21 @@ namespace StardewModdingAPI.Metadata ["TileSheets\\weapons"] = (content, key) => Tool.weaponsTexture = content.Load(key), ["TileSheets\\Projectiles"] = (content, key) => Projectile.projectileSheet = content.Load(key), - // from Farmer constructor + // from Bush + ["TileSheets\\bushes"] = (content, key) => Bush.texture = content.Load(key), + + // from Critter + ["TileSheets\\critters"] = (content, key) => Critter.critterTexture = content.Load(key), + + // from Farm + ["Buildings\\houses"] = (content, key) => + { + Farm farm = Game1.getFarm(); + if (farm != null) + farm.houseTextures = content.Load(key); + }, + + // from Farmer ["Characters\\Farmer\\farmer_base"] = (content, key) => { if (Game1.player != null && Game1.player.isMale) @@ -87,7 +101,18 @@ namespace StardewModdingAPI.Metadata Game1.player.FarmerRenderer = new FarmerRenderer(content.Load(key)); }, - // from Wallpaper constructor + // from Flooring + ["TerrainFeatures\\Flooring"] = (content, key) => Flooring.floorsTexture = content.Load(key), + + // from FruitTree + ["TileSheets\\fruitTrees"] = (content, key) => FruitTree.texture = content.Load(key), + + // from HoeDirt + ["TerrainFeatures\\hoeDirt"] = (content, key) => HoeDirt.lightTexture = content.Load(key), + ["TerrainFeatures\\hoeDirtDark"] = (content, key) => HoeDirt.darkTexture = content.Load(key), + ["TerrainFeatures\\hoeDirtSnow"] = (content, key) => HoeDirt.snowTexture = content.Load(key), + + // from Wallpaper ["Maps\\walls_and_floors"] = (content, key) => Wallpaper.wallpaperTexture = content.Load(key) } .ToDictionary(p => getNormalisedPath(p.Key), p => p.Value); @@ -100,7 +125,7 @@ namespace StardewModdingAPI.Metadata public bool ReloadForKey(SContentManager content, string key) { // static assets - if (this.StaticSetters.TryGetValue(key, out Action reload)) + if (this.SingletonSetters.TryGetValue(key, out Action reload)) { reload(content, key); return true; -- cgit From 1e48e7d12b4ef3ad9f630f530cc5495eca3593ec Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 16 Aug 2017 21:46:01 -0400 Subject: add tool tilesheet to core assets to reload (#346) --- src/StardewModdingAPI/Metadata/CoreAssets.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/StardewModdingAPI/Metadata') diff --git a/src/StardewModdingAPI/Metadata/CoreAssets.cs b/src/StardewModdingAPI/Metadata/CoreAssets.cs index 3818314d..24f23af7 100644 --- a/src/StardewModdingAPI/Metadata/CoreAssets.cs +++ b/src/StardewModdingAPI/Metadata/CoreAssets.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework.Graphics; @@ -75,6 +75,9 @@ namespace StardewModdingAPI.Metadata ["TileSheets\\weapons"] = (content, key) => Tool.weaponsTexture = content.Load(key), ["TileSheets\\Projectiles"] = (content, key) => Projectile.projectileSheet = content.Load(key), + // from Game1.ResetToolSpriteSheet + ["TileSheets\\tools"] = (content, key) => Game1.ResetToolSpriteSheet(), + // from Bush ["TileSheets\\bushes"] = (content, key) => Bush.texture = content.Load(key), -- cgit