cssdm_ffa.cpp
author David Anderson <dvander@alliedmods.net>
Sun Jun 24 21:58:09 2007 +0000 (2007-06-24)
changeset 0 67dcd379a2d5
child 22 dc33122d8cb1
permissions -rw-r--r--
initial import of the amazing CS:S DM 2.0.0, so far only Windows is tested
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * CS:S DM, Copyright (C) 2004-2007 AlliedModders LLC. 
     5  * By David "BAILOPAN" Anderson
     6  * All rights reserved.
     7  * ===============================================================
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or (at
    12  * your option) any later version.
    13  * 
    14  * This program is distributed in the hope that it will be useful, but
    15  * WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    17  * General Public License for more details.
    18  * 
    19  * You should have received a copy of the GNU General Public License
    20  * along with this program; see the file COPYING; if not, write to the
    21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
    22  * MA 02110-1301 USA
    23  * 
    24  * Version: $Id$
    25  */
    26 
    27 #include "cssdm_headers.h"
    28 #include "cssdm_utils.h"
    29 #include "sm_platform.h"
    30 #include "cssdm_includesdk.h"
    31 
    32 SH_DECL_MANUALHOOK2(CGameRules_IPointsForKill, 62+EXTRA_VTBL_OFFSET, 0, 0, int, CBasePlayer *, CBasePlayer *);
    33 
    34 #if defined PLATFORM_WINDOWS
    35 #define PLATFORM_NAME	"Windows"
    36 #elif defined PLATFORM_LINUX
    37 #define PLATFORM_NAME	"Linux"
    38 #endif
    39 
    40 bool g_FFA_Patched = false;
    41 bool g_FFA_Prepared = false;
    42 
    43 void **g_gamerules_addr = NULL;
    44 
    45 /* Lagcomp */
    46 int g_lagcomp_offset = 0;
    47 void *g_lagcomp_addr = NULL;
    48 dmpatch_t g_lagcomp_patch;
    49 dmpatch_t g_lagcomp_restore;
    50 
    51 /* Takedamage */
    52 int g_takedmg_offset[5] = {0};
    53 void *g_takedmg_addr = NULL;
    54 dmpatch_t g_takedmg_patch[2];
    55 dmpatch_t g_takedmg_restore[2];
    56 
    57 int OnIPointsForKill(CBasePlayer *pl1, CBasePlayer *pl2)
    58 {
    59 	/* If we're hooked, FFA is always on. */
    60 	RETURN_META_VALUE(MRES_SUPERCEDE, 1);
    61 }
    62 
    63 bool DM_FFA_LoadPatch(const char *name, dmpatch_t *patch, char *error, size_t maxlength)
    64 {
    65 	char fullname[255];
    66 	snprintf(fullname, sizeof(fullname), "%s_%s", name, PLATFORM_NAME);
    67 
    68 	const char *str = g_pDmConf->GetKeyValue(fullname);
    69 	if (!str)
    70 	{
    71 		snprintf(error, maxlength, "Could not find signature value for \"%s\"", fullname);
    72 		return false;
    73 	}
    74 
    75 	patch->bytes = DM_StringToBytes(str, patch->patch, sizeof(patch->patch));
    76 	if (!patch->bytes)
    77 	{
    78 		snprintf(error, maxlength, "Invalid signature detected for \"%s\"", fullname);
    79 		return false;
    80 	}
    81 
    82 	return true;
    83 }
    84 
    85 bool DM_Prepare_FFA(char *error, size_t maxlength)
    86 {
    87 	void *gamerules = NULL;
    88 	if (!g_pDmConf->GetMemSig("OnTakeDamage", &g_takedmg_addr) || !g_takedmg_addr)
    89 	{
    90 		snprintf(error, maxlength, "Could not find \"OnTakeDamage\" signature!");
    91 		return false;
    92 	}
    93 	if (!g_pDmConf->GetMemSig("WantsLagComp", &g_lagcomp_addr) || !g_lagcomp_addr)
    94 	{
    95 		snprintf(error, maxlength, "Could not find \"WantsLagComp\" signature!");
    96 		return false;
    97 	}
    98 	if (!g_pDmConf->GetMemSig("CGameRules", &gamerules) || !gamerules)
    99 	{
   100 		snprintf(error, maxlength, "Could not find \"CGameRules\" signature!");
   101 		return false;
   102 	}
   103 	
   104 	if (!g_pDmConf->GetOffset("LagCompPatch", &g_lagcomp_offset)
   105 		|| !g_lagcomp_offset)
   106 	{
   107 		snprintf(error, maxlength, "Could not find LagCompPatch offset");
   108 		return false;
   109 	}
   110 	if (!DM_FFA_LoadPatch("LagCompPatch", &g_lagcomp_patch, error, maxlength))
   111 	{
   112 		return false;
   113 	}
   114 
   115 	if (!g_pDmConf->GetOffset("TakeDmgPatch1", &g_takedmg_offset[0])
   116 		|| !g_takedmg_offset[0])
   117 	{
   118 		snprintf(error, maxlength, "Could not find TakeDmgPatch1 offset");
   119 		return false;
   120 	}
   121 	if (!DM_FFA_LoadPatch("TakeDmgPatch1", &g_takedmg_patch[0], error, maxlength))
   122 	{
   123 		return false;
   124 	}
   125 
   126 #if defined PLATFORM_LINUX
   127 	if (!g_pDmConf->GetOffset("TakeDmgPatch2", &g_takedmg_offset[1])
   128 		|| !g_takedmg_offset[1])
   129 	{
   130 		snprintf(error, maxlength, "Could not find TakeDmgPatch1 offset");
   131 		return false;
   132 	}
   133 	if (!DM_FFA_LoadPatch("TakeDmgPatch2", &g_takedmg_patch[1], error, maxlength))
   134 	{
   135 		return false;
   136 	}
   137 #endif
   138 
   139 	/* Load the GameRules pointer */
   140 	int offset;
   141 	if (!g_pDmConf->GetOffset("g_pGameRules", &offset)
   142 		|| !offset)
   143 	{
   144 		snprintf(error, maxlength, "Could not find g_pGameRules offset");
   145 		return false;
   146 	}
   147 	g_gamerules_addr = (void **)*(void **)((unsigned char *)gamerules + offset);
   148 
   149 	/* Get the "IPointsForKill" offset */
   150 	if (!g_pDmConf->GetOffset("IPointsForKill", &offset)
   151 		|| !offset)
   152 	{
   153 		snprintf(error, maxlength, "Could not find IPointsForKills offset");
   154 		return false;
   155 	}
   156 	SH_MANUALHOOK_RECONFIGURE(CGameRules_IPointsForKill, offset, 0, 0);
   157 
   158 	g_FFA_Prepared = true;
   159 
   160 	return true;
   161 }
   162 
   163 bool DM_Patch_FFA()
   164 {
   165 	if (g_FFA_Patched || !g_FFA_Prepared || *g_gamerules_addr == NULL)
   166 	{
   167 		return false;
   168 	}
   169 
   170 	DM_ApplyPatch(g_lagcomp_addr, g_lagcomp_offset, &g_lagcomp_patch, &g_lagcomp_restore);
   171 	DM_ApplyPatch(g_takedmg_addr, g_takedmg_offset[0], &g_takedmg_patch[0], &g_takedmg_restore[0]);
   172 #if defined PLATFORM_LINUX
   173 	DM_ApplyPatch(g_takedmg_addr, g_takedmg_offset[1], &g_takedmg_patch[1], &g_takedmg_restore[1]);
   174 #endif
   175 
   176 	SH_ADD_MANUALHOOK_STATICFUNC(CGameRules_IPointsForKill, *g_gamerules_addr, OnIPointsForKill, false);
   177 
   178 	g_FFA_Patched = true;
   179 
   180 	return true;
   181 }
   182 
   183 bool DM_Unpatch_FFA()
   184 {
   185 	if (!g_FFA_Patched)
   186 	{
   187 		return false;
   188 	}
   189 
   190 	if (!g_IsInGlobalShutdown)
   191 	{
   192 		SH_REMOVE_MANUALHOOK_STATICFUNC(CGameRules_IPointsForKill, *g_gamerules_addr, OnIPointsForKill, false);
   193 	}
   194 
   195 	DM_ApplyPatch(g_lagcomp_addr, g_lagcomp_offset, &g_lagcomp_restore, NULL);
   196 	DM_ApplyPatch(g_takedmg_addr, g_takedmg_offset[0], &g_takedmg_restore[0], NULL);
   197 #if defined PLATFORM_LINUX
   198 	DM_ApplyPatch(g_takedmg_addr, g_takedmg_offset[1], &g_takedmg_restore[1], NULL);
   199 #endif
   200 
   201 	return true;
   202 }
   203 
   204 bool DM_FFA_IsPatched()
   205 {
   206 	return g_FFA_Patched;
   207 }
   208 
   209 bool DM_FFA_IsPrepared()
   210 {
   211 	return g_FFA_Prepared;
   212 }