-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patha3parser.cpp
1716 lines (1358 loc) · 48.3 KB
/
a3parser.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
998
999
1000
#include "pch.h"
#include "a3parser.h"
#include "message_scrambler.h"
#include "cheat.h"
a3parser::a3parser(/*uint8_t* client_ip, */uint8_t* server_ip/*, uint8_t* victimmac, uint8_t* targetmac, const uint8_t* my_mac*/): m_client_ports_set(false), m_server_ports_set(false), m_enable_printing(false), m_parted_to_client(nullptr), m_parted_to_server(nullptr) {
//m_client_info = IpPortInfo(client_ip, server_ip);
m_server_info = IpPortInfo(server_ip, server_ip);
//memcpy(m_victimmac, victimmac, 6);
//memcpy(m_targetmac, targetmac, 6);
//memcpy(m_my_mac, my_mac, 6);
// set the server encryption & decryption key
uint8_t key[16];
for (int i = 0; i < 16; i++) {
key[i] = server_xorkey[i] ^ server_xorhash[i];
}
aes_setkey_dec(&m_server_dec, key, 128);
aes_setkey_enc(&m_server_enc, key, 128);
// set client encryption & decryption key
for (int i = 0; i < 16; i++) {
key[i] = client_xorkey[i] ^ client_xorhash[i];
}
aes_setkey_dec(&m_client_dec, key, 128);
aes_setkey_enc(&m_client_enc, key, 128);
// set be server encryption & decryption key
for (int i = 0; i < 16; i++) {
key[i] = be_server_xorkey[i] ^ be_server_xorhash[i];
}
aes_setkey_dec(&m_be_server_dec, key, 128);
aes_setkey_enc(&m_be_server_enc, key, 128);
// set be client encryption & decryption key
for (int i = 0; i < 16; i++) {
key[i] = be_client_xorkey[i] ^ be_client_xorhash[i];
}
aes_setkey_dec(&m_be_client_dec, key, 128);
aes_setkey_enc(&m_be_client_enc, key, 128);
init_default_formats();
memset(m_log_message, false, sizeof(bool) * 2 * NETWORK_MESSAGE_COUNT);
memset(m_callbacks, 0, sizeof(MsgCallback) * 2 * NETWORK_MESSAGE_COUNT);
setup_callbacks();
setup_printing();
m_out_file = fopen("log.txt", "w");
}
void a3parser::setup_callbacks() {
SET_CALLBACK(TO_SERVER, MessageChat);
ENABLE_CALLBACK(MessageAskForApplyDoDamage);
SET_CALLBACK(TO_CLIENT, MessageWeatherUpdate);
SET_CALLBACK(TO_CLIENT, MessagePlayerRole);
SET_CALLBACK(TO_SERVER, MessageMarkerCreate);
// Create/Update function for entities
SET_CALLBACK(TO_SERVER, MessageClientState);
ENABLE_CALLBACK(MessageDeleteObject);
ENABLE_CALLBACK(MessageForceDeleteObject);
ENABLE_CALLBACK(MessageDisposeObject);
SET_CALLBACK(TO_CLIENT, MessageLogin);
SET_CALLBACK(TO_CLIENT, MessageLogout);
ENABLE_CALLBACK(MessageCreateVehicle);
ENABLE_CALLBACK(MessageCreateHelicopterRTD);
ENABLE_CALLBACK(MessageCreateTurret);
ENABLE_CALLBACK(MessageCreateEntitySimple);
ENABLE_CALLBACK(MessageCreateObject);
ENABLE_CALLBACK(MessageUpdateMan);
ENABLE_CALLBACK(MessageUpdatePositionMan);
ENABLE_CALLBACK(MessageUpdateTank);
ENABLE_CALLBACK(MessageUpdatePositionTank);
ENABLE_CALLBACK(MessageUpdateCar);
ENABLE_CALLBACK(MessageUpdatePositionCar);
ENABLE_CALLBACK(MessageUpdateAirplane);
ENABLE_CALLBACK(MessageUpdatePositionAirplane);
ENABLE_CALLBACK(MessageUpdateHelicopterRTD);
ENABLE_CALLBACK(MessageUpdatePositionHelicopterRTD);
ENABLE_CALLBACK(MessageUpdateShip);
ENABLE_CALLBACK(MessageUpdatePositionShip);
ENABLE_CALLBACK(MessageUpdateAnimal);
ENABLE_CALLBACK(MessageUpdatePositionAnimal);
ENABLE_CALLBACK(MessageUpdateParachute);
ENABLE_CALLBACK(MessageUpdateParaglide);
ENABLE_CALLBACK(MessageUpdateTurret);
ENABLE_CALLBACK(MessageUpdatePositionTurret);
ENABLE_CALLBACK(MessageUpdateInvisibleVehicle);
ENABLE_CALLBACK(MessageUpdateVehicle);
ENABLE_CALLBACK(MessageUpdateObject);
ENABLE_CALLBACK(MessageUpdatePositionVehicle);
}
void a3parser::setup_printing() {
//ENABLE_PRINT(TO_SERVER, NetworkMessageMarkerCreate);
//ENABLE_PRINT(TO_SERVER, NetworkMessageMarkerDelete);
/*
//these are for when inverted mode is turned on as these spam like crazy
ENABLE_PRINT(TO_SERVER, NetworkMessageDataSignatureAnswer);
ENABLE_PRINT(TO_CLIENT, NetworkMessageTransferMissionFile);
ENABLE_PRINT(TO_CLIENT, NetworkMessageTransferFile);
ENABLE_PRINT(TO_SERVER, NetworkMessageTransferFile);
ENABLE_PRINT(TO_SERVER, NetworkMessageUpdatePositionVehicle);
*/
}
bool a3parser::parse_packet(uint8_t* pkt) {
uint8_t buf[2048];
m_packet_out = pkt;
iphdr* iph = (iphdr*)(pkt);
// if it isnt a UDP packet lets not process it further
if (iph->protocol != UDP_PROTOCOL)
return false;
// figure out if this packet is going to the client or the server
uint8_t to;
if (!memcmp(&iph->saddr, m_server_info.m_srcip, 4))
to = TO_CLIENT;
else if (!memcmp(&iph->daddr, m_server_info.m_srcip, 4))
to = TO_SERVER;
else // if neither this wasnt meant for us, not a game packet.
return false;
udphdr* udph = (udphdr*)(uintptr_t(iph) + sizeof(iphdr));
uint8_t* data = (uint8_t*)(uintptr_t(udph) + sizeof(udphdr));
int16_t datagram_size = ntohs(udph->len) - sizeof(udphdr);
// make sure its a valid packet going to the game
if (datagram_size < MSG_HEADER_LEN || datagram_size > 2048)
return false;
memcpy(buf, data, datagram_size);
m_be_message = false;
// packet going to the BEClient, ignore (for now, reversing this might be interesting)
// and might be able to be used to bypass the heartbeat (by emulating it)
uint16_t be_port = htons(2306);
if (udph->uh_dport == be_port || udph->uh_sport == be_port) {
m_be_message = true;
return false;
}
// decrypt the packet
decrypt_packet(buf);
// verify the arma crc and only continue processing if the crc verifies
if (!check_crc(buf, datagram_size))
{
printf("CRC failed\n");
print_ascii(buf, datagram_size, to);
return false;
}
// dont process battleye further, lets just dump for analysis...
if (m_be_message) {
// dump?
return false;
}
// just override any current flags/whatnot if we need to send a part message
if (to == TO_SERVER) {
if (m_parted_to_server) {
write_part_packet(m_parted_to_server);
return true;
}
}
else if (m_parted_to_client) {
printf("Writing part to client\n");
write_part_packet(m_parted_to_client);
return true;
}
int16_t len = datagram_size - MSG_HEADER_LEN;
if (!len)
return false;
ArmaMessageHeader* hdr = (ArmaMessageHeader*)buf;
uint16_t flags = hdr->flags;
if (flags & MSG_VOICE_FLAG || flags & MSG_MAGIC_FLAG || flags & MSG_DUMMY_FLAG)
return false;
// skip partial messages for now until I rebuild the packet merging
if (flags & MSG_PART_FLAG) {
return false;
printf("\nPART header\n");
print_header((uint8_t*)hdr);
if (flags & MSG_CLOSING_FLAG)
printf("CLOSING FLAG\n");
if (flags & MSG_BUNCH_FLAG)
printf("bunch flag\n");
printf("\n");
return false;
}
NetworkMessageRaw src = NetworkMessageRaw(buf, len);
bool ok = process_messages(&src, to);
if (ok) {
process_queued_messages(&src, to == TO_SERVER ? m_messages_to_server : m_messages_to_client);
// if we changed or added something we need to actually re-encrypt the packet and write it to the "actual" packet
if (src.m_write_to_packet)
write_to_packet(&src, to);
}
return ok;
}
void a3parser::process_queued_messages(NetworkMessageRaw* src, threaded_queue<msg_queue_item>& queue) {
std::lock_guard<std::mutex> lock(queue.mtx); // locks the queue so no other thread can mess with this while we are processing >:(
auto& q = queue.q;
while (!q.empty() && src->m_pos < 1200) { // maximum is 1500 octets for eth packets (lets not bother with fragmenting the eth packet), headers are about 100 - lets just be safe and not overrun the packet by using 1250 as the cut-off
auto it = q.front();
// encode the queue message and increase the message count
if (!encode_message(src, it.m_type, it.m_msg)) {
printf("Failed queuing messages!\n");
return;
}
src->m_message_count++;
// now clean up the memory allocated for the message
CleanupNetworkMessage[it.m_type](it.m_msg);
q.pop();
}
// finally change the message count in the message if we need.
int pos_backup = src->m_pos;
src->m_pos = sizeof(int);
int type;
src->Get(type, NCTSmallUnsigned);
if (type == NetworkMessageMessages) {
int pos_message_count = src->m_pos;
int message_count = 0;
src->Get(message_count, NCTSmallUnsigned);
if (message_count != src->m_message_count) {
auto tmp_buf = new unsigned char[src->m_buf_size + 24];
src->m_write_to_packet = true; // make sure this is true
int pos_after_count = src->m_pos;
// backup everything in the case that the size of the message count changes
memcpy(tmp_buf, src->m_buf + src->m_pos, pos_backup - src->m_pos);
src->m_pos = pos_message_count;
src->Put(src->m_message_count, NCTSmallUnsigned);
// restore the saved bytes in case the size changed
if (pos_after_count != src->m_pos) {
memcpy(src->m_buf + src->m_pos, tmp_buf, pos_backup - pos_after_count);
pos_backup += src->m_pos - pos_after_count; // add the delta to this (so it has the proper size incase the size changed)
}
delete[ ] tmp_buf;
}
}
else if (src->m_message_count != 1) {
auto tmp_buf = new unsigned char[src->m_buf_size + 24];
// if message count is not 1 and not type is not NetworkMessageMessages we have "encode" a NetworkMessageMessages as the first message
src->m_write_to_packet = true;
src->m_pos = sizeof(int);
int first_message_pos = src->m_pos;
// back dis up
memcpy(tmp_buf, src->m_buf + first_message_pos, pos_backup - first_message_pos);
src->Put((int)NetworkMessageMessages, NCTSmallUnsigned);
src->Put(src->m_message_count, NCTSmallUnsigned);
// and now restore it at the new pos
memcpy(src->m_buf + src->m_pos, tmp_buf, pos_backup - first_message_pos);
pos_backup += src->m_pos - first_message_pos; // add the delta to the "size"
delete[ ] tmp_buf;
}
src->m_pos = pos_backup;
src->m_size = pos_backup;
}
void a3parser::queue_message_to_server(int type, unsigned char* msg) {
auto& q = m_messages_to_server;
std::lock_guard<std::mutex> lock(q.mtx);
auto new_msg = msg_queue_item(type, msg);
q.q.push(new_msg);
}
void a3parser::queue_message_to_client(int type, unsigned char* msg) {
auto& q = m_messages_to_client;
std::lock_guard<std::mutex> lock(q.mtx);
auto new_msg = msg_queue_item(type, msg);
q.q.push(new_msg);
}
bool a3parser::process_messages(NetworkMessageRaw* src, int to) {
if (!decrypt_message(src, to))
return false;
m_message_print_indentation = "";
bool printed_source = false;
m_enable_printing = false;
// now start the actual processing of networkmessages
// first read the time from the message (useless for us)
int time = 0;
src->Get(time, NCTNone);
// save the position before reading the message, the callback might need it if it re-encodes the message
src->m_saved_pos = src->m_pos;
// now get the message type
int type = 0;
src->Get(type, NCTSmallUnsigned);
// if type is "Messages" it means this packet contains several messages, process them all.
if (type == NetworkMessageMessages) {
int message_count = 0;
src->Get(message_count, NCTSmallUnsigned);
src->m_message_count = message_count;
for (int i = 0; i < message_count; i++)
{
// save the position before reading the message, the callback might need it if it re-encodes the message
src->m_saved_pos = src->m_pos;
// get the message type
src->Get(type, NCTSmallUnsigned);
if (type >= NETWORK_MESSAGE_COUNT) {
printf("invalid message type\n");
return false;
}
if (m_log_message[to][type]/* || to == TO_SERVER*/) {
m_enable_printing = true;
if (!printed_source) {
PRINT("====================[%s]====================\n", to == TO_CLIENT ? "server" : "client");
printed_source = true;
}
}
else
m_enable_printing = false;
NetworkMessageFormat* format = &GMsgFormats[type];
if (!decode_message(to, src, format, type)) {
printf("failed to decode message %s\n", NetworkMessageNames[type]);
return false;
}
// add extra newline between messages
PRINT("\n");
}
}
else {
if (type >= NETWORK_MESSAGE_COUNT) {
printf("invalid message type\n");
return false;
}
if (m_log_message[to][type] /*|| to == TO_SERVER*/) {
m_enable_printing = true;
if (!printed_source) {
PRINT("====================[%s]====================\n", to == TO_CLIENT ? "server" : "client");
printed_source = true;
}
}
NetworkMessageFormat* format = &GMsgFormats[type];
src->m_message_count = 1;
if (!decode_message(to, src, format, type)) {
printf("failed to decode message %s\n", NetworkMessageNames[type]);
return false;
}
}
PRINT("\n");
return true;
}
bool a3parser::decode_message(int to, NetworkMessageRaw* src, NetworkMessageFormat* format, int type, unsigned char* msg) {
auto items = format->m_items;
// only set the callback and allocate space for the msg if the param is nullptr, else it might be a nested message
MsgCallback callback = nullptr;
if (!msg) {
callback = m_callbacks[to][type];
if (callback) {
msg = CreateNetworkMessage[type]();
}
}
// this means its a nested message - set it to nullptr (needs to be done AFTER above check)
// this is because we dont want to process nested messages individually, only the "mother" message.
if (msg == (unsigned char*)-1)
msg = nullptr;
PRINT_SPACED("[%s (%i)] (msg: %lx, size %i)\n", NetworkMessageNames[type], type, msg, format->m_struct_size);
// iterate all the format items for the message and process em'
for (auto it : items) {
PRINT_SPACED("%s (%s): ", it.m_name, NetworkDataTypeNames[it.m_specs.m_type]);
// parse the item depending on the datatype and compression
if (!decode_message_item(to, src, it, msg)) {
printf("Decoding item %s (%s) failed\n", it.m_name, NetworkDataTypeNames[it.m_specs.m_type]);
return false;
}
}
// DISCLAIMER: We DON'T need to clean up the msg here, its easier if we let the callback clean it up
// WHICH MEANS that: dont fucking forget to clean it all up in the callback unless you want a horrible memleak
// "delete" the actual casted object and not the buffer itself, so that the destructors gets called properly to clean up properly and free memory
if (callback)
callback(to, msg, src);
return true;
}
bool a3parser::encode_message(NetworkMessageRaw* src, int type, unsigned char* msg) {
src->m_write_to_packet = true;
src->Put(type, NCTSmallUnsigned);
auto format = &GMsgFormats[type];
auto items = format->m_items;
// iterate all the items of this message type and write each one
for (auto it : items) {
// write the item with the proper compression etc (reading it from the msg buffer, using the offset from the format
if (!encode_message_item(src, it, msg)) {
printf("Encoding items %s (%s) failed\n", it.m_name, NetworkDataTypeNames[it.m_specs.m_type]);
return false;
}
}
return true;
}
bool a3parser::remove_cur_message(NetworkMessageRaw* src) {
src->m_write_to_packet = true;
// decrease the message count (this will get written to the packet later on)
src->m_message_count--;
auto buf = src->m_buf;
int pos = src->m_pos;
int saved = src->m_saved_pos;
int delta = pos - saved; // size of the message we are removing
memcpy(buf + saved, buf + pos, src->m_size - pos);
// change the size of it, and set the pos correctly
src->m_size -= delta;
src->m_pos = saved;
return true;
}
bool a3parser::reencode_message(NetworkMessageRaw* src, int type, unsigned char* msg) {
auto tmp_buf = new unsigned char[src->m_buf_size];
int old_size = src->m_size;
int pos = src->m_pos;
int backup_size = src->m_size - pos;
memcpy(tmp_buf, src->m_buf + pos, src->m_size - pos);
// set pos to the start of the current netmessage to rewrite it
src->m_pos = src->m_saved_pos;
encode_message(src, type, msg);
// apply the delta between the old position and the new position to the size to "resize" it properly
int delta = src->m_pos - pos;
src->m_size = old_size;
src->m_size += delta;
// restore the bytes we backed up earlier
memcpy(src->m_buf + src->m_pos, tmp_buf, backup_size);
delete[ ] tmp_buf;
return true;
}
bool a3parser::encode_message_item(NetworkMessageRaw* src, NetworkMessageFormatItem format, unsigned char* msg) {
auto compression = format.m_specs.m_compression;
bool ok = true;
switch (format.m_specs.m_type) {
case NDTBool:
{
WRITE_MSG_VAL_FROM_BUFFER(bool, compression, );
break;
}
case NDTInteger:
{
WRITE_MSG_VAL_FROM_BUFFER(int, compression, );
break;
}
case NDTInt64:
{
WRITE_MSG_VAL_FROM_BUFFER(int64_t, compression, );
break;
}
case NDTFloat:
{
WRITE_MSG_VAL_FROM_BUFFER(float, compression, );
break;
}
case NDTString:
{
CREATE_VAR_FROM_BUFFER(std::string);
src->Put((char*)val.c_str(), compression);
break;
}
case NDTRawData:
{
CREATE_VAR_FROM_BUFFER(std::vector<char>);
src->Put((int)val.size(), NCTSmallUnsigned);
for (auto& it : val) {
src->Put(it, NCTNone);
}
break;
}
case NDTTime:
{
WRITE_MSG_VAL_FROM_BUFFER(Time, NCTNone, );
break;
}
case NDTVector:
{
WRITE_MSG_VAL_FROM_BUFFER(vec3, compression, );
break;
}
case NDTMatrix:
{
WRITE_MSG_VAL_FROM_BUFFER(matrix3x3, compression, );
break;
}
case NDTBoolArray:
{
CREATE_VAR_FROM_BUFFER(std::vector<char>);
int size = val.size();
src->Put(size, NCTSmallUnsigned);
if (compression == NCTDefault) {
int numbytes = (size + 7) >> 3;
auto bits = (unsigned char*)src->PutRaw(numbytes);
for (int j = 0; j < size; j++) {
if (val[j])
bits[j >> 3] |= (1 << (j & 7));
else
bits[j >> 3] &= ~(1 << (j & 7));
}
}
else {
for (auto& it : val)
src->Put((bool)it, compression);
}
break;
}
case NDTIntArray:
{
CREATE_VAR_FROM_BUFFER(std::vector<int>);
src->Put((int)val.size(), NCTSmallUnsigned);
for (auto& it : val)
src->Put(it, compression);
break;
}
case NDTFloatArray:
{
CREATE_VAR_FROM_BUFFER(std::vector<float>);
src->Put((int)val.size(), NCTSmallUnsigned);
for (auto& it : val)
src->Put(it, compression);
break;
}
case NDTStringArray:
{
CREATE_VAR_FROM_BUFFER(std::vector<std::string>);
src->Put((int)val.size(), NCTSmallUnsigned);
for (auto& it : val)
src->Put((char*)it.c_str(), compression);
break;
}
case NDTStringBArray:
{
CREATE_VAR_FROM_BUFFER(std::vector<std::string>);
src->Put((int)val.size(), NCTSmallUnsigned);
for (auto& it : val)
src->Put((char*)it.c_str(), compression);
break;
}
case NDTSentence:
{
CREATE_VAR_FROM_BUFFER(std::vector<Sentence>);
src->Put((int)val.size(), NCTSmallUnsigned);
for (auto& it : val) {
src->Put((char*)it.m_string.c_str(), compression);
src->Put(it.m_pause, compression);
}
break;
}
case NDTObject:
{
int subtype = *(int*)format.m_specs.m_def_value;
encode_message(src, subtype, msg + format.m_offset);
break;
}
case NDTObjectArray:
{
CREATE_VAR_FROM_BUFFER(std::vector<unsigned char*>);
src->Put((int)val.size(), NCTSmallUnsigned);
int subtype = *(int*)format.m_specs.m_def_value;
for (auto& it : val) {
bool not_empty = it != nullptr;
src->Put(not_empty, compression);
if (!not_empty)
continue;
encode_message(src, subtype, it);
}
break;
}
case NDTObjectSRef:
{
bool not_empty = *(bool*)(msg + format.m_offset - sizeof(int64_t));
src->Put(not_empty, compression);
if (!not_empty)
break;
int subtype = *(int*)format.m_specs.m_def_value;
encode_message(src, subtype, msg + format.m_offset);
break;
}
case NDTRef:
{
WRITE_MSG_VAL_FROM_BUFFER(NetworkId, compression, );
break;
}
case NDTRefArray:
{
CREATE_VAR_FROM_BUFFER(std::vector<NetworkId>);
src->Put((int)val.size(), NCTSmallUnsigned);
for (auto& it : val)
src->Put(it, compression);
break;
}
case NDTData:
{
printf("fuck you, no encoding for this stupid shit until I actually ever see it sent ingame\n");
ok = false;
break;
}
case NDTXUIDArray:
{
CREATE_VAR_FROM_BUFFER(std::vector<int64_t>);
src->Put((int)val.size(), NCTSmallUnsigned);
for (auto& it : val)
src->Put(it, compression);
break;
}
case NDTXNADDR:
{
auto buf = (unsigned char*)src->PutRaw(36);
memcpy(buf, msg + format.m_offset, 36);
break;
}
case NDTXOnlineStatArray:
{
CREATE_VAR_FROM_BUFFER(std::vector<XOnlineStat>);
src->Put((int)val.size(), NCTSmallUnsigned);
for (auto& it : val) {
auto buf = (unsigned char*)src->PutRaw(16);
memcpy(buf, it.m_stat, 16);
}
break;
}
case NDTLocalizedString:
{
WRITE_MSG_VAL_FROM_BUFFER(LocalizedString, compression, );
break;
}
case NDTLocalizedStringArray:
{
CREATE_VAR_FROM_BUFFER(std::vector<LocalizedString>);
src->Put((int)val.size(), NCTSmallUnsigned);
for (auto& it : val)
src->Put(it, compression);
break;
}
default:
return false;
}
return ok;
}
bool a3parser::decode_message_item(int to, NetworkMessageRaw* src, NetworkMessageFormatItem format, unsigned char* msg) {
auto compression = format.m_specs.m_compression;
bool ok = true;
switch (format.m_specs.m_type) {
case NDTBool:
{
bool val;
src->Get(val, compression);
if (val) {
PRINT("true\n");
}
else {
PRINT("false\n");
}
READ_MSG_VAL_TO_BUFFER(bool, = val);
break;
}
case NDTInteger:
{
int val;
src->Get(val, compression);
PRINT("%i\n", val);
READ_MSG_VAL_TO_BUFFER(int, = val);
break;
}
case NDTInt64:
{
int64_t val;
src->Get(val, compression);
PRINT("%lld\n", val);
READ_MSG_VAL_TO_BUFFER(int64_t, = val);
break;
}
case NDTFloat:
{
float val;
src->Get(val, compression);
PRINT("%s\n", get_float_truncated(val).c_str());
READ_MSG_VAL_TO_BUFFER(float, = val);
break;
}
case NDTString:
{
// It returns the pointer to the null-terminated string in val variable
char* val = nullptr;
src->Get(val, compression);
if (val) {
PRINT("\"%s\"\n", val);
READ_MSG_VAL_TO_BUFFER(std::string, = std::string(val));
}
else {
PRINT("\n");
READ_MSG_VAL_TO_BUFFER(std::string, = std::string());
}
break;
}
case NDTRawData:
{
int m;
src->Get(m, NCTSmallUnsigned);
if (m) {
// print the first element seperately so formatting is correct
char val = 0;
src->Get(val, NCTNone);
PRINT("{ %02x", (unsigned char)val);
READ_MSG_VAL_TO_BUFFER(std::vector<char>, .push_back(val));
for (int i = 1; i < m; i++) {
src->Get(val, NCTNone);
PRINT(", %02x", (unsigned char)val);
READ_MSG_VAL_TO_BUFFER(std::vector<char>, .push_back(val));
}
PRINT(" }\n");
}
else {
PRINT("\n");
}
break;
}
case NDTTime:
{
int val;
src->Get(val, NCTNone);
PRINT("%i\n", val);
READ_MSG_VAL_TO_BUFFER(int, = val);
break;
}
case NDTVector:
{
vec3 val;
src->Get(val, compression);
PRINT("{x: %s, y: %s, z: %s}\n", get_float_truncated(val.x).c_str(), get_float_truncated(val.y).c_str(), get_float_truncated(val.z).c_str());
READ_MSG_VAL_TO_BUFFER(vec3, = val);
break;
}
case NDTMatrix:
{
matrix3x3 val;
src->Get(val, compression);
PRINT("{ aside{x: %s, y: %s, z: %s}, up{x: %s, y: %s, z: %s}, dir{x: %s, y: %s, z: %s} }\n", get_float_truncated(val.m_aside.x).c_str(), get_float_truncated(val.m_aside.y).c_str(), get_float_truncated(val.m_aside.z).c_str(), get_float_truncated(val.m_up.x).c_str(), get_float_truncated(val.m_up.y).c_str(), get_float_truncated(val.m_up.z).c_str(), get_float_truncated(val.m_dir.x).c_str(), get_float_truncated(val.m_dir.y).c_str(), get_float_truncated(val.m_dir.z).c_str());
READ_MSG_VAL_TO_BUFFER(matrix3x3, = val);
break;
}
case NDTBoolArray:
{
int m;
src->Get(m, NCTSmallUnsigned);
if (!m) {
PRINT("\n");
break;
}
if (compression == NCTDefault) {
int numbytes = (m + 7) >> 3;
unsigned char* bits = (unsigned char*)src->GetRaw(numbytes);
PRINT("{ %i", ((bits[0 >> 3] & (1 << (0 & 7))) != 0));
READ_MSG_VAL_TO_BUFFER(std::vector<char>, .push_back((bits[0 >> 3] & (1 << (0 & 7))) != 0))
for (int j = 1; j < m; j++)
{
bool val = (bits[j >> 3] & (1 << (j & 7))) != 0;
PRINT(", %i", val);
READ_MSG_VAL_TO_BUFFER(std::vector<char>, .push_back((bits[j >> 3] & (1 << (j & 7))) != 0))
}
PRINT(" }\n");
}
else {
bool val;
src->Get(val, compression);
PRINT("{ %i", val);
for (int j = 1; j < m; j++)
{
src->Get(val, compression);
PRINT(", %i", val);
}
PRINT(" }\n");
}
break;
}
case NDTIntArray:
{
int m;
src->Get(m, NCTSmallUnsigned);
if (m) {
int val;
src->Get(val, compression);
PRINT("{ %i", val);
READ_MSG_VAL_TO_BUFFER(std::vector<int>, .push_back(val));
for (int j = 1; j < m; j++)
{
src->Get(val, compression);
PRINT(", %i", val);
READ_MSG_VAL_TO_BUFFER(std::vector<int>, .push_back(val));
}
PRINT(" }\n");
}
else
PRINT("\n");
break;
}
case NDTFloatArray:
{
int m;
src->Get(m, NCTSmallUnsigned);
if (m) {
float val;
src->Get(val, compression);
PRINT("{ %s", get_float_truncated(val).c_str());
READ_MSG_VAL_TO_BUFFER(std::vector<float>, .push_back(val));
for (int j = 1; j < m; j++)
{
src->Get(val, compression);
PRINT(", %s", get_float_truncated(val).c_str());
READ_MSG_VAL_TO_BUFFER(std::vector<float>, .push_back(val));
}
PRINT(" }\n");
}
else
PRINT("\n");
break;
}
case NDTStringArray:
{
int m;
src->Get(m, NCTSmallUnsigned);
if (m) {
char* val = nullptr;
src->Get(val, compression);
if (!val)
break;