forked from Attano/Left4Downtown2
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplayer_slots.cpp
471 lines (373 loc) · 13.5 KB
/
player_slots.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
/**
* vim: set ts=4 :
* =============================================================================
* Left 4 Downtown SourceMod Extension
* Copyright (C) 2009-2011 Downtown1, ProdigySim; 2012-2015 Visor
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* 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, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#include "extension.h"
#include "player_slots.h"
#include "asm/asm.h"
#include "CDetour/detourhelpers.h"
#define OP_JMP_REL8 '\xEB'
#define OP_JL_REL8 '\x7C'
#define OP_JLE_REL8 '\x7E'
#define OP_JLE_REL8_SIZE 2
#define OP_JZ_REL8 '\x74'
#define OP_JZ_REL8_SIZE 2
//this is actually a near return
#define OP_RETN '\xC3'
#define OP_RETN_SIZE 1
//CMP EAX, imm32
#define OP_CMP_EAX_IMM32 '\x3D'
#define OP_CMP_EAX_IMM32_SIZE 5
#define OP_CMP_R32_RM32 '\x3B'
#define OP_CMP_R32_RM32_SIZE 6
//CMP r/m32, IMM32 ... byte opcode is 81/7
#define OP_CMP_RM32_IMM32 '\x81'
#define OP_CMP_RM32_IMM32_SIZE 6
#define OP_CMP_RM32_IMM32_MODRM_DIGIT ((char)(7 << 3))
//MOV EAX, imm32
#define OP_MOV_EAX_IMM32 '\xB8'
#define OP_MOV_EAX_IMM32_SIZE 5
#define MODRM_BYTE 1
#define MODRM_MOD_DIRECT ((char)(3 << 6))
#define MODRM_RM_EDX '\x02'
#define MODRM_RM_ESI '\x06'
#define MODRM_RM_EDI '\x07'
#if defined PLATFORM_WINDOWS
static void *humanLimitSig = NULL;
#endif
static void *lobbyConnectSig = NULL;
static int serverFullOffset = -1;
static patch_t humanLimitRestore;
static patch_t lobbyConnectRestore;
static patch_t serverFullRestore;
extern ConVar g_MaxPlayers;
int PlayerSlots::MaxSlots = -1;
int PlayerSlots::MaxClients = -1;
int PlayerSlots::MaxPlayers = -1;
#define PLAYER_SLOTS_MAX MaxSlots
void PlayerSlots::Patch()
{
#if defined PLATFORM_WINDOWS
bool firstTime = !(humanLimitSig && lobbyConnectSig);
#else
bool firstTime = !(lobbyConnectSig);
#endif
L4D_DEBUG_LOG("PlayerSlots - Patching ...");
/*
Server patch
** Needed on windows only. On Linux, this called GetMaxHumanPlayers
L4DToolZ doesn't patch this anymore?
*/
#if defined PLATFORM_WINDOWS
if(firstTime)
{
if (!g_pGameConf->GetMemSig("HumanPlayerLimitReached", &humanLimitSig) || !humanLimitSig)
{
g_pSM->LogError(myself, "PlayerSlots -- Could not find 'HumanPlayerLimitReached' signature");
return;
}
}
//code pages can't be written to by default, so ApplyPatch changes that ;)
/*
jl around the string "Human player limit reached (%d/%d)"
jl -> jmp (Windows)
jle -> nop (Linux)
we never check if the human player limit has been reached
*/
patch_t humanLimitPatch;
//#if defined PLATFORM_WINDOWS
humanLimitPatch.bytes = 1;
humanLimitPatch.patch[0] = OP_JMP_REL8;
//#else //PLATFORM_LINUX
// humanLimitPatch.bytes = OP_JLE_REL8_SIZE;
// fill_nop(humanLimitPatch.patch, humanLimitPatch.bytes);
//#endif
ApplyPatch(humanLimitSig, /*offset*/0, &humanLimitPatch, firstTime ? &humanLimitRestore : NULL);
L4D_DEBUG_LOG("PlayerSlots -- 'HumanPlayerLimitReached' jl(e) patched to jmp");
#endif
/*
Engine patch
*/
if (!g_pGameConf->GetMemSig("ConnectClientLobbyCheck", &lobbyConnectSig) || !lobbyConnectSig)
{
g_pSM->LogError(myself, "PlayerSlots -- Could not find 'ConnectClientLobbyCheck' signature");
return;
}
#if defined PLATFORM_WINDOWS
/*
jz around the string "#Valve_Reject_Server_Full"
jz -> nop (linux or windows)
do not skip to the server is full code when sv_allow_lobby_connect is 0
*/
patch_t lobbyConnectPatch;
lobbyConnectPatch.bytes = OP_JZ_REL8_SIZE;
fill_nop(lobbyConnectPatch.patch, OP_JZ_REL8_SIZE);
ApplyPatch(lobbyConnectSig, /*offset*/0, &lobbyConnectPatch, firstTime ? &lobbyConnectRestore : NULL);
unsigned char oldValue2 = lobbyConnectRestore.patch[0];
L4D_DEBUG_LOG("PlayerSlots -- 'ConnectClientLobbyCheck' jz(%x) patched to 2 nops", oldValue2);
#endif
}
static void *getMaxHumanPlayersSig = NULL;
static patch_t getMaxHumanPlayersRestore;
/*
Patch CTerrorGameRules::GetMaxHumanPlayers(void) to always return our own value
This will cause server browsers to display our own 'max players' value (Linux only)
On Windows overriding it does nothing at first..
but actually it breaks scavenge mode which is stuck in a 'scavenge versus' type mode
so it should NEVER BE PATCHED FOR WINDOWS EVER
On Linux just overriding ServerPlayerCounts isn't enough (it only fixes 'status' command)
but overwriting GetMaxHumanPlayers works great
*/
void PlayerSlots::PatchGetMaxHumanPlayers()
{
#if !defined PLATFORM_WINDOWS
bool firstTime = (getMaxHumanPlayersSig == NULL);
if(firstTime)
{
if (!g_pGameConf->GetMemSig("GetMaxHumanPlayers", &getMaxHumanPlayersSig) || !getMaxHumanPlayersSig)
{
g_pSM->LogError(myself, "PlayerSlots -- Could not find 'GetMaxHumanPlayers' signature");
return;
}
}
/*
Normally CTerrorGameRules::GetMaxHumanRules(void)
returns CTerrorGameRules::IsVersusMode() ? 8 : 4
we just want it to return our own PLAYER_SLOTS_MAX always though
*/
/*
The trick here is just to patch the function to:
mov eax, PLAYER_SLOTS_MAX
retn
Now it will always return our value instead of 4 or 8
*/
patch_t getMaxHumanPlayersPatch;
getMaxHumanPlayersPatch.bytes = OP_MOV_EAX_IMM32_SIZE + OP_RETN_SIZE;
// mov eax, PLAYER_SLOTS_MAX
getMaxHumanPlayersPatch.patch[0] = OP_MOV_EAX_IMM32;
*(uint32_t*)(getMaxHumanPlayersPatch.patch+sizeof(uint8_t)) = (uint32_t)PLAYER_SLOTS_MAX;
// retn
getMaxHumanPlayersPatch.patch[OP_MOV_EAX_IMM32_SIZE] = OP_RETN;
if(firstTime)
{
ApplyPatch(getMaxHumanPlayersSig, /*offset*/0, &getMaxHumanPlayersPatch, &getMaxHumanPlayersRestore);
}
else
{
ApplyPatch(getMaxHumanPlayersSig, /*offset*/0, &getMaxHumanPlayersPatch, /*restore*/NULL);
}
L4D_DEBUG_LOG("PlayerSlots -- 'GetMaxHumanPlayers' patched to MOV eax, %d; retn", PLAYER_SLOTS_MAX);
#endif
}
/*
static void *getTotalNumPlayersSupportedSig = NULL;
static patch_t getTotalNumPlayersSupportedRestore;
void PlayerSlots::PatchGetTotalNumPlayersSupported()
{
bool firstTime = (getTotalNumPlayersSupportedSig == NULL);
if(firstTime)
{
if (!g_pGameConf->GetMemSig("GetTotalNumPlayersSupported", &getTotalNumPlayersSupportedSig) || !getTotalNumPlayersSupportedSig)
{
g_pSM->LogError(myself, "PlayerSlots -- Could not find 'GetTotalNumPlayersSupported' signature");
return;
}
}
patch_t getTotalNumPlayersSupportedPatch;
getTotalNumPlayersSupportedPatch.bytes = OP_MOV_EAX_IMM32_SIZE + OP_RETN_SIZE;
// mov eax, PLAYER_SLOTS_MAX
getTotalNumPlayersSupportedPatch.patch[0] = OP_MOV_EAX_IMM32;
*(uint32_t*)(getTotalNumPlayersSupportedPatch.patch+sizeof(uint8_t)) = (uint32_t)PLAYER_SLOTS_MAX;
// retn
getTotalNumPlayersSupportedPatch.patch[OP_MOV_EAX_IMM32_SIZE] = OP_RETN;
if(firstTime)
{
ApplyPatch(getTotalNumPlayersSupportedSig, 0, &getTotalNumPlayersSupportedPatch, &getTotalNumPlayersSupportedRestore);
}
else
{
ApplyPatch(getTotalNumPlayersSupportedSig, 0, &getTotalNumPlayersSupportedPatch, NULL);
}
L4D_DEBUG_LOG("PlayerSlots -- 'GetTotalNumberOfPlayersSupported' patched to MOV eax, %d; retn", PLAYER_SLOTS_MAX);
}
* EOP */
void PlayerSlots::PatchSlotCheckOnly()
{
if(!lobbyConnectSig) return; //avoid a null dereference if we failed to lookup the sig
bool firstTime = (serverFullOffset == -1);
if(firstTime)
{
//////////////////////////
//valve reject server full
if (!g_pGameConf->GetOffset("ValveRejectServerFullFirst", &serverFullOffset))
{
g_pSM->LogError(myself, "PlayerSlots -- Could not find 'ValveRejectServerFullFirst' offset");
return;
}
//the offset should point to the cmp eax, [esi+180h]... first byte incorrect => wrong offset
if(*((uint8_t*)lobbyConnectSig + serverFullOffset) != OP_CMP_R32_RM32)
{
g_pSM->LogError(myself, "PlayerSlots -- Offset for 'ValveRejectServerFullFirst' is incorrect");
return;
}
}
/*
cmp around the string "#Valve_Reject_Server_Full"
cmp eax, [esi+180h] -> cmp eax, IMM32(PLAYER_SLOTS_MAX) (Windows)
cmp esi, [ebx+17Ch] -> cmp esi, IMM32(PLAYER_SLOTS_MAX) (Linux)
we effectively change how many max players we allow
*/
patch_t serverFullPatch;
#if defined PLATFORM_LINUX
serverFullPatch.bytes = OP_CMP_RM32_IMM32_SIZE;
serverFullPatch.patch[0] = OP_CMP_RM32_IMM32;
serverFullPatch.patch[MODRM_BYTE] = MODRM_MOD_DIRECT | OP_CMP_RM32_IMM32_MODRM_DIGIT | MODRM_RM_ESI; //0xFF
*(uint32_t*)(serverFullPatch.patch+MODRM_BYTE+sizeof(uint8_t)) = (uint32_t)PLAYER_SLOTS_MAX;
#else //PLATFORM_WINDOWS
serverFullPatch.bytes = OP_CMP_R32_RM32_SIZE;
//replace the first part of cmp eax, [esi+180h] with cmp eax, IMM32(player_slots_max)
serverFullPatch.patch[0] = OP_CMP_EAX_IMM32;
*(uint32_t*)(serverFullPatch.patch+sizeof(uint8_t)) = (uint32_t)PLAYER_SLOTS_MAX;
//fill in the rest of the patch size with nops
fill_nop(serverFullPatch.patch + OP_CMP_EAX_IMM32_SIZE, OP_CMP_R32_RM32_SIZE - OP_CMP_EAX_IMM32_SIZE);
#endif
if(firstTime)
{
ApplyPatch(lobbyConnectSig, serverFullOffset, &serverFullPatch, &serverFullRestore);
}
else
{
ApplyPatch(lobbyConnectSig, serverFullOffset, &serverFullPatch, /*restore*/NULL);
}
L4D_DEBUG_LOG("PlayerSlots -- 'ValveRejectServerFullFirst' patched to CMP eax, %d", PLAYER_SLOTS_MAX);
// L4D_DEBUG_LOG("PlayerSlots -- CBaseServer player limit: %d", *(unsigned int *)((int)g_pServer+ 0x17C));
}
void PlayerSlots::UnpatchGetMaxHumanPlayers()
{
if(getMaxHumanPlayersSig)
{
ApplyPatch(getMaxHumanPlayersSig, /*offset*/0, &getMaxHumanPlayersRestore, /*restore*/NULL);
L4D_DEBUG_LOG("PlayerSlots -- 'GetMaxHumanPlayers' restored");
}
}
/* Unpatch for Group Lobby - Future implementation - XBetaAlpha
void PlayerSlots::UnpatchGetTotalNumPlayersSupported()
{
if(getTotalNumPlayersSupportedSig)
{
ApplyPatch(getTotalNumPlayersSupportedSig, 0, &getTotalNumPlayersSupportedRestore, NULL);
L4D_DEBUG_LOG("PlayerSlots -- 'GetTotalNumPlayersSupported' restored");
}
}
* EOP */
void PlayerSlots::UnpatchSlotCheckOnly()
{
//cmp around the string "#Valve_Reject_Server_Full"
if(lobbyConnectSig && serverFullOffset != -1)
{
ApplyPatch(lobbyConnectSig, serverFullOffset, &serverFullRestore, /*restore*/NULL);
L4D_DEBUG_LOG("PlayerSlots -- 'ValveRejectServerFullFirst' restored");
}
}
void PlayerSlots::Unpatch()
{
L4D_DEBUG_LOG("PlayersSlots - Unpatching ...");
//jl around the string "Human player limit reached (%d/%d)"
// Windows only
#if defined PLATFORM_WINDOWS
if(humanLimitSig)
{
ApplyPatch(humanLimitSig, /*offset*/0, &humanLimitRestore, /*restore*/NULL);
L4D_DEBUG_LOG("PlayerSlots -- 'HumanPlayerLimitReached' jl(e) restored");
}
#endif
//jz around the string "#Valve_Reject_Server_Full"
if(lobbyConnectSig)
{
ApplyPatch(lobbyConnectSig, /*offset*/0, &lobbyConnectRestore, /*restore*/NULL);
L4D_DEBUG_LOG("PlayerSlots -- 'ConnectClientLobbyCheck' restored");
}
PlayerSlots::UnpatchSlotCheckOnly();
PlayerSlots::UnpatchGetMaxHumanPlayers();
//PlayerSlots::UnpatchGetTotalNumPlayersSupported();
}
void OnMaxPlayersChanged( IConVar *var, const char *pOldValue, float flOldValue )
{
L4D_DEBUG_LOG("CVAR l4d_maxplayers changed to %d...", g_MaxPlayers.GetInt());
PlayerSlots::OnMaxSlotsChanged(g_MaxPlayers.GetInt());
}
static void UpdateMaxSlots(int max_slots)
{
PlayerSlots::MaxSlots = max_slots;
g_MaxPlayers.SetValue(max_slots);
}
void PlayerSlots::OnMaxSlotsChanged(int max_slots)
{
if(MaxClients == -1)
{
L4D_DEBUG_LOG("MaxClients -1! Disallowing slots patch");
UpdateMaxSlots(-1);
return;
}
/* no change */
if(MaxSlots == max_slots)
return;
// disable
if(max_slots < 0)
{
L4D_DEBUG_LOG("Disabling slots patch");
PlayerSlots::Unpatch();
UpdateMaxSlots(-1);
return;
}
#if RESTRICT_MAX_PLAYERS_BY_COMMAND_LINE
/* dont allow slots to exceed +-maxplayers */
int max_players = PlayerSlots::MaxPlayers;
if(max_players >= 0 && max_slots > max_players)
{
L4D_DEBUG_LOG("Attempt to set slots higher than set command line value (%d max: %d)", max_slots, max_players);
UpdateMaxSlots(MaxSlots);
return;
}
#endif
// cant allow this obviously
if(max_slots > MaxClients)
{
L4D_DEBUG_LOG("Attempt to set slots higher than MaxClients (%d max: %d)", max_slots, MaxClients);
UpdateMaxSlots(MaxSlots);
return;
}
//enable
UpdateMaxSlots(max_slots);
L4D_DEBUG_LOG("Enabling slots patch");
PlayerSlots::Patch();
PlayerSlots::PatchSlotCheckOnly();
PlayerSlots::PatchGetMaxHumanPlayers();
//PlayerSlots::PatchGetTotalNumPlayersSupported();
}