Skip to content
0.2.0-dev Download ZIP
52 lines ·1.7 KB Raw
1using System.Xml;
2using EvilEngine.Mounts;
3
4namespace EvilEngine.Adapters.Hpl3;
5
6public sealed record ResourceFolder(VirtualPath Path, bool Subfolders);
7
8public static class ResourceFolders
9{
10 public static readonly VirtualPath FileName = "resources.cfg";
11
12 public static IReadOnlyList<ResourceFolder> Read(IFileSource tree, VirtualPath file, List<string> notes)
13 {
14 if (!tree.Exists(file))
15 {
16 notes.Add($"{file} isn't in the install, so nothing is found by name.");
17 return [];
18 }
19
20 return Parse(file, tree.ReadAllText(file), notes);
21 }
22
23 public static IReadOnlyList<ResourceFolder> Parse(VirtualPath file, string text, List<string> notes)
24 {
25 IReadOnlyList<HplElement> elements;
26 try
27 {
28 elements = HplConfig.Elements(text);
29 }
30 catch (XmlException error)
31 {
32 notes.Add($"{file} can't be read, so nothing is found by name: {error.Message}");
33 return [];
34 }
35
36 var folders = new List<ResourceFolder>();
37 foreach (var element in elements.Where(element => element.Name == "Directory"))
38 {
39 var path = (element["Path"] ?? "").Replace('\\', VirtualPath.Separator).Trim(VirtualPath.Separator);
40 if (!VirtualPath.TryParse(path, out var folder))
41 {
42 notes.Add($"{file} names {element["Path"]}, which isn't a folder in the install, so it's left out.");
43 continue;
44 }
45
46 folders.Add(new ResourceFolder(file.Parent.Join(folder),
47 string.Equals(element["AddSubDirs"], "true", StringComparison.OrdinalIgnoreCase)));
48 }
49
50 return folders;
51 }
52}