3 * =============================================================================
4 * SourceMod SDKTools Extension
5 * Copyright (C) 2004-2008 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>
44 SourceHook::List<ValveCall *> g_RegCalls;
45 SourceHook::List<ICallWrapper *> g_CallWraps;
47 inline void InitPass(ValvePassInfo &info, ValveType vtype, PassType type, unsigned int flags, unsigned int decflags=0)
49 info.decflags = decflags;
56 #define START_CALL() \
57 unsigned char *vptr = pCall->stk_get();
59 #define FINISH_CALL_SIMPLE(vret) \
60 pCall->call->Execute(vptr, vret); \
63 #define ENCODE_VALVE_PARAM(num, which, vnum) \
64 if (EncodeValveParam(pContext, \
67 &pCall->which[vnum], \
74 #define DECODE_VALVE_PARAM(num, which, vnum) \
75 if (DecodeValveParam(pContext, \
78 &pCall->which[vnum], \
85 bool CreateBaseCall(const char *name,
86 ValveCallType vcalltype,
87 const ValvePassInfo *retinfo,
88 const ValvePassInfo params[],
89 unsigned int numParams,
94 if (g_pGameConf->GetOffset(name, &offset))
96 call = CreateValveVCall(offset, vcalltype, retinfo, params, numParams);
99 g_RegCalls.push_back(call);
105 if (g_pGameConf->GetMemSig(name, &addr) && addr != NULL)
107 call = CreateValveCall(addr, vcalltype, retinfo, params, numParams);
110 g_RegCalls.push_back(call);
119 static cell_t RemovePlayerItem(IPluginContext *pContext, const cell_t *params)
121 static ValveCall *pCall = NULL;
124 ValvePassInfo pass[2];
125 InitPass(pass[0], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
126 InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
127 if (!CreateBaseCall("RemovePlayerItem", ValveCall_Player, &pass[1], pass, 1, &pCall))
129 return pContext->ThrowNativeError("\"RemovePlayerItem\" not supported by this mod");
131 return pContext->ThrowNativeError("\"RemovePlayerItem\" wrapper failed to initialize");
137 DECODE_VALVE_PARAM(1, thisinfo, 0);
138 DECODE_VALVE_PARAM(2, vparams, 0);
139 FINISH_CALL_SIMPLE(&ret);
143 static cell_t GiveNamedItem(IPluginContext *pContext, const cell_t *params)
145 static ValveCall *pCall = NULL;
148 ValvePassInfo pass[3];
149 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
150 InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
151 InitPass(pass[2], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
152 if (!CreateBaseCall("GiveNamedItem", ValveCall_Player, &pass[2], pass, 2, &pCall))
154 return pContext->ThrowNativeError("\"GiveNamedItem\" not supported by this mod");
156 return pContext->ThrowNativeError("\"GiveNamedItem\" wrapper failed to initialize");
160 CBaseEntity *pEntity = NULL;
162 DECODE_VALVE_PARAM(1, thisinfo, 0);
163 DECODE_VALVE_PARAM(2, vparams, 0);
164 DECODE_VALVE_PARAM(3, vparams, 1);
165 FINISH_CALL_SIMPLE(&pEntity);
167 return gamehelpers->EntityToBCompatRef(pEntity);
170 static cell_t GetPlayerWeaponSlot(IPluginContext *pContext, const cell_t *params)
172 static ValveCall *pCall = NULL;
175 ValvePassInfo pass[2];
176 InitPass(pass[0], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
177 InitPass(pass[1], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
178 if (!CreateBaseCall("Weapon_GetSlot", ValveCall_Player, &pass[1], pass, 1, &pCall))
180 return pContext->ThrowNativeError("\"Weapon_GetSlot\" not supported by this mod");
182 return pContext->ThrowNativeError("\"Weapon_GetSlot\" wrapper failed to initialize");
186 CBaseEntity *pEntity;
188 DECODE_VALVE_PARAM(1, thisinfo, 0);
189 DECODE_VALVE_PARAM(2, vparams, 0);
190 FINISH_CALL_SIMPLE(&pEntity);
192 return gamehelpers->EntityToBCompatRef(pEntity);
195 #if SOURCE_ENGINE != SE_DARKMESSIAH
196 static cell_t IgniteEntity(IPluginContext *pContext, const cell_t *params)
198 static ValveCall *pCall = NULL;
201 ValvePassInfo pass[4];
202 InitPass(pass[0], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
203 InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
204 InitPass(pass[2], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
205 InitPass(pass[3], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
206 if (!CreateBaseCall("Ignite", ValveCall_Entity, NULL, pass, 4, &pCall))
208 return pContext->ThrowNativeError("\"Ignite\" not supported by this mod");
210 return pContext->ThrowNativeError("\"Ignite\" wrapper failed to initialize");
215 DECODE_VALVE_PARAM(1, thisinfo, 0);
216 DECODE_VALVE_PARAM(2, vparams, 0);
217 DECODE_VALVE_PARAM(3, vparams, 1);
218 DECODE_VALVE_PARAM(4, vparams, 2);
219 DECODE_VALVE_PARAM(5, vparams, 3);
220 FINISH_CALL_SIMPLE(NULL);
225 /* Dark Messiah specific version */
226 static cell_t IgniteEntity(IPluginContext *pContext, const cell_t *params)
228 static ValveCall *pCall = NULL;
231 ValvePassInfo pass[6];
232 InitPass(pass[0], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
233 InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
234 InitPass(pass[2], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
235 InitPass(pass[3], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
236 InitPass(pass[4], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
237 InitPass(pass[5], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
238 if (!CreateBaseCall("Ignite", ValveCall_Entity, NULL, pass, 6, &pCall))
240 return pContext->ThrowNativeError("\"Ignite\" not supported by this mod");
242 return pContext->ThrowNativeError("\"Ignite\" wrapper failed to initialize");
247 DECODE_VALVE_PARAM(1, thisinfo, 0);
248 DECODE_VALVE_PARAM(2, vparams, 0);
249 DECODE_VALVE_PARAM(3, vparams, 1);
250 DECODE_VALVE_PARAM(4, vparams, 2);
251 DECODE_VALVE_PARAM(5, vparams, 3);
252 /* Not sure what these params do, but they appear to be the default values */
253 *(int *)(vptr + 14) = 3;
254 *(int *)(vptr + 18) = 0;
255 FINISH_CALL_SIMPLE(NULL);
261 static cell_t ExtinguishEntity(IPluginContext *pContext, const cell_t *params)
263 static ValveCall *pCall = NULL;
266 if (!CreateBaseCall("Extinguish", ValveCall_Entity, NULL, NULL, 0, &pCall))
268 return pContext->ThrowNativeError("\"Extinguish\" not supported by this mod");
270 return pContext->ThrowNativeError("\"Extinguish\" wrapper failed to initialize");
275 DECODE_VALVE_PARAM(1, thisinfo, 0);
276 FINISH_CALL_SIMPLE(NULL);
281 static cell_t TeleportEntity(IPluginContext *pContext, const cell_t *params)
283 static ValveCall *pCall = NULL;
286 ValvePassInfo pass[3];
287 InitPass(pass[0], Valve_Vector, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL);
288 InitPass(pass[1], Valve_QAngle, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL);
289 InitPass(pass[2], Valve_Vector, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL);
290 if (!CreateBaseCall("Teleport", ValveCall_Entity, NULL, pass, 3, &pCall))
292 return pContext->ThrowNativeError("\"Teleport\" not supported by this mod");
294 return pContext->ThrowNativeError("\"Teleport\" wrapper failed to initialize");
299 DECODE_VALVE_PARAM(1, thisinfo, 0);
300 DECODE_VALVE_PARAM(2, vparams, 0);
301 DECODE_VALVE_PARAM(3, vparams, 1);
302 DECODE_VALVE_PARAM(4, vparams, 2);
303 FINISH_CALL_SIMPLE(NULL);
308 #if SOURCE_ENGINE >= SE_ORANGEBOX
309 /* :TODO: This is Team Fortress 2 specific */
310 static cell_t ForcePlayerSuicide(IPluginContext *pContext, const cell_t *params)
312 static ValveCall *pCall = NULL;
315 ValvePassInfo pass[2];
316 InitPass(pass[0], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
317 InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
318 if (!CreateBaseCall("CommitSuicide", ValveCall_Player, NULL, pass, 2, &pCall))
320 return pContext->ThrowNativeError("\"CommitSuicide\" not supported by this mod");
324 return pContext->ThrowNativeError("\"CommitSuicide\" wrapper failed to initialize");
329 DECODE_VALVE_PARAM(1, thisinfo, 0);
330 *(bool *)(vptr + 4) = false;
331 *(bool *)(vptr + 5) = false;
332 FINISH_CALL_SIMPLE(NULL);
337 static cell_t ForcePlayerSuicide(IPluginContext *pContext, const cell_t *params)
339 static ValveCall *pCall = NULL;
342 if (!CreateBaseCall("CommitSuicide", ValveCall_Player, NULL, NULL, 0, &pCall))
344 return pContext->ThrowNativeError("\"CommitSuicide\" not supported by this mod");
346 return pContext->ThrowNativeError("\"CommitSuicide\" wrapper failed to initialize");
351 DECODE_VALVE_PARAM(1, thisinfo, 0);
352 FINISH_CALL_SIMPLE(NULL);
358 static cell_t SetClientViewEntity(IPluginContext *pContext, const cell_t *params)
360 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
363 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
365 if (!player->IsInGame())
367 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
370 edict_t *pEdict = PEntityOfEntIndex(gamehelpers->ReferenceToIndex(params[1]));
371 if (!pEdict || pEdict->IsFree())
373 return pContext->ThrowNativeError("Entity %d is not valid", params[2]);
376 engine->SetView(player->GetEdict(), pEdict);
381 static SourceHook::String *g_lightstyle[MAX_LIGHTSTYLES] = {NULL};
382 static cell_t SetLightStyle(IPluginContext *pContext, const cell_t *params)
384 int style = params[1];
385 if (style >= MAX_LIGHTSTYLES)
387 return pContext->ThrowNativeError("Light style %d is invalid (range: 0-%d)", style, MAX_LIGHTSTYLES - 1);
390 if (g_lightstyle[style] == NULL)
392 /* We allocate and never free this because the Engine wants to hold onto it :\
393 * in theory we could hook light style and know whether we're supposed to free
394 * this or not on shutdown, but for ~4K of memory MAX, it doesn't seem worth it yet.
397 g_lightstyle[style] = new SourceHook::String();
401 pContext->LocalToString(params[2], &str);
403 g_lightstyle[style]->assign(str);
405 engine->LightStyle(style, g_lightstyle[style]->c_str());
410 static cell_t SlapPlayer(IPluginContext *pContext, const cell_t *params)
412 static bool s_slap_supported = false;
413 static bool s_slap_setup = false;
414 static ICallWrapper *s_teleport = NULL;
415 static int s_health_offs = 0;
416 static int s_sound_count = 0;
417 static int s_frag_offs = 0;
425 if (IsTeleportSupported())
429 if (IsGetVelocitySupported())
435 if (g_pGameConf->GetOffset("m_iHealth", &s_health_offs) && s_health_offs)
442 s_slap_supported = true;
445 if ((key = g_pGameConf->GetKeyValue("SlapSoundCount")) != NULL)
447 s_sound_count = atoi(key);
452 if (!s_slap_supported)
454 return pContext->ThrowNativeError("This function is not supported on this mod");
457 /* First check if the client is valid */
458 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
461 return pContext->ThrowNativeError("Client %d is not valid", params[1]);
462 } else if (!player->IsInGame()) {
463 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
466 edict_t *pEdict = player->GetEdict();
467 CBaseEntity *pEntity = pEdict->GetUnknown()->GetBaseEntity();
469 /* See if we should be taking away health */
470 bool should_slay = false;
473 #if SOURCE_ENGINE != SE_DARKMESSIAH
474 int *health = (int *)((char *)pEntity + s_health_offs);
476 if (*health - params[2] <= 0)
481 *health -= params[2];
484 float *health = (float *)((char *)pEntity + s_health_offs);
486 if (*health - (float)params[2] <= 0)
491 *health -= (float)params[2];
496 /* Teleport in a random direction - thank you, Mani!*/
498 GetVelocity(pEntity, &velocity, NULL);
499 velocity.x += ((rand() % 180) + 50) * (((rand() % 2) == 1) ? -1 : 1);
500 velocity.y += ((rand() % 180) + 50) * (((rand() % 2) == 1) ? -1 : 1);
501 velocity.z += rand() % 200 + 100;
502 Teleport(pEntity, NULL, NULL, &velocity);
504 /* Play a random sound */
505 if (params[3] && s_sound_count > 0)
508 const char *sound_name;
509 cell_t player_list[256], total_players = 0;
510 int maxClients = playerhelpers->GetMaxClients();
512 int r = (rand() % s_sound_count) + 1;
513 snprintf(name, sizeof(name), "SlapSound%d", r);
515 if ((sound_name = g_pGameConf->GetKeyValue(name)) != NULL)
518 for (int i=1; i<=maxClients; i++)
520 other = playerhelpers->GetGamePlayer(i);
521 if (other->IsInGame())
523 player_list[total_players++] = i;
527 const Vector & pos = pEdict->GetCollideable()->GetCollisionOrigin();
528 CellRecipientFilter rf;
529 rf.SetToReliable(true);
530 rf.Initialize(player_list, total_players);
531 engsound->EmitSound(rf, params[1], CHAN_AUTO, sound_name, VOL_NORM, ATTN_NORM, 0, PITCH_NORM, &pos);
537 const char *frag_prop = g_pGameConf->GetKeyValue("m_iFrags");
540 datamap_t *pMap = gamehelpers->GetDataMap(pEntity);
541 typedescription_t *pType = gamehelpers->FindInDataMap(pMap, frag_prop);
544 s_frag_offs = pType->fieldOffset[TD_OFFSET_NORMAL];
556 old_frags = *(int *)((char *)pEntity + s_frag_offs);
562 pluginhelpers->ClientCommand(pEdict, "kill\n");
567 *(int *)((char *)pEntity + s_frag_offs) = old_frags;
573 static cell_t GetClientEyePosition(IPluginContext *pContext, const cell_t *params)
575 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
578 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
580 if (!player->IsInGame())
582 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
586 serverClients->ClientEarPosition(player->GetEdict(), &pos);
589 pContext->LocalToPhysAddr(params[2], &addr);
590 addr[0] = sp_ftoc(pos.x);
591 addr[1] = sp_ftoc(pos.y);
592 addr[2] = sp_ftoc(pos.z);
597 static cell_t GetClientEyeAngles(IPluginContext *pContext, const cell_t *params)
599 IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(params[1]);
603 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
605 else if (!pPlayer->IsInGame())
607 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
610 edict_t *pEdict = pPlayer->GetEdict();
611 CBaseEntity *pEntity = pEdict->GetUnknown() ? pEdict->GetUnknown()->GetBaseEntity() : NULL;
613 /* We always set the angles for backwards compatibility --
614 * The original function had no return value.
617 bool got_angles = false;
621 got_angles = GetEyeAngles(pEntity, &angles);
625 pContext->LocalToPhysAddr(params[2], &addr);
626 addr[0] = sp_ftoc(angles.x);
627 addr[1] = sp_ftoc(angles.y);
628 addr[2] = sp_ftoc(angles.z);
630 return got_angles ? 1 : 0;
633 static cell_t FindEntityByClassname(IPluginContext *pContext, const cell_t *params)
635 static ValveCall *pCall = NULL;
638 ValvePassInfo pass[3];
639 InitPass(pass[0], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL|VDECODE_FLAG_ALLOWWORLD);
640 InitPass(pass[1], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
641 InitPass(pass[2], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
642 if (!CreateBaseCall("FindEntityByClassname", ValveCall_EntityList, &pass[2], pass, 2, &pCall))
644 return pContext->ThrowNativeError("\"FindEntityByClassname\" not supported by this mod");
646 return pContext->ThrowNativeError("\"FindEntityByClassname\" wrapper failed to initialize");
650 CBaseEntity *pEntity;
652 *(void **)vptr = g_EntList;
653 DECODE_VALVE_PARAM(1, vparams, 0);
654 DECODE_VALVE_PARAM(2, vparams, 1);
655 FINISH_CALL_SIMPLE(&pEntity);
657 return gamehelpers->EntityToBCompatRef(pEntity);
660 #if SOURCE_ENGINE == SE_LEFT4DEAD
661 static cell_t CreateEntityByName(IPluginContext *pContext, const cell_t *params)
663 static ValveCall *pCall = NULL;
666 ValvePassInfo pass[4];
667 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
668 InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
669 InitPass(pass[2], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
670 InitPass(pass[3], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
671 if (!CreateBaseCall("CreateEntityByName", ValveCall_Static, &pass[3], pass, 3, &pCall))
673 return pContext->ThrowNativeError("\"CreateEntityByName\" not supported by this mod");
675 return pContext->ThrowNativeError("\"CreateEntityByName\" wrapper failed to initialize");
679 CBaseEntity *pEntity = NULL;
681 DECODE_VALVE_PARAM(1, vparams, 0);
682 DECODE_VALVE_PARAM(2, vparams, 1);
683 *(bool *)(vptr + 8) = true;
684 FINISH_CALL_SIMPLE(&pEntity);
686 return gamehelpers->EntityToBCompatRef(pEntity);
689 static cell_t CreateEntityByName(IPluginContext *pContext, const cell_t *params)
691 static ValveCall *pCall = NULL;
694 ValvePassInfo pass[3];
695 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
696 InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
697 InitPass(pass[2], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
698 if (!CreateBaseCall("CreateEntityByName", ValveCall_Static, &pass[2], pass, 2, &pCall))
700 return pContext->ThrowNativeError("\"CreateEntityByName\" not supported by this mod");
702 return pContext->ThrowNativeError("\"CreateEntityByName\" wrapper failed to initialize");
706 CBaseEntity *pEntity = NULL;
708 DECODE_VALVE_PARAM(1, vparams, 0);
709 DECODE_VALVE_PARAM(2, vparams, 1);
710 FINISH_CALL_SIMPLE(&pEntity);
712 return gamehelpers->EntityToBCompatRef(pEntity);
716 static cell_t DispatchSpawn(IPluginContext *pContext, const cell_t *params)
718 static ValveCall *pCall = NULL;
721 ValvePassInfo pass[2];
722 InitPass(pass[0], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
723 InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
724 if (!CreateBaseCall("DispatchSpawn", ValveCall_Static, &pass[1], pass, 1, &pCall))
726 return pContext->ThrowNativeError("\"DispatchSpawn\" not supported by this mod");
728 return pContext->ThrowNativeError("\"DispatchSpawn\" wrapper failed to initialize");
734 DECODE_VALVE_PARAM(1, vparams, 0);
735 FINISH_CALL_SIMPLE(&ret);
737 return (ret == -1) ? 0 : 1;
740 static cell_t DispatchKeyValue(IPluginContext *pContext, const cell_t *params)
742 static ValveCall *pCall = NULL;
745 ValvePassInfo pass[3];
746 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
747 InitPass(pass[1], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
748 InitPass(pass[2], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
749 if (!CreateBaseCall("DispatchKeyValue", ValveCall_Entity, &pass[2], pass, 2, &pCall))
751 return pContext->ThrowNativeError("\"DispatchKeyValue\" not supported by this mod");
753 return pContext->ThrowNativeError("\"DispatchKeyValue\" wrapper failed to initialize");
759 DECODE_VALVE_PARAM(1, thisinfo, 0);
760 DECODE_VALVE_PARAM(2, vparams, 0);
761 DECODE_VALVE_PARAM(3, vparams, 1);
762 FINISH_CALL_SIMPLE(&ret);
767 static cell_t DispatchKeyValueFloat(IPluginContext *pContext, const cell_t *params)
769 static ValveCall *pCall = NULL;
772 ValvePassInfo pass[3];
773 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
774 InitPass(pass[1], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
775 InitPass(pass[2], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
776 if (!CreateBaseCall("DispatchKeyValueFloat", ValveCall_Entity, &pass[2], pass, 2, &pCall))
778 return pContext->ThrowNativeError("\"DispatchKeyValueFloat\" not supported by this mod");
780 return pContext->ThrowNativeError("\"DispatchKeyValueFloat\" wrapper failed to initialize");
786 DECODE_VALVE_PARAM(1, thisinfo, 0);
787 DECODE_VALVE_PARAM(2, vparams, 0);
788 DECODE_VALVE_PARAM(3, vparams, 1);
789 FINISH_CALL_SIMPLE(&ret);
794 static cell_t DispatchKeyValueVector(IPluginContext *pContext, const cell_t *params)
796 static ValveCall *pCall = NULL;
799 ValvePassInfo pass[3];
800 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
801 #if SOURCE_ENGINE >= SE_ORANGEBOX
802 InitPass(pass[1], Valve_Vector, PassType_Basic, PASSFLAG_BYVAL);
804 InitPass(pass[1], Valve_Vector, PassType_Object, PASSFLAG_BYVAL|PASSFLAG_OCTOR|PASSFLAG_OASSIGNOP);
806 InitPass(pass[2], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
807 if (!CreateBaseCall("DispatchKeyValueVector", ValveCall_Entity, &pass[2], pass, 2, &pCall))
809 return pContext->ThrowNativeError("\"DispatchKeyValueVector\" not supported by this mod");
811 return pContext->ThrowNativeError("\"DispatchKeyValueVector\" wrapper failed to initialize");
817 DECODE_VALVE_PARAM(1, thisinfo, 0);
818 DECODE_VALVE_PARAM(2, vparams, 0);
819 DECODE_VALVE_PARAM(3, vparams, 1);
820 FINISH_CALL_SIMPLE(&ret);
825 static cell_t sm_GetClientAimTarget(IPluginContext *pContext, const cell_t *params)
827 IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(params[1]);
831 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
833 else if (!pPlayer->IsInGame())
835 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
838 return GetClientAimTarget(pPlayer->GetEdict(), params[2] ? true : false);
841 static cell_t sm_SetEntityModel(IPluginContext *pContext, const cell_t *params)
843 static ValveCall *pCall = NULL;
846 ValvePassInfo pass[1];
847 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
848 if (!CreateBaseCall("SetEntityModel", ValveCall_Entity, NULL, pass, 1, &pCall))
850 return pContext->ThrowNativeError("\"SetEntityModel\" not supported by this mod");
852 return pContext->ThrowNativeError("\"SetEntityModel\" wrapper failed to initialize");
857 DECODE_VALVE_PARAM(1, thisinfo, 0);
858 DECODE_VALVE_PARAM(2, vparams, 0);
859 FINISH_CALL_SIMPLE(NULL);
864 static cell_t GetPlayerDecalFile(IPluginContext *pContext, const cell_t *params)
866 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
869 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
871 if (!player->IsInGame())
873 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
879 if (!GetPlayerInfo(params[1], &info) || !info.customFiles[0])
884 pContext->LocalToString(params[2], &buffer);
885 Q_binarytohex((byte *)&info.customFiles[0], sizeof(info.customFiles[0]), buffer, params[3]);
890 static cell_t GetServerNetStats(IPluginContext *pContext, const cell_t *params)
894 return pContext->ThrowNativeError("IServer interface not supported, file a bug report.");
900 pContext->LocalToPhysAddr(params[1], &pIn);
901 pContext->LocalToPhysAddr(params[2], &pOut);
902 iserver->GetNetStats(in, out);
905 *pOut = sp_ftoc(out);
910 static cell_t WeaponEquip(IPluginContext *pContext, const cell_t *params)
912 static ValveCall *pCall = NULL;
915 ValvePassInfo pass[1];
916 InitPass(pass[0], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
917 if (!CreateBaseCall("WeaponEquip", ValveCall_Player, NULL, pass, 1, &pCall))
919 return pContext->ThrowNativeError("\"WeaponEquip\" not supported by this mod");
921 return pContext->ThrowNativeError("\"WeaponEquip\" wrapper failed to initialize");
926 DECODE_VALVE_PARAM(1, thisinfo, 0);
927 DECODE_VALVE_PARAM(2, vparams, 0);
928 FINISH_CALL_SIMPLE(NULL);
933 static cell_t ActivateEntity(IPluginContext *pContext, const cell_t *params)
935 static ValveCall *pCall = NULL;
938 if (!CreateBaseCall("Activate", ValveCall_Entity, NULL, NULL, 0, &pCall))
940 return pContext->ThrowNativeError("\"Activate\" not supported by this mod");
944 return pContext->ThrowNativeError("\"Activate\" wrapper failed to initialize");
949 DECODE_VALVE_PARAM(1, thisinfo, 0);
950 FINISH_CALL_SIMPLE(NULL);
955 static cell_t SetClientInfo(IPluginContext *pContext, const cell_t *params)
959 return pContext->ThrowNativeError("IServer interface not supported, file a bug report.");
962 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
963 IClient *pClient = iserver->GetClient(params[1] - 1);
965 if (player == NULL || pClient == NULL)
967 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
969 if (!player->IsConnected())
971 return pContext->ThrowNativeError("Client %d is not connected", params[1]);
974 static ValveCall *pCall = NULL;
977 ValvePassInfo params[2];
978 InitPass(params[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
979 InitPass(params[1], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
981 if (!CreateBaseCall("SetUserCvar", ValveCall_Entity, NULL, params, 2, &pCall))
983 return pContext->ThrowNativeError("\"SetUserCvar\" not supported by this mod");
987 return pContext->ThrowNativeError("\"SetUserCvar\" wrapper failed to initialize");
991 /* TODO: Use UpdateUserSettings function for all engines */
992 #if SOURCE_ENGINE == SE_DARKMESSIAH
993 static ValveCall *pUpdateSettings = NULL;
994 if (!pUpdateSettings)
996 if (!CreateBaseCall("UpdateUserSettings", ValveCall_Entity, NULL, NULL, 0, &pUpdateSettings))
998 return pContext->ThrowNativeError("\"SetUserCvar\" not supported by this mod");
1000 else if (!pUpdateSettings)
1002 return pContext->ThrowNativeError("\"SetUserCvar\" wrapper failed to initialize");
1006 static int changedOffset = -1;
1008 if (changedOffset == -1)
1010 if (!g_pGameConf->GetOffset("InfoChanged", &changedOffset))
1012 return pContext->ThrowNativeError("\"SetUserCvar\" not supported by this mod");
1017 unsigned char *CGameClient = (unsigned char *)pClient - 4;
1020 /* Not really a CBaseEntity* but this works */
1021 CBaseEntity **ebuf = (CBaseEntity **)vptr;
1022 *ebuf = (CBaseEntity *)CGameClient;
1023 DECODE_VALVE_PARAM(2, vparams, 0);
1024 DECODE_VALVE_PARAM(3, vparams, 1);
1025 FINISH_CALL_SIMPLE(NULL);
1027 #if SOURCE_ENGINE == SE_DARKMESSIAH
1028 unsigned char *args = pUpdateSettings->stk_get();
1029 *(void **)args = CGameClient;
1030 pUpdateSettings->call->Execute(args, NULL);
1031 pUpdateSettings->stk_put(args);
1033 uint8_t* changed = (uint8_t *)(CGameClient + changedOffset);
1040 sp_nativeinfo_t g_Natives[] =
1042 {"ExtinguishEntity", ExtinguishEntity},
1043 {"ForcePlayerSuicide", ForcePlayerSuicide},
1044 {"GivePlayerItem", GiveNamedItem},
1045 {"GetPlayerWeaponSlot", GetPlayerWeaponSlot},
1046 {"IgniteEntity", IgniteEntity},
1047 {"RemovePlayerItem", RemovePlayerItem},
1048 {"TeleportEntity", TeleportEntity},
1049 {"SetClientViewEntity", SetClientViewEntity},
1050 {"SetLightStyle", SetLightStyle},
1051 {"SlapPlayer", SlapPlayer},
1052 {"GetClientEyePosition", GetClientEyePosition},
1053 {"GetClientEyeAngles", GetClientEyeAngles},
1054 {"FindEntityByClassname", FindEntityByClassname},
1055 {"CreateEntityByName", CreateEntityByName},
1056 {"DispatchSpawn", DispatchSpawn},
1057 {"DispatchKeyValue", DispatchKeyValue},
1058 {"DispatchKeyValueFloat", DispatchKeyValueFloat},
1059 {"DispatchKeyValueVector", DispatchKeyValueVector},
1060 {"GetClientAimTarget", sm_GetClientAimTarget},
1061 {"SetEntityModel", sm_SetEntityModel},
1062 {"GetPlayerDecalFile", GetPlayerDecalFile},
1063 {"GetServerNetStats", GetServerNetStats},
1064 {"EquipPlayerWeapon", WeaponEquip},
1065 {"ActivateEntity", ActivateEntity},
1066 {"SetClientInfo", SetClientInfo},