forked from AnAkkk/TFTrue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtournament.cpp
773 lines (644 loc) · 25.2 KB
/
tournament.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
/*
* Copyright (C) 2015 AnAkkk <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "logs.h"
#include "sourcetv.h"
#include "tournament.h"
#include "editablecommands.h"
#include "iclient.h"
#include <chrono>
CTournament g_Tournament;
int *g_sv_pure_mode = nullptr;
int *cmd_source = nullptr;
int *cmd_clientslot = nullptr;
EditableConCommand *sv_pure = nullptr;
ConCommand *status = nullptr;
ConCommand *mp_tournament_restart = nullptr;
ConCommand *pause_ = nullptr;
ConVar tftrue_tournament_config("tftrue_tournament_config", "0", FCVAR_NOTIFY,
"Available configs:\n"
"0: Disabled\n"
"1: ETF2L 6on6\n"
"2: ETF2L 9on9",
true, 0, true, 2,
&CTournament::Tournament_Config_Callback);
ConVar tftrue_unpause_delay("tftrue_unpause_delay", "2", FCVAR_NOTIFY,
"Set the delay before someone can unpause the game after it has been paused.",
true, 0, false, 0);
ConVar tftrue_log_verbose_pauses("tftrue_log_verbose_pauses", "1", FCVAR_NOTIFY,
"Log verbose pause info, in addition to simple Game_Paused and Unpaused events.",
true, 0, true, 1);
bool CTournament::Init(const CModuleScanner& EngineModule, const CModuleScanner& ServerModule)
{
gameeventmanager->AddListener(this, "teamplay_game_over", true);
gameeventmanager->AddListener(this, "tf_game_over", true);
gameeventmanager->AddListener(this, "teamplay_round_win", true);
gameeventmanager->AddListener(this, "teamplay_round_stalemate", true);
ConVarRef mp_tournament("mp_tournament");
mp_tournament_restart = g_pCVar->FindCommand("mp_tournament_restart");
sv_pure = (EditableConCommand*)g_pCVar->FindCommand("sv_pure");
status = g_pCVar->FindCommand("status");
pause_ = g_pCVar->FindCommand("pause");
ConVarRef tf_gamemode_arena ("tf_gamemode_arena");
ConVarRef tf_gamemode_cp ("tf_gamemode_cp");
ConVarRef tf_gamemode_ctf ("tf_gamemode_ctf");
ConVarRef tf_gamemode_payload ("tf_gamemode_payload");
ConVarRef tf_gamemode_mvm ("tf_gamemode_mvm");
ConVarRef sv_pausable ("sv_pausable");
if(!(mp_tournament.IsValid() && tf_gamemode_arena.IsValid() && tf_gamemode_cp.IsValid() && tf_gamemode_ctf.IsValid() && tf_gamemode_payload.IsValid() &&
tf_gamemode_mvm.IsValid() && sv_pausable.IsValid() && mp_tournament_restart && sv_pure && status && pause_))
{
Warning("[TFTrue] Can't find tournament cvars\n");
return false;
}
ConCommand *mp_tournament_restart = g_pCVar->FindCommand("mp_tournament_restart");
if(mp_tournament_restart)
m_DispatchTournamentRestartRoute.RouteVirtualFunction(mp_tournament_restart, &ConCommand::Dispatch, &CTournament::Tournament_Restart_Callback, false);
EditableConCommand *sv_pure = (EditableConCommand*)g_pCVar->FindCommand("sv_pure");
if(sv_pure)
m_DispatchPureRoute.RouteVirtualFunction(sv_pure, &ConCommand::Dispatch, &CTournament::Pure_Callback, false);
EditableConCommand *status = (EditableConCommand*)g_pCVar->FindCommand("status");
if(status)
m_DispatchStatusRoute.RouteVirtualFunction(status, &ConCommand::Dispatch, &CTournament::Status_Callback, false);
ConCommand *pause = g_pCVar->FindCommand("pause");
if(pause)
m_DispatchPauseRoute.RouteVirtualFunction(pause, &ConCommand::Dispatch, &CTournament::Pause_Callback, false);
char* os;
os = (char*)"Linux";
#ifdef _LINUX
os = (char*)"Linux";
void *pStartCompetitiveMatch = ServerModule.FindSymbol(
"_ZN12CTFGameRules21StartCompetitiveMatchEv");
void *CanPlayerChooseClass = ServerModule.FindSymbol(
"_ZN12CTFGameRules20CanPlayerChooseClassEP11CBasePlayeri");
cmd_source = (int*)EngineModule.FindSymbol(
"cmd_source");
cmd_clientslot = (int*)EngineModule.FindSymbol(
"cmd_clientslot");
// _Z13GetSvPureModev -> _ZL14g_sv_pure_mode
g_sv_pure_mode = (int*)EngineModule.FindSymbol(
"_ZL14g_sv_pure_mode");
void *CTFGameRules_GetClassLimit = ServerModule.FindSymbol("_ZN12CTFGameRules13GetClassLimitEi");
PatchAddress((void*)CTFGameRules_GetClassLimit, 0x18, 1, (unsigned char*)"\xEB");
#else
os = (char*)"Windows";
void *pStartCompetitiveMatch = ServerModule.FindSignature(
"\x56\x8B\xF1\x6A\x00\xC7\x86", "xxxxxxx");
void *CanPlayerChooseClass = ServerModule.FindSignature((unsigned char*)
"\x55\x8B\xEC\x83\xEC\x08\xFF\x75\x0C\xE8\x00","xxxxxxxxxx?");
// _ZL6statusRK8CCommand on linux (status concommand)
// uniq string "(secure mode enabled, disconnected from Steam3)"
// offset 0x09 + 2 (operator)
// dword_1060AC60
// 83 3D 60 AC 60 10
cmd_source = *(int**)((unsigned char*)
status->m_fnCommandCallback + 0xB);
// offset 0x41 + 2 (operator)
// dword_103ACC54
// 8B 0D 54 CC 3A 10
cmd_clientslot = *(int**)((unsigned char*)
status->m_fnCommandCallback + 0x43);
// SV_Pure_f / _ZL9SV_Pure_fRK8CCommand on linux (sv_pure concommand)
// uniq string "sv_pure set to %d"
// immediately after is our g_sv_pure_mode offset
// offset 0x56 + 2 (operator)
// dword_105D8A2C
// 89 3D 2C 8A 5D 10
g_sv_pure_mode = *(int**)((unsigned char*)
sv_pure->m_fnCommandCallback + 0x58);
// CanPlayerChooseClass calls another function that calls IsInTournamentMode, so we need the address of that other function
unsigned long CanPlayerChooseClass_TournamentCall = (unsigned long)((unsigned char*)CanPlayerChooseClass + 0xA);
unsigned long CanPlayerChooseClass_TournamentOffset = *(unsigned long*)(CanPlayerChooseClass_TournamentCall);
unsigned long CanPlayerChooseClass_Tournament = CanPlayerChooseClass_TournamentOffset + CanPlayerChooseClass_TournamentCall + 4;
PatchAddress((void*)CanPlayerChooseClass_Tournament, 0xD, 1, (unsigned char*)"\xEB");
#endif
if(pStartCompetitiveMatch)
{
m_StartCompetitiveMatchRoute.RouteFunction(pStartCompetitiveMatch, (void*)&CTournament::StartCompetitiveMatch);
}
else
{
Warning("[TFTrue] Couldn't get sig for pStartCompetitiveMatch! OS: %s\n", os);
}
if (!CanPlayerChooseClass)
{
Warning("[TFTrue] Couldn't get sig for CanPlayerChooseClass! OS: %s\n", os);
return false;
}
if (!cmd_source)
{
Warning("[TFTrue] Couldn't get sig for cmd_source! OS: %s\n", os);
return false;
}
if (!cmd_clientslot)
{
Warning("[TFTrue] Couldn't get sig for cmd_clientslot! OS: %s\n", os);
return false;
}
if (!g_sv_pure_mode)
{
Warning("[TFTrue] Couldn't get sig for g_sv_pure_mode! OS: %s\n", os);
return false;
}
((ConVar*)mp_tournament.GetLinkedConVar())->InstallChangeCallback(&CTournament::Tournament_Callback);
((ConVar*)tf_gamemode_arena.GetLinkedConVar())->InstallChangeCallback(&CTournament::GameMode_Callback);
((ConVar*)tf_gamemode_cp.GetLinkedConVar())->InstallChangeCallback(&CTournament::GameMode_Callback);
((ConVar*)tf_gamemode_ctf.GetLinkedConVar())->InstallChangeCallback(&CTournament::GameMode_Callback);
((ConVar*)tf_gamemode_payload.GetLinkedConVar())->InstallChangeCallback(&CTournament::GameMode_Callback);
((ConVar*)tf_gamemode_mvm.GetLinkedConVar())->InstallChangeCallback(&CTournament::GameMode_Callback);
((ConVar*)mp_tournament.GetLinkedConVar())->InstallChangeCallback(&CTournament::Tournament_Callback);
return true;
}
void CTournament::OnUnload()
{
gameeventmanager->RemoveListener(this);
ConVarRef mp_tournament("mp_tournament");
ConVarRef tf_gamemode_arena("tf_gamemode_arena");
ConVarRef tf_gamemode_cp("tf_gamemode_cp");
ConVarRef tf_gamemode_ctf("tf_gamemode_ctf");
ConVarRef tf_gamemode_payload("tf_gamemode_payload");
ConVarRef tf_gamemode_mvm("tf_gamemode_mvm");
((ConVar*)mp_tournament.GetLinkedConVar())->InstallChangeCallback(nullptr);
((ConVar*)tf_gamemode_arena.GetLinkedConVar())->InstallChangeCallback(nullptr);
((ConVar*)tf_gamemode_cp.GetLinkedConVar())->InstallChangeCallback(nullptr);
((ConVar*)tf_gamemode_ctf.GetLinkedConVar())->InstallChangeCallback(nullptr);
((ConVar*)tf_gamemode_payload.GetLinkedConVar())->InstallChangeCallback(nullptr);
((ConVar*)tf_gamemode_mvm.GetLinkedConVar())->InstallChangeCallback(nullptr);
}
void CTournament::OnServerActivate()
{
m_bTournamentStarted = false;
}
void CTournament::StartCompetitiveMatch(void *pGameRules EDX2)
{
typedef void (__thiscall *StartCompetitiveMatch_t)(void *pGameRules);
g_Tournament.m_StartCompetitiveMatchRoute.CallOriginalFunction<StartCompetitiveMatch_t>()(pGameRules);
static ConVarRef mp_tournament_blueteamname("mp_tournament_blueteamname");
static ConVarRef mp_tournament_redteamname("mp_tournament_redteamname");
char szBlueTeamName[10];
char szRedTeamName[10];
V_strncpy(szBlueTeamName, mp_tournament_blueteamname.GetString(), sizeof(szBlueTeamName));
V_strncpy(szRedTeamName, mp_tournament_redteamname.GetString(), sizeof(szRedTeamName));
for(int i = 0; i < sizeof(szBlueTeamName)/sizeof(char); i++)
{
if(!szBlueTeamName[i])
break;
if((szBlueTeamName[i] >= '0' && szBlueTeamName[i] <= '9')
|| (szBlueTeamName[i] >= 'A' && szBlueTeamName[i] <= 'Z')
|| (szBlueTeamName[i] >= 'a' && szBlueTeamName[i] <= 'z'))
continue;
else
szBlueTeamName[i] = '_';
}
for(int i = 0; i < sizeof(szRedTeamName)/sizeof(char); i++)
{
if(!szRedTeamName[i])
break;
if((szRedTeamName[i] >= '0' && szRedTeamName[i] <= '9')
|| (szRedTeamName[i] >= 'A' && szRedTeamName[i] <= 'Z')
|| (szRedTeamName[i] >= 'a' && szRedTeamName[i] <= 'z'))
continue;
else
szRedTeamName[i] = '_';
}
g_SourceTV.OnTournamentStarted(szBlueTeamName, szRedTeamName);
char szMsg[100];
V_snprintf(szMsg, sizeof(szMsg), "Tournament mode started\nBlue Team: %s\nRed Team: %s\n", szBlueTeamName, szRedTeamName);
engine->LogPrint(szMsg);
static ConVarRef sv_cheats("sv_cheats");
if(sv_cheats.GetBool())
AllMessage("\003[TFTrue] WARNING: Cheats are enabled !\n");
if(*g_sv_pure_mode != 2)
AllMessage("\003[TFTrue] WARNING: The server is not correctly set up: sv_pure is not enabled!\n");
if(g_Tournament.m_iConfigDownloadFailed)
AllMessage("\003[TFTrue] WARNING: The download of %d tournament config files failed! "
"The server might not be setup correctly.\n", g_Tournament.m_iConfigDownloadFailed);
g_Logs.OnTournamentStarted();
g_Tournament.m_bTournamentStarted = true;
}
void CTournament::FireGameEvent(IGameEvent *pEvent)
{
if(!strcmp(pEvent->GetName(),"teamplay_game_over") || !strcmp(pEvent->GetName(), "tf_game_over"))
{
if(m_bTournamentStarted)
{
m_bTournamentStarted = false;
g_SourceTV.OnGameOver();
g_Logs.OnGameOver();
}
}
else if(!strcmp(pEvent->GetName(), "teamplay_round_win") || !strcmp(pEvent->GetName(), "teamplay_round_stalemate"))
{
if(m_bTournamentStarted)
g_Logs.OnRoundWin();
}
}
void CTournament::FindMapType()
{
static ConVarRef tf_gamemode_arena("tf_gamemode_arena");
static ConVarRef tf_gamemode_cp("tf_gamemode_cp");
static ConVarRef tf_gamemode_ctf("tf_gamemode_ctf");
static ConVarRef tf_gamemode_payload("tf_gamemode_payload");
static ConVarRef tf_gamemode_mvm("tf_gamemode_mvm");
eMapType = MAPTYPE_UNKNOWN;
if(tf_gamemode_arena.GetBool())
eMapType = MAPTYPE_ARENA;
if(tf_gamemode_cp.GetBool())
{
CBaseEntity *pEntity = nullptr;
while((pEntity = g_pServerTools->FindEntityByClassname(pEntity, "team_control_point")) != nullptr)
{
eMapType = MAPTYPE_ATTACKDEFENSE;
if(*g_EntityProps.GetSendProp<int>(pEntity, "m_iTeamNum") != 2)
{
eMapType = MAPTYPE_CP;
break;
}
}
}
if(tf_gamemode_ctf.GetBool())
eMapType = MAPTYPE_CTF;
if(tf_gamemode_payload.GetBool())
eMapType = MAPTYPE_ATTACKDEFENSE;
if(tf_gamemode_mvm.GetBool())
eMapType = MAPTYPE_MVM;
}
void CTournament::SetTournamentMapVars()
{
switch(tftrue_tournament_config.GetInt())
{
case CONFIG_ETF2L6v6:
{
switch(eMapType)
{
case MAPTYPE_ATTACKDEFENSE:
engine->InsertServerCommand("exec etf2l_6v6_stopwatch\n");
engine->ServerExecute();
break;
case MAPTYPE_CP:
engine->InsertServerCommand("exec etf2l_6v6_5cp\n");
engine->ServerExecute();
break;
case MAPTYPE_CTF:
engine->InsertServerCommand("exec etf2l_6v6_ctf\n");
engine->ServerExecute();
break;
case MAPTYPE_ARENA:
engine->InsertServerCommand("exec etf2l_6v6_koth\n");
engine->ServerExecute();
break;
case MAPTYPE_MVM:
break;
}
break;
}
case CONFIG_ETF2L9v9:
{
switch(eMapType)
{
case MAPTYPE_ATTACKDEFENSE:
engine->InsertServerCommand("exec etf2l_9v9_stopwatch\n");
engine->ServerExecute();
break;
case MAPTYPE_CP:
engine->InsertServerCommand("exec etf2l_9v9_5cp\n");
engine->ServerExecute();
break;
case MAPTYPE_CTF:
engine->InsertServerCommand("exec etf2l_9v9_ctf\n");
engine->ServerExecute();
break;
case MAPTYPE_ARENA:
engine->InsertServerCommand("exec etf2l_9v9_koth\n");
engine->ServerExecute();
break;
case MAPTYPE_MVM:
break;
}
break;
}
}
}
void CTournament::GameMode_Callback( IConVar *var, const char *pOldValue, float flOldValue )
{
static ConVarRef mp_tournament("mp_tournament");
g_Tournament.FindMapType();
ConVar* v = (ConVar*)var;
if(v->GetBool())
{
if(mp_tournament.GetBool())
g_Tournament.SetTournamentMapVars();
}
}
void CTournament::Tournament_Callback( IConVar *var, const char *pOldValue, float flOldValue )
{
static ConVarRef tf_gamemode_mvm("tf_gamemode_mvm");
ConVar* v = (ConVar*)var;
if (v->GetBool() && !flOldValue)
{
if(*g_sv_pure_mode == 2 && !tf_gamemode_mvm.GetBool())
sv_pure->m_nFlags |= FCVAR_DEVELOPMENTONLY;
g_Tournament.SetTournamentMapVars();
}
else if(!v->GetBool() && flOldValue)
sv_pure->m_nFlags &= ~FCVAR_DEVELOPMENTONLY;
}
void CTournament::Tournament_Restart_Callback(ConCommand *pCmd, EDX const CCommand &args)
{
g_Plugin.ForwardCommand(pCmd, args);
if(g_Plugin.GetCommandIndex() == -1)
{
g_Tournament.m_bTournamentStarted = false;
g_SourceTV.StopTVRecord();
g_Logs.ResetLastLogID();
// TODO: create a new log if we're auto uploading?
}
}
void CTournament::Tournament_Config_Callback( IConVar *var, const char *pOldValue, float flOldValue )
{
static ConVarRef mp_tournament("mp_tournament");
ConVar *v = (ConVar*)var;
if(v->GetInt() == CONFIG_NONE)
return;
switch(v->GetInt())
{
case CONFIG_ETF2L6v6:
{
g_Tournament.DownloadConfig("https://etf2l.org/configs/etf2l.cfg");
g_Tournament.DownloadConfig("https://etf2l.org/configs/etf2l_custom.cfg", false);
g_Tournament.DownloadConfig("https://etf2l.org/configs/etf2l_6v6.cfg");
g_Tournament.DownloadConfig("https://etf2l.org/configs/etf2l_6v6_5cp.cfg");
g_Tournament.DownloadConfig("https://etf2l.org/configs/etf2l_6v6_ctf.cfg");
g_Tournament.DownloadConfig("https://etf2l.org/configs/etf2l_6v6_koth.cfg");
g_Tournament.DownloadConfig("https://etf2l.org/configs/etf2l_6v6_stopwatch.cfg");
g_Tournament.DownloadConfig("https://etf2l.org/configs/etf2l_whitelist_6v6.txt");
g_Tournament.DownloadConfig("https://etf2l.org/configs/etf2l_golden_cap.cfg");
break;
}
case CONFIG_ETF2L9v9:
{
g_Tournament.DownloadConfig("https://etf2l.org/configs/etf2l.cfg");
g_Tournament.DownloadConfig("https://etf2l.org/configs/etf2l_custom.cfg", false);
g_Tournament.DownloadConfig("https://etf2l.org/configs/etf2l_9v9.cfg");
g_Tournament.DownloadConfig("https://etf2l.org/configs/etf2l_9v9_5cp.cfg");
g_Tournament.DownloadConfig("https://etf2l.org/configs/etf2l_9v9_ctf.cfg");
g_Tournament.DownloadConfig("https://etf2l.org/configs/etf2l_9v9_koth.cfg");
g_Tournament.DownloadConfig("https://etf2l.org/configs/etf2l_9v9_stopwatch.cfg");
g_Tournament.DownloadConfig("https://etf2l.org/configs/etf2l_whitelist_9v9.txt");
g_Tournament.DownloadConfig("https://etf2l.org/configs/etf2l_golden_cap.cfg");
break;
}
}
if(v->GetInt() != flOldValue && mp_tournament.GetBool())
g_Tournament.SetTournamentMapVars();
}
void CTournament::Pure_Callback(ConCommand *pCmd, EDX const CCommand &args)
{
static ConVarRef mp_tournament("mp_tournament");
static ConVarRef tf_gamemode_mvm("tf_gamemode_mvm");
int iOldPureValue = *g_sv_pure_mode;
g_Plugin.ForwardCommand(pCmd, args);
int iNewPureValue = *g_sv_pure_mode;
if(iNewPureValue == 2 && iOldPureValue != 2)
{
if(mp_tournament.GetBool() && !tf_gamemode_mvm.GetBool())
sv_pure->m_nFlags |= FCVAR_DEVELOPMENTONLY;
AllMessage("\003[TFTrue] sv_pure changed to 2. Changing map...\n");
g_Plugin.ForceReloadMap(gpGlobals->curtime+3.0f);
}
else if(iNewPureValue == 1 && iOldPureValue != 1)
{
AllMessage("\003[TFTrue] sv_pure changed to 1. Changing map...\n");
g_Plugin.ForceReloadMap(gpGlobals->curtime+3.0f);
}
else if(iNewPureValue == 0 && iOldPureValue != 0)
{
AllMessage("\003[TFTrue] sv_pure changed to 0. Changing map...\n");
g_Plugin.ForceReloadMap(gpGlobals->curtime+3.0f);
}
else if(iNewPureValue == -1 && iOldPureValue != -1)
{
AllMessage("\003[TFTrue] sv_pure changed to -1. Changing map...\n");
g_Plugin.ForceReloadMap(gpGlobals->curtime+3.0f);
}
}
void CTournament::Status_Callback(ConCommand *pCmd, EDX const CCommand &args)
{
g_Plugin.ForwardCommand(pCmd, args);
static ConVarRef mp_tournament("mp_tournament");
static ConVarRef tf_gamemode_mvm("tf_gamemode_mvm");
if(cmd_source && *cmd_source == 1)
{
static ConCommand* plugin_print = g_pCVar->FindCommand("plugin_print");
plugin_print->Dispatch(CCommand());
}
else if(cmd_clientslot && *cmd_clientslot != -1 && mp_tournament.GetBool() && !tf_gamemode_mvm.GetBool())
{
edict_t* pEntity = EdictFromIndex(*cmd_clientslot+1);
if(helpers)
{
engine->ClientPrintf(pEntity, "Loaded plugins:\n");
engine->ClientPrintf(pEntity, "---------------------\n");
CUtlVector<CPlugin *> *m_Plugins = (CUtlVector<CPlugin *>*)((char*)helpers+4);
for ( int i = 0; i < m_Plugins->Count(); i++ )
{
char szPluginName[255];
V_snprintf(szPluginName, sizeof(szPluginName), "%s\n", m_Plugins->Element(i)->m_szName);
engine->ClientPrintf(pEntity, szPluginName);
}
engine->ClientPrintf(pEntity, "---------------------\n");
}
PrintTFTrueInfos(pEntity);
}
}
void CTournament::Pause_Callback(ConCommand *pCmd, EDX const CCommand &args)
{
static ConVarRef sv_pausable("sv_pausable");
if(sv_pausable.GetBool() && *cmd_source == 0)
{
// a little hacky but it works.
// client index = client slot + 1
int icl = *cmd_clientslot+1;
// let the engine handle the rest
IClient* pClient = g_pServer->GetClient(*cmd_clientslot);
edict_t* pEdict = EdictFromIndex(icl);
IPlayerInfo* pInfo = playerinfomanager->GetPlayerInfo(pEdict);
int iTeamNum = pInfo->GetTeamIndex();
char szTeamName[5];
if(iTeamNum == 2)
{
V_strncpy(szTeamName, "Red", sizeof(szTeamName));
}
else if(iTeamNum == 3)
{
V_strncpy(szTeamName, "Blue", sizeof(szTeamName));
}
// unpausing
if(g_pServer->IsPaused())
{
if(time(NULL) >= g_Tournament.m_tNextUnpauseAllowed)
{
AllMessage(icl, "\x05[TFTrue] The game was unpaused by \x03%s\x05.\n", pClient->GetClientName());
// https://github.com/AnAkkk/TFTrue/issues/17#issuecomment-674427577
char msg[128];
V_snprintf(msg, sizeof(msg), "%s", "World triggered \"Game_Unpaused\"\n");
engine->LogPrint(msg);
// verbose mode
if (tftrue_log_verbose_pauses.GetBool())
{
V_snprintf(msg, sizeof(msg),
"\"%s<%d><%s><%s>\" triggered \"matchunpause\"\n",
pInfo->GetName(),
pInfo->GetUserID(),
pInfo->GetNetworkIDString(),
szTeamName);
engine->LogPrint(msg);
auto PauseEndTime = std::chrono::high_resolution_clock::now();
double elapsed_time_ms = std::chrono::duration<double, std::milli>(PauseEndTime-g_Tournament.m_tPauseStartTime).count();
double elapsed_time_sec = elapsed_time_ms / 1000.0;
// just for you, wiet
// https://github.com/AnAkkk/TFTrue/issues/17#issue-678751185
V_snprintf(msg, sizeof(msg),
"World triggered \"Pause_Length\" (seconds \"%.2f\")\n", elapsed_time_sec);
engine->LogPrint(msg);
}
g_Plugin.ForwardCommand(pCmd, args);
}
else
{
Message(icl, "\x07%sPlease wait %ld seconds before unpausing!\n", "FF0000", g_Tournament.m_tNextUnpauseAllowed - time(NULL));
}
}
// pausing
else
{
g_Tournament.m_tNextUnpauseAllowed = time(NULL) + tftrue_unpause_delay.GetInt();
g_Tournament.m_tPauseStartTime = std::chrono::high_resolution_clock::now();
AllMessage(icl, "\x05[TFTrue] The game was paused by \x03%s\x05.\n", pClient->GetClientName());
// https://github.com/AnAkkk/TFTrue/issues/17#issuecomment-674427577
char msg[128];
V_snprintf(msg, sizeof(msg), "%s", "World triggered \"Game_Paused\"\n");
engine->LogPrint(msg);
// verbose mode
if (tftrue_log_verbose_pauses.GetBool())
{
V_snprintf(msg, sizeof(msg),
"\"%s<%d><%s><%s>\" triggered \"matchpause\"\n",
pInfo->GetName(),
pInfo->GetUserID(),
pInfo->GetNetworkIDString(),
szTeamName);
engine->LogPrint(msg);
}
g_Plugin.ForwardCommand(pCmd, args);
}
}
}
void CTournament::DownloadConfig(const char *szURL, bool bOverwrite)
{
const char *pFileName = strrchr(szURL, '/');
char szGameDir[MAX_PATH];
engine->GetGameDir(szGameDir, sizeof(szGameDir));
char szFullPath[MAX_PATH];
char szBakFile[MAX_PATH];
V_snprintf(szFullPath, sizeof(szFullPath), "%s/cfg%s", szGameDir, pFileName);
V_snprintf(szBakFile, sizeof(szBakFile), "%s/cfg%s.bak", szGameDir, pFileName);
if(!bOverwrite && filesystem->FileExists(szFullPath))
return;
std::unique_ptr<ConfigDownloadRequest> configDownloadRequest(new ConfigDownloadRequest());
configDownloadRequest->url = szURL;
SteamAPICall_t hCallServer;
configDownloadRequest->handle = steam.SteamHTTP()->CreateHTTPRequest(k_EHTTPMethodGET, szURL);
steam.SteamHTTP()->SetHTTPRequestHeaderValue(configDownloadRequest->handle, "Cache-Control", "no-cache");
steam.SteamHTTP()->SendHTTPRequest(configDownloadRequest->handle, &hCallServer);
configDownloadRequest->callResult.SetGameserverFlag();
configDownloadRequest->callResult.Set(hCallServer, this, &CTournament::DownloadConfigCallback);
m_ConfigDownloadRequests.emplace_back(std::move(configDownloadRequest));
}
void CTournament::DownloadConfigCallback(HTTPRequestCompleted_t *arg, bool bFailed)
{
std::vector<std::unique_ptr<ConfigDownloadRequest>>::iterator currConfigDownloadRequest;
for ( auto it = m_ConfigDownloadRequests.begin(); it != m_ConfigDownloadRequests.end(); it++ )
{
if(it->get()->handle == arg->m_hRequest)
{
currConfigDownloadRequest = it;
break;
}
}
if(bFailed || arg->m_eStatusCode < 200 || arg->m_eStatusCode > 299)
{
uint32 size;
steam.SteamHTTP()->GetHTTPResponseBodySize(arg->m_hRequest, &size);
if(size > 0)
{
uint8 *pResponse = new uint8[size+1];
steam.SteamHTTP()->GetHTTPResponseBodyData(arg->m_hRequest, pResponse, size);
pResponse[size] = '\0';
Msg("[TFTrue] The config hasn't been received. HTTP error %d. Response: %s\n", arg->m_eStatusCode, pResponse);
delete[] pResponse;
}
else if(!arg->m_bRequestSuccessful)
{
Msg("[TFTrue] The config hasn't been received. No response from the server.\n");
}
else
{
Msg("[TFTrue] The config hasn't been received. HTTP error %d\n", arg->m_eStatusCode);
}
m_iConfigDownloadFailed++;
}
else
{
uint32 size;
steam.SteamHTTP()->GetHTTPResponseBodySize(arg->m_hRequest, &size);
if(size > 0)
{
uint8 *pResponse = new uint8[size];
steam.SteamHTTP()->GetHTTPResponseBodyData(arg->m_hRequest, pResponse, size);
const char *pFileName = strrchr(currConfigDownloadRequest->get()->url.c_str(), '/');
char szGameDir[MAX_PATH];
engine->GetGameDir(szGameDir, sizeof(szGameDir));
char szFullPath[MAX_PATH];
char szBakFile[MAX_PATH];
V_snprintf(szFullPath, sizeof(szFullPath), "%s/cfg%s", szGameDir, pFileName);
V_snprintf(szBakFile, sizeof(szBakFile), "%s/cfg%s.bak", szGameDir, pFileName);
rename(szFullPath, szBakFile);
FILE *pConfigFile = fopen(szFullPath, "w");
if(pConfigFile)
{
fwrite(pResponse, 1, size, pConfigFile);
fclose(pConfigFile);
Msg("[TFTrue] Successfully downloaded %s\n", currConfigDownloadRequest->get()->url.c_str());
remove(szBakFile);
}
else
{
Msg("[TFTrue] Failed to open %s\n", szFullPath);
rename(szBakFile, szFullPath);
m_iConfigDownloadFailed++;
}
delete[] pResponse;
}
else
{
Msg("[TFTrue] Received empty response for %s\n", currConfigDownloadRequest->get()->url.c_str());
}
}
m_ConfigDownloadRequests.erase(currConfigDownloadRequest);
steam.SteamHTTP()->ReleaseHTTPRequest(arg->m_hRequest);
}