extensions/sdktools/inputnatives.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Fri Jul 08 07:53:31 2011 -0400 (2011-07-08)
changeset 3348 fcae86e8cb8f
parent 2746 8d68569b9648
permissions -rw-r--r--
Backed out changeset: 9007dacc012d
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod SDKTools 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 <datamap.h>
    34 
    35 #define SIZEOF_VARIANT_T		20
    36 
    37 ICallWrapper *g_pAcceptInput = NULL;
    38 unsigned char g_Variant_t[SIZEOF_VARIANT_T] = {0};
    39 
    40 #define ENTINDEX_TO_CBASEENTITY(ref, buffer) \
    41 	buffer = gamehelpers->ReferenceToEntity(ref); \
    42 	if (!buffer) \
    43 	{ \
    44 		return pContext->ThrowNativeError("Entity %d (%d) is not a CBaseEntity", gamehelpers->ReferenceToIndex(ref), ref); \
    45 	}
    46 
    47 /* Hack to init the variant_t object for the first time */
    48 class VariantFirstTimeInit
    49 {
    50 public:
    51 	VariantFirstTimeInit()
    52 	{
    53 		*(unsigned int *)(&g_Variant_t[12]) = INVALID_EHANDLE_INDEX;
    54 	}
    55 } g_VariantFirstTimeInit;
    56 
    57 inline void _init_variant_t()
    58 {
    59 	unsigned char *vptr = g_Variant_t;
    60 
    61 	*(int *)vptr = 0;
    62 	vptr += sizeof(int)*3;
    63 	*(unsigned long *)vptr = INVALID_EHANDLE_INDEX;
    64 	vptr += sizeof(unsigned long);
    65 	*(fieldtype_t *)vptr = FIELD_VOID;
    66 }
    67 
    68 static cell_t AcceptEntityInput(IPluginContext *pContext, const cell_t *params)
    69 {
    70 	if (!g_pAcceptInput)
    71 	{
    72 		int offset;
    73 		if (!g_pGameConf->GetOffset("AcceptInput", &offset))
    74 		{
    75 			return pContext->ThrowNativeError("\"AcceptEntityInput\" not supported by this mod");
    76 		}
    77 
    78 		PassInfo pass[6];
    79 		pass[0].type = PassType_Basic;
    80 		pass[0].flags = PASSFLAG_BYVAL;
    81 		pass[0].size = sizeof(const char *);
    82 		pass[1].type = pass[2].type = PassType_Basic;
    83 		pass[1].flags = pass[2].flags = PASSFLAG_BYVAL;
    84 		pass[1].size = pass[2].size = sizeof(CBaseEntity *);
    85 		pass[3].type = PassType_Object;
    86 		pass[3].flags = PASSFLAG_BYVAL|PASSFLAG_OCTOR|PASSFLAG_ODTOR|PASSFLAG_OASSIGNOP;
    87 		pass[3].size = SIZEOF_VARIANT_T;
    88 		pass[4].type = PassType_Basic;
    89 		pass[4].flags = PASSFLAG_BYVAL;
    90 		pass[4].size = sizeof(int);
    91 		pass[5].type = PassType_Basic;
    92 		pass[5].flags = PASSFLAG_BYVAL;
    93 		pass[5].size = sizeof(bool);
    94 
    95 		if (!(g_pAcceptInput=g_pBinTools->CreateVCall(offset, 0, 0, &pass[5], pass, 5)))
    96 		{
    97 			pContext->ThrowNativeError("\"AcceptEntityInput\" wrapper failed to initialized");
    98 		}
    99 	}
   100 
   101 	CBaseEntity *pActivator, *pCaller, *pDest;
   102 
   103 	char *inputname;
   104 	unsigned char vstk[sizeof(void *) + sizeof(const char *) + sizeof(CBaseEntity *)*2 + SIZEOF_VARIANT_T + sizeof(int)];
   105 	unsigned char *vptr = vstk;
   106 
   107 	ENTINDEX_TO_CBASEENTITY(params[1], pDest);
   108 	pContext->LocalToString(params[2], &inputname);
   109 	if (params[3] == -1)
   110 	{
   111 		pActivator = NULL;
   112 	} else {
   113 		ENTINDEX_TO_CBASEENTITY(params[3], pActivator);
   114 	}
   115 	if (params[4] == -1)
   116 	{
   117 		pCaller = NULL;
   118 	} else {
   119 		ENTINDEX_TO_CBASEENTITY(params[4], pCaller);
   120 	}
   121 
   122 	*(void **)vptr = pDest;
   123 	vptr += sizeof(void *);
   124 	*(const char **)vptr = inputname;
   125 	vptr += sizeof(const char *);
   126 	*(CBaseEntity **)vptr = pActivator;
   127 	vptr += sizeof(CBaseEntity *);
   128 	*(CBaseEntity **)vptr = pCaller;
   129 	vptr += sizeof(CBaseEntity *);
   130 	memcpy(vptr, g_Variant_t, SIZEOF_VARIANT_T);
   131 	vptr += SIZEOF_VARIANT_T;
   132 	*(int *)vptr = params[5];
   133 
   134 	bool ret;
   135 	g_pAcceptInput->Execute(vstk, &ret);
   136 
   137 	_init_variant_t();
   138 
   139 	return (ret) ? 1 : 0;
   140 }
   141 
   142 static cell_t SetVariantBool(IPluginContext *pContext, const cell_t *params)
   143 {
   144 	unsigned char *vptr = g_Variant_t;
   145 
   146 	*(bool *)vptr = (params[1]) ? true : false;
   147 	vptr += sizeof(int)*3 + sizeof(unsigned long);
   148 	*(fieldtype_t *)vptr = FIELD_BOOLEAN;
   149 
   150 	return 1;
   151 }
   152 
   153 static cell_t SetVariantString(IPluginContext *pContext, const cell_t *params)
   154 {
   155 	char *str;
   156 	unsigned char *vptr = g_Variant_t;
   157 
   158 	pContext->LocalToString(params[1], &str);
   159 
   160 	*(string_t *)vptr = MAKE_STRING(str);
   161 	vptr += sizeof(int)*3 + sizeof(unsigned long);
   162 	*(fieldtype_t *)vptr = FIELD_STRING;
   163 
   164 	return 1;
   165 }
   166 
   167 static cell_t SetVariantInt(IPluginContext *pContext, const cell_t *params)
   168 {
   169 	unsigned char *vptr = g_Variant_t;
   170 
   171 	*(int *)vptr = params[1];
   172 	vptr += sizeof(int)*3 + sizeof(unsigned long);
   173 	*(fieldtype_t *)vptr = FIELD_INTEGER;
   174 
   175 	return 1;
   176 }
   177 
   178 static cell_t SetVariantFloat(IPluginContext *pContext, const cell_t *params)
   179 {
   180 	unsigned char *vptr = g_Variant_t;
   181 
   182 	*(float *)vptr = sp_ctof(params[1]);
   183 	vptr += sizeof(int)*3 + sizeof(unsigned long);
   184 	*(fieldtype_t *)vptr = FIELD_FLOAT;
   185 
   186 	return 1;
   187 }
   188 
   189 static cell_t SetVariantVector3D(IPluginContext *pContext, const cell_t *params)
   190 {
   191 	cell_t *val;
   192 	unsigned char *vptr = g_Variant_t;
   193 
   194 	pContext->LocalToPhysAddr(params[1], &val);
   195 
   196 	*(float *)vptr = sp_ctof(val[0]);
   197 	vptr += sizeof(float);
   198 	*(float *)vptr = sp_ctof(val[1]);
   199 	vptr += sizeof(float);
   200 	*(float *)vptr = sp_ctof(val[2]);
   201 	vptr += sizeof(float) + sizeof(unsigned long);
   202 	*(fieldtype_t *)vptr = FIELD_VECTOR;
   203 
   204 	return 1;
   205 }
   206 
   207 static cell_t SetVariantPosVector3D(IPluginContext *pContext, const cell_t *params)
   208 {
   209 	cell_t *val;
   210 	unsigned char *vptr = g_Variant_t;
   211 
   212 	pContext->LocalToPhysAddr(params[1], &val);
   213 
   214 	*(float *)vptr = sp_ctof(val[0]);
   215 	vptr += sizeof(float);
   216 	*(float *)vptr = sp_ctof(val[1]);
   217 	vptr += sizeof(float);
   218 	*(float *)vptr = sp_ctof(val[2]);
   219 	vptr += sizeof(float) + sizeof(unsigned long);
   220 	*(fieldtype_t *)vptr = FIELD_POSITION_VECTOR;
   221 
   222 	return 1;
   223 }
   224 
   225 static cell_t SetVariantColor(IPluginContext *pContext, const cell_t *params)
   226 {
   227 	cell_t *val;
   228 	unsigned char *vptr = g_Variant_t;
   229 
   230 	pContext->LocalToPhysAddr(params[1], &val);
   231 
   232 	*(unsigned char *)vptr = val[0];
   233 	vptr += sizeof(unsigned char);
   234 	*(unsigned char *)vptr = val[1];
   235 	vptr += sizeof(unsigned char);
   236 	*(unsigned char *)vptr = val[2];
   237 	vptr += sizeof(unsigned char);
   238 	*(unsigned char *)vptr = val[3];
   239 	vptr += sizeof(unsigned char) + sizeof(int)*2 + sizeof(unsigned long);
   240 	*(fieldtype_t *)vptr = FIELD_COLOR32;
   241 
   242 	return 1;
   243 }
   244 
   245 static cell_t SetVariantEntity(IPluginContext *pContext, const cell_t *params)
   246 {
   247 	CBaseEntity *pEntity;
   248 	unsigned char *vptr = g_Variant_t;
   249 	CBaseHandle bHandle;
   250 
   251 	ENTINDEX_TO_CBASEENTITY(params[1], pEntity);
   252 	bHandle = reinterpret_cast<IHandleEntity *>(pEntity)->GetRefEHandle();
   253 
   254 	vptr += sizeof(int)*3;
   255 	*(unsigned long *)vptr = (unsigned long)(bHandle.ToInt());
   256 	vptr += sizeof(unsigned long);
   257 	*(fieldtype_t *)vptr = FIELD_EHANDLE;
   258 
   259 	return 1;
   260 }
   261 
   262 sp_nativeinfo_t g_EntInputNatives[] =
   263 {
   264 	{"AcceptEntityInput",			AcceptEntityInput},
   265 	{"SetVariantBool",				SetVariantBool},
   266 	{"SetVariantString",			SetVariantString},
   267 	{"SetVariantInt",				SetVariantInt},
   268 	{"SetVariantFloat",				SetVariantFloat},
   269 	{"SetVariantVector3D",			SetVariantVector3D},
   270 	{"SetVariantPosVector3D",		SetVariantPosVector3D},
   271 	{"SetVariantColor",				SetVariantColor},
   272 	{"SetVariantEntity",			SetVariantEntity},
   273 	{NULL,							NULL},
   274 };