From 984b398d9df0d38e217a8aa5299de5694c56d751 Mon Sep 17 00:00:00 2001 From: Miepee Date: Thu, 6 Jun 2024 19:35:56 +0200 Subject: [PATCH] Make cosmetic patches ~~half as slow~~ ~~twice as fast~~ faster --- YAMS-LIB/Program.cs | 10 ++----- YAMS-LIB/patches/CosmeticHud.cs | 52 +++++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/YAMS-LIB/Program.cs b/YAMS-LIB/Program.cs index 86b63ba..33f2c92 100644 --- a/YAMS-LIB/Program.cs +++ b/YAMS-LIB/Program.cs @@ -1,12 +1,6 @@ -using System.ComponentModel.Design; -using System.Globalization; +using System.Globalization; using System.Reflection; using System.Text.Json; -using NaturalSort.Extension; -using SixLabors.ImageSharp; -using SixLabors.ImageSharp.Formats.Png; -using SixLabors.ImageSharp.PixelFormats; -using SixLabors.ImageSharp.Processing; using UndertaleModLib; using UndertaleModLib.Decompiler; using UndertaleModLib.Models; @@ -69,7 +63,7 @@ public static void Main(string am2rPath, string outputAm2rPath, string jsonPath) // Import new Sprites Sprites.Apply(gmData, decompileContext, seedObject); - // Apply cosmetic patches + // Apply cosmetic patches - TODO: run this in parallel? CosmeticHud.Apply(gmData, decompileContext, seedObject); // Shuffle Music - TODO: run this in parallel? MusicShuffle.ShuffleMusic(Path.GetDirectoryName(outputAm2rPath), seedObject.Cosmetics.MusicShuffleDict); diff --git a/YAMS-LIB/patches/CosmeticHud.cs b/YAMS-LIB/patches/CosmeticHud.cs index a27242b..b27322e 100644 --- a/YAMS-LIB/patches/CosmeticHud.cs +++ b/YAMS-LIB/patches/CosmeticHud.cs @@ -9,25 +9,38 @@ namespace YAMS_LIB.patches; public class CosmeticHud { - static void RotateTextureAndSaveToTexturePage(int rotation, UndertaleTexturePageItem texture) + static void RotateTextureAndSaveToTexturePage(UndertaleEmbeddedTexture texture, List> rectangleRotationTuple) { - using Image texturePage = Image.Load(texture.TexturePage.TextureData.TextureBlob); - texturePage.Mutate(im => im.Hue(rotation, new Rectangle(texture.SourceX, texture.SourceY, texture.SourceWidth, texture.SourceHeight))); - + using Image texturePage = Image.Load(texture.TextureData.TextureBlob); + foreach ((var rectangle, var rotation) in rectangleRotationTuple) + { + texturePage.Mutate(im => im.Hue(rotation, rectangle)); + } using MemoryStream ms = new MemoryStream(); texturePage.Save(ms, PngFormat.Instance); - texture.TexturePage.TextureData.TextureBlob = ms.ToArray(); + texture.TextureData.TextureBlob = ms.ToArray(); } public static void Apply(UndertaleData gmData, GlobalDecompileContext decompileContext, SeedObject seedObject) { + Dictionary>> textureDict = new Dictionary>>(); + + // TODO: less copypaste // Hue shift etanks if (seedObject.Cosmetics.EtankHUDRotation != 0) { foreach (UndertaleSprite.TextureEntry? textureEntry in gmData.Sprites.ByName("sGUIETank").Textures) { - RotateTextureAndSaveToTexturePage(seedObject.Cosmetics.EtankHUDRotation, textureEntry.Texture); + var texture = textureEntry.Texture; + bool wasInDict = textureDict.TryGetValue(texture.TexturePage, out var tupleList); + if (tupleList is null) + tupleList = new List>(); + tupleList.Add(new Tuple(new Rectangle(texture.SourceX, texture.SourceY, texture.SourceWidth, texture.SourceHeight), seedObject.Cosmetics.EtankHUDRotation)); + if (!wasInDict) + textureDict.Add(texture.TexturePage, null); + + textureDict[texture.TexturePage] = tupleList; } } @@ -36,7 +49,16 @@ public static void Apply(UndertaleData gmData, GlobalDecompileContext decompileC { foreach (UndertaleSprite.TextureEntry? textureEntry in gmData.Sprites.ByName("sGUIFont1").Textures.Concat(gmData.Sprites.ByName("sGUIFont1A").Textures)) { - RotateTextureAndSaveToTexturePage(seedObject.Cosmetics.HealthHUDRotation, textureEntry.Texture); + var texture = textureEntry.Texture; + bool wasInDict = textureDict.TryGetValue(texture.TexturePage, out var tupleList); + if (tupleList is null) + tupleList = new List>(); + tupleList.Add(new Tuple(new Rectangle(texture.SourceX, texture.SourceY, texture.SourceWidth, texture.SourceHeight), seedObject.Cosmetics.HealthHUDRotation)); + if (!wasInDict) + textureDict.Add(texture.TexturePage, null); + + textureDict[texture.TexturePage] = tupleList; + } } @@ -45,8 +67,20 @@ public static void Apply(UndertaleData gmData, GlobalDecompileContext decompileC { foreach (UndertaleBackground bg in new List { gmData.Backgrounds.ByName("bgGUIMetCountBG1"), gmData.Backgrounds.ByName("bgGUIMetCountBG2ELM") }) { - RotateTextureAndSaveToTexturePage(seedObject.Cosmetics.DNAHUDRotation, bg.Texture); - } + var texture = bg.Texture; + bool wasInDict = textureDict.TryGetValue(texture.TexturePage, out var tupleList); + if (tupleList is null) + tupleList = new List>(); + tupleList.Add(new Tuple(new Rectangle(texture.SourceX, texture.SourceY, texture.SourceWidth, texture.SourceHeight), seedObject.Cosmetics.DNAHUDRotation)); + if (!wasInDict) + textureDict.Add(texture.TexturePage, null); + + textureDict[texture.TexturePage] = tupleList;} + } + + foreach ((var texturePage, var rectangles) in textureDict) + { + RotateTextureAndSaveToTexturePage(texturePage, rectangles); } } }