0.2.0-dev Download ZIP
39 lines ·1.1 KB Raw
| 1 | using System.Text; |
| 2 | using EvilEngine.Mounts; |
| 3 | |
| 4 | namespace EvilEngine.Adapters.Hpl2; |
| 5 | |
| 6 | public static class Hpl2Xml |
| 7 | { |
| 8 | private static readonly byte[] Bom = [0xEF, 0xBB, 0xBF]; |
| 9 | |
| 10 | public static GameXmlDocument? Read(IFileSource tree, VirtualPath path) |
| 11 | { |
| 12 | return tree.TryOpen(path, out var stream) ? Parse(Text(stream)) : null; |
| 13 | } |
| 14 | |
| 15 | public static GameXmlDocument? Parse(string text) |
| 16 | { |
| 17 | var document = GameXml.Parse(text); |
| 18 | return document.Error is null ? document : null; |
| 19 | } |
| 20 | |
| 21 | public static GameXmlDocument? Parse(byte[] bytes) |
| 22 | { |
| 23 | var text = bytes.AsSpan(); |
| 24 | if (text.StartsWith(Bom)) |
| 25 | text = text[Bom.Length..]; |
| 26 | return Parse(Encoding.UTF8.GetString(text)); |
| 27 | } |
| 28 | |
| 29 | public static string? Attribute(GameXmlDocument document, string element, string attribute) |
| 30 | { |
| 31 | return document.Elements.FirstOrDefault(found => found.Name == element)?[attribute]; |
| 32 | } |
| 33 | |
| 34 | private static string Text(Stream stream) |
| 35 | { |
| 36 | using var reader = new StreamReader(stream); |
| 37 | return reader.ReadToEnd(); |
| 38 | } |
| 39 | } |