-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreplays.cpp
1131 lines (1035 loc) · 30.5 KB
/
replays.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 "stdafx.h"
#include "unit.h"
#include <iostream>
#include <fstream>
#include <map>
#include <sstream>
#include <math.h>
#define REPLAY_VERSION 1
#define REPLAY_PLAYERSTART_SIZE 0x81
typedef unsigned int u_int;
using namespace std;
bool replayIsNewPacket=false;
bool replayIsNewMap=false;
void* origClientPacketHandlerEnd;
void* origClientPacketHandlerPointer;
int origClientPacketHandlerSize;
byte* lastEntry;
int lastEntrySize=0;
// èìïîðòû èç Íîêñà
bool (__cdecl *noxReplayStartSave)(const char* filename);
void (__cdecl *noxReplayStopSave)();
bool (__cdecl *noxReplayStartView)(const char* filename);
void (__cdecl *noxReplayStopView)();
void (__cdecl *sub_470A80)();
int *mapFrameTime_6F9838;
int (__cdecl *noxNewSessionStart)();
extern void *(__cdecl *serverGetGameDataBySel)();
extern void *(__cdecl *serverGetGameData)(int N);
extern char *(__cdecl *mapGetName)();
extern void teamCreateDefault(int TeamNumParam, bool notRestrict=false);
extern void (__cdecl *consoleServerMapLoad)(int spaceNumber, int tokensNumber, void* tokens);
extern void (__cdecl *guiServerOptionsStartGameMB)();
extern int (__cdecl *serverModeIndexByModeFlags)(DWORD Flags);
extern short *serverLessonLimitArr;
extern byte *serverTimeLimitArr;
extern int *serverInfoChanged;
extern int (__cdecl *noxCheckGameFlags) (int);
extern void (__cdecl *playerGoObserver)(void* playerPtr, byte unk1, byte unk2);
extern bigUnitStruct* (__cdecl *playerFirstUnit)(); ///âîçâðàùàåò ïåðâûé þíèò ñåòåâîãî èãðîêà
extern bigUnitStruct* (__cdecl *playerNextUnit)(void* Prev); /// Âîçâðàùàåò ñëåäóþùåãî ñåòåâîãî èãðîêà
void* (__cdecl *playerInfoFirst)();
void* (__cdecl *playerInfoNext)(void* Prev);
int (__cdecl *initGame)();
extern DWORD *GameFlags;
extern int (__cdecl *netCreatePlayerStartPacket)(void* Dst, void* playerInfo);
int (__cdecl *deletePlayer)(int netCode);
extern BYTE *(__cdecl *playerInfoFromNetCodeCli) (int NetCode);
void* (__cdecl *netOnPacketRecvCli)(int plrId, void* listPtr, int listSize);
extern int (__cdecl *playerKickByIdx)(int playerIdx, int unknownArg);
extern void* getPlayerUDataFromPlayerIdx(int idx);
extern bigUnitStruct *netUnitByCodeServ(DWORD NetCode);
bigUnitStruct* (__cdecl *unitCreateByThingType)(int thingType);
ServerData replayViewFormGameData;
enum replayEntries
{
REPLAY_HEADER,
REPLAY_FRAME,
};
struct replayFileHeader
{
byte entryType; //default: (byte)REPLAY_HEADER
u_int fileVersion; //default: REPLAY_VERSION
u_int fileFramesCount;
char mapName[8];
__int16 mapMode;
__int16 mapFragLimit;
byte mapTimeLimit;
byte mapPlayersQuantity;
// Followed by a dynamic-sized array of playerStarts packets of players on the map
};
struct replayFileFrame
{
byte entryType; //default: (byte)REPLAY_FRAME
u_int frameId;
u_int frameSize;
// Followed by a dynamic-sized array of bytes with frame data
};
// replayView block
namespace
{
struct replayViewLoadMapStruct
{
char mapName[8];
__int16 mapMode;
__int16 mapFragLimit;
byte mapTimeLimit;
};
bool replayViewEnabled=false;
bool replayViewFormGameRequired=false;
bool replayViewFrameDispatched=false;
bool replayViewRestoreHoster=false;
bool replayViewRestoreHosterFinalize=false;
int replayViewSpeedFactor=0;
int replayViewSpeedFactorCount=0;
map<int, int> replayViewFakeNetCodes;
u_int replayViewFrameSize=0;
byte* replayViewFrame=NULL;
u_int replayViewFrameCount=0;
u_int replayViewSafeSuspendFrames=0;
replayViewLoadMapStruct replayViewLoadMap;
byte* replayViewHosterData=NULL;
ifstream replayView;
bool replayViewReadData();
void replayViewKickAllButHoster();
void replayViewStop()
{
*GameFlags=(*GameFlags)|0x1;
replayViewEnabled=false;
replayViewFormGameRequired=false;
replayViewSafeSuspendFrames=0;
if(replayViewFrame!=NULL)
{
delete [] replayViewFrame;
replayViewFrame=NULL;
replayViewFrameSize=0;
}
replayViewFrameDispatched=false;
replayView.close();
replayView.clear();
replayViewKickAllButHoster();
replayViewRestoreHoster=true;
replayViewRestoreHosterFinalize=false;
replayViewFakeNetCodes.clear();
}
bool replayViewStart(const char* fileName)
{
replayViewStop();
replayViewRestoreHoster=false;
for(byte* P=(byte*)playerInfoFirst(); P!=NULL; P=(byte*)playerInfoNext(P))
{
byte playerIdx = *((byte*)(P+0x810));
if(playerIdx==0x1F)
{
replayViewHosterData = new byte[REPLAY_PLAYERSTART_SIZE];
netCreatePlayerStartPacket(replayViewHosterData, P);
}
//REPLAY_PLAYERSTART_SIZE - ðàçìåð ïàêåòà
}
if(replayViewHosterData==NULL)
{
replayViewStop();
return false;
}
replayView.open(fileName, ios::in | ios::binary);
if(!replayView.is_open())
{
replayViewStop();
return false;
}
char fileType[4];
replayView.read(fileType, 4);
if(replayView.eof() && 0!=strncmp(fileType, "bnru", 4))
{
replayViewStop();
return false;
}
replayViewEnabled=true;
replayViewSafeSuspendFrames=0;
return true;
}
bool replayViewReadData()
{
u_int packetSize=0;
/*char packetIndicator[6];
replayView.read(packetIndicator, 5);*/
replayView.read((char*)&packetSize, sizeof(packetSize));
if(replayView.eof())
{
replayViewStop();
return false;
}
packetSize-=sizeof(packetSize);
u_int currentPosition=replayView.tellg();
byte packetEntry;
replayView.read((char*)&packetEntry, 1);
if(replayView.eof())
{
replayViewStop();
return false;
}
replayView.seekg((u_int)replayView.tellg()-1, ios::beg);
switch(packetEntry)
{
case REPLAY_HEADER:
{
replayFileHeader packetHeader;
replayView.read((char*)&packetHeader, sizeof(packetHeader));
if(packetHeader.fileVersion>REPLAY_VERSION || replayView.eof())
{
// Unsupported replay file version
replayViewStop();
return false;
}
replayViewFrameCount = packetHeader.fileFramesCount;
strncpy(replayViewLoadMap.mapName, packetHeader.mapName, 8);
replayViewLoadMap.mapMode = packetHeader.mapMode;
replayViewLoadMap.mapFragLimit = packetHeader.mapFragLimit;
replayViewLoadMap.mapTimeLimit = packetHeader.mapTimeLimit;
replayViewFrameSize = REPLAY_PLAYERSTART_SIZE*packetHeader.mapPlayersQuantity;
replayViewFrame = new byte[replayViewFrameSize];
replayView.read((char*)replayViewFrame, replayViewFrameSize);
if(replayView.eof())
{
replayViewStop();
return false;
}
replayViewFormGameRequired=true;
replayViewFrameDispatched=true;
}
break;
case REPLAY_FRAME:
{
replayFileFrame packetHeader;
replayView.read((char*)&packetHeader, sizeof(packetHeader));
if(replayView.eof())
{
replayViewStop();
return false;
}
replayViewFrameSize = packetHeader.frameSize;
replayViewFrameDispatched=true;
if(replayViewFrameSize>0)
{
replayViewFrame = new byte[replayViewFrameSize];
replayView.read((char*)replayViewFrame, replayViewFrameSize);
if(replayView.eof())
{
replayViewStop();
return false;
}
}
}
break;
default:
replayView.seekg(currentPosition+packetSize, ios::beg);
}
if((currentPosition+packetSize)!=replayView.tellg())
{
// File probably corrupted
replayViewStop();
return false;
}
//replayView.read(packetIndicator, 3);
return true;
}
void replayViewKickAllButHoster()
{
void* player=playerFirstUnit();
while(player!=NULL)
{
void **PP=(void **)(((char*)player)+0x2EC);
PP=(void**)(((char*)*PP)+0x114);
byte *P=(byte*)(*PP);
byte playerIdx = *((byte*)(P+0x810));
if(playerIdx!=0x1F)
{
playerKickByIdx((int)playerIdx, 4);
}
player=playerNextUnit(player);
}
}
void replayViewLoadFrame(BYTE **frameBuffer, int *size)
{
if(replayViewEnabled)
{
if(replayViewSafeSuspendFrames==0 && replayViewFormGameRequired==false)
{
byte* unitedFrame=NULL;
u_int unitedSize=0;
if(replayViewSpeedFactor<0 && replayViewSpeedFactorCount==0)
replayViewSpeedFactorCount=replayViewSpeedFactor-2;
if(replayViewSpeedFactorCount<0)
{
replayViewSpeedFactorCount++;
if(replayViewSpeedFactorCount<-1)
replayViewFrameDispatched=true;
}
int speedFactor=replayViewSpeedFactor+1;
do
{
speedFactor--;
while(replayViewFrameDispatched==false)
{
if(!replayViewReadData() || replayViewSafeSuspendFrames>0)
{
return;
}
}
replayViewFrameDispatched=false;
u_int unitedSizeBefore=unitedSize;
unitedSize+=replayViewFrameSize;
if(unitedSize>0 && unitedSizeBefore!=unitedSize)
{
unitedFrame=(byte*)realloc(unitedFrame, unitedSize);
memcpy(&unitedFrame[unitedSizeBefore], replayViewFrame, replayViewFrameSize);
}
}
while(speedFactor>0);
*GameFlags=(*GameFlags)^0x1;
*size = unitedSize;
*frameBuffer = unitedFrame;
}
else
replayViewSafeSuspendFrames--;
*GameFlags=(*GameFlags)|0x1;
}
if(replayViewRestoreHoster)
{
if(replayViewHosterData!=NULL)
{
*size = REPLAY_PLAYERSTART_SIZE;
*frameBuffer = replayViewHosterData;
replayViewRestoreHosterFinalize=true;
}
replayViewRestoreHoster=false;
}
}
void replayViewFinalizeFrame()
{
if(replayViewEnabled && replayViewFrameDispatched==false)
{
if(replayViewFrame!=NULL)
{
delete[] replayViewFrame;
replayViewFrame=NULL;
replayViewFrameSize=0;
}
}
if(replayViewRestoreHosterFinalize)
{
delete [] replayViewHosterData;
replayViewHosterData=NULL;
replayViewRestoreHosterFinalize=false;
}
}
void replayViewFormGame()
{
if(replayViewFormGameRequired && replayViewEnabled)
{
*GameFlags=(*GameFlags)|0x1;
ServerData *Data=(ServerData*)serverGetGameDataBySel();
strncpy(Data->mapName, replayViewLoadMap.mapName, 8);
Data->gameFlags = replayViewLoadMap.mapMode|0xe00f;
Data->fragLimit = replayViewLoadMap.mapFragLimit;
Data->timeLimitMB = replayViewLoadMap.mapTimeLimit;
bool sameMap=false;
if(strcmp(Data->mapName,mapGetName())==0)
{
sameMap=true;
}
int Index=serverModeIndexByModeFlags(Data->gameFlags);
serverLessonLimitArr[Index]=Data->fragLimit;
serverTimeLimitArr[Index]=Data->timeLimitMB;
*serverInfoChanged=1;
if(Data->isNew!=1 && Data->gameFlags&0x60)
{
teamCreateDefault(2);
}
else
{
teamCreateDefault(-1);
}
if(sameMap)
{
wchar_t command[5] = L"load";
wchar_t wMapName[9]={0};
int *commandPointer=(int*)&command;
int *wMapNamePointer=(int*)&wMapName;
MultiByteToWideChar(CP_ACP, MB_COMPOSITE, Data->mapName, 8, wMapName, 9);
BYTE result[8]={0};
memcpy(&result[0], &commandPointer, 4);
memcpy(&result[4], &wMapNamePointer, 4);
consoleServerMapLoad(1, 2, (void*)&result);
}
else
{
guiServerOptionsStartGameMB();
}
replayViewSafeSuspendFrames=30;
replayViewFormGameRequired=false;
}
}
void replayViewEachFrame()
{
}
int replayViewSimulateClient(int gf)
{
if((replayViewEnabled && replayViewSafeSuspendFrames==0) || replayViewRestoreHosterFinalize)
{
return 0;
}
return noxCheckGameFlags(gf);
}
int replayViewFakeNetcode(int oldNetCode)
{
if(replayViewEnabled)
{
int newNetCode=oldNetCode;
if(replayViewFakeNetCodes.count(oldNetCode)!=0)
newNetCode = replayViewFakeNetCodes[oldNetCode];
return newNetCode;
}
else
return oldNetCode;
}
int replayViewFakeNetcodeCreate(int thingType, int oldNetCode)
{
if(replayViewEnabled && replayViewSafeSuspendFrames==0 && replayViewFormGameRequired==false)
{
int newNetCode=oldNetCode;
if(replayViewFakeNetCodes.count(oldNetCode)!=0)
newNetCode = replayViewFakeNetCodes[oldNetCode];
else if(thingType!=0)
{
u_int currentPosition=replayView.tellg();
bigUnitStruct* unit = unitCreateByThingType(thingType);
newNetCode = unit->netCode;
replayViewFakeNetCodes[oldNetCode]=newNetCode;
}
return newNetCode;
}
else
return oldNetCode;
}
int replayViewStopL(lua_State* L)
{
replayViewStop();
return 0;
}
int replayViewStartL(lua_State* L)
{
if(lua_type(L, -1)==LUA_TSTRING)
replayViewStart(lua_tostring(L, 1));
else
{
lua_pushstring(L,"wrong args!");
lua_error_(L);
}
return 0;
}
int replayViewSpeedL(lua_State* L)
{
if(lua_type(L, -1)==LUA_TNUMBER)
{
int coef = lua_tonumber(L, 1);
if(coef>=-10 && coef<=10)
{
replayViewSpeedFactor=coef;
}
else
{
lua_pushstring(L,"wrong args: value out of bounds!");
lua_error_(L);
}
}
else
{
lua_pushstring(L,"wrong args!");
lua_error_(L);
}
return 0;
}
//replaySave block
bool replaySaveEnabled=false;
bool replaySaveFrameSaved=false;
u_int replaySaveDropFrame=0;
char* replaySaveFileName = NULL;
byte* replaySaveHeader = NULL;
u_int replaySaveHeaderSize=0;
ofstream replaySave;
u_int replaySaveFrameCount=0;
u_int replaySavePreviousMapLocation=0;
byte* replaySaveFramePackets=NULL;
byte* replaySaveFramePacketsSeeker=NULL;
byte* replaySaveFramePacketsEnd=NULL;
byte* replaySaveFramePayload=NULL;
u_int replaySaveFramePayloadSize=0;
byte* replaySaveFrameBaseAddress=NULL;
bool replaySaveWriteData(replayEntries type, void* payload=NULL, u_int size=0);
void replaySaveStop(bool resetFileName=true)
{
if(resetFileName && replaySaveFileName!=NULL)
{
delete [] replaySaveFileName;
replaySaveFileName=NULL;
replaySaveEnabled=false;
}
if(replaySave.is_open())
{
replaySaveWriteData(REPLAY_HEADER);
}
replaySaveFrameCount=0;
if(replaySaveHeader!=NULL)
{
delete [] replaySaveHeader;
replaySaveHeader=NULL;
}
replaySaveDropFrame=0;
replaySaveHeaderSize=0;
replaySaveFrameBaseAddress=NULL;
replaySaveFrameSaved=false;
replaySave.close();
replaySave.clear();
}
bool replaySaveStart(const char* fileName=NULL, unsigned int fileNameLength=0)
{
if(fileNameLength>0)
{
if(replaySaveFileName!=NULL)
{
replaySaveStop();
}
else
replaySaveStop(!replaySaveEnabled);
replaySaveFileName = new char[fileNameLength+1];
memset(replaySaveFileName, 0, fileNameLength+1);
strncpy(replaySaveFileName, fileName, fileNameLength);
}
else
replaySaveStop(!replaySaveEnabled);
string fileNameFull="replays/";
TIME_ZONE_INFORMATION tz;
SYSTEMTIME time;
GetLocalTime(&time);
GetTimeZoneInformation(&tz);
char timeZone[7];
if(tz.Bias==0)
strcpy(timeZone, "Z");
else
{
char symbol=(tz.Bias>0)?('-'):('+');
u_int tzHours = floor((long double)(abs(tz.Bias)/60));
u_int tzMinutes = abs(tz.Bias)%60;
sprintf(timeZone, "%c%02u.%02u", symbol, tzHours, tzMinutes);
}
char timeStamp[30];
// Ïî÷òè ISO 8601 (ê ñîæàëåíèþ äâîåòî÷èÿ â èìåíè ôàéëîâ íå äîïóñòèìû)
sprintf(timeStamp, "%04i-%02i-%02iT%02i.%02i.%02i.%03i%s", time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds, timeZone);
if(strlen(replaySaveFileName)>0)
{
fileNameFull.append(replaySaveFileName);
fileNameFull.append("_");
}
fileNameFull.append(timeStamp);
fileNameFull.append("_");
fileNameFull.append(mapGetName());
fileNameFull.append(".nru");
replaySave.open(fileNameFull.c_str(), ios::out | ios::trunc | ios::binary);
if(!replaySave.is_open())
{
replaySaveStop();
return false;
}
replaySave.write("bnru", 4); // Ñòàâèì ìåòêó ôîðìàòà, çàîäíî òåñòèðóåì, ðàáîòàåò ëè çàïèñü
if(!replaySave.is_open())
{
replaySaveStop();
return false;
}
replaySaveEnabled=true;
replaySaveFrameCount=0;
replaySaveWriteData(REPLAY_HEADER);
return true;
}
void replaySaveWriteFrameCount()
{
u_int currentPosition=replaySave.tellp();
u_int offset=4+1+sizeof(u_int);
replaySave.seekp(offset, ios::beg);
replaySave.write((const char*)&replaySaveFrameCount, sizeof(u_int));
replaySave.seekp(currentPosition, ios::beg);
}
bool replaySaveWriteData(replayEntries type, void* payload, u_int size)
{
if(!replaySave.is_open())
{
replaySaveStop();
return false;
}
u_int totalLength=0;
byte* packet;
switch(type)
{
case REPLAY_HEADER:
{
// Ãîòîâèì äàííûå. Äëÿ îïðåäåëåíèÿ ðàçìåðîâ
if(replaySaveHeader==NULL)
{
byte* playersList=NULL;
byte playersListQuantity=0;
for(void* P=playerInfoFirst(); P!=NULL; P=playerInfoNext(P))
{
playersListQuantity+=1;
playersList=(byte*)realloc(playersList, playersListQuantity*REPLAY_PLAYERSTART_SIZE);
netCreatePlayerStartPacket(&playersList[(playersListQuantity-1)*REPLAY_PLAYERSTART_SIZE], P);
//REPLAY_PLAYERSTART_SIZE - ðàçìåð ïàêåòà
}
// Ôîðìèðóåì çàãîëîâîê ïàêåòà
replayFileHeader* packetHelper = new replayFileHeader;
memset(packetHelper, 0, sizeof(packetHelper));
packetHelper->entryType=REPLAY_HEADER;
packetHelper->fileVersion=REPLAY_VERSION;
packetHelper->fileFramesCount=replaySaveFrameCount;
strncpy(packetHelper->mapName, mapGetName(),8);
packetHelper->mapMode=(__int16)*GameFlags&0x1FF0;
int Index=serverModeIndexByModeFlags(*GameFlags);
packetHelper->mapFragLimit=serverLessonLimitArr[Index];
packetHelper->mapTimeLimit=serverTimeLimitArr[Index];
packetHelper->mapPlayersQuantity=playersListQuantity;
replaySaveHeaderSize=sizeof(replayFileHeader)+playersListQuantity*REPLAY_PLAYERSTART_SIZE;
replaySaveHeader = new byte[replaySaveHeaderSize];
memcpy(replaySaveHeader, packetHelper, sizeof(replayFileHeader));
memcpy(&replaySaveHeader[sizeof(replayFileHeader)], playersList, playersListQuantity*REPLAY_PLAYERSTART_SIZE);
// Çà÷èñòêà âðåìåííûõ äàííûõ
realloc(playersList, 0);
delete [] packetHelper;
}
else
{
replayFileHeader* packetHelper = (replayFileHeader*)replaySaveHeader;
packetHelper->fileFramesCount=replaySaveFrameCount;
}
// Ïîäãîòîâêà äàííûõ çàâåðøåíà. Ôîðìèðóåì ñàì ïàêåò
packet=new byte[replaySaveHeaderSize];
memset(packet, 0, replaySaveHeaderSize);
memcpy(packet, replaySaveHeader, replaySaveHeaderSize);
totalLength+=replaySaveHeaderSize; // ôèíàëüíûé ðàçìåð ïàêåòà
}
break;
case REPLAY_FRAME:
{
// Ôîðìèðóåì çàãîëîâîê ïàêåòà
replayFileFrame* packetHelper = new replayFileFrame;
memset(packetHelper, 0, sizeof(packetHelper));
packetHelper->entryType=REPLAY_FRAME;
packetHelper->frameId=replaySaveFrameCount;
packetHelper->frameSize = size;
// Ïîäãîòîâêà äàííûõ çàâåðøåíà. Ôîðìèðóåì ñàì ïàêåò
packet=new byte[sizeof(replayFileFrame)+size];
memset(packet, 0, sizeof(replayFileFrame)+size);
memcpy(packet, packetHelper, sizeof(replayFileFrame));
memcpy(&packet[sizeof(replayFileFrame)], payload, size);
totalLength+=sizeof(replayFileFrame)+size;
// Çà÷èñòêà âðåìåííûõ äàííûõ
delete [] packetHelper;
}
break;
}
u_int relocate=0;
if(type==REPLAY_HEADER)
{
relocate=replaySave.tellp();
if(relocate>4)
replaySave.seekp(4, ios::beg);
}
/*char packetIndicator[6];
strncpy(packetIndicator,"start", 5);
replaySave.write(packetIndicator, 5);*/
totalLength+=sizeof(totalLength);
replaySave.write((const char*)&totalLength, sizeof(totalLength));
replaySave.write((const char*)packet, totalLength-sizeof(totalLength));
/*strncpy(packetIndicator,"end", 3);
replaySave.write(packetIndicator, 3);*/
if(type==REPLAY_HEADER && relocate>4)
{
replaySave.seekp(relocate, ios::beg);
}
delete [] packet;
}
void replaySaveAddFramePayload(byte* payload, int size)
{
if(size>0)
{
replaySaveFramePayload=(byte*)realloc(replaySaveFramePayload, replaySaveFramePayloadSize+size);
byte* pointer=replaySaveFramePayload+replaySaveFramePayloadSize;
memset(pointer, 0, size);
memcpy(pointer, payload, size);
}
replaySaveFramePayloadSize+=size;
}
bool replaySaveFullFrame(BYTE *BufStart, int size)
{
if(replaySaveEnabled)
{
// Ñîõðàíÿåì âåñü ôðåéì â áóôôåð, ïîòîì îò íåãî áóäåì îòðåçàòü ïîïàêåòíî. Çàîäíî ñåéâèì â ôàéë èíôó î íîâîì ôðåéìå. Íóæíî ÷òîá â ðåïëåé íå ïîïàëè "ëèøíèå" ïàêåòû.
if(size>0)
{
replaySaveFramePackets = new byte[size];
memcpy(replaySaveFramePackets, BufStart, size);
}
replaySaveFramePacketsSeeker=replaySaveFramePackets;
replaySaveFramePacketsEnd=replaySaveFramePackets+size;
replaySaveFrameCount++;
replaySaveFrameBaseAddress=BufStart;
return true;
}
return false;
}
bool replaySavePacketChecker(byte* packet, int size)
{
// Çäåñü ìû ïðîñòî ïðîâåðèì, ïèõàòü ëè ýòîò ïàêåò â ðåïëåé èëè íó åãî íàôèã.
byte *P = packet;
bool allowed=true; //Åñëè ïàêåò ïðîâåðêó íå ïðîõîäèò - false
// Òóò îñóùåñòâëÿòü ïðîâåðêó ïàêåòà íà âíîñèìîñòü â ðåïëåé. Íà êîäû ïàêåòîâ.
if(*P==0x2B)
{
// Îáðàáîòêà ñìåíû ìàïû
allowed=false;
replaySaveDropFrame=5; // Äðîïàåì ôðåéìû, ÷òî íà ñòûêå äâóõ ôàéëîâ
replaySaveStart(); //Ðåñòàðò ïðîöåññà ïðîèñõîäèò (äëÿ çàïèñè â íîâûé ôàéë). Çàìåòüòå - áåç ñòîïà. Ñòîï îáðàáàòûâàåòñÿ â ñàìîì íà÷àëå replaySaveStart. Ýêñïëèñèòíûé ñòîï íóæå äëÿ ôèíàëèçàöèè âñåãî ïðîöåññà.
}
if(*P==0x6C)
{
allowed=false;
}
if(*P==0x6D)
{
allowed=false;
}
if(allowed)
{
replaySaveAddFramePayload(packet, size);
}
return true;
}
bool replaySavePacketHandler(BYTE *&BufStart, BYTE *&E, bool &found)
{
if(replaySaveEnabled)
{
// Àíàëèç ïàêåòîâ - îòðåçàåì ïî-ïàêåòíî îò îáùåãî áóôåðà ôðåéìà. BufStart ïîñëå àíàëèçà ïàêåòà ñàìèì îáðàáîò÷èêîì ñäâèãàåòñÿ âïåð¸ä, ÷òî ïîçâîëÿåò íàì çàñå÷ü ïàêåò.
u_int size=BufStart-replaySaveFrameBaseAddress;
if(size>0 && (replaySaveFramePacketsSeeker+size)<=replaySaveFramePacketsEnd)
{
byte* packet = new byte[size];
memcpy(packet, replaySaveFramePacketsSeeker, size);
replaySavePacketChecker(packet, size);
delete [] packet;
replaySaveFramePacketsSeeker+=size;
}
replaySaveFrameBaseAddress=BufStart;
return true;
}
return false;
}
bool replaySaveFinalizeFrame()
{
if(replaySaveEnabled)
{
// Àíàëèç ïîñëåäíåãî ïàêåòà - îòðåçàåì ïî-ïàêåòíî. Âûçîâ â ñàìì êîíöå îáðàáîò÷èêà ïàêåòîâ.
if(replaySaveFramePacketsSeeker<=replaySaveFramePacketsEnd)
{
u_int size=replaySaveFramePacketsEnd-replaySaveFramePacketsSeeker;
replaySaveFrameBaseAddress=NULL;
if(size>0)
{
byte* packet = new byte[size];
memcpy(packet, replaySaveFramePacketsSeeker, size);
replaySaveFramePacketsSeeker=replaySaveFramePacketsEnd;
replaySavePacketChecker(packet, size);
}
// Ôèíàëèçèðóåì ôðåéì (ïèøåì â ôàéë) - åñëè íå íàäî äðîïàòü ôðåéì. Ôðåéì äðîïàåòñÿ íà ñòûêå ìåæäó äâóìÿ ôàéëàìè.
if(replaySaveDropFrame==0)
replaySaveWriteData(REPLAY_FRAME, replaySaveFramePayload, replaySaveFramePayloadSize);
else
replaySaveDropFrame--;
replaySaveFrameSaved = true;
// Çà÷èùàåì
replaySaveFramePayloadSize=0;
if(replaySaveFramePayload!=NULL)
{
realloc(replaySaveFramePayload, 0);
replaySaveFramePayload=NULL;
}
if(replaySaveFramePackets!=NULL)
{
delete [] replaySaveFramePackets;
replaySaveFramePackets=NULL;
}
replaySaveFramePacketsSeeker=NULL;
replaySaveFramePacketsEnd=NULL;
}
return true;
}
return false;
}
void replaySaveZeroFrame()
{
if(replaySaveEnabled && replaySaveDropFrame==0)
{
if(replaySaveFrameSaved)
{
replaySaveFrameSaved=false;
}
else
{
replaySaveWriteData(REPLAY_FRAME);
}
}
}
int replaySaveStopL(lua_State* L) //îá¸ðòêà äëÿ ËÓÀ
{
replaySaveStop();
return 0;
}
int replaySaveStartL(lua_State* L) //îá¸ðòêà äëÿ ËÓÀ
{
if(lua_type(L, -1)==LUA_TSTRING)
{
const char *S=lua_tostring(L,-1);
if(replaySaveStart(S, strlen(S)))
return 0;
else
{
lua_pushstring(L,"file save error!");
lua_error_(L);
}
}
else
{
if(replaySaveStart())
return 0;
else
{
lua_pushstring(L,"file save error!");
lua_error_(L);
}
}
return 0;
}
//common replay block
//Ñàìûé êîíåö netOnPacketRecvCli.
void clientPacketHandlerEnd(void* BufStart, int size, int edx, int ecx, int eax, void* returnFunction)
{
returnFunction = origClientPacketHandlerEnd;
BufStart = origClientPacketHandlerPointer;
size = origClientPacketHandlerSize;
replaySaveFinalizeFrame();
replayViewFinalizeFrame();
}
//internal - ÷òî-òî èç îáëàñòè ÷¸ðíîé ìàãèè. Ñàì óæå íå ïîìíþ ÷òî.
void __declspec(naked) clientPacketHandlerEndTrap()
{
__asm
{
push 0
push eax
push ecx
push edx
push 0
push 0
call clientPacketHandlerEnd
pop edx
pop ecx
mov [esp+16+4], edx
mov [esp+16+4+4], ecx
pop edx
pop ecx
pop eax
ret
}
}
// Ñàìîå íà÷àëî netOnPacketRecvCli. Àäðåñ âîçâðàòà, íà÷àëî îáðàáàòûâàåìîãî áóôåðà è ðàçìåð
void* __cdecl replayInterceptClientPackets(void* noxOrigClientPacketHandlerEnd, BYTE *BufStart, int size) //Âíèìàíèå! Ìîäèôèêàöèÿ ïåðåìåííûõ ôóíêöèè ïðèâåä¸ò ê èçìåíåíèþ îíûõ â onPacketRecvCli!
{
origClientPacketHandlerEnd=noxOrigClientPacketHandlerEnd; // Çäåñü àäðåñ ñòàðîãî return-à.
origClientPacketHandlerPointer=BufStart;
origClientPacketHandlerSize=size;
replaySaveFullFrame(BufStart, size);
replayViewLoadFrame(&BufStart, &size);
return &clientPacketHandlerEndTrap; // Ïîäìåíà return-à èç onNetPacketCli.
}
//internal
void __cdecl packetHandlerBegin()
{
//replayIsNewPacket=true;
sub_470A80();
}
//internal - òîæ ÷¸ðíàÿ ìàãèÿ.
void __declspec(naked) clientPacketHandlerBeginTrap()
{
__asm
{
push eax
push ebp
mov eax, [esp+0x1748+4+8] // Âûòàñêèâàåì àäðåñ ñòàíäàðòíîãî return-à
push eax
call replayInterceptClientPackets
mov [esp+0x1748+4+8+4], eax // Ïîäìåíà return-à èç onNetPacketCli.
pop eax
pop ebp
pop eax
lea ebx, [eax+ebp]
mov [esp+0x1748+4-0x1704], ebx //Ïîñëåäíÿÿ ïîçèöèÿ â ñòåêå ïåðåä call - 1748h. Ïîçèöèÿ â ñòåêå ïåðåìåííîé - -1704h. Ñîîòâ. +4 (àäðåñ âîçâðàòà ïî ret)
mov [esp+0x1748+4+0x8], ebp //Àíàëîãè÷íî ïðåäûäóùåìó
mov [esp+0x1748+4+0xC], eax //Àíàëîãè÷íî ïðåäûäóùåìó
call packetHandlerBegin
ret
}
}
}
void replayGuiUpdate()
{
replaySaveZeroFrame();
replayViewFormGame();
}
//onNetPacket äëÿ ðåïëååâ.
bool replayPacketHandler(BYTE *&BufStart, BYTE *&E, bool &found)
{
replaySavePacketHandler(BufStart, E, found);
return true;
}
void __cdecl replayQuitGame()
{
replaySaveStop();
replayViewStop();
//
noxReplayStopSave();
}
void replayEachFrame()
{
replayViewEachFrame();
}
int __cdecl replayOnSpriteRequest(int oldNetCode)
{
//return replayViewFakeNetcode(oldNetCode);
return oldNetCode;
}
int __cdecl replayOnSpriteCreate(int thingType, int oldNetCode)
{