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