-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgar5_parser.lua
More file actions
776 lines (613 loc) · 19.2 KB
/
gar5_parser.lua
File metadata and controls
776 lines (613 loc) · 19.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
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
assert("Lua 5.3" == _VERSION)
assert(arg[1], "\n\n[ERROR] no input file\n\n")
local DBG = arg[2] or nil
require("mod_binary_reader")
local r = BinaryReader
local ok, NAMES = pcall(dofile, "names_i686.luac")
if not ok then
NAMES = dofile("names_x64.luac")
end
--[[ header ]]---------------------------------------------
local read_FOUR
local read_GEC2
--[[ data ]]-------------------------------------------------------------------
local wr
if DBG then
wr = io.write
else
DBG = assert(io.open(arg[1] .. ".lua", "w+b"))
wr = function(s) DBG:write(s) end
end
local wf = function(fmt, ...)
wr(string.format(fmt, ...))
end
local eol = function()
wr("\n")
end
local tab_level = 0
local function tab(l)
l = l or tab_level
wr((" "):rep(l))
tab_level = l
end
local hash_unknown = {}
local function get_name(id)
local n = NAMES[id]
if n then
return n, true
else
local hash = string.format("0x%08X", id)
if not hash_unknown[hash] then
hash_unknown[hash] = true
end
return hash, false
end
end
local function read_string(is_not_quoted)
local sz = r:uint16()
local str = r:str(sz)
if 0 == is_not_quoted then
wr(str)
else
wr("\"" .. str .. "\"")
end
end
local function read_guid()
local t = {
"\"",
r:hex32(1), "-", r:hex16(1), "-",
r:hex16(1), "-", r:hex16(), "-",
r:hex32(), r:hex16(),
"\""
}
wr(table.concat(t))
end
local function read_time(time)
local t = time or (r:uint32() << 32) | r:uint32()
local u = math.floor(t / 10000000 - 11644473600)
local str = os.date("%Y-%m-%d %H:%M:%S", u)
if time then
return str
else
wr(str)
end
end
local function read_matrix(level)
for i = 1, 4 do
eol()
tab(level)
for j = 1, 4 do
wf("%11.3f, ", r:float())
end
end
-- tab(level)
end
local function read_float(n)
if n then
local t = { [n] = nil }
for i = 1, n do
t[i] = r:float()
end
wr("{ " .. table.concat(t, ", ") .. " }")
else
wr(r:float())
end
end
local function read_enum()
local hash, val = r:uint32(), r:sint32()
wf("{ %d, %q }", val, get_name(hash))
end
local function read_script_proxy(comment)
local u = r:uint16()
assert(1 == u, "\n\npos -> " .. r:pos()-2 .. ", var ~= 1 == " .. u)
local sz = r:uint16()
wr("\"" .. r:str(sz) .. "\"")
if comment then wf(" --[[%s]] ", comment) end
end
local function read_class(level)
local class = get_name(r:uint32())
local version = r:uint16()
local size = r:uint32()
local pos = r:pos()
tab(level)
wr(("[\"%s\"] = { -- c v%d, [%d], off 0x%08X\n"):format(class, version, size, pos))
return class, version, size
end
local function read_array(level, no_block, count)
if not no_block then wr("{\n") end
if not count then count = r:uint32() end
for i = 1, count do
tab(level+1)
wf("[%d] = { -- of %d, off 0x%08X\n", i, count, r:pos())
read_FOUR(level+2)
tab(level+1)
wf("}%s -- array %d/%d\n", i<count and "," or "", i, count)
end
if not no_block then tab(level); wr("}") end
end
local function read_arr_string(level, count)
count = count or r:uint32()
wf("{ -- %d entries\n", count)
for i = 1, count do
tab(level+1)
wf("[%d] = ", i)
read_string()
wf("%s -- %d/%d\n", i<count and "," or "", i, count)
end
tab(level)
wr("}")
end
local function read_arr_guid(level)
local count = r:uint32()
wf("{ -- %d entries\n", count)
for i = 1, count do
tab(level+1)
wf("[%d] = ", i)
read_guid()
wf("%s -- %d/%d\n", i<count and "," or "", i, count)
end
tab(level)
wr("}")
end
local function read_arr_float(level)
local count = r:uint32()
wf("{ -- %d entries\n", count)
for i = 1, count do
tab(level+1)
wf("[%d] = ", i)
read_float()
wf("%s -- %d/%d\n", i<count and "," or "", i, count)
end
tab(level)
wr("}")
end
local function read_arr_uint(level, count)
count = count or r:uint32()
wf("{ -- %d entries\n", count)
for i = 1, count do
tab(level+1)
wf("[%d] = ", i)
wr(r:uint32())
wf("%s -- %d/%d\n", i<count and "," or "", i, count)
end
tab(level)
wr("}")
end
local function read_unknown_bytes(size, comment)
if size == 0 then return end
local max = 128
local start = r:pos()
local sz = math.min(size, 128)
local hex, str = {}, {}
for i = 1, sz do
local b = r:uint8()
table.insert(hex, string.format("%02X", b))
if b < 32 or b > 126 then b = 46 end
table.insert(str, string.char(b))
end
wr("--[[ SKIP " .. (comment or "") .. "\n")
local i = 1
while i < sz do
local j = math.min(i+15, sz)
wr(string.format("%08X: ", start + i - 1))
wr(table.concat(hex, " ", i, j))
local rep = 15 - sz + i
if rep < 16 then
wr(string.rep(" ", rep))
end
wr(" | ")
wr(table.concat(str, "", i, j))
wr("\n")
i = i + 16
end
if size > max then
wr("... \n" .. size .. " bytes ")
elseif size > 16 then
wr(size .. " bytes ")
end
wr("--]]\n")
-- skip this data
r:seek(start + size)
end
--[[ hash ]]----------------------------------------------------------------
local hash = {}
hash["bool"] = function() wr(r:uint32() == 1 and "true" or "false") end
hash["float"] = function() wr(r:float()) end
hash["int"] = function() wr(r:sint32()) end
hash["unsigned int"] = function() wr(r:uint32()) end
hash["__int64"] = function() wr(r:uint64()) end
hash["class bCGuid"] = function() read_guid() end
hash["class eCEntityProxy"] = function() read_guid() end
hash["class eCTemplateEntityProxy"] = function() read_guid() end
hash["class bCMatrix"] = function() read_float(16) end
hash["class bCMotion"] = function() read_float(7) end
hash["class bCVector"] = function() read_float(3) end
hash["class bCFloatColor"] = function() read_float(3) end
hash["class bCRange1"] = function() read_float(2) end
hash["class eCWeatherOverwrite"] = function(level) tab(level); read_float(3) end
hash["class bCDateTime"] = function() wr("\""); read_time(); wr("\"") end
hash["class eCLocString"] = function()
local hex = r:hex32(1)
read_string()
wf(" --[[L(0x%s)]]", hex)
end
hash["class bCUnicodeString"] = function()
local tochar = string.char
local function utf16_to_utf8(u16)
local u8 = {}
if u16 < 127 then
u8[1] = tochar(u16 & 255)
elseif u16 <= 2047 then
u8[1] = tochar(u16 >> 6 & 31 | 192)
u8[2] = tochar(u16 & 63 | 128)
else
u8[1] = tochar(u16 >> 12 & 15 | 224)
u8[2] = tochar(u16 >> 6 & 63 | 128)
u8[3] = tochar(u16 & 63 | 128)
end
return table.concat(u8)
end
local sz = r:uint16()
local str = {}
while sz > 0 do
table.insert(str, utf16_to_utf8(r:uint16()))
sz = sz - 1
end
wr("\"" .. table.concat(str) .. "\"")
end
hash["class bCString"] = function() read_string() end
hash["class gCEffectProxy"] = function() read_string() end
hash["class gCStateGraphTransition"] = function() read_string() end
hash["class eCVegetationDefinitionProxy"] = function() read_string() end
hash["class eCCollisionShapeList"] = function(level) read_array(level) end
hash["class bTSceneRefPropertyArray<class gCStateGraphEventFilter *>"] = function(level) read_array(level) end
hash["class bTSceneRefPtrArray<class gCStateGraphState *>"] = function(level) read_array(level) end
--[[ unknown hash funcs ]]-----------------------------------------------------
hash["0xBD7025AF"] = function(level, size)
local start = r:pos()
local unk1 = r:uint16()
assert(1 == unk1)
local unk2 = r:float()
local count = r:uint32()
-- 10 bytes
wf("%10.2f, -- %d*8 + 2*3 floats\n", unk2, count)
for i = 1, count do
for j = 1, 8 do
wf("%10.2f, ", r:float())
end
wr("\n")
end
for i = 1, 2 do
for j = 1, 3 do
wf("%10.2f, ", r:float())
end
wr("\n")
end
end
-- ["Shapes"] ... ["class eCVegModShapeSphere"]
hash["0x889E4D02"] = function(level, size, cn)
wf("--[[< %s >]] ", cn)
read_array(level)
end
-- ["Nodes"] in XXX_Spline.sec
hash["0x6660C286"] = function(level, size) read_array(level) end
--[[ func ]]-------------------------------------------------------------------
local function execute_func(class_name, level, size, not_found_comment)
local c = hash[class_name]
if c then
c(level, size, class_name)
else
wr("nil\n")
read_unknown_bytes(size, not_found_comment .. ": " .. class_name)
end
end
local function read_props(level, count)
for i = 1, count do
local class_name, ok = get_name(r:uint32())
local var_name = get_name(r:uint32())
local size = r:uint32()
tab(level)
-- wf("[\"%s\"] = --[[%s %d]] ", var_name, class_name, size)
wf("[\"%s\"] = ", var_name)
local not_found_comment
if ok then
not_found_comment = "not implemented var (not in script)"
else
not_found_comment = "unknown hash (name)"
end
if class_name:find("enum ") == 1 then
read_enum()
elseif class_name:find("class bTObjArray<class bCString>") == 1 then
read_arr_string(level)
elseif class_name:find("class bTSceneObjArray<class eCEntityProxy>") == 1
or class_name:find("class bTObjArray<class eCTemplateEntityProxy>") == 1 then
read_arr_guid(level)
elseif class_name:find("class bTSceneObjArray") == 1
or class_name:find("class bTObjArray") == 1
or class_name:find("class bTSceneRefPtrArray") == 1 then
read_array(level)
elseif class_name:find("class eTResourceProxy") == 1 then
read_string()
elseif class_name:find("class eCScriptProxy") == 1
or class_name:find("class gCScriptProxy") == 1 then
read_script_proxy()
elseif class_name:find("class gCLetterLocString") == 1 then
local hash = r:uint32()
read_string()
wf(" --[[hash 0x%08X]]", hash)
elseif class_name:find("class bTValArray<float>") == 1 then
read_arr_float(level)
elseif class_name:find("class gCFlightPath") == 1
or class_name:find("class gCProjectile") == 1
or class_name:find("class eCWeatherZoneShape") == 1 then
wr("{\n")
read_FOUR(level+1)
tab(level)
wr("}")
else
execute_func(class_name, level, size, not_found_comment)
end
wf("%s -- <%s> %d/%d\n", i<count and "," or "", class_name, i, count)
end
end
local function read_dynamic1(level)
local c = get_name(r:uint32())
local v = r:uint16()
local sz = r:uint32()
local pos = r:pos()
tab(level)
wr(("[\"%s\"] = { -- c v%d, [%d], off 0x%08X\n"):format(c, v, sz, pos))
if "class gCEmbeddedLayer" == c then
tab(level+1)
wf("[\"%s\"] = {\n", get_name(r:uint32()))
assert(0 == r:uint32())
read_FOUR(level+2)
tab(level+1)
wr("} -- gCEmbeddedLayer\n")
elseif "class eCScene" == c then
tab(level)
local unk1 = r:uint32()
io.stderr:write("-- eCScene val ??? " .. unk1 .. " ???\n")
wf("-- ??? %d ???\n", unk1)
read_FOUR(level+1)
elseif "class gCNavigation_PS" == c then
read_array(level, true)
elseif "class gCRoutine" == c then
read_array(level, true)
elseif "0xBD7025AF" == c
or "class eCWeatherOverwrite" == c then
hash[c](level+1)
elseif "class gCItem_PS" == c then
tab(level+1)
wf("[\"%s\"] = ", get_name(r:uint32()))
assert(0 == r:uint32())
wr(r:uint32())
wr("\n")
elseif "class gCInventory_PS" == c then
read_array(level, true)
local count = r:uint16()
local unk1 = r:uint8(); assert(0 == unk1)
local unk2 = r:uint8()
if unk2 > 0 then
read_array(level, true, count)
end
elseif "class gCParty_PS" == c then
tab(level+1); wr("string1 = "); read_string(); wr(",\n")
tab(level+1); wr("int1 = " .. r:uint32() .. ",\n")
tab(level+1); wr("guid1 = "); read_guid(); wr("\n")
elseif "class eCPrefabMesh" == c then
tab(level+1)
read_float(16)
eol()
elseif "class gCStateGraphState" == c then
read_unknown_bytes(16)
wr("arr1 = {\n")
read_array(level, true)
wr("},\narr2 = {\n")
read_array(level, true) -- TODO: check this
wr("}\n")
elseif "class gCStateGraphTransition" == c then
read_string()
eol()
else
read_unknown_bytes(sz, "unknown < " .. c .. " >")
end
tab(level)
wf("} -- <%s>, off 0x%08X \n", c, r:pos())
end
local function read_eCEntity(level)
local c, v, sz = read_class(level)
-- wrote "{..."
local start = r:pos()
level = level + 1
tab(level); wr("guid2 = "); read_guid(); wr(",\n")
tab(level); wr("string1 = "); read_string(); wr(",\n")
read_unknown_bytes(6, "eCEntity")
tab(level); wr("eCEntity1 = {\n")
local count = r:uint8()
for i = 1, count do
tab(level)
wf("[%d] = { -- eCE d1 of %d, off 0x%08X\n", i, count, r:pos())
read_FOUR(level+1)
tab(level)
wf("}, -- eCE d1 %d/%d\n", i, count)
end
tab(level); wr("}, --eCEntity1\n")
tab(level); wr("eCEntity2 = {\n")
count = r:uint32()
for i = 1, count do
tab(level)
wf("[%d] = { -- eCE d2 of %d\n", i, count)
read_FOUR(level+1)
tab(level)
wf("}%s -- eCE d2 %d/%d\n", i<count and "," or "", i, count)
end
tab(level); wr("} --eCEntity2\n")
read_unknown_bytes(start + sz - r:pos(), "eCEntity left")
level = level - 1
tab(level)
wr("} -- eCEntity\n")
end
local function read_eCDynamicEntity(size, level, class_name, version)
local start = r:pos()
tab(level)
wf("[\"%s\"] = { -- v%d, [%d], off 0x%08X\n", class_name, version, size, start)
level = level + 1
tab(level); wr("matrix1 = {"); read_matrix(level+1); wr("},\n")
tab(level); wr("matrix2 = {"); read_matrix(level+1); wr("},\n")
tab(level); wr("bb_min = "); read_float(3); wr(",\n")
tab(level); wr("bb_max = "); read_float(3); wr(",\n")
tab(level); wr("bb_mid = "); read_float(3); wr(",\n")
tab(level); wr("diameter = "); read_float(); wr(",\n")
tab(level); wr("guid1 = "); read_guid(); wr(",\n")
read_unknown_bytes(start + size - r:pos(), "eCDynamicEntity")
level = level - 1
tab(level)
wf("}, -- <%s>\n", class_name)
read_eCEntity(level)
end
local function read_eCAnimation3Controller_PS(size, level)
local start = r:pos()
tab(level); wr("string1 = "); read_string(); wr(",\n")
read_unknown_bytes(6)
tab(level); wr("string2 = "); read_string(); wr(",\n")
tab(level); wr("string3 = "); read_string(); wr(",\n")
read_unknown_bytes(4)
tab(level); wr("string4 = "); read_string(); wr(",\n")
read_unknown_bytes(6)
tab(level); wr("string5 = "); read_string(); wr(",\n")
read_unknown_bytes(start + size - r:pos())
-- eCAnimation3Base_PS
local c, v, sz = read_class()
read_unknown_bytes(sz)
tab(level)
wr("}\n")
end
local function read_eCTemplateEntity(size, level)
tab(level)
wf("[\"class eCTemplateEntity\"] = { -- [%d], off 0x%08X\n", size, r:pos())
read_unknown_bytes(size)
tab(level)
wr("}, -- <class eCTemplateEntity>\n")
read_eCEntity(level, size)
end
local function read_dynamic2(level)
local class_hash = r:uint32()
local class_name = get_name(class_hash)
local version = r:uint16()
local size = r:uint32()
if class_hash == 0x36182D05 then
read_eCDynamicEntity(size, level+1, class_name, version)
elseif class_hash == 0xD256AADC then
read_eCAnimation3Controller_PS(size, level+1)
elseif class_hash == 0x60DE515C then
read_eCTemplateEntity(size, level+1)
else
read_unknown_bytes(size, "unknown dynamic2 entry")
end
end
read_GEC2 = function(level)
local c, v, sz = read_class(level)
local start = r:pos()
local count = r:uint16()
level = level+1
tab(level)
wf("prop = { -- off 0x%08X, %d entries\n", start, count)
read_props(level+1, count)
local pos = r:pos()
local readed = pos - start
local left = start + sz - pos
tab(level)
wf("}, -- prop end <%s>, %d <- %d bytes\n", c, readed, left)
pos = r:pos()
local code = r:uint32()
tab(level)
wf("data = { -- off 0x%08X, :%d:\n", pos, code)
if code == 2 then
read_dynamic2(level)
elseif code == 1 then
read_dynamic1(level+1)
elseif code == 0 then
--
else
assert(false, "\n\ncode " .. code .. "\n\n")
end
tab(level)
wf("} -- data <%s>\n", c)
level = level-1
pos = r:pos()
readed = pos - start
left = start + sz - pos
tab(level)
wf("} -- <%s>, off 0x%08X, %d <- %d bytes\n", c, pos, readed, left)
assert(0 == left, "\n\n\n\n")
end
read_FOUR = function(level)
local FOUR = r:str(4)
if "GEC2" == FOUR then
read_GEC2(level)
else
print("\n!!! unknown FOUR: " .. FOUR .. "\n\n")
assert(false)
end
end
local function read_GAR5()
r:idstring("GAR5")
r:idstring("\x20\x00\x00\x00")
if "STB" == r:str(3) then
local ver = r:uint8()
io.stderr:write("\nthe file looks like a localization resource (ver ")
io.stderr:write(ver)
io.stderr:write(").\ntry to use w_strings.lua\n")
else
r:seek(44)
r:idstring("GAR5")
r:idstring("\x20\x00\x00\x00")
local four = r:str(4)
if "GTP0" == four then -- .tpl
wr("template = {\n")
read_unknown_bytes(8)
read_FOUR(0)
wr("}\n")
elseif "GEC2" == four then -- .wrl
wr("wrl = {\n")
read_GEC2(0)
wr("}\nstrings = ")
local count = r:uint16()
read_arr_string(1, count)
eol()
else -- .sec
r:seek(r:pos() - 4)
local count = r:uint16()
for i = 1, count do
read_string(0)
wf(" = { -- %d/%d entries\n", i, count)
read_FOUR(0)
wf("}%s\n", i<count and "," or "")
end
end
eol()
end
end
local function show_unknown_hash()
local hash_sorted = {}
for k, v in pairs(hash_unknown) do
table.insert(hash_sorted, k)
end
if #hash_sorted > 0 then
table.sort(hash_sorted)
print("unknown hashes:")
for i = 1, #hash_sorted do
print(hash_sorted[i])
end
print()
end
end
--[[ main ]]-------------------------------------------------------------------
r:open(arg[1], "rb")
read_GAR5()
r:close()
if not DBG then
DBG:close()
end
show_unknown_hash()