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
|
/*
Bot replay recording logic and processes.
Records data every time OnPlayerRunCmdPost is called.
If the player doesn't have their timer running, it keeps track
of the last 2 minutes of their actions. If a player is banned
while their timer isn't running, those 2 minutes are saved.
If the player has their timer running, the recording is done from
the beginning of the run. If the player can no longer beat their PB,
then the recording goes back to only keeping track of the last
two minutes. Upon beating their PB, a temporary binary file will be
written with a 'header' containing information about the run,
followed by the recorded tick data from OnPlayerRunCmdPost.
The binary file will be permanently locally saved on the server
if the run beats the server record.
*/
static float tickrate;
static int preAndPostRunTickCount;
static int maxCheaterReplayTicks;
static int recordingIndex[MAXPLAYERS + 1];
static float playerSensitivity[MAXPLAYERS + 1];
static float playerMYaw[MAXPLAYERS + 1];
static bool isTeleportTick[MAXPLAYERS + 1];
static ReplaySaveState replaySaveState[MAXPLAYERS + 1];
static bool recordingPaused[MAXPLAYERS + 1];
static bool postRunRecording[MAXPLAYERS + 1];
static ArrayList recordedRecentData[MAXPLAYERS + 1];
static ArrayList recordedRunData[MAXPLAYERS + 1];
static ArrayList recordedPostRunData[MAXPLAYERS + 1];
static Handle runningRunBreatherTimer[MAXPLAYERS + 1];
static ArrayList runningJumpstatTimers[MAXPLAYERS + 1];
// =====[ EVENTS ]=====
void OnMapStart_Recording()
{
CreateReplaysDirectory(gC_CurrentMap);
tickrate = 1/GetTickInterval();
preAndPostRunTickCount = RoundToZero(RP_PLAYBACK_BREATHER_TIME * tickrate);
maxCheaterReplayTicks = RoundToCeil(RP_MAX_CHEATER_REPLAY_LENGTH * tickrate);
}
void OnClientPutInServer_Recording(int client)
{
ClearClientRecordingState(client);
}
void OnClientAuthorized_Recording(int client)
{
// Apparently the client isn't valid yet here, so we can't check for that!
if(!IsFakeClient(client))
{
// Create directory path for player if not exists
char replayPath[PLATFORM_MAX_PATH];
BuildPath(Path_SM, replayPath, sizeof(replayPath), "%s/%d", RP_DIRECTORY_JUMPS, GetSteamAccountID(client));
if (!DirExists(replayPath))
{
CreateDirectory(replayPath, 511);
}
BuildPath(Path_SM, replayPath, sizeof(replayPath), "%s/%d/%s", RP_DIRECTORY_JUMPS, GetSteamAccountID(client), RP_DIRECTORY_BLOCKJUMPS);
if (!DirExists(replayPath))
{
CreateDirectory(replayPath, 511);
}
}
}
void OnClientDisconnect_Recording(int client)
{
// Stop exceptions if OnClientPutInServer was never ran for this client id.
// As long as the arrays aren't null we'll be fine.
if (runningJumpstatTimers[client] == null)
{
return;
}
// Trigger all timers early
if(!IsFakeClient(client))
{
if (runningRunBreatherTimer[client] != INVALID_HANDLE)
{
TriggerTimer(runningRunBreatherTimer[client], false);
}
// We have to clone the array because the timer callback removes the timer
// from the array we're running over, and doing weird tricks is scary.
ArrayList timers = runningJumpstatTimers[client].Clone();
for (int i = 0; i < timers.Length; i++)
{
Handle timer = timers.Get(i);
TriggerTimer(timer, false);
}
delete timers;
}
ClearClientRecordingState(client);
}
void OnPlayerRunCmdPost_Recording(int client, int buttons, int tickCount, const float vel[3], const int mouse[2])
{
if (!IsValidClient(client) || IsFakeClient(client) || !IsPlayerAlive(client) || recordingPaused[client])
{
return;
}
ReplayTickData tickData;
Movement_GetOrigin(client, tickData.origin);
tickData.mouse = mouse;
tickData.vel = vel;
Movement_GetVelocity(client, tickData.velocity);
Movement_GetEyeAngles(client, tickData.angles);
tickData.flags = EncodePlayerFlags(client, buttons, tickCount);
tickData.packetsPerSecond = GetClientAvgPackets(client, NetFlow_Incoming);
tickData.laggedMovementValue = GetEntPropFloat(client, Prop_Send, "m_flLaggedMovementValue");
tickData.buttonsForced = GetEntProp(client, Prop_Data, "m_afButtonForced");
// HACK: Reset teleport tick marker. Too bad!
if (isTeleportTick[client])
{
isTeleportTick[client] = false;
}
if (replaySaveState[client] != ReplaySave_Disabled)
{
int runTick = GetArraySize(recordedRunData[client]);
if (runTick < RP_MAX_DURATION)
{
// Resize might fail if the timer exceed the max duration,
// as it is not guaranteed to allocate more than 1GB of contiguous memory,
// causing mass lag spikes that kick everyone out of the server.
// We can still attempt to save the rest of the recording though.
recordedRunData[client].Resize(runTick + 1);
recordedRunData[client].SetArray(runTick, tickData);
}
}
if (postRunRecording[client])
{
int tick = GetArraySize(recordedPostRunData[client]);
if (tick < RP_MAX_DURATION)
{
recordedPostRunData[client].Resize(tick + 1);
recordedPostRunData[client].SetArray(tick, tickData);
}
}
int tick = recordingIndex[client];
if (recordedRecentData[client].Length < maxCheaterReplayTicks)
{
recordedRecentData[client].Resize(recordedRecentData[client].Length + 1);
recordingIndex[client] = recordingIndex[client] + 1 == maxCheaterReplayTicks ? 0 : recordingIndex[client] + 1;
}
else
{
recordingIndex[client] = RecordingIndexAdd(client, 1);
}
recordedRecentData[client].SetArray(tick, tickData);
}
Action GOKZ_OnTimerStart_Recording(int client)
{
// Hack to fix an exception when starting the timer on the very
// first tick after loading the plugin.
if (recordedRecentData[client].Length == 0)
{
return Plugin_Handled;
}
return Plugin_Continue;
}
void GOKZ_OnTimerStart_Post_Recording(int client)
{
replaySaveState[client] = ReplaySave_Local;
StartRunRecording(client);
}
void GOKZ_OnTimerEnd_Recording(int client, int course, float time, int teleportsUsed)
{
if (replaySaveState[client] == ReplaySave_Disabled)
{
return;
}
DataPack data = new DataPack();
data.WriteCell(GetClientUserId(client));
data.WriteCell(course);
data.WriteFloat(time);
data.WriteCell(teleportsUsed);
data.WriteCell(replaySaveState[client]);
// The previous run breather still did not finish, end it now or
// we will start overwriting the data.
if (runningRunBreatherTimer[client] != INVALID_HANDLE)
{
TriggerTimer(runningRunBreatherTimer[client], false);
}
replaySaveState[client] = ReplaySave_Disabled;
postRunRecording[client] = true;
// Swap recordedRunData and recordedPostRunData.
// This lets new runs start immediately, before the post-run breather is
// finished recording.
ArrayList tmp = recordedPostRunData[client];
recordedPostRunData[client] = recordedRunData[client];
recordedRunData[client] = tmp;
recordedRunData[client].Clear();
runningRunBreatherTimer[client] = CreateTimer(RP_PLAYBACK_BREATHER_TIME, Timer_EndRecording, data);
if (runningRunBreatherTimer[client] == INVALID_HANDLE)
{
LogError("Could not create a timer so can't end the run replay recording");
}
}
public Action Timer_EndRecording(Handle timer, DataPack data)
{
data.Reset();
int client = GetClientOfUserId(data.ReadCell());
int course = data.ReadCell();
float time = data.ReadFloat();
int teleportsUsed = data.ReadCell();
ReplaySaveState saveState = data.ReadCell();
delete data;
// The client left after the run was done but before the post-run
// breather had the chance to finish. This should not happen, as we
// trigger all running timers on disconnect.
if (!IsValidClient(client))
{
return Plugin_Stop;
}
runningRunBreatherTimer[client] = INVALID_HANDLE;
postRunRecording[client] = false;
if (gB_GOKZLocalDB && GOKZ_DB_IsCheater(client))
{
// Replay might be submitted globally, but will not be saved locally.
saveState = ReplaySave_Temp;
}
char path[PLATFORM_MAX_PATH];
if (SaveRecordingOfRun(path, client, course, time, teleportsUsed, saveState == ReplaySave_Temp))
{
Call_OnTimerEnd_Post(client, path, course, time, teleportsUsed);
}
else
{
Call_OnTimerEnd_Post(client, "", course, time, teleportsUsed);
}
return Plugin_Stop;
}
void GOKZ_OnPause_Recording(int client)
{
PauseRecording(client);
}
void GOKZ_OnResume_Recording(int client)
{
ResumeRecording(client);
}
void GOKZ_OnTimerStopped_Recording(int client)
{
replaySaveState[client] = ReplaySave_Disabled;
}
void GOKZ_OnCountedTeleport_Recording(int client)
{
if (gB_NubRecordMissed[client])
{
replaySaveState[client] = ReplaySave_Disabled;
}
isTeleportTick[client] = true;
}
void GOKZ_LR_OnRecordMissed_Recording(int client, int recordType)
{
if (replaySaveState[client] == ReplaySave_Disabled)
{
return;
}
// If missed PRO record or both records, then can no longer beat a server record
if (recordType == RecordType_NubAndPro || recordType == RecordType_Pro)
{
replaySaveState[client] = ReplaySave_Temp;
}
// If on a NUB run and missed NUB record, then can no longer beat a server record
// Otherwise wait to see if they teleport before stopping the recording
if (recordType == RecordType_Nub)
{
if (GOKZ_GetTeleportCount(client) > 0)
{
replaySaveState[client] = ReplaySave_Temp;
}
}
}
public void GOKZ_LR_OnPBMissed(int client, float pbTime, int course, int mode, int style, int recordType)
{
if (replaySaveState[client] == ReplaySave_Disabled)
{
return;
}
// If missed PRO record or both records, then can no longer beat PB
if (recordType == RecordType_NubAndPro || recordType == RecordType_Pro)
{
replaySaveState[client] = ReplaySave_Disabled;
}
// If on a NUB run and missed NUB record, then can no longer beat PB
// Otherwise wait to see if they teleport before stopping the recording
if (recordType == RecordType_Nub)
{
if (GOKZ_GetTeleportCount(client) > 0)
{
replaySaveState[client] = ReplaySave_Disabled;
}
}
}
void GOKZ_AC_OnPlayerSuspected_Recording(int client, ACReason reason)
{
SaveRecordingOfCheater(client, reason);
}
void GOKZ_DB_OnJumpstatPB_Recording(int client, int jumptype, float distance, int block, int strafes, float sync, float pre, float max, int airtime)
{
DataPack data = new DataPack();
data.WriteCell(GetClientUserId(client));
data.WriteCell(jumptype);
data.WriteFloat(distance);
data.WriteCell(block);
data.WriteCell(strafes);
data.WriteFloat(sync);
data.WriteFloat(pre);
data.WriteFloat(max);
data.WriteCell(airtime);
Handle timer = CreateTimer(RP_PLAYBACK_BREATHER_TIME, SaveJump, data);
if (timer != INVALID_HANDLE)
{
runningJumpstatTimers[client].Push(timer);
}
else
{
LogError("Could not create a timer so can't save jumpstat pb replay");
}
}
public Action SaveJump(Handle timer, DataPack data)
{
data.Reset();
int client = GetClientOfUserId(data.ReadCell());
int jumptype = data.ReadCell();
float distance = data.ReadFloat();
int block = data.ReadCell();
int strafes = data.ReadCell();
float sync = data.ReadFloat();
float pre = data.ReadFloat();
float max = data.ReadFloat();
int airtime = data.ReadCell();
delete data;
// The client left after the jump was done but before the post-jump
// breather had the chance to finish. This should not happen, as we
// trigger all running timers on disconnect.
if (!IsValidClient(client))
{
return Plugin_Stop;
}
RemoveFromRunningTimers(client, timer);
SaveRecordingOfJump(client, jumptype, distance, block, strafes, sync, pre, max, airtime);
return Plugin_Stop;
}
// =====[ PRIVATE ]=====
static void ClearClientRecordingState(int client)
{
recordingIndex[client] = 0;
playerSensitivity[client] = -1.0;
playerMYaw[client] = -1.0;
isTeleportTick[client] = false;
replaySaveState[client] = ReplaySave_Disabled;
recordingPaused[client] = false;
postRunRecording[client] = false;
runningRunBreatherTimer[client] = INVALID_HANDLE;
if (recordedRecentData[client] == null)
recordedRecentData[client] = new ArrayList(sizeof(ReplayTickData));
if (recordedRunData[client] == null)
recordedRunData[client] = new ArrayList(sizeof(ReplayTickData));
if (recordedPostRunData[client] == null)
recordedPostRunData[client] = new ArrayList(sizeof(ReplayTickData));
if (runningJumpstatTimers[client] == null)
runningJumpstatTimers[client] = new ArrayList();
recordedRecentData[client].Clear();
recordedRunData[client].Clear();
recordedPostRunData[client].Clear();
runningJumpstatTimers[client].Clear();
}
static void StartRunRecording(int client)
{
if (IsFakeClient(client))
{
return;
}
QueryClientConVar(client, "sensitivity", SensitivityCheck, client);
QueryClientConVar(client, "m_yaw", MYAWCheck, client);
DiscardRecording(client);
ResumeRecording(client);
// Copy pre data
int index;
recordedRunData[client].Resize(preAndPostRunTickCount);
if (recordedRecentData[client].Length < preAndPostRunTickCount)
{
index = recordingIndex[client] - preAndPostRunTickCount;
}
else
{
index = RecordingIndexAdd(client, -preAndPostRunTickCount);
}
for (int i = 0; i < preAndPostRunTickCount; i++)
{
ReplayTickData tickData;
if (index < 0)
{
recordedRecentData[client].GetArray(0, tickData);
recordedRunData[client].SetArray(i, tickData);
index += 1;
}
else
{
recordedRecentData[client].GetArray(index, tickData);
recordedRunData[client].SetArray(i, tickData);
index = RecordingIndexAdd(client, -preAndPostRunTickCount + i + 1);
}
}
}
static void DiscardRecording(int client)
{
recordedRunData[client].Clear();
Call_OnReplayDiscarded(client);
}
static void PauseRecording(int client)
{
recordingPaused[client] = true;
}
static void ResumeRecording(int client)
{
recordingPaused[client] = false;
}
static bool SaveRecordingOfRun(char replayPath[PLATFORM_MAX_PATH], int client, int course, float time, int teleportsUsed, bool temp)
{
// Prepare data
int timeType = GOKZ_GetTimeTypeEx(teleportsUsed);
// Create and fill General Header
GeneralReplayHeader generalHeader;
FillGeneralHeader(generalHeader, client, ReplayType_Run, recordedPostRunData[client].Length);
// Create and fill Run Header
RunReplayHeader runHeader;
runHeader.time = time;
runHeader.course = course;
runHeader.teleportsUsed = teleportsUsed;
// Build path and create/overwrite associated file
FormatRunReplayPath(replayPath, sizeof(replayPath), course, generalHeader.mode, generalHeader.style, timeType, temp);
if (FileExists(replayPath))
{
DeleteFile(replayPath);
}
else if (!temp)
{
AddToReplayInfoCache(course, generalHeader.mode, generalHeader.style, timeType);
SortReplayInfoCache();
}
File file = OpenFile(replayPath, "wb");
if (file == null)
{
LogError("Failed to create/open replay file to write to: \"%s\".", replayPath);
return false;
}
WriteGeneralHeader(file, generalHeader);
// Write run header
file.WriteInt32(view_as<int>(runHeader.time));
file.WriteInt8(runHeader.course);
file.WriteInt32(runHeader.teleportsUsed);
WriteTickData(file, client, ReplayType_Run);
delete file;
// If there is no plugin that wants to take over the replay file, we will delete it ourselves.
if (Call_OnReplaySaved(client, ReplayType_Run, gC_CurrentMap, course, timeType, time, replayPath, temp) == Plugin_Continue && temp)
{
DeleteFile(replayPath);
}
return true;
}
static bool SaveRecordingOfCheater(int client, ACReason reason)
{
// Create and fill general header
GeneralReplayHeader generalHeader;
FillGeneralHeader(generalHeader, client, ReplayType_Cheater, recordedRecentData[client].Length);
// Create and fill cheater header
CheaterReplayHeader cheaterHeader;
cheaterHeader.ACReason = reason;
//Build path and create/overwrite associated file
char replayPath[PLATFORM_MAX_PATH];
FormatCheaterReplayPath(replayPath, sizeof(replayPath), client, generalHeader.mode, generalHeader.style);
File file = OpenFile(replayPath, "wb");
if (file == null)
{
LogError("Failed to create/open replay file to write to: \"%s\".", replayPath);
return false;
}
WriteGeneralHeader(file, generalHeader);
file.WriteInt8(view_as<int>(cheaterHeader.ACReason));
WriteTickData(file, client, ReplayType_Cheater);
delete file;
return true;
}
static bool SaveRecordingOfJump(int client, int jumptype, float distance, int block, int strafes, float sync, float pre, float max, int airtime)
{
// Just cause I know how buggy jumpstats can be
int airtimeTicks = RoundToNearest((float(airtime) / GOKZ_DB_JS_AIRTIME_PRECISION) * tickrate);
if (airtimeTicks + 2 * preAndPostRunTickCount >= maxCheaterReplayTicks)
{
LogError("WARNING: Invalid airtime (this is probably a bugged jump, please report it!).");
return false;
}
// Create and fill general header
GeneralReplayHeader generalHeader;
FillGeneralHeader(generalHeader, client, ReplayType_Jump, 2 * preAndPostRunTickCount + airtimeTicks);
// Create and fill jump header
JumpReplayHeader jumpHeader;
FillJumpHeader(jumpHeader, jumptype, distance, block, strafes, sync, pre, max, airtime);
// Make sure the client is authenticated
if (GetSteamAccountID(client) == 0)
{
LogError("Failed to save jump, client is not authenticated.");
return false;
}
// Build path and create/overwrite associated file
char replayPath[PLATFORM_MAX_PATH];
if (block > 0)
{
FormatBlockJumpReplayPath(replayPath, sizeof(replayPath), client, block, jumpHeader.jumpType, generalHeader.mode, generalHeader.style);
}
else
{
FormatJumpReplayPath(replayPath, sizeof(replayPath), client, jumpHeader.jumpType, generalHeader.mode, generalHeader.style);
}
File file = OpenFile(replayPath, "wb");
if (file == null)
{
LogError("Failed to create/open replay file to write to: \"%s\".", replayPath);
delete file;
return false;
}
WriteGeneralHeader(file, generalHeader);
WriteJumpHeader(file, jumpHeader);
WriteTickData(file, client, ReplayType_Jump, airtimeTicks);
delete file;
return true;
}
static void FillGeneralHeader(GeneralReplayHeader generalHeader, int client, int replayType, int tickCount)
{
// Prepare data
int mode = GOKZ_GetCoreOption(client, Option_Mode);
int style = GOKZ_GetCoreOption(client, Option_Style);
// Fill general header
generalHeader.magicNumber = RP_MAGIC_NUMBER;
generalHeader.formatVersion = RP_FORMAT_VERSION;
generalHeader.replayType = replayType;
generalHeader.gokzVersion = GOKZ_VERSION;
generalHeader.mapName = gC_CurrentMap;
generalHeader.mapFileSize = gI_CurrentMapFileSize;
generalHeader.serverIP = FindConVar("hostip").IntValue;
generalHeader.timestamp = GetTime();
GetClientName(client, generalHeader.playerAlias, sizeof(GeneralReplayHeader::playerAlias));
generalHeader.playerSteamID = GetSteamAccountID(client);
generalHeader.mode = mode;
generalHeader.style = style;
generalHeader.playerSensitivity = playerSensitivity[client];
generalHeader.playerMYaw = playerMYaw[client];
generalHeader.tickrate = tickrate;
generalHeader.tickCount = tickCount;
generalHeader.equippedWeapon = GetPlayerWeaponSlotDefIndex(client, CS_SLOT_SECONDARY);
generalHeader.equippedKnife = GetPlayerWeaponSlotDefIndex(client, CS_SLOT_KNIFE);
}
static void FillJumpHeader(JumpReplayHeader jumpHeader, int jumptype, float distance, int block, int strafes, float sync, float pre, float max, int airtime)
{
jumpHeader.jumpType = jumptype;
jumpHeader.distance = distance;
jumpHeader.blockDistance = block;
jumpHeader.strafeCount = strafes;
jumpHeader.sync = sync;
jumpHeader.pre = pre;
jumpHeader.max = max;
jumpHeader.airtime = airtime;
}
static void WriteGeneralHeader(File file, GeneralReplayHeader generalHeader)
{
file.WriteInt32(generalHeader.magicNumber);
file.WriteInt8(generalHeader.formatVersion);
file.WriteInt8(generalHeader.replayType);
file.WriteInt8(strlen(generalHeader.gokzVersion));
file.WriteString(generalHeader.gokzVersion, false);
file.WriteInt8(strlen(generalHeader.mapName));
file.WriteString(generalHeader.mapName, false);
file.WriteInt32(generalHeader.mapFileSize);
file.WriteInt32(generalHeader.serverIP);
file.WriteInt32(generalHeader.timestamp);
file.WriteInt8(strlen(generalHeader.playerAlias));
file.WriteString(generalHeader.playerAlias, false);
file.WriteInt32(generalHeader.playerSteamID);
file.WriteInt8(generalHeader.mode);
file.WriteInt8(generalHeader.style);
file.WriteInt32(view_as<int>(generalHeader.playerSensitivity));
file.WriteInt32(view_as<int>(generalHeader.playerMYaw));
file.WriteInt32(view_as<int>(generalHeader.tickrate));
file.WriteInt32(generalHeader.tickCount);
file.WriteInt32(generalHeader.equippedWeapon);
file.WriteInt32(generalHeader.equippedKnife);
}
static void WriteJumpHeader(File file, JumpReplayHeader jumpHeader)
{
file.WriteInt8(jumpHeader.jumpType);
file.WriteInt32(view_as<int>(jumpHeader.distance));
file.WriteInt32(jumpHeader.blockDistance);
file.WriteInt8(jumpHeader.strafeCount);
file.WriteInt32(view_as<int>(jumpHeader.sync));
file.WriteInt32(view_as<int>(jumpHeader.pre));
file.WriteInt32(view_as<int>(jumpHeader.max));
file.WriteInt32((jumpHeader.airtime));
}
static void WriteTickData(File file, int client, int replayType, int airtime = 0)
{
ReplayTickData tickData;
ReplayTickData prevTickData;
bool isFirstTick = true;
switch(replayType)
{
case ReplayType_Run:
{
for (int i = 0; i < recordedPostRunData[client].Length; i++)
{
recordedPostRunData[client].GetArray(i, tickData);
recordedPostRunData[client].GetArray(IntMax(0, i-1), prevTickData);
WriteTickDataToFile(file, isFirstTick, tickData, prevTickData);
isFirstTick = false;
}
}
case ReplayType_Cheater:
{
for (int i = 0; i < recordedRecentData[client].Length; i++)
{
int rollingI = RecordingIndexAdd(client, i);
recordedRecentData[client].GetArray(rollingI, tickData);
recordedRecentData[client].GetArray(IntMax(0, i-1), prevTickData);
WriteTickDataToFile(file, isFirstTick, tickData, prevTickData);
isFirstTick = false;
}
}
case ReplayType_Jump:
{
int replayLength = 2 * preAndPostRunTickCount + airtime;
for (int i = 0; i < replayLength; i++)
{
int rollingI = RecordingIndexAdd(client, i - replayLength);
recordedRecentData[client].GetArray(rollingI, tickData);
recordedRecentData[client].GetArray(IntMax(0, i-1), prevTickData);
WriteTickDataToFile(file, isFirstTick, tickData, prevTickData);
isFirstTick = false;
}
}
}
}
static void WriteTickDataToFile(File file, bool isFirstTick, ReplayTickData tickDataStruct, ReplayTickData prevTickDataStruct)
{
any tickData[RP_V2_TICK_DATA_BLOCKSIZE];
any prevTickData[RP_V2_TICK_DATA_BLOCKSIZE];
TickDataToArray(tickDataStruct, tickData);
TickDataToArray(prevTickDataStruct, prevTickData);
int deltaFlags = (1 << RPDELTA_DELTAFLAGS);
if (isFirstTick)
{
// NOTE: Set every bit to 1 until RP_V2_TICK_DATA_BLOCKSIZE.
deltaFlags = (1 << (RP_V2_TICK_DATA_BLOCKSIZE)) - 1;
}
else
{
// NOTE: Test tickData against prevTickData for differences.
for (int i = 1; i < sizeof(tickData); i++)
{
// If the bits in tickData[i] are different to prevTickData[i], then
// set the corresponding bitflag.
if (tickData[i] ^ prevTickData[i])
{
deltaFlags |= (1 << i);
}
}
}
file.WriteInt32(deltaFlags);
// NOTE: write only data that has changed since the previous tick.
for (int i = 1; i < sizeof(tickData); i++)
{
int currentFlag = (1 << i);
if (deltaFlags & currentFlag)
{
file.WriteInt32(tickData[i]);
}
}
}
static void FormatRunReplayPath(char[] buffer, int maxlength, int course, int mode, int style, int timeType, bool tempPath)
{
// Use GetEngineTime to prevent accidental replay overrides.
// Technically it would still be possible to override this file by accident,
// if somehow the server restarts to this exact map and course,
// and this function is run at the exact same time, but that is extremely unlikely.
// Also by then this file should have already been deleted.
char tempTimeString[32];
Format(tempTimeString, sizeof(tempTimeString), "%f_", GetEngineTime());
BuildPath(Path_SM, buffer, maxlength,
"%s/%s/%s%d_%s_%s_%s.%s",
tempPath ? RP_DIRECTORY_RUNS_TEMP : RP_DIRECTORY_RUNS,
gC_CurrentMap,
tempPath ? tempTimeString : "",
course,
gC_ModeNamesShort[mode],
gC_StyleNamesShort[style],
gC_TimeTypeNames[timeType],
RP_FILE_EXTENSION);
}
static void FormatCheaterReplayPath(char[] buffer, int maxlength, int client, int mode, int style)
{
BuildPath(Path_SM, buffer, maxlength,
"%s/%d_%s_%d_%s_%s.%s",
RP_DIRECTORY_CHEATERS,
GetSteamAccountID(client),
gC_CurrentMap,
GetTime(),
gC_ModeNamesShort[mode],
gC_StyleNamesShort[style],
RP_FILE_EXTENSION);
}
static void FormatJumpReplayPath(char[] buffer, int maxlength, int client, int jumpType, int mode, int style)
{
BuildPath(Path_SM, buffer, maxlength,
"%s/%d/%d_%s_%s.%s",
RP_DIRECTORY_JUMPS,
GetSteamAccountID(client),
jumpType,
gC_ModeNamesShort[mode],
gC_StyleNamesShort[style],
RP_FILE_EXTENSION);
}
static void FormatBlockJumpReplayPath(char[] buffer, int maxlength, int client, int block, int jumpType, int mode, int style)
{
BuildPath(Path_SM, buffer, maxlength,
"%s/%d/%s/%d_%d_%s_%s.%s",
RP_DIRECTORY_JUMPS,
GetSteamAccountID(client),
RP_DIRECTORY_BLOCKJUMPS,
jumpType,
block,
gC_ModeNamesShort[mode],
gC_StyleNamesShort[style],
RP_FILE_EXTENSION);
}
static int EncodePlayerFlags(int client, int buttons, int tickCount)
{
int flags = 0;
MoveType movetype = Movement_GetMovetype(client);
int clientFlags = GetEntityFlags(client);
flags = view_as<int>(movetype) & RP_MOVETYPE_MASK;
SetKthBit(flags, 4, IsBitSet(buttons, IN_ATTACK));
SetKthBit(flags, 5, IsBitSet(buttons, IN_ATTACK2));
SetKthBit(flags, 6, IsBitSet(buttons, IN_JUMP));
SetKthBit(flags, 7, IsBitSet(buttons, IN_DUCK));
SetKthBit(flags, 8, IsBitSet(buttons, IN_FORWARD));
SetKthBit(flags, 9, IsBitSet(buttons, IN_BACK));
SetKthBit(flags, 10, IsBitSet(buttons, IN_LEFT));
SetKthBit(flags, 11, IsBitSet(buttons, IN_RIGHT));
SetKthBit(flags, 12, IsBitSet(buttons, IN_MOVELEFT));
SetKthBit(flags, 13, IsBitSet(buttons, IN_MOVERIGHT));
SetKthBit(flags, 14, IsBitSet(buttons, IN_RELOAD));
SetKthBit(flags, 15, IsBitSet(buttons, IN_SPEED));
SetKthBit(flags, 16, IsBitSet(buttons, IN_USE));
SetKthBit(flags, 17, IsBitSet(buttons, IN_BULLRUSH));
SetKthBit(flags, 18, IsBitSet(clientFlags, FL_ONGROUND));
SetKthBit(flags, 19, IsBitSet(clientFlags, FL_DUCKING));
SetKthBit(flags, 20, IsBitSet(clientFlags, FL_SWIM));
SetKthBit(flags, 21, GetEntProp(client, Prop_Data, "m_nWaterLevel") != 0);
SetKthBit(flags, 22, isTeleportTick[client]);
SetKthBit(flags, 23, Movement_GetTakeoffTick(client) == tickCount);
SetKthBit(flags, 24, GOKZ_GetHitPerf(client));
SetKthBit(flags, 25, IsCurrentWeaponSecondary(client));
return flags;
}
// Function to set the bitNum bit in integer to value
static void SetKthBit(int &number, int offset, bool value)
{
int intValue = value ? 1 : 0;
number |= intValue << offset;
}
static bool IsBitSet(int number, int checkBit)
{
return (number & checkBit) ? true : false;
}
static int GetPlayerWeaponSlotDefIndex(int client, int slot)
{
int ent = GetPlayerWeaponSlot(client, slot);
// Nothing equipped in the slot
if (ent == -1)
{
return -1;
}
return GetEntProp(ent, Prop_Send, "m_iItemDefinitionIndex");
}
static bool IsCurrentWeaponSecondary(int client)
{
int activeWeaponEnt = GetEntPropEnt(client, Prop_Send, "m_hActiveWeapon");
int secondaryEnt = GetPlayerWeaponSlot(client, CS_SLOT_SECONDARY);
return activeWeaponEnt == secondaryEnt;
}
static void CreateReplaysDirectory(const char[] map)
{
char path[PLATFORM_MAX_PATH];
// Create parent replay directory
BuildPath(Path_SM, path, sizeof(path), RP_DIRECTORY);
if (!DirExists(path))
{
CreateDirectory(path, 511);
}
// Create maps parent replay directory
BuildPath(Path_SM, path, sizeof(path), "%s", RP_DIRECTORY_RUNS);
if (!DirExists(path))
{
CreateDirectory(path, 511);
}
// Create maps replay directory
BuildPath(Path_SM, path, sizeof(path), "%s/%s", RP_DIRECTORY_RUNS, map);
if (!DirExists(path))
{
CreateDirectory(path, 511);
}
// Create maps parent replay directory
BuildPath(Path_SM, path, sizeof(path), "%s", RP_DIRECTORY_RUNS_TEMP);
if (!DirExists(path))
{
CreateDirectory(path, 511);
}
// Create maps replay directory
BuildPath(Path_SM, path, sizeof(path), "%s/%s", RP_DIRECTORY_RUNS_TEMP, map);
if (!DirExists(path))
{
CreateDirectory(path, 511);
}
// Create cheaters replay directory
BuildPath(Path_SM, path, sizeof(path), "%s", RP_DIRECTORY_CHEATERS);
if (!DirExists(path))
{
CreateDirectory(path, 511);
}
// Create jumps parent replay directory
BuildPath(Path_SM, path, sizeof(path), "%s", RP_DIRECTORY_JUMPS);
if (!DirExists(path))
{
CreateDirectory(path, 511);
}
}
public void MYAWCheck(QueryCookie cookie, int client, ConVarQueryResult result, const char[] cvarName, const char[] cvarValue, any value)
{
if (IsValidClient(client) && !IsFakeClient(client))
{
playerMYaw[client] = StringToFloat(cvarValue);
}
}
public void SensitivityCheck(QueryCookie cookie, int client, ConVarQueryResult result, const char[] cvarName, const char[] cvarValue, any value)
{
if (IsValidClient(client) && !IsFakeClient(client))
{
playerSensitivity[client] = StringToFloat(cvarValue);
}
}
static int RecordingIndexAdd(int client, int offset)
{
int index = recordingIndex[client] + offset;
if (index < 0)
{
index += recordedRecentData[client].Length;
}
return index % recordedRecentData[client].Length;
}
static void RemoveFromRunningTimers(int client, Handle timerToRemove)
{
int index = runningJumpstatTimers[client].FindValue(timerToRemove);
if (index != -1)
{
runningJumpstatTimers[client].Erase(index);
}
}
|