extensions/tf2/natives.cpp
author Scott Ehlert <ds@alliedmods.net>
Sun Sep 14 03:01:05 2008 -0500 (2008-09-14)
changeset 2289 44045ead61cd
parent 2097 0a56b8faac9c
parent 1947 e73a1e51833b
child 2673 87d4d9f3cbc3
permissions -rw-r--r--
Eliminated head: da55a80522cf, a remnant of sourcemod-1.1.0 branch
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Team Fortress 2 Extension
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "extension.h"
    33 #include "util.h"
    34 #include "time.h"
    35 #include "RegNatives.h"
    36 
    37 
    38 // native TF2_Burn(client, target)
    39 cell_t TF2_Burn(IPluginContext *pContext, const cell_t *params)
    40 {
    41 	static ICallWrapper *pWrapper = NULL;
    42 
    43 	// CTFPlayerShared::Burn(CTFPlayer*)
    44 	if (!pWrapper)
    45 	{
    46 		REGISTER_NATIVE_ADDR("Burn", 
    47 			PassInfo pass[2]; \
    48 			pass[0].flags = PASSFLAG_BYVAL; \
    49 			pass[0].size = sizeof(CBaseEntity *); \
    50 			pass[0].type = PassType_Basic; \
    51 			pass[1].flags = PASSFLAG_BYVAL; \
    52 			pass[1].size = sizeof(CBaseEntity *); \
    53 			pass[1].type = PassType_Basic; \
    54 			pWrapper = g_pBinTools->CreateCall(addr, CallConv_ThisCall, NULL, pass, 2))
    55 	}
    56 
    57 	CBaseEntity *pEntity;
    58 	if (!(pEntity = UTIL_GetCBaseEntity(params[1], true)))
    59 	{
    60 		return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
    61 	}
    62 
    63 	CBaseEntity *pTarget;
    64 	if (!(pTarget = UTIL_GetCBaseEntity(params[2], true)))
    65 	{
    66 		return pContext->ThrowNativeError("Client index %d is not valid", params[2]);
    67 	}
    68 
    69 	void *obj = (void *)((uint8_t *)pEntity + playerSharedOffset->actual_offset);
    70 
    71 	unsigned char vstk[sizeof(void *) + sizeof(CBaseEntity *)];
    72 	unsigned char *vptr = vstk;
    73 
    74 	*(void **)vptr = obj;
    75 	vptr += sizeof(void *);
    76 	*(CBaseEntity **)vptr = pTarget;
    77 	vptr += sizeof(CBaseEntity *);
    78 	*(CBaseEntity **)vptr = NULL;
    79 
    80 	pWrapper->Execute(vstk, NULL);
    81 
    82 	return 1;
    83 }
    84 
    85 // native TF2_Invuln(client, bool:enabled)
    86 cell_t TF2_Invuln(IPluginContext *pContext, const cell_t *params)
    87 {
    88 	return pContext->ThrowNativeError("TF2_SetPlayerInvuln is no longer available");
    89 }
    90 
    91 cell_t TF2_Disguise(IPluginContext *pContext, const cell_t *params)
    92 {
    93 	static ICallWrapper *pWrapper = NULL;
    94 
    95 	//CTFPlayerShared::Disguise(int, int)
    96 	if (!pWrapper)
    97 	{
    98 		REGISTER_NATIVE_ADDR("Disguise", 
    99 			PassInfo pass[2]; \
   100 			pass[0].flags = PASSFLAG_BYVAL; \
   101 			pass[0].size = sizeof(int); \
   102 			pass[0].type = PassType_Basic; \
   103 			pass[1].flags = PASSFLAG_BYVAL; \
   104 			pass[1].size = sizeof(int); \
   105 			pass[1].type = PassType_Basic; \
   106 			pWrapper = g_pBinTools->CreateCall(addr, CallConv_ThisCall, NULL, pass, 2))
   107 	}
   108 
   109 	CBaseEntity *pEntity;
   110 	if (!(pEntity = UTIL_GetCBaseEntity(params[1], true)))
   111 	{
   112 		return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
   113 	}
   114 
   115 	void *obj = (void *)((uint8_t *)pEntity + playerSharedOffset->actual_offset);
   116 
   117 	unsigned char vstk[sizeof(void *) + 2*sizeof(int)];
   118 	unsigned char *vptr = vstk;
   119 
   120 
   121 	*(void **)vptr = obj;
   122 	vptr += sizeof(void *);
   123 	*(int *)vptr = params[2];
   124 	vptr += sizeof(int);
   125 	*(int *)vptr = params[3];
   126 
   127 	pWrapper->Execute(vstk, NULL);
   128 
   129 	return 1;
   130 }
   131 
   132 cell_t TF2_RemoveDisguise(IPluginContext *pContext, const cell_t *params)
   133 {
   134 	static ICallWrapper *pWrapper = NULL;
   135 
   136 	//CTFPlayerShared::RemoveDisguise()
   137 	if (!pWrapper)
   138 	{
   139 		REGISTER_NATIVE_ADDR("RemoveDisguise", 
   140 				pWrapper = g_pBinTools->CreateCall(addr, CallConv_ThisCall, NULL, NULL, 0))
   141 	}
   142 
   143 	CBaseEntity *pEntity;
   144 	if (!(pEntity = UTIL_GetCBaseEntity(params[1], true)))
   145 	{
   146 		return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
   147 	}
   148 
   149 	void *obj = (void *)((uint8_t *)pEntity + playerSharedOffset->actual_offset);
   150 
   151 	unsigned char vstk[sizeof(void *)];
   152 	unsigned char *vptr = vstk;
   153 
   154 
   155 	*(void **)vptr = obj;
   156 
   157 	pWrapper->Execute(vstk, NULL);
   158 
   159 	return 1;
   160 }
   161 
   162 cell_t TF2_Respawn(IPluginContext *pContext, const cell_t *params)
   163 {
   164 	static ICallWrapper *pWrapper = NULL;
   165 
   166 	//CTFPlayer::ForceRespawn()
   167 
   168 	if (!pWrapper)
   169 	{
   170 		int offset;
   171 
   172 		if (!g_pGameConf->GetOffset("ForceRespawn", &offset))
   173 		{
   174 			return pContext->ThrowNativeError("Failed to locate function");
   175 		}
   176 
   177 		pWrapper = g_pBinTools->CreateVCall(offset,
   178 											0,
   179 											0,
   180 											NULL,
   181 											NULL,
   182 											0);
   183 
   184 		g_RegNatives.Register(pWrapper);
   185 	}
   186 
   187 	CBaseEntity *pEntity;
   188 	if (!(pEntity = UTIL_GetCBaseEntity(params[1], true)))
   189 	{
   190 		return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
   191 	}
   192 
   193 	unsigned char vstk[sizeof(void *)];
   194 	unsigned char *vptr = vstk;
   195 
   196 
   197 	*(void **)vptr = (void *)pEntity;
   198 
   199 	pWrapper->Execute(vstk, NULL);
   200 
   201 	return 1;
   202 }
   203 
   204 cell_t TF2_GetResourceEntity(IPluginContext *pContext, const cell_t *params)
   205 {
   206 	return g_resourceEntity;
   207 }
   208 
   209 cell_t TF2_GetClass(IPluginContext *pContext, const cell_t *params)
   210 {
   211 	char *str;
   212 	pContext->LocalToString(params[1], &str);
   213 
   214 	return (cell_t)ClassnameToType(str);
   215 }
   216 
   217 sp_nativeinfo_t g_TFNatives[] = 
   218 {
   219 	{"TF2_IgnitePlayer",			TF2_Burn},
   220 	{"TF2_SetPlayerInvuln",			TF2_Invuln},
   221 	{"TF2_RespawnPlayer",			TF2_Respawn},
   222 	{"TF2_DisguisePlayer",			TF2_Disguise},
   223 	{"TF2_RemovePlayerDisguise",	TF2_RemoveDisguise},
   224 	{"TF2_GetResourceEntity",		TF2_GetResourceEntity},
   225 	{"TF2_GetClass",				TF2_GetClass},
   226 	{NULL,							NULL}
   227 };