/
Ship.cs
487 lines (468 loc) · 17 KB
/
Ship.cs
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
* Idmr.LfdReader.dll, Library file to read and write LFD resource files
* Copyright (C) 2009-2023 Michael Gaisser (mjgaisser@gmail.com)
* Licensed under the MPL v2.0 or later
*
* Full notice in help/Idmr.LfdReader.chm
* Version: 2.1
*/
/* CHANGE LOG
* v2.1, 221030
* [UPD] Properties now have internal sets
* [NEW] Creation of Lines during Decode
* v2.0, 210309
* [NEW] Created
*/
using System;
using System.Collections.Generic;
using System.IO;
using Idmr.Common;
namespace Idmr.LfdReader
{
/// <summary>Object for "SHIP" mesh resources.</summary>
/// <remarks>This is the third iteration of craft mesh data, used in TIE. This format adds designations to mesh data such as MainHull or Engine, and is the precursor to the OPT format.<br/>
/// Resource is read-only.</remarks>
/// <seealso cref="Cplx"/>
/// <seealso cref="Crft"/>
/// <example>
/// <h4>Raw Data definition</h4>
/// <code>
/// RawData
/// {
/// /* 0x00 */ short Length
/// /* 0x02 */ byte[30] Unknown
/// /* 0x20 */ byte NumComponents
/// /* 0x21 */ byte NumShadingSets
/// /* 0x22 */ short Unknown
/// /* 0x24 */ ShadingSet[NumShadingSets]
/// MeshSettings[NumComponents]
/// Component[NumComponents]
/// }
///
/// ShadingSet (size 0x6)
/// {
/// ??? // don't know what this does
/// }
///
/// MeshSettings (size 0x40)
/// {
/// /* 0x00 */ short Type (enum)
/// /* 0x2C */ short ComponentOffset
/// }
///
/// Component
/// {
/// /* 0x00 */ LodHeader[]
/// LodMesh[]
/// }
///
/// LodHeader (size 0x6)
/// {
/// /* 0x00 */ int Distance
/// /* 0x04 */ short Offset
/// }
///
/// LodMesh
/// {
/// /* 0x00 */ byte Signature
/// /* 0x01 */ byte Unknown
/// /* 0x02 */ byte NumVertices
/// /* 0x03 */ byte Unknown
/// /* 0x04 */ byte NumShapes
/// /* 0x05 */ byte[NumShapes] ColorIndices
/// Vertex16 MinimumBound
/// Vertex16 MaximumBound
/// Vertex16[NumVertices] MeshVertices
/// Vertex16[NumVertices] VertexNormals
/// ShapeSettings[NumShapes]
/// Shape[NumShapes] MeshGeometry
/// Unknown1[NumShapes]
/// short NumUnk2
/// Unknown2[NumUnk2]
/// Unknown3[NumUnk2]
/// }
///
/// Vertex16 (size 0x6)
/// {
/// /* 0x00 */ short X
/// /* 0x02 */ short Y
/// /* 0x04 */ short Z
/// }
///
/// ShapeSettings (size 0x8)
/// {
/// /* 0x00 */ Vertex16 FaceNormal
/// /* 0x06 */ short Offset
/// }
///
/// Shape
/// {
/// /* 0x00 */ byte Type
/// /* 0x01 */ byte[] Data
/// }
///
/// Unknown1 (size 0x3)
/// {
/// /* 0x00 */ byte
/// /* 0x01 */ short
/// }
///
/// Unknown2 (size 0x3)
/// {
/// /* 0x00 */ byte
/// /* 0x01 */ short Offset
/// }
///
/// Unknown3
/// {
/// /* 0x00 */ byte Type?
/// #if (Type == 1)
/// /* 0x01 */ byte
/// /* 0x02 */ byte ArraySize
/// /* 0x03 */ Triplet[ArraySize]
/// #else
/// /* 0x01 */ byte[]
/// // I've seen Types 2-5 and 7, but don't know how the data is sized
/// #endif
/// }
///
/// Triplet
/// {
/// /* 0x00 */ byte
/// /* 0x01 */ byte
/// /* 0x02 */ byte
/// }
/// </code>
/// <para>The first <i>Length</i> value is the remaining data after it, so it will always equal (<see cref="Resource.Length"/>-2).</para>
/// <para>The <i>ComponentsOffsets</i> values are jump offsets from the beginning of their respective MeshSettings, such that if an offset value is at location (p+0x2C), the component will be at (p+offset).</para>
/// <para>The <i>LodHeader.Offset</i> is a jump offset from the beginning of the LodHeader object, such that if LodHeader[i] is at location p, the Mesh will start at (p+offset).
/// This also means that LodHeader[0].Offset will be the entire length of the array.
/// There isn't a defined length for the LOD arrays, rather the last LOD will have a <see cref="Crft.Lod.Distance">Distance</see> of <b>0x7FFFFFFF</b>.</para>
/// <para>In <i>LodMesh.MeshVertices</i>, if the top byte of a value is <b>0x7F</b> then it's repeating a previous vertex's value.
/// The repeat will match the sub-type (X-X, Y-Y, Z-Z) and the appropriate index is calculated with the bottom byte, right-shifted once, and subtracted from the current index.
/// E.g. if the current index is 5 and the Y value is 0x7F02, then it will be using MeshVertices[5 - (2 >> 1)].Y, or [4].Y.</para>
/// <para><i>ShapeSettings.Offset</i> is a jump offset from the beginning of the ShapeSettings object, similar to LodHeader.Offset.</para>
/// <para><see cref="Crft.Lod.Shape.Type">Shape.Type</see> uses the bottom nibble for the number of vertices, top nibble for type as a bitfield. The first bit is IsTwoSided, the second bit is IsGouraudShaded.
/// <see cref="Crft.Lod.Shape.Data">Data</see> is length(3 + (numVertices* 2)).
/// If the number of vertices is 2, then Data has a pair of vertex indexes for a line defined in Data[2] and Data[3].
/// Otherwise, for each vertex there is a line, with the vertex indexes defined in Data[v * 2] and Data[(v + 1) * 2].</para>
/// <para>After that there's some Unknown data which is currently not read into the class.
/// The Offset within Unknown2 points to the Unknown3 struct, measured from Unknown2's start position.<br/>
/// Some meshes are missing the <i>Unknown1</i> array. I've noticed this in SPECIES.LFD:SHIPCONTAIN, on a <i>Signature</i>=81 LOD.</para></example>
public partial class Ship : Resource
{
/// <summary>Defines the functionality of the mesh in-game.</summary>
public enum MeshType : short
{
/// <summary>General mesh</summary>
Default,
/// <summary>Main body, no special behavior</summary>
MainHull,
/// <summary>Wing component, no special behavior</summary>
Wing,
/// <summary>Main body, no special behavior</summary>
Fuselage,
/// <summary>Destructable static gun emplacement</summary>
GunTurret,
/// <summary>Destructable static gun emplacement</summary>
SmallGun,
/// <summary>Engines</summary>
Engine,
/// <summary>Bridge, no special behavior</summary>
Bridge,
/// <summary>Shield Generator</summary>
ShieldGen,
/// <summary>Power systems, no special behavior</summary>
EnergyGen,
/// <summary>Destrucable warhead launcher</summary>
Launcher,
/// <summary>Destrucable communication system</summary>
CommSys,
/// <summary>Beam emitter</summary>
BeamSys,
/// <summary>Beam emitter</summary>
CommandVBeam,
/// <summary>External docking platform</summary>
DockingPlat,
/// <summary>External landing platform</summary>
LandingPlat,
/// <summary>Internal hangar</summary>
Hangar,
/// <summary>Cargo component</summary>
CargoPod,
/// <summary>Main body, no special behavior</summary>
MiscHull,
/// <summary>Antenna</summary>
Antenna,
/// <summary>Rotating wing, S-foils</summary>
RotWing,
/// <summary>Rotating destructable gun emplacement</summary>
RotGunTurret,
/// <summary>Rotating destructable warhead launcher</summary>
RotLauncher,
/// <summary>Rotating destructable communications system</summary>
RotCommSys,
/// <summary>Rotating beam emitter</summary>
RotBeamSys,
/// <summary>Rotating beam emitter</summary>
RotCommandBeam,
/// <summary>Unused</summary>
Custom1,
/// <summary>Unused</summary>
Custom2,
/// <summary>Unused</summary>
Custom3,
/// <summary>Unused</summary>
Custom4,
/// <summary>Unused</summary>
Custom5,
/// <summary>Unused</summary>
Custom6
}
#region constructors
/// <summary>Blank constructor.</summary>
public Ship()
{
_type = ResourceType.Ship;
}
/// <summary>Creates a new instance from an existing opened file.</summary>
/// <param name="stream">The opened LFD file.</param>
/// <param name="filePosition">The offset of the beginning of the resource.</param>
/// <exception cref="LoadFileException">Typically due to file corruption.</exception>
public Ship(FileStream stream, long filePosition)
{
read(stream, filePosition);
}
/// <summary>Creates a new instance from an existing file.</summary>
/// <param name="path">The full path to the unopened LFD file.</param>
/// <param name="filePosition">The offset of the beginning of the resource.</param>
/// <exception cref="LoadFileException">Typically due to file corruption.</exception>
public Ship(string path, long filePosition)
{
FileStream stream = File.OpenRead(path);
read(stream, filePosition);
stream.Close();
}
#endregion
void read(FileStream stream, long filePosition)
{
try
{
_process(stream, filePosition);
}
catch (Exception x) { throw new LoadFileException(x); }
}
/// <summary>Processes raw data to populate the resource.</summary>
/// <param name="raw">Raw byte data.</param>
/// <param name="containsHeader">Whether or not <paramref name="raw"/> contains the resource Header information.</param>
/// <exception cref="ArgumentException">Header-defined <see cref="Type"/> from the LFD is not <see cref="Resource.ResourceType.Ship"/>.</exception>
public override void DecodeResource(byte[] raw, bool containsHeader)
{
_decodeResource(raw, containsHeader);
if (_type != ResourceType.Ship) throw new ArgumentException("Raw header is not for a Ship resource");
int pos = 2;
byte[] unks = new byte[30];
bool[] readOnly;
ArrayFunctions.TrimArray(_rawData, pos, unks);
pos += unks.Length;
readOnly = new bool[unks.Length];
for (int i = 0; i < readOnly.Length; i++) readOnly[i] = true;
Unknowns = new Indexer<byte>(unks, readOnly);
byte componentCount = _rawData[pos++];
Component[] components = new Component[componentCount];
byte shadingCount = _rawData[pos++];
Unknown = BitConverter.ToInt16(_rawData, pos);
pos += 2;
if (shadingCount != 0)
{
Indexer<byte>[] sets = new Indexer<byte>[shadingCount];
int setLength = 6;
readOnly = new bool[setLength];
for (int i = 0; i < setLength; i++) readOnly[i] = true;
for (int i = 0; i < shadingCount; i++)
{
byte[] shadingSet = new byte[setLength];
ArrayFunctions.TrimArray(_rawData, pos, shadingSet);
pos += setLength;
sets[i] = new Indexer<byte>(shadingSet, readOnly);
}
readOnly = new bool[shadingCount];
for (int i = 0; i < shadingCount; i++) readOnly[i] = true;
ShadingSets = new Indexer<Indexer<byte>>(sets, readOnly);
}
else ShadingSets = null;
int componentJumpStart = pos;
short[] meshTypes = new short[componentCount];
short[] componentJumps = new short[componentCount];
for (int i = 0; i < componentCount; i++)
{
meshTypes[i] = BitConverter.ToInt16(_rawData, pos);
componentJumps[i] = BitConverter.ToInt16(_rawData, pos + 0x2C);
pos += 0x40;
}
for (int c = 0; c < componentCount; c++)
{
pos = componentJumpStart + c * 0x40 + componentJumps[c];
List<int> lodDistances = new List<int>();
List<short> lodJumps = new List<short>();
int lodCount = 0;
do
{
lodDistances.Add(BitConverter.ToInt32(_rawData, pos + lodCount * 6));
lodJumps.Add(BitConverter.ToInt16(_rawData, pos + 4 + lodCount * 6));
lodCount++;
}
while (lodDistances[lodDistances.Count - 1] != int.MaxValue);
components[c] = new Component(lodCount) { MeshType = (MeshType)meshTypes[c] };
for (int l = 0; l < lodCount; l++)
{
Cplx.Lod lod = components[c].Lods[l];
lod.Distance = lodDistances[l];
pos = componentJumpStart + c * 0x40 + componentJumps[c] + l * 6 + lodJumps[l];
pos++; //skip Signature
lod.Unknown1 = _rawData[pos++];
byte vertexCount = _rawData[pos++];
lod.Unknown2 = _rawData[pos++];
byte shapeCount = _rawData[pos++];
byte[] colors = new byte[shapeCount];
ArrayFunctions.TrimArray(_rawData, pos, colors);
pos += shapeCount;
readOnly = new bool[shapeCount];
for (int i = 0; i < shapeCount; i++) readOnly[i] = true;
lod.ColorIndices = new Indexer<byte>(colors, readOnly);
lod.MinimumBound = new Crft.Lod.Vertex16(_rawData, ref pos);
lod.MaximumBound = new Crft.Lod.Vertex16(_rawData, ref pos);
Crft.Lod.Vertex16[] vertices = new Crft.Lod.Vertex16[vertexCount];
readOnly = new bool[vertexCount];
for (int v = 0; v < vertexCount; v++)
{
vertices[v] = new Crft.Lod.Vertex16(_rawData, ref pos);
readOnly[v] = true;
for (int i = 0; i < 3; i++)
{
if ((vertices[v][i] & 0xFF00) == 0x7F00)
{
int delta = (vertices[v][i] & 0x00FF) >> 1;
vertices[v][i] = vertices[v - delta][i];
}
}
}
lod.MeshVertices = new Indexer<Crft.Lod.Vertex16>(vertices, readOnly);
Crft.Lod.Vector16[] normals = new Crft.Lod.Vector16[vertexCount];
for (int v = 0; v < vertexCount; v++)
{
normals[v] = new Crft.Lod.Vector16(_rawData, ref pos);
}
lod.VertexNormals = new Indexer<Crft.Lod.Vector16>(normals, readOnly);
normals = new Crft.Lod.Vector16[shapeCount];
short[] shapeJumps = new short[shapeCount];
int shapeJumpStart = pos;
for (int s = 0; s < shapeCount; s++)
{
normals[s] = new Crft.Lod.Vector16(_rawData, ref pos);
shapeJumps[s] = BitConverter.ToInt16(_rawData, pos);
pos += 2;
}
Crft.Lod.Shape[] shapes = new Crft.Lod.Shape[shapeCount];
for (int s = 0; s < shapeCount; s++)
{
shapes[s] = new Crft.Lod.Shape();
pos = shapeJumpStart + s * 8 + shapeJumps[s];
shapes[s].FaceNormal = normals[s];
shapes[s].Type = _rawData[pos++];
int len = (shapes[s].Type & 0x0F) * 2 + 3;
byte[] data = new byte[len];
readOnly = new bool[len];
for (int i = 0; i < len; i++) readOnly[i] = true;
ArrayFunctions.TrimArray(_rawData, pos, data);
pos += len;
shapes[s].Data = new Indexer<byte>(data, readOnly);
byte shapeVertexCount = (byte)(shapes[s].Type & 0xF);
Crft.Lod.Line[] lines = new Crft.Lod.Line[shapeVertexCount / 2];
readOnly = new bool[lines.Length];
for (int i = 0; i < readOnly.Length; i++) readOnly[i] = true;
if (shapeVertexCount == 2)
lines[0] = new Crft.Lod.Line(shapes[s].Data[2], shapes[s].Data[3]);
else
for (int ln = 0; ln < lines.Length; ln++)
lines[ln] = new Crft.Lod.Line(shapes[s].Data[ln * 2], shapes[s].Data[(ln + 1) * 2]);
shapes[s].Lines = new Indexer<Crft.Lod.Line>(lines, readOnly);
}
readOnly = new bool[shapeCount];
//short unkCount;
for (int i = 0; i < shapeCount; i++) readOnly[i] = true;
try
{
for (int s = 0; s < shapeCount; s++)
{
shapes[s].Unknown1 = _rawData[pos++];
shapes[s].Unknown2 = BitConverter.ToInt16(_rawData, pos); // this throws in SHIPCONTAIN, effectively these values are missing
pos += 2;
}
//unkCount = BitConverter.ToInt16(_rawData, pos);
}
catch
{
//unkCount = 0;
} // if it threw, assume it doesn't exist and skip the Unks since now we can't trust it.
lod.Shapes = new Indexer<Crft.Lod.Shape>(shapes, readOnly);
/*pos += 2;
if (unkCount != 0)
{
byte[] unkID = new byte[unkCount];
short[] unkJumps = new short[unkCount];
int unkJumpStart = pos;
for (int u = 0; u < unkCount; u++)
{
unkID[u] = _rawData[pos++];
unkJumps[u] = BitConverter.ToInt16(_rawData, pos);
pos += 2;
}
Crft.Lod.UnknownData[] unks2 = new Crft.Lod.UnknownData[unkCount];
for (int u = 0; u < unkCount; u++)
{
pos = unkJumpStart + u * 3 + unkJumps[u];
unks2[u] = new Crft.Lod.UnknownData
{
Type = _rawData[pos],
Unknown = unkID[u]
};
byte[] data;
if (unks2[u].Type == 1)
{
data = new byte[_rawData[pos + 2] * 3 + 2];
}
else
{
data = new byte[1]; // don't keep it, haven't figured out how the sizing is done
}
ArrayFunctions.TrimArray(_rawData, pos + 1, data);
readOnly = new bool[data.Length];
for (int i = 0; i < data.Length; i++) readOnly[i] = true;
unks2[u].Data = new Indexer<byte>(data, readOnly);
}
readOnly = new bool[unkCount];
for (int i = 0; i < unkCount; i++) readOnly[i] = true;
lod.UnkData = new Indexer<Crft.Lod.UnknownData>(unks2, readOnly);
}
else { lod.UnkData = null; }*/
}
}
readOnly = new bool[componentCount];
for (int i = 0; i < componentCount; i++) readOnly[i] = true;
Components = new Indexer<Component>(components, readOnly);
}
/// <summary>Gets the components of the model.</summary>
/// <remarks>Each component is read-only.</remarks>
public Indexer<Component> Components { get; internal set; }
/// <summary>Gets the shading data.</summary>
/// <remarks>Each set is read-only.</remarks>
public Indexer<Indexer<byte>> ShadingSets { get; internal set; }
/// <summary>Unknown values</summary>
/// <remarks>Bytes 0x02 through 0x1F</remarks>
public Indexer<byte> Unknowns { get; internal set; }
/// <summary>Unknown value</summary>
/// <remarks>Byte 0x22</remarks>
public short Unknown { get; internal set; }
}
}