drifter01620@184: /**
drifter01620@184: * vim: set ts=4 :
drifter01620@184: * =============================================================================
drifter01620@184: * SourceMod
drifter01620@184: * Copyright (C) 2004-2010 AlliedModders LLC. All rights reserved.
drifter01620@184: * =============================================================================
drifter01620@184: *
drifter01620@184: * This program is free software; you can redistribute it and/or modify it under
drifter01620@184: * the terms of the GNU General Public License, version 3.0, as published by the
drifter01620@184: * Free Software Foundation.
drifter01620@184: *
drifter01620@184: * This program is distributed in the hope that it will be useful, but WITHOUT
drifter01620@184: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
drifter01620@184: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
drifter01620@184: * details.
drifter01620@184: *
drifter01620@184: * You should have received a copy of the GNU General Public License along with
drifter01620@184: * this program. If not, see .
drifter01620@184: *
drifter01620@184: * As a special exception, AlliedModders LLC gives you permission to link the
drifter01620@184: * code of this program (as well as its derivative works) to "Half-Life 2," the
drifter01620@184: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
drifter01620@184: * by the Valve Corporation. You must obey the GNU General Public License in
drifter01620@184: * all respects for all other code used. Additionally, AlliedModders LLC grants
drifter01620@184: * this exception to all derivative works. AlliedModders LLC defines further
drifter01620@184: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
drifter01620@184: * or .
drifter01620@184: *
drifter01620@184: * Version: $Id: detours.cpp 248 2008-08-27 00:56:22Z pred $
drifter01620@184: */
drifter01620@184:
drifter01620@184: #include "detours.h"
drifter01620@184: #include "../asm/asm.h"
drifter01620@184:
drifter01620@184: ISourcePawnEngine *CDetourManager::spengine = NULL;
drifter01620@184: IGameConfig *CDetourManager::gameconf = NULL;
drifter01620@184:
drifter01620@184: void CDetourManager::Init(ISourcePawnEngine *spengine, IGameConfig *gameconf)
drifter01620@184: {
drifter01620@184: CDetourManager::spengine = spengine;
drifter01620@184: CDetourManager::gameconf = gameconf;
drifter01620@184: }
drifter01620@184:
drifter01620@184: CDetour *CDetourManager::CreateDetour(void *callbackfunction, void **trampoline, const char *signame)
drifter01620@184: {
drifter01620@184: CDetour *detour = new CDetour(callbackfunction, trampoline, signame);
drifter01620@184: if (detour)
drifter01620@184: {
drifter01620@184: if (!detour->Init(spengine, gameconf))
drifter01620@184: {
drifter01620@184: delete detour;
drifter01620@184: return NULL;
drifter01620@184: }
drifter01620@184:
drifter01620@184: return detour;
drifter01620@184: }
drifter01620@184:
drifter01620@184: return NULL;
drifter01620@184: }
drifter01620@184:
drifter01620@184: CDetour::CDetour(void *callbackfunction, void **trampoline, const char *signame)
drifter01620@184: {
drifter01620@184: enabled = false;
drifter01620@184: detoured = false;
drifter01620@184: detour_address = NULL;
drifter01620@184: detour_trampoline = NULL;
drifter01620@184: this->signame = signame;
drifter01620@184: this->detour_callback = callbackfunction;
drifter01620@184: spengine = NULL;
drifter01620@184: gameconf = NULL;
drifter01620@184: this->trampoline = trampoline;
drifter01620@184: }
drifter01620@184:
drifter01620@184: bool CDetour::Init(ISourcePawnEngine *spengine, IGameConfig *gameconf)
drifter01620@184: {
drifter01620@184: this->spengine = spengine;
drifter01620@184: this->gameconf = gameconf;
drifter01620@184:
drifter01620@184: if (!CreateDetour())
drifter01620@184: {
drifter01620@184: enabled = false;
drifter01620@184: return enabled;
drifter01620@184: }
drifter01620@184:
drifter01620@184: enabled = true;
drifter01620@184:
drifter01620@184: return enabled;
drifter01620@184: }
drifter01620@184:
drifter01620@184: void CDetour::Destroy()
drifter01620@184: {
drifter01620@184: DeleteDetour();
drifter01620@184: delete this;
drifter01620@184: }
drifter01620@184:
drifter01620@184: bool CDetour::IsEnabled()
drifter01620@184: {
drifter01620@184: return enabled;
drifter01620@184: }
drifter01620@184:
drifter01620@184: bool CDetour::CreateDetour()
drifter01620@184: {
drifter01620@184: if (!gameconf->GetMemSig(signame, &detour_address))
drifter01620@184: {
drifter01620@184: g_pSM->LogError(myself, "Could not locate %s - Disabling detour", signame);
drifter01620@184: return false;
drifter01620@184: }
drifter01620@184:
drifter01620@184: if (!detour_address)
drifter01620@184: {
drifter01620@184: g_pSM->LogError(myself, "Sigscan for %s failed - Disabling detour to prevent crashes", signame);
drifter01620@184: return false;
drifter01620@184: }
drifter01620@184:
drifter01620@184: detour_restore.bytes = copy_bytes((unsigned char *)detour_address, NULL, OP_JMP_SIZE+1);
drifter01620@184:
drifter01620@184: /* First, save restore bits */
drifter01620@184: for (size_t i=0; iAllocatePageMemory(CodeSize);
drifter01620@184: spengine->SetReadWrite(wr.outbase);
drifter01620@184: wr.outptr = wr.outbase;
drifter01620@184: detour_trampoline = wr.outbase;
drifter01620@184: goto jit_rewind;
drifter01620@184: }
drifter01620@184:
drifter01620@184: spengine->SetReadExecute(wr.outbase);
drifter01620@184:
drifter01620@184: *trampoline = detour_trampoline;
drifter01620@184:
drifter01620@184: return true;
drifter01620@184: }
drifter01620@184:
drifter01620@184: void CDetour::DeleteDetour()
drifter01620@184: {
drifter01620@184: if (detoured)
drifter01620@184: {
drifter01620@184: DisableDetour();
drifter01620@184: }
drifter01620@184:
drifter01620@184: if (detour_trampoline)
drifter01620@184: {
drifter01620@184: /* Free the allocated trampoline memory */
drifter01620@184: spengine->FreePageMemory(detour_trampoline);
drifter01620@184: detour_trampoline = NULL;
drifter01620@184: }
drifter01620@184: }
drifter01620@184:
drifter01620@184: void CDetour::EnableDetour()
drifter01620@184: {
drifter01620@184: if (!detoured)
drifter01620@184: {
drifter01620@184: DoGatePatch((unsigned char *)detour_address, &detour_callback);
drifter01620@184: detoured = true;
drifter01620@184: }
drifter01620@184: }
drifter01620@184:
drifter01620@184: void CDetour::DisableDetour()
drifter01620@184: {
drifter01620@184: if (detoured)
drifter01620@184: {
drifter01620@184: /* Remove the patch */
drifter01620@184: ApplyPatch(detour_address, 0, &detour_restore, NULL);
drifter01620@184: detoured = false;
drifter01620@184: }
drifter01620@184: }