extensions/tf2/natives.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Mon Jun 14 09:32:50 2010 -0400 (2010-06-14)
changeset 3041 a0587dee87ae
parent 2943 1bbdb33fee53
child 3096 edd61d1e1e26
permissions -rw-r--r--
Fixed outputname null pointer crash in sdktools (bug 4422, r=fyren)
     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*, CTFWeaponBase*)
    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 *) + 2*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_AddCondition(IPluginContext *pContext, const cell_t *params)
   163 {
   164 	static ICallWrapper *pWrapper = NULL;
   165 
   166 	// CTFPlayerShared::AddCond(int, float)
   167 	if (!pWrapper)
   168 	{
   169 		REGISTER_NATIVE_ADDR("AddCondition", 
   170 			PassInfo pass[2]; \
   171 			pass[0].flags = PASSFLAG_BYVAL; \
   172 			pass[0].size = sizeof(int); \
   173 			pass[0].type = PassType_Basic; \
   174 			pass[1].flags = PASSFLAG_BYVAL; \
   175 			pass[1].size = sizeof(float); \
   176 			pass[1].type = PassType_Basic; \
   177 			pWrapper = g_pBinTools->CreateCall(addr, CallConv_ThisCall, NULL, pass, 2))
   178 	}
   179 
   180 	CBaseEntity *pEntity;
   181 	if (!(pEntity = UTIL_GetCBaseEntity(params[1], true)))
   182 	{
   183 		return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
   184 	}
   185 
   186 	void *obj = (void *)((uint8_t *)pEntity + playerSharedOffset->actual_offset);
   187 
   188 	unsigned char vstk[sizeof(void *) + sizeof(int) + sizeof(float)];
   189 	unsigned char *vptr = vstk;
   190 
   191 	*(void **)vptr = obj;
   192 	vptr += sizeof(void *);
   193 	*(int *)vptr = params[2];
   194 	vptr += sizeof(int);
   195 	*(float *)vptr = *(float *)&params[3];
   196 
   197 	pWrapper->Execute(vstk, NULL);
   198 
   199 	return 1;
   200 }
   201 
   202 cell_t TF2_RemoveCondition(IPluginContext *pContext, const cell_t *params)
   203 {
   204 	static ICallWrapper *pWrapper = NULL;
   205 
   206 	// CTFPlayerShared::RemoveCond(int)
   207 	if (!pWrapper)
   208 	{
   209 		REGISTER_NATIVE_ADDR("RemoveCondition", 
   210 			PassInfo pass[1]; \
   211 			pass[0].flags = PASSFLAG_BYVAL; \
   212 			pass[0].size = sizeof(int); \
   213 			pass[0].type = PassType_Basic; \
   214 			pWrapper = g_pBinTools->CreateCall(addr, CallConv_ThisCall, NULL, pass, 1))
   215 	}
   216 
   217 	CBaseEntity *pEntity;
   218 	if (!(pEntity = UTIL_GetCBaseEntity(params[1], true)))
   219 	{
   220 		return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
   221 	}
   222 
   223 	void *obj = (void *)((uint8_t *)pEntity + playerSharedOffset->actual_offset);
   224 
   225 	unsigned char vstk[sizeof(void *) + sizeof(int)];
   226 	unsigned char *vptr = vstk;
   227 
   228 	*(void **)vptr = obj;
   229 	vptr += sizeof(void *);
   230 	*(int *)vptr = params[2];
   231 
   232 	pWrapper->Execute(vstk, NULL);
   233 
   234 	return 1;
   235 }
   236 
   237 cell_t TF2_StunPlayer(IPluginContext *pContext, const cell_t *params)
   238 {
   239 	static ICallWrapper *pWrapper = NULL;
   240 
   241 	// CTFPlayerShared::StunPlayer(float, float, int, CTFPlayer *)
   242 	if (!pWrapper)
   243 	{
   244 		REGISTER_NATIVE_ADDR("StunPlayer", 
   245 			PassInfo pass[4]; \
   246 			pass[0].flags = PASSFLAG_BYVAL; \
   247 			pass[0].size = sizeof(float); \
   248 			pass[0].type = PassType_Basic; \
   249 			pass[1].flags = PASSFLAG_BYVAL; \
   250 			pass[1].size = sizeof(float); \
   251 			pass[1].type = PassType_Basic; \
   252 			pass[2].flags = PASSFLAG_BYVAL; \
   253 			pass[2].size = sizeof(int); \
   254 			pass[2].type = PassType_Basic; \
   255 			pass[3].flags = PASSFLAG_BYVAL; \
   256 			pass[3].size = sizeof(CBaseEntity *); \
   257 			pass[3].type = PassType_Basic; \
   258 			pWrapper = g_pBinTools->CreateCall(addr, CallConv_ThisCall, NULL, pass, 4))
   259 	}
   260 
   261 	CBaseEntity *pEntity;
   262 	if (!(pEntity = UTIL_GetCBaseEntity(params[1], true)))
   263 	{
   264 		return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
   265 	}
   266 
   267 	bool bByPlayer = (params[5] != 0);
   268 	CBaseEntity *pAttacker = NULL;
   269 	if (bByPlayer && !(pAttacker = UTIL_GetCBaseEntity(params[5], true)))
   270 	{
   271 		return pContext->ThrowNativeError("Attacker index %d is not valid", params[5]);
   272 	}
   273 
   274 	void *obj = (void *)((uint8_t *)pEntity + playerSharedOffset->actual_offset);
   275 
   276 	unsigned char vstk[sizeof(void *) + 2*sizeof(float) + sizeof(int) + sizeof(CBaseEntity *)];
   277 	unsigned char *vptr = vstk;
   278 
   279 	*(void **)vptr = obj;
   280 	vptr += sizeof(void *);
   281 	*(float *)vptr = sp_ctof(params[2]);
   282 	vptr += sizeof(float);
   283 	*(float *)vptr = sp_ctof(params[3]);
   284 	vptr += sizeof(float);
   285 	*(int *)vptr = params[4];
   286 	vptr += sizeof(int);
   287 	*(CBaseEntity **)vptr = pAttacker;
   288 
   289 	pWrapper->Execute(vstk, NULL);
   290 
   291 	return 1;
   292 }
   293 
   294 cell_t TF2_SetPowerplayEnabled(IPluginContext *pContext, const cell_t *params)
   295 {
   296 	static ICallWrapper *pWrapper = NULL;
   297 
   298 	// CTFPlayer::SetPowerPlayEnabled(bool)
   299 	if (!pWrapper)
   300 	{
   301 		REGISTER_NATIVE_ADDR("SetPowerplayEnabled", 
   302 			PassInfo pass[1]; \
   303 			pass[0].flags = PASSFLAG_BYVAL; \
   304 			pass[0].size = sizeof(bool); \
   305 			pass[0].type = PassType_Basic; \
   306 			pWrapper = g_pBinTools->CreateCall(addr, CallConv_ThisCall, NULL, pass, 1))
   307 	}
   308 
   309 	CBaseEntity *pEntity;
   310 	if (!(pEntity = UTIL_GetCBaseEntity(params[1], true)))
   311 	{
   312 		return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
   313 	}
   314 
   315 	bool bEnablePP = false;
   316 	if (params[2] != 0)
   317 	{
   318 		bEnablePP = true;
   319 	}
   320 
   321 	unsigned char vstk[sizeof(void *) + sizeof(bool)];
   322 	unsigned char *vptr = vstk;
   323 
   324 	*(void **)vptr = (void *)pEntity;
   325 	vptr += sizeof(void *);
   326 	*(bool *)vptr = bEnablePP;
   327 
   328 	pWrapper->Execute(vstk, NULL);
   329 
   330 	return 1;
   331 }
   332 
   333 cell_t TF2_Respawn(IPluginContext *pContext, const cell_t *params)
   334 {
   335 	static ICallWrapper *pWrapper = NULL;
   336 
   337 	//CTFPlayer::ForceRespawn()
   338 
   339 	if (!pWrapper)
   340 	{
   341 		int offset;
   342 
   343 		if (!g_pGameConf->GetOffset("ForceRespawn", &offset))
   344 		{
   345 			return pContext->ThrowNativeError("Failed to locate function");
   346 		}
   347 
   348 		pWrapper = g_pBinTools->CreateVCall(offset,
   349 											0,
   350 											0,
   351 											NULL,
   352 											NULL,
   353 											0);
   354 
   355 		g_RegNatives.Register(pWrapper);
   356 	}
   357 
   358 	CBaseEntity *pEntity;
   359 	if (!(pEntity = UTIL_GetCBaseEntity(params[1], true)))
   360 	{
   361 		return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
   362 	}
   363 
   364 	unsigned char vstk[sizeof(void *)];
   365 	unsigned char *vptr = vstk;
   366 
   367 
   368 	*(void **)vptr = (void *)pEntity;
   369 
   370 	pWrapper->Execute(vstk, NULL);
   371 
   372 	return 1;
   373 }
   374 
   375 cell_t TF2_Regenerate(IPluginContext *pContext, const cell_t *params)
   376 {
   377 	static ICallWrapper *pWrapper = NULL;
   378 
   379 	//CTFPlayer::Regenerate()
   380 
   381 	if (!pWrapper)
   382 	{
   383 		REGISTER_NATIVE_ADDR("Regenerate", 
   384 			pWrapper = g_pBinTools->CreateCall(addr, CallConv_ThisCall, NULL, NULL, 0));
   385 	}
   386 
   387 	CBaseEntity *pEntity;
   388 	if (!(pEntity = UTIL_GetCBaseEntity(params[1], true)))
   389 	{
   390 		return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
   391 	}
   392 	pWrapper->Execute(&pEntity, NULL);
   393 
   394 	return 1;
   395 }
   396 
   397 cell_t TF2_GetResourceEntity(IPluginContext *pContext, const cell_t *params)
   398 {
   399 	return g_resourceEntity;
   400 }
   401 
   402 cell_t TF2_GetClass(IPluginContext *pContext, const cell_t *params)
   403 {
   404 	char *str;
   405 	pContext->LocalToString(params[1], &str);
   406 
   407 	return (cell_t)ClassnameToType(str);
   408 }
   409 
   410 sp_nativeinfo_t g_TFNatives[] = 
   411 {
   412 	{"TF2_IgnitePlayer",			TF2_Burn},
   413 	{"TF2_SetPlayerInvuln",			TF2_Invuln},
   414 	{"TF2_RespawnPlayer",			TF2_Respawn},
   415 	{"TF2_DisguisePlayer",			TF2_Disguise},
   416 	{"TF2_RemovePlayerDisguise",	TF2_RemoveDisguise},
   417 	{"TF2_GetResourceEntity",		TF2_GetResourceEntity},
   418 	{"TF2_GetClass",				TF2_GetClass},
   419 	{"TF2_RegeneratePlayer",		TF2_Regenerate},
   420 	{"TF2_AddCondition",			TF2_AddCondition},
   421 	{"TF2_RemoveCondition",			TF2_RemoveCondition},
   422 	{"TF2_SetPlayerPowerPlay",		TF2_SetPowerplayEnabled},
   423 	{"TF2_StunPlayer",				TF2_StunPlayer},
   424 	{NULL,							NULL}
   425 };