25
25
#include " PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
26
26
27
27
#include " Common/Recorder.h"
28
+ #include " Common/file.h"
28
29
#include " Common/FileSystem.h"
29
30
#include " Common/PlayerList.h"
30
31
#include " Common/Player.h"
@@ -368,6 +369,7 @@ RecorderClass::RecorderClass()
368
369
m_originalGameMode = GAME_NONE;
369
370
m_mode = RECORDERMODETYPE_RECORD;
370
371
m_file = NULL ;
372
+ m_replayFile = NULL ;
371
373
m_fileName.clear ();
372
374
m_currentFilePosition = 0 ;
373
375
// Added By Sadullah Nader
@@ -397,6 +399,7 @@ void RecorderClass::init() {
397
399
m_originalGameMode = GAME_NONE;
398
400
m_mode = RECORDERMODETYPE_NONE;
399
401
m_file = NULL ;
402
+ m_replayFile = NULL ;
400
403
m_fileName.clear ();
401
404
m_currentFilePosition = 0 ;
402
405
m_gameInfo.clearSlotList ();
@@ -419,6 +422,10 @@ void RecorderClass::reset() {
419
422
fclose (m_file);
420
423
m_file = NULL ;
421
424
}
425
+ if (m_replayFile != NULL ) {
426
+ m_replayFile->close ();
427
+ m_replayFile = NULL ;
428
+ }
422
429
m_fileName.clear ();
423
430
424
431
init ();
@@ -472,9 +479,9 @@ void RecorderClass::updatePlayback() {
472
479
* reaching the end of the playback file.
473
480
*/
474
481
void RecorderClass::stopPlayback () {
475
- if (m_file != NULL ) {
476
- fclose (m_file );
477
- m_file = NULL ;
482
+ if (m_replayFile != NULL ) {
483
+ m_replayFile-> close ( );
484
+ m_replayFile = NULL ;
478
485
}
479
486
m_fileName.clear ();
480
487
@@ -828,52 +835,52 @@ Bool RecorderClass::readReplayHeader(ReplayHeader& header)
828
835
{
829
836
AsciiString filepath = getReplayDir ();
830
837
filepath.concat (header.filename .str ());
831
- m_file = fopen (filepath.str (), " rb " );
832
- if (m_file == NULL )
838
+ m_replayFile = TheFileSystem-> openFile (filepath.str (), File::READ );
839
+ if (m_replayFile == NULL )
833
840
{
834
841
DEBUG_LOG ((" Can't open %s (%s)\n " , filepath.str (), header.filename .str ()));
835
842
return FALSE ;
836
843
}
837
844
838
845
// Read the GENREP header.
839
846
char genrep[7 ];
840
- fread (&genrep, sizeof (char ), 6 , m_file );
847
+ m_replayFile-> read (&genrep, sizeof (char ) * 6 );
841
848
genrep[6 ] = 0 ;
842
849
if (strncmp (genrep, " GENREP" , 6 )) {
843
850
DEBUG_LOG ((" RecorderClass::readReplayHeader - replay file did not have GENREP at the start.\n " ));
844
- fclose (m_file );
845
- m_file = NULL ;
851
+ m_replayFile-> close ( );
852
+ m_replayFile = NULL ;
846
853
return FALSE ;
847
854
}
848
855
849
856
// read in some stats
850
857
replay_time_t tmp;
851
- fread (&tmp, sizeof (replay_time_t ), 1 , m_file );
858
+ m_replayFile-> read (&tmp, sizeof (replay_time_t ));
852
859
header.startTime = tmp;
853
- fread (&tmp, sizeof (replay_time_t ), 1 , m_file );
860
+ m_replayFile-> read (&tmp, sizeof (replay_time_t ));
854
861
header.endTime = tmp;
855
862
856
- fread (&header.frameCount , sizeof (UnsignedInt), 1 , m_file );
863
+ m_replayFile-> read (&header.frameCount , sizeof (UnsignedInt));
857
864
858
- fread (&header.desyncGame , sizeof (Bool), 1 , m_file );
859
- fread (&header.quitEarly , sizeof (Bool), 1 , m_file );
865
+ m_replayFile-> read (&header.desyncGame , sizeof (Bool));
866
+ m_replayFile-> read (&header.quitEarly , sizeof (Bool));
860
867
for (Int i=0 ; i<MAX_SLOTS; ++i)
861
868
{
862
- fread (&(header.playerDiscons [i]), sizeof (Bool), 1 , m_file );
869
+ m_replayFile-> read (&(header.playerDiscons [i]), sizeof (Bool));
863
870
}
864
871
865
872
// Read the Replay Name. We don't actually do anything with it. Oh well.
866
873
header.replayName = readUnicodeString ();
867
874
868
875
// Read the date and time. We don't really do anything with this either. Oh well.
869
- fread (&header.timeVal , sizeof (SYSTEMTIME), 1 , m_file );
876
+ m_replayFile-> read (&header.timeVal , sizeof (SYSTEMTIME));
870
877
871
878
// Read in the Version info
872
879
header.versionString = readUnicodeString ();
873
880
header.versionTimeString = readUnicodeString ();
874
- fread (&header.versionNumber , sizeof (UnsignedInt), 1 , m_file );
875
- fread (&header.exeCRC , sizeof (UnsignedInt), 1 , m_file );
876
- fread (&header.iniCRC , sizeof (UnsignedInt), 1 , m_file );
881
+ m_replayFile-> read (&header.versionNumber , sizeof (UnsignedInt));
882
+ m_replayFile-> read (&header.exeCRC , sizeof (UnsignedInt));
883
+ m_replayFile-> read (&header.iniCRC , sizeof (UnsignedInt));
877
884
878
885
// Read in the GameInfo
879
886
header.gameOptions = readAsciiString ();
@@ -883,8 +890,8 @@ Bool RecorderClass::readReplayHeader(ReplayHeader& header)
883
890
if (!ParseAsciiStringToGameInfo (&m_gameInfo, header.gameOptions ))
884
891
{
885
892
DEBUG_LOG ((" RecorderClass::readReplayHeader - replay file did not have a valid GameInfo string.\n " ));
886
- fclose (m_file );
887
- m_file = NULL ;
893
+ m_replayFile-> close ( );
894
+ m_replayFile = NULL ;
888
895
return FALSE ;
889
896
}
890
897
m_gameInfo.startGame (0 );
@@ -896,8 +903,8 @@ Bool RecorderClass::readReplayHeader(ReplayHeader& header)
896
903
DEBUG_LOG ((" RecorderClass::readReplayHeader - invalid local slot number.\n " ));
897
904
m_gameInfo.endGame ();
898
905
m_gameInfo.reset ();
899
- fclose (m_file );
900
- m_file = NULL ;
906
+ m_replayFile-> close ( );
907
+ m_replayFile = NULL ;
901
908
return FALSE ;
902
909
}
903
910
if (header.localPlayerIndex >= 0 )
@@ -910,9 +917,13 @@ Bool RecorderClass::readReplayHeader(ReplayHeader& header)
910
917
{
911
918
m_gameInfo.endGame ();
912
919
m_gameInfo.reset ();
913
- fclose (m_file);
914
- m_file = NULL ;
920
+ m_replayFile->close ();
921
+ m_replayFile = NULL ;
922
+ }
923
+ else {
924
+ m_replayFile = m_replayFile->convertToRAMFile ();
915
925
}
926
+
916
927
return TRUE ;
917
928
}
918
929
@@ -1210,15 +1221,15 @@ Bool RecorderClass::playbackFile(AsciiString filename)
1210
1221
DEBUG_LOG ((" Player index is %d, replay CRC interval is %d\n " , m_crcInfo->getLocalPlayer (), REPLAY_CRC_INTERVAL));
1211
1222
1212
1223
Int difficulty = 0 ;
1213
- fread (&difficulty, sizeof (difficulty), 1 , m_file );
1224
+ m_replayFile-> read (&difficulty, sizeof (difficulty));
1214
1225
1215
- fread (&m_originalGameMode, sizeof (m_originalGameMode), 1 , m_file );
1226
+ m_replayFile-> read (&m_originalGameMode, sizeof (m_originalGameMode));
1216
1227
1217
1228
Int rankPoints = 0 ;
1218
- fread (&rankPoints, sizeof (rankPoints), 1 , m_file );
1229
+ m_replayFile-> read (&rankPoints, sizeof (rankPoints));
1219
1230
1220
1231
Int maxFPS = 0 ;
1221
- fread (&maxFPS, sizeof (maxFPS), 1 , m_file );
1232
+ m_replayFile-> read (&maxFPS, sizeof (maxFPS));
1222
1233
1223
1234
DEBUG_LOG ((" RecorderClass::playbackFile() - original game was mode %d\n " , m_originalGameMode));
1224
1235
@@ -1259,15 +1270,17 @@ UnicodeString RecorderClass::readUnicodeString() {
1259
1270
WideChar str[1024 ] = L" " ;
1260
1271
Int index = 0 ;
1261
1272
1262
- Int c = fgetwc (m_file);
1273
+ Int c;
1274
+ m_replayFile->read (&c, sizeof (WideChar));
1263
1275
if (c == EOF) {
1264
1276
str[index] = 0 ;
1265
1277
}
1266
1278
str[index] = c;
1267
1279
1268
1280
while (index < 1024 && str[index] != 0 ) {
1269
1281
++index;
1270
- Int c = fgetwc (m_file);
1282
+ Int c;
1283
+ m_replayFile->read (&c, sizeof (WideChar));
1271
1284
if (c == EOF) {
1272
1285
str[index] = 0 ;
1273
1286
break ;
@@ -1287,15 +1300,17 @@ AsciiString RecorderClass::readAsciiString() {
1287
1300
char str[1024 ] = " " ;
1288
1301
Int index = 0 ;
1289
1302
1290
- Int c = fgetc (m_file);
1303
+ Int c;
1304
+ m_replayFile->read (&c, sizeof (Char));
1291
1305
if (c == EOF) {
1292
1306
str[index] = 0 ;
1293
1307
}
1294
1308
str[index] = c;
1295
1309
1296
1310
while (index < 1024 && str[index] != 0 ) {
1297
1311
++index;
1298
- Int c = fgetc (m_file);
1312
+ Int c;
1313
+ m_replayFile->read (&c, sizeof (Char));
1299
1314
if (c == EOF) {
1300
1315
str[index] = 0 ;
1301
1316
break ;
@@ -1313,9 +1328,9 @@ AsciiString RecorderClass::readAsciiString() {
1313
1328
* is stopped and the next frame is said to be -1.
1314
1329
*/
1315
1330
void RecorderClass::readNextFrame () {
1316
- Int retcode = fread (&m_nextFrame, sizeof (m_nextFrame), 1 , m_file );
1317
- if (retcode != 1 ) {
1318
- DEBUG_LOG ((" RecorderClass::readNextFrame - fread failed on frame %d\n " , TheGameLogic->getFrame ()));
1331
+ Int retcode = m_replayFile-> read (&m_nextFrame, sizeof (m_nextFrame));
1332
+ if (retcode != sizeof (m_nextFrame) ) {
1333
+ DEBUG_LOG ((" RecorderClass::readNextFrame - read failed on frame %d\n " , TheGameLogic->getFrame ()));
1319
1334
m_nextFrame = -1 ;
1320
1335
stopPlayback ();
1321
1336
}
@@ -1326,9 +1341,9 @@ void RecorderClass::readNextFrame() {
1326
1341
*/
1327
1342
void RecorderClass::appendNextCommand () {
1328
1343
GameMessage::Type type;
1329
- Int retcode = fread (&type, sizeof (type), 1 , m_file );
1330
- if (retcode != 1 ) {
1331
- DEBUG_LOG ((" RecorderClass::appendNextCommand - fread failed on frame %d\n " , m_nextFrame/* TheGameLogic->getFrame()*/ ));
1344
+ Int retcode = m_replayFile-> read (&type, sizeof (type));
1345
+ if (retcode != sizeof (type) ) {
1346
+ DEBUG_LOG ((" RecorderClass::appendNextCommand - read failed on frame %d\n " , m_nextFrame/* TheGameLogic->getFrame()*/ ));
1332
1347
return ;
1333
1348
}
1334
1349
@@ -1347,7 +1362,7 @@ void RecorderClass::appendNextCommand() {
1347
1362
#endif // DEBUG_LOGGING
1348
1363
1349
1364
Int playerIndex = -1 ;
1350
- fread (&playerIndex, sizeof (playerIndex), 1 , m_file );
1365
+ m_replayFile-> read (&playerIndex, sizeof (playerIndex));
1351
1366
msg->friend_setPlayerIndex (playerIndex);
1352
1367
1353
1368
// don't debug log this if we're debugging sync errors, as it will cause diff problems between a game and it's replay...
@@ -1366,14 +1381,14 @@ void RecorderClass::appendNextCommand() {
1366
1381
1367
1382
UnsignedByte numTypes = 0 ;
1368
1383
Int totalArgs = 0 ;
1369
- fread (&numTypes, sizeof (numTypes), 1 , m_file );
1384
+ m_replayFile-> read (&numTypes, sizeof (numTypes));
1370
1385
1371
1386
GameMessageParser *parser = newInstance (GameMessageParser)();
1372
1387
for (UnsignedByte i = 0 ; i < numTypes; ++i) {
1373
1388
UnsignedByte type = (UnsignedByte)ARGUMENTDATATYPE_UNKNOWN;
1374
- fread (&type, sizeof (type), 1 , m_file );
1389
+ m_replayFile-> read (&type, sizeof (type));
1375
1390
UnsignedByte numArgs = 0 ;
1376
- fread (&numArgs, sizeof (numArgs), 1 , m_file );
1391
+ m_replayFile-> read (&numArgs, sizeof (numArgs));
1377
1392
parser->addArgType ((GameMessageArgumentDataType)type, numArgs);
1378
1393
totalArgs += numArgs;
1379
1394
}
@@ -1421,7 +1436,7 @@ void RecorderClass::appendNextCommand() {
1421
1436
void RecorderClass::readArgument (GameMessageArgumentDataType type, GameMessage *msg) {
1422
1437
if (type == ARGUMENTDATATYPE_INTEGER) {
1423
1438
Int theint;
1424
- fread (&theint, sizeof (theint), 1 , m_file );
1439
+ m_replayFile-> read (&theint, sizeof (theint));
1425
1440
msg->appendIntegerArgument (theint);
1426
1441
#ifdef DEBUG_LOGGING
1427
1442
if (m_doingAnalysis)
@@ -1431,7 +1446,7 @@ void RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage *
1431
1446
#endif
1432
1447
} else if (type == ARGUMENTDATATYPE_REAL) {
1433
1448
Real thereal;
1434
- fread (&thereal, sizeof (thereal), 1 , m_file );
1449
+ m_replayFile-> read (&thereal, sizeof (thereal));
1435
1450
msg->appendRealArgument (thereal);
1436
1451
#ifdef DEBUG_LOGGING
1437
1452
if (m_doingAnalysis)
@@ -1441,7 +1456,7 @@ void RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage *
1441
1456
#endif
1442
1457
} else if (type == ARGUMENTDATATYPE_BOOLEAN) {
1443
1458
Bool thebool;
1444
- fread (&thebool, sizeof (thebool), 1 , m_file );
1459
+ m_replayFile-> read (&thebool, sizeof (thebool));
1445
1460
msg->appendBooleanArgument (thebool);
1446
1461
#ifdef DEBUG_LOGGING
1447
1462
if (m_doingAnalysis)
@@ -1451,7 +1466,7 @@ void RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage *
1451
1466
#endif
1452
1467
} else if (type == ARGUMENTDATATYPE_OBJECTID) {
1453
1468
ObjectID theid;
1454
- fread (&theid, sizeof (theid), 1 , m_file );
1469
+ m_replayFile-> read (&theid, sizeof (theid));
1455
1470
msg->appendObjectIDArgument (theid);
1456
1471
#ifdef DEBUG_LOGGING
1457
1472
if (m_doingAnalysis)
@@ -1461,7 +1476,7 @@ void RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage *
1461
1476
#endif
1462
1477
} else if (type == ARGUMENTDATATYPE_DRAWABLEID) {
1463
1478
DrawableID theid;
1464
- fread (&theid, sizeof (theid), 1 , m_file );
1479
+ m_replayFile-> read (&theid, sizeof (theid));
1465
1480
msg->appendDrawableIDArgument (theid);
1466
1481
#ifdef DEBUG_LOGGING
1467
1482
if (m_doingAnalysis)
@@ -1471,7 +1486,7 @@ void RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage *
1471
1486
#endif
1472
1487
} else if (type == ARGUMENTDATATYPE_TEAMID) {
1473
1488
UnsignedInt theid;
1474
- fread (&theid, sizeof (theid), 1 , m_file );
1489
+ m_replayFile-> read (&theid, sizeof (theid));
1475
1490
msg->appendTeamIDArgument (theid);
1476
1491
#ifdef DEBUG_LOGGING
1477
1492
if (m_doingAnalysis)
@@ -1481,7 +1496,7 @@ void RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage *
1481
1496
#endif
1482
1497
} else if (type == ARGUMENTDATATYPE_LOCATION) {
1483
1498
Coord3D loc;
1484
- fread (&loc, sizeof (loc), 1 , m_file );
1499
+ m_replayFile-> read (&loc, sizeof (loc));
1485
1500
msg->appendLocationArgument (loc);
1486
1501
#ifdef DEBUG_LOGGING
1487
1502
if (m_doingAnalysis)
@@ -1492,7 +1507,7 @@ void RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage *
1492
1507
#endif
1493
1508
} else if (type == ARGUMENTDATATYPE_PIXEL) {
1494
1509
ICoord2D pixel;
1495
- fread (&pixel, sizeof (pixel), 1 , m_file );
1510
+ m_replayFile-> read (&pixel, sizeof (pixel));
1496
1511
msg->appendPixelArgument (pixel);
1497
1512
#ifdef DEBUG_LOGGING
1498
1513
if (m_doingAnalysis)
@@ -1502,7 +1517,7 @@ void RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage *
1502
1517
#endif
1503
1518
} else if (type == ARGUMENTDATATYPE_PIXELREGION) {
1504
1519
IRegion2D reg;
1505
- fread (®, sizeof (reg), 1 , m_file );
1520
+ m_replayFile-> read (®, sizeof (reg));
1506
1521
msg->appendPixelRegionArgument (reg);
1507
1522
#ifdef DEBUG_LOGGING
1508
1523
if (m_doingAnalysis)
@@ -1512,7 +1527,7 @@ void RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage *
1512
1527
#endif
1513
1528
} else if (type == ARGUMENTDATATYPE_TIMESTAMP) { // Not to be confused with Terrance Stamp... Kneel before Zod!!!
1514
1529
UnsignedInt stamp;
1515
- fread (&stamp, sizeof (stamp), 1 , m_file );
1530
+ m_replayFile-> read (&stamp, sizeof (stamp));
1516
1531
msg->appendTimestampArgument (stamp);
1517
1532
#ifdef DEBUG_LOGGING
1518
1533
if (m_doingAnalysis)
@@ -1522,7 +1537,7 @@ void RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage *
1522
1537
#endif
1523
1538
} else if (type == ARGUMENTDATATYPE_WIDECHAR) {
1524
1539
WideChar theid;
1525
- fread (&theid, sizeof (theid), 1 , m_file );
1540
+ m_replayFile-> read (&theid, sizeof (theid));
1526
1541
msg->appendWideCharArgument (theid);
1527
1542
#ifdef DEBUG_LOGGING
1528
1543
if (m_doingAnalysis)
0 commit comments