Skip to content

Commit 2f01d78

Browse files
20kdcDrSmugleafPJB3005
authored
Mapping merge driver: continued (space-wizards#2803)
Co-authored-by: DrSmugleaf <[email protected]> Co-authored-by: Pieter-Jan Briers <[email protected]>
1 parent 5cbb3f1 commit 2f01d78

14 files changed

+1026
-0
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#*.modelproj merge=binary
3535
#*.sqlproj merge=binary
3636
#*.wwaproj merge=binary
37+
Resources/Maps/**.yml merge=mapping-merge-driver
3738

3839
###############################################################################
3940
# behavior for image files

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,7 @@ BuildFiles/Windows/Godot/*
284284
# Windows image file caches
285285
Thumbs.db
286286
ehthumbs.db
287+
288+
# Merge driver stuff
289+
Content.Tools/test/out.yml
290+

Content.Tools/Content.Tools.csproj

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="YamlDotNet" Version="9.1.0" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\RobustToolbox\Robust.Shared\Robust.Shared.csproj" />
14+
</ItemGroup>
15+
16+
</Project>

Content.Tools/Map.cs

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System.IO;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Linq;
5+
using YamlDotNet.Core;
6+
using Robust.Shared.Utility;
7+
using YamlDotNet.RepresentationModel;
8+
9+
namespace Content.Tools
10+
{
11+
public class Map
12+
{
13+
public Map(string path)
14+
{
15+
Path = path;
16+
17+
using var reader = new StreamReader(path);
18+
var stream = new YamlStream();
19+
20+
stream.Load(reader);
21+
22+
Root = stream.Documents[0].RootNode;
23+
TilemapNode = (YamlMappingNode) Root["tilemap"];
24+
GridsNode = (YamlSequenceNode) Root["grids"];
25+
_entitiesNode = (YamlSequenceNode) Root["entities"];
26+
27+
foreach (var entity in _entitiesNode)
28+
{
29+
var uid = uint.Parse(entity["uid"].AsString());
30+
if (uid >= NextAvailableEntityId)
31+
NextAvailableEntityId = uid + 1;
32+
Entities[uid] = (YamlMappingNode) entity;
33+
}
34+
}
35+
36+
// Core
37+
38+
public string Path { get; }
39+
40+
public YamlNode Root { get; }
41+
42+
// Useful
43+
44+
public YamlMappingNode TilemapNode { get; }
45+
46+
public YamlSequenceNode GridsNode { get; }
47+
48+
// Entities lookup
49+
50+
private YamlSequenceNode _entitiesNode { get; }
51+
52+
public Dictionary<uint, YamlMappingNode> Entities { get; } = new Dictionary<uint, YamlMappingNode>();
53+
54+
public uint MaxId => Entities.Max(entry => entry.Key);
55+
56+
public uint NextAvailableEntityId { get; set; }
57+
58+
// ----
59+
60+
public void Save(string fileName)
61+
{
62+
// Update entities node
63+
_entitiesNode.Children.Clear();
64+
foreach (var kvp in Entities)
65+
_entitiesNode.Add(kvp.Value);
66+
67+
using var writer = new StreamWriter(fileName);
68+
var document = new YamlDocument(Root);
69+
var stream = new YamlStream(document);
70+
var emitter = new Emitter(writer);
71+
var fixer = new TypeTagPreserver(emitter);
72+
73+
stream.Save(fixer, false);
74+
75+
writer.Flush();
76+
}
77+
78+
public void Save()
79+
{
80+
Save(Path);
81+
}
82+
}
83+
}

Content.Tools/MappingMergeDriver.cs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Linq;
5+
using YamlDotNet.Core;
6+
using YamlDotNet.RepresentationModel;
7+
8+
namespace Content.Tools
9+
{
10+
internal static class MappingMergeDriver
11+
{
12+
/// %A: Our file
13+
/// %O: Origin (common, base) file
14+
/// %B: Other file
15+
/// %P: Actual filename of the resulting file
16+
public static void Main(string[] args)
17+
{
18+
var ours = new Map(args[0]);
19+
var based = new Map(args[1]); // On what?
20+
var other = new Map(args[2]);
21+
22+
if ((ours.GridsNode.Children.Count != 1) || (based.GridsNode.Children.Count != 1) || (other.GridsNode.Children.Count != 1))
23+
{
24+
Console.WriteLine("one or more files had an amount of grids not equal to 1");
25+
Environment.Exit(1);
26+
}
27+
28+
if (!(new Merger(ours, based, other).Merge()))
29+
{
30+
Console.WriteLine("unable to merge!");
31+
Environment.Exit(1);
32+
}
33+
34+
ours.Save();
35+
Environment.Exit(0);
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)