1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/extensions/sdktools/inputnatives.cpp Sun Mar 30 07:00:22 2008 +0000
1.3 @@ -0,0 +1,288 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * =============================================================================
1.7 + * SourceMod SDKTools Extension
1.8 + * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
1.9 + * =============================================================================
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify it under
1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
1.13 + * Free Software Foundation.
1.14 + *
1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 + * details.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License along with
1.21 + * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 + *
1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 + * by the Valve Corporation. You must obey the GNU General Public License in
1.27 + * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 + * this exception to all derivative works. AlliedModders LLC defines further
1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 + * or <http://www.sourcemod.net/license.php>.
1.31 + *
1.32 + * Version: $Id$
1.33 + */
1.34 +
1.35 +#include "extension.h"
1.36 +#include <datamap.h>
1.37 +
1.38 +#define SIZEOF_VARIANT_T 20
1.39 +
1.40 +ICallWrapper *g_pAcceptInput = NULL;
1.41 +unsigned char g_Variant_t[SIZEOF_VARIANT_T] = {0};
1.42 +
1.43 +#define ENTINDEX_TO_CBASEENTITY(index, buffer) \
1.44 + pEdict = engine->PEntityOfEntIndex(index); \
1.45 + if (!pEdict || pEdict->IsFree()) \
1.46 + { \
1.47 + return pContext->ThrowNativeError("Entity %d is not valid or is freed", index); \
1.48 + } \
1.49 + pUnk = pEdict->GetUnknown(); \
1.50 + if (!pUnk) \
1.51 + { \
1.52 + return pContext->ThrowNativeError("Entity %d is a not an IServerUnknown", index); \
1.53 + } \
1.54 + buffer = pUnk->GetBaseEntity(); \
1.55 + if (!buffer) \
1.56 + { \
1.57 + return pContext->ThrowNativeError("Entity %d is not a CBaseEntity", index); \
1.58 + }
1.59 +
1.60 +/* Hack to init the variant_t object for the first time */
1.61 +class VariantFirstTimeInit
1.62 +{
1.63 +public:
1.64 + VariantFirstTimeInit()
1.65 + {
1.66 + *(unsigned int *)(&g_Variant_t[12]) = INVALID_EHANDLE_INDEX;
1.67 + }
1.68 +} g_VariantFirstTimeInit;
1.69 +
1.70 +inline void _init_variant_t()
1.71 +{
1.72 + unsigned char *vptr = g_Variant_t;
1.73 +
1.74 + *(int *)vptr = 0;
1.75 + vptr += sizeof(int)*3;
1.76 + *(unsigned long *)vptr = INVALID_EHANDLE_INDEX;
1.77 + vptr += sizeof(unsigned long);
1.78 + *(fieldtype_t *)vptr = FIELD_VOID;
1.79 +}
1.80 +
1.81 +static cell_t AcceptEntityInput(IPluginContext *pContext, const cell_t *params)
1.82 +{
1.83 + if (!g_pAcceptInput)
1.84 + {
1.85 + int offset;
1.86 + if (!g_pGameConf->GetOffset("AcceptInput", &offset))
1.87 + {
1.88 + return pContext->ThrowNativeError("\"AcceptEntityInput\" not supported by this mod");
1.89 + }
1.90 +
1.91 + PassInfo pass[6];
1.92 + pass[0].type = PassType_Basic;
1.93 + pass[0].flags = PASSFLAG_BYVAL;
1.94 + pass[0].size = sizeof(const char *);
1.95 + pass[1].type = pass[2].type = PassType_Basic;
1.96 + pass[1].flags = pass[2].flags = PASSFLAG_BYVAL;
1.97 + pass[1].size = pass[2].size = sizeof(CBaseEntity *);
1.98 + pass[3].type = PassType_Object;
1.99 + pass[3].flags = PASSFLAG_BYVAL|PASSFLAG_OCTOR|PASSFLAG_ODTOR|PASSFLAG_OASSIGNOP;
1.100 + pass[3].size = SIZEOF_VARIANT_T;
1.101 + pass[4].type = PassType_Basic;
1.102 + pass[4].flags = PASSFLAG_BYVAL;
1.103 + pass[4].size = sizeof(int);
1.104 + pass[5].type = PassType_Basic;
1.105 + pass[5].flags = PASSFLAG_BYVAL;
1.106 + pass[5].size = sizeof(bool);
1.107 +
1.108 + if (!(g_pAcceptInput=g_pBinTools->CreateVCall(offset, 0, 0, &pass[5], pass, 5)))
1.109 + {
1.110 + pContext->ThrowNativeError("\"AcceptEntityInput\" wrapper failed to initialized");
1.111 + }
1.112 + }
1.113 +
1.114 + CBaseEntity *pActivator, *pCaller, *pDest;
1.115 + edict_t *pEdict;
1.116 + IServerUnknown *pUnk;
1.117 +
1.118 + char *inputname;
1.119 + unsigned char vstk[sizeof(void *) + sizeof(const char *) + sizeof(CBaseEntity *)*2 + SIZEOF_VARIANT_T + sizeof(int)];
1.120 + unsigned char *vptr = vstk;
1.121 +
1.122 + ENTINDEX_TO_CBASEENTITY(params[1], pDest);
1.123 + pContext->LocalToString(params[2], &inputname);
1.124 + if (params[3] == -1)
1.125 + {
1.126 + pActivator = NULL;
1.127 + } else {
1.128 + ENTINDEX_TO_CBASEENTITY(params[3], pActivator);
1.129 + }
1.130 + if (params[4] == -1)
1.131 + {
1.132 + pCaller = NULL;
1.133 + } else {
1.134 + ENTINDEX_TO_CBASEENTITY(params[4], pCaller);
1.135 + }
1.136 +
1.137 + *(void **)vptr = pDest;
1.138 + vptr += sizeof(void *);
1.139 + *(const char **)vptr = inputname;
1.140 + vptr += sizeof(const char *);
1.141 + *(CBaseEntity **)vptr = pActivator;
1.142 + vptr += sizeof(CBaseEntity *);
1.143 + *(CBaseEntity **)vptr = pCaller;
1.144 + vptr += sizeof(CBaseEntity *);
1.145 + memcpy(vptr, g_Variant_t, SIZEOF_VARIANT_T);
1.146 + vptr += SIZEOF_VARIANT_T;
1.147 + *(int *)vptr = params[5];
1.148 +
1.149 + bool ret;
1.150 + g_pAcceptInput->Execute(vstk, &ret);
1.151 +
1.152 + _init_variant_t();
1.153 +
1.154 + return (ret) ? 1 : 0;
1.155 +}
1.156 +
1.157 +static cell_t SetVariantBool(IPluginContext *pContext, const cell_t *params)
1.158 +{
1.159 + unsigned char *vptr = g_Variant_t;
1.160 +
1.161 + *(bool *)vptr = (params[1]) ? true : false;
1.162 + vptr += sizeof(int)*3 + sizeof(unsigned long);
1.163 + *(fieldtype_t *)vptr = FIELD_BOOLEAN;
1.164 +
1.165 + return 1;
1.166 +}
1.167 +
1.168 +static cell_t SetVariantString(IPluginContext *pContext, const cell_t *params)
1.169 +{
1.170 + char *str;
1.171 + unsigned char *vptr = g_Variant_t;
1.172 +
1.173 + pContext->LocalToString(params[1], &str);
1.174 +
1.175 + *(string_t *)vptr = MAKE_STRING(str);
1.176 + vptr += sizeof(int)*3 + sizeof(unsigned long);
1.177 + *(fieldtype_t *)vptr = FIELD_STRING;
1.178 +
1.179 + return 1;
1.180 +}
1.181 +
1.182 +static cell_t SetVariantInt(IPluginContext *pContext, const cell_t *params)
1.183 +{
1.184 + unsigned char *vptr = g_Variant_t;
1.185 +
1.186 + *(int *)vptr = params[1];
1.187 + vptr += sizeof(int)*3 + sizeof(unsigned long);
1.188 + *(fieldtype_t *)vptr = FIELD_INTEGER;
1.189 +
1.190 + return 1;
1.191 +}
1.192 +
1.193 +static cell_t SetVariantFloat(IPluginContext *pContext, const cell_t *params)
1.194 +{
1.195 + unsigned char *vptr = g_Variant_t;
1.196 +
1.197 + *(float *)vptr = sp_ctof(params[1]);
1.198 + vptr += sizeof(int)*3 + sizeof(unsigned long);
1.199 + *(fieldtype_t *)vptr = FIELD_FLOAT;
1.200 +
1.201 + return 1;
1.202 +}
1.203 +
1.204 +static cell_t SetVariantVector3D(IPluginContext *pContext, const cell_t *params)
1.205 +{
1.206 + cell_t *val;
1.207 + unsigned char *vptr = g_Variant_t;
1.208 +
1.209 + pContext->LocalToPhysAddr(params[1], &val);
1.210 +
1.211 + *(float *)vptr = sp_ctof(val[0]);
1.212 + vptr += sizeof(float);
1.213 + *(float *)vptr = sp_ctof(val[1]);
1.214 + vptr += sizeof(float);
1.215 + *(float *)vptr = sp_ctof(val[2]);
1.216 + vptr += sizeof(float) + sizeof(unsigned long);
1.217 + *(fieldtype_t *)vptr = FIELD_VECTOR;
1.218 +
1.219 + return 1;
1.220 +}
1.221 +
1.222 +static cell_t SetVariantPosVector3D(IPluginContext *pContext, const cell_t *params)
1.223 +{
1.224 + cell_t *val;
1.225 + unsigned char *vptr = g_Variant_t;
1.226 +
1.227 + pContext->LocalToPhysAddr(params[1], &val);
1.228 +
1.229 + *(float *)vptr = sp_ctof(val[0]);
1.230 + vptr += sizeof(float);
1.231 + *(float *)vptr = sp_ctof(val[1]);
1.232 + vptr += sizeof(float);
1.233 + *(float *)vptr = sp_ctof(val[2]);
1.234 + vptr += sizeof(float) + sizeof(unsigned long);
1.235 + *(fieldtype_t *)vptr = FIELD_POSITION_VECTOR;
1.236 +
1.237 + return 1;
1.238 +}
1.239 +
1.240 +static cell_t SetVariantColor(IPluginContext *pContext, const cell_t *params)
1.241 +{
1.242 + cell_t *val;
1.243 + unsigned char *vptr = g_Variant_t;
1.244 +
1.245 + pContext->LocalToPhysAddr(params[1], &val);
1.246 +
1.247 + *(unsigned char *)vptr = val[0];
1.248 + vptr += sizeof(unsigned char);
1.249 + *(unsigned char *)vptr = val[1];
1.250 + vptr += sizeof(unsigned char);
1.251 + *(unsigned char *)vptr = val[2];
1.252 + vptr += sizeof(unsigned char);
1.253 + *(unsigned char *)vptr = val[3];
1.254 + vptr += sizeof(unsigned char) + sizeof(int)*2 + sizeof(unsigned long);
1.255 + *(fieldtype_t *)vptr = FIELD_COLOR32;
1.256 +
1.257 + return 1;
1.258 +}
1.259 +
1.260 +static cell_t SetVariantEntity(IPluginContext *pContext, const cell_t *params)
1.261 +{
1.262 + CBaseEntity *pEntity;
1.263 + edict_t *pEdict;
1.264 + IServerUnknown *pUnk;
1.265 + unsigned char *vptr = g_Variant_t;
1.266 + CBaseHandle bHandle;
1.267 +
1.268 + ENTINDEX_TO_CBASEENTITY(params[1], pEntity);
1.269 + bHandle = reinterpret_cast<IHandleEntity *>(pEntity)->GetRefEHandle();
1.270 +
1.271 + vptr += sizeof(int)*3;
1.272 + *(unsigned long *)vptr = (unsigned long)(bHandle.ToInt());
1.273 + vptr += sizeof(unsigned long);
1.274 + *(fieldtype_t *)vptr = FIELD_EHANDLE;
1.275 +
1.276 + return 1;
1.277 +}
1.278 +
1.279 +sp_nativeinfo_t g_EntInputNatives[] =
1.280 +{
1.281 + {"AcceptEntityInput", AcceptEntityInput},
1.282 + {"SetVariantBool", SetVariantBool},
1.283 + {"SetVariantString", SetVariantString},
1.284 + {"SetVariantInt", SetVariantInt},
1.285 + {"SetVariantFloat", SetVariantFloat},
1.286 + {"SetVariantVector3D", SetVariantVector3D},
1.287 + {"SetVariantPosVector3D", SetVariantPosVector3D},
1.288 + {"SetVariantColor", SetVariantColor},
1.289 + {"SetVariantEntity", SetVariantEntity},
1.290 + {NULL, NULL},
1.291 +};