diff --git a/docs/release-notes.md b/docs/release-notes.md index 1f394849..8dc53e19 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -3,6 +3,7 @@ # Release notes ## Upcoming release * For players: + * Improved performance when mods change some asset types (including NPC portraits/sprites). * Fixed CurseForge update checks for the new CurseForge API. ## 3.14.4 diff --git a/src/SMAPI/Metadata/CoreAssetPropagator.cs b/src/SMAPI/Metadata/CoreAssetPropagator.cs index dabd802d..ce9ba7a8 100644 --- a/src/SMAPI/Metadata/CoreAssetPropagator.cs +++ b/src/SMAPI/Metadata/CoreAssetPropagator.cs @@ -219,7 +219,7 @@ namespace StardewModdingAPI.Metadata ** Animals ****/ case "animals/horse": - return !ignoreWorld && this.UpdatePetOrHorseSprites(content, assetName); + return !ignoreWorld && this.UpdatePetOrHorseSprites(assetName); /**** ** Buildings @@ -486,7 +486,7 @@ namespace StardewModdingAPI.Metadata return true; case "tilesheets/critters": // Critter constructor - return !ignoreWorld && this.UpdateCritterTextures(content, assetName); + return !ignoreWorld && this.UpdateCritterTextures(assetName); case "tilesheets/crops": // Game1.LoadContent Game1.cropSpriteSheet = content.Load(key); @@ -555,27 +555,27 @@ namespace StardewModdingAPI.Metadata return true; case "terrainfeatures/mushroom_tree": // from Tree - return !ignoreWorld && this.UpdateTreeTextures(content, assetName, Tree.mushroomTree); + return !ignoreWorld && this.UpdateTreeTextures(Tree.mushroomTree); case "terrainfeatures/tree_palm": // from Tree - return !ignoreWorld && this.UpdateTreeTextures(content, assetName, Tree.palmTree); + return !ignoreWorld && this.UpdateTreeTextures(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 !ignoreWorld && this.UpdateTreeTextures(content, assetName, Tree.bushyTree); + return !ignoreWorld && this.UpdateTreeTextures(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 !ignoreWorld && this.UpdateTreeTextures(content, assetName, Tree.leafyTree); + return !ignoreWorld && this.UpdateTreeTextures(Tree.leafyTree); case "terrainfeatures/tree3_fall": // from Tree case "terrainfeatures/tree3_spring": // from Tree case "terrainfeatures/tree3_winter": // from Tree - return !ignoreWorld && this.UpdateTreeTextures(content, assetName, Tree.pineTree); + return !ignoreWorld && this.UpdateTreeTextures(Tree.pineTree); } /**** @@ -585,11 +585,11 @@ namespace StardewModdingAPI.Metadata { // dynamic textures if (assetName.StartsWith("animals/cat")) - return this.UpdatePetOrHorseSprites(content, assetName); + return this.UpdatePetOrHorseSprites(assetName); if (assetName.StartsWith("animals/dog")) - return this.UpdatePetOrHorseSprites(content, assetName); + return this.UpdatePetOrHorseSprites(assetName); if (assetName.IsDirectlyUnderPath("Animals")) - return this.UpdateFarmAnimalSprites(content, assetName); + return this.UpdateFarmAnimalSprites(assetName); if (assetName.IsDirectlyUnderPath("Buildings")) return this.UpdateBuildings(assetName); @@ -643,33 +643,29 @@ namespace StardewModdingAPI.Metadata /// Update the sprites for matching pets or horses. /// The animal type. - /// The content manager through which to update the asset. /// The asset name to update. /// Returns whether any references were updated. - private bool UpdatePetOrHorseSprites(LocalizedContentManager content, IAssetName assetName) + private bool UpdatePetOrHorseSprites(IAssetName assetName) where TAnimal : NPC { // find matches TAnimal[] animals = this.GetCharacters() .OfType() - .Where(p => this.IsSameBaseName(assetName, p.Sprite?.Texture?.Name)) + .Where(p => this.IsSameBaseName(assetName, p.Sprite?.spriteTexture?.Name)) .ToArray(); - if (!animals.Any()) - return false; // update sprites - Texture2D texture = content.Load(assetName.BaseName); + bool changed = false; foreach (TAnimal animal in animals) - animal.Sprite.spriteTexture = texture; - return true; + changed |= this.MarkSpriteDirty(animal.Sprite); + return changed; } /// Update the sprites for matching farm animals. - /// The content manager through which to update the asset. /// The asset name to update. /// Returns whether any references were updated. /// Derived from . - private bool UpdateFarmAnimalSprites(LocalizedContentManager content, IAssetName assetName) + private bool UpdateFarmAnimalSprites(IAssetName assetName) { // find matches FarmAnimal[] animals = this.GetFarmAnimals().ToArray(); @@ -677,7 +673,7 @@ namespace StardewModdingAPI.Metadata return false; // update sprites - Lazy texture = new Lazy(() => content.Load(assetName.BaseName)); + bool changed = true; foreach (FarmAnimal animal in animals) { // get expected key @@ -690,9 +686,9 @@ namespace StardewModdingAPI.Metadata // reload asset if (this.IsSameBaseName(assetName, expectedKey)) - animal.Sprite.spriteTexture = texture.Value; + changed |= this.MarkSpriteDirty(animal.Sprite); } - return texture.IsValueCreated; + return changed; } /// Update building textures. @@ -707,7 +703,7 @@ namespace StardewModdingAPI.Metadata // get building type string type = Path.GetFileName(assetName.BaseName); if (isPaintMask) - type = type.Substring(0, type.Length - paintMaskSuffix.Length); + type = type[..^paintMaskSuffix.Length]; // get buildings Building[] buildings = this.GetLocations(buildingInteriors: false) @@ -747,7 +743,7 @@ namespace StardewModdingAPI.Metadata foreach (MapSeat seat in location.mapSeats.Where(p => p != null)) { if (this.IsSameBaseName(assetName, seat._loadedTextureFile)) - seat.overlayTexture = MapSeat.mapChairTexture; + seat._loadedTextureFile = null; } } } @@ -756,10 +752,9 @@ namespace StardewModdingAPI.Metadata } /// Update critter textures. - /// The content manager through which to reload the asset. /// The asset name to update. /// Returns whether any references were updated. - private bool UpdateCritterTextures(LocalizedContentManager content, IAssetName assetName) + private bool UpdateCritterTextures(IAssetName assetName) { // get critters Critter[] critters = @@ -767,19 +762,16 @@ namespace StardewModdingAPI.Metadata from location in this.GetLocations() where location.critters != null from Critter critter in location.critters - where this.IsSameBaseName(assetName, critter.sprite?.Texture?.Name) + where this.IsSameBaseName(assetName, critter.sprite?.spriteTexture?.Name) select critter ) .ToArray(); - if (!critters.Any()) - return false; // update sprites - Texture2D texture = content.Load(assetName.BaseName); + bool changed = false; foreach (Critter entry in critters) - entry.sprite.spriteTexture = texture; - - return true; + changed |= this.MarkSpriteDirty(entry.sprite); + return changed; } /// Update the sprites for interior doors. @@ -814,7 +806,7 @@ namespace StardewModdingAPI.Metadata private bool UpdateFenceTextures(IAssetName assetName) { // get fence type (e.g. LooseSprites/Fence3 => 3) - if (!int.TryParse(this.GetSegments(assetName.BaseName)[1].Substring("Fence".Length), out int fenceType)) + if (!int.TryParse(this.GetSegments(assetName.BaseName)[1]["Fence".Length..], out int fenceType)) return false; // get fences @@ -830,9 +822,16 @@ namespace StardewModdingAPI.Metadata .ToArray(); // update fence textures + bool changed = false; foreach (Fence fence in fences) - fence.fenceTexture = new Lazy(fence.loadFenceTexture); - return true; + { + if (fence.fenceTexture.IsValueCreated) + { + fence.fenceTexture = new Lazy(fence.loadFenceTexture); + changed = true; + } + } + return changed; } /// Update tree textures. @@ -850,15 +849,16 @@ namespace StardewModdingAPI.Metadata ) .ToArray(); - if (grasses.Any()) + bool changed = false; + foreach (Grass grass in grasses) { - Lazy texture = new Lazy(() => content.Load(assetName.BaseName)); - foreach (Grass grass in grasses) - grass.texture = texture; - return true; + if (grass.texture.IsValueCreated) + { + grass.texture = new Lazy(() => content.Load(assetName.BaseName)); + changed = true; + } } - - return false; + return changed; } /// Update the sprites for matching NPCs. @@ -869,19 +869,17 @@ namespace StardewModdingAPI.Metadata var characters = ( from npc in this.GetCharacters() - let key = this.ParseAssetNameOrNull(npc.Sprite?.Texture?.Name)?.GetBaseAssetName() + let key = this.ParseAssetNameOrNull(npc.Sprite?.spriteTexture?.Name)?.GetBaseAssetName() where key != null && propagated.ContainsKey(key) select new { Npc = npc, AssetName = key } ) .ToArray(); - if (!characters.Any()) - return; // update sprite foreach (var target in characters) { - target.Npc.Sprite.spriteTexture = this.LoadTexture(target.AssetName.BaseName); - propagated[target.AssetName] = true; + if (this.MarkSpriteDirty(target.Npc.Sprite)) + propagated[target.AssetName] = true; } } @@ -919,7 +917,7 @@ namespace StardewModdingAPI.Metadata // update portrait foreach (var target in characters) { - target.Npc.Portrait = this.LoadTexture(target.AssetName.BaseName); + target.Npc.resetPortrait(); propagated[target.AssetName] = true; } } @@ -976,26 +974,38 @@ namespace StardewModdingAPI.Metadata } /// Update tree textures. - /// The content manager through which to reload the asset. - /// The asset name to update. /// The type to update. /// Returns whether any references were updated. - private bool UpdateTreeTextures(LocalizedContentManager content, IAssetName assetName, int type) + private bool UpdateTreeTextures(int type) { Tree[] trees = this.GetLocations() .SelectMany(p => p.terrainFeatures.Values.OfType()) .Where(tree => tree.treeType.Value == type) .ToArray(); - if (trees.Any()) + bool changed = false; + foreach (Tree tree in trees) { - Lazy texture = new Lazy(() => content.Load(assetName.BaseName)); - foreach (Tree tree in trees) - tree.texture = texture; - return true; + if (tree.texture.IsValueCreated) + { + this.Reflection.GetMethod(tree, "resetTexture").Invoke(); + changed = true; + } } + return changed; + } - return false; + /// Mark an animated sprite's texture dirty, so it's reloaded next time it's rendered. + /// The animated sprite to change. + /// Returns whether the sprite was changed. + private bool MarkSpriteDirty(AnimatedSprite sprite) + { + if (sprite.loadedTexture is null && sprite.spriteTexture is null) + return false; + + sprite.loadedTexture = null; + sprite.spriteTexture = null; + return true; } /****