diff -r 000000000000 -r 67dcd379a2d5 cssdm_ffa.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cssdm_ffa.cpp Sun Jun 24 21:58:09 2007 +0000 @@ -0,0 +1,212 @@ +/** + * vim: set ts=4 : + * =============================================================== + * CS:S DM, Copyright (C) 2004-2007 AlliedModders LLC. + * By David "BAILOPAN" Anderson + * All rights reserved. + * =============================================================== + * + * 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; see the file COPYING; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, + * MA 02110-1301 USA + * + * Version: $Id$ + */ + +#include "cssdm_headers.h" +#include "cssdm_utils.h" +#include "sm_platform.h" +#include "cssdm_includesdk.h" + +SH_DECL_MANUALHOOK2(CGameRules_IPointsForKill, 62+EXTRA_VTBL_OFFSET, 0, 0, int, CBasePlayer *, CBasePlayer *); + +#if defined PLATFORM_WINDOWS +#define PLATFORM_NAME "Windows" +#elif defined PLATFORM_LINUX +#define PLATFORM_NAME "Linux" +#endif + +bool g_FFA_Patched = false; +bool g_FFA_Prepared = false; + +void **g_gamerules_addr = NULL; + +/* Lagcomp */ +int g_lagcomp_offset = 0; +void *g_lagcomp_addr = NULL; +dmpatch_t g_lagcomp_patch; +dmpatch_t g_lagcomp_restore; + +/* Takedamage */ +int g_takedmg_offset[5] = {0}; +void *g_takedmg_addr = NULL; +dmpatch_t g_takedmg_patch[2]; +dmpatch_t g_takedmg_restore[2]; + +int OnIPointsForKill(CBasePlayer *pl1, CBasePlayer *pl2) +{ + /* If we're hooked, FFA is always on. */ + RETURN_META_VALUE(MRES_SUPERCEDE, 1); +} + +bool DM_FFA_LoadPatch(const char *name, dmpatch_t *patch, char *error, size_t maxlength) +{ + char fullname[255]; + snprintf(fullname, sizeof(fullname), "%s_%s", name, PLATFORM_NAME); + + const char *str = g_pDmConf->GetKeyValue(fullname); + if (!str) + { + snprintf(error, maxlength, "Could not find signature value for \"%s\"", fullname); + return false; + } + + patch->bytes = DM_StringToBytes(str, patch->patch, sizeof(patch->patch)); + if (!patch->bytes) + { + snprintf(error, maxlength, "Invalid signature detected for \"%s\"", fullname); + return false; + } + + return true; +} + +bool DM_Prepare_FFA(char *error, size_t maxlength) +{ + void *gamerules = NULL; + if (!g_pDmConf->GetMemSig("OnTakeDamage", &g_takedmg_addr) || !g_takedmg_addr) + { + snprintf(error, maxlength, "Could not find \"OnTakeDamage\" signature!"); + return false; + } + if (!g_pDmConf->GetMemSig("WantsLagComp", &g_lagcomp_addr) || !g_lagcomp_addr) + { + snprintf(error, maxlength, "Could not find \"WantsLagComp\" signature!"); + return false; + } + if (!g_pDmConf->GetMemSig("CGameRules", &gamerules) || !gamerules) + { + snprintf(error, maxlength, "Could not find \"CGameRules\" signature!"); + return false; + } + + if (!g_pDmConf->GetOffset("LagCompPatch", &g_lagcomp_offset) + || !g_lagcomp_offset) + { + snprintf(error, maxlength, "Could not find LagCompPatch offset"); + return false; + } + if (!DM_FFA_LoadPatch("LagCompPatch", &g_lagcomp_patch, error, maxlength)) + { + return false; + } + + if (!g_pDmConf->GetOffset("TakeDmgPatch1", &g_takedmg_offset[0]) + || !g_takedmg_offset[0]) + { + snprintf(error, maxlength, "Could not find TakeDmgPatch1 offset"); + return false; + } + if (!DM_FFA_LoadPatch("TakeDmgPatch1", &g_takedmg_patch[0], error, maxlength)) + { + return false; + } + +#if defined PLATFORM_LINUX + if (!g_pDmConf->GetOffset("TakeDmgPatch2", &g_takedmg_offset[1]) + || !g_takedmg_offset[1]) + { + snprintf(error, maxlength, "Could not find TakeDmgPatch1 offset"); + return false; + } + if (!DM_FFA_LoadPatch("TakeDmgPatch2", &g_takedmg_patch[1], error, maxlength)) + { + return false; + } +#endif + + /* Load the GameRules pointer */ + int offset; + if (!g_pDmConf->GetOffset("g_pGameRules", &offset) + || !offset) + { + snprintf(error, maxlength, "Could not find g_pGameRules offset"); + return false; + } + g_gamerules_addr = (void **)*(void **)((unsigned char *)gamerules + offset); + + /* Get the "IPointsForKill" offset */ + if (!g_pDmConf->GetOffset("IPointsForKill", &offset) + || !offset) + { + snprintf(error, maxlength, "Could not find IPointsForKills offset"); + return false; + } + SH_MANUALHOOK_RECONFIGURE(CGameRules_IPointsForKill, offset, 0, 0); + + g_FFA_Prepared = true; + + return true; +} + +bool DM_Patch_FFA() +{ + if (g_FFA_Patched || !g_FFA_Prepared || *g_gamerules_addr == NULL) + { + return false; + } + + DM_ApplyPatch(g_lagcomp_addr, g_lagcomp_offset, &g_lagcomp_patch, &g_lagcomp_restore); + DM_ApplyPatch(g_takedmg_addr, g_takedmg_offset[0], &g_takedmg_patch[0], &g_takedmg_restore[0]); +#if defined PLATFORM_LINUX + DM_ApplyPatch(g_takedmg_addr, g_takedmg_offset[1], &g_takedmg_patch[1], &g_takedmg_restore[1]); +#endif + + SH_ADD_MANUALHOOK_STATICFUNC(CGameRules_IPointsForKill, *g_gamerules_addr, OnIPointsForKill, false); + + g_FFA_Patched = true; + + return true; +} + +bool DM_Unpatch_FFA() +{ + if (!g_FFA_Patched) + { + return false; + } + + if (!g_IsInGlobalShutdown) + { + SH_REMOVE_MANUALHOOK_STATICFUNC(CGameRules_IPointsForKill, *g_gamerules_addr, OnIPointsForKill, false); + } + + DM_ApplyPatch(g_lagcomp_addr, g_lagcomp_offset, &g_lagcomp_restore, NULL); + DM_ApplyPatch(g_takedmg_addr, g_takedmg_offset[0], &g_takedmg_restore[0], NULL); +#if defined PLATFORM_LINUX + DM_ApplyPatch(g_takedmg_addr, g_takedmg_offset[1], &g_takedmg_restore[1], NULL); +#endif + + return true; +} + +bool DM_FFA_IsPatched() +{ + return g_FFA_Patched; +} + +bool DM_FFA_IsPrepared() +{ + return g_FFA_Prepared; +}