This repository has been archived by the owner on Nov 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
d2fixups.cpp
844 lines (686 loc) · 23.9 KB
/
d2fixups.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
/**
* =============================================================================
* D2Fixups
* Copyright (C) 2013 Nicholas Hastings
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 2.0 or later, 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, you are also granted permission to link the code
* of this program (as well as its derivative works) to "Dota 2," the
* "Source Engine, 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, this exception is granted to all derivative works.
*/
// Self
#include "d2fixups.h"
// SDK
#include <filesystem.h>
#include <iclient.h>
#include <icvar.h>
#include <iserver.h>
#include <tier0/platform.h>
#include <tier1/fmtstr.h>
#include <tier1/iconvar.h>
#include <vstdlib/random.h>
// Msg types have the high bit set if it's a protobuf msg (which are all that we care about).
const uint32 MSG_PROTOBUF_BIT = (1 << 31);
const uint32 k_EMsgGCServerVersionUpdated = 2522;
const uint32 k_EMsgGCServerWelcome = 4005;
const uint32 k_EMsgGCGCToRelayConnect = 7089;
const uint32 k_EMsgGCToServerConsoleCommand = 7418;
#define MSG_TAG "[D2Fixups] "
// SourceHook
#include <sh_memory.h>
// Platform specific
#if defined(LINUX)
#include <elf.h>
#elif defined(OSX)
#include <mach-o/loader.h>
#include <mach-o/nlist.h>
#endif
#if !defined(PAGE_SIZE)
#define PAGE_SIZE 4096
#endif
#define PAGE_ALIGN_UP(x) ((x + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))
#define OPCODE_JZ_1 0x74
#define OPCODE_JMP_1 0xEB
#if defined(WIN32)
static struct SrcdsPatch
{
const char *sig;
int sigsize;
int patchOffs;
uint8 patchFrom;
uint8 patchTo;
void *addr;
const char *name;
GameLibraryType ptype;
} s_Patches[] = {
// Test Client
{ "\x68\x2A\x2A\x2A\x2A\xFF\xD3\x83\x2A\x04\xE8\x2A\x2A\x2A\x2A\x80\x2A\x2A\x2A\x2A\x2A\x2A\x5B\x74\x2A\xE8",
26, // siglen
23, // offset
OPCODE_JZ_1, OPCODE_JMP_1,
NULL,
"Test Client shutdown patch",
Engine,
},
// Main Client (463) - This matches twice, but the first one is the one we want.
{ "\x8B\x2A\x8B\x2A\x08\xFF\xD0\xA3\x2A\x2A\x2A\x2A\x83\xF8\x02\x74\x2A\x85",
18, // siglen
15, // offset
OPCODE_JZ_1, OPCODE_JMP_1,
NULL,
"Shutdown patch",
Engine,
},
};
#endif
SH_DECL_HOOK1(IServerGCLobby, SteamIDAllowedToConnect, const, 0, bool, const CSteamID &);
SH_DECL_HOOK0(IVEngineServer, IsServerLocalOnly, SH_NOATTRIB, 0, bool);
SH_DECL_HOOK0(IServerGameDLL, GameInit, SH_NOATTRIB, 0, bool);
SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, 0, bool, const char *, const char *, const char *, const char *, bool, bool);
SH_DECL_HOOK4(ISteamGameCoordinator, RetrieveMessage, SH_NOATTRIB, 0, EGCResults, uint32 *, void *, uint32, uint32 *);
SH_DECL_HOOK0_void(IServerGameDLL, GameServerSteamAPIActivated, SH_NOATTRIB, 0);
SH_DECL_HOOK0(IVEngineServer, GetServerVersion, SH_NOATTRIB, 0, int);
SH_DECL_HOOK1_void(IClient, Disconnect, SH_NOATTRIB, 0, int);
static D2Fixups g_D2Fixups;
static IVEngineServer *engine = NULL;
static IServerGameDLL *gamedll = NULL;
static IFileSystem *filesystem = NULL;
static IGameEventManager2 *eventmgr = NULL;
static ISteamGameCoordinator *gamecoordinator = NULL;
static IServer *server = NULL;
ConVar dota_local_custom_allow_multiple("dota_local_custom_allow_multiple", "0", FCVAR_RELEASE, "0 - Only load selected mode's addon. 1 - Load all addons giving selected mode priority");
ConVar d2f_allow_all("d2f_allow_all", "1", FCVAR_RELEASE, "0 - Dota 2 default of disallowing players not in a lobby (all). 1 (default) - Allow all players to join");
ConVar d2f_blockgc_server_command("d2f_blockgc_server_command", "1", FCVAR_RELEASE);
static class BaseAccessor : public IConCommandBaseAccessor
{
public:
bool RegisterConCommandBase(ConCommandBase *pVar)
{
return META_REGCVAR(pVar);
}
} s_BaseAccessor;
PLUGIN_EXPOSE(D2Fixups, g_D2Fixups);
bool D2Fixups::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
{
m_bPretendToBeLocal = false;
m_iCheatGameVersionCount = 0;
m_iRetrieveMsgHook = 0;
m_iRetrieveMsgHookPost = 0;
PLUGIN_SAVEVARS();
#if defined(WIN32)
for (int i = 0; i < ARRAYSIZE(s_Patches); ++i)
{
void *addr = FindPatchAddress(s_Patches[i].sig, s_Patches[i].sigsize, s_Patches[i].ptype);
s_Patches[i].addr = addr;
if (addr)
{
s_Patches[i].addr = addr = (void *)((intp)addr + s_Patches[i].patchOffs);
SourceHook::SetMemAccess(addr, sizeof(uint8), SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
if (*reinterpret_cast<uint8 *>(addr) == s_Patches[i].patchFrom)
{
*reinterpret_cast<uint8 *>(addr) = s_Patches[i].patchTo;
continue;
}
}
ismm->Format(error, maxlen, "Failed to setup %s.", s_Patches[i].name);
META_CONPRINTF(MSG_TAG "Warning: Failed to setup %s.\n", s_Patches[i].name);
}
#endif
if (!InitGlobals(error, maxlen))
{
ismm->Format(error, maxlen, "Failed to setup globals");
return false;
}
InitHooks();
eventmgr->AddListener(this, "server_pre_shutdown", true);
if (server)
{
eventmgr->AddListener(this, "player_connect", true);
eventmgr->AddListener(this, "player_disconnect", true);
}
return true;
}
bool D2Fixups::InitGlobals(char *error, size_t maxlen)
{
// For compat with GET_V_IFACE macros
ISmmAPI *ismm = g_SMAPI;
GET_V_IFACE_CURRENT(GetServerFactory, gamedll, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
GET_V_IFACE_CURRENT(GetEngineFactory, eventmgr, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
GET_V_IFACE_CURRENT(GetFileSystemFactory, filesystem, IFileSystem, FILESYSTEM_INTERFACE_VERSION);
#if defined(WIN32)
static const char s_IServerFinder [] = "\x55\x8B\xEC\x56\xFF\x2A\x2A\xB9\x2A\x2A\x2A\x2A\xE8\x2A\x2A\x2A\x2A\x8B\xF0";
static const int s_IServerOffs = 8;
server = (IServer *) FindPatchAddress(s_IServerFinder, sizeof(s_IServerFinder) - 1, GameLibraryType::Engine);
if (server)
{
server = *(IServer **) ((intp) server + s_IServerOffs);
}
else
{
META_CONPRINT(MSG_TAG "Warning: Failed to find IServer. SourceTV kick fix will not be available.\n");
}
#elif defined(OSX)
server = (IServer *)D2Fixups::ResolveSymbol("sv", Engine);
#endif
ICvar *icvar;
GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
g_pCVar = icvar;
ConVar_Register(0, &s_BaseAccessor);
return true;
}
void D2Fixups::InitHooks()
{
int h;
h = SH_ADD_HOOK(IServerGCLobby, SteamIDAllowedToConnect, gamedll->GetServerGCLobby(), SH_MEMBER(this, &D2Fixups::Hook_SteamIDAllowedToConnect), false);
m_GlobalHooks.push_back(h);
h = SH_ADD_HOOK(IVEngineServer, IsServerLocalOnly, engine, SH_MEMBER(this, &D2Fixups::Hook_IsServerLocalOnly), false);
m_GlobalHooks.push_back(h);
h = SH_ADD_HOOK(IServerGameDLL, GameInit, gamedll, SH_MEMBER(this, &D2Fixups::Hook_GameInit), false);
m_GlobalHooks.push_back(h);
h = SH_ADD_HOOK(IServerGameDLL, LevelInit, gamedll, SH_MEMBER(this, &D2Fixups::Hook_LevelInit), false);
m_GlobalHooks.push_back(h);
h = SH_ADD_HOOK(IServerGameDLL, LevelInit, gamedll, SH_MEMBER(this, &D2Fixups::Hook_LevelInit_Post), true);
m_GlobalHooks.push_back(h);
h = SH_ADD_HOOK(IServerGameDLL, GameServerSteamAPIActivated, gamedll, SH_MEMBER(this, &D2Fixups::Hook_GameServerSteamAPIActivated), true);
m_GlobalHooks.push_back(h);
h = SH_ADD_HOOK(IVEngineServer, GetServerVersion, engine, SH_MEMBER(this, &D2Fixups::Hook_GetServerVersion), true);
m_GlobalHooks.push_back(h);
}
void D2Fixups::UnhookGC()
{
if (m_iRetrieveMsgHook != 0)
{
SH_REMOVE_HOOK_ID(m_iRetrieveMsgHook);
m_iRetrieveMsgHook = 0;
}
if (m_iRetrieveMsgHookPost != 0)
{
SH_REMOVE_HOOK_ID(m_iRetrieveMsgHookPost);
m_iRetrieveMsgHookPost = 0;
}
}
void D2Fixups::ShutdownHooks()
{
UnhookGC();
SourceHook::List<int>::iterator iter;
for (iter = m_GlobalHooks.begin(); iter != m_GlobalHooks.end(); ++iter)
{
SH_REMOVE_HOOK_ID(*iter);
}
m_GlobalHooks.clear();
}
bool D2Fixups::Unload(char *error, size_t maxlen)
{
eventmgr->RemoveListener(this);
ShutdownHooks();
#if defined(WIN32)
for (size_t i = 0; i < ARRAYSIZE(s_Patches); ++i)
{
if (!s_Patches[i].addr)
continue;
*reinterpret_cast<uint8 *>(s_Patches[i].addr) = s_Patches[i].patchFrom;
}
#endif
return true;
}
void D2Fixups::FireGameEvent(IGameEvent *pEvent)
{
if (!strcmp(pEvent->GetName(), "player_connect"))
{
Event_PlayerConnect(pEvent);
}
else if (!strcmp(pEvent->GetName(), "player_disconnect"))
{
Event_PlayerDisconnect(pEvent);
}
else if (!strcmp(pEvent->GetName(), "server_pre_shutdown"))
{
UnhookGC();
}
}
static void WfpCountChanged(IConVar *pConVar, const char *pOldValue, float flOldValue);
static ConVar dota_wfp_count("dota_wait_for_players_to_load_count", "10", FCVAR_RELEASE, "Number of players to wait for before starting game", true, 0.f, true, 32.f, WfpCountChanged);
static void WfpCountChanged(IConVar *pConVar, const char *pOldValue, float flOldValue)
{
D2Fixups::RefreshWaitForPlayersCount();
}
void D2Fixups::RefreshWaitForPlayersCount()
{
#if defined(WIN32)
// Find unique string "exec lan_server.cfg\n".
// In same block as "tutorial_m1", above it, look for global ptr being set to new value.
static const char * const sig = "\x8B\x86\x2A\x2A\x2A\x2A\x8D\x8E\x2A\x2A\x2A\x2A\xFF\x50\x08\x8B\x0D\x2A\x2A\x2A\x2A\xA3\x2A\x2A\x2A\x2A\x83";
static const int siglen = 27;
static const int offset = 22;
#elif defined(LINUX)
// Find unique string "Loading unit...%s\n".
// At the beginning of the function, the first call will be to DOTAGameManager().
// There are two matches for the signature but either will work.
static const char * const sig = "\x55\x89\xE5\x57\x56\x53\x83\xEC\x2A\x8B\x5D\x08\xE8\x2A\x2A\x2A\x2A\x89\xC6\xE8";
static const int siglen = 20;
static const int offset = 13;
// Look for "exec lan_server.cfg\n" as on Windows
// Toward the beginning of the function, DOTAGameManager() is called and the mov instruction after it should have the needed offset.
static const int playerCountOffset = 0x3E4;
#elif defined(OSX)
static const char * const symbol = "_Z15DOTAGameManagerv";
// This offset can be found the same way as on Linux except that that mov instruction with an offset is directly before a cmp instruction testing for a value of 10.
static const int playerCountOffset = 0x3E4;
#endif
static void *addr = NULL;
if (addr == NULL)
{
#if defined(WIN32)
addr = D2Fixups::FindPatchAddress(sig, siglen, Server);
if (addr)
addr = *(void **)((intp)addr + offset);
#elif defined(LINUX) || defined(OSX)
#if defined(LINUX)
addr = D2Fixups::FindPatchAddress(sig, siglen, Server);
if (addr)
{
// Get relative offset to DOTAGameManager()
int32_t funcOffset = *(int32_t *)((intp)addr + offset);
// Get real address of function:
// Address of signature + offset of relative offset + sizeof(int32_t) offset + relative offset
addr = (void *)((intp)addr + offset + 4 + funcOffset);
}
#elif defined(OSX)
addr = D2Fixups::ResolveSymbol(symbol, Server);
#endif
if (addr)
{
typedef void *(*GameManagerFn)(void);
void *objAddr = reinterpret_cast<GameManagerFn>(addr)();
if (objAddr)
addr = (void *)((intp)objAddr + playerCountOffset);
}
#endif // LINUX || OSX
}
if (addr == NULL)
{
META_CONPRINT(MSG_TAG "Failed to update waiting for players count.\n");
return;
}
*((int *)addr) = dota_wfp_count.GetInt();
}
bool D2Fixups::Hook_SteamIDAllowedToConnect(const CSteamID &steamId) const
{
if (d2f_allow_all.GetBool())
{
RETURN_META_VALUE(MRES_SUPERCEDE, true);
}
RETURN_META_VALUE(MRES_IGNORED, true);
}
bool D2Fixups::GetLibraryInfo(GameLibraryType type, LibraryInfo &libraryInfo)
{
void *libraryPtr;
switch (type)
{
case Engine:
libraryPtr = (void*)g_SMAPI->GetEngineFactory(false);
break;
case Server:
libraryPtr = (void*)g_SMAPI->GetServerFactory(false);
break;
default:
return false;
}
if (!libraryPtr)
return false;
#if defined(WIN32)
MEMORY_BASIC_INFORMATION mem;
if (!VirtualQuery(libraryPtr, &mem, sizeof(mem)))
return false;
IMAGE_DOS_HEADER *dos = reinterpret_cast<IMAGE_DOS_HEADER *>(mem.AllocationBase);
IMAGE_NT_HEADERS *pe = reinterpret_cast<IMAGE_NT_HEADERS *>((intp)dos + dos->e_lfanew);
if (pe->Signature != IMAGE_NT_SIGNATURE)
{
// GetDllMemInfo failedpe points to a bad location
return false;
}
libraryInfo.base = mem.AllocationBase;
libraryInfo.size = pe->OptionalHeader.SizeOfImage;
#elif defined(POSIX)
Dl_info info;
uint32_t segmentCount;
size_t memorySize = 0;
if (!dladdr(libraryPtr, &info) || !info.dli_fbase || !info.dli_fname)
return false;
#if defined(LINUX)
Elf32_Ehdr *file = reinterpret_cast<Elf32_Ehdr *>(info.dli_fbase);
Elf32_Phdr *phdr = reinterpret_cast<Elf32_Phdr *>((intp)file + file->e_phoff);
if (memcmp(ELFMAG, file->e_ident, SELFMAG) != 0)
return false;
segmentCount = file->e_phnum;
for (uint32_t i = 0; i < segmentCount; i++)
{
if (phdr[i].p_type == PT_LOAD && phdr[i].p_flags == (PF_X|PF_R))
memorySize += PAGE_ALIGN_UP(phdr[i].p_filesz);
}
#elif defined(OSX)
struct mach_header *file = reinterpret_cast<struct mach_header *>(info.dli_fbase);
struct segment_command *seg = reinterpret_cast<struct segment_command *>((intp)file + sizeof(mach_header));
if (file->magic != MH_MAGIC)
return false;
segmentCount = file->ncmds;
for (uint32_t i = 0; i < segmentCount; i++)
{
if (seg->cmd == LC_SEGMENT)
memorySize += seg->vmsize;
seg = reinterpret_cast<struct segment_command *>((intp)seg + seg->cmdsize);
}
#endif // LINUX || OSX
libraryInfo.base = info.dli_fbase;
libraryInfo.size = memorySize;
#endif // POSIX
return true;
}
void *D2Fixups::FindPatchAddress(const char *sig, size_t len, GameLibraryType type)
{
bool found;
char *ptr, *end;
LibraryInfo info;
if (!GetLibraryInfo(type, info))
return NULL;
ptr = reinterpret_cast<char *>(info.base);
end = ptr + info.size - len;
while (ptr < end)
{
found = true;
for (size_t i = 0; i < len; i++)
{
if (sig[i] != '\x2A' && sig[i] != ptr[i])
{
found = false;
break;
}
}
if (found)
return ptr;
ptr++;
}
return NULL;
}
void *D2Fixups::ResolveSymbol(const char *symbol, GameLibraryType type)
{
LibraryInfo info;
if (!GetLibraryInfo(type, info))
return NULL;
#if defined(WIN32)
// No hidden symbols on Windows
return GetProcAddress((HMODULE)info.base, symbol);
#elif defined(LINUX)
// Symbols are stripped on Linux, so just use dlsym() for any visible symbols
Dl_info dlinfo;
void *handle;
void *symAddr;
if (!dladdr(info.base, &dlinfo))
return NULL;
handle = dlopen(dlinfo.dli_fname, RTLD_NOLOAD);
if (!handle)
return NULL;
symAddr = dlsym(handle, symbol);
dlclose(handle);
return symAddr;
#elif defined(OSX)
uintptr_t linkEditAddr;
struct mach_header *file;
struct load_command *loadcmds;
struct segment_command *linkEditHdr;
struct symtab_command *symTabHdr;
struct nlist *symtab;
const char *strtab;
uint32_t loadcmdCount;
uint32_t symbolCount;
linkEditHdr = NULL;
symTabHdr = NULL;
file = reinterpret_cast<struct mach_header *>(info.base);
loadcmds = reinterpret_cast<struct load_command *>((intp)info.base + sizeof(mach_header));
loadcmdCount = file->ncmds;
for (uint32_t i = 0; i < loadcmdCount; i++)
{
if (loadcmds->cmd == LC_SEGMENT && !linkEditHdr)
{
struct segment_command *seg = reinterpret_cast<struct segment_command *>(loadcmds);
if (strcmp(seg->segname, "__LINKEDIT") == 0)
{
linkEditHdr = seg;
if (symTabHdr)
break;
}
}
else if (loadcmds->cmd == LC_SYMTAB)
{
symTabHdr = reinterpret_cast<struct symtab_command *>(loadcmds);
if (linkEditHdr)
break;
}
loadcmds = reinterpret_cast<struct load_command *>((intp)loadcmds + loadcmds->cmdsize);
}
if (!linkEditHdr || !symTabHdr || !symTabHdr->symoff || !symTabHdr->stroff)
return NULL;
linkEditAddr = (intp)info.base + linkEditHdr->vmaddr;
symtab = reinterpret_cast<struct nlist *>(linkEditAddr + symTabHdr->symoff - linkEditHdr->fileoff);
strtab = reinterpret_cast<const char *>(linkEditAddr + symTabHdr->stroff - linkEditHdr->fileoff);
symbolCount = symTabHdr->nsyms;
for (uint32_t i = 0; i < symbolCount; i++)
{
struct nlist &sym = symtab[i];
// Skip undefined symbols
if (sym.n_sect == NO_SECT)
continue;
// Ignore prepended underscore on symbol name comparison
if (strcmp(symbol, strtab + sym.n_un.n_strx + 1) == 0)
return reinterpret_cast<void *>((intp)info.base + sym.n_value);
}
return NULL;
#endif
}
bool D2Fixups::Hook_GameInit()
{
static ConVarRef dota_local_addon_game("dota_local_addon_game");
// If we're not running a custom game when 'map' is executed, then nothing to do.
const char *pszDesiredAddon = dota_local_addon_game.GetString();
if (!pszDesiredAddon[0])
{
RETURN_META_VALUE(MRES_IGNORED, true);
}
// This should be our 'dota' gamedir.
char modPath[MAX_PATH];
filesystem->GetSearchPath("MOD", false, modPath, sizeof(modPath));
// This is the full path to the addon we want.
char desiredAddonPath[MAX_PATH];
V_snprintf(desiredAddonPath, sizeof(desiredAddonPath), "%s%s" CORRECT_PATH_SEPARATOR_S "%s" CORRECT_PATH_SEPARATOR_S, modPath, "addons", pszDesiredAddon);
// If it doesn't exist, the rest is pointless.
if (!filesystem->IsDirectory(desiredAddonPath))
{
RETURN_META_VALUE(MRES_IGNORED, true);
}
char gameSearchPath[10 * MAX_PATH];
char addonsSearchString[MAX_PATH];
CUtlStringList gameSearchPathList;
CUtlVector<const char *> demotedSearchPathList;
// This should match any GAME path under the addons dir.
V_snprintf(addonsSearchString, sizeof(addonsSearchString), "%s%s", modPath, "addons");
filesystem->GetSearchPath("GAME", false, gameSearchPath, sizeof(gameSearchPath));
V_SplitString(gameSearchPath, ";", gameSearchPathList);
bool foundDesired = false;
FOR_EACH_VEC(gameSearchPathList, i)
{
if (V_stristr(gameSearchPathList[i], addonsSearchString))
{
if (!V_stricmp(gameSearchPathList[i], desiredAddonPath))
{
foundDesired = true;
continue;
}
// We'll re-add these to the tail.
demotedSearchPathList.AddToTail(gameSearchPathList[i]);
filesystem->RemoveSearchPath(gameSearchPathList[i], "GAME");
}
}
if (!foundDesired)
{
filesystem->AddSearchPath(desiredAddonPath, "GAME", PATH_ADD_TO_TAIL);
}
if (dota_local_custom_allow_multiple.GetBool())
{
FOR_EACH_VEC(demotedSearchPathList, i)
{
filesystem->AddSearchPath(demotedSearchPathList[i], "GAME", PATH_ADD_TO_TAIL);
}
}
demotedSearchPathList.RemoveAll();
RETURN_META_VALUE(MRES_IGNORED, true);
}
bool D2Fixups::Hook_LevelInit(const char *pMapName, const char *pMapEntities, const char *pOldLevel, const char *pLandmarkName, bool loadGame, bool background)
{
m_bPretendToBeLocal = true;
RefreshWaitForPlayersCount();
RETURN_META_VALUE(MRES_IGNORED, true);
}
bool D2Fixups::Hook_LevelInit_Post(const char *pMapName, const char *pMapEntities, const char *pOldLevel, const char *pLandmarkName, bool loadGame, bool background)
{
m_bPretendToBeLocal = false;
RETURN_META_VALUE(MRES_IGNORED, true);
}
bool D2Fixups::Hook_IsServerLocalOnly()
{
if (m_bPretendToBeLocal)
{
RETURN_META_VALUE(MRES_SUPERCEDE, true);
}
RETURN_META_VALUE(MRES_IGNORED, true);
}
void D2Fixups::Hook_GameServerSteamAPIActivated()
{
HSteamUser hSteamUser = SteamGameServer_GetHSteamUser();
HSteamPipe hSteamPipe = SteamGameServer_GetHSteamPipe();
gamecoordinator = (ISteamGameCoordinator *) g_pSteamClientGameServer->GetISteamGenericInterface(hSteamUser, hSteamPipe, STEAMGAMECOORDINATOR_INTERFACE_VERSION);
m_iRetrieveMsgHook = SH_ADD_HOOK(ISteamGameCoordinator, RetrieveMessage, gamecoordinator, SH_MEMBER(this, &D2Fixups::Hook_RetrieveMessage), false);
m_iRetrieveMsgHookPost = SH_ADD_HOOK(ISteamGameCoordinator, RetrieveMessage, gamecoordinator, SH_MEMBER(this, &D2Fixups::Hook_RetrieveMessagePost), true);
RETURN_META(MRES_IGNORED);
}
EGCResults D2Fixups::Hook_RetrieveMessage(uint32 *punMsgType, void *pubDest, uint32 cubDest, uint32 *pcubMsgSize)
{
if (!d2f_blockgc_server_command.GetBool())
{
RETURN_META_VALUE(MRES_IGNORED, k_EGCResultOK);
}
EGCResults ret = SH_CALL(gamecoordinator, &ISteamGameCoordinator::RetrieveMessage)(punMsgType, pubDest, cubDest, pcubMsgSize);
uint32 msgType = *punMsgType & ~MSG_PROTOBUF_BIT;
switch (msgType)
{
case k_EMsgGCGCToRelayConnect:
case k_EMsgGCToServerConsoleCommand:
RETURN_META_VALUE(MRES_SUPERCEDE, k_EGCResultNoMessage);
}
RETURN_META_VALUE(MRES_SUPERCEDE, ret);
}
EGCResults D2Fixups::Hook_RetrieveMessagePost(uint32 *punMsgType, void *pubDest, uint32 cubDest, uint32 *pcubMsgSize)
{
uint32 msgType = *punMsgType & ~MSG_PROTOBUF_BIT;
switch (msgType)
{
case k_EMsgGCServerWelcome:
m_iCheatGameVersionCount = 2;
break;
case k_EMsgGCServerVersionUpdated:
m_iCheatGameVersionCount = 1;
break;
}
RETURN_META_VALUE(MRES_IGNORED, k_EGCResultOK);
}
int D2Fixups::Hook_GetServerVersion()
{
// After the initial GC welcome or a GC server update notice, the game server will
// query its server version from the engine to see if it's out of date. That check has
// the handy logic of just skipping the check if either server or GC version are 0.
if (m_iCheatGameVersionCount)
{
--m_iCheatGameVersionCount;
RETURN_META_VALUE(MRES_SUPERCEDE, 0);
}
RETURN_META_VALUE(MRES_IGNORED, 0);
}
void D2Fixups::Event_PlayerConnect(IGameEvent *pEvent)
{
int slot = pEvent->GetInt("index");
auto *pClient = server->GetClient(slot);
// This is too early for IsHLTV to return true, so hook all bots and check in callback.
if (!strcmp(pEvent->GetString("networkid"), "BOT"))
{
SH_ADD_HOOK(IClient, Disconnect, pClient, SH_MEMBER(this, &D2Fixups::Hook_OnBotDisconnect), false);
}
}
void D2Fixups::Event_PlayerDisconnect(IGameEvent *pEvent)
{
int slot = pEvent->GetInt("index");
auto *pClient = server->GetClient(slot);
if (!strcmp(pEvent->GetString("networkid"), "BOT"))
{
SH_REMOVE_HOOK(IClient, Disconnect, pClient, SH_MEMBER(this, &D2Fixups::Hook_OnBotDisconnect), false);
}
}
void D2Fixups::Hook_OnBotDisconnect(int reason)
{
auto *pClient = META_IFACEPTR(IClient);
// HLTV bot disconnects at hibernation time with a reason of "".
// All other /normal/ disconnect reasons for it will be NULL or have text.
if (pClient->IsHLTV())
{
if (reason == 0)
{
RETURN_META(MRES_SUPERCEDE);
}
}
RETURN_META(MRES_IGNORED);
}
const char *D2Fixups::GetLicense()
{
return "GPLv2";
}
const char *D2Fixups::GetVersion()
{
return "2.1.4";
}
const char *D2Fixups::GetDate()
{
return __DATE__;
}
const char *D2Fixups::GetLogTag()
{
return "D2FIXUPS";
}
const char *D2Fixups::GetAuthor()
{
return "Nicholas Hastings";
}
const char *D2Fixups::GetDescription()
{
return "Provides SRCDS shutdown fix and no-lobby connect fix for Dota 2.";
}
const char *D2Fixups::GetName()
{
return "Dota 2 Fixups";
}
const char *D2Fixups::GetURL()
{
return "http://www.sourcemod.net/";
}