Skip to content
0.2.0-dev Download ZIP
92 lines ·3.2 KB Raw
1using EvilEngine.Mounts;
2
3namespace EvilEngine.Adapters.Unreal;
4
5public sealed class IoStoreSource : IndexedSource<int>
6{
7 private readonly SharedAccess[] _partitions;
8
9 public IoStoreSource(string location, IoStoreToc toc, Func<int, IRandomAccess> openPartition)
10 : base(location, CaseRule.Insensitive)
11 {
12 Toc = toc;
13 _partitions = Enumerable.Range(0, toc.Partitions).Select(i => new SharedAccess(() => openPartition(i)))
14 .ToArray();
15 }
16
17 public IoStoreToc Toc { get; }
18
19 public bool TryGetChunk(VirtualPath path, out int chunk)
20 {
21 return Index.TryGet(path, out chunk);
22 }
23
24 public IRandomAccess OpenChunk(int chunk)
25 {
26 if (Toc.Encrypted)
27 throw new MountException($"'{Location}' encrypts its chunks, and no key is read.");
28 var (_, offset, length, _, _) = Toc.Chunks[chunk];
29 if (length == 0)
30 return new MemoryAccess(Array.Empty<byte>());
31 var first = offset / Toc.BlockSize;
32 var last = (offset + length - 1) / Toc.BlockSize;
33 if (last >= Toc.Blocks.Count)
34 throw new MountException($"'{Location}' puts chunk {chunk} past its last block.");
35 var blocks = new List<Block>((int)(last - first + 1));
36 for (var i = first; i <= last; i++)
37 {
38 var block = Toc.Blocks[(int)i];
39 var partition = Toc.PartitionSize is > 0 and < long.MaxValue ? (int)(block.Offset / Toc.PartitionSize) : 0;
40 var at = Toc.PartitionSize is > 0 and < long.MaxValue ? block.Offset % Toc.PartitionSize : block.Offset;
41 if (partition >= _partitions.Length)
42 throw new MountException(
43 $"'{Location}' puts block {i} in partition {partition} of {_partitions.Length}.");
44 var method = block.Method == 0 ? "" :
45 block.Method <= Toc.Methods.Count ? Toc.Methods[block.Method - 1] : $"method {block.Method}";
46 blocks.Add(new Block(_partitions[partition].Value, at, block.Stored, block.Size, method));
47 }
48
49 return new BlockAccess(blocks, offset - first * Toc.BlockSize, length, $"{Location} chunk {chunk}");
50 }
51
52 public byte[] ReadChunk(int chunk)
53 {
54 using var access = OpenChunk(chunk);
55 var bytes = new byte[access.Length];
56 access.ReadExactly(0, bytes);
57 return bytes;
58 }
59
60 public bool IsStored(int chunk)
61 {
62 var (_, offset, length, _, _) = Toc.Chunks[chunk];
63 if (length == 0)
64 return true;
65 for (var i = offset / Toc.BlockSize; i <= (offset + length - 1) / Toc.BlockSize; i++)
66 if (Toc.Blocks[(int)i].Method != 0)
67 return false;
68 return true;
69 }
70
71 protected override void Fill(PathIndex<int> index)
72 {
73 foreach (var (path, chunk) in Toc.Files)
74 index.Add(path, Toc.Chunks[chunk].Length, chunk);
75 }
76
77 protected override Stream OpenEntry(int chunk)
78 {
79 return new AccessStream(OpenChunk(chunk));
80 }
81
82 protected override IRandomAccess OpenEntryAccess(int chunk)
83 {
84 return OpenChunk(chunk);
85 }
86
87 protected override void Release()
88 {
89 foreach (var partition in _partitions)
90 partition.Dispose();
91 }
92}