Fixed requiring dependencies at the wrong time.
3 * =============================================================================
4 * SourceMod SDKTools Extension
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #ifndef _INCLUDE_SOURCEMOD_DETOURS_H_
33 #define _INCLUDE_SOURCEMOD_DETOURS_H_
35 #if defined PLATFORM_LINUX
37 #define PAGE_SIZE 4096
38 #define ALIGN(ar) ((long)ar & ~(PAGE_SIZE-1))
39 #define PAGE_EXECUTE_READWRITE PROT_READ|PROT_WRITE|PROT_EXEC
49 unsigned char patch[20];
53 inline void ProtectMemory(void *addr, int length, int prot)
55 #if defined PLATFORM_LINUX
56 void *addr2 = (void *)ALIGN(addr);
57 mprotect(addr2, sysconf(_SC_PAGESIZE), prot);
58 #elif defined PLATFORM_WINDOWS
60 VirtualProtect(addr, length, prot, &old_prot);
64 inline void SetMemPatchable(void *address, size_t size)
66 ProtectMemory(address, (int)size, PAGE_EXECUTE_READWRITE);
69 inline void DoGatePatch(unsigned char *target, void *callback)
71 SetMemPatchable(target, 20);
73 target[0] = 0xFF; /* JMP */
74 target[1] = 0x25; /* MEM32 */
75 *(void **)(&target[2]) = callback;
78 inline void ApplyPatch(void *address, int offset, const patch_t *patch, patch_t *restore)
80 ProtectMemory(address, 20, PAGE_EXECUTE_READWRITE);
82 unsigned char *addr = (unsigned char *)address + offset;
85 for (size_t i=0; i<patch->bytes; i++)
87 restore->patch[i] = addr[i];
89 restore->bytes = patch->bytes;
92 for (size_t i=0; i<patch->bytes; i++)
94 addr[i] = patch->patch[i];
98 #endif //_INCLUDE_SOURCEMOD_DETOURS_H_