Skip to content

Commit c5ef560

Browse files
committed
[Minor] export added gameobjects as json
1 parent e55ba59 commit c5ef560

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

DiffUtils.cs

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using Serilog;
21
using UndertaleModLib;
32
using UndertaleModLib.Models;
43
using DiffMatchPatch;
54
using UndertaleModLib.Decompiler;
65
using UndertaleModLib.Util;
7-
using System.Runtime.Serialization.Formatters.Binary;
86
using Polenter.Serialization;
7+
using Newtonsoft.Json;
98

109
namespace ModShardDiff;
1110

@@ -45,6 +44,35 @@ public int GetHashCode(UndertaleSprite x)
4544
return x.Name.Content.GetHashCode();
4645
}
4746
}
47+
class UndertaleGameObjectComparer : IEqualityComparer<UndertaleGameObject>
48+
{
49+
public bool Equals(UndertaleGameObject? x, UndertaleGameObject? y)
50+
{
51+
if (x == null || y == null) return false;
52+
return x.Name.Content == y.Name.Content;
53+
}
54+
55+
// If Equals() returns true for a pair of objects
56+
// then GetHashCode() must return the same value for these objects.
57+
58+
public int GetHashCode(UndertaleGameObject x)
59+
{
60+
//Check whether the object is null
61+
if (x == null) return 0;
62+
return x.Name.Content.GetHashCode();
63+
}
64+
}
65+
public class GameObjectSummary
66+
{
67+
public string name = "";
68+
public string spriteName = "";
69+
public string parentName = "";
70+
public bool isVisible;
71+
public bool isPersistent;
72+
public bool isAwake;
73+
public string collisionShapeFlags = "";
74+
}
75+
4876
static public class DiffUtils
4977
{
5078
// thanks to Pong to acknowledge me that possibility
@@ -75,7 +103,7 @@ private static unsafe bool UnsafeCompare(byte[] a1, byte[] a2)
75103
return true;
76104
}
77105
}
78-
private static bool CompareUndertaleCode(MemoryStream ms, SharpSerializer burstSerializer, UndertaleCode codeRef, UndertaleCode code)
106+
private static bool CompareUndertaleCode(MemoryStream ms, SharpSerializer burstSerializer, UndertaleCode code, UndertaleCode codeRef)
79107
{
80108
ms.SetLength(0);
81109
burstSerializer.Serialize(code, ms);
@@ -173,11 +201,39 @@ public static void DiffCodes(UndertaleData name, UndertaleData reference, Direct
173201
AddedRemovedCodes(name, reference, outputFolder);
174202
ModifiedCodes(name, reference, outputFolder);
175203
}
204+
private static void AddedRemovedObjects(UndertaleData name, UndertaleData reference, DirectoryInfo outputFolder)
205+
{
206+
DirectoryInfo dirAddedObject = new(Path.Join(outputFolder.FullName, Path.DirectorySeparatorChar.ToString(), "AddedGameObjects"));
207+
dirAddedObject.Create();
208+
209+
IEnumerable<UndertaleGameObject> added = name.GameObjects.Except(reference.GameObjects, new UndertaleGameObjectComparer());
210+
IEnumerable<UndertaleGameObject> removed = reference.GameObjects.Except(name.GameObjects, new UndertaleGameObjectComparer());
211+
using (StreamWriter sw = new(Path.Join(outputFolder.FullName, Path.DirectorySeparatorChar.ToString(), $"addedGameObjects.txt")))
212+
{
213+
foreach(UndertaleGameObject ob in added)
214+
{
215+
sw.WriteLine(ob.Name.Content);
216+
File.WriteAllText(Path.Join(dirAddedObject.FullName, Path.DirectorySeparatorChar.ToString(), $"{ob.Name.Content}.json"),
217+
JsonConvert.SerializeObject(
218+
new GameObjectSummary()
219+
{
220+
name = ob.Name.Content,
221+
spriteName = ob.Sprite?.Name.Content ?? "",
222+
parentName = ob.ParentId?.Name.Content ?? "",
223+
isVisible = ob.Visible,
224+
isPersistent = ob.Persistent,
225+
isAwake = ob.Awake,
226+
collisionShapeFlags = ob.CollisionShape.ToString(),
227+
}
228+
)
229+
);
230+
}
231+
}
232+
File.WriteAllLines(Path.Join(outputFolder.FullName, Path.DirectorySeparatorChar.ToString(), $"removedGameObjects.txt"), removed.Select(x => x.Name.Content));
233+
}
176234
public static void DiffObjects(UndertaleData name, UndertaleData reference, DirectoryInfo outputFolder)
177235
{
178-
IEnumerable<string> added = name.GameObjects.Select(x => x.Name.Content).Except(reference.GameObjects.Select(x => x.Name.Content));
179-
IEnumerable<string> removed = reference.GameObjects.Select(x => x.Name.Content).Except(name.GameObjects.Select(x => x.Name.Content));
180-
ExportDiffs(added, removed, "GameObjects", outputFolder);
236+
AddedRemovedObjects(name, reference, outputFolder);
181237
}
182238
public static void DiffRooms(UndertaleData name, UndertaleData reference, DirectoryInfo outputFolder)
183239
{

ModShardDiff.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<PackageReference Include="SharpSerializer" Version="4.0.2"/>
3737
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1"/>
3838
<PackageReference Include="System.Drawing.Common" Version="8.0.1"/>
39+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
3940
</ItemGroup>
4041

4142
<ItemGroup>

0 commit comments

Comments
 (0)