forked from technificentConsulting/TS4-SimRipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBGEO.cs
432 lines (391 loc) · 15.7 KB
/
BGEO.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
/* TS4 SimRipper, a tool for creating custom content for The Sims 4,
Copyright (C) 2014 C. Marinetti
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
The author may be contacted at modthesims.info, username cmarNYC. */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace TS4SimRipper
{
public class BGEO
{
uint contextVersion;
TGI[] publicKey;
TGI[] externalKey;
TGI[] delayloadKey;
ObjectData[] objectData;
uint tag = 0x4F454742; //BGEO
uint version = 0x00000600;
LOD[] lodData;
Blend[] blendMap;
Vector[] vectorData;
public float weight = 1f;
public TGI PublicKey { get { return publicKey[0]; } set { this.publicKey[0] = value; } }
public LOD[] LodData { get { return lodData; } set { this.lodData = value; } }
public Blend[] BlendMap { get { return blendMap; } set { this.blendMap = value; } }
public Vector[] VectorData { get { return vectorData; } set { this.vectorData = value; } }
public BGEO Multiply(float factor)
{
BGEO b = new BGEO(this);
for (int i = 0; i < b.vectorData.Length; i++)
{
float[] v = b.vectorData[i].TranslatedVector;
v[0] *= factor;
v[1] *= factor;
v[2] *= factor;
b.vectorData[i] = new Vector(v);
}
return b;
}
public BGEO(BGEO basis)
: this(basis.contextVersion, basis.publicKey, basis.externalKey, basis.delayloadKey, basis.objectData,
basis.version, basis.lodData, basis.blendMap, basis.vectorData) { }
public BGEO(uint contextVersion, TGI[] publicKey, TGI[] externalKey, TGI[] delayloadKey, ObjectData[] objectData,
uint version, LOD[] lodData, Blend[] blendMap, Vector[] vectorData)
{
this.contextVersion = contextVersion;
this.publicKey = new TGI[publicKey != null ? publicKey.Length : 0];
for (int i = 0; i < this.publicKey.Length; i++)
{
this.publicKey[i] = new TGI(publicKey[i]);
}
this.externalKey = new TGI[externalKey != null ? externalKey.Length : 0];
for (int i = 0; i < this.externalKey.Length; i++)
{
this.externalKey[i] = new TGI(externalKey[i]);
}
this.delayloadKey = new TGI[delayloadKey != null ? delayloadKey.Length : 0];
for (int i = 0; i < this.delayloadKey.Length; i++)
{
this.delayloadKey[i] = new TGI(delayloadKey[i]);
}
this.objectData = new ObjectData[objectData.Length];
for (int i = 0; i < objectData.Length; i++)
{
this.objectData[i] = new ObjectData(objectData[i]);
}
this.version = version;
this.lodData = lodData;
this.blendMap = blendMap;
this.vectorData = vectorData;
}
public BGEO(BinaryReader br)
{
br.BaseStream.Position = 0;
this.contextVersion = br.ReadUInt32();
uint publicKeyCount = br.ReadUInt32();
uint externalKeyCount = br.ReadUInt32();
uint delayloadKeyCount = br.ReadUInt32();
uint objectCount = br.ReadUInt32();
this.publicKey = new TGI[publicKeyCount];
for (int i = 0; i < publicKeyCount; i++)
{
this.publicKey[i] = new TGI(br, TGI.TGIsequence.ITG);
}
this.externalKey = new TGI[externalKeyCount];
for (int i = 0; i < externalKeyCount; i++)
{
this.externalKey[i] = new TGI(br, TGI.TGIsequence.ITG);
}
this.delayloadKey = new TGI[delayloadKeyCount];
for (int i = 0; i < delayloadKeyCount; i++)
{
this.delayloadKey[i] = new TGI(br, TGI.TGIsequence.ITG);
}
this.objectData = new ObjectData[objectCount];
for (int i = 0; i < objectCount; i++)
{
this.objectData[i] = new ObjectData(br);
}
this.tag = br.ReadUInt32();
if (tag != BitConverter.ToUInt32(Encoding.ASCII.GetBytes("BGEO"), 0))
{
throw new ApplicationException("Not a valid BGEO file!");
}
version = br.ReadUInt32();
if (version != 0x00000600)
{
throw new ApplicationException("Invalid version of BGEO file!");
}
uint lodCount = br.ReadUInt32();
uint totalVertexCount = br.ReadUInt32();
uint totalVectorCount = br.ReadUInt32();
lodData = new LOD[lodCount];
for (int i = 0; i < lodCount; i++)
{
lodData[i] = new LOD(br);
}
blendMap = new Blend[totalVertexCount];
int runningIndex = 0;
int lodCounter = 0;
int previousLODnumVerts = 0;
for (int i = 0; i < totalVertexCount; i++)
{
if (i == lodData[lodCounter].NumberVertices + previousLODnumVerts)
{
runningIndex += (int)lodData[lodCounter].NumberDeltaVectors;
previousLODnumVerts += (int)lodData[lodCounter].NumberVertices;
lodCounter++;
if (lodCounter > 3) lodCounter = 3;
}
blendMap[i] = new Blend(br, runningIndex);
runningIndex += blendMap[i].Offset;
}
vectorData = new Vector[totalVectorCount];
for (int i = 0; i < totalVectorCount; i++)
{
vectorData[i] = new Vector(br);
}
}
public void Write(BinaryWriter bw)
{
bw.Write(contextVersion);
if (publicKey == null) publicKey = new TGI[0];
bw.Write(publicKey.Length);
if (externalKey == null) externalKey = new TGI[0];
bw.Write(externalKey.Length);
if (delayloadKey == null) delayloadKey = new TGI[0];
bw.Write(delayloadKey.Length);
bw.Write(objectData.Length);
for (int i = 0; i < publicKey.Length; i++)
{
this.publicKey[i].Write(bw, TGI.TGIsequence.ITG);
}
for (int i = 0; i < externalKey.Length; i++)
{
this.externalKey[i].Write(bw, TGI.TGIsequence.ITG);
}
for (int i = 0; i < delayloadKey.Length; i++)
{
this.delayloadKey[i].Write(bw, TGI.TGIsequence.ITG);
}
for (int i = 0; i < objectData.Length; i++)
{
this.objectData[i].Write(bw);
}
bw.Write(tag);
bw.Write(version);
if (lodData == null) lodData = new LOD[0];
bw.Write(lodData.Length);
if (blendMap == null) blendMap = new Blend[0];
bw.Write(blendMap.Length);
if (vectorData == null) vectorData = new Vector[0];
bw.Write(vectorData.Length);
for (int i = 0; i < lodData.Length; i++)
{
if (lodData[i] != null) { lodData[i].Write(bw); } else { (new LOD()).Write(bw); }
}
for (int i = 0; i < blendMap.Length; i++)
{
if (blendMap[i] != null) { blendMap[i].Write(bw); } else { (new Blend()).Write(bw); }
}
for (int i = 0; i < vectorData.Length; i++)
{
if (vectorData[i] != null) { vectorData[i].Write(bw); } else { (new Vector()).Write(bw); }
}
}
public enum BlendMapFlags
{
// Apply a position delta to this vertex.
FlagPosDelta = 1,
// Apply a normal delta to this vertex. It follows the position delta if both
// are present.
FlagNorDelta = 2,
FlagAll = 3,
PackIndexScale = 4,
};
const byte packIndexShift = 2;
public class ObjectData
{
uint position;
uint length;
internal ObjectData(ObjectData other)
{
this.position = other.position;
this.length = other.length;
}
internal ObjectData(uint position, uint length)
{
this.position = position;
this.length = length;
}
internal ObjectData(BinaryReader br)
{
this.position = br.ReadUInt32();
this.length = br.ReadUInt32();
}
internal void Write(BinaryWriter bw)
{
bw.Write(this.position);
bw.Write(this.length);
}
}
public class LOD : IEquatable<LOD>
{
uint indexBase;
uint numVerts;
uint numDeltaVectors;
public uint IndexBase { get { return indexBase; } }
public uint NumberVertices { get { return numVerts; } }
public uint NumberDeltaVectors { get { return numDeltaVectors; } }
public LOD()
{
this.indexBase = 0;
this.numVerts = 0;
this.numDeltaVectors = 0;
}
public LOD(LOD basis)
: this(basis.indexBase, basis.numVerts, basis.numDeltaVectors) { }
public LOD(uint indexBase, uint numVerts, uint numDeltaVectors)
{
this.indexBase = indexBase;
this.numVerts = numVerts;
this.numDeltaVectors = numDeltaVectors;
}
internal LOD(BinaryReader br)
{
this.indexBase = br.ReadUInt32();
this.numVerts = br.ReadUInt32();
this.numDeltaVectors = br.ReadUInt32();
}
internal void Write(BinaryWriter bw)
{
bw.Write(this.indexBase);
bw.Write(this.numVerts);
bw.Write(this.numDeltaVectors);
}
public bool Equals(LOD other)
{
return this.indexBase.Equals(other.indexBase)
&& this.numVerts.Equals(other.numVerts)
&& this.numDeltaVectors.Equals(other.numDeltaVectors);
}
public override bool Equals(object obj) { return obj is LOD && Equals(obj as LOD); }
public override int GetHashCode() { return indexBase.GetHashCode() ^ numVerts.GetHashCode() ^ numDeltaVectors.GetHashCode(); }
}
public class Blend : IEquatable<Blend>
{
bool positionDelta, normalDelta;
short offset;
internal int index;
public bool PositionDelta { get { return positionDelta; } }
public bool NormalDelta { get { return normalDelta; } }
public int Offset { get { return offset; } }
public int Index { get { return index; } }
public Blend()
{
this.positionDelta = false;
this.normalDelta = false;
this.offset = 0;
this.index = 0;
}
public Blend(Blend basis)
: this(basis.positionDelta, basis.normalDelta, basis.offset, basis.index) { }
public Blend(bool posDelta, bool normDelta, short off, int ind)
{
this.positionDelta = posDelta;
this.normalDelta = normDelta;
this.offset = off;
this.index = ind;
}
public Blend(bool posDelta, bool normDelta, short off)
{
this.positionDelta = posDelta;
this.normalDelta = normDelta;
this.offset = off;
this.index = 0;
}
internal Blend(BinaryReader br, int lastIndex)
{
short tmp = br.ReadInt16();
positionDelta = (tmp & (ushort)BlendMapFlags.FlagPosDelta) > 0;
normalDelta = (tmp & (ushort)BlendMapFlags.FlagNorDelta) > 0;
offset = (short)(tmp >> packIndexShift);
index = offset + lastIndex;
}
internal void Write(BinaryWriter bw)
{
short tmp = (short)((offset << packIndexShift) + (positionDelta ? (short)BlendMapFlags.FlagPosDelta : 0) +
(normalDelta ? (short)BlendMapFlags.FlagNorDelta : 0));
bw.Write(tmp);
}
public bool Equals(Blend other)
{
return (this.positionDelta == other.positionDelta) & (this.normalDelta == other.normalDelta) &
(this.offset == other.offset);
}
public override bool Equals(object obj) { return obj is Blend && Equals(obj as Blend); }
}
public class Vector : IEquatable<Vector>
{
ushort[] vector;
const float scaleFactor = 8000f;
public float[] TranslatedVector
{
get
{
float[] translated = new float[3];
for (int i = 0; i < 3; i++)
{
int tmp = ((vector[i] ^ 0x8000) << 16) >> 16; //flip sign bit and move it to high bit
translated[i] = tmp / scaleFactor;
}
return translated;
}
}
public Vector()
{
this.vector = new ushort[] { 0, 0, 0 };
}
public Vector(Vector basis)
: this(basis.vector) { }
public Vector(ushort[] vector)
{
this.vector = new ushort[vector.Length];
for (int i = 0; i < vector.Length; i++)
{
this.vector[i] = vector[i];
}
}
public Vector(float[] vector)
{
this.vector = new ushort[vector.Length];
for (int i = 0; i < vector.Length; i++)
{
int tmp = (Convert.ToInt32(vector[i] * scaleFactor)) ^ 0x8000;
this.vector[i] = BitConverter.ToUInt16(BitConverter.GetBytes(tmp), 0);
}
}
public Vector(BinaryReader br)
{
this.vector = new ushort[3];
this.vector[0] = br.ReadUInt16();
this.vector[1] = br.ReadUInt16();
this.vector[2] = br.ReadUInt16();
}
internal void Write(BinaryWriter bw)
{
bw.Write(this.vector[0]);
bw.Write(this.vector[1]);
bw.Write(this.vector[2]);
}
public bool Equals(Vector other)
{
return this.vector.SequenceEqual(other.vector);
}
public override bool Equals(object obj) { return obj is Vector && Equals(obj as Vector); }
}
}
}