|
| 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 | +} |
0 commit comments