This repository was archived by the owner on Oct 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScripts.cs
More file actions
168 lines (146 loc) · 6.64 KB
/
Copy pathScripts.cs
File metadata and controls
168 lines (146 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System.Reflection;
using UndertaleModLib;
using UndertaleModLib.Models;
using UndertaleModLib.Decompiler;
using UndertaleModLib.Util;
using System.Drawing;
using System.Drawing.Imaging;
public static class Scripts
{
public static UndertaleData Data { get; set; } = new UndertaleData();
public static string ExportFolder { get; set; } = @"./mod.gmmod";
private static string CodeFolder { get => Path.Combine(ExportFolder, "Code"); }
private static string TextureFolder { get => Path.Combine(ExportFolder, "Textures"); }
private static string SpriteFolder { get => Path.Combine(TextureFolder, "Sprites"); }
private static string FontFolder { get => Path.Combine(TextureFolder, "Fonts"); }
private static string BackgroundFolder { get => Path.Combine(TextureFolder, "Backgrounds"); }
private static string FontDataFolder { get => Path.Combine(ExportFolder, "FontData"); }
// Modified versions of the ExportASM.csx functions from UTMT
#region ExportASM.csx
public static void ExportASM(UndertaleCode code)
{
string path = Path.Combine(CodeFolder, code.Name.Content + ".asm");
if (!Directory.Exists(CodeFolder))
Directory.CreateDirectory(CodeFolder);
try
{
File.WriteAllText(path, (code != null ? code.Disassemble(Data.Variables, Data.CodeLocals.For(code)) : ""));
}
catch (Exception e)
{
File.WriteAllText(path, "/*\nDISASSEMBLY FAILED!\n\n" + e.ToString() + "\n*/"); // Please don't
}
}
public static void ExportASM(string asm, string codeName)
{
string path = Path.Combine(CodeFolder, codeName + ".asm");
if (!Directory.Exists(CodeFolder))
Directory.CreateDirectory(CodeFolder);
File.WriteAllText(path, asm);
}
#endregion
// Modified versions of the ExportAllTexturesGroup.csx functions from UTMT
#region ExportAllTexturesGroup.csx
public static void DumpSprite(UndertaleSprite sprite)
{
TextureWorker worker = new();
for (int i = 0; i < sprite.Textures.Count; i++)
if (sprite.Textures[i]?.Texture != null)
{
UndertaleTexturePageItem tex = sprite.Textures[i].Texture;
string sprFolder2 = Path.Combine(SpriteFolder, sprite.Name.Content);
Directory.CreateDirectory(sprFolder2);
worker.ExportAsPNG(tex, Path.Combine(sprFolder2, sprite.Name.Content + "_" + i + ".png"));
}
}
public static void DumpSingleSpriteTexture(UndertaleSprite sprite, int index)
{
TextureWorker worker = new();
if (sprite.Textures[index]?.Texture != null)
{
UndertaleTexturePageItem tex = sprite.Textures[index].Texture;
string sprFolder2 = Path.Combine(SpriteFolder, sprite.Name.Content);
Directory.CreateDirectory(sprFolder2);
worker.ExportAsPNG(tex, Path.Combine(sprFolder2, sprite.Name.Content + "_" + index + ".png"));
}
}
public static void DumpFont(UndertaleFont font)
{
if (font.Texture is null)
return;
TextureWorker worker = new();
UndertaleTexturePageItem tex = font.Texture;
string fntFolder2 = Path.Combine(FontFolder, font.Name.Content);
Directory.CreateDirectory(fntFolder2);
worker.ExportAsPNG(tex, Path.Combine(fntFolder2, font.Name.Content + "_0.png"));
}
public static void DumpBackground(UndertaleBackground background)
{
if (background.Texture is null)
return;
TextureWorker worker = new();
UndertaleTexturePageItem tex = background.Texture;
string bgrFolder2 = Path.Combine(BackgroundFolder, background.Name.Content);
Directory.CreateDirectory(bgrFolder2);
worker.ExportAsPNG(tex, Path.Combine(bgrFolder2, background.Name.Content + "_0.png"));
}
#endregion
// Modified versions of the ExportAllFontData.csx functions from UTMT
#region ExportFontData.csx
public static void DumpFontData(UndertaleFont font)
{
if (!Directory.Exists(FontDataFolder))
Directory.CreateDirectory(FontDataFolder);
using (StreamWriter writer = new StreamWriter(Path.Combine(FontDataFolder, "glyphs_" + font.Name.Content + ".csv")))
{
writer.WriteLine(font.DisplayName + ";" + font.EmSize + ";" + font.Bold + ";" + font.Italic + ";" + font.Charset + ";" + font.AntiAliasing + ";" + font.ScaleX + ";" + font.ScaleY);
foreach (var g in font.Glyphs)
writer.WriteLine(g.Character + ";" + g.SourceX + ";" + g.SourceY + ";" + g.SourceWidth + ";" + g.SourceHeight + ";" + g.Shift + ";" + g.Offset);
}
}
#endregion
#region Helpers
// I hate the default .Equals() returning whether they are the same instance or not instead of comparing the class's values
public static bool PropertiesEqual<T>(T ogNamedResource, T moddedNamedResource, string[] propertiesToCompare)
{
if (ogNamedResource is null || moddedNamedResource is null)
throw new NullReferenceException();
foreach (string propertyName in propertiesToCompare)
{
var propertyToCompare = typeof(T).GetProperty(propertyName);
if (propertyToCompare is null)
throw new NullReferenceException($"Property {propertyName} not found.");
object? ogValue = propertyToCompare.GetValue(ogNamedResource);
object? moddedValue = propertyToCompare.GetValue(moddedNamedResource);
// For some reason != doesn't work here
if (ogValue?.Equals(moddedValue) == false)
return false;
}
return true;
}
public static bool TextureEquals(UndertaleTexturePageItem ogTexture, UndertaleTexturePageItem moddedTexture)
{
var worker = new TextureWorker();
var ogTextureImage = worker.GetTextureFor(ogTexture, "");
var moddedTextureImage = worker.GetTextureFor(moddedTexture, moddedTexture.Name.Content);
return ImageEquals(ogTextureImage, moddedTextureImage);
}
public static bool ImageEquals(Image ogImage, Image moddedImage)
{
var ogImageData = GetImageBytes(ogImage);
var moddedImageData = GetImageBytes(moddedImage);
return ogImageData.SequenceEqual(moddedImageData);
}
// Modified version of TextureWorker.GetImageBytes()
private static byte[] GetImageBytes(Image image, bool disposeImage = true)
{
using (var ms = new MemoryStream())
{
image.Save(ms, ImageFormat.Png);
if (disposeImage)
image.Dispose();
return ms.ToArray();
}
}
#endregion
}