1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/cssdm_ffa.cpp Sun Jun 24 21:58:09 2007 +0000
1.3 @@ -0,0 +1,212 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * ===============================================================
1.7 + * CS:S DM, Copyright (C) 2004-2007 AlliedModders LLC.
1.8 + * By David "BAILOPAN" Anderson
1.9 + * All rights reserved.
1.10 + * ===============================================================
1.11 + *
1.12 + * This program is free software; you can redistribute it and/or modify
1.13 + * it under the terms of the GNU General Public License as published by
1.14 + * the Free Software Foundation; either version 2 of the License, or (at
1.15 + * your option) any later version.
1.16 + *
1.17 + * This program is distributed in the hope that it will be useful, but
1.18 + * WITHOUT ANY WARRANTY; without even the implied warranty of
1.19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.20 + * General Public License for more details.
1.21 + *
1.22 + * You should have received a copy of the GNU General Public License
1.23 + * along with this program; see the file COPYING; if not, write to the
1.24 + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
1.25 + * MA 02110-1301 USA
1.26 + *
1.27 + * Version: $Id$
1.28 + */
1.29 +
1.30 +#include "cssdm_headers.h"
1.31 +#include "cssdm_utils.h"
1.32 +#include "sm_platform.h"
1.33 +#include "cssdm_includesdk.h"
1.34 +
1.35 +SH_DECL_MANUALHOOK2(CGameRules_IPointsForKill, 62+EXTRA_VTBL_OFFSET, 0, 0, int, CBasePlayer *, CBasePlayer *);
1.36 +
1.37 +#if defined PLATFORM_WINDOWS
1.38 +#define PLATFORM_NAME "Windows"
1.39 +#elif defined PLATFORM_LINUX
1.40 +#define PLATFORM_NAME "Linux"
1.41 +#endif
1.42 +
1.43 +bool g_FFA_Patched = false;
1.44 +bool g_FFA_Prepared = false;
1.45 +
1.46 +void **g_gamerules_addr = NULL;
1.47 +
1.48 +/* Lagcomp */
1.49 +int g_lagcomp_offset = 0;
1.50 +void *g_lagcomp_addr = NULL;
1.51 +dmpatch_t g_lagcomp_patch;
1.52 +dmpatch_t g_lagcomp_restore;
1.53 +
1.54 +/* Takedamage */
1.55 +int g_takedmg_offset[5] = {0};
1.56 +void *g_takedmg_addr = NULL;
1.57 +dmpatch_t g_takedmg_patch[2];
1.58 +dmpatch_t g_takedmg_restore[2];
1.59 +
1.60 +int OnIPointsForKill(CBasePlayer *pl1, CBasePlayer *pl2)
1.61 +{
1.62 + /* If we're hooked, FFA is always on. */
1.63 + RETURN_META_VALUE(MRES_SUPERCEDE, 1);
1.64 +}
1.65 +
1.66 +bool DM_FFA_LoadPatch(const char *name, dmpatch_t *patch, char *error, size_t maxlength)
1.67 +{
1.68 + char fullname[255];
1.69 + snprintf(fullname, sizeof(fullname), "%s_%s", name, PLATFORM_NAME);
1.70 +
1.71 + const char *str = g_pDmConf->GetKeyValue(fullname);
1.72 + if (!str)
1.73 + {
1.74 + snprintf(error, maxlength, "Could not find signature value for \"%s\"", fullname);
1.75 + return false;
1.76 + }
1.77 +
1.78 + patch->bytes = DM_StringToBytes(str, patch->patch, sizeof(patch->patch));
1.79 + if (!patch->bytes)
1.80 + {
1.81 + snprintf(error, maxlength, "Invalid signature detected for \"%s\"", fullname);
1.82 + return false;
1.83 + }
1.84 +
1.85 + return true;
1.86 +}
1.87 +
1.88 +bool DM_Prepare_FFA(char *error, size_t maxlength)
1.89 +{
1.90 + void *gamerules = NULL;
1.91 + if (!g_pDmConf->GetMemSig("OnTakeDamage", &g_takedmg_addr) || !g_takedmg_addr)
1.92 + {
1.93 + snprintf(error, maxlength, "Could not find \"OnTakeDamage\" signature!");
1.94 + return false;
1.95 + }
1.96 + if (!g_pDmConf->GetMemSig("WantsLagComp", &g_lagcomp_addr) || !g_lagcomp_addr)
1.97 + {
1.98 + snprintf(error, maxlength, "Could not find \"WantsLagComp\" signature!");
1.99 + return false;
1.100 + }
1.101 + if (!g_pDmConf->GetMemSig("CGameRules", &gamerules) || !gamerules)
1.102 + {
1.103 + snprintf(error, maxlength, "Could not find \"CGameRules\" signature!");
1.104 + return false;
1.105 + }
1.106 +
1.107 + if (!g_pDmConf->GetOffset("LagCompPatch", &g_lagcomp_offset)
1.108 + || !g_lagcomp_offset)
1.109 + {
1.110 + snprintf(error, maxlength, "Could not find LagCompPatch offset");
1.111 + return false;
1.112 + }
1.113 + if (!DM_FFA_LoadPatch("LagCompPatch", &g_lagcomp_patch, error, maxlength))
1.114 + {
1.115 + return false;
1.116 + }
1.117 +
1.118 + if (!g_pDmConf->GetOffset("TakeDmgPatch1", &g_takedmg_offset[0])
1.119 + || !g_takedmg_offset[0])
1.120 + {
1.121 + snprintf(error, maxlength, "Could not find TakeDmgPatch1 offset");
1.122 + return false;
1.123 + }
1.124 + if (!DM_FFA_LoadPatch("TakeDmgPatch1", &g_takedmg_patch[0], error, maxlength))
1.125 + {
1.126 + return false;
1.127 + }
1.128 +
1.129 +#if defined PLATFORM_LINUX
1.130 + if (!g_pDmConf->GetOffset("TakeDmgPatch2", &g_takedmg_offset[1])
1.131 + || !g_takedmg_offset[1])
1.132 + {
1.133 + snprintf(error, maxlength, "Could not find TakeDmgPatch1 offset");
1.134 + return false;
1.135 + }
1.136 + if (!DM_FFA_LoadPatch("TakeDmgPatch2", &g_takedmg_patch[1], error, maxlength))
1.137 + {
1.138 + return false;
1.139 + }
1.140 +#endif
1.141 +
1.142 + /* Load the GameRules pointer */
1.143 + int offset;
1.144 + if (!g_pDmConf->GetOffset("g_pGameRules", &offset)
1.145 + || !offset)
1.146 + {
1.147 + snprintf(error, maxlength, "Could not find g_pGameRules offset");
1.148 + return false;
1.149 + }
1.150 + g_gamerules_addr = (void **)*(void **)((unsigned char *)gamerules + offset);
1.151 +
1.152 + /* Get the "IPointsForKill" offset */
1.153 + if (!g_pDmConf->GetOffset("IPointsForKill", &offset)
1.154 + || !offset)
1.155 + {
1.156 + snprintf(error, maxlength, "Could not find IPointsForKills offset");
1.157 + return false;
1.158 + }
1.159 + SH_MANUALHOOK_RECONFIGURE(CGameRules_IPointsForKill, offset, 0, 0);
1.160 +
1.161 + g_FFA_Prepared = true;
1.162 +
1.163 + return true;
1.164 +}
1.165 +
1.166 +bool DM_Patch_FFA()
1.167 +{
1.168 + if (g_FFA_Patched || !g_FFA_Prepared || *g_gamerules_addr == NULL)
1.169 + {
1.170 + return false;
1.171 + }
1.172 +
1.173 + DM_ApplyPatch(g_lagcomp_addr, g_lagcomp_offset, &g_lagcomp_patch, &g_lagcomp_restore);
1.174 + DM_ApplyPatch(g_takedmg_addr, g_takedmg_offset[0], &g_takedmg_patch[0], &g_takedmg_restore[0]);
1.175 +#if defined PLATFORM_LINUX
1.176 + DM_ApplyPatch(g_takedmg_addr, g_takedmg_offset[1], &g_takedmg_patch[1], &g_takedmg_restore[1]);
1.177 +#endif
1.178 +
1.179 + SH_ADD_MANUALHOOK_STATICFUNC(CGameRules_IPointsForKill, *g_gamerules_addr, OnIPointsForKill, false);
1.180 +
1.181 + g_FFA_Patched = true;
1.182 +
1.183 + return true;
1.184 +}
1.185 +
1.186 +bool DM_Unpatch_FFA()
1.187 +{
1.188 + if (!g_FFA_Patched)
1.189 + {
1.190 + return false;
1.191 + }
1.192 +
1.193 + if (!g_IsInGlobalShutdown)
1.194 + {
1.195 + SH_REMOVE_MANUALHOOK_STATICFUNC(CGameRules_IPointsForKill, *g_gamerules_addr, OnIPointsForKill, false);
1.196 + }
1.197 +
1.198 + DM_ApplyPatch(g_lagcomp_addr, g_lagcomp_offset, &g_lagcomp_restore, NULL);
1.199 + DM_ApplyPatch(g_takedmg_addr, g_takedmg_offset[0], &g_takedmg_restore[0], NULL);
1.200 +#if defined PLATFORM_LINUX
1.201 + DM_ApplyPatch(g_takedmg_addr, g_takedmg_offset[1], &g_takedmg_restore[1], NULL);
1.202 +#endif
1.203 +
1.204 + return true;
1.205 +}
1.206 +
1.207 +bool DM_FFA_IsPatched()
1.208 +{
1.209 + return g_FFA_Patched;
1.210 +}
1.211 +
1.212 +bool DM_FFA_IsPrepared()
1.213 +{
1.214 + return g_FFA_Prepared;
1.215 +}