-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcsxfunction.cpp
997 lines (912 loc) · 28 KB
/
csxfunction.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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
#include "csxfunction.h"
#include "csxutils.h"
#include "csximage.h"
#include "csxfile.h"
#include <locale>
#include <codecvt>
#include <algorithm>
#include <QString>
CSXFunction::CSXFunction()
{
}
CSXFunction::~CSXFunction()
{
for (operation_t* oper : m_operations)
delete oper;
}
#define OP_CAT_LOCAL 0x00
#define OP_CAT_NOP 0x01 //TODO
#define OP_CAT_STACK 0x02
#define OP_CAT_END_MATH 0x03
#define OP_CAT_LABEL 0x04
#define OP_CAT_NOP2 0x05 //TODO
#define OP_CAT_JUMP 0x06
#define OP_CAT_JUMP_COND 0x07
#define OP_CAT_CALL 0x08
#define OP_CAT_RET 0x09
#define OP_CAT_GET 0x0a
#define OP_CAT_FIELD_BY_OFFSET 0x0b
#define OP_CAT_MATH 0x0c
#define OP_CAT_LOGIC 0x0d
#define OP_CAT_CMP 0x0e
#define NAME_ON_TABLE 0x80000000
void CSXFunction::decompile_from_bin(Stream* str)
{
m_define_offset = str->getPosition() - 1;
m_name = CSXUtils::read_unicode_string(str);
str->read(&m_params_count, sizeof(uint32_t));
for (uint32_t i=0 ; i<m_params_count ; ++i)
m_variables.push_back(parse_var_def(str));
int op_idx = 0;
bool loop_fl = true;
while (loop_fl)
{
uint32_t offset = str->getPosition();
if ((CSXFile::get_function_name(offset).length() > 0) || (str->atEnd()))
break;
m_addr_to_op_idx[offset] = op_idx;
uint8_t op_cat;
str->read(&op_cat, sizeof(uint8_t));
m_cur_op = nullptr;
switch (op_cat)
{
case OP_CAT_LOCAL:
parse_local_op(str);
break;
case OP_CAT_NOP:
{
m_cur_op = new operation_t;
m_cur_op->op = EFunctionOperation::NOT_OPERATION;
m_operations.push_back(m_cur_op);
}
break;
case OP_CAT_STACK:
parse_stack_op(str);
break;
case OP_CAT_END_MATH:
parse_pop_op(str);
break;
case OP_CAT_LABEL:
parse_label_op(str);
break;
case OP_CAT_NOP2:
{
m_cur_op = new operation_t;
m_cur_op->op = EFunctionOperation::NOT_OPERATION;
m_operations.push_back(m_cur_op);
}
break;
case OP_CAT_JUMP:
parse_jump_op(str);
m_jumps_op_idx.push_back(op_idx);
break;
case OP_CAT_JUMP_COND:
parse_jump_cond_op(str);
m_jumps_op_idx.push_back(op_idx);
break;
case OP_CAT_CALL:
parse_call_op(str);
break;
case OP_CAT_RET:
parse_ret_op(str);
//loop_fl = false;
break;
case OP_CAT_GET:
parse_get_op(str);
break;
case OP_CAT_FIELD_BY_OFFSET:
{
m_cur_op = new operation_t;
m_cur_op->op = EFunctionOperation::FIELD_BY_OFFSET;
m_operations.push_back(m_cur_op);
}
break;
case OP_CAT_MATH:
parse_math_op(str);
break;
case OP_CAT_LOGIC:
parse_logic_op(str);
break;
case OP_CAT_CMP:
parse_cmd_op(str);
break;
default:
throw_ex("Unknown operation category: ", op_cat, offset);
}
//printf("%s\n", oper_to_string(m_cur_op, op_idx).c_str());
++op_idx;
}
for (uint32_t jump_op : m_jumps_op_idx)
{
operation_t* oper = m_operations[jump_op];
int32_t offset = (int32_t)oper->field_name.value_integer;
int32_t jump_size;
if (oper->op == EFunctionOperation::JUMP)
jump_size = 5;
else
jump_size = 6;
for (auto kk : m_addr_to_op_idx)
if (kk.second == jump_op)
{
jump_op = kk.first;
break;
}
offset = jump_op + jump_size + offset;
oper->field_name.value_integer = m_addr_to_op_idx[offset];
m_labels.push_back(oper->field_name.value_integer);
}
}
void CSXFunction::listing_to_stream(Stream* str)
{
str->writeWideStr(L"Function " + QString::fromStdU16String(m_name).toStdWString() + L"(");
if (m_params_count > 0)
for (uint32_t i=0 ; i<m_params_count ; ++i)
{
if (m_variables[i].type == EVariableType::STRUCT)
str->writeWideStr(QString::fromStdU16String(m_variables[i].struct_name).toStdWString());
else
str->writeWideStr(QString::fromStdU16String(get_type_name(m_variables[i])).toStdWString());
str->writeWideStr(L" " + QString::fromStdU16String(m_variables[i].name).toStdWString());
if (i<(m_params_count - 1))
str->writeWideStr(L", ");
}
str->writeWideStr(L")\n");
uint32_t op_idx = 0;
for (operation_t* oper : m_operations)
{
str->writeWideStr(QString::fromStdU16String(oper_to_string(oper, op_idx++)).toStdWString() + L"\n");
}
str->writeWideStr(L"EndFunc\n\n");
}
std::u16string CSXFunction::get_type_name(variable_t &var)
{
switch (var.type)
{
case EVariableType::STRUCT:
return u"struct " + var.struct_name;
case EVariableType::REF:
return u"Reference";
case EVariableType::PARENT:
return u"parent";
case EVariableType::UNK:
return u"unknown_type";
case EVariableType::INTEGER:
return u"Integer";
case EVariableType::REAL:
return u"Real";
case EVariableType::STRING:
return u"String";
default:
return u"";
}
}
#define OP_LOC_LOCAL_VAR 0x01
#define OP_LOC_INIT_FIELD 0x02
void CSXFunction::parse_local_op(Stream* str)
{
uint32_t offset = str->getPosition();
uint8_t op_code;
m_cur_op = new operation_t;
str->read(&op_code, sizeof(uint8_t));
switch (op_code)
{
case OP_LOC_LOCAL_VAR:
//m_cur_op->op = EFunctionOperation::INIT_LOCAL;
//m_cur_op->field_name = parse_var_def(str);
m_cur_op->op = EFunctionOperation::INIT_LOCAL;
m_cur_op->field_name = parse_var_def(str);
m_variables.push_back(m_cur_op->field_name);
break;
case OP_LOC_INIT_FIELD:
m_cur_op->op = EFunctionOperation::INIT_FIELD;
m_cur_op->field_name = parse_var_def(str);
break;
default:
throw_ex("Unknown local operation: ", op_code, offset);
}
m_operations.push_back(m_cur_op);
}
#define OP_STACK_PUSH_VAL 0x00
#define OP_STACK_PUSH_VAR 0x01
#define OP_STACK_PUSH_THIS_FIELD 0x02
#define OP_STACK_PUSH_OBJ_LINK 0x03
#define OP_STACK_PUSH_UNK 0x04
#define OP_STACK_PUSH_VAR_LOC_REF 0x04
#define OP_STACK_PUSH_VAR_LOC_NAME 0x06
#define OP_PUSH_THIS 0x01
#define OP_PUSH_FIELD 0x06
void CSXFunction::parse_stack_op(Stream* str)
{
uint32_t offset = str->getPosition();
uint8_t op_code;
m_cur_op = new operation_t;
str->read(&op_code, sizeof(uint8_t));
switch (op_code)
{
case OP_STACK_PUSH_VAL:
m_cur_op->op = EFunctionOperation::PUSH_VAL;
parse_value(str);
break;
case OP_STACK_PUSH_VAR:
{
m_cur_op->op = EFunctionOperation::PUSH_VAR;
uint8_t var_loc;
uint32_t var_idx;
str->read(&var_loc, sizeof(uint8_t));
switch (var_loc)
{
case OP_STACK_PUSH_VAR_LOC_REF:
str->read(&var_idx, sizeof(uint32_t));
m_cur_op->field_name.name = m_variables[var_idx].name;
break;
case OP_STACK_PUSH_VAR_LOC_NAME:
m_cur_op->field_name.name = CSXUtils::read_unicode_string(str);
break;
default:
throw_ex("Invalid variable location!", op_code, offset);
break;
}
}
break;
case OP_STACK_PUSH_THIS_FIELD:
str->read(&op_code, sizeof(uint8_t));
switch (op_code)
{
case OP_PUSH_THIS:
m_cur_op->op = EFunctionOperation::PUSH_THIS;
break;
case OP_PUSH_FIELD:
m_cur_op->op = EFunctionOperation::PUSH_FIELD;
m_cur_op->field_name.name = CSXUtils::read_unicode_string(str);
break;
default:
throw_ex("Invalid variable location!", op_code, offset);
}
break;
case OP_STACK_PUSH_OBJ_LINK:
str->read(&op_code, sizeof(uint8_t));
m_cur_op->op = EFunctionOperation::PUSH_OBJ;
if (op_code == 0x04)
{
m_cur_op->field_name.name = CSXFile::get_linkinf(offset + 2);
str->read(&m_cur_op->field_name.value_integer, sizeof(int32_t));
}
else if (op_code == 0x06)
m_cur_op->field_name.name = CSXUtils::read_unicode_string(str);
else
throw_ex("Invalid object location!", op_code, offset);
//str->seek(4, spCurrent); //TODO
break;
case OP_STACK_PUSH_UNK: //TODO
{
uint8_t unk1;
str->read(&unk1, sizeof(uint8_t));
str->read(&m_cur_op->field_name.value_integer, sizeof(uint32_t));
m_cur_op->op = EFunctionOperation::PUSH_UNK;
}
break;
default:
throw_ex("Unknown stack operation: ", op_code, offset);
}
m_operations.push_back(m_cur_op);
}
#define OP_POP_INC 0x00
#define OP_POP_DEC 0x01
#define OP_POP_MUL 0x02
#define OP_POP_DIV 0x03
#define OP_POP_MOD 0x04
#define OP_POP_AND 0x05
#define OP_POP_OR 0x06
#define OP_POP_XOR 0x07
#define OP_POP_TO_VAR 0xff
void CSXFunction::parse_pop_op(Stream* str)
{
uint32_t offset = str->getPosition();
uint8_t pop_code;
m_cur_op = new operation_t;
str->read(&pop_code, sizeof(uint8_t));
switch (pop_code)
{
case OP_POP_INC:
m_cur_op->op = EFunctionOperation::POP_INC;
break;
case OP_POP_DEC:
m_cur_op->op = EFunctionOperation::POP_DEC;
break;
case OP_POP_MUL:
m_cur_op->op = EFunctionOperation::POP_MUL;
break;
case OP_POP_DIV:
m_cur_op->op = EFunctionOperation::POP_DIV;
break;
case OP_POP_MOD:
m_cur_op->op = EFunctionOperation::POP_MOD;
break;
case OP_POP_AND:
m_cur_op->op = EFunctionOperation::POP_AND;
break;
case OP_POP_OR:
m_cur_op->op = EFunctionOperation::POP_OR;
break;
case OP_POP_XOR:
m_cur_op->op = EFunctionOperation::POP_XOR;
break;
case OP_POP_TO_VAR:
m_cur_op->op = EFunctionOperation::POP_VAR;
break;
default:
throw_ex("Unknown pop operation: ", pop_code, offset);
break;
}
m_operations.push_back(m_cur_op);
}
void CSXFunction::parse_label_op(Stream* str)
{
//uint32_t offset = str->getPosition();
m_cur_op = new operation_t;
m_cur_op->op = EFunctionOperation::LABEL;
m_cur_op->field_name.name = CSXUtils::read_unicode_string(str);
int32_t var_loc_count;
str->read(&var_loc_count, sizeof(int32_t));
if (var_loc_count > 0)
for (int32_t i=0 ; i<var_loc_count ; ++i)
{
operation_t *local_var = new operation_t;
local_var->op = EFunctionOperation::INIT_LOCAL;
uint8_t var_type;
str->read(&var_type, sizeof(uint8_t));
local_var->field_name.type = (EVariableType)var_type;
local_var->field_name.name = CSXUtils::read_unicode_string(str);
m_operations.push_back(local_var);
}
if (m_cur_op->field_name.name.compare(u"@TRY") == 0)
{
// read offset if catch
uint8_t unk2;
str->read(&unk2, sizeof(uint8_t));
str->read(&m_cur_op->field_name.value_integer, sizeof(uint32_t));
}
m_operations.push_back(m_cur_op);
}
void CSXFunction::parse_jump_op(Stream* str)
{
m_cur_op = new operation_t;
int32_t jump_off;
str->read(&jump_off, sizeof(int32_t));
m_cur_op->op = EFunctionOperation::JUMP;
m_cur_op->field_name.value_integer = jump_off;
m_operations.push_back(m_cur_op);
}
#define OP_JUMP_RELATIVE 0x00 //TODO
#define OP_JUMP_ABSOLUTE 0x01 //TODO
void CSXFunction::parse_jump_cond_op(Stream* str)
{
uint32_t offset = str->getPosition();
m_cur_op = new operation_t;
uint8_t jump_type;
str->read(&jump_type, sizeof(uint8_t));
int32_t jump_off;
str->read(&jump_off, sizeof(int32_t));
switch (jump_type)
{
case OP_JUMP_RELATIVE:
m_cur_op->op = EFunctionOperation::JUMP_COND;
m_cur_op->field_name.value_integer = jump_off;
break;
case OP_JUMP_ABSOLUTE:
m_cur_op->op = EFunctionOperation::JUMP_COND;
m_cur_op->field_name.value_integer = jump_off;
break;
default:
throw_ex("Unknown jump operation: ", jump_type, offset);
}
m_operations.push_back(m_cur_op);
}
#define OP_CALL_UNK 0x00 //TODO
#define OP_CALL_METHOD 0x02
#define OP_CALL_FUNCTION 0x05
void CSXFunction::parse_call_op(Stream* str)
{
uint32_t offset = str->getPosition();
m_cur_op = new operation_t;
uint8_t call_type;
str->read(&call_type, sizeof(uint8_t));
switch (call_type)
{
case OP_CALL_UNK:
m_cur_op->op = EFunctionOperation::CALL_UNK;
break;
case OP_CALL_METHOD:
m_cur_op->op = EFunctionOperation::CALL_METHOD;
break;
case OP_CALL_FUNCTION:
m_cur_op->op = EFunctionOperation::CALL_FUNCTION;
break;
default:
throw_ex("Unknown call operation: ", call_type, offset);
}
uint32_t code;
str->read(&code, sizeof(uint32_t));
//if (code != 0x1)
// throw_ex("Unknown call operation: ", code, offset);
uint32_t is_on_table;
str->read(&is_on_table, sizeof(uint32_t));
if (is_on_table == NAME_ON_TABLE)
{
uint32_t name_offset = str->getPosition();
str->read(&is_on_table, sizeof(uint32_t));
m_cur_op->field_name.name = CSXFile::get_conststr(name_offset);
}
else
{
str->seek(str->getPosition() - 4, spBegin);
m_cur_op->field_name.name = CSXUtils::read_unicode_string(str);
}
m_operations.push_back(m_cur_op);
}
#define OP_RET_NONE 0x00
#define OP_RET_THIS 0x01
#define OP_RET_UNK 0x03 //TODO
void CSXFunction::parse_ret_op(Stream* str)
{
uint32_t offset = str->getPosition();
uint8_t ret_type;
m_cur_op = new operation_t;
str->read(&ret_type, sizeof(uint8_t));
switch (ret_type)
{
case OP_RET_NONE:
m_cur_op->op = EFunctionOperation::RET_NONE;
break;
case OP_RET_THIS:
m_cur_op->op = EFunctionOperation::RET_THIS;
break;
case OP_RET_UNK:
m_cur_op->op = EFunctionOperation::RET_UNK;
break;
default:
throw_ex("Unknown return operation: ", ret_type, offset);
}
m_operations.push_back(m_cur_op);
}
#define OP_GET_FIELD 0x06
void CSXFunction::parse_get_op(Stream* str)
{
uint32_t offset = str->getPosition();
uint8_t get_type;
m_cur_op = new operation_t;
str->read(&get_type, sizeof(uint8_t));
switch (get_type)
{
case OP_GET_FIELD:
m_cur_op->op = EFunctionOperation::GET_FIELD;
break;
default:
throw_ex("Unknown get operation: ", get_type, offset);
}
uint32_t is_on_table;
str->read(&is_on_table, sizeof(uint32_t));
if (is_on_table == NAME_ON_TABLE)
{
uint32_t name_offset = str->getPosition();
str->read(&is_on_table, sizeof(uint32_t));
m_cur_op->field_name.name = CSXFile::get_conststr(name_offset);
}
else
{
str->seek(str->getPosition() - 4, spBegin);
m_cur_op->field_name.name = CSXUtils::read_unicode_string(str);
}
m_operations.push_back(m_cur_op);
}
#define OP_MATH_ADD 0x00
#define OP_MATH_SUB 0x01
#define OP_MATH_MUL 0x02
#define OP_MATH_DIV 0x03
#define OP_MATH_MOD 0x04
#define OP_MATH_AND 0x05
#define OP_MATH_OR 0x06
#define OP_MATH_XOR 0x07
#define OP_MATH_UNK1 0x08 //TODO
#define OP_MATH_UNK2 0x09 //TODO
void CSXFunction::parse_math_op(Stream* str)
{
uint32_t offset = str->getPosition();
uint8_t math_type;
m_cur_op = new operation_t;
str->read(&math_type, sizeof(uint8_t));
switch (math_type)
{
case OP_MATH_ADD:
m_cur_op->op = EFunctionOperation::MATH_ADD;
break;
case OP_MATH_SUB:
m_cur_op->op = EFunctionOperation::MATH_SUB;
break;
case OP_MATH_MUL:
m_cur_op->op = EFunctionOperation::MATH_MUL;
break;
case OP_MATH_DIV:
m_cur_op->op = EFunctionOperation::MATH_DIV;
break;
case OP_MATH_MOD:
m_cur_op->op = EFunctionOperation::MATH_MOD;
break;
case OP_MATH_AND:
m_cur_op->op = EFunctionOperation::MATH_AND;
break;
case OP_MATH_OR:
m_cur_op->op = EFunctionOperation::MATH_OR;
break;
case OP_MATH_XOR:
m_cur_op->op = EFunctionOperation::MATH_XOR;
break;
case OP_MATH_UNK1:
m_cur_op->op = EFunctionOperation::MATH_UNK1;
break;
case OP_MATH_UNK2:
m_cur_op->op = EFunctionOperation::MATH_UNK2;
break;
default:
throw_ex("Unknown math operation: ", math_type, offset);
}
m_operations.push_back(m_cur_op);
}
#define OP_LOGIC_UNK1 0x01
#define OP_LOGIC_UNK2 0x02
#define OP_LOGIC_UNK3 0x03
void CSXFunction::parse_logic_op(Stream* str)
{
uint32_t offset = str->getPosition();
uint8_t lg_type;
m_cur_op = new operation_t;
str->read(&lg_type, sizeof(uint8_t));
switch (lg_type)
{
case OP_LOGIC_UNK1:
m_cur_op->op = EFunctionOperation::LOGIC_UNK1;
break;
case OP_LOGIC_UNK2:
m_cur_op->op = EFunctionOperation::LOGIC_UNK2;
break;
case OP_LOGIC_UNK3:
m_cur_op->op = EFunctionOperation::LOGIC_UNK3;
break;
default:
throw_ex("Unknown logic operation: ", lg_type, offset);
}
m_operations.push_back(m_cur_op);
}
#define OP_CMP_NE 0x00
#define OP_CMP_EQ 0x01
#define OP_CMP_LE 0x02
#define OP_CMP_LQ 0x03
#define OP_CMP_GE 0x04
#define OP_CMP_GQ 0x05
void CSXFunction::parse_cmd_op(Stream* str)
{
uint32_t offset = str->getPosition();
uint8_t cmp_type;
m_cur_op = new operation_t;
str->read(&cmp_type, sizeof(uint8_t));
switch (cmp_type)
{
case OP_CMP_NE:
m_cur_op->op = EFunctionOperation::CMP_NE;
break;
case OP_CMP_EQ:
m_cur_op->op = EFunctionOperation::CMP_EQ;
break;
case OP_CMP_LE:
m_cur_op->op = EFunctionOperation::CMP_LE;
break;
case OP_CMP_LQ:
m_cur_op->op = EFunctionOperation::CMP_LQ;
break;
case OP_CMP_GE:
m_cur_op->op = EFunctionOperation::CMP_GE;
break;
case OP_CMP_GQ:
m_cur_op->op = EFunctionOperation::CMP_GQ;
break;
default:
throw_ex("Unknown comparasion operation: ", cmp_type, offset);
}
m_operations.push_back(m_cur_op);
}
variable_t CSXFunction::parse_var_def(Stream* str)
{
uint32_t start_offset = str->getPosition();
uint8_t var_type;
str->read(&var_type, sizeof(uint8_t));
variable_t var;
switch (var_type)
{
case FIELD_TYPE_STRUCT:
{
uint32_t is_on_table;
str->read(&is_on_table, sizeof(uint32_t));
if (is_on_table == NAME_ON_TABLE)
{
uint32_t name_offset = str->getPosition();
str->read(&is_on_table, sizeof(uint32_t));
var.type = EVariableType::STRUCT;
var.struct_name = CSXFile::get_conststr(name_offset);
}
else
{
str->seek(str->getPosition() - 4, spBegin);
var.type = EVariableType::STRUCT;
var.struct_name = CSXUtils::read_unicode_string(str);
}
var.name = CSXUtils::read_unicode_string(str);
}
break;
case FIELD_TYPE_REF:
var.type = EVariableType::REF;
var.name = CSXUtils::read_unicode_string(str);
break;
case FIELD_TYPE_INTEGER:
var.type = EVariableType::INTEGER;
var.name = CSXUtils::read_unicode_string(str);
break;
case FIELD_TYPE_REAL:
var.type = EVariableType::REAL;
var.name = CSXUtils::read_unicode_string(str);
break;
case FIELD_TYPE_STRING:
var.type = EVariableType::STRING;
var.name = CSXUtils::read_unicode_string(str);
break;
case FIELD_TYPE_PARENT:
var.type = EVariableType::PARENT;
var.name = CSXUtils::read_unicode_string(str);
break;
case FIELD_TYPE_UNK:
var.type = EVariableType::UNK;
var.name = CSXUtils::read_unicode_string(str);
break;
default:
throw_ex("Unknown variable type: ", var_type, start_offset);
break;
}
return var;
}
void CSXFunction::parse_value(Stream* str)
{
uint32_t start_offset = str->getPosition();
uint8_t val_type;
str->read(&val_type, sizeof(uint8_t));
switch (val_type)
{
case FIELD_TYPE_STRUCT:
{
uint32_t is_on_table;
str->read(&is_on_table, sizeof(uint32_t));
if (is_on_table == NAME_ON_TABLE)
{
uint32_t name_offset = str->getPosition();
str->read(&is_on_table, sizeof(uint32_t));
m_cur_op->field_name.type = EVariableType::STRUCT;
m_cur_op->field_name.struct_name = CSXFile::get_conststr(name_offset);
}
else
{
str->seek(str->getPosition() - 4, spBegin);
m_cur_op->field_name.type = EVariableType::STRUCT;
m_cur_op->field_name.struct_name = CSXUtils::read_unicode_string(str);
}
}
break;
case FIELD_TYPE_REF:
m_cur_op->field_name.type = EVariableType::REF;
break;
case FIELD_TYPE_PARENT:
m_cur_op->field_name.type = EVariableType::PARENT;
break;
case FIELD_TYPE_INTEGER:
m_cur_op->field_name.type = EVariableType::INTEGER;
str->read(&m_cur_op->field_name.value_integer, sizeof(uint32_t));
break;
case FIELD_TYPE_REAL:
m_cur_op->field_name.type = EVariableType::REAL;
str->read(&m_cur_op->field_name.value_real, sizeof(double));
break;
case FIELD_TYPE_STRING:
m_cur_op->field_name.type = EVariableType::STRING;
m_cur_op->field_name.value_str = CSXUtils::read_unicode_string(str);
break;
default:
throw_ex("Unknown value type: ", val_type, start_offset);
break;
}
}
std::u16string CSXFunction::var_to_string(variable_t *var)
{
switch (var->type)
{
case EVariableType::STRUCT:
return u"struct";
case EVariableType::REF:
return u"ref";
case EVariableType::PARENT:
return u"parent";
case EVariableType::INTEGER:
return CSXUtils::to_u16string(var->value_integer) + u"i";
case EVariableType::REAL:
return CSXUtils::to_u16string(var->value_real) + u"d";
case EVariableType::STRING:
return u"\"" + var->value_str + u"\"";
default:
return u"";
}
}
std::u16string CSXFunction::oper_to_string(operation_t *oper, uint32_t op_idx)
{
if (oper == nullptr)
return u"";
std::u16string res = u"";
if(std::find(m_labels.begin(), m_labels.end(), op_idx) != m_labels.end())
res += u"@_l_" + CSXUtils::to_u16string(op_idx) + u":\n";
switch (oper->op)
{
case EFunctionOperation::INIT_LOCAL:
res += u"\tINIT_LOCAL " + get_type_name(oper->field_name) + u" " + oper->field_name.name;
break;
case EFunctionOperation::INIT_FIELD:
res += u"\tINIT_FIELD " + get_type_name(oper->field_name) + u" " + oper->field_name.name;
break;
case EFunctionOperation::PUSH_VAL:
res += u"\tPUSH_VAL " + get_type_name(oper->field_name) + u" " + var_to_string(&oper->field_name);
break;
case EFunctionOperation::PUSH_VAR:
res += u"\tPUSH_VAR " + oper->field_name.name;
break;
case EFunctionOperation::PUSH_FIELD:
res += u"\tPUSH_FIELD " + oper->field_name.name;
break;
case EFunctionOperation::PUSH_THIS:
res += u"\tPUSH_THIS " + oper->field_name.name;
break;
case EFunctionOperation::PUSH_OBJ:
res += u"\tPUSH_OBJ " + oper->field_name.name + u" " + CSXUtils::to_u16string(oper->field_name.value_integer);
break;
case EFunctionOperation::PUSH_UNK:
res += u"\tPUSH_UNK " + CSXUtils::to_u16string(oper->field_name.value_integer);
break;
case EFunctionOperation::POP_INC:
res += u"\tPOP +=";
break;
case EFunctionOperation::POP_DEC:
res += u"\tPOP -=";
break;
case EFunctionOperation::POP_MUL:
res += u"\tPOP *=";
break;
case EFunctionOperation::POP_DIV:
res += u"\tPOP /=";
break;
case EFunctionOperation::POP_MOD:
res += u"\tPOP %=";
break;
case EFunctionOperation::POP_AND:
res += u"\tPOP &=";
break;
case EFunctionOperation::POP_OR:
res += u"\tPOP |=";
break;
case EFunctionOperation::POP_XOR:
res += u"\tPOP ^=";
break;
case EFunctionOperation::POP_VAR:
res += u"\tPOP =";
break;
case EFunctionOperation::JUMP:
res += u"\tJUMP _l_" + CSXUtils::to_u16string(oper->field_name.value_integer);
break;
case EFunctionOperation::JUMP_COND:
res += u"\tJUMP_OK _l_" + CSXUtils::to_u16string(oper->field_name.value_integer);
break;
case EFunctionOperation::CALL_UNK:
res += u"\tCALL_UNK " + oper->field_name.name;
break;
case EFunctionOperation::CALL_METHOD:
res += u"\tCALL_METHOD " + oper->field_name.name;
break;
case EFunctionOperation::CALL_FUNCTION:
res += u"\tCALL " + oper->field_name.name;
break;
case EFunctionOperation::RET_NONE:
res += u"\tReturn";
break;
case EFunctionOperation::RET_THIS:
res += u"\tReturn this";
break;
case EFunctionOperation::RET_UNK:
res += u"\tReturn unknown";
break;
case EFunctionOperation::GET_FIELD:
res += u"\tGET_FIELD " + oper->field_name.name;
break;
case EFunctionOperation::FIELD_BY_OFFSET:
res += u"\tGET_FIELD_BY_OFFSET";
break;
case EFunctionOperation::MATH_ADD:
res += u"\t+";
break;
case EFunctionOperation::MATH_SUB:
res += u"\t-";
break;
case EFunctionOperation::MATH_MUL:
res += u"\t*";
break;
case EFunctionOperation::MATH_DIV:
res += u"\t/";
break;
case EFunctionOperation::MATH_MOD:
res += u"\t%";
break;
case EFunctionOperation::MATH_AND:
res += u"\tand";
break;
case EFunctionOperation::MATH_OR:
res += u"\tor";
break;
case EFunctionOperation::MATH_XOR:
res += u"\txor";
break;
case EFunctionOperation::MATH_UNK1:
res += u"\tmath_UNK2";
break;
case EFunctionOperation::MATH_UNK2:
res += u"\tmath_UNK2";
break;
case EFunctionOperation::LOGIC_UNK1:
res += u"\tlogic_unk1";
break;
case EFunctionOperation::LOGIC_UNK2:
res += u"\tlogic_unk2";
break;
case EFunctionOperation::LOGIC_UNK3:
res += u"\tlogic_unk3";
break;
case EFunctionOperation::CMP_NE:
res += u"\t!=";
break;
case EFunctionOperation::CMP_EQ:
res += u"\t==";
break;
case EFunctionOperation::CMP_LE:
res += u"\t<=";
break;
case EFunctionOperation::CMP_LQ:
res += u"\t<";
break;
case EFunctionOperation::CMP_GE:
res += u"\t>";
break;
case EFunctionOperation::CMP_GQ:
res += u"\t>=";
break;
case EFunctionOperation::NOT_OPERATION:
res += u"\tNOP";
break;
case EFunctionOperation::LABEL:
if (oper->field_name.name.length() > 0)
{
res += u"\t@" + oper->field_name.name;
if (oper->field_name.name.compare(u"@TRY") == 0)
{
res += u" IF CATCH -> JUMP " + CSXUtils::to_u16string(oper->field_name.value_integer);
}
res += u"\n";
}
break;
}
return res;
}
void CSXFunction::throw_ex(std::string msg, uint32_t arg1, uint32_t arg2)
{
printf("THROW: %s %X %X\n", msg.c_str(), arg1, arg2);
exit(1);
}