0.2.0-dev Download ZIP
166 lines ·5.8 KB Raw
| 1 | using System.Buffers.Binary; |
| 2 | using EvilEngine.Mounts; |
| 3 | |
| 4 | namespace EvilEngine.Adapters.Unreal; |
| 5 | |
| 6 | public sealed class PackageGraph |
| 7 | { |
| 8 | public const uint Signature = 0x496F436E; |
| 9 | public const int ReadVersion = 5; |
| 10 | private const int StoreEntrySize = 16; |
| 11 | private readonly Dictionary<string, ulong> _byPath = new(StringComparer.OrdinalIgnoreCase); |
| 12 | private readonly Dictionary<ulong, List<VirtualPath>> _files = []; |
| 13 | |
| 14 | private readonly Dictionary<ulong, ulong[]> _imports = []; |
| 15 | private readonly Dictionary<ulong, VirtualPath> _names = []; |
| 16 | |
| 17 | private PackageGraph(IReadOnlyList<ulong> packages) |
| 18 | { |
| 19 | Packages = packages; |
| 20 | } |
| 21 | |
| 22 | public IReadOnlyList<ulong> Packages { get; } |
| 23 | |
| 24 | public int Imports => _imports.Values.Sum(imports => imports.Length); |
| 25 | |
| 26 | public IReadOnlyCollection<ulong> Missing => |
| 27 | _imports.Values.SelectMany(imports => imports).Where(id => !_imports.ContainsKey(id)).ToHashSet(); |
| 28 | |
| 29 | public static PackageGraph? Read(IoStoreSource store, List<string> notes) |
| 30 | { |
| 31 | var toc = store.Toc; |
| 32 | var header = -1; |
| 33 | for (var i = 0; i < toc.Chunks.Count; i++) |
| 34 | if (toc.Chunks[i].Id.Type == IoChunkType.ContainerHeader) |
| 35 | { |
| 36 | header = i; |
| 37 | break; |
| 38 | } |
| 39 | |
| 40 | if (header < 0) |
| 41 | { |
| 42 | notes.Add($"{store.Location} has no container header, so its packages have no graph."); |
| 43 | return null; |
| 44 | } |
| 45 | |
| 46 | byte[] bytes; |
| 47 | try |
| 48 | { |
| 49 | bytes = store.ReadChunk(header); |
| 50 | } |
| 51 | catch (MountException error) |
| 52 | { |
| 53 | notes.Add( |
| 54 | $"{store.Location}'s container header can't be read, so its packages have no graph: {error.Message}"); |
| 55 | return null; |
| 56 | } |
| 57 | |
| 58 | var reader = new UnrealReader(bytes, $"{store.Location} container header"); |
| 59 | if (bytes.Length < 16 || reader.U32() != Signature) |
| 60 | { |
| 61 | notes.Add( |
| 62 | $"{store.Location}'s container header doesn't open with its signature, so its packages have no graph."); |
| 63 | return null; |
| 64 | } |
| 65 | |
| 66 | var version = reader.U32(); |
| 67 | if (version != ReadVersion) |
| 68 | { |
| 69 | notes.Add( |
| 70 | $"{store.Location}'s container header is version {version}, and only version {ReadVersion} is read, so its packages have no graph."); |
| 71 | return null; |
| 72 | } |
| 73 | |
| 74 | reader.U64(); |
| 75 | var count = reader.Count(8); |
| 76 | var packages = new ulong[count]; |
| 77 | for (var i = 0; i < count; i++) |
| 78 | packages[i] = reader.U64(); |
| 79 | var size = reader.Count(1); |
| 80 | var entries = reader.Bytes(size); |
| 81 | if (size < count * StoreEntrySize) |
| 82 | throw new MountException($"'{store.Location}' keeps {size} bytes of store entries for {count} packages."); |
| 83 | |
| 84 | var graph = new PackageGraph(packages); |
| 85 | for (var i = 0; i < count; i++) |
| 86 | { |
| 87 | var at = i * StoreEntrySize; |
| 88 | var importCount = BinaryPrimitives.ReadUInt32LittleEndian(entries[at..]); |
| 89 | var importAt = at + BinaryPrimitives.ReadUInt32LittleEndian(entries[(at + 4)..]); |
| 90 | if (importAt + importCount * 8 > size) |
| 91 | throw new MountException( |
| 92 | $"'{store.Location}' points package {packages[i]:x16}'s imports past its store entries."); |
| 93 | var imports = new ulong[importCount]; |
| 94 | for (var j = 0; j < importCount; j++) |
| 95 | imports[j] = BinaryPrimitives.ReadUInt64LittleEndian(entries[(int)(importAt + j * 8)..]); |
| 96 | graph._imports[packages[i]] = imports; |
| 97 | } |
| 98 | |
| 99 | foreach (var (path, chunk) in toc.Files) |
| 100 | { |
| 101 | var id = toc.Chunks[chunk].Id; |
| 102 | if (!id.IsPackageData || !graph._imports.ContainsKey(id.Id)) |
| 103 | continue; |
| 104 | if (!graph._files.TryGetValue(id.Id, out var files)) |
| 105 | graph._files[id.Id] = files = []; |
| 106 | files.Add(path); |
| 107 | if (id.Type == IoChunkType.ExportBundleData) |
| 108 | { |
| 109 | graph._names[id.Id] = path; |
| 110 | graph._byPath[path.WithoutExtension.Value] = id.Id; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | var unnamed = packages.Count(package => !graph._names.ContainsKey(package)); |
| 115 | if (unnamed > 0) |
| 116 | notes.Add( |
| 117 | $"{unnamed} of {store.Location}'s packages have no name in its directory index, so no part can name them."); |
| 118 | return graph; |
| 119 | } |
| 120 | |
| 121 | public VirtualPath? NameOf(ulong package) |
| 122 | { |
| 123 | return _names.TryGetValue(package, out var name) ? name : (VirtualPath?)null; |
| 124 | } |
| 125 | |
| 126 | public IReadOnlyList<ulong> ImportsOf(ulong package) |
| 127 | { |
| 128 | return _imports.TryGetValue(package, out var imports) ? imports : []; |
| 129 | } |
| 130 | |
| 131 | public IReadOnlyList<VirtualPath> FilesOf(ulong package) |
| 132 | { |
| 133 | return _files.TryGetValue(package, out var files) ? files : []; |
| 134 | } |
| 135 | |
| 136 | public ulong? Find(VirtualPath path) |
| 137 | { |
| 138 | return _byPath.TryGetValue(path.WithoutExtension.Value, out var id) ? id : null; |
| 139 | } |
| 140 | |
| 141 | public Closure Close(IEnumerable<ulong> roots) |
| 142 | { |
| 143 | var seen = new HashSet<ulong>(); |
| 144 | var missing = new List<(ulong Importer, ulong Package)>(); |
| 145 | var pending = new Stack<ulong>(); |
| 146 | foreach (var root in roots) |
| 147 | if (seen.Add(root)) |
| 148 | pending.Push(root); |
| 149 | while (pending.TryPop(out var package)) |
| 150 | foreach (var import in ImportsOf(package)) |
| 151 | { |
| 152 | if (!_imports.ContainsKey(import)) |
| 153 | { |
| 154 | missing.Add((package, import)); |
| 155 | continue; |
| 156 | } |
| 157 | |
| 158 | if (seen.Add(import)) |
| 159 | pending.Push(import); |
| 160 | } |
| 161 | |
| 162 | return new Closure(seen, missing); |
| 163 | } |
| 164 | |
| 165 | public sealed record Closure(IReadOnlySet<ulong> Packages, IReadOnlyList<(ulong Importer, ulong Package)> Missing); |
| 166 | } |