-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDiffUtils.cs
More file actions
412 lines (379 loc) · 19.2 KB
/
DiffUtils.cs
File metadata and controls
412 lines (379 loc) · 19.2 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
using UndertaleModLib;
using UndertaleModLib.Models;
using DiffMatchPatch;
using UndertaleModLib.Decompiler;
using UndertaleModLib.Util;
using Polenter.Serialization;
using Newtonsoft.Json;
namespace ModShardDiff;
class UndertaleCodeNameComparer : IEqualityComparer<UndertaleCode>
{
public bool Equals(UndertaleCode? x, UndertaleCode? y)
{
if (x == null || y == null) return false;
return x.Name.Content == y.Name.Content;
}
// If Equals() returns true for a pair of objects
// then GetHashCode() must return the same value for these objects.
public int GetHashCode(UndertaleCode x)
{
//Check whether the object is null
if (x == null) return 0;
return x.Name.Content.GetHashCode();
}
}
class UndertaleSpriteNameComparer : IEqualityComparer<UndertaleSprite>
{
public bool Equals(UndertaleSprite? x, UndertaleSprite? y)
{
if (x == null || y == null) return false;
return x.Name.Content == y.Name.Content;
}
// If Equals() returns true for a pair of objects
// then GetHashCode() must return the same value for these objects.
public int GetHashCode(UndertaleSprite x)
{
//Check whether the object is null
if (x == null) return 0;
return x.Name.Content.GetHashCode();
}
}
class UndertaleGameObjectComparer : IEqualityComparer<UndertaleGameObject>
{
public bool Equals(UndertaleGameObject? x, UndertaleGameObject? y)
{
if (x == null || y == null) return false;
return x.Name.Content == y.Name.Content &&
(x.Sprite?.Name.Content ?? "") == (y.Sprite?.Name.Content ?? "") &&
(x.ParentId?.Name.Content ?? "") == (y.ParentId?.Name.Content ?? "") &&
x.Visible == y.Visible &&
x.Persistent == y.Persistent &&
x.Awake == y.Awake &&
x.CollisionShape == y.CollisionShape &&
x.Events.Enumerate().SelectMany(x => x.Item2.Select(y => (((EventType)x.Item1).ToString(), (int)y.EventSubtype))).ToList().Count == y.Events.Enumerate().SelectMany(x => x.Item2.Select(y => (((EventType)x.Item1).ToString(), (int)y.EventSubtype))).ToList().Count;
}
// If Equals() returns true for a pair of objects
// then GetHashCode() must return the same value for these objects.
public int GetHashCode(UndertaleGameObject x)
{
//Check whether the object is null
if (x == null) return 0;
return x.Name.Content.GetHashCode() ^
(x.Sprite?.Name.Content ?? "").GetHashCode() ^
(x.ParentId?.Name.Content ?? "").GetHashCode() ^
x.Visible.GetHashCode() ^
x.Persistent.GetHashCode() ^
x.Awake.GetHashCode() ^
x.CollisionShape.GetHashCode() ^
x.Events.Enumerate().SelectMany(x => x.Item2.Select(y => (((EventType)x.Item1).ToString(), (int)y.EventSubtype))).ToList().Count.GetHashCode();
}
}
class UndertaleGameObjectNameComparer : IEqualityComparer<UndertaleGameObject>
{
public bool Equals(UndertaleGameObject? x, UndertaleGameObject? y)
{
if (x == null || y == null) return false;
return x.Name.Content == y.Name.Content;
}
// If Equals() returns true for a pair of objects
// then GetHashCode() must return the same value for these objects.
public int GetHashCode(UndertaleGameObject x)
{
//Check whether the object is null
if (x == null) return 0;
return x.Name.Content.GetHashCode();
}
}
public class GameObjectSummary
{
public string name {get; set;} = "";
public string spriteName {get; set;} = "";
public string parentName {get; set;} = "";
public bool isVisible {get; set;}
public bool isPersistent {get; set;}
public bool isAwake {get; set;}
public string collisionShapeFlags {get; set;} = "";
public List<(string, int)> Events {get; set;} = new List<(string, int)>();
}
static public class DiffUtils
{
public static IEnumerable<(int, T)> Enumerate<T>(this IEnumerable<T> ienumerable)
{
int ind = 0;
foreach(T element in ienumerable) {
yield return (ind++, element);
}
}
// thanks to Pong to acknowledge me that possibility
private static unsafe bool UnsafeCompare(byte[] a1, byte[] a2)
{
if (a1 == null || a2 == null || a1.Length != a2.Length) return false;
fixed (byte* p1 = a1, p2 = a2)
{
byte* x1 = p1, x2 = p2;
int len = a1.Length;
for (int i = 0; i < len / 8; i++, x1 += 8, x2 += 8)
{
if (*(long*) x1 != *(long*) x2) return false; // classic type casting in C, testing 8 bits by 8 bits
}
if ((len & 4) != 0) // testing last 4 bits
{
if (*(int*) x1 != *(int*) x2) return false;
x1 += 4;
x2 += 4;
}
if ((len & 2) != 0) // 2 bits
{
if (*(short*) x1 != *(short*) x2) return false;
x1 += 2;
x2 += 2;
}
if ((len & 1) != 0 && *x1 != *x2) return false;
return true;
}
}
private static bool CompareUndertaleCode(MemoryStream ms, SharpSerializer burstSerializer, UndertaleCode code, UndertaleCode codeRef)
{
ms.SetLength(0);
burstSerializer.Serialize(code, ms);
byte[] bytes = ms.ToArray();
ms.SetLength(0);
burstSerializer.Serialize(codeRef, ms);
byte[] bytesRef = ms.ToArray();
return UnsafeCompare(bytes, bytesRef);
}
public static void ExportDiffs(IEnumerable<string> added, IEnumerable<string> removed, string name, DirectoryInfo outputFolder)
{
File.WriteAllLines(Path.Join(outputFolder.FullName, Path.DirectorySeparatorChar.ToString(), $"added{name}.txt"), added);
File.WriteAllLines(Path.Join(outputFolder.FullName, Path.DirectorySeparatorChar.ToString(), $"removed{name}.txt"), removed);
}
private static void AddedRemovedCodes(UndertaleData name, UndertaleData reference, DirectoryInfo outputFolder)
{
GlobalDecompileContext contextName = new(name, false);
DirectoryInfo dirAddedCode = new(Path.Join(outputFolder.FullName, Path.DirectorySeparatorChar.ToString(), "AddedCodes"));
dirAddedCode.Create();
IEnumerable<UndertaleCode> added = name.Code.Except(reference.Code, new UndertaleCodeNameComparer());
IEnumerable<UndertaleCode> removed = reference.Code.Except(name.Code, new UndertaleCodeNameComparer());
using (StreamWriter sw = new(Path.Join(outputFolder.FullName, Path.DirectorySeparatorChar.ToString(), $"addedCodes.txt")))
{
foreach(UndertaleCode code in added)
{
sw.WriteLine(code.Name.Content);
string strCode = "";
try
{
strCode = Decompiler.Decompile(code, contextName);
File.WriteAllText(Path.Join(dirAddedCode.FullName, Path.DirectorySeparatorChar.ToString(), $"{code.Name.Content}.gml"), strCode);
}
catch
{
// pass if failed
}
}
}
File.WriteAllLines(Path.Join(outputFolder.FullName, Path.DirectorySeparatorChar.ToString(), $"removedCodes.txt"), removed.Select(x => x.Name.Content));
}
private static void ModifiedCodes(UndertaleData name, UndertaleData reference, DirectoryInfo outputFolder)
{
using MemoryStream ms = new();
SharpSerializer burstSerializer = new(new SharpSerializerBinarySettings(BinarySerializationMode.Burst));
GlobalDecompileContext contextName = new(name, false);
GlobalDecompileContext contextRef = new(reference, false);
DirectoryInfo dirModifiedCode = new(Path.Join(outputFolder.FullName, Path.DirectorySeparatorChar.ToString(), "ModifiedCodes"));
dirModifiedCode.Create();
string strName = "";
string strRef = "";
UndertaleCode codeRef;
IEnumerable<UndertaleCode> common = name.Code.Intersect(reference.Code, new UndertaleCodeNameComparer());
diff_match_patch dmp = new();
foreach(UndertaleCode code in common)
{
codeRef = reference.Code.First(t => t.Name.Content == code.Name.Content);
if (CompareUndertaleCode(ms, burstSerializer, code, codeRef)) continue;
try
{
strName = Decompiler.Decompile(code, contextName);
strRef = Decompiler.Decompile(codeRef, contextRef);
}
catch(Exception ex)
{
if (!ex.Message.Contains("This code block represents a function nested inside"))
{
try
{
strName = code.Disassemble(name.Variables, name.CodeLocals.For(code));
strRef = codeRef.Disassemble(reference.Variables, reference.CodeLocals.For(codeRef));
}
catch (Exception ex2)
{
Console.WriteLine($@"{ex2.GetType()}: {ex2.Message}");
}
}
}
List<Diff> diff = dmp.diff_main(strRef, strName);
if (diff.Count == 0 || (diff.Count == 1 && diff[0].operation == Operation.EQUAL)) continue;
string report = dmp.diff_prettyHtml(diff);
File.WriteAllText(Path.Join(dirModifiedCode.FullName, Path.DirectorySeparatorChar.ToString(), $"{code.Name.Content}.html"), report);
}
}
public static void DiffCodes(UndertaleData name, UndertaleData reference, DirectoryInfo outputFolder)
{
AddedRemovedCodes(name, reference, outputFolder);
ModifiedCodes(name, reference, outputFolder);
}
private static void AddedRemovedObjects(UndertaleData name, UndertaleData reference, DirectoryInfo outputFolder)
{
DirectoryInfo dirAddedObject = new(Path.Join(outputFolder.FullName, Path.DirectorySeparatorChar.ToString(), "AddedGameObjects"));
dirAddedObject.Create();
IEnumerable<UndertaleGameObject> added = name.GameObjects.Except(reference.GameObjects, new UndertaleGameObjectNameComparer());
IEnumerable<UndertaleGameObject> removed = reference.GameObjects.Except(name.GameObjects, new UndertaleGameObjectNameComparer());
using (StreamWriter sw = new(Path.Join(outputFolder.FullName, Path.DirectorySeparatorChar.ToString(), $"addedGameObjects.txt")))
{
foreach(UndertaleGameObject ob in added)
{
sw.WriteLine(ob.Name.Content);
GameObjectSummary gameObjectSummary = new()
{
name = ob.Name.Content,
spriteName = ob.Sprite?.Name.Content ?? "",
parentName = ob.ParentId?.Name.Content ?? "",
isVisible = ob.Visible,
isPersistent = ob.Persistent,
isAwake = ob.Awake,
collisionShapeFlags = ob.CollisionShape.ToString(),
Events = ob.Events.Enumerate().SelectMany(x => x.Item2.Select(y => (((EventType)x.Item1).ToString(), (int)y.EventSubtype))).ToList(),
};
File.WriteAllText(Path.Join(dirAddedObject.FullName, Path.DirectorySeparatorChar.ToString(), $"{ob.Name.Content}.json"),
JsonConvert.SerializeObject(gameObjectSummary)
);
}
}
File.WriteAllLines(Path.Join(outputFolder.FullName, Path.DirectorySeparatorChar.ToString(), $"removedGameObjects.txt"), removed.Select(x => x.Name.Content));
}
private static void ModifiedObjects(UndertaleData name, UndertaleData reference, DirectoryInfo outputFolder)
{
diff_match_patch dmp = new();
UndertaleGameObjectComparer comparer = new();
DirectoryInfo dirModifiedObject = new(Path.Join(outputFolder.FullName, Path.DirectorySeparatorChar.ToString(), "ModifiedObjects"));
dirModifiedObject.Create();
UndertaleGameObject obRef;
string strName = "";
string strRef = "";
IEnumerable<UndertaleGameObject> common = name.GameObjects.Intersect(reference.GameObjects, new UndertaleGameObjectNameComparer());
foreach(UndertaleGameObject ob in common)
{
obRef = reference.GameObjects.First(t => t.Name.Content == ob.Name.Content);
if (comparer.Equals(ob, obRef)) continue;
strName = JsonConvert.SerializeObject(
new GameObjectSummary()
{
name = ob.Name.Content,
spriteName = ob.Sprite?.Name.Content ?? "",
parentName = ob.ParentId?.Name.Content ?? "",
isVisible = ob.Visible,
isPersistent = ob.Persistent,
isAwake = ob.Awake,
collisionShapeFlags = ob.CollisionShape.ToString(),
Events = ob.Events.Enumerate().SelectMany(x => x.Item2.Select(y => (((EventType)x.Item1).ToString(), (int)y.EventSubtype))).ToList(),
}
);
strRef = JsonConvert.SerializeObject(
new GameObjectSummary()
{
name = obRef.Name.Content,
spriteName = obRef.Sprite?.Name.Content ?? "",
parentName = obRef.ParentId?.Name.Content ?? "",
isVisible = obRef.Visible,
isPersistent = obRef.Persistent,
isAwake = obRef.Awake,
collisionShapeFlags = obRef.CollisionShape.ToString(),
Events = ob.Events.Enumerate().SelectMany(x => x.Item2.Select(y => (((EventType)x.Item1).ToString(), (int)y.EventSubtype))).ToList(),
}
);
List<Diff> diff = dmp.diff_main(strRef, strName);
if (diff.Count == 0 || (diff.Count == 1 && diff[0].operation == Operation.EQUAL)) continue;
string report = dmp.diff_prettyHtml(diff);
File.WriteAllText(Path.Join(dirModifiedObject.FullName, Path.DirectorySeparatorChar.ToString(), $"{ob.Name.Content}.html"), report);
}
}
public static void DiffObjects(UndertaleData name, UndertaleData reference, DirectoryInfo outputFolder)
{
AddedRemovedObjects(name, reference, outputFolder);
ModifiedObjects(name, reference, outputFolder);
}
public static void DiffRooms(UndertaleData name, UndertaleData reference, DirectoryInfo outputFolder)
{
IEnumerable<string> added = name.Rooms.Select(x => x.Name.Content).Except(reference.Rooms.Select(x => x.Name.Content));
IEnumerable<string> removed = reference.Rooms.Select(x => x.Name.Content).Except(name.Rooms.Select(x => x.Name.Content));
ExportDiffs(added, removed, "Rooms", outputFolder);
}
public static void DiffSounds(UndertaleData name, UndertaleData reference, DirectoryInfo outputFolder)
{
IEnumerable<string> added = name.Sounds.Select(x => x.Name.Content).Except(reference.Sounds.Select(x => x.Name.Content));
IEnumerable<string> removed = reference.Sounds.Select(x => x.Name.Content).Except(name.Sounds.Select(x => x.Name.Content));
ExportDiffs(added, removed, "Sounds", outputFolder);
}
private static void AddedRemovedSprites(UndertaleData name, UndertaleData reference, DirectoryInfo outputFolder, TextureWorker worker)
{
DirectoryInfo dirAddedSprite = new(Path.Join(outputFolder.FullName, Path.DirectorySeparatorChar.ToString(), "AddedSprites"));
dirAddedSprite.Create();
IEnumerable<UndertaleSprite> added = name.Sprites.Except(reference.Sprites, new UndertaleSpriteNameComparer());
IEnumerable<UndertaleSprite> removed = reference.Sprites.Except(name.Sprites, new UndertaleSpriteNameComparer());
using StreamWriter swOrigin = new(Path.Join(outputFolder.FullName, Path.DirectorySeparatorChar.ToString(), $"addedSpritesOrigin.txt"));
using (StreamWriter sw = new(Path.Join(outputFolder.FullName, Path.DirectorySeparatorChar.ToString(), $"addedSprites.txt")))
{
foreach(UndertaleSprite sprite in added)
{
sw.WriteLine(sprite.Name.Content);
for (int i = 0; i < sprite.Textures.Count; i++)
{
if (sprite.Textures[i]?.Texture is not null)
{
worker.ExportAsPNG(sprite.Textures[i].Texture, Path.Combine(dirAddedSprite.FullName , sprite.Name.Content + "_" + i + ".png"), null, true);
}
}
swOrigin.WriteLine($"Msl.GetSprite(\"{sprite.Name.Content}\").OriginX = {sprite.OriginX};\nMsl.GetSprite(\"{sprite.Name.Content}\").OriginY = {sprite.OriginY};");
}
}
File.WriteAllLines(Path.Join(outputFolder.FullName, Path.DirectorySeparatorChar.ToString(), $"removedSprites.txt"), removed.Select(x => x.Name.Content));
}
private static void ModifiedSprites(UndertaleData name, UndertaleData reference, DirectoryInfo outputFolder, TextureWorker worker)
{
DirectoryInfo dirModifiedSprite = new(Path.Join(outputFolder.FullName, Path.DirectorySeparatorChar.ToString(), "ModifiedSprites"));
dirModifiedSprite.Create();
IEnumerable<UndertaleSprite> common = name.Sprites.Intersect(reference.Sprites, new UndertaleSpriteNameComparer());
int minCount = 0;
foreach(UndertaleSprite sprite in common)
{
UndertaleSprite spriteRef = reference.Sprites.First(t => t.Name.Content == sprite.Name.Content);
minCount = sprite.Textures.Count < spriteRef.Textures.Count ? sprite.Textures.Count : spriteRef.Textures.Count;
for (int i = 0; i < minCount; i++)
{
if (sprite.Textures[i]?.Texture is not null)
{
if(UnsafeCompare(sprite.Textures[i].Texture.TexturePage.TextureData.TextureBlob, spriteRef.Textures[i].Texture.TexturePage.TextureData.TextureBlob)) continue;
worker.ExportAsPNG(sprite.Textures[i].Texture, Path.Combine(dirModifiedSprite.FullName , sprite.Name.Content + "_" + i + ".png"), null, true);
}
}
for (int i = minCount; i < sprite.Textures.Count; i++)
{
if (sprite.Textures[i]?.Texture is not null)
{
worker.ExportAsPNG(sprite.Textures[i].Texture, Path.Combine(dirModifiedSprite.FullName , sprite.Name.Content + "_" + i + ".png"), null, true);
}
}
}
}
public static void DiffSprites(UndertaleData name, UndertaleData reference, DirectoryInfo outputFolder)
{
TextureWorker worker = new();
AddedRemovedSprites(name, reference, outputFolder, worker);
ModifiedSprites(name, reference, outputFolder, worker);
}
public static void DiffTexturePageItems(UndertaleData name, UndertaleData reference, DirectoryInfo outputFolder)
{
IEnumerable<string> added = name.TexturePageItems.Select(x => x.Name.Content).Except(reference.TexturePageItems.Select(x => x.Name.Content));
IEnumerable<string> removed = reference.TexturePageItems.Select(x => x.Name.Content).Except(name.TexturePageItems.Select(x => x.Name.Content));
ExportDiffs(added, removed, "TexturePageItems", outputFolder);
}
}