0.2.0-dev Download ZIP
289 lines ·9.4 KB Raw
| 1 | using System.Collections.Concurrent; |
| 2 | using System.Text; |
| 3 | using EvilEngine.Mounts; |
| 4 | |
| 5 | namespace EvilEngine.Adapters.Hpl2; |
| 6 | |
| 7 | public sealed record Reference(VirtualPath From, string Written); |
| 8 | |
| 9 | public sealed record WalkResult(IReadOnlyList<VirtualPath> Files, IReadOnlyList<Reference> Missing); |
| 10 | |
| 11 | public sealed class ReferenceWalk(IFileSource tree, Searcher searcher) |
| 12 | { |
| 13 | private static readonly HashSet<string> Extensions = new(StringComparer.OrdinalIgnoreCase) |
| 14 | { |
| 15 | "ent", "mat", "dds", "jpg", "png", "tif", "bmp", "tga", "dae", "dae_anim", "msh", "anm", "ps", "hps", "snt", |
| 16 | "ogg", "wav", "map", "lang", "fnt", "cfg", "nodes", "flash", "comment", "sps", "rope", "ho", "skin" |
| 17 | }; |
| 18 | |
| 19 | private static readonly HashSet<string> Attributed = new(StringComparer.OrdinalIgnoreCase) |
| 20 | { "map", "ent", "ps", "cfg", "ho", "rope", "sps", "skin", "flash", "comment" }; |
| 21 | |
| 22 | private static readonly HashSet<string> Bare = new(StringComparer.OrdinalIgnoreCase) |
| 23 | { "hps", "ent", "map", "ps", "cfg" }; |
| 24 | |
| 25 | private static readonly HashSet<string>.AlternateLookup<ReadOnlySpan<char>> ExtensionLookup = |
| 26 | Extensions.GetAlternateLookup<ReadOnlySpan<char>>(); |
| 27 | |
| 28 | private static readonly string[] BareKinds = [".snt", ".ps", ".ent"]; |
| 29 | |
| 30 | private static readonly string[] SoundKinds = [".wav", ".ogg"]; |
| 31 | |
| 32 | private static readonly string[] ImageKinds = [".dds", ".jpg", ".png", ".tif", ".bmp", ".tga"]; |
| 33 | |
| 34 | private static readonly byte[] MaterialEnd = ".mat\0"u8.ToArray(); |
| 35 | |
| 36 | private readonly ConcurrentDictionary<VirtualPath, Found> _found = new(); |
| 37 | |
| 38 | private readonly Lazy<HashSet<string>> _paths = new(() => |
| 39 | tree.EnumerateAll().Select(entry => entry.Path.Value).ToHashSet(StringComparer.Ordinal)); |
| 40 | |
| 41 | public WalkResult Walk(IEnumerable<VirtualPath> roots) |
| 42 | { |
| 43 | var seen = new HashSet<VirtualPath>(); |
| 44 | var missing = new HashSet<Reference>(); |
| 45 | var frontier = roots.Where(seen.Add).ToList(); |
| 46 | while (frontier.Count > 0) |
| 47 | { |
| 48 | var found = new Found[frontier.Count]; |
| 49 | Parallel.For(0, frontier.Count, i => found[i] = _found.GetOrAdd(frontier[i], Find)); |
| 50 | var next = new List<VirtualPath>(); |
| 51 | foreach (var file in found) |
| 52 | { |
| 53 | missing.UnionWith(file.Missing); |
| 54 | next.AddRange(file.Files.Where(seen.Add)); |
| 55 | } |
| 56 | |
| 57 | frontier = next; |
| 58 | } |
| 59 | |
| 60 | return new WalkResult([.. seen.Order()], |
| 61 | [ |
| 62 | .. missing.OrderBy(reference => reference.From) |
| 63 | .ThenBy(reference => reference.Written, StringComparer.Ordinal) |
| 64 | ]); |
| 65 | } |
| 66 | |
| 67 | public static bool EndsInExtension(string value) |
| 68 | { |
| 69 | return Extension(value) is not null; |
| 70 | } |
| 71 | |
| 72 | public static bool IsPiece(string value) |
| 73 | { |
| 74 | var name = value[(value.LastIndexOfAny(['/', '\\']) + 1)..]; |
| 75 | return Extension(value) is { } extension && name.Length == extension.Length + 1; |
| 76 | } |
| 77 | |
| 78 | public static bool IsBareName(string value) |
| 79 | { |
| 80 | if (value.Length < 3) |
| 81 | return false; |
| 82 | foreach (var c in value) |
| 83 | if (c is not (>= 'a' and <= 'z' or >= 'A' and <= 'Z' or >= '0' and <= '9' or '_')) |
| 84 | return false; |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | public static IReadOnlyList<string> Literals(string script) |
| 89 | { |
| 90 | var literals = new List<string>(); |
| 91 | foreach (var line in script.Split('\n')) |
| 92 | { |
| 93 | var at = 0; |
| 94 | while (line.IndexOf('"', at) is var open and >= 0 && line.IndexOf('"', open + 1) is var close and >= 0) |
| 95 | { |
| 96 | literals.Add(line[(open + 1)..close]); |
| 97 | at = close + 1; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return literals; |
| 102 | } |
| 103 | |
| 104 | public static IReadOnlyList<string> MeshMaterials(byte[] mesh) |
| 105 | { |
| 106 | var found = new List<string>(); |
| 107 | var span = mesh.AsSpan(); |
| 108 | var at = 0; |
| 109 | while (span[at..].IndexOf(MaterialEnd) is var offset and >= 0) |
| 110 | { |
| 111 | var end = at + offset + MaterialEnd.Length - 1; |
| 112 | var start = end; |
| 113 | while (start > 0 && IsPathByte(mesh[start - 1])) |
| 114 | start--; |
| 115 | if (end - start >= 5) |
| 116 | found.Add(Encoding.UTF8.GetString(mesh, start, end - start)); |
| 117 | at = end + 1; |
| 118 | } |
| 119 | |
| 120 | return found; |
| 121 | } |
| 122 | |
| 123 | private static string? Extension(string value) |
| 124 | { |
| 125 | if (value.AsSpan().IndexOfAny('<', '>') >= 0) |
| 126 | return null; |
| 127 | var dot = value.LastIndexOf('.'); |
| 128 | if (dot < 0) |
| 129 | return null; |
| 130 | var extension = value.AsSpan(dot + 1); |
| 131 | return ExtensionLookup.TryGetValue(extension, out var known) ? known : null; |
| 132 | } |
| 133 | |
| 134 | private Found Find(VirtualPath file) |
| 135 | { |
| 136 | var kind = file.Extension.ToLowerInvariant(); |
| 137 | var found = new Found(file); |
| 138 | switch (kind) |
| 139 | { |
| 140 | case "dae": |
| 141 | Twin(file, "msh", found); |
| 142 | break; |
| 143 | case "dae_anim": |
| 144 | Twin(file, "anm", found); |
| 145 | break; |
| 146 | case "msh": |
| 147 | foreach (var written in MeshMaterials(Bytes(file))) |
| 148 | Named(written, found); |
| 149 | break; |
| 150 | case "mat": |
| 151 | if (Hpl2Xml.Parse(Bytes(file)) is { } material) |
| 152 | foreach (var unit in material.Descendants().Where(element => element.Name == "TextureUnits")) |
| 153 | foreach (var texture in unit.Elements) |
| 154 | if (texture["File"] is { Length: > 0 } written) |
| 155 | Texture(written, found); |
| 156 | break; |
| 157 | case "snt": |
| 158 | if (Hpl2Xml.Parse(Bytes(file)) is { } sound) |
| 159 | foreach (var element in sound.Descendants().Where(element => element.Name == "Sound")) |
| 160 | if (element["File"] is { Length: > 0 } written) |
| 161 | Sound(written, found); |
| 162 | break; |
| 163 | case "hps": |
| 164 | foreach (var literal in Literals(Encoding.UTF8.GetString(Bytes(file)))) |
| 165 | Value(literal, true, found); |
| 166 | break; |
| 167 | default: |
| 168 | if (Attributed.Contains(kind) && Hpl2Xml.Parse(Bytes(file)) is { } document) |
| 169 | { |
| 170 | var bare = Bare.Contains(kind); |
| 171 | foreach (var element in document.Descendants()) |
| 172 | foreach (var (_, value) in element.Attributes) |
| 173 | Value(value, bare, found); |
| 174 | } |
| 175 | |
| 176 | break; |
| 177 | } |
| 178 | |
| 179 | return found; |
| 180 | } |
| 181 | |
| 182 | private void Value(string value, bool bare, Found found) |
| 183 | { |
| 184 | if (EndsInExtension(value)) |
| 185 | { |
| 186 | if (!IsPiece(value)) |
| 187 | Named(value, found); |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | if (!bare || !IsBareName(value)) |
| 192 | return; |
| 193 | foreach (var kind in BareKinds) |
| 194 | if (searcher.Resolve(value + kind) is { } resolved) |
| 195 | { |
| 196 | found.Files.Add(resolved); |
| 197 | return; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | private void Texture(string written, Found found) |
| 202 | { |
| 203 | if (EndsInExtension(written)) |
| 204 | { |
| 205 | if (!IsPiece(written)) |
| 206 | Named(written, found); |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | VirtualPath? best = null; |
| 211 | var top = -1; |
| 212 | foreach (var kind in ImageKinds) |
| 213 | if (searcher.Resolve(written + kind) is { } resolved && searcher.Score(written, resolved) is var score && |
| 214 | score > top) |
| 215 | { |
| 216 | best = resolved; |
| 217 | top = score; |
| 218 | } |
| 219 | |
| 220 | if (best is { } image) |
| 221 | found.Files.Add(image); |
| 222 | else |
| 223 | found.Missing.Add(new Reference(found.From, written)); |
| 224 | } |
| 225 | |
| 226 | private void Sound(string written, Found found) |
| 227 | { |
| 228 | if (EndsInExtension(written)) |
| 229 | { |
| 230 | Named(written, found); |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | foreach (var kind in SoundKinds) |
| 235 | if (searcher.Resolve(written + kind) is { } resolved) |
| 236 | { |
| 237 | found.Files.Add(resolved); |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | found.Missing.Add(new Reference(found.From, written)); |
| 242 | } |
| 243 | |
| 244 | private void Named(string written, Found found) |
| 245 | { |
| 246 | if (searcher.Resolve(written) is { } resolved) |
| 247 | found.Files.Add(resolved); |
| 248 | else |
| 249 | found.Missing.Add(new Reference(found.From, written)); |
| 250 | } |
| 251 | |
| 252 | private void Twin(VirtualPath file, string extension, Found found) |
| 253 | { |
| 254 | var twin = file.WithoutExtension.Value + "." + extension; |
| 255 | if (_paths.Value.Contains(twin)) |
| 256 | found.Files.Add(twin); |
| 257 | } |
| 258 | |
| 259 | private byte[] Bytes(VirtualPath file) |
| 260 | { |
| 261 | using var stream = tree.Open(file); |
| 262 | if (!stream.CanSeek) |
| 263 | { |
| 264 | using var copy = new MemoryStream(); |
| 265 | stream.CopyTo(copy); |
| 266 | return copy.ToArray(); |
| 267 | } |
| 268 | |
| 269 | var bytes = new byte[stream.Length]; |
| 270 | stream.ReadExactly(bytes); |
| 271 | return bytes; |
| 272 | } |
| 273 | |
| 274 | private static bool IsPathByte(byte value) |
| 275 | { |
| 276 | return value is >= (byte)'a' and <= (byte)'z' or >= (byte)'A' and <= (byte)'Z' or >= (byte)'0' and <= (byte)'9' |
| 277 | or (byte)'_' or (byte)'/' or (byte)'\\' or (byte)':' or (byte)'.' or (byte)' ' or (byte)'(' or (byte)')' |
| 278 | or (byte)'-'; |
| 279 | } |
| 280 | |
| 281 | private sealed class Found(VirtualPath from) |
| 282 | { |
| 283 | public VirtualPath From { get; } = from; |
| 284 | |
| 285 | public List<VirtualPath> Files { get; } = []; |
| 286 | |
| 287 | public List<Reference> Missing { get; } = []; |
| 288 | } |
| 289 | } |