Skip to content

Commit e0f4a27

Browse files
committed
Implemented #97, removed custom skins handeling for now, made custom json converters public
1 parent 7e89b2a commit e0f4a27

1 file changed

Lines changed: 107 additions & 0 deletions

File tree

src/Json/JsonMerger.cs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using Il2CppSystem.IO;
2+
using Newtonsoft.Json.Linq;
3+
4+
namespace PolyMod.Json;
5+
public static class JsonMerger
6+
{
7+
public static JObject Merge(JObject original, JObject patch)
8+
{
9+
foreach (var property in patch.Properties().ToArray().ToList())
10+
{
11+
if (property == null || property.Name == null)
12+
continue;
13+
14+
string propName = property.Name;
15+
JToken? originalValue = original[propName];
16+
JToken patchValue = property.Value;
17+
18+
if (originalValue == null)
19+
{
20+
original[propName] = patchValue;
21+
continue;
22+
}
23+
24+
JObject? originalObj = originalValue.TryCast<JObject>();
25+
JObject? patchObj = patchValue.TryCast<JObject>();
26+
if (originalObj != null && patchObj != null)
27+
{
28+
Merge(originalObj, patchObj);
29+
continue;
30+
}
31+
32+
JArray? originalArr = originalValue.TryCast<JArray>();
33+
JArray? patchArr = patchValue.TryCast<JArray>();
34+
if (originalArr != null && patchArr != null)
35+
{
36+
bool isSkins = propName.Equals("skins", StringComparison.OrdinalIgnoreCase);
37+
JArray merged = MergeArrays(originalArr, patchArr, isSkins);
38+
original[propName] = merged;
39+
continue;
40+
}
41+
original[propName] = patchValue;
42+
}
43+
44+
return original;
45+
}
46+
47+
private static JArray MergeArrays(JArray original, JArray patch, bool isSkins)
48+
{
49+
var result = new JArray(original);
50+
var patchList = patch._values.ToArray().ToList();
51+
52+
bool hasDirectValues = patchList.Any(v =>
53+
v.Type == JTokenType.String &&
54+
!v.ToString().StartsWith("+") &&
55+
!v.ToString().StartsWith("-"));
56+
57+
if (!isSkins && hasDirectValues)
58+
{
59+
result = new JArray();
60+
}
61+
62+
foreach (var token in patchList)
63+
{
64+
if (token.Type != JTokenType.String)
65+
{
66+
result.Add(token);
67+
continue;
68+
}
69+
70+
string str = token.ToString();
71+
72+
if (str.StartsWith("+"))
73+
{
74+
string value = str.Substring(1);
75+
if (!result._values.ToArray().Any(t => t.Type == JTokenType.String && t.ToString() == value))
76+
{
77+
result.Add(value);
78+
}
79+
}
80+
else if (str.StartsWith("-"))
81+
{
82+
string value = str.Substring(1);
83+
var toRemove = result._values.ToArray()
84+
.Where(t => t.Type == JTokenType.String && t.ToString() == value)
85+
.ToList();
86+
foreach (var rem in toRemove)
87+
result.Remove(rem);
88+
}
89+
else
90+
{
91+
if (isSkins)
92+
{
93+
if (!result._values.ToArray().Any(t => t.Type == JTokenType.String && t.ToString() == str))
94+
{
95+
result.Add(str);
96+
}
97+
}
98+
else
99+
{
100+
result.Add(str);
101+
}
102+
}
103+
}
104+
105+
return result;
106+
}
107+
}

0 commit comments

Comments
 (0)