-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMerger.cs
354 lines (323 loc) · 13.7 KB
/
Merger.cs
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
using System;
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using YamlDotNet.RepresentationModel;
namespace Content.Tools
{
public sealed class Merger
{
public Map MapOurs { get; }
public Map MapBased { get; }
public Map MapOther { get; }
public Dictionary<uint, uint> TileMapFromOtherToOurs { get; } = new Dictionary<uint, uint>();
public Dictionary<uint, uint> TileMapFromBasedToOurs { get; } = new Dictionary<uint, uint>();
public Dictionary<uint, uint> EntityMapFromOtherToOurs { get; } = new Dictionary<uint, uint>();
public List<uint> EntityListDirectMerge { get; } = new List<uint>();
private const int ExpectedChunkSize = 16 * 16 * 4;
public Merger(Map ours, Map based, Map other)
{
MapOurs = ours;
MapBased = based;
MapOther = other;
}
public bool Merge()
{
PlanTileMapping(TileMapFromOtherToOurs, MapOther);
PlanTileMapping(TileMapFromBasedToOurs, MapBased);
MergeTiles();
PlanEntityMapping();
return MergeEntities();
}
// -- Tiles --
public void PlanTileMapping(Dictionary<uint, uint> relativeOtherToOurs, Map relativeOther)
{
var mapping = new Dictionary<string, uint>();
uint nextAvailable = 0;
foreach (var kvp in MapOurs.TilemapNode)
{
var k = uint.Parse(kvp.Key.ToString());
var v = kvp.Value.ToString();
mapping[v] = k;
if (k >= nextAvailable)
nextAvailable = k + 1;
}
foreach (var kvp in relativeOther.TilemapNode)
{
var k = uint.Parse(kvp.Key.ToString());
var v = kvp.Value.ToString();
if (mapping.ContainsKey(v))
{
relativeOtherToOurs[k] = mapping[v];
}
else
{
MapOurs.TilemapNode.Add(nextAvailable.ToString(CultureInfo.InvariantCulture), v);
relativeOtherToOurs[k] = nextAvailable++;
}
}
}
public void MergeTiles()
{
var a = MapOurs.GridsNode.Children[0];
var b = MapBased.GridsNode.Children[0];
var c = MapOther.GridsNode.Children[0];
var aChunks = a["chunks"];
var bChunks = b["chunks"];
var cChunks = c["chunks"];
MergeTileChunks((YamlSequenceNode) aChunks, (YamlSequenceNode) bChunks, (YamlSequenceNode) cChunks);
}
public void MergeTileChunks(YamlSequenceNode aChunks, YamlSequenceNode bChunks, YamlSequenceNode cChunks)
{
var aMap = ConvertTileChunks(aChunks);
var bMap = ConvertTileChunks(bChunks);
var cMap = ConvertTileChunks(cChunks);
var xMap = new HashSet<string>();
foreach (var kvp in aMap)
xMap.Add(kvp.Key);
// don't include b because that would mess with chunk deletion
foreach (var kvp in cMap)
xMap.Add(kvp.Key);
foreach (var ind in xMap)
{
using var a = new MemoryStream(GetChunkBytes(aMap, ind));
using var b = new MemoryStream(GetChunkBytes(bMap, ind));
using var c = new MemoryStream(GetChunkBytes(cMap, ind));
using var aR = new BinaryReader(a);
using var bR = new BinaryReader(b);
using var cR = new BinaryReader(c);
var outB = new byte[ExpectedChunkSize];
{
using (var outS = new MemoryStream(outB))
using (var outW = new BinaryWriter(outS))
for (var i = 0; i < ExpectedChunkSize; i += 4)
{
var aI = aR.ReadUInt32();
var bI = MapTileId(bR.ReadUInt32(), TileMapFromBasedToOurs);
var cI = MapTileId(cR.ReadUInt32(), TileMapFromOtherToOurs);
// cI needs translation.
uint result = aI;
if (aI == bI)
{
// If aI == bI then aI did not change anything, so cI always wins
result = cI;
}
else if (bI != cI)
{
// If bI != cI then cI definitely changed something (conflict, but overrides aI)
result = cI;
Console.WriteLine("WARNING: Tile (" + ind + ")[" + i + "] was changed by both branches.");
}
outW.Write(result);
}
}
// Actually output chunk
if (!aMap.ContainsKey(ind))
{
var res = new YamlMappingNode();
res.Children["ind"] = ind;
aMap[ind] = res;
}
aMap[ind].Children["tiles"] = Convert.ToBase64String(outB);
}
}
public uint MapTileId(uint src, Dictionary<uint, uint> mapping)
{
return (src & 0xFFFF0000) | mapping[src & 0xFFFF];
}
public Dictionary<string, YamlMappingNode> ConvertTileChunks(YamlSequenceNode chunks)
{
var map = new Dictionary<string, YamlMappingNode>();
foreach (var chunk in chunks)
map[chunk["ind"].ToString()] = (YamlMappingNode) chunk;
return map;
}
public byte[] GetChunkBytes(Dictionary<string, YamlMappingNode> chunks, string ind)
{
if (!chunks.ContainsKey(ind))
return new byte[ExpectedChunkSize];
return Convert.FromBase64String(chunks[ind]["tiles"].ToString());
}
// -- Entities --
public void PlanEntityMapping()
{
// Ok, so here's how it works:
// 1. Entities that do not exist in "based" are additions.
// 2. Entities that exist in "based" but do not exist in the one map or the other are removals.
// Find modifications and deletions
foreach (var kvp in MapBased.Entities)
{
var deletedByOurs = !MapOurs.Entities.ContainsKey(kvp.Key);
var deletedByOther = !MapOther.Entities.ContainsKey(kvp.Key);
if (deletedByOther && !deletedByOurs)
{
// Delete
MapOurs.Entities.Remove(kvp.Key);
}
else if (!(deletedByOurs || deletedByOther))
{
// Modify
EntityMapFromOtherToOurs[kvp.Key] = kvp.Key;
}
}
// Find additions
foreach (var kvp in MapOther.Entities)
{
if (!MapBased.Entities.ContainsKey(kvp.Key))
{
// New
var newId = MapOurs.NextAvailableEntityId++;
EntityMapFromOtherToOurs[kvp.Key] = newId;
}
}
}
public bool MergeEntities()
{
bool success = true;
foreach (var kvp in EntityMapFromOtherToOurs)
{
// For debug use.
// Console.WriteLine("Entity C/" + kvp.Key + " -> A/" + kvp.Value);
YamlMappingNode oursEnt;
if (MapOurs.Entities.ContainsKey(kvp.Value))
{
oursEnt = MapOurs.Entities[kvp.Value];
if (!MapBased.Entities.TryGetValue(kvp.Value, out var basedEnt))
{
basedEnt = oursEnt;
}
if (!MergeEntityNodes(oursEnt, basedEnt, MapOther.Entities[kvp.Key]))
{
Console.WriteLine("Unable to successfully merge entity C/" + kvp.Key);
success = false;
}
}
else
{
oursEnt = (YamlMappingNode) YamlTools.CopyYamlNodes(MapOther.Entities[kvp.Key]);
if (!MapEntity(oursEnt)) {
Console.WriteLine("Unable to successfully import entity C/" + kvp.Key);
success = false;
} else {
MapOurs.Entities[kvp.Value] = oursEnt;
}
}
oursEnt.Children["uid"] = kvp.Value.ToString(CultureInfo.InvariantCulture);
}
return success;
}
public bool MergeEntityNodes(YamlMappingNode ours, YamlMappingNode based, YamlMappingNode other)
{
// Copy to intermediate
var otherMapped = (YamlMappingNode) YamlTools.CopyYamlNodes(other);
if (!MapEntity(otherMapped))
return false;
// Merge stuff that isn't components
var path = "Entity" + (other["uid"].ToString());
YamlTools.MergeYamlMappings(ours, based, otherMapped, path, new string[] {"components"});
// Components are special
var ourComponents = new Dictionary<string, YamlMappingNode>();
var basedComponents = new Dictionary<string, YamlMappingNode>();
var ourComponentsNode = (YamlSequenceNode) ours["components"];
var basedComponentsNode = (YamlSequenceNode) based["components"];
var otherComponentsNode = (YamlSequenceNode) otherMapped["components"];
foreach (var component in ourComponentsNode)
{
var name = component["type"].ToString();
ourComponents[name] = (YamlMappingNode) component;
}
foreach (var component in basedComponentsNode)
{
var name = component["type"].ToString();
basedComponents[name] = (YamlMappingNode) component;
}
foreach (var otherComponent in otherComponentsNode)
{
var name = otherComponent["type"].ToString();
if (ourComponents.ContainsKey(name))
{
var ourComponent = ourComponents[name];
if (!basedComponents.TryGetValue(name, out var basedComponent))
basedComponent = new YamlMappingNode();
YamlTools.MergeYamlNodes(ourComponent, basedComponent, otherComponent, path + "/components/" + name);
}
else
{
ourComponentsNode.Add(otherComponent);
}
}
return true;
}
public bool MapEntity(YamlMappingNode other)
{
var path = "Entity" + (other["uid"].ToString());
if (other.Children.ContainsKey("components"))
{
var components = (YamlSequenceNode) other["components"];
foreach (var component in components)
{
var type = component["type"].ToString();
if (type == "Transform")
{
if (!MapEntityProperty((YamlMappingNode) component, "parent", path))
return false;
}
else if (type == "ContainerContainer")
{
MapEntityRecursiveAndBadly(component, path);
}
}
}
return true;
}
public bool MapEntityProperty(YamlMappingNode node, string property, string path)
{
if (node.Children.ContainsKey(property)) {
var prop = node[property];
if (prop is YamlScalarNode)
return MapEntityProperty((YamlScalarNode) prop, path + "/" + property);
}
return true;
}
public bool MapEntityProperty(YamlScalarNode node, string path)
{
if (uint.TryParse(node.ToString(), out var uid))
{
if (EntityMapFromOtherToOurs.ContainsKey(uid))
{
node.Value = EntityMapFromOtherToOurs[uid].ToString(CultureInfo.InvariantCulture);
}
else
{
Console.WriteLine($"Error finding UID in MapEntityRecursiveAndBadly {path}. To fix this, the merge driver needs to be improved.");
return false;
}
}
return true;
}
public bool MapEntityRecursiveAndBadly(YamlNode node, string path)
{
switch (node)
{
case YamlSequenceNode subSequence:
var idx = 0;
foreach (var val in subSequence)
if (!MapEntityRecursiveAndBadly(val, path + "/" + (idx++)))
return false;
return true;
case YamlMappingNode subMapping:
foreach (var kvp in subMapping)
if (!MapEntityRecursiveAndBadly(kvp.Key, path))
return false;
foreach (var kvp in subMapping)
if (!MapEntityRecursiveAndBadly(kvp.Value, path + "/" + kvp.Key.ToString()))
return false;
return true;
case YamlScalarNode subScalar:
return MapEntityProperty(subScalar, path);
default:
throw new ArgumentException($"Unrecognized YAML node type: {node.GetType()} at {path}");
}
}
}
}