-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMap.cs
82 lines (60 loc) · 2.11 KB
/
Map.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
using System.IO;
using System.Collections.Generic;
using System.Linq;
using YamlDotNet.Core;
using Robust.Shared.Utility;
using YamlDotNet.RepresentationModel;
namespace Content.Tools
{
public sealed class Map
{
public Map(string path)
{
Path = path;
using var reader = new StreamReader(path);
var stream = new YamlStream();
stream.Load(reader);
Root = stream.Documents[0].RootNode;
TilemapNode = (YamlMappingNode) Root["tilemap"];
GridsNode = (YamlSequenceNode) Root["grids"];
EntitiesNode = (YamlSequenceNode) Root["entities"];
foreach (var entity in EntitiesNode)
{
var uid = uint.Parse(entity["uid"].AsString());
if (uid >= NextAvailableEntityId)
NextAvailableEntityId = uid + 1;
Entities[uid] = (YamlMappingNode) entity;
}
}
// Core
public string Path { get; }
public YamlNode Root { get; }
// Useful
public YamlMappingNode TilemapNode { get; }
public YamlSequenceNode GridsNode { get; }
// Entities lookup
private YamlSequenceNode EntitiesNode { get; }
public Dictionary<uint, YamlMappingNode> Entities { get; } = new Dictionary<uint, YamlMappingNode>();
public uint MaxId => Entities.Max(entry => entry.Key);
public uint NextAvailableEntityId { get; set; }
// ----
public void Save(string fileName)
{
// Update entities node
EntitiesNode.Children.Clear();
foreach (var kvp in Entities)
EntitiesNode.Add(kvp.Value);
using var writer = new StreamWriter(fileName);
var document = new YamlDocument(Root);
var stream = new YamlStream(document);
var emitter = new Emitter(writer);
var fixer = new TypeTagPreserver(emitter);
stream.Save(fixer, false);
writer.Flush();
}
public void Save()
{
Save(Path);
}
}
}