Skip to content

Commit

Permalink
Make cosmetic patches ~~half as slow~~ ~~twice as fast~~ faster
Browse files Browse the repository at this point in the history
  • Loading branch information
Miepee committed Jun 6, 2024
1 parent a10de12 commit 984b398
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
10 changes: 2 additions & 8 deletions YAMS-LIB/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down
52 changes: 43 additions & 9 deletions YAMS-LIB/patches/CosmeticHud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,38 @@ namespace YAMS_LIB.patches;

public class CosmeticHud
{
static void RotateTextureAndSaveToTexturePage(int rotation, UndertaleTexturePageItem texture)
static void RotateTextureAndSaveToTexturePage(UndertaleEmbeddedTexture texture, List<Tuple<Rectangle, int>> 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<UndertaleEmbeddedTexture, List<Tuple<Rectangle, int>>> textureDict = new Dictionary<UndertaleEmbeddedTexture, List<Tuple<Rectangle, int>>>();

// 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<Tuple<Rectangle, int>>();
tupleList.Add(new Tuple<Rectangle, int>(new Rectangle(texture.SourceX, texture.SourceY, texture.SourceWidth, texture.SourceHeight), seedObject.Cosmetics.EtankHUDRotation));
if (!wasInDict)
textureDict.Add(texture.TexturePage, null);

textureDict[texture.TexturePage] = tupleList;
}
}

Expand All @@ -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<Tuple<Rectangle, int>>();
tupleList.Add(new Tuple<Rectangle, int>(new Rectangle(texture.SourceX, texture.SourceY, texture.SourceWidth, texture.SourceHeight), seedObject.Cosmetics.HealthHUDRotation));
if (!wasInDict)
textureDict.Add(texture.TexturePage, null);

textureDict[texture.TexturePage] = tupleList;

}
}

Expand All @@ -45,8 +67,20 @@ public static void Apply(UndertaleData gmData, GlobalDecompileContext decompileC
{
foreach (UndertaleBackground bg in new List<UndertaleBackground> { 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<Tuple<Rectangle, int>>();
tupleList.Add(new Tuple<Rectangle, int>(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);
}
}
}

0 comments on commit 984b398

Please sign in to comment.