Skip to content

Commit

Permalink
Update UTMT submodule, replace imagesharp with imagemagick.net
Browse files Browse the repository at this point in the history
  • Loading branch information
Miepee committed Aug 23, 2024
1 parent 9c3cbb5 commit 15cc72b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 46 deletions.
2 changes: 1 addition & 1 deletion UndertaleModTool
1 change: 0 additions & 1 deletion YAMS-LIB/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Globalization;
using System.Reflection;
using System.Text.Json;
using SixLabors.ImageSharp.Processing;
using UndertaleModLib;
using UndertaleModLib.Decompiler;
using UndertaleModLib.Models;
Expand Down
1 change: 0 additions & 1 deletion YAMS-LIB/YAMS-LIB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

<ItemGroup>
<PackageReference Include="Macross.Json.Extensions" Version="3.0.0"/>
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.4"/>
<PackageReference Include="NaturalSort.Extension" Version="4.3.0" />
</ItemGroup>

Expand Down
41 changes: 21 additions & 20 deletions YAMS-LIB/patches/CosmeticRotation.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Processing;
using ImageMagick;
using UndertaleModLib;
using UndertaleModLib.Decompiler;
using UndertaleModLib.Models;
using UndertaleModLib.Util;

namespace YAMS_LIB.patches;

Expand Down Expand Up @@ -253,23 +252,25 @@ public class CosmeticRotation
"bgWFilter2",
];

static void RotateTextureAndSaveToTexturePage(UndertaleEmbeddedTexture texture, List<Tuple<Rectangle, int>> rectangleRotationTuple)
static void RotateTextureAndSaveToTexturePage(UndertaleEmbeddedTexture texture, List<Tuple<MagickGeometry, int>> rectangleRotationTuple)
{
using Image texturePage = Image.Load(texture.TextureData.TextureBlob);
using MagickImage texturePage = texture.TextureData.Image.GetMagickImage();
foreach ((var rectangle, var rotation) in rectangleRotationTuple)
{
texturePage.Mutate(im => im.Hue(rotation, rectangle));
using var mask = new MagickImage(MagickColors.White, texturePage.Width, texturePage.Height);
mask.Draw(new DrawableFillColor(MagickColors.Black), new DrawableRectangle(rectangle.X, rectangle.Y, rectangle.X+rectangle.Width, rectangle.Y+rectangle.Height));
texturePage.SetWriteMask(mask);
texturePage.Modulate((Percentage)100.0, (Percentage)100.0, (Percentage)((rotation * 100 / 180) + 100));
texturePage.RemoveWriteMask();
}

using MemoryStream ms = new MemoryStream();
texturePage.Save(ms, PngFormat.Instance);
texture.TextureData.TextureBlob = ms.ToArray();
texture.TextureData.Image = GMImage.FromPng(texturePage.ToByteArray(MagickFormat.Png));
}


public static void Apply(UndertaleData gmData, GlobalDecompileContext decompileContext, SeedObject seedObject)
{
var textureDict = new Dictionary<UndertaleEmbeddedTexture, List<Tuple<Rectangle, int>>>();
var textureDict = new Dictionary<UndertaleEmbeddedTexture, List<Tuple<MagickGeometry, int>>>();

// TODO: less copypaste
// Hue shift etanks
Expand All @@ -280,8 +281,8 @@ public static void Apply(UndertaleData gmData, GlobalDecompileContext decompileC
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),
tupleList = new List<Tuple<MagickGeometry, int>>();
tupleList.Add(new Tuple<MagickGeometry, int>(new MagickGeometry(texture.SourceX, texture.SourceY, texture.SourceWidth, texture.SourceHeight),
seedObject.Cosmetics.EtankHUDRotation));
if (!wasInDict)
textureDict.Add(texture.TexturePage, null);
Expand All @@ -298,8 +299,8 @@ public static void Apply(UndertaleData gmData, GlobalDecompileContext decompileC
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),
tupleList = new List<Tuple<MagickGeometry, int>>();
tupleList.Add(new Tuple<MagickGeometry, int>(new MagickGeometry(texture.SourceX, texture.SourceY, texture.SourceWidth, texture.SourceHeight),
seedObject.Cosmetics.HealthHUDRotation));
if (!wasInDict)
textureDict.Add(texture.TexturePage, null);
Expand All @@ -316,8 +317,8 @@ public static void Apply(UndertaleData gmData, GlobalDecompileContext decompileC
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),
tupleList = new List<Tuple<MagickGeometry, int>>();
tupleList.Add(new Tuple<MagickGeometry, int>(new MagickGeometry(texture.SourceX, texture.SourceY, texture.SourceWidth, texture.SourceHeight),
seedObject.Cosmetics.DNAHUDRotation));
if (!wasInDict)
textureDict.Add(texture.TexturePage, null);
Expand All @@ -334,8 +335,8 @@ public static void Apply(UndertaleData gmData, GlobalDecompileContext decompileC
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),
tupleList = new List<Tuple<MagickGeometry, int>>();
tupleList.Add(new Tuple<MagickGeometry, int>(new MagickGeometry(texture.SourceX, texture.SourceY, texture.SourceWidth, texture.SourceHeight),
seedObject.Cosmetics.TilesetRotation));
if (!wasInDict)
textureDict.Add(texture.TexturePage, null);
Expand All @@ -352,8 +353,8 @@ public static void Apply(UndertaleData gmData, GlobalDecompileContext decompileC
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),
tupleList = new List<Tuple<MagickGeometry, int>>();
tupleList.Add(new Tuple<MagickGeometry, int>(new MagickGeometry(texture.SourceX, texture.SourceY, texture.SourceWidth, texture.SourceHeight),
seedObject.Cosmetics.BackgroundRotation));
if (!wasInDict)
textureDict.Add(texture.TexturePage, null);
Expand Down
41 changes: 18 additions & 23 deletions YAMS-LIB/patches/Sprites.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System.Reflection;
using System.Text.Json;
using ImageMagick;
using NaturalSort.Extension;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Processing;
using UndertaleModLib;
using UndertaleModLib.Decompiler;
using UndertaleModLib.Models;
using UndertaleModLib.Util;

namespace YAMS_LIB.patches;

Expand All @@ -18,13 +17,11 @@ public static void Apply(UndertaleData gmData, GlobalDecompileContext decompileC
var nameToPageItemDict = new Dictionary<string, int>();
UndertaleEmbeddedTexture? utTexturePage = new UndertaleEmbeddedTexture();

using (MemoryStream ms = new MemoryStream())
using var texturePage = new MagickImage(new FileInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/sprites/texturepage.png"));
{
var texturePage = Image.Load(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/sprites/texturepage.png");
utTexturePage.TextureWidth = texturePage.Width;
utTexturePage.TextureHeight = texturePage.Height;
texturePage.Save(ms, PngFormat.Instance);
utTexturePage.TextureData = new UndertaleEmbeddedTexture.TexData { TextureBlob = ms.ToArray() };
utTexturePage.TextureData = new UndertaleEmbeddedTexture.TexData { Image = GMImage.FromPng(texturePage.ToByteArray(MagickFormat.Png)) };
}
gmData.EmbeddedTextures.Add(utTexturePage);

Expand All @@ -44,27 +41,25 @@ public static void Apply(UndertaleData gmData, GlobalDecompileContext decompileC
// Replace A4 doors
{
UndertaleTexturePageItem? a4DoorTex = gmData.TexturePageItems[nameToPageItemDict["newA4Doors"]];
Image a4DoorImage = Image.Load(a4DoorTex.TexturePage.TextureData.TextureBlob);
a4DoorImage.Mutate(i => i.Crop(new Rectangle(a4DoorTex.SourceX, a4DoorTex.SourceY, a4DoorTex.SourceWidth, a4DoorTex.SourceHeight)));
UndertaleTexturePageItem? a4Tex = gmData.Backgrounds.ByName("tlArea4Tech").Texture;
Image a4PageImage = Image.Load(a4Tex.TexturePage.TextureData.TextureBlob);
a4PageImage.Mutate(i => i.DrawImage(a4DoorImage, new Point(a4Tex.SourceX + 104, a4Tex.SourceY), 1));
using (MemoryStream ms = new MemoryStream())
using var a4DoorImage = a4DoorTex.TexturePage.TextureData.Image.GetMagickImage();
{
a4PageImage.Save(ms, PngFormat.Instance);
a4Tex.TexturePage.TextureData.TextureBlob = ms.ToArray();
a4DoorImage.Crop(new MagickGeometry(a4DoorTex.SourceX, a4DoorTex.SourceY, a4DoorTex.SourceWidth, a4DoorTex.SourceHeight));
a4DoorImage.RePage();
UndertaleTexturePageItem? a4Tex = gmData.Backgrounds.ByName("tlArea4Tech").Texture;
using var a4PageImage = a4Tex.TexturePage.TextureData.Image.GetMagickImage();
a4PageImage.Composite(a4DoorImage, a4Tex.SourceX + 104, a4Tex.SourceY, CompositeOperator.Over);
a4Tex.TexturePage.TextureData.Image = GMImage.FromPng(a4PageImage.ToByteArray(MagickFormat.Png));
}

UndertaleTexturePageItem? a4door2Tex = gmData.TexturePageItems[nameToPageItemDict["newA4Doors2"]];
Image a4Door2Image = Image.Load(a4door2Tex.TexturePage.TextureData.TextureBlob);
a4Door2Image.Mutate(i => i.Crop(new Rectangle(a4door2Tex.SourceX, a4door2Tex.SourceY, a4door2Tex.SourceWidth, a4door2Tex.SourceHeight)));
UndertaleTexturePageItem? a4Tex2 = gmData.Backgrounds.ByName("tlArea4Tech2").Texture;
Image a4Page2Image = Image.Load(a4Tex2.TexturePage.TextureData.TextureBlob);
a4Page2Image.Mutate(i => i.DrawImage(a4Door2Image, new Point(a4Tex2.SourceX + 104, a4Tex2.SourceY), 1));
using (MemoryStream ms = new MemoryStream())
using var a4Door2Image = a4door2Tex.TexturePage.TextureData.Image.GetMagickImage();
{
a4Page2Image.Save(ms, PngFormat.Instance);
a4Tex2.TexturePage.TextureData.TextureBlob = ms.ToArray();
a4Door2Image.Crop(new MagickGeometry(a4door2Tex.SourceX, a4door2Tex.SourceY, a4door2Tex.SourceWidth, a4door2Tex.SourceHeight));
a4Door2Image.RePage();
UndertaleTexturePageItem? a4Tex2 = gmData.Backgrounds.ByName("tlArea4Tech2").Texture;
using var a4Page2Image = a4Tex2.TexturePage.TextureData.Image.GetMagickImage();
a4Page2Image.Composite(a4Door2Image, a4Tex2.SourceX + 104, a4Tex2.SourceY, CompositeOperator.Over);
a4Tex2.TexturePage.TextureData.Image = GMImage.FromPng(a4Page2Image.ToByteArray(MagickFormat.Png));
}
}

Expand Down

0 comments on commit 15cc72b

Please sign in to comment.