3 * =============================================================================
4 * SourceMod SDKTools Extension
5 * Copyright (C) 2004-2010 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
33 #include <sh_string.h>
34 #include "extension.h"
35 #include "vcallbuilder.h"
39 #include "CellRecipientFilter.h"
40 #include <inetchannel.h>
43 #include <iserverentity.h>
45 SourceHook::List<ValveCall *> g_RegCalls;
46 SourceHook::List<ICallWrapper *> g_CallWraps;
48 inline void InitPass(ValvePassInfo &info, ValveType vtype, PassType type, unsigned int flags, unsigned int decflags=0)
50 info.decflags = decflags;
57 #define START_CALL() \
58 unsigned char *vptr = pCall->stk_get();
60 #define FINISH_CALL_SIMPLE(vret) \
61 pCall->call->Execute(vptr, vret); \
64 #define ENCODE_VALVE_PARAM(num, which, vnum) \
65 if (EncodeValveParam(pContext, \
68 &pCall->which[vnum], \
75 #define DECODE_VALVE_PARAM(num, which, vnum) \
76 if (DecodeValveParam(pContext, \
79 &pCall->which[vnum], \
86 bool CreateBaseCall(const char *name,
87 ValveCallType vcalltype,
88 const ValvePassInfo *retinfo,
89 const ValvePassInfo params[],
90 unsigned int numParams,
95 if (g_pGameConf->GetOffset(name, &offset))
97 call = CreateValveVCall(offset, vcalltype, retinfo, params, numParams);
100 g_RegCalls.push_back(call);
106 if (g_pGameConf->GetMemSig(name, &addr) && addr != NULL)
108 call = CreateValveCall(addr, vcalltype, retinfo, params, numParams);
111 g_RegCalls.push_back(call);
120 static cell_t RemovePlayerItem(IPluginContext *pContext, const cell_t *params)
122 static ValveCall *pCall = NULL;
125 ValvePassInfo pass[2];
126 InitPass(pass[0], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
127 InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
128 if (!CreateBaseCall("RemovePlayerItem", ValveCall_Player, &pass[1], pass, 1, &pCall))
130 return pContext->ThrowNativeError("\"RemovePlayerItem\" not supported by this mod");
132 return pContext->ThrowNativeError("\"RemovePlayerItem\" wrapper failed to initialize");
138 DECODE_VALVE_PARAM(1, thisinfo, 0);
139 DECODE_VALVE_PARAM(2, vparams, 0);
140 FINISH_CALL_SIMPLE(&ret);
144 static cell_t GiveNamedItem(IPluginContext *pContext, const cell_t *params)
146 static ValveCall *pCall = NULL;
149 ValvePassInfo pass[3];
150 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
151 InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
152 InitPass(pass[2], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
153 if (!CreateBaseCall("GiveNamedItem", ValveCall_Player, &pass[2], pass, 2, &pCall))
155 return pContext->ThrowNativeError("\"GiveNamedItem\" not supported by this mod");
157 return pContext->ThrowNativeError("\"GiveNamedItem\" wrapper failed to initialize");
161 CBaseEntity *pEntity = NULL;
163 DECODE_VALVE_PARAM(1, thisinfo, 0);
164 DECODE_VALVE_PARAM(2, vparams, 0);
165 DECODE_VALVE_PARAM(3, vparams, 1);
166 FINISH_CALL_SIMPLE(&pEntity);
168 return gamehelpers->EntityToBCompatRef(pEntity);
171 static cell_t GetPlayerWeaponSlot(IPluginContext *pContext, const cell_t *params)
173 static ValveCall *pCall = NULL;
176 ValvePassInfo pass[2];
177 InitPass(pass[0], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
178 InitPass(pass[1], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
179 if (!CreateBaseCall("Weapon_GetSlot", ValveCall_Player, &pass[1], pass, 1, &pCall))
181 return pContext->ThrowNativeError("\"Weapon_GetSlot\" not supported by this mod");
183 return pContext->ThrowNativeError("\"Weapon_GetSlot\" wrapper failed to initialize");
187 CBaseEntity *pEntity;
189 DECODE_VALVE_PARAM(1, thisinfo, 0);
190 DECODE_VALVE_PARAM(2, vparams, 0);
191 FINISH_CALL_SIMPLE(&pEntity);
193 return gamehelpers->EntityToBCompatRef(pEntity);
196 #if SOURCE_ENGINE != SE_DARKMESSIAH
197 static cell_t IgniteEntity(IPluginContext *pContext, const cell_t *params)
199 static ValveCall *pCall = NULL;
202 ValvePassInfo pass[4];
203 InitPass(pass[0], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
204 InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
205 InitPass(pass[2], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
206 InitPass(pass[3], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
207 if (!CreateBaseCall("Ignite", ValveCall_Entity, NULL, pass, 4, &pCall))
209 return pContext->ThrowNativeError("\"Ignite\" not supported by this mod");
211 return pContext->ThrowNativeError("\"Ignite\" wrapper failed to initialize");
216 DECODE_VALVE_PARAM(1, thisinfo, 0);
217 DECODE_VALVE_PARAM(2, vparams, 0);
218 DECODE_VALVE_PARAM(3, vparams, 1);
219 DECODE_VALVE_PARAM(4, vparams, 2);
220 DECODE_VALVE_PARAM(5, vparams, 3);
221 FINISH_CALL_SIMPLE(NULL);
226 /* Dark Messiah specific version */
227 static cell_t IgniteEntity(IPluginContext *pContext, const cell_t *params)
229 static ValveCall *pCall = NULL;
232 ValvePassInfo pass[6];
233 InitPass(pass[0], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
234 InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
235 InitPass(pass[2], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
236 InitPass(pass[3], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
237 InitPass(pass[4], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
238 InitPass(pass[5], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
239 if (!CreateBaseCall("Ignite", ValveCall_Entity, NULL, pass, 6, &pCall))
241 return pContext->ThrowNativeError("\"Ignite\" not supported by this mod");
243 return pContext->ThrowNativeError("\"Ignite\" wrapper failed to initialize");
248 DECODE_VALVE_PARAM(1, thisinfo, 0);
249 DECODE_VALVE_PARAM(2, vparams, 0);
250 DECODE_VALVE_PARAM(3, vparams, 1);
251 DECODE_VALVE_PARAM(4, vparams, 2);
252 DECODE_VALVE_PARAM(5, vparams, 3);
253 /* Not sure what these params do, but they appear to be the default values */
254 *(int *)(vptr + 14) = 3;
255 *(int *)(vptr + 18) = 0;
256 FINISH_CALL_SIMPLE(NULL);
262 static cell_t ExtinguishEntity(IPluginContext *pContext, const cell_t *params)
264 static ValveCall *pCall = NULL;
267 if (!CreateBaseCall("Extinguish", ValveCall_Entity, NULL, NULL, 0, &pCall))
269 return pContext->ThrowNativeError("\"Extinguish\" not supported by this mod");
271 return pContext->ThrowNativeError("\"Extinguish\" wrapper failed to initialize");
276 DECODE_VALVE_PARAM(1, thisinfo, 0);
277 FINISH_CALL_SIMPLE(NULL);
282 static cell_t TeleportEntity(IPluginContext *pContext, const cell_t *params)
284 static ValveCall *pCall = NULL;
287 ValvePassInfo pass[3];
288 InitPass(pass[0], Valve_Vector, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL);
289 InitPass(pass[1], Valve_QAngle, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL);
290 InitPass(pass[2], Valve_Vector, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL);
291 if (!CreateBaseCall("Teleport", ValveCall_Entity, NULL, pass, 3, &pCall))
293 return pContext->ThrowNativeError("\"Teleport\" not supported by this mod");
295 return pContext->ThrowNativeError("\"Teleport\" wrapper failed to initialize");
300 DECODE_VALVE_PARAM(1, thisinfo, 0);
301 DECODE_VALVE_PARAM(2, vparams, 0);
302 DECODE_VALVE_PARAM(3, vparams, 1);
303 DECODE_VALVE_PARAM(4, vparams, 2);
304 FINISH_CALL_SIMPLE(NULL);
309 #if SOURCE_ENGINE >= SE_ORANGEBOX
310 /* :TODO: This is Team Fortress 2 specific */
311 static cell_t ForcePlayerSuicide(IPluginContext *pContext, const cell_t *params)
313 static ValveCall *pCall = NULL;
316 ValvePassInfo pass[2];
317 InitPass(pass[0], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
318 InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
319 if (!CreateBaseCall("CommitSuicide", ValveCall_Player, NULL, pass, 2, &pCall))
321 return pContext->ThrowNativeError("\"CommitSuicide\" not supported by this mod");
325 return pContext->ThrowNativeError("\"CommitSuicide\" wrapper failed to initialize");
330 DECODE_VALVE_PARAM(1, thisinfo, 0);
331 *(bool *)(vptr + 4) = false;
332 *(bool *)(vptr + 5) = false;
333 FINISH_CALL_SIMPLE(NULL);
338 static cell_t ForcePlayerSuicide(IPluginContext *pContext, const cell_t *params)
340 static ValveCall *pCall = NULL;
343 if (!CreateBaseCall("CommitSuicide", ValveCall_Player, NULL, NULL, 0, &pCall))
345 return pContext->ThrowNativeError("\"CommitSuicide\" not supported by this mod");
347 return pContext->ThrowNativeError("\"CommitSuicide\" wrapper failed to initialize");
352 DECODE_VALVE_PARAM(1, thisinfo, 0);
353 FINISH_CALL_SIMPLE(NULL);
359 static cell_t SetClientViewEntity(IPluginContext *pContext, const cell_t *params)
361 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
364 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
366 if (!player->IsInGame())
368 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
371 edict_t *pEdict = PEntityOfEntIndex(gamehelpers->ReferenceToIndex(params[2]));
372 if (!pEdict || pEdict->IsFree())
374 return pContext->ThrowNativeError("Entity %d is not valid", params[2]);
377 engine->SetView(player->GetEdict(), pEdict);
382 static SourceHook::String *g_lightstyle[MAX_LIGHTSTYLES] = {NULL};
383 static cell_t SetLightStyle(IPluginContext *pContext, const cell_t *params)
385 int style = params[1];
386 if (style >= MAX_LIGHTSTYLES)
388 return pContext->ThrowNativeError("Light style %d is invalid (range: 0-%d)", style, MAX_LIGHTSTYLES - 1);
391 if (g_lightstyle[style] == NULL)
393 /* We allocate and never free this because the Engine wants to hold onto it :\
394 * in theory we could hook light style and know whether we're supposed to free
395 * this or not on shutdown, but for ~4K of memory MAX, it doesn't seem worth it yet.
398 g_lightstyle[style] = new SourceHook::String();
402 pContext->LocalToString(params[2], &str);
404 g_lightstyle[style]->assign(str);
406 engine->LightStyle(style, g_lightstyle[style]->c_str());
411 static cell_t SlapPlayer(IPluginContext *pContext, const cell_t *params)
413 static bool s_slap_supported = false;
414 static bool s_slap_setup = false;
415 static ICallWrapper *s_teleport = NULL;
416 static int s_health_offs = 0;
417 static int s_sound_count = 0;
418 static int s_frag_offs = 0;
426 if (IsTeleportSupported())
430 if (IsGetVelocitySupported())
436 if (g_pGameConf->GetOffset("m_iHealth", &s_health_offs) && s_health_offs)
443 s_slap_supported = true;
446 if ((key = g_pGameConf->GetKeyValue("SlapSoundCount")) != NULL)
448 s_sound_count = atoi(key);
453 if (!s_slap_supported)
455 return pContext->ThrowNativeError("This function is not supported on this mod");
458 /* First check if the client is valid */
459 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
462 return pContext->ThrowNativeError("Client %d is not valid", params[1]);
463 } else if (!player->IsInGame()) {
464 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
467 edict_t *pEdict = player->GetEdict();
468 CBaseEntity *pEntity = pEdict->GetUnknown()->GetBaseEntity();
470 /* See if we should be taking away health */
471 bool should_slay = false;
474 #if SOURCE_ENGINE != SE_DARKMESSIAH
475 int *health = (int *)((char *)pEntity + s_health_offs);
477 if (*health - params[2] <= 0)
482 *health -= params[2];
485 float *health = (float *)((char *)pEntity + s_health_offs);
487 if (*health - (float)params[2] <= 0)
492 *health -= (float)params[2];
497 /* Teleport in a random direction - thank you, Mani!*/
499 GetVelocity(pEntity, &velocity, NULL);
500 velocity.x += ((rand() % 180) + 50) * (((rand() % 2) == 1) ? -1 : 1);
501 velocity.y += ((rand() % 180) + 50) * (((rand() % 2) == 1) ? -1 : 1);
502 velocity.z += rand() % 200 + 100;
503 Teleport(pEntity, NULL, NULL, &velocity);
505 /* Play a random sound */
506 if (params[3] && s_sound_count > 0)
509 const char *sound_name;
510 cell_t player_list[256], total_players = 0;
511 int maxClients = playerhelpers->GetMaxClients();
513 int r = (rand() % s_sound_count) + 1;
514 snprintf(name, sizeof(name), "SlapSound%d", r);
516 if ((sound_name = g_pGameConf->GetKeyValue(name)) != NULL)
519 for (int i=1; i<=maxClients; i++)
521 other = playerhelpers->GetGamePlayer(i);
522 if (other->IsInGame())
524 player_list[total_players++] = i;
528 const Vector & pos = pEdict->GetCollideable()->GetCollisionOrigin();
529 CellRecipientFilter rf;
530 rf.SetToReliable(true);
531 rf.Initialize(player_list, total_players);
532 engsound->EmitSound(rf, params[1], CHAN_AUTO, sound_name, VOL_NORM, ATTN_NORM, 0, PITCH_NORM, &pos);
538 const char *frag_prop = g_pGameConf->GetKeyValue("m_iFrags");
541 datamap_t *pMap = gamehelpers->GetDataMap(pEntity);
542 typedescription_t *pType = gamehelpers->FindInDataMap(pMap, frag_prop);
545 s_frag_offs = GetTypeDescOffs(pType);
557 old_frags = *(int *)((char *)pEntity + s_frag_offs);
563 pluginhelpers->ClientCommand(pEdict, "kill\n");
568 *(int *)((char *)pEntity + s_frag_offs) = old_frags;
574 static cell_t GetClientEyePosition(IPluginContext *pContext, const cell_t *params)
576 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
579 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
581 if (!player->IsInGame())
583 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
587 serverClients->ClientEarPosition(player->GetEdict(), &pos);
590 pContext->LocalToPhysAddr(params[2], &addr);
591 addr[0] = sp_ftoc(pos.x);
592 addr[1] = sp_ftoc(pos.y);
593 addr[2] = sp_ftoc(pos.z);
598 static cell_t GetClientEyeAngles(IPluginContext *pContext, const cell_t *params)
600 IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(params[1]);
604 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
606 else if (!pPlayer->IsInGame())
608 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
611 edict_t *pEdict = pPlayer->GetEdict();
612 CBaseEntity *pEntity = pEdict->GetUnknown() ? pEdict->GetUnknown()->GetBaseEntity() : NULL;
614 /* We always set the angles for backwards compatibility --
615 * The original function had no return value.
618 bool got_angles = false;
622 got_angles = GetEyeAngles(pEntity, &angles);
626 pContext->LocalToPhysAddr(params[2], &addr);
627 addr[0] = sp_ftoc(angles.x);
628 addr[1] = sp_ftoc(angles.y);
629 addr[2] = sp_ftoc(angles.z);
631 return got_angles ? 1 : 0;
634 #if SOURCE_ENGINE >= SE_ORANGEBOX
635 static cell_t FindEntityByClassname(IPluginContext *pContext, const cell_t *params)
638 CBaseEntity *pEntity;
642 pEntity = (CBaseEntity *)servertools->FirstEntity();
646 pEntity = gamehelpers->ReferenceToEntity(params[1]);
649 return pContext->ThrowNativeError("Entity %d (%d) is invalid",
650 gamehelpers->ReferenceToIndex(params[1]),
655 pContext->LocalToString(params[2], &searchname);
658 const char *classname = buffer;
660 bool ismatch = false;
664 if (!servertools->GetKeyValue(pEntity, "classname", buffer, sizeof(buffer)))
666 pEntity = (CBaseEntity *)servertools->NextEntity(pEntity);
670 lastcharpos = strlen(searchname);
671 if (searchname[lastcharpos-1] == '*')
673 if (strncasecmp(searchname, classname, lastcharpos-1) == 0)
678 else if (strcasecmp(searchname, classname) == 0)
685 return gamehelpers->EntityToBCompatRef(pEntity);
688 pEntity = (CBaseEntity *)servertools->NextEntity(pEntity);
694 static cell_t FindEntityByClassname(IPluginContext *pContext, const cell_t *params)
696 static ValveCall *pCall = NULL;
699 ValvePassInfo pass[3];
700 InitPass(pass[0], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL|VDECODE_FLAG_ALLOWWORLD);
701 InitPass(pass[1], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
702 InitPass(pass[2], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
703 if (!CreateBaseCall("FindEntityByClassname", ValveCall_EntityList, &pass[2], pass, 2, &pCall))
705 return pContext->ThrowNativeError("\"FindEntityByClassname\" not supported by this mod");
707 return pContext->ThrowNativeError("\"FindEntityByClassname\" wrapper failed to initialize");
711 CBaseEntity *pEntity;
713 *(void **)vptr = g_EntList;
714 DECODE_VALVE_PARAM(1, vparams, 0);
715 DECODE_VALVE_PARAM(2, vparams, 1);
716 FINISH_CALL_SIMPLE(&pEntity);
718 return gamehelpers->EntityToBCompatRef(pEntity);
722 #if SOURCE_ENGINE >= SE_ORANGEBOX
723 static cell_t CreateEntityByName(IPluginContext *pContext, const cell_t *params)
726 pContext->LocalToString(params[1], &classname);
727 CBaseEntity *pEntity = (CBaseEntity *)servertools->CreateEntityByName(classname);
728 return gamehelpers->EntityToBCompatRef(pEntity);
731 static cell_t CreateEntityByName(IPluginContext *pContext, const cell_t *params)
733 static ValveCall *pCall = NULL;
736 ValvePassInfo pass[3];
737 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
738 InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
739 InitPass(pass[2], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
740 if (!CreateBaseCall("CreateEntityByName", ValveCall_Static, &pass[2], pass, 2, &pCall))
742 return pContext->ThrowNativeError("\"CreateEntityByName\" not supported by this mod");
744 return pContext->ThrowNativeError("\"CreateEntityByName\" wrapper failed to initialize");
748 CBaseEntity *pEntity = NULL;
750 DECODE_VALVE_PARAM(1, vparams, 0);
751 DECODE_VALVE_PARAM(2, vparams, 1);
752 FINISH_CALL_SIMPLE(&pEntity);
754 return gamehelpers->EntityToBCompatRef(pEntity);
758 #if SOURCE_ENGINE >= SE_ORANGEBOX
759 static cell_t DispatchSpawn(IPluginContext *pContext, const cell_t *params)
761 CBaseEntity *pEntity = gamehelpers->ReferenceToEntity(params[1]);
764 return pContext->ThrowNativeError("Entity %d (%d) is invalid", gamehelpers->ReferenceToIndex(params[1]), params[1]);
767 servertools->DispatchSpawn(pEntity);
772 static cell_t DispatchSpawn(IPluginContext *pContext, const cell_t *params)
774 static ValveCall *pCall = NULL;
777 ValvePassInfo pass[2];
778 InitPass(pass[0], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
779 InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
780 if (!CreateBaseCall("DispatchSpawn", ValveCall_Static, &pass[1], pass, 1, &pCall))
782 return pContext->ThrowNativeError("\"DispatchSpawn\" not supported by this mod");
784 return pContext->ThrowNativeError("\"DispatchSpawn\" wrapper failed to initialize");
790 DECODE_VALVE_PARAM(1, vparams, 0);
791 FINISH_CALL_SIMPLE(&ret);
793 return (ret == -1) ? 0 : 1;
797 #if SOURCE_ENGINE >= SE_ORANGEBOX
798 static cell_t DispatchKeyValue(IPluginContext *pContext, const cell_t *params)
800 CBaseEntity *pEntity = gamehelpers->ReferenceToEntity(params[1]);
803 return pContext->ThrowNativeError("Entity %d (%d) is invalid", gamehelpers->ReferenceToIndex(params[1]), params[1]);
808 pContext->LocalToString(params[2], &key);
809 pContext->LocalToString(params[3], &value);
811 return (servertools->SetKeyValue(pEntity, key, value) ? 1 : 0);
814 static cell_t DispatchKeyValue(IPluginContext *pContext, const cell_t *params)
816 static ValveCall *pCall = NULL;
819 ValvePassInfo pass[3];
820 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
821 InitPass(pass[1], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
822 InitPass(pass[2], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
823 if (!CreateBaseCall("DispatchKeyValue", ValveCall_Entity, &pass[2], pass, 2, &pCall))
825 return pContext->ThrowNativeError("\"DispatchKeyValue\" not supported by this mod");
827 return pContext->ThrowNativeError("\"DispatchKeyValue\" wrapper failed to initialize");
833 DECODE_VALVE_PARAM(1, thisinfo, 0);
834 DECODE_VALVE_PARAM(2, vparams, 0);
835 DECODE_VALVE_PARAM(3, vparams, 1);
836 FINISH_CALL_SIMPLE(&ret);
842 #if SOURCE_ENGINE >= SE_ORANGEBOX
843 static cell_t DispatchKeyValueFloat(IPluginContext *pContext, const cell_t *params)
845 CBaseEntity *pEntity = gamehelpers->ReferenceToEntity(params[1]);
848 return pContext->ThrowNativeError("Entity %d (%d) is invalid", gamehelpers->ReferenceToIndex(params[1]), params[1]);
852 pContext->LocalToString(params[2], &key);
854 float value = sp_ctof(params[3]);
856 return (servertools->SetKeyValue(pEntity, key, value) ? 1 : 0);
859 static cell_t DispatchKeyValueFloat(IPluginContext *pContext, const cell_t *params)
861 static ValveCall *pCall = NULL;
864 ValvePassInfo pass[3];
865 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
866 InitPass(pass[1], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
867 InitPass(pass[2], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
868 if (!CreateBaseCall("DispatchKeyValueFloat", ValveCall_Entity, &pass[2], pass, 2, &pCall))
870 return pContext->ThrowNativeError("\"DispatchKeyValueFloat\" not supported by this mod");
872 return pContext->ThrowNativeError("\"DispatchKeyValueFloat\" wrapper failed to initialize");
878 DECODE_VALVE_PARAM(1, thisinfo, 0);
879 DECODE_VALVE_PARAM(2, vparams, 0);
880 DECODE_VALVE_PARAM(3, vparams, 1);
881 FINISH_CALL_SIMPLE(&ret);
887 #if SOURCE_ENGINE >= SE_ORANGEBOX
888 static cell_t DispatchKeyValueVector(IPluginContext *pContext, const cell_t *params)
890 CBaseEntity *pEntity = gamehelpers->ReferenceToEntity(params[1]);
893 return pContext->ThrowNativeError("Entity %d (%d) is invalid", gamehelpers->ReferenceToIndex(params[1]), params[1]);
897 pContext->LocalToString(params[2], &key);
900 pContext->LocalToPhysAddr(params[3], &vec);
902 const Vector *v = new Vector(
907 return (servertools->SetKeyValue(pEntity, key, *v) ? 1 : 0);
910 static cell_t DispatchKeyValueVector(IPluginContext *pContext, const cell_t *params)
912 static ValveCall *pCall = NULL;
915 ValvePassInfo pass[3];
916 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
917 #if SOURCE_ENGINE >= SE_ORANGEBOX
918 InitPass(pass[1], Valve_Vector, PassType_Basic, PASSFLAG_BYVAL);
920 InitPass(pass[1], Valve_Vector, PassType_Object, PASSFLAG_BYVAL|PASSFLAG_OCTOR|PASSFLAG_OASSIGNOP);
922 InitPass(pass[2], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
923 if (!CreateBaseCall("DispatchKeyValueVector", ValveCall_Entity, &pass[2], pass, 2, &pCall))
925 return pContext->ThrowNativeError("\"DispatchKeyValueVector\" not supported by this mod");
927 return pContext->ThrowNativeError("\"DispatchKeyValueVector\" wrapper failed to initialize");
933 DECODE_VALVE_PARAM(1, thisinfo, 0);
934 DECODE_VALVE_PARAM(2, vparams, 0);
935 DECODE_VALVE_PARAM(3, vparams, 1);
936 FINISH_CALL_SIMPLE(&ret);
942 static cell_t sm_GetClientAimTarget(IPluginContext *pContext, const cell_t *params)
944 IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(params[1]);
948 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
950 else if (!pPlayer->IsInGame())
952 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
955 return GetClientAimTarget(pPlayer->GetEdict(), params[2] ? true : false);
958 static cell_t sm_SetEntityModel(IPluginContext *pContext, const cell_t *params)
960 static ValveCall *pCall = NULL;
963 ValvePassInfo pass[1];
964 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
965 if (!CreateBaseCall("SetEntityModel", ValveCall_Entity, NULL, pass, 1, &pCall))
967 return pContext->ThrowNativeError("\"SetEntityModel\" not supported by this mod");
969 return pContext->ThrowNativeError("\"SetEntityModel\" wrapper failed to initialize");
974 DECODE_VALVE_PARAM(1, thisinfo, 0);
975 DECODE_VALVE_PARAM(2, vparams, 0);
976 FINISH_CALL_SIMPLE(NULL);
981 static cell_t GetPlayerDecalFile(IPluginContext *pContext, const cell_t *params)
983 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
986 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
988 if (!player->IsInGame())
990 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
996 if (!GetPlayerInfo(params[1], &info) || !info.customFiles[0])
1001 pContext->LocalToString(params[2], &buffer);
1002 Q_binarytohex((byte *)&info.customFiles[0], sizeof(info.customFiles[0]), buffer, params[3]);
1007 static cell_t GetServerNetStats(IPluginContext *pContext, const cell_t *params)
1009 if (iserver == NULL)
1011 return pContext->ThrowNativeError("IServer interface not supported, file a bug report.");
1017 pContext->LocalToPhysAddr(params[1], &pIn);
1018 pContext->LocalToPhysAddr(params[2], &pOut);
1019 iserver->GetNetStats(in, out);
1022 *pOut = sp_ftoc(out);
1027 static cell_t WeaponEquip(IPluginContext *pContext, const cell_t *params)
1029 static ValveCall *pCall = NULL;
1032 ValvePassInfo pass[1];
1033 InitPass(pass[0], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
1034 if (!CreateBaseCall("WeaponEquip", ValveCall_Player, NULL, pass, 1, &pCall))
1036 return pContext->ThrowNativeError("\"WeaponEquip\" not supported by this mod");
1037 } else if (!pCall) {
1038 return pContext->ThrowNativeError("\"WeaponEquip\" wrapper failed to initialize");
1043 DECODE_VALVE_PARAM(1, thisinfo, 0);
1044 DECODE_VALVE_PARAM(2, vparams, 0);
1045 FINISH_CALL_SIMPLE(NULL);
1050 static cell_t ActivateEntity(IPluginContext *pContext, const cell_t *params)
1052 static ValveCall *pCall = NULL;
1055 if (!CreateBaseCall("Activate", ValveCall_Entity, NULL, NULL, 0, &pCall))
1057 return pContext->ThrowNativeError("\"Activate\" not supported by this mod");
1061 return pContext->ThrowNativeError("\"Activate\" wrapper failed to initialize");
1066 DECODE_VALVE_PARAM(1, thisinfo, 0);
1067 FINISH_CALL_SIMPLE(NULL);
1072 static cell_t SetClientInfo(IPluginContext *pContext, const cell_t *params)
1074 if (iserver == NULL)
1076 return pContext->ThrowNativeError("IServer interface not supported, file a bug report.");
1079 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
1080 IClient *pClient = iserver->GetClient(params[1] - 1);
1082 if (player == NULL || pClient == NULL)
1084 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
1086 if (!player->IsConnected())
1088 return pContext->ThrowNativeError("Client %d is not connected", params[1]);
1091 static ValveCall *pCall = NULL;
1094 ValvePassInfo params[2];
1095 InitPass(params[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
1096 InitPass(params[1], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
1098 if (!CreateBaseCall("SetUserCvar", ValveCall_Entity, NULL, params, 2, &pCall))
1100 return pContext->ThrowNativeError("\"SetUserCvar\" not supported by this mod");
1104 return pContext->ThrowNativeError("\"SetUserCvar\" wrapper failed to initialize");
1108 /* TODO: Use UpdateUserSettings function for all engines */
1109 #if SOURCE_ENGINE == SE_DARKMESSIAH
1110 static ValveCall *pUpdateSettings = NULL;
1111 if (!pUpdateSettings)
1113 if (!CreateBaseCall("UpdateUserSettings", ValveCall_Entity, NULL, NULL, 0, &pUpdateSettings))
1115 return pContext->ThrowNativeError("\"SetUserCvar\" not supported by this mod");
1117 else if (!pUpdateSettings)
1119 return pContext->ThrowNativeError("\"SetUserCvar\" wrapper failed to initialize");
1123 static int changedOffset = -1;
1125 if (changedOffset == -1)
1127 if (!g_pGameConf->GetOffset("InfoChanged", &changedOffset))
1129 return pContext->ThrowNativeError("\"SetUserCvar\" not supported by this mod");
1134 unsigned char *CGameClient = (unsigned char *)pClient - 4;
1137 /* Not really a CBaseEntity* but this works */
1138 CBaseEntity **ebuf = (CBaseEntity **)vptr;
1139 *ebuf = (CBaseEntity *)CGameClient;
1140 DECODE_VALVE_PARAM(2, vparams, 0);
1141 DECODE_VALVE_PARAM(3, vparams, 1);
1142 FINISH_CALL_SIMPLE(NULL);
1144 #if SOURCE_ENGINE == SE_DARKMESSIAH
1145 unsigned char *args = pUpdateSettings->stk_get();
1146 *(void **)args = CGameClient;
1147 pUpdateSettings->call->Execute(args, NULL);
1148 pUpdateSettings->stk_put(args);
1150 uint8_t* changed = (uint8_t *)(CGameClient + changedOffset);
1157 sp_nativeinfo_t g_Natives[] =
1159 {"ExtinguishEntity", ExtinguishEntity},
1160 {"ForcePlayerSuicide", ForcePlayerSuicide},
1161 {"GivePlayerItem", GiveNamedItem},
1162 {"GetPlayerWeaponSlot", GetPlayerWeaponSlot},
1163 {"IgniteEntity", IgniteEntity},
1164 {"RemovePlayerItem", RemovePlayerItem},
1165 {"TeleportEntity", TeleportEntity},
1166 {"SetClientViewEntity", SetClientViewEntity},
1167 {"SetLightStyle", SetLightStyle},
1168 {"SlapPlayer", SlapPlayer},
1169 {"GetClientEyePosition", GetClientEyePosition},
1170 {"GetClientEyeAngles", GetClientEyeAngles},
1171 {"FindEntityByClassname", FindEntityByClassname},
1172 {"CreateEntityByName", CreateEntityByName},
1173 {"DispatchSpawn", DispatchSpawn},
1174 {"DispatchKeyValue", DispatchKeyValue},
1175 {"DispatchKeyValueFloat", DispatchKeyValueFloat},
1176 {"DispatchKeyValueVector", DispatchKeyValueVector},
1177 {"GetClientAimTarget", sm_GetClientAimTarget},
1178 {"SetEntityModel", sm_SetEntityModel},
1179 {"GetPlayerDecalFile", GetPlayerDecalFile},
1180 {"GetServerNetStats", GetServerNetStats},
1181 {"EquipPlayerWeapon", WeaponEquip},
1182 {"ActivateEntity", ActivateEntity},
1183 {"SetClientInfo", SetClientInfo},