forked from d07RiV/SNOParser
/
webgl.cpp
796 lines (772 loc) · 30.4 KB
/
webgl.cpp
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
#include "webgl.h"
#include "textures.h"
#include "types/GameBalance.h"
#include "types/Actor.h"
#include "types/Particle.h"
#include "types/AnimSet.h"
#include "itemlib.h"
#include "strings.h"
#include <set>
#include <bitset>
#include <algorithm>
static Vector Read(Anim::Type::DT_VECTOR3D const& v) {
return Vector(v.x00_X, v.x04_Y, v.x08_Z);
}
static Vector Read(Appearance::Type::DT_VECTOR3D const& v) {
return Vector(v.x00_X, v.x04_Y, v.x08_Z);
}
static Quaternion Read(Appearance::Type::Quaternion const& q) {
return Quaternion(q.x00_DT_VECTOR3D.x00_X, q.x00_DT_VECTOR3D.x04_Y,
q.x00_DT_VECTOR3D.x08_Z, q.x0C);
}
static Quaternion Read(Anim::Type::Quaternion16 const& q) {
return Quaternion(q.x00.value(), q.x02.value(), q.x04.value(), q.x06.value());
}
std::map<std::string, uint32> get_item_icons(bool leg = false);
bool isParent(Appearance::Type::Structure& data, int from, int to) {
while (from != to && from >= 0) {
from = data.x010_BoneStructures[from].x040;
}
return from == to;
}
bool isRelated(Appearance::Type::Structure& data, int a, int b) {
return isParent(data, a, b) || isParent(data, b, a);
}
namespace WebGL {
struct Triangle {
Index verts[3];
BoneIndex bones[9];
uint32 numBones = 0;
uint32 group = -1;
uint32 unused;
};
static const int MAX_BONES = 128;
static const int MAX_GROUP_BONES = 24;
struct TriangleSet {
typedef std::pair<int, Triangle*> Ref;
std::set<Ref> queue;
std::vector<Triangle*> list[MAX_BONES];
void add(Triangle* tri) {
for (uint32 i = 0; i < tri->numBones; ++i) {
list[tri->bones[i]].push_back(tri);
}
queue.emplace(tri->unused = tri->numBones, tri);
}
Triangle* pop() {
auto it = queue.begin();
Triangle* tri = it->second;
queue.erase(it);
return tri;
}
void reduce(BoneIndex bone) {
for (Triangle* tri : list[bone]) {
if (tri->group != -1) continue;
auto it = queue.find(Ref(tri->unused, tri));
if (it == queue.end()) throw Exception("uh oh");
queue.erase(it);
queue.emplace(--tri->unused, tri);
}
}
bool empty() const {
return queue.empty();
}
};
struct ObjectData {
struct Group {
std::bitset<MAX_BONES> bones;
std::map<Index, Index> vertices;
std::vector<Index> indices;
};
std::vector<Group> groups;
Appearance::Type::SubObject& original;
ObjectData(Appearance::Type::SubObject& object, uint32 maxBones)
: original(object)
{
if (!object.x020_VertInfluences.size()) {
groups.emplace_back();
auto& group = groups[0];
for (uint32 i = 0; i < maxBones; ++i) group.bones.set(i);
for (uint32 i = 0; i < object.x010_FatVertexs.size(); ++i) group.vertices[i] = i;
for (auto idx : object.x038_short) group.indices.push_back(idx);
return;
}
std::vector<Triangle> triangles;
for (uint32 i = 0; i + 3 <= object.x038_short.size(); i += 3) {
triangles.emplace_back();
auto& tri = triangles.back();
for (uint32 j = 0; j < 3; ++j) {
tri.verts[j] = object.x038_short[i + j];
for (auto& inf : object.x020_VertInfluences[tri.verts[j]].x00_Influences) {
if (inf.x04) tri.bones[tri.numBones++] = inf.x00;
}
}
std::sort(tri.bones, tri.bones + tri.numBones);
tri.numBones = std::unique(tri.bones, tri.bones + tri.numBones) - tri.bones;
}
while (true) {
TriangleSet tris;
for (auto& tri : triangles) {
if (tri.group == -1) {
tris.add(&tri);
}
}
if (tris.empty()) break;
groups.emplace_back();
auto& group = groups.back();
uint32 used = 0;
while (!tris.empty()) {
Triangle* tri = tris.pop();
if (tri->unused + used > MAX_GROUP_BONES) break;
for (uint16 v : tri->verts) {
auto it = group.vertices.find(v);
if (it == group.vertices.end()) {
Index index = group.vertices.size();
group.vertices[v] = index;
group.indices.push_back(index);
} else {
group.indices.push_back(it->second);
}
}
tri->group = groups.size() - 1;
for (uint32 i = 0; i < tri->numBones; ++i) {
if (!group.bones.test(tri->bones[i])) {
tris.reduce(tri->bones[i]);
++used;
group.bones.set(tri->bones[i]);
}
}
}
}
}
};
static Archive* texArchive = nullptr;
uint32 AddTexture(uint32 texId, bool gray = false) {
if (texArchive && texId != -1 && !texArchive->has(texId)) {
Image image = GameTextures::get(texId);
if (image) image.write(texArchive->create(texId), gray ? ImageFormat::PNGGrayscale : ImageFormat::PNG);
}
return texId == -1 ? 0 : texId;
}
void DoWriteModel(File& file, SnoFile<Appearance>& app) {
ModelHeader header;
header.numBones = app->x010_Structure.x010_BoneStructures.size();
header.numHardpoints = app->x010_Structure.x0F0_Hardpoints.size();
header.numAppearances = app->x1C0_AppearanceLooks.size();
header.numMaterials = app->x1B0_AppearanceMaterials.size();
header.numObjects = app->x010_Structure.x088_GeoSets[0].x10_SubObjects.size();
header.boneOffset = sizeof header;
header.hardpointOffset = header.boneOffset + header.numBones * sizeof(Bone);
header.objectOffset = header.hardpointOffset + header.numHardpoints * sizeof(Hardpoint);
header.materialOffset = header.objectOffset + header.numObjects * sizeof(Object);
uint32 vCount = 0;
for (auto& object : app->x010_Structure.x088_GeoSets[0].x10_SubObjects) {
for (auto& v : object.x010_FatVertexs) {
header.center += Read(v.x00_Position);
++vCount;
}
}
if (vCount) header.center /= static_cast<float>(vCount);
uint32 fileSize = header.materialOffset + sizeof(Material) * header.numAppearances * header.numMaterials;
file.write(header);
std::vector<Matrix> bones;
for (auto& src : app->x010_Structure.x010_BoneStructures) {
Bone dst;
memset(dst.name, 0, sizeof dst.name);
strcpy(dst.name, src.x000_Text);
dst.parent = src.x040;
dst.transform.translate = Read(src.x06C_PRSTransform.x10_DT_VECTOR3D);
dst.transform.rotate = Read(src.x06C_PRSTransform.x00_Quaternion);
dst.transform.scale = src.x06C_PRSTransform.x1C;
dst.capsuleOffset = 0;
dst.constraintOffset = 0;
bones.push_back(Matrix::scale(1.0f / dst.transform.scale) *
dst.transform.rotate.conj().matrix() * Matrix::translate(-dst.transform.translate));
if (src.x118_CollisionShapes.size()) {
dst.capsuleOffset = fileSize;
fileSize += sizeof(CapsuleInfo);
}
if (src.x128_ConstraintParameters.size()) {
dst.constraintOffset = fileSize;
fileSize += sizeof(Constraint);
}
file.write(dst);
}
for (auto& src : app->x010_Structure.x0F0_Hardpoints) {
Hardpoint dst;
memset(dst.name, 0, sizeof dst.name);
strcpy(dst.name, src.x00_Text);
dst.parent = src.x40;
dst.transform = Matrix::translate(Read(src.x44_PRTransform.x10_DT_VECTOR3D)) *
Read(src.x44_PRTransform.x00_Quaternion).matrix();
if (dst.parent != -1) dst.transform = bones[dst.parent] * dst.transform;
dst.transform.transpose();
file.write(dst);
}
std::map<std::string, uint32> materials;
for (uint32 i = 0; i < header.numMaterials; ++i) {
materials[app->x1B0_AppearanceMaterials[i].x00_Text] = i;
}
std::vector<ObjectData> objects;
for (auto& src : app->x010_Structure.x088_GeoSets[0].x10_SubObjects) {
objects.emplace_back(src, app->x010_Structure.x010_BoneStructures.size());
auto& data = objects.back();
Object dst;
dst.material = materials[src.x05C_Text];
dst.numGroups = data.groups.size();
dst.groupOffset = fileSize;
fileSize += sizeof(ObjectGroup) * dst.numGroups;
file.write(dst);
}
for (auto& mat : app->x1B0_AppearanceMaterials) {
for (auto& sub : mat.x88_SubObjectAppearances) {
uint32 fpos = file.tell();
uint32 texDiffuse = -1, texSpecular = -1, texTintBase = -1, texTintMask = -1;
for (auto& tex : sub.x18_UberMaterial.x58_MaterialTextureEntries) {
switch (tex.x00) {
case 1:
texDiffuse = tex.x08_MaterialTexture.x00_TexturesSno;
break;
case 5:
texSpecular = tex.x08_MaterialTexture.x00_TexturesSno;
break;
case 11:
texTintBase = tex.x08_MaterialTexture.x00_TexturesSno;
break;
case 54:
texTintMask = tex.x08_MaterialTexture.x00_TexturesSno;
break;
}
}
float alpha = 1.0f;
for (size_t i = 0; i < sub.x10_TagMap.size() - 1; ++i) {
if (sub.x10_TagMap[i] == 196864) {
alpha = *(float*) &sub.x10_TagMap[i + 1];
}
}
Material dst;
dst.diffuse = AddTexture(texDiffuse);
dst.specular = AddTexture(texSpecular);
dst.tintBase = AddTexture(texTintBase);
dst.tintMask = AddTexture(texTintMask);
file.write(dst);
}
}
for (auto& src : app->x010_Structure.x010_BoneStructures) {
if (src.x118_CollisionShapes.size()) {
CapsuleInfo dst;
auto& col = src.x118_CollisionShapes[0];
dst.start = Read(col.x30_DT_VECTOR3D);
dst.end = Read(col.x3C_DT_VECTOR3D);
dst.radius = col.x48;
file.write(dst);
}
if (src.x128_ConstraintParameters.size()) {
Constraint dst;
auto& data = src.x128_ConstraintParameters[0];
dst.parent.rotate = Read(data.x078_PRTransform.x00_Quaternion);
dst.parent.translate = Read(data.x078_PRTransform.x10_DT_VECTOR3D);
dst.local.rotate = Read(data.x094_PRTransform.x00_Quaternion);
dst.local.translate = Read(data.x094_PRTransform.x10_DT_VECTOR3D);
dst.angles[0] = data.x0B0;
dst.angles[1] = data.x0B4;
dst.angles[2] = data.x0B8;
dst.angles[3] = data.x0BC;
dst.angles[4] = data.x0C0;
file.write(dst);
}
}
for (auto& object : objects) {
for (auto& group : object.groups) {
uint32 pos = file.tell();
ObjectGroup dst;
dst.numBones = group.bones.count();
dst.boneOffset = fileSize;
fileSize += sizeof(BoneIndex) * dst.numBones;
dst.numVertices = group.vertices.size();
dst.vertexOffset = fileSize;
fileSize += sizeof(Vertex) * dst.numVertices;
dst.numIndices = group.indices.size();
dst.indexOffset = fileSize;
fileSize += sizeof(Index) * ((dst.numIndices + 1) / 2) * 2;
file.write(dst);
}
}
for (auto& object : objects) {
for (auto& group : object.groups) {
uint32 boneMap[MAX_BONES];
uint32 numBones = 0;
for (uint32 i = 0; i < app->x010_Structure.x010_BoneStructures.size(); ++i) {
if (group.bones.test(i)) {
file.write(BoneIndex(i));
boneMap[i] = numBones++;
}
}
std::vector<uint32> vertices(group.vertices.size());
for (auto it : group.vertices) {
vertices[it.second] = it.first;
}
for (uint32 index : vertices) {
auto& src = object.original.x010_FatVertexs[index];
auto* inf = (object.original.x020_VertInfluences.size() > index ?
&object.original.x020_VertInfluences[index].x00_Influences : nullptr);
Vertex dst;
memset(&dst, 0, sizeof dst);
dst.position = Read(src.x00_Position);
dst.normal[0] = src.x0C_Normal.x00_X - 128;
dst.normal[1] = src.x0C_Normal.x01_Y - 128;
dst.normal[2] = src.x0C_Normal.x02_Z - 128;
dst.texcoord[0] = src.x18_TexCoords[0].x00_U - 0x8000;
dst.texcoord[1] = src.x18_TexCoords[0].x02_V - 0x8000;
if (inf) {
for (uint32 j = 0; j < 3; ++j) {
dst.bone_idx[j] = boneMap[(*inf)[j].x00];
dst.bone_weight[j] = (*inf)[j].x04;
}
} else {
dst.bone_weight[0] = 1;
}
file.write(dst);
}
file.write(group.indices.data(), group.indices.size() * sizeof(Index));
if (group.indices.size() & 1) {
file.write16(0);
}
}
}
}
void WriteModel(std::string const& name) {
DoWriteModel(File("WebGL" / name + ".model", "wb"), SnoFile<Appearance>(name));
}
void DoWriteAnimation(File& file, Anim::Type& anim) {
auto& perm = anim.x28_AnimPermutations[0];
AnimationSequence header;
header.numFrames = perm.x090;
header.velocity = perm.x048_Velocity;
header.numBones = perm.x088_BoneNames.size();
header.animationOffset = sizeof header;
uint32 fileSize = header.animationOffset + header.numBones * sizeof(AnimationCurve);
file.write(header);
for (size_t i = 0; i < header.numBones; ++i) {
AnimationCurve curve;
memset(&curve, 0, sizeof curve);
strcpy(curve.bone, perm.x088_BoneNames[i].x00_Text);
curve.numTranslations = perm.x0A0_TranslationCurves[i].x10_TranslationKeies.size();
curve.numRotations = perm.x0B0_RotationCurves[i].x10_RotationKeies.size();
curve.numScales = perm.x0C0_ScaleCurves[i].x10_ScaleKeies.size();
curve.translationOffset = fileSize;
fileSize += curve.numTranslations * sizeof(TranslationKey);
curve.rotationOffset = fileSize;
fileSize += curve.numRotations * sizeof(RotationKey);
curve.scaleOffset = fileSize;
fileSize += curve.numScales * sizeof(ScaleKey);
file.write(curve);
}
for (size_t i = 0; i < header.numBones; ++i) {
for (auto& key : perm.x0A0_TranslationCurves[i].x10_TranslationKeies) {
file.write(key.x00);
file.write(Read(key.x04_DT_VECTOR3D));
}
for (auto& key : perm.x0B0_RotationCurves[i].x10_RotationKeies) {
file.write(key.x00);
file.write(Read(key.x04_Quaternion16));
}
for (auto& key : perm.x0C0_ScaleCurves[i].x10_ScaleKeies) {
file.write(key.x00);
file.write(key.x04);
}
}
}
void WriteAnimation(std::string const& name) {
SnoFile<Anim> anim(name);
if (!anim) return;
File file("WebGL" / name + ".anim", "wb");
DoWriteAnimation(file, *anim);
}
void DumpActorData(Archive& mdl, Archive& ani, uint32 aid, uint32 raid = 0) {
SnoFile<Actor> actor(Actor::name(aid));
if (!actor) return;
SnoFile<Appearance> app(actor->x014_AppearanceSno.name());
if (app) DoWriteModel(mdl.create(raid ? raid : aid), app);
File animFile = SnoLoader::Load<AnimSet>(actor->x068_AnimSetSno.name());
if (animFile) {
json::Value value;
json::Visitor::printExStrings = false;
json::BuilderVisitor visitor(value);
AnimSet::parse(animFile, &visitor);
visitor.onEnd();
for (auto& sub : value) {
if (sub.type() != json::Value::tObject) continue;
for (auto& val : sub) {
uint32 id = val.getInteger();
SnoFile<Anim> anim(Anim::name(id));
if (anim) {
DoWriteAnimation(ani.create(id), *anim);
}
}
}
}
}
uint32 FixEmitter(SnoFile<Actor>& actor) {
if (1 || actor->x014_AppearanceSno.name() == "Emitter") {
if (!actor->x080_MsgTriggeredEvents.size()) return actor->x000_Header.id;
auto& name = actor->x080_MsgTriggeredEvents[0].x004_TriggerEvent.x02C_SNOName;
if (name.type() != "Particle") return actor->x000_Header.id;
SnoFile<Particle> particle(name.name());
if (!particle) return actor->x000_Header.id;
if (!Actor::name(particle->x338_ActorSno)) return actor->x000_Header.id;
//printf("Fixing emitter: %s -> %s\n", actor.name().c_str(), Actor::name(particle->x338_ActorSno));
return particle->x338_ActorSno;
}
}
bool DumpItemActor(Archive& mdl, Archive& ani, std::set<uint32>& done, uint32 aid, bool fixEmitter, uint32 raid = 0) {
if (done.count(aid)) return true;
SnoFile<Actor> actor(Actor::name(aid));
if (!actor) return false;
done.insert(aid);
std::map<uint32, uint32> tags;
for (uint32 i = 1; i + 3 <= actor->x060_TagMap.size(); i += 3) {
tags[actor->x060_TagMap[i + 1]] = actor->x060_TagMap[i + 2];
}
if (tags[94240] && !raid) {
bool hasSelf = false;
bool hasAny = false;
for (uint32 id = 94208; id <= 94219; ++id) {
if (tags[id] == aid) { hasSelf = true; continue; }
if (DumpItemActor(mdl, ani, done, tags[id], fixEmitter, tags[id])) hasAny = true;
}
for (uint32 id = 94720; id <= 94731; ++id) {
if (tags[id] == aid) { hasSelf = true; continue; }
if (DumpItemActor(mdl, ani, done, tags[id], fixEmitter, tags[id])) hasAny = true;
}
if (!hasSelf && hasAny) return true;
}
DumpActorData(mdl, ani, fixEmitter ? FixEmitter(actor) : aid, raid ? raid : aid);
return true;
}
void DumpActorLook(json::Value& value, uint32 aid) {
SnoFile<Actor> actor(Actor::name(aid));
if (!actor) return;
SnoFile<Appearance> app(actor->x014_AppearanceSno.name());
if (!app) return;
auto& val = value[fmtstring("%d", aid)]["looks"];
uint32 index = 0;
for (auto& look : app->x1C0_AppearanceLooks) {
val[fmtstring("%u", HashName(look.x00_Text))] = index++;
}
}
void ClassInfo() {
SnoFile<GameBalance> gmb("Characters");
json::Value value;
for (auto& hero : gmb->x088_Heros) {
DumpActorLook(value, hero.x108_ActorSno);
DumpActorLook(value, hero.x10C_ActorSno);
}
json::write(File("webgl_look.js", "w"), value, json::mJS);
}
void AddActorInfo(json::Value& values, uint32 aid, bool hair, bool fixEmitter, uint32 orig = 0) {
SnoFile<Actor> actor(Actor::name(aid));
if (!actor) return;
uint32 emitId = (fixEmitter ? FixEmitter(actor) : aid);
if (emitId != actor->x000_Header.id) {
AddActorInfo(values, emitId, hair, false, aid);
return;
}
auto& value = values[fmtstring("%d", orig ? orig : aid)];
if (hair) {
std::map<uint32, uint32> tags;
for (uint32 i = 1; i + 3 <= actor->x060_TagMap.size(); i += 3) {
tags[actor->x060_TagMap[i + 1]] = actor->x060_TagMap[i + 2];
}
value["hair"] = tags[66564];
}
SnoFile<AnimSet> animSet(actor->x068_AnimSetSno.name());
if (animSet) {
uint32 anim = 0;
for (auto& tm : animSet->x010_AnimSetTagMaps) {
if (!tm.x08_TagMap.size() || tm.x08_TagMap.size() != tm.x08_TagMap[0] * 3 + 1) continue;
for (uint32 i = 1; i + 3 <= tm.x08_TagMap.size(); i += 3) {
if (Anim::name(tm.x08_TagMap[i + 2])) {
anim = tm.x08_TagMap[i + 2];
}
}
}
if (anim) {
value["animation"] = anim;
}
}
SnoFile<Appearance> app(actor->x014_AppearanceSno.name());
if (app) {
json::Value enable(json::Value::tObject);
uint32 index = 0;
for (auto& object : app->x010_Structure.x088_GeoSets[0].x10_SubObjects) {
if (strcmp(object.x05C_Text, "FX_EMIT")) {
enable[fmtstring("%d", index)] = 0;
}
++index;
}
value["enable"] = enable;
}
}
json::Value MakeActor(SnoFile<Actor>& actor, std::map<uint32, uint32>& tags, json::Value& actors, bool fixEmitter, bool right = false, bool hair = false) {
static int charActors[] = {3301, 3285, 6544, 6526, 6485, 6481, 4721, 4717, 75207, 74706, 238284, 238286};
if (tags[94240]) {
json::Value res(json::Value::tObject);
for (uint32 id = (right ? 94720 : 94208), idx = 0; idx < 12; ++id, ++idx) {
if (Actor::name(tags[id])) {
res[fmtstring("%d", charActors[idx])] = tags[id];
AddActorInfo(actors, tags[id], hair, fixEmitter);
}
}
return res;
} else {
AddActorInfo(actors, actor->x000_Header.id, hair, fixEmitter);
return actor->x000_Header.id;
}
}
void FillItemInfo(json::Value& dst, json::Value& actors, GameBalance::Type::Item& item, std::string const& type, std::string const& slot) {
SnoFile<Actor> actor(item.x108_ActorSno.name());
if (!actor) return;
std::map<uint32, uint32> tags;
for (uint32 i = 1; i + 3 <= actor->x060_TagMap.size(); i += 3) {
tags[actor->x060_TagMap[i + 1]] = actor->x060_TagMap[i + 2];
}
uint32 aid = 0;
if (slot == "head") {
dst["actor"] = MakeActor(actor, tags, actors, false, false, true);
}
if (slot == "legs" || slot == "feet" || slot == "torso" || slot == "hands") {
dst["armortype"] = tags[66560];
dst["look"] = tags[66561];
}
if (slot == "shoulders") {
dst["actor"] = MakeActor(actor, tags, actors, false);
dst["actor_r"] = MakeActor(actor, tags, actors, false, true);
}
if (slot == "mainhand" || slot == "offhand" || slot == "twohand" || slot == "onehand") {
if (type == "quiver") return;
dst["actor"] = MakeActor(actor, tags, actors, type == "source" || type == "mojo");
}
}
void ItemInfo() {
json::Value out;
json::Value items;
json::Value actors;
json::parse(File("webgl_actors.js"), actors, json::mJS);
json::parse(File("itemtypes.js"), items, json::mJS);
Logger::begin(items["itemById"].getMap().size(), "Dumping items");
std::set<uint32> done;
for (auto& kv : items["itemById"].getMap()) {
Logger::item(kv.first.c_str());
std::string type = kv.second["type"].getString();
std::string slot = items["itemTypes"][type]["slot"].getString();
auto* item = ItemLibrary::get(kv.first);
if (!item) continue;
auto& dst = out[kv.first];
FillItemInfo(dst, actors, *item, type, slot);
}
json::Value final;
for (auto& kv : out.getMap()) {
if (kv.second.type() == json::Value::tObject) {
final[kv.first] = kv.second;
}
}
json::write(File("webgl_items.js", "w"), final, json::mJS);
json::write(File("webgl_actors.js", "w"), actors, json::mJS);
}
void DumpActorSets(Archive& ans, uint32 aid) {
SnoFile<Actor> actor(Actor::name(aid));
if (!actor) return;
SnoFile<AnimSet> animSet(actor->x068_AnimSetSno.name());
if (!animSet) return;
File& dst = ans.create(aid);
for (auto& tm : animSet->x010_AnimSetTagMaps) {
uint32 count = tm.x08_TagMap[0];
if (!count || tm.x08_TagMap.size() != count * 3 + 1) {
dst.write32(0);
continue;
}
dst.write32(count);
for (uint32 i = 0; i < count; ++i) {
dst.write32(tm.x08_TagMap[i * 3 + 2]);
dst.write32(tm.x08_TagMap[i * 3 + 3]);
}
}
}
void AnimSets() {
Archive ans;
SnoFile<GameBalance> gmb("Characters");
Logger::begin(gmb->x088_Heros.size(), "Dumping characters");
for (auto& hero : gmb->x088_Heros) {
Logger::item(hero.x000_Text);
DumpActorSets(ans, hero.x108_ActorSno);
DumpActorSets(ans, hero.x10C_ActorSno);
}
Logger::end();
ans.write(File("animsets.wgz", "wb"), true);
}
void GenericItems() {
Logger::begin(3, "Loading assets");
Logger::item("textures"); Archive tex;// (File("textures.wgz"), false);
Logger::item("models"); Archive mdl;// (File("models.wgz"), true);
Logger::item("animations"); Archive ani;// (File("animations.wgz"), true);
Logger::end();
//texArchive = &tex;
json::Value actors;
json::parse(File("d3gl_actors.js"), actors, json::mJS);
std::set<uint32> done;
json::Value items;
json::Value itemsout;
json::parse(File("itemtypes.js"), items, json::mJS);
Logger::begin(items["itemById"].getMap().size(), "Dumping items");
for (auto& kv : items["itemById"].getMap()) {
Logger::item(kv.first.c_str());
auto* item = ItemLibrary::get(kv.first);
if (!item) continue;
std::string type = kv.second["type"].getString();
std::string slot = items["itemTypes"][type]["slot"].getString();
if (type != "mojo") continue;
DumpItemActor(mdl, ani, done, item->x108_ActorSno, type == "source" || type == "mojo");
FillItemInfo(itemsout[item->x000_Text], actors, *item, type, slot);
}
Logger::end();
json::Value genitems;
json::Value generic;
json::parse(File("d3gl_items.js"), generic, json::mJS);
for (auto& kv : generic.getMap()) {
std::string id = kv.first;
if (!kv.second.has("type")) continue;
std::string type = kv.second["type"].getString();
if (!items["itemTypes"].has(type)) {
Logger::log("unknown type: %s", type.c_str());
continue;
}
std::string slot = items["itemTypes"][type]["slot"].getString();
auto* item = ItemLibrary::get(id);
if (!item || !Actor::name(item->x108_ActorSno)) continue;
if (type != "mojo") continue;
auto& dst = genitems[id];
dst["name"] = Strings::get("Items", id);
dst["type"] = type;
dst["promo"] = true;
FillItemInfo(dst, actors, *item, type, slot);
}
json::write(File("extra_actors.js", "w"), actors, json::mJS);
json::write(File("extra_items.js", "w"), genitems, json::mJS);
json::write(File("extra_items_orig.js", "w"), itemsout, json::mJS);
//return;
Logger::begin(3, "Writing assets");
//Logger::item("textures"); tex.write(File("textures.wgz", "wb"), false);
//Logger::item("models"); mdl.write(File("models.wgz", "wb"), true);
//Logger::item("animations"); ani.write(File("animations.wgz", "wb"), true);
Logger::end();
texArchive = nullptr;
}
void AllItems(bool models, bool info, bool load) {
Archive tex, mdl, ani;
if (models && load) {
Logger::begin(3, "Loading assets");
Logger::item("textures"); tex.load(File("textures.wgz"), false);
Logger::item("models"); mdl.load(File("models.wgz"), true);
Logger::item("animations"); ani.load(File("animations.wgz"), true);
Logger::end();
}
texArchive = &tex;
//SnoFile<GameBalance> gmb("Characters");
//Logger::begin(gmb->x088_Heros.size(), "Dumping characters");
//for (auto& hero : gmb->x088_Heros) {
// Logger::item(hero.x000_Text);
// DumpActorData(mdl, ani, hero.x108_ActorSno);
// DumpActorData(mdl, ani, hero.x10C_ActorSno);
//}
//Logger::end();
json::Value items, itemsout, actors;
//if (info) json::parse(File("d3gl_actors.js"), actors, json::mJS);
//json::parse(File("itemtypes.js"), items, json::mJS);
//Logger::begin(items["itemById"].getMap().size(), "Dumping items");
std::set<uint32> done;
//for (auto& kv : items["itemById"].getMap()) {
// Logger::item(kv.first.c_str());
// auto* item = ItemLibrary::get(kv.first);
// if (!item) continue;
// std::string type = kv.second["type"].getString();
// std::string slot = items["itemTypes"][type]["slot"].getString();
// if (models) DumpItemActor(mdl, ani, done, item->x108_ActorSno, type == "source" || type == "mojo");
// if (info) FillItemInfo(itemsout[item->x000_Text], actors, *item, type, slot);
//}
{
auto* item = ItemLibrary::get("Unique_Offhand_001_x1");
std::string type = "dagger";
std::string slot = "onehand";
if (models) DumpItemActor(mdl, ani, done, item->x108_ActorSno, type == "source" || type == "mojo");
if (info) FillItemInfo(itemsout[item->x000_Text], actors, *item, type, slot);
}
//Logger::end();
/*
json::Value genitems;
json::Value generic;
json::parse(File("d3gl_items.js"), generic, json::mJS);
for (auto& kv : generic.getMap()) {
std::string id = kv.first;
if (!kv.second.has("type")) continue;
std::string type = kv.second["type"].getString();
if (!items["itemTypes"].has(type)) {
Logger::log("unknown type: %s", type.c_str());
continue;
}
std::string slot = items["itemTypes"][type]["slot"].getString();
auto* item = ItemLibrary::get(id);
if (!item || !Actor::name(item->x108_ActorSno)) continue;
if (models) DumpItemActor(mdl, ani, done, item->x108_ActorSno, type == "source" || type == "mojo");
auto& dst = genitems[id];
dst["name"] = Strings::get("Items", id);
dst["type"] = type;
dst["promo"] = true;
FillItemInfo(dst, actors, *item, type, slot);
}*/
if (info) {
json::write(File("extra_actors.js", "w"), actors, json::mJS);
//json::write(File("extra_items.js", "w"), genitems, json::mJS);
json::write(File("extra_items_orig.js", "w"), itemsout, json::mJS);
}
if (models) {
Logger::begin(3, "Writing assets");
Logger::item("textures"); tex.write(File("textures.wgz", "wb"), false);
Logger::item("models"); mdl.write(File("models.wgz", "wb"), true);
Logger::item("animations"); ani.write(File("animations.wgz", "wb"), true);
Logger::end();
texArchive = nullptr;
}
}
void AddPhysics() {
json::Value actors, phys;
json::Value items;
json::parse(File("d3gl_actors.js"), actors, json::mJS);
json::parse(File("d3gl_items.js"), items, json::mJS);
std::map<uint32, std::string> actor2item;
for (auto& kv : items.getMap()) {
auto& actor = kv.second["actor"];
std::string name = Strings::get("Items", kv.first);
if (actor.type() == json::Value::tObject) {
for (auto& sub : actor) {
actor2item[sub.getInteger()] = name;
}
} else {
actor2item[actor.getInteger()] = name;
}
}
for (auto it = actors.begin(); it != actors.end(); ++it) {
uint32 id = atoi(it.key().c_str());
SnoFile<Actor> actor(Actor::name(id));
if (!actor) continue;
uint32 physics = actor->x2B4_PhysicsSno;
if (Physics::name(physics)) {
(*it)["physics"] = physics;
if (actor2item.count(id)) {
phys[actor2item[id]] = Physics::name(physics);
}
}
}
json::write(File("d3gl_actors.js", "w"), actors, json::mJS);
json::write(File("d3gl_physics.js", "w"), phys, json::mJS);
}
}