0.2.0-dev Download ZIP
52 lines ·1.7 KB Raw
| 1 | using EvilEngine.Mounts; |
| 2 | |
| 3 | namespace EvilEngine.Adapters.ScpContainmentBreach; |
| 4 | |
| 5 | public sealed record IniSection(string Name, IReadOnlyDictionary<string, string> Values) |
| 6 | { |
| 7 | public string? this[string key] => Values.GetValueOrDefault(key); |
| 8 | } |
| 9 | |
| 10 | public static class Ini |
| 11 | { |
| 12 | public static IReadOnlyList<IniSection> Read(IFileSource files, VirtualPath path) |
| 13 | { |
| 14 | return files.Exists(path) ? Parse(files.ReadAllText(path)) : []; |
| 15 | } |
| 16 | |
| 17 | public static IReadOnlyList<IniSection> Parse(string text) |
| 18 | { |
| 19 | var sections = new List<IniSection>(); |
| 20 | Dictionary<string, string>? values = null; |
| 21 | foreach (var raw in text.Split('\n')) |
| 22 | { |
| 23 | var line = raw.Trim().TrimStart(''); |
| 24 | if (line.Length == 0 || line[0] is ';' or '#') |
| 25 | continue; |
| 26 | if (line[0] == '[' && line.IndexOf(']') is var end and > 0) |
| 27 | { |
| 28 | values = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| 29 | sections.Add(new IniSection(line[1..end], values)); |
| 30 | continue; |
| 31 | } |
| 32 | |
| 33 | var equals = line.IndexOf('='); |
| 34 | if (equals <= 0) |
| 35 | continue; |
| 36 | if (values is null) |
| 37 | { |
| 38 | values = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| 39 | sections.Add(new IniSection("", values)); |
| 40 | } |
| 41 | |
| 42 | values.TryAdd(line[..equals].Trim(), line[(equals + 1)..].Trim()); |
| 43 | } |
| 44 | |
| 45 | return sections; |
| 46 | } |
| 47 | |
| 48 | public static IniSection? Section(IEnumerable<IniSection> sections, string name) |
| 49 | { |
| 50 | return sections.FirstOrDefault(section => section.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); |
| 51 | } |
| 52 | } |