Skip to content
0.2.0-dev Download ZIP
223 lines ·7.7 KB Raw
1using System.Buffers.Binary;
2using System.Security.Cryptography;
3using System.Text;
4
5namespace EvilEngine.Adapters.Tests.Support;
6
7public sealed class IoStoreWriter
8{
9 private readonly List<(ulong Id, ushort Index, byte Type, byte[] Data, string? Name, bool Oodle)> _chunks = [];
10 private readonly List<(ulong Id, ulong[] Imports)> _packages = [];
11
12 public string MountPoint { get; init; } = "../../../";
13
14 public int Version { get; init; } = 8;
15
16 public int BlockSize { get; init; } = 64;
17
18 public bool WithHeader { get; init; } = true;
19
20 public IoStoreWriter Package(string path, ulong id, string text, ulong[]? imports = null, string? bulk = null,
21 bool oodle = false)
22 {
23 _chunks.Add((id, 0, 1, Encoding.UTF8.GetBytes(text), path, oodle));
24 if (bulk is not null)
25 _chunks.Add((id, 0, 2, Encoding.UTF8.GetBytes(bulk), Path.ChangeExtension(path, "ubulk"), false));
26 _packages.Add((id, imports ?? []));
27 return this;
28 }
29
30 public IoStoreWriter Chunk(ulong id, byte type, byte[] data, string? name = null, bool oodle = false)
31 {
32 _chunks.Add((id, 0, type, data, name, oodle));
33 return this;
34 }
35
36 public static byte[] Hash(byte[] data)
37 {
38 return SHA256.HashData(data)[..20];
39 }
40
41 public (byte[] Toc, byte[] Cas) Build()
42 {
43 var chunks = _chunks.ToList();
44 if (WithHeader)
45 chunks.Add((0x5E2371746F7AF252, 0, 6, Header(), null, false));
46 var cas = new MemoryStream();
47 var blocks = new List<(long Offset, int Stored, int Size, byte Method)>();
48 var spans = new List<(long Offset, long Length)>();
49 long uncompressed = 0;
50 foreach (var chunk in chunks)
51 {
52 spans.Add((uncompressed, chunk.Data.Length));
53 for (var at = 0; at < chunk.Data.Length; at += BlockSize)
54 {
55 var piece = chunk.Data.AsSpan(at, Math.Min(BlockSize, chunk.Data.Length - at)).ToArray();
56 var stored = chunk.Oodle ? UnrealPakWriter.Compress("Oodle", piece) : piece;
57 blocks.Add((cas.Position, stored.Length, piece.Length, (byte)(chunk.Oodle ? 1 : 0)));
58 cas.Write(stored);
59 }
60
61 uncompressed += (chunk.Data.Length + BlockSize - 1) / BlockSize * BlockSize;
62 }
63
64 var directory = Directory(chunks.Select((chunk, i) => (chunk.Name, i)).Where(pair => pair.Name is not null)
65 .Select(pair => (pair.Name!, pair.i)).ToList());
66 var toc = new MemoryStream();
67 var writer = new BinaryWriter(toc);
68 writer.Write("-==--==--==--==-"u8);
69 writer.Write((byte)Version);
70 writer.Write(new byte[3]);
71 writer.Write(144);
72 writer.Write(chunks.Count);
73 writer.Write(blocks.Count);
74 writer.Write(12);
75 writer.Write(1);
76 writer.Write(32);
77 writer.Write(BlockSize);
78 writer.Write(directory.Length);
79 writer.Write(1);
80 writer.Write(0x5E2371746F7AF252UL);
81 writer.Write(new byte[16]);
82 writer.Write((byte)9);
83 writer.Write(new byte[3]);
84 writer.Write(0);
85 writer.Write(ulong.MaxValue);
86 writer.Write(0);
87 writer.Write(0);
88 writer.Write(new byte[40]);
89 foreach (var chunk in chunks)
90 {
91 var id = new byte[12];
92 BinaryPrimitives.WriteUInt64LittleEndian(id, chunk.Id);
93 BinaryPrimitives.WriteUInt16BigEndian(id.AsSpan(8), chunk.Index);
94 id[11] = chunk.Type;
95 writer.Write(id);
96 }
97
98 foreach (var (offset, length) in spans)
99 {
100 writer.Write(Big40(offset));
101 writer.Write(Big40(length));
102 }
103
104 foreach (var (offset, stored, size, method) in blocks)
105 {
106 var entry = new byte[12];
107 for (var i = 0; i < 5; i++)
108 entry[i] = (byte)(offset >> (8 * i));
109 for (var i = 0; i < 3; i++)
110 {
111 entry[5 + i] = (byte)(stored >> (8 * i));
112 entry[8 + i] = (byte)(size >> (8 * i));
113 }
114
115 entry[11] = method;
116 writer.Write(entry);
117 }
118
119 var name = new byte[32];
120 "Oodle"u8.CopyTo(name);
121 writer.Write(name);
122 writer.Write(directory);
123 foreach (var chunk in chunks)
124 {
125 writer.Write(Hash(chunk.Data));
126 writer.Write((byte)(chunk.Oodle ? 1 : 0));
127 writer.Write(new byte[3]);
128 }
129
130 return (toc.ToArray(), cas.ToArray());
131 }
132
133 private byte[] Header()
134 {
135 var output = new MemoryStream();
136 var writer = new BinaryWriter(output);
137 writer.Write(0x496F436Eu);
138 writer.Write(5u);
139 writer.Write(0x5E2371746F7AF252UL);
140 writer.Write(_packages.Count);
141 foreach (var (id, _) in _packages)
142 writer.Write(id);
143 var entries = new MemoryStream();
144 var entriesWriter = new BinaryWriter(entries);
145 var importsAt = _packages.Count * 16;
146 var imports = new MemoryStream();
147 var importsWriter = new BinaryWriter(imports);
148 for (var i = 0; i < _packages.Count; i++)
149 {
150 var list = _packages[i].Imports;
151 entriesWriter.Write((uint)list.Length);
152 entriesWriter.Write((uint)(importsAt + imports.Length - i * 16));
153 entriesWriter.Write(0u);
154 entriesWriter.Write(0u);
155 foreach (var import in list)
156 importsWriter.Write(import);
157 }
158
159 entriesWriter.Write(imports.ToArray());
160 writer.Write((int)entries.Length);
161 writer.Write(entries.ToArray());
162 writer.Write(new byte[20]);
163 return output.ToArray();
164 }
165
166 private byte[] Directory(List<(string Name, int Chunk)> files)
167 {
168 var strings = new List<string>();
169 var folders = new List<uint[]> { new[] { uint.MaxValue, uint.MaxValue, uint.MaxValue, uint.MaxValue } };
170 var entries = new List<uint[]>();
171 var byPath = new Dictionary<string, int> { [""] = 0 };
172
173 uint Text(string text)
174 {
175 var at = strings.IndexOf(text);
176 if (at >= 0)
177 return (uint)at;
178 strings.Add(text);
179 return (uint)(strings.Count - 1);
180 }
181
182 int FolderOf(string path)
183 {
184 if (byPath.TryGetValue(path, out var known))
185 return known;
186 var slash = path.LastIndexOf('/');
187 var parent = FolderOf(slash < 0 ? "" : path[..slash]);
188 folders.Add([Text(path[(slash + 1)..]), uint.MaxValue, folders[parent][1], uint.MaxValue]);
189 folders[parent][1] = (uint)(folders.Count - 1);
190 byPath[path] = folders.Count - 1;
191 return folders.Count - 1;
192 }
193
194 foreach (var (name, chunk) in files)
195 {
196 var slash = name.LastIndexOf('/');
197 var folder = FolderOf(slash < 0 ? "" : name[..slash]);
198 entries.Add([Text(name[(slash + 1)..]), folders[folder][3], (uint)chunk]);
199 folders[folder][3] = (uint)(entries.Count - 1);
200 }
201
202 var output = new MemoryStream();
203 var writer = new BinaryWriter(output);
204 UnrealPakWriter.FString(writer, MountPoint);
205 writer.Write(folders.Count);
206 foreach (var folder in folders)
207 foreach (var value in folder)
208 writer.Write(value);
209 writer.Write(entries.Count);
210 foreach (var entry in entries)
211 foreach (var value in entry)
212 writer.Write(value);
213 writer.Write(strings.Count);
214 foreach (var text in strings)
215 UnrealPakWriter.FString(writer, text);
216 return output.ToArray();
217 }
218
219 private static byte[] Big40(long value)
220 {
221 return [(byte)(value >> 32), (byte)(value >> 24), (byte)(value >> 16), (byte)(value >> 8), (byte)value];
222 }
223}