-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLithTechWorldDatDecoder.cs
More file actions
416 lines (360 loc) · 14.2 KB
/
LithTechWorldDatDecoder.cs
File metadata and controls
416 lines (360 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
using System.Buffers.Binary;
using System.Globalization;
using System.IO;
using System.Text;
namespace CFRezManager;
internal static class LithTechWorldDatDecoder
{
private const uint SupportedVersion = 85;
private const int HeaderLength = 60;
private const int VertexByteCount = 68;
private const int TriangleByteCount = 16;
private const long MaxDecodedWorldBytes = 512L * 1024 * 1024;
private const int MaxNodeCount = 100_000;
private const int MaxVertexCountPerNode = 1_000_000;
private const int MaxTriangleCountPerNode = 1_000_000;
static LithTechWorldDatDecoder()
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
public static bool IsCandidate(string extension)
{
return string.Equals(extension, "dat", StringComparison.OrdinalIgnoreCase);
}
public static bool TryDecode(
byte[] data,
string fallbackName,
out LithTechModelDocument? document,
out string? errorMessage)
{
document = null;
errorMessage = null;
try
{
byte[]? prepared = LzmaAloneDecoder.TryPrepareData(data, MaxDecodedWorldBytes);
if (prepared is null)
{
errorMessage = "DAT world compression could not be decoded.";
return false;
}
if (prepared.Length < HeaderLength)
{
errorMessage = "DAT file is too small for a LithTech world header.";
return false;
}
uint version = BinaryPrimitives.ReadUInt32LittleEndian(prepared.AsSpan(0, sizeof(uint)));
if (version != SupportedVersion)
{
errorMessage = $"DAT world version is not supported: {version}.";
return false;
}
uint renderDataPosition = BinaryPrimitives.ReadUInt32LittleEndian(prepared.AsSpan(24, sizeof(uint)));
if (renderDataPosition < HeaderLength || renderDataPosition >= prepared.Length)
{
errorMessage = "DAT render data offset is outside the file.";
return false;
}
var reader = new DatReader(prepared, checked((int)renderDataPosition));
var meshes = new List<LithTechMesh>();
uint renderTreeNodeCount = reader.ReadUInt32();
ValidateCount(renderTreeNodeCount, MaxNodeCount, "render tree node");
for (int i = 0; i < renderTreeNodeCount; i++)
{
ReadRenderNode(reader, $"RenderNode {i + 1}", meshes);
}
uint worldModelNodeCount = reader.ReadUInt32();
ValidateCount(worldModelNodeCount, MaxNodeCount, "world model render node");
for (int worldModelIndex = 0; worldModelIndex < worldModelNodeCount; worldModelIndex++)
{
string worldModelName = reader.ReadString();
uint nodesInWorldModel = reader.ReadUInt32();
ValidateCount(nodesInWorldModel, MaxNodeCount, "world model child node");
for (int nodeIndex = 0; nodeIndex < nodesInWorldModel; nodeIndex++)
{
ReadRenderNode(reader, $"{worldModelName} #{nodeIndex + 1}", meshes);
}
reader.Skip(sizeof(uint)); // noChildFlag
}
uint lightGroupCount = reader.ReadUInt32();
ValidateCount(lightGroupCount, MaxNodeCount, "world light group");
for (int i = 0; i < lightGroupCount; i++)
{
SkipWorldLightGroup(reader);
}
if (meshes.Count == 0)
{
errorMessage = "DAT render data did not contain previewable geometry.";
return false;
}
document = new LithTechModelDocument(
fallbackName,
meshes,
ReferenceEquals(prepared, data)
? $"LithTech DAT world v{version}"
: $"LZMA-compressed LithTech DAT world v{version}",
data.Length,
prepared.Length);
return true;
}
catch (Exception ex) when (ex is InvalidDataException or ArgumentOutOfRangeException or OverflowException)
{
errorMessage = ex.Message;
return false;
}
}
private static void ReadRenderNode(DatReader reader, string meshName, List<LithTechMesh> meshes)
{
reader.Skip(24); // center and half dimensions
uint sectionCount = reader.ReadUInt32();
ValidateCount(sectionCount, MaxNodeCount, "section");
var sections = new List<DatRenderSection>((int)Math.Min(sectionCount, 1024));
for (int i = 0; i < sectionCount; i++)
{
sections.Add(ReadSection(reader));
}
uint vertexCount = reader.ReadUInt32();
ValidateCount(vertexCount, MaxVertexCountPerNode, "vertex");
var vertices = new List<LithTechVector3>((int)Math.Min(vertexCount, 65536));
var textureCoordinates = new List<LithTechVector2>((int)Math.Min(vertexCount, 65536));
for (int i = 0; i < vertexCount; i++)
{
float x = reader.ReadSingle();
float y = reader.ReadSingle();
float z = reader.ReadSingle();
float u = reader.ReadSingle();
float v = reader.ReadSingle();
vertices.Add(new LithTechVector3(x, y, z));
textureCoordinates.Add(new LithTechVector2(u, v));
reader.Skip(VertexByteCount - 20);
}
uint triangleCount = reader.ReadUInt32();
ValidateCount(triangleCount, MaxTriangleCountPerNode, "triangle");
var triangleIndices = new List<int>(checked((int)Math.Min(triangleCount, 65536) * 3));
var sectionTriangleIndices = sections.Count == 0
? null
: sections.Select(_ => new List<int>()).ToList();
int sectionIndex = 0;
uint remainingSectionTriangles = sections.Count > 0 ? sections[0].TriangleCount : 0;
for (int i = 0; i < triangleCount; i++)
{
uint a = reader.ReadUInt32();
uint b = reader.ReadUInt32();
uint c = reader.ReadUInt32();
reader.Skip(sizeof(uint)); // polyIndex
while (sections.Count > 0 && sectionIndex + 1 < sections.Count && remainingSectionTriangles == 0)
{
sectionIndex++;
remainingSectionTriangles = sections[sectionIndex].TriangleCount;
}
if (a < vertexCount && b < vertexCount && c < vertexCount)
{
List<int> targetIndices = sectionTriangleIndices is null
? triangleIndices
: sectionTriangleIndices[sectionIndex];
targetIndices.Add((int)a);
targetIndices.Add((int)b);
targetIndices.Add((int)c);
}
if (sections.Count > 0 && remainingSectionTriangles > 0)
{
remainingSectionTriangles--;
}
}
uint skyPortalCount = reader.ReadUInt32();
ValidateCount(skyPortalCount, MaxNodeCount, "sky portal");
for (int i = 0; i < skyPortalCount; i++)
{
SkipVerticesPos(reader);
reader.Skip(16); // plane
}
uint occluderCount = reader.ReadUInt32();
ValidateCount(occluderCount, MaxNodeCount, "occluder");
for (int i = 0; i < occluderCount; i++)
{
SkipVerticesPos(reader);
reader.Skip(20); // plane + other
}
uint lightGroupCount = reader.ReadUInt32();
ValidateCount(lightGroupCount, MaxNodeCount, "light group");
for (int i = 0; i < lightGroupCount; i++)
{
SkipLightGroup(reader);
}
reader.Skip(1 + sizeof(uint) * 2); // childFlags + child node indices
if (vertices.Count > 0 && triangleIndices.Count >= 3)
{
meshes.Add(new LithTechMesh(meshName, vertices, triangleIndices, textureCoordinates));
}
if (vertices.Count > 0 && sectionTriangleIndices is not null)
{
for (int i = 0; i < sections.Count; i++)
{
List<int> indices = sectionTriangleIndices[i];
if (indices.Count < 3)
{
continue;
}
DatRenderSection section = sections[i];
string sectionName = string.IsNullOrWhiteSpace(section.TexturePath)
? $"{meshName} / Section {i + 1}"
: $"{meshName} / {Path.GetFileNameWithoutExtension(section.TexturePath)}";
meshes.Add(new LithTechMesh(sectionName, vertices, indices, textureCoordinates, section.TexturePath));
}
}
}
private static DatRenderSection ReadSection(DatReader reader)
{
string textureName0 = reader.ReadString();
string textureName1 = reader.ReadString();
reader.Skip(1); // shaderCode
uint triangleCount = reader.ReadUInt32();
string textureEffect = reader.ReadString();
reader.Skip(sizeof(uint) * 2); // lightMapWidth + lightMapHeight
uint lightMapSize = reader.ReadUInt32();
reader.Skip(checked((int)lightMapSize));
string? texturePath = SelectSectionTexture(textureName0, textureName1, textureEffect);
return new DatRenderSection(texturePath, triangleCount);
}
private static void SkipVerticesPos(DatReader reader)
{
int count = reader.ReadByte();
reader.Skip(checked(count * 12));
}
private static void SkipLightGroup(DatReader reader)
{
reader.ReadString();
reader.Skip(12); // color
uint intensitySize = reader.ReadUInt32();
reader.Skip(checked((int)intensitySize));
uint sectionLightMapCount = reader.ReadUInt32();
ValidateCount(sectionLightMapCount, MaxNodeCount, "section light map");
for (int i = 0; i < sectionLightMapCount; i++)
{
uint subLightMapCount = reader.ReadUInt32();
ValidateCount(subLightMapCount, MaxNodeCount, "sub light map");
for (int j = 0; j < subLightMapCount; j++)
{
reader.Skip(sizeof(uint) * 4);
uint dataLength = reader.ReadUInt32();
reader.Skip(checked((int)dataLength));
}
}
}
private static void SkipWorldLightGroup(DatReader reader)
{
reader.ReadString();
reader.Skip(12); // color
reader.Skip(12); // offset
float sizeX = reader.ReadSingle();
float sizeY = reader.ReadSingle();
float sizeZ = reader.ReadSingle();
int byteCount = checked(ToDimension(sizeX) * ToDimension(sizeY) * ToDimension(sizeZ));
reader.Skip(byteCount);
}
private static int ToDimension(float value)
{
if (!float.IsFinite(value) || value < 0 || value > 1_000_000)
{
throw new InvalidDataException($"Invalid DAT world light group dimension: {value.ToString(CultureInfo.InvariantCulture)}.");
}
return checked((int)MathF.Round(value));
}
private static void ValidateCount(uint count, int max, string label)
{
if (count > max)
{
throw new InvalidDataException($"DAT {label} count is too large: {count}.");
}
}
private static string? SelectSectionTexture(params string[] textureNames)
{
foreach (string textureName in textureNames)
{
string normalized = NormalizeTextureName(textureName);
if (!string.IsNullOrWhiteSpace(normalized))
{
return normalized;
}
}
return null;
}
private static string NormalizeTextureName(string textureName)
{
return textureName
.Trim()
.Trim('"', '\'')
.Replace('\\', '/')
.TrimStart('/');
}
private readonly record struct DatRenderSection(string? TexturePath, uint TriangleCount);
private sealed class DatReader
{
private readonly byte[] _data;
public DatReader(byte[] data, int position)
{
_data = data;
Position = position;
}
public int Position { get; private set; }
public int ReadByte()
{
Ensure(1);
return _data[Position++];
}
public uint ReadUInt32()
{
Ensure(sizeof(uint));
uint value = BinaryPrimitives.ReadUInt32LittleEndian(_data.AsSpan(Position, sizeof(uint)));
Position += sizeof(uint);
return value;
}
public float ReadSingle()
{
Ensure(sizeof(float));
int bits = BinaryPrimitives.ReadInt32LittleEndian(_data.AsSpan(Position, sizeof(float)));
Position += sizeof(float);
return BitConverter.Int32BitsToSingle(bits);
}
public string ReadString()
{
Ensure(sizeof(ushort));
ushort length = BinaryPrimitives.ReadUInt16LittleEndian(_data.AsSpan(Position, sizeof(ushort)));
Position += sizeof(ushort);
Ensure(length);
string value = DecodeString(_data.AsSpan(Position, length));
Position += length;
return value;
}
public void Skip(int byteCount)
{
if (byteCount < 0)
{
throw new InvalidDataException("DAT parser attempted to skip a negative byte count.");
}
Ensure(byteCount);
Position += byteCount;
}
private void Ensure(int byteCount)
{
if (Position < 0 || byteCount < 0 || Position + byteCount > _data.Length)
{
throw new InvalidDataException("DAT data ended before the expected structure was complete.");
}
}
private static string DecodeString(ReadOnlySpan<byte> bytes)
{
if (bytes.IsEmpty)
{
return string.Empty;
}
try
{
return Encoding.GetEncoding(949).GetString(bytes).TrimEnd('\0');
}
catch
{
return Encoding.ASCII.GetString(bytes).TrimEnd('\0');
}
}
}
}