forked from louisdx/cnc-replayreaders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnc3reader.cpp
869 lines (718 loc) · 29.8 KB
/
cnc3reader.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
/*****************************************
* Schneider's Replay Reader for TW/KW/RA3
* Handle with care.
*
* Compile like this:
* g++ -O2 -s -o cnc3reader.exe cnc3reader.cpp cnc3readerimpl.cpp replayreader.cpp \
* -enable-auto-import -static-libgcc -static-libstdc++ -fwhole-program
*
******************************************/
/*** Factions ***
** Kane's Wrath **
14: T59
13: R17
12: Scrin
11: MoK
10: BH
9: Nod
8: ZOCOM
7: ST
6: GDI
3: Commentator
2: Observer
1: Random
** Tiberium Wars
8: Scrin
7: Nod
6: GDI
3: Commentator
2: Observer
1: Random
** Red Alert
1: Observer
3: Commentator
7: Random
2: Empire
4: Allies
8: Soviets
***/
/*** Canonical chunk types ***
*
// Full chunk structure:
{
uint32 time_code;
byte chunk_type; // 1, 2, 3 or 4
uint32 chunk_size;
byte[chunk_size] data;
uint32 zero; // == 0
}
// Repeat until chunk_number == 0x7FFFFFF
Let us call (data[0], data[1]) the "chunk signature". chunk_type 2 has signature
(1,0), while chunk_type 1 has signature (1,x), x = 1,2,3,...
There seem to be two classes of chunks of chunk_type 1: Those of signature (1,0),
and those of signature (1,x), where x=1,2,3,... is some positive integer.
These chunks appear to have the following "canonical" structure.
Type 1:
{
byte; // == 1
uint32 n; // number of commands
byte[chunk_size-5] payload; // each command terminated by { 0x00, 0x00, 0x00, 0xFF }
}
Type 2:
{
byte[2]; // == { 1, 0 }
uint32 n; // maybe the player number, 0 <= n < number_of_players ?
byte; // == 0x0E in TW/KW or 0x0F in RA3
uint32 c; // == time_code;
byte[chunk_size-11] payload;
}
Type 3: // only occurs when six == 0x1E?
{
byte[2]; // == { 1, 0 }
uint32 n; // maybe the player number, 0 <= n < number_of_players ?
byte; // == 0x0D
uint32 c; // == chunk_number;
byte[chunk_size-11] payload;
}
Type 4: // only occurs when six == 0x1E?
{
byte[2]; // == { 1, 0 }
uint32 n; // maybe the player number, 0 <= n < number_of_players ?
byte; // == 0x0F
uint32 c; // == chunk_number;
byte[chunk_size-11] payload;
}
Types 3 and 4 also exist as empty versions of length 2 and content { 0 ,0 }.
Note that types 3 and 4 seem to only exist when there is a commentary track.
Type 1 exists as an empty version of length 5 in skirmish replays.
Update: In RA3, there also seems to be a type 0xFE = 254.
******************************/
#include "replayreader.h"
extern command_map_t RA3_commands;
extern command_map_t KW_commands;
extern command_map_t TW_commands;
extern command_names_t RA3_cmd_names;
extern command_names_t KW_cmd_names;
extern command_names_t TW_cmd_names;
/** Faction names for all TW/KW/RA3 games.
*/
std::string faction(unsigned int f, Options::GameType g);
/** Parse command line options.
*/
bool parse_options(int argc, char * argv[], Options & opts);
/** Set up type-1 chunk command codes, lengths and descriptions.
*/
void populate_command_map_RA3(command_map_t & ra3_commands, command_names_t & ra3_cmd_names);
void populate_command_map_KW(command_map_t & kw_commands, command_names_t & kw_cmd_names);
void populate_command_map_TW(command_map_t & tw_commands, command_names_t & tw_cmd_names);
void fix_replay_file(const char * filename, Options & opts);
bool dumpchunks(const unsigned char * buf, char chunktype, unsigned int chunklen, unsigned int timecode,
unsigned char hsix, unsigned char hnumber1, std::ostream & audioout,
apm_1_map_t & player_1_apm, apm_2_map_t & player_2_apm,
apm_histo_map_t & player_indi_histo_apm, apm_histo_map_t & player_coal_histo_apm,
unsigned int block_count, Options::GameType gametype, const Options & opts);
/* The main worker function.
*/
bool parse_replay_file(const char * filename, Options & opts)
{
Options::GameType gametype = opts.gametype;;
header_cnc3_t header;
header_ra3_t header_ra3;
std::string str_title, str_matchdesc, str_mapname, str_mapid, str_filename, str_vermagic, str_anothername;
std::vector<std::string> playerNames;
std::vector< std::vector<std::string> > playerNames2;
std::vector<int> playerIDs;
std::vector<int> playerNos;
char nplayers, onebyte, cncrpl_magic[8], dummy3[9], cncfooter_magic[18], timeout[200];
uint32_t player_id, hlen, after_vermagic, dummy, lastgood, firstchunk;
unsigned char u33[33], u31[31], hnumber1, hsix;
date_text_t datetime;
unknown_uints_t<19> u19;
unknown_uints_t<20> u20;
std::cerr << "Opening file \"" << filename << "\"...";
std::ifstream myfile(filename, std::ios::in | std::ios::binary);
if (!myfile) { std::cerr << " failed!" << std::endl; return false; }
myfile.seekg(0, std::fstream::end);
int filesize = myfile.tellg();
std::cerr << " succeeded. File size: " << filesize << " bytes." << std::endl;
std::ofstream audioout;
if (!opts.type.empty())
{
std::cerr << "Displaying only events of type(s) ";
std::copy(opts.type.begin(), opts.type.end(), std::ostream_iterator<int>(std::cerr, " "));
std::cerr << "." << std::endl;
}
if (!opts.cmd_filter.empty())
{
std::cerr << "Displaying only type-1 chunk commands of type(s) ";
std::copy(opts.cmd_filter.begin(), opts.cmd_filter.end(), std::ostream_iterator<int>(std::cerr, " "));
std::cerr << "." << std::endl;
}
if (opts.dumpaudio)
{
if (opts.audiofn == NULL)
{
std::cerr << "Error: You must specify the audio dump filename with the \"-A\" option." << std::endl;
return false;
}
std::cerr << "Attempting to dump audio tracks!" << std::endl;
audioout.open(opts.audiofn, std::ios::out | std::ios::binary | std::ios::trunc);
if (!audioout)
{
std::cerr << "Failed to create dump file \"" << opts.audiofn << "\", not dumping audio." << std::endl;
}
else
{
std::cerr << "Successfully opened dump file \"" << opts.audiofn << "\"." << std::endl;
}
}
/* Unless explicitly overridden, set the game type according to filename */
if (gametype == Options::GAME_UNDEF)
{
std::string fn(filename);
std::transform(fn.begin(), fn.end(), fn.begin(), ::tolower);
std::cerr << "Selecting game type according to file suffix: ";
if (fn.rfind(".cnc3replay") + 11 == fn.length())
{
std::cerr << "We pick Tiberium Wars.";
gametype = Options::GAME_TW;
}
else if (fn.rfind(".kwreplay") + 9 == fn.length())
{
std::cerr << "We pick Kane's Wrath.";
gametype = Options::GAME_KW;
}
else if (fn.rfind(".ra3replay") + 10 == fn.length())
{
std::cerr << "We pick Red Alert 3.";
gametype = Options::GAME_RA3;
}
else
{
std::cerr << "unable to determine game type. Please specify manually ('-w', '-k', '-r').";
}
std::cerr << std::endl;
}
myfile.seekg(0, std::fstream::beg);
if (gametype != Options::GAME_RA3)
{
myfile.read(reinterpret_cast<char*>(&header), sizeof(header_cnc3_t));
if ( strncmp(header.str_magic, "C&C3 REPLAY HEADER", 18) ||
((header.six != 6 ) && (header.six != 0x1E )) ||
(header.zero != 0 ) ||
((header.number1 != 5) && (header.number1 != 4)) ||
((READ_UINT32LE(header.vermajor) != 1) && (READ_UINT32LE(header.verminor) > 9))
)
{
std::cerr << "File does not seem to be a replay file." << std::endl;
return false;
}
hnumber1 = header.number1;
hsix = header.six;
}
else
{
myfile.read(reinterpret_cast<char*>(&header_ra3), sizeof(header_ra3_t));
if ( strncmp(header_ra3.str_magic, "RA3 REPLAY HEADER", 17) ||
((header_ra3.six != 6 ) && (header_ra3.six != 0x1E )) ||
(header_ra3.zero != 0 ) ||
((header_ra3.number1 != 5) && (header_ra3.number1 != 4)) ||
((READ_UINT32LE(header_ra3.vermajor) != 1) && (READ_UINT32LE(header_ra3.verminor) > 12))
)
{
std::cerr << "File does not seem to be a RA3 replay file." << std::endl;
return false;
}
hnumber1 = header_ra3.number1;
hsix = header_ra3.six;
}
str_title = read2ByteString(myfile);
str_matchdesc = read2ByteString(myfile);
str_mapname = read2ByteString(myfile);
str_mapid = read2ByteString(myfile);
myfile.read(&nplayers, 1);
for (int n = 0 ; n <= int(nplayers); ++n)
{
myfile.read(reinterpret_cast<char*>(&player_id), 4);
playerIDs.push_back(player_id);
playerNames.push_back(read2ByteString(myfile));
// Multiplayer replay
if (hnumber1 == 5)
{
myfile.read(&onebyte, 1);
playerNos.push_back(int(onebyte));
}
// Skirmish replay
else if (hnumber1 == 4)
{
playerNos.push_back(0);
}
}
if (gametype != Options::GAME_RA3)
{
std::cout << "Game version: " << std::dec << READ_UINT32LE(header.vermajor) << "." << READ_UINT32LE(header.verminor)
<< ", Build: " << READ_UINT32LE(header.buildmajor) << "." << READ_UINT32LE(header.buildminor) << std::endl;
}
else
{
std::cout << "Game version: " << std::dec << READ_UINT32LE(header_ra3.vermajor) << "." << READ_UINT32LE(header_ra3.verminor)
<< ", Build: " << READ_UINT32LE(header_ra3.buildmajor) << "." << READ_UINT32LE(header_ra3.buildminor) << std::endl;
}
std::cout << "Title: " << str_title << std::endl
<< "Description: " << str_matchdesc << std::endl
<< "Map name: " << str_mapname << std::endl
<< "Map ID: " << str_mapid << std::endl
<< std::endl << "Number of players: " << int(nplayers) << ", + 1 additional" << std::endl;
if (hsix == 0x1E) std::cout << "Commentary track available." << std::endl;
for (size_t i = 0; i < playerNames.size(); ++i)
fprintf(stdout, "Team %d (ID: %08X): %s\n", playerNos[i], playerIDs[i], playerNames[i].c_str());
myfile.read(reinterpret_cast<char*>(&dummy), 4);
firstchunk = (unsigned int)myfile.tellg() + 4 + dummy;
fprintf(stdout, "\nOffset from CNC3RPL magic to first chunk: 0x%X, first chunk at 0x%X.\n", dummy, firstchunk);
myfile.read(reinterpret_cast<char*>(&dummy), 4);
myfile.read(cncrpl_magic, 8);
if (dummy != 8 || strncmp(cncrpl_magic, "CNC3RPL\0", 8))
{
std::cerr << "Error: Unexpected content! Aborting." << std::endl;
exit(1);
}
/* For TW, version 1.07+, there is this extra bit of info, char modinfo[22]. */
char modinfo[22];
if (gametype == Options::GAME_UNDEF ||
(gametype == Options::GAME_TW && READ_UINT32LE(header.verminor) >= 7))
{
myfile.read(modinfo, 22);
if (gametype == Options::GAME_UNDEF && !strncmp(modinfo, "CNC3", 4))
{
gametype = Options::GAME_TW;
}
else if (gametype == Options::GAME_UNDEF)
{
gametype = Options::GAME_KW;
myfile.seekg(-22, std::fstream::cur);
}
}
if (gametype == Options::GAME_TW && READ_UINT32LE(header.verminor) >= 7)
{
std::cout << "Interpreting file as Tiberium Wars replay. Mod info: ";
char *p(modinfo), *q(NULL);
while (p < modinfo + 22)
{
q = std::strchr(p, '\0');
if (q == NULL) break;
if (p[0] != '\0')
std::cout << "\"" << p << "\" ";
p = q+1;
}
std::cout << std::endl;
}
else if (gametype == Options::GAME_TW)
{
std::cout << "Interpreting file as pre-1.07 Tiberium Wars replay." << std::endl;
}
else if (gametype == Options::GAME_KW)
{
std::cout << "Interpreting file as Kane's Wrath replay." << std::endl;
}
else if (gametype == Options::GAME_RA3)
{
std::cout << "Interpreting file as Red Alert 3 replay. Mod info: ";
myfile.read(modinfo, 22);
char *p(modinfo), *q(NULL);
while (p < modinfo + 22)
{
q = std::strchr(p, '\0');
if (q == NULL) break;
if (p[0] != '\0')
std::cout << "\"" << p << "\" ";
p = q+1;
}
std::cout << std::endl;
}
myfile.read(reinterpret_cast<char*>(&dummy), 4);
strftime(timeout, 200, "%Y-%m-%d %H:%M:%S (%Z)", gmtime(reinterpret_cast<time_t*>(&dummy)));
std::cout << "Timestamp: " << std::dec << dummy << ", that is " << timeout << "." << std::endl;
// Skipping unknown data. We print all this later.
if (gametype == Options::GAME_RA3)
myfile.read(reinterpret_cast<char*>(&u31), 31);
else
myfile.read(reinterpret_cast<char*>(&u33), 33);
myfile.read(reinterpret_cast<char*>(&hlen), 4);
if (hlen > 10000) { throw std::length_error("Requested header length too big."); }
std::vector<char> header2(hlen);
myfile.read(header2.data(), hlen);
if (opts.printraw)
std::cout << "Header string length: " << std::dec << hlen << ". Raw header data:" << std::endl
<< std::string(header2.begin(), header2.end()) << std::endl << std::endl;
std::cout << std::endl << "Header string length: " << std::dec << hlen << ". Header fields:" << std::endl;
std::vector<std::string> tokens = tokenize(std::string(header2.begin(), header2.end()), ";");
for (size_t i = 0; i < tokens.size(); ++i)
std::cout << tokens[i] << std::endl;
for (std::vector<std::string>::const_iterator it = tokens.begin(), end = tokens.end(); it != end; ++it)
{
const std::string & token = *it;
if (token[0] == 'S' && token[1] == '=')
{
std::cout << std::endl << "Found player information, parsing..." << std::endl;
std::vector<std::string> subtokens = tokenize(token.substr(2), ":");
for (size_t i = 0; i < subtokens.size(); ++i)
{
if (subtokens[i][0] != 'H' && !(subtokens[i].size() > 2 && subtokens[i][0] == 'C' && subtokens[i][2] == ',')) continue;
std::vector<std::string> subsubtokens = tokenize(subtokens[i].substr(subtokens[i][0] == 'C' ? 0 : 1), ",");
if (subsubtokens.size() < 6) { throw std::length_error("Unexpected game header!."); }
playerNames2.push_back(subsubtokens);
uint32_t v = std::strtoul(subsubtokens[1].c_str(), NULL, 16);
if (subtokens[i].size() > 2 && subtokens[i][0] == 'C' && subtokens[i][2] == ',')
{
fprintf(stdout, "Computer opponent: %s (Faction: %s) Other data: \"",
subsubtokens[0].c_str(), faction(std::atoi(subsubtokens[2].c_str()), gametype).c_str());
for (size_t j = 1; j < subsubtokens.size() - 1; ++j) std::cout << subsubtokens[j] << ", ";
}
else
{
fprintf(stdout, "Ingame player name: %s (Faction: %s, IP addr.: 0x%08X, %d.%d.%d.%d:%s) Other data: \"",
subsubtokens[0].c_str(), faction(std::atoi(subsubtokens[5].c_str()), gametype).c_str(), v,
v>>24, ((v<<8)>>24), ((v<<16)>>24), ((v<<24)>>24), subsubtokens[2].c_str());
for (size_t j = 3; j < subsubtokens.size() - 1; ++j) std::cout << subsubtokens[j] << ", ";
}
std::cout << subsubtokens[subsubtokens.size() - 1] << "\"." << std::endl;
}
}
}
// Skipping unknown data. We print all this later.
myfile.read(dummy3, 9);
myfile.read(reinterpret_cast<char*>(&dummy), 4);
str_filename = read2ByteStringN(myfile, dummy);
std::cout << "File name (?): " << str_filename << std::endl;
myfile.read(reinterpret_cast<char*>(&datetime), sizeof(datetime));
myfile.read(reinterpret_cast<char*>(&dummy), 4);
if (dummy > 10000) { throw std::length_error("Requested version magic length too big."); }
std::vector<char> ch_vermagic(dummy);
myfile.read(ch_vermagic.data(), dummy);
str_vermagic = std::string(ch_vermagic.begin(), ch_vermagic.end());
myfile.read(reinterpret_cast<char*>(&after_vermagic), 4);
// Skipping unknown data. We print all this later.
myfile.read(&onebyte, 1);
if (gametype == Options::GAME_RA3) myfile.read(reinterpret_cast<char*>(&u20), 20*4);
else myfile.read(reinterpret_cast<char*>(&u19), 19*4);
std::cout << "Version/build magic string: \"" << str_vermagic << "\", followed by 0x"
<< std::hex << std::uppercase << std::setfill('0') << std::setw(8) << after_vermagic << " and 0x"
<< std::setw(2) << (unsigned int)(onebyte) << std::endl;
/* 10 uint16_t's before the version magic are another version of the time stamp:
* Final two numbers ([8],[9]) always seem to be (14,0), (7,0) or (15,0).
*/
fprintf(stdout, "The literal timestamp says: \"%s, %04hu-%02hu-%02hu %02hu:%02hu:%02hu\". It is followed by the number %hu.\n",
weekday(datetime.data[2]), datetime.data[0], datetime.data[1], datetime.data[3],
datetime.data[4], datetime.data[5], datetime.data[6], datetime.data[7]);
std::cout << std::endl << "===== Report on unknown header data follows ====" << std::endl;
// 33 bytes skipped after global header, 'CNC3RPL ' magic and timestamp, expected all zero.
if (gametype == Options::GAME_RA3)
{
if (array_is_zero(u31, 31))
{
std::cout << "We skipped 31 expected mysterious bytes, which were all zero." << std::endl;
}
else
{
std::cout << std::endl << "We skipped 31 mysterious bytes which were unexpected! They were:" << std::endl;
hexdump(stdout, u31, 31, " ");
}
}
else
{
if (array_is_zero(u33, 33))
{
std::cout << "We skipped 33 expected mysterious bytes, which were all zero." << std::endl;
}
else
{
std::cout << std::endl << "We skipped 33 mysterious bytes which were unexpected! They were:" << std::endl;
hexdump(stdout, u33, 33, " ");
}
}
if ((unsigned int)(dummy3[0]) < playerNames2.size())
fprintf(stdout, "The player who saved this replay was number %u (%s).\n", dummy3[0], playerNames2[dummy3[0]][0].c_str());
else
fprintf(stdout, "Warning: unexpected value for the index of the player who saved the replay (got: %u)!\n", dummy3[0]);
// 8 bytes after global header #2 + 1, expected all zero.
if (array_is_zero(reinterpret_cast<unsigned char*>(dummy3)+1, 8))
{
fprintf(stdout, "We skipped 8 expected mysterious bytes which were all zero.\n");
}
else
{
fprintf(stdout, "We skipped 8 mysterious bytes which were unexpected; values: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\n",
dummy3[1], dummy3[2], dummy3[3], dummy3[4], dummy3[5], dummy3[6], dummy3[7], dummy3[8]);
}
// 19/20 uint32_t's after the version magic
if (gametype == Options::GAME_RA3)
{
fprintf(stdout, "\nThe 20 integers after the version magic are: ");
for (size_t i = 0; i < 20; ++i) fprintf(stdout, "%u, ", u20.data[i]);
}
else
{
fprintf(stdout, "\nThe 19 integers after the version magic are: ");
for (size_t i = 0; i < 19; ++i) fprintf(stdout, "%u, ", u19.data[i]);
}
fprintf(stdout, "\n");
uint32_t footer_offset;
dummy = myfile.tellg();
myfile.seekg(-4, std::fstream::end);
myfile.read(reinterpret_cast<char*>(&footer_offset), 4);
if (footer_offset < 100) // a random safety check
{
fprintf(stdout, "Footer length is %u.", footer_offset);
}
else
{
fprintf(stdout, "Invalid footer - is this a defective replay? Footer will be ignored.\n");
footer_offset = 0;
}
if (!opts.dumpchunks && !opts.apm && !opts.printraw)
{
if (footer_offset != 0)
{
myfile.seekg((gametype == Options::GAME_RA3 ? 17 : 18) - int(footer_offset), std::fstream::end);
myfile.read(reinterpret_cast<char*>(&dummy), 4);
fprintf(stdout, " Footer chunk number: 0x%08X (timecode: %s); %u bytes / %u frames = %.2f Bpf = %.2f Bps.\n",
dummy, timecode_to_string(dummy).c_str(), filesize, dummy, double(filesize) / dummy, double(filesize) * 15.0 / dummy);
}
return true;
}
fprintf(stdout, "\n");
myfile.seekg(dummy, std::fstream::beg);
apm_1_map_t player_1_apm;
apm_2_map_t player_2_apm;
apm_histo_map_t player_indi_histo_apm;
apm_histo_map_t player_coal_histo_apm;
if (myfile.tellg() != firstchunk)
{
fprintf(stdout, "\nWarning: We're not at the beginning of the chunks yet, difference = %d. Advancing...\n", (int)firstchunk - (int)myfile.tellg());
myfile.seekg(firstchunk, std::fstream::beg);
}
if (opts.apm)
{
std::cout << std::endl << "==== gathering APM statistics ====" << std::endl << std::endl;
}
else
{
std::cout << std::endl << "=================================================" << std::endl
<< std::endl << "Now dumping individual data blocks." << std::endl << std::endl;
}
lastgood = int(myfile.tellg());
for (int block_count = 0; !myfile.eof(); block_count++)
{
uint32_t len;
myfile.read(reinterpret_cast<char*>(&dummy), 4);
if (dummy == 0x7FFFFFFF) break;
myfile.read(&onebyte, 1);
myfile.read(reinterpret_cast<char*>(&len), 4);
if (len > 10000) { throw std::length_error("Requested chunk length too big."); }
if (myfile.eof() || filesize - myfile.tellg() < len + 4)
{
if (opts.autofix)
{
opts.fixfn = (std::string(filename) + "-FIXED").c_str();
opts.fixpos = lastgood;
std::cerr << "Warning: Unexpected end of file! Auto fix is requested, attempting to fix this replay. (Params: " << opts.fixfn << ", " << opts.fixpos << ")" << std::endl;
myfile.close();
opts.gametype = gametype;
fix_replay_file(filename, opts);
return true;
}
else
{
std::cerr << "Error: Unexpected end of file! Aborting. Try 'cnc3reader -f " << lastgood
<< (gametype == Options::GAME_RA3 ? " -r" : gametype == Options::GAME_KW ? " -k" : " -w")
<< " " << filename << "' for fixing." << std::endl;
return false;
}
}
std::vector<unsigned char> vbuf(len + 4);
lastgood = int(myfile.tellg()) - 9;
myfile.read(reinterpret_cast<char*>(vbuf.data()), len + 4);
const unsigned char * const buf = vbuf.data();
if (opts.printraw)
{
if (is_filtered(onebyte, opts.type)) continue;
fprintf(stdout, "\nBlock TC: 0x%08X, timecode: %s, length: %u bytes, count: %u, filepos: 0x%X, Chunk Type: %u.\n",
dummy, timecode_to_string(dummy).c_str(), len, block_count, int(myfile.tellg()), onebyte);
hexdump(stdout, buf, len+4, " ");
}
else if (opts.dumpchunks)
{
if (!dumpchunks(buf, onebyte, len, dummy, hsix, hnumber1, audioout,
player_1_apm, player_2_apm, player_indi_histo_apm, player_coal_histo_apm, block_count, gametype, opts)) return false;
}
} // for(...)
/* Process the footer */
if (gametype == Options::GAME_RA3)
{
myfile.read(cncfooter_magic, 17);
}
else
{
myfile.read(cncfooter_magic, 18);
}
if ((gametype != Options::GAME_RA3 && strncmp(cncfooter_magic, "C&C3 REPLAY FOOTER", 18)) ||
(gametype == Options::GAME_RA3 && strncmp(cncfooter_magic, "RA3 REPLAY FOOTER", 17)) )
{
std::cerr << "Error: Unexpected content! Aborting." << std::endl;
exit(1);
}
uint32_t final_timecode;
myfile.read(reinterpret_cast<char*>(&final_timecode), 4);
fprintf(stdout, "Footer magic string as expected.\nFooter chunk number: 0x%08X (timecode: %s).\n",
final_timecode, timecode_to_string(final_timecode).c_str());
std::vector<char> footerdata(footer_offset - 8 - (gametype == Options::GAME_RA3 ? 17 : 18));
myfile.read(footerdata.data(), footerdata.size());
fprintf(stdout, "Numbers in the footer:");
for (size_t i = 0; i < footerdata.size(); ++i) fprintf(stdout, " 0x%02X", (unsigned char)(footerdata[i]));
fprintf(stdout, ".\n");
if (footerdata.size() == 42 || footerdata.size() == 38)
{
fprintf(stdout, "Ints in the footer:");
for (size_t i = 6; i + 28 <= footerdata.size(); i += 4)
fprintf(stdout, " %i", *reinterpret_cast<const uint32_t*>(footerdata.data() + i));
fprintf(stdout, ". Six floats in the footer:");
for (size_t i = footerdata.size() - 24; i + 4 <= footerdata.size(); i += 4)
fprintf(stdout, " %6.2f", *reinterpret_cast<const float*>(footerdata.data() + i));
fprintf(stdout, "\n");
}
/* Report APM stats */
if (opts.apm)
{
const command_names_t & cmd_names = gametype == Options::GAME_TW ? TW_cmd_names
: (gametype == Options::GAME_KW ? KW_cmd_names : RA3_cmd_names);
std::map<unsigned int, std::pair<unsigned int, unsigned int>> apm_total;
fprintf(stdout, "\nAPM statistics: Type-2 Chunks\n");
for (apm_2_map_t::const_iterator i = player_2_apm.begin(), end = player_2_apm.end(); i != end; ++i)
fprintf(stdout,
"Player %u: 1s-heartbeats: %u (%.1f). Len40: %u (%.1f). Len24: %u (%.1f). Other: %u (%.1f).\n",
i->first,
i->second.counter[0], (double)(i->second.counter[0])*15.0*60.0/(double)(final_timecode),
i->second.counter[1], (double)(i->second.counter[1])*15.0*60.0/(double)(final_timecode),
i->second.counter[2], (double)(i->second.counter[2])*15.0*60.0/(double)(final_timecode),
i->second.counter[3], (double)(i->second.counter[3])*15.0*60.0/(double)(final_timecode)
);
fprintf(stdout, "\nAPM statistics: Type-1 Chunks\n");
for (apm_1_map_t::const_iterator i = player_1_apm.begin(), end = player_1_apm.end() ; i != end; ++i)
{
fprintf(stdout, "Player %u: %u\n", i->first, i->second);
}
fprintf(stdout, "\nAPM statistics: Type-1 command histogram\n");
for (apm_histo_map_t::const_iterator i = player_indi_histo_apm.begin(), end = player_indi_histo_apm.end(); i != end; ++i)
{
for (auto j = i->second.begin(), end = i->second.end(); j != end; ++j)
{
const command_names_t::const_iterator nit = cmd_names.find(j->first);
const std::string cn = nit == cmd_names.end() ? "" : nit->second;
fprintf(stdout, "Raw player 0x%02X --> command 0x%02X: %u (\"%s\")\n",
i->first, j->first, j->second.size(), cn.c_str());
}
}
for (apm_histo_map_t::const_iterator i = player_coal_histo_apm.begin(), end = player_coal_histo_apm.end(); i != end; ++i)
{
for (auto j = i->second.begin(), end = i->second.end(); j != end; ++j)
{
const command_names_t::const_iterator nit = cmd_names.find(j->first);
const std::string cn = nit == cmd_names.end() ? "" : nit->second;
fprintf(stdout, "Player %u, command 0x%02X: %u (\"%s\")\n",
i->first, j->first, j->second.size(), cn.c_str());
const unsigned int & c = j->first;
/* RA3 APM filter: 0x37: some automatic, irregular sync command ("scroll"??)
0x21: hearbeat, every 3 seconds
KW APM filter: 0x8F: some automatic, irregular sync command ("scroll"??)
0x61: heartbeat, every 30 seconds
TW APM filter: 0x85: some automatic, irregular sync command ("scroll"??)
0x57: heartbeat, every 30 seconds
All games: 0xF5: drag selection box and/or select units. We could micro-filter this depending on how many units got selected.
0xF8: left-click on the map, can be used to "deselect" a selected unit, but is also caused by dumb blank clicks.
*/
if ((gametype == Options::GAME_RA3 && c != 0x21 && c != 0x37) ||
(gametype == Options::GAME_TW && c != 0x85 && c != 0x57) ||
(gametype == Options::GAME_KW && c != 0x8F && c != 0x61) )
{
apm_total[i->first].first += j->second.size();
if (c != 0xF8 && c != 0xF5)
apm_total[i->first].second += j->second.size();
}
}
fprintf(stdout, "\n");
}
if (!opts.time_series_filter.empty())
{
fprintf(stdout, "Event time series:\n");
for (apm_histo_map_t::const_iterator i = player_coal_histo_apm.begin(), end = player_coal_histo_apm.end(); i != end; ++i)
{
for (auto j = i->second.begin(), end = i->second.end(); j != end; ++j)
{
if (opts.time_series_filter.find(j->first) == opts.time_series_filter.end()) continue;
const command_names_t::const_iterator nit = cmd_names.find(j->first);
const std::string cn = nit == cmd_names.end() ? "" : nit->second;
fprintf(stdout, "Player %u, command 0x%02X (\"%s\"):", i->first, j->first, cn.c_str());
for (auto k = j->second.cbegin(), end = j->second.cend(); k != end; ++k)
{
std::cout << " " << timecode_to_string(*k);
}
fprintf(stdout, "\n");
}
}
fprintf(stdout, "\n");
}
fprintf(stdout, "Experimental APM count:\n");
for (auto it = apm_total.cbegin(), end = apm_total.cend(); it != end; ++it)
{
fprintf(stdout, " Player %u: %u actions including clicks (%.1f apm), %u actions excluding clicks (%.1f apm)\n",
it->first, it->second.first, double(it->second.first * 60 * 15)/double(final_timecode),
it->second.second, double(it->second.second * 60 * 15)/double(final_timecode));
}
if (footerdata.size() == 42 || footerdata.size() == 38)
{
fprintf(stdout, "\nKill/death ratios:\n");
for (size_t i = footerdata.size() - 24, n = 0; i + 4 <= footerdata.size(); i += 4)
fprintf(stdout, " Player %u: %6.2f\n", n++, *reinterpret_cast<const float*>(footerdata.data() + i));
}
}
return true;
}
int main(int argc, char * argv[])
{
Options opts;
if (!parse_options(argc, argv, opts))
return 1;
if (opts.fixbroken)
{
if (optind + 1 != argc) { std::cerr << "Can only fix one replay file at a time." << std::endl; return 0; }
if (opts.fixfn == NULL) opts.fixfn = (std::string(argv[optind]) + "-FIXED").c_str();
fix_replay_file(argv[optind], opts);
}
else
{
populate_command_map_RA3(RA3_commands, RA3_cmd_names);
populate_command_map_KW(KW_commands, KW_cmd_names);
populate_command_map_TW(TW_commands, TW_cmd_names);
for ( ; optind < argc; ++optind)
{
bool res;
try
{
res = parse_replay_file(argv[optind], opts);
}
catch (const std::exception & e)
{
std::cout << "Exception: " << e.what() << std::endl;
res = false;
}
catch (...)
{
std::cout << "Unknown Exception!" << std::endl;
res = false;
}
if (!res && opts.breakonerror) return 1;
std::cout << std::endl << std::endl;
}
}
return 0;
}