0.2.0-dev Download ZIP
186 lines ·6.1 KB Raw
| 1 | using System.Diagnostics.CodeAnalysis; |
| 2 | using System.Text.Json.Nodes; |
| 3 | using EvilEngine.Adapters.Zip; |
| 4 | using EvilEngine.Mounts; |
| 5 | |
| 6 | namespace EvilEngine.Adapters.BeamNG; |
| 7 | |
| 8 | public enum Resolution |
| 9 | { |
| 10 | Found, |
| 11 | Sealed, |
| 12 | Missing |
| 13 | } |
| 14 | |
| 15 | public sealed class GameFiles |
| 16 | { |
| 17 | public static readonly VirtualPath Content = "content"; |
| 18 | |
| 19 | public static readonly VirtualPath EngineArchive = "gameengine.zip"; |
| 20 | |
| 21 | private static readonly string[] Images = ["png", "jpg", "jpeg", "tga", "dds"]; |
| 22 | |
| 23 | private static readonly StringComparer Paths = StringComparer.OrdinalIgnoreCase; |
| 24 | |
| 25 | private readonly Dictionary<string, (ZipSource Archive, ZipEntry Entry)> _entries = new(Paths); |
| 26 | |
| 27 | private readonly Dictionary<string, VirtualPath[]> _links = new(Paths); |
| 28 | |
| 29 | public GameFiles(IFileSource install, ZipContainer zip, List<string> notes) |
| 30 | { |
| 31 | Install = install; |
| 32 | var archives = new List<(VirtualPath Path, ZipSource Source)>(); |
| 33 | foreach (var path in ArchivePaths(install)) |
| 34 | try |
| 35 | { |
| 36 | archives.Add((path, zip.Open(install, path, CaseRule.Insensitive))); |
| 37 | } |
| 38 | catch (MountException error) |
| 39 | { |
| 40 | notes.Add($"{path} can't be read as a ZIP, so it's left out: {error.Message}"); |
| 41 | } |
| 42 | |
| 43 | Archives = archives; |
| 44 | var collisions = 0; |
| 45 | foreach (var (path, archive) in archives) |
| 46 | foreach (var entry in archive.Directory.Entries) |
| 47 | { |
| 48 | if (!_entries.TryAdd(entry.Path.Value, (archive, entry))) |
| 49 | { |
| 50 | collisions++; |
| 51 | continue; |
| 52 | } |
| 53 | |
| 54 | if (entry.Encryption != ZipEncryption.None) |
| 55 | Sealed[path] = Sealed.GetValueOrDefault(path) + 1; |
| 56 | } |
| 57 | |
| 58 | if (collisions > 0) |
| 59 | notes.Add( |
| 60 | $"{collisions} paths are in more than one archive, and the first archive in ordinal order wins each."); |
| 61 | View = new LayeredSource($"{install.Location} (game)", CaseRule.Insensitive, |
| 62 | [ |
| 63 | .. archives.Select(archive => new MountLayer(archive.Path.Value, "base", archive.Source)), |
| 64 | new MountLayer("install", "base", install) |
| 65 | ]); |
| 66 | } |
| 67 | |
| 68 | public IFileSource Install { get; } |
| 69 | |
| 70 | public IReadOnlyList<(VirtualPath Path, ZipSource Source)> Archives { get; } |
| 71 | |
| 72 | public IFileSource View { get; } |
| 73 | |
| 74 | public Dictionary<VirtualPath, int> Sealed { get; } = []; |
| 75 | |
| 76 | public static IEnumerable<VirtualPath> ArchivePaths(IFileSource install) |
| 77 | { |
| 78 | var inContent = install.Enumerate(Content, true).Select(entry => entry.Path).Where(IsArchive); |
| 79 | return inContent.Concat(install.Exists(EngineArchive) ? [EngineArchive] : []) |
| 80 | .OrderBy(path => path.Value, StringComparer.Ordinal); |
| 81 | } |
| 82 | |
| 83 | public bool TryEntry(VirtualPath path, [NotNullWhen(true)] out ZipEntry? entry) |
| 84 | { |
| 85 | entry = _entries.TryGetValue(path.Value, out var found) ? found.Entry : null; |
| 86 | return entry is not null; |
| 87 | } |
| 88 | |
| 89 | public bool IsSealed(VirtualPath path) |
| 90 | { |
| 91 | return _entries.TryGetValue(path.Value, out var found) && found.Entry.Encryption != ZipEncryption.None; |
| 92 | } |
| 93 | |
| 94 | public bool Exists(VirtualPath path) |
| 95 | { |
| 96 | return _entries.ContainsKey(path.Value) || Install.Exists(path); |
| 97 | } |
| 98 | |
| 99 | public IEnumerable<VirtualPath> EntriesOf(VirtualPath archive) |
| 100 | { |
| 101 | return Archives.Where(pair => pair.Path.Equals(archive, CaseRule.Insensitive)) |
| 102 | .SelectMany(pair => pair.Source.Directory.Entries) |
| 103 | .Where(entry => entry.Encryption == ZipEncryption.None) |
| 104 | .Select(entry => entry.Path); |
| 105 | } |
| 106 | |
| 107 | public IEnumerable<VirtualPath> Under(VirtualPath folder) |
| 108 | { |
| 109 | return View.Enumerate(folder, true).Select(entry => entry.Path).Where(path => !IsSealed(path)); |
| 110 | } |
| 111 | |
| 112 | public Resolution Resolve(string written, out VirtualPath[] files) |
| 113 | { |
| 114 | files = []; |
| 115 | var text = written.Replace('\\', '/').TrimStart('/'); |
| 116 | if (!VirtualPath.TryParse(text, out var path) || path.IsRoot) |
| 117 | return Resolution.Missing; |
| 118 | var stem = path.WithoutExtension; |
| 119 | var image = Images.Contains(path.Extension, Paths); |
| 120 | VirtualPath[] candidates = image |
| 121 | ? [path, Linked(path), Dds(stem), Linked(Dds(stem))] |
| 122 | : [path, Linked(path)]; |
| 123 | foreach (var candidate in candidates) |
| 124 | { |
| 125 | if (!Exists(candidate)) |
| 126 | continue; |
| 127 | if (IsSealed(candidate)) |
| 128 | return Resolution.Sealed; |
| 129 | if (!candidate.Extension.Equals("link", StringComparison.OrdinalIgnoreCase) || |
| 130 | path.Extension.Equals("link", StringComparison.OrdinalIgnoreCase)) |
| 131 | { |
| 132 | files = [candidate]; |
| 133 | return Resolution.Found; |
| 134 | } |
| 135 | |
| 136 | files = [candidate, .. Follow(candidate)]; |
| 137 | return files.Length > 1 ? Resolution.Found : Resolution.Missing; |
| 138 | } |
| 139 | |
| 140 | return Resolution.Missing; |
| 141 | } |
| 142 | |
| 143 | public byte[] Read(VirtualPath path) |
| 144 | { |
| 145 | return View.ReadAllBytes(path); |
| 146 | } |
| 147 | |
| 148 | private VirtualPath[] Follow(VirtualPath link) |
| 149 | { |
| 150 | if (_links.TryGetValue(link.Value, out var known)) |
| 151 | return known; |
| 152 | VirtualPath[] result = []; |
| 153 | try |
| 154 | { |
| 155 | if (LooseJson.Parse(Read(link)) is JsonObject json && json["path"]?.ToString() is { } target && |
| 156 | VirtualPath.TryParse(target.Replace('\\', '/').TrimStart('/'), out var path) && !path.IsRoot) |
| 157 | { |
| 158 | if (Exists(path) && !IsSealed(path)) |
| 159 | result = [path]; |
| 160 | else if (Images.Contains(path.Extension, Paths) && Exists(Dds(path.WithoutExtension))) |
| 161 | result = [Dds(path.WithoutExtension)]; |
| 162 | } |
| 163 | } |
| 164 | catch (FormatException) |
| 165 | { |
| 166 | } |
| 167 | |
| 168 | _links[link.Value] = result; |
| 169 | return result; |
| 170 | } |
| 171 | |
| 172 | private static VirtualPath Linked(VirtualPath path) |
| 173 | { |
| 174 | return VirtualPath.Parse(path.Value + ".link"); |
| 175 | } |
| 176 | |
| 177 | private static VirtualPath Dds(VirtualPath stem) |
| 178 | { |
| 179 | return VirtualPath.Parse(stem.Value + ".dds"); |
| 180 | } |
| 181 | |
| 182 | private static bool IsArchive(VirtualPath path) |
| 183 | { |
| 184 | return path.Extension.Equals("zip", StringComparison.OrdinalIgnoreCase); |
| 185 | } |
| 186 | } |