Add hack to not throw error on FEBC not found if not attempted to be found (nobug, r=asherkin).
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>
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 #if SOURCE_ENGINE == SE_CSGO
145 static cell_t GiveNamedItem(IPluginContext *pContext, const cell_t *params)
147 static ValveCall *pCall = NULL;
150 ValvePassInfo pass[5];
151 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
152 InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
153 InitPass(pass[2], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
154 InitPass(pass[3], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
155 InitPass(pass[4], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
156 if (!CreateBaseCall("GiveNamedItem", ValveCall_Player, &pass[4], pass, 4, &pCall))
158 return pContext->ThrowNativeError("\"GiveNamedItem\" not supported by this mod");
160 return pContext->ThrowNativeError("\"GiveNamedItem\" wrapper failed to initialize");
164 CBaseEntity *pEntity = NULL;
166 DECODE_VALVE_PARAM(1, thisinfo, 0);
167 DECODE_VALVE_PARAM(2, vparams, 0);
168 DECODE_VALVE_PARAM(3, vparams, 1);
169 *(CEconItemView **)(vptr + 12) = NULL;
170 *(bool *)(vptr + 16) = false;
171 FINISH_CALL_SIMPLE(&pEntity);
173 return gamehelpers->EntityToBCompatRef(pEntity);
176 static cell_t GiveNamedItem(IPluginContext *pContext, const cell_t *params)
178 static ValveCall *pCall = NULL;
181 ValvePassInfo pass[3];
182 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
183 InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
184 InitPass(pass[2], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
185 if (!CreateBaseCall("GiveNamedItem", ValveCall_Player, &pass[2], pass, 2, &pCall))
187 return pContext->ThrowNativeError("\"GiveNamedItem\" not supported by this mod");
189 return pContext->ThrowNativeError("\"GiveNamedItem\" wrapper failed to initialize");
193 CBaseEntity *pEntity = NULL;
195 DECODE_VALVE_PARAM(1, thisinfo, 0);
196 DECODE_VALVE_PARAM(2, vparams, 0);
197 DECODE_VALVE_PARAM(3, vparams, 1);
198 FINISH_CALL_SIMPLE(&pEntity);
200 return gamehelpers->EntityToBCompatRef(pEntity);
204 static cell_t GetPlayerWeaponSlot(IPluginContext *pContext, const cell_t *params)
206 static ValveCall *pCall = NULL;
209 ValvePassInfo pass[2];
210 InitPass(pass[0], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
211 InitPass(pass[1], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
212 if (!CreateBaseCall("Weapon_GetSlot", ValveCall_Player, &pass[1], pass, 1, &pCall))
214 return pContext->ThrowNativeError("\"Weapon_GetSlot\" not supported by this mod");
216 return pContext->ThrowNativeError("\"Weapon_GetSlot\" wrapper failed to initialize");
220 CBaseEntity *pEntity;
222 DECODE_VALVE_PARAM(1, thisinfo, 0);
223 DECODE_VALVE_PARAM(2, vparams, 0);
224 FINISH_CALL_SIMPLE(&pEntity);
226 return gamehelpers->EntityToBCompatRef(pEntity);
229 #if SOURCE_ENGINE != SE_DARKMESSIAH
230 static cell_t IgniteEntity(IPluginContext *pContext, const cell_t *params)
232 static ValveCall *pCall = NULL;
235 #if SOURCE_ENGINE == SE_SDK2013
236 if (!strcmp(g_pSM->GetGameFolderName(), "nmrih"))
238 ValvePassInfo pass[6];
239 InitPass(pass[0], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
240 InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
241 InitPass(pass[2], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
242 InitPass(pass[3], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
243 InitPass(pass[4], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
244 InitPass(pass[5], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
245 if (!CreateBaseCall("Ignite", ValveCall_Entity, NULL, pass, 6, &pCall))
247 return pContext->ThrowNativeError("\"Ignite\" not supported by this mod");
250 return pContext->ThrowNativeError("\"Ignite\" wrapper failed to initialize");
256 ValvePassInfo pass[4];
257 InitPass(pass[0], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
258 InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
259 InitPass(pass[2], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
260 InitPass(pass[3], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
261 if (!CreateBaseCall("Ignite", ValveCall_Entity, NULL, pass, 4, &pCall))
263 return pContext->ThrowNativeError("\"Ignite\" not supported by this mod");
266 return pContext->ThrowNativeError("\"Ignite\" wrapper failed to initialize");
272 DECODE_VALVE_PARAM(1, thisinfo, 0);
273 DECODE_VALVE_PARAM(2, vparams, 0);
274 DECODE_VALVE_PARAM(3, vparams, 1);
275 DECODE_VALVE_PARAM(4, vparams, 2);
276 DECODE_VALVE_PARAM(5, vparams, 3);
278 #if SOURCE_ENGINE == SE_SDK2013
279 if (!strcmp(g_pSM->GetGameFolderName(), "nmrih"))
281 *(int *) (vptr + 14) = 0;
282 *(int *) (vptr + 18) = 0;
286 FINISH_CALL_SIMPLE(NULL);
291 /* Dark Messiah specific version */
292 static cell_t IgniteEntity(IPluginContext *pContext, const cell_t *params)
294 static ValveCall *pCall = NULL;
297 ValvePassInfo pass[6];
298 InitPass(pass[0], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
299 InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
300 InitPass(pass[2], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
301 InitPass(pass[3], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
302 InitPass(pass[4], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
303 InitPass(pass[5], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
304 if (!CreateBaseCall("Ignite", ValveCall_Entity, NULL, pass, 6, &pCall))
306 return pContext->ThrowNativeError("\"Ignite\" not supported by this mod");
308 return pContext->ThrowNativeError("\"Ignite\" wrapper failed to initialize");
313 DECODE_VALVE_PARAM(1, thisinfo, 0);
314 DECODE_VALVE_PARAM(2, vparams, 0);
315 DECODE_VALVE_PARAM(3, vparams, 1);
316 DECODE_VALVE_PARAM(4, vparams, 2);
317 DECODE_VALVE_PARAM(5, vparams, 3);
318 /* Not sure what these params do, but they appear to be the default values */
319 *(int *)(vptr + 14) = 3;
320 *(int *)(vptr + 18) = 0;
321 FINISH_CALL_SIMPLE(NULL);
327 static cell_t ExtinguishEntity(IPluginContext *pContext, const cell_t *params)
329 static ValveCall *pCall = NULL;
332 if (!CreateBaseCall("Extinguish", ValveCall_Entity, NULL, NULL, 0, &pCall))
334 return pContext->ThrowNativeError("\"Extinguish\" not supported by this mod");
336 return pContext->ThrowNativeError("\"Extinguish\" wrapper failed to initialize");
341 DECODE_VALVE_PARAM(1, thisinfo, 0);
342 FINISH_CALL_SIMPLE(NULL);
347 static cell_t TeleportEntity(IPluginContext *pContext, const cell_t *params)
349 static ValveCall *pCall = NULL;
352 ValvePassInfo pass[3];
353 InitPass(pass[0], Valve_Vector, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL);
354 InitPass(pass[1], Valve_QAngle, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL);
355 InitPass(pass[2], Valve_Vector, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL);
356 if (!CreateBaseCall("Teleport", ValveCall_Entity, NULL, pass, 3, &pCall))
358 return pContext->ThrowNativeError("\"Teleport\" not supported by this mod");
360 return pContext->ThrowNativeError("\"Teleport\" wrapper failed to initialize");
365 DECODE_VALVE_PARAM(1, thisinfo, 0);
366 DECODE_VALVE_PARAM(2, vparams, 0);
367 DECODE_VALVE_PARAM(3, vparams, 1);
368 DECODE_VALVE_PARAM(4, vparams, 2);
369 FINISH_CALL_SIMPLE(NULL);
374 #if SOURCE_ENGINE >= SE_ORANGEBOX
375 /* :TODO: This is Team Fortress 2 specific */
376 static cell_t ForcePlayerSuicide(IPluginContext *pContext, const cell_t *params)
378 static ValveCall *pCall = NULL;
381 ValvePassInfo pass[2];
382 InitPass(pass[0], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
383 InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
384 if (!CreateBaseCall("CommitSuicide", ValveCall_Player, NULL, pass, 2, &pCall))
386 return pContext->ThrowNativeError("\"CommitSuicide\" not supported by this mod");
390 return pContext->ThrowNativeError("\"CommitSuicide\" wrapper failed to initialize");
395 DECODE_VALVE_PARAM(1, thisinfo, 0);
396 *(bool *)(vptr + 4) = false;
397 *(bool *)(vptr + 5) = false;
398 FINISH_CALL_SIMPLE(NULL);
403 static cell_t ForcePlayerSuicide(IPluginContext *pContext, const cell_t *params)
405 static ValveCall *pCall = NULL;
408 if (!CreateBaseCall("CommitSuicide", ValveCall_Player, NULL, NULL, 0, &pCall))
410 return pContext->ThrowNativeError("\"CommitSuicide\" not supported by this mod");
412 return pContext->ThrowNativeError("\"CommitSuicide\" wrapper failed to initialize");
417 DECODE_VALVE_PARAM(1, thisinfo, 0);
418 FINISH_CALL_SIMPLE(NULL);
424 static cell_t SetClientViewEntity(IPluginContext *pContext, const cell_t *params)
426 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
429 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
431 if (!player->IsInGame())
433 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
436 edict_t *pEdict = PEntityOfEntIndex(gamehelpers->ReferenceToIndex(params[2]));
437 if (!pEdict || pEdict->IsFree())
439 return pContext->ThrowNativeError("Entity %d is not valid", params[2]);
442 #if SOURCE_ENGINE == SE_DOTA
443 engine->SetView(params[1], params[2]);
445 engine->SetView(player->GetEdict(), pEdict);
451 static SourceHook::String *g_lightstyle[MAX_LIGHTSTYLES] = {NULL};
452 static cell_t SetLightStyle(IPluginContext *pContext, const cell_t *params)
454 int style = params[1];
455 if (style >= MAX_LIGHTSTYLES)
457 return pContext->ThrowNativeError("Light style %d is invalid (range: 0-%d)", style, MAX_LIGHTSTYLES - 1);
460 if (g_lightstyle[style] == NULL)
462 /* We allocate and never free this because the Engine wants to hold onto it :\
463 * in theory we could hook light style and know whether we're supposed to free
464 * this or not on shutdown, but for ~4K of memory MAX, it doesn't seem worth it yet.
467 g_lightstyle[style] = new SourceHook::String();
471 pContext->LocalToString(params[2], &str);
473 g_lightstyle[style]->assign(str);
475 engine->LightStyle(style, g_lightstyle[style]->c_str());
480 static cell_t SlapPlayer(IPluginContext *pContext, const cell_t *params)
482 static bool s_slap_supported = false;
483 static bool s_slap_setup = false;
484 static ICallWrapper *s_teleport = NULL;
485 static int s_health_offs = 0;
486 static int s_sound_count = 0;
487 static int s_frag_offs = 0;
495 if (IsTeleportSupported())
499 if (IsGetVelocitySupported())
505 if (g_pGameConf->GetOffset("m_iHealth", &s_health_offs) && s_health_offs)
512 s_slap_supported = true;
515 if ((key = g_pGameConf->GetKeyValue("SlapSoundCount")) != NULL)
517 s_sound_count = atoi(key);
522 if (!s_slap_supported)
524 return pContext->ThrowNativeError("This function is not supported on this mod");
527 /* First check if the client is valid */
528 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
531 return pContext->ThrowNativeError("Client %d is not valid", params[1]);
532 } else if (!player->IsInGame()) {
533 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
536 edict_t *pEdict = player->GetEdict();
537 CBaseEntity *pEntity = pEdict->GetUnknown()->GetBaseEntity();
539 /* See if we should be taking away health */
540 bool should_slay = false;
543 #if SOURCE_ENGINE != SE_DARKMESSIAH
544 int *health = (int *)((char *)pEntity + s_health_offs);
546 if (*health - params[2] <= 0)
551 *health -= params[2];
554 float *health = (float *)((char *)pEntity + s_health_offs);
556 if (*health - (float)params[2] <= 0)
561 *health -= (float)params[2];
566 /* Teleport in a random direction - thank you, Mani!*/
568 GetVelocity(pEntity, &velocity, NULL);
569 velocity.x += ((rand() % 180) + 50) * (((rand() % 2) == 1) ? -1 : 1);
570 velocity.y += ((rand() % 180) + 50) * (((rand() % 2) == 1) ? -1 : 1);
571 velocity.z += rand() % 200 + 100;
572 Teleport(pEntity, NULL, NULL, &velocity);
574 /* Play a random sound */
575 if (params[3] && s_sound_count > 0)
578 const char *sound_name;
579 cell_t player_list[256], total_players = 0;
580 int maxClients = playerhelpers->GetMaxClients();
582 int r = (rand() % s_sound_count) + 1;
583 snprintf(name, sizeof(name), "SlapSound%d", r);
585 if ((sound_name = g_pGameConf->GetKeyValue(name)) != NULL)
588 for (int i=1; i<=maxClients; i++)
590 other = playerhelpers->GetGamePlayer(i);
591 if (other->IsInGame())
593 player_list[total_players++] = i;
597 const Vector & pos = pEdict->GetCollideable()->GetCollisionOrigin();
598 CellRecipientFilter rf;
599 rf.SetToReliable(true);
600 rf.Initialize(player_list, total_players);
601 #if SOURCE_ENGINE == SE_CSS || SOURCE_ENGINE == SE_HL2DM || SOURCE_ENGINE == SE_DODS || SOURCE_ENGINE == SE_SDK2013 || SOURCE_ENGINE == SE_TF2
602 engsound->EmitSound(rf, params[1], CHAN_AUTO, sound_name, VOL_NORM, ATTN_NORM, 0, PITCH_NORM, 0, &pos);
603 #elif SOURCE_ENGINE < SE_PORTAL2
604 engsound->EmitSound(rf, params[1], CHAN_AUTO, sound_name, VOL_NORM, ATTN_NORM, 0, PITCH_NORM, &pos);
606 engsound->EmitSound(rf, params[1], CHAN_AUTO, sound_name, -1, sound_name, VOL_NORM, ATTN_NORM, 0, 0, PITCH_NORM, &pos);
613 const char *frag_prop = g_pGameConf->GetKeyValue("m_iFrags");
616 datamap_t *pMap = gamehelpers->GetDataMap(pEntity);
617 sm_datatable_info_t info;
618 if (gamehelpers->FindDataMapInfo(pMap, frag_prop, &info))
620 s_frag_offs = info.actual_offset;
632 old_frags = *(int *)((char *)pEntity + s_frag_offs);
638 #if SOURCE_ENGINE == SE_DOTA
639 engine->ClientCommand(params[1], "kill\n");
641 pluginhelpers->ClientCommand(pEdict, "kill\n");
647 *(int *)((char *)pEntity + s_frag_offs) = old_frags;
653 static cell_t GetClientEyePosition(IPluginContext *pContext, const cell_t *params)
655 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
658 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
660 if (!player->IsInGame())
662 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
666 #if SOURCE_ENGINE == SE_DOTA
667 serverClients->ClientEarPosition(params[1], &pos);
669 serverClients->ClientEarPosition(player->GetEdict(), &pos);
673 pContext->LocalToPhysAddr(params[2], &addr);
674 addr[0] = sp_ftoc(pos.x);
675 addr[1] = sp_ftoc(pos.y);
676 addr[2] = sp_ftoc(pos.z);
681 static cell_t GetClientEyeAngles(IPluginContext *pContext, const cell_t *params)
683 IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(params[1]);
687 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
689 else if (!pPlayer->IsInGame())
691 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
694 edict_t *pEdict = pPlayer->GetEdict();
695 CBaseEntity *pEntity = pEdict->GetUnknown() ? pEdict->GetUnknown()->GetBaseEntity() : NULL;
697 /* We always set the angles for backwards compatibility --
698 * The original function had no return value.
701 bool got_angles = false;
705 got_angles = GetEyeAngles(pEntity, &angles);
709 pContext->LocalToPhysAddr(params[2], &addr);
710 addr[0] = sp_ftoc(angles.x);
711 addr[1] = sp_ftoc(angles.y);
712 addr[2] = sp_ftoc(angles.z);
714 return got_angles ? 1 : 0;
717 #if SOURCE_ENGINE >= SE_ORANGEBOX
718 static cell_t NativeFindEntityByClassname(IPluginContext *pContext, const cell_t *params)
721 CBaseEntity *pEntity;
725 pEntity = (CBaseEntity *)servertools->FirstEntity();
729 pEntity = gamehelpers->ReferenceToEntity(params[1]);
732 return pContext->ThrowNativeError("Entity %d (%d) is invalid",
733 gamehelpers->ReferenceToIndex(params[1]),
736 pEntity = (CBaseEntity *)servertools->NextEntity(pEntity);
739 // it's tough to find a good ent these days
745 pContext->LocalToString(params[2], &searchname);
747 const char *classname;
750 static int offset = -1;
753 sm_datatable_info_t info;
754 if (!gamehelpers->FindDataMapInfo(gamehelpers->GetDataMap(pEntity), "m_iClassname", &info))
759 offset = info.actual_offset;
766 if ((s = *(string_t *)((uint8_t *)pEntity + offset)) == NULL_STRING)
768 pEntity = (CBaseEntity *)servertools->NextEntity(pEntity);
772 classname = STRING(s);
774 lastletterpos = strlen(searchname) - 1;
775 if (searchname[lastletterpos] == '*')
777 if (strncasecmp(searchname, classname, lastletterpos) == 0)
779 return gamehelpers->EntityToBCompatRef(pEntity);
782 else if (strcasecmp(searchname, classname) == 0)
784 return gamehelpers->EntityToBCompatRef(pEntity);
787 pEntity = (CBaseEntity *)servertools->NextEntity(pEntity);
794 static cell_t FindEntityByClassname(IPluginContext *pContext, const cell_t *params)
796 static ValveCall *pCall = NULL;
797 static bool bProbablyNoFEBC = false;
799 #if SOURCE_ENGINE >= SE_ORANGEBOX
802 return NativeFindEntityByClassname(pContext, params);
808 ValvePassInfo pass[3];
809 InitPass(pass[0], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL|VDECODE_FLAG_ALLOWWORLD);
810 InitPass(pass[1], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
811 InitPass(pass[2], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
815 if (!CreateBaseCall("FindEntityByClassname", ValveCall_EntityList, &pass[2], pass, 2, &pCall))
817 g_pSM->Format(error, sizeof(error), "\"FindEntityByClassname\" not supported by this mod");
819 g_pSM->Format(error, sizeof(error), "\"FindEntityByClassname\" wrapper failed to initialize");
822 if (error[0] != '\0')
824 #if SOURCE_ENGINE >= SE_ORANGEBOX
825 if (!bProbablyNoFEBC)
827 bProbablyNoFEBC = true;
829 // CreateBaseCall above abstracts all of the gamedata logic, but we need to know if the key was even found.
830 // We don't want to log an error if key isn't present (knowing falling back to native method), only throw
831 // error if signature/symbol was not found.
833 if (g_pGameConf->GetMemSig("FindEntityByClassname", &dummy))
835 g_pSM->LogError(myself, "%s, falling back to IServerTools method.", error);
838 return NativeFindEntityByClassname(pContext, params);
840 return pContext->ThrowNativeError("%s", error);
845 CBaseEntity *pEntity;
847 *(void **)vptr = g_EntList;
848 DECODE_VALVE_PARAM(1, vparams, 0);
849 DECODE_VALVE_PARAM(2, vparams, 1);
850 FINISH_CALL_SIMPLE(&pEntity);
852 return gamehelpers->EntityToBCompatRef(pEntity);
855 #if SOURCE_ENGINE >= SE_ORANGEBOX
856 static cell_t CreateEntityByName(IPluginContext *pContext, const cell_t *params)
858 if (!g_pSM->IsMapRunning())
860 return pContext->ThrowNativeError("Cannot create new entity when no map is running");
864 pContext->LocalToString(params[1], &classname);
865 CBaseEntity *pEntity = (CBaseEntity *)servertools->CreateEntityByName(classname);
866 return gamehelpers->EntityToBCompatRef(pEntity);
869 static cell_t CreateEntityByName(IPluginContext *pContext, const cell_t *params)
871 if (!g_pSM->IsMapRunning())
873 return pContext->ThrowNativeError("Cannot create new entity when no map is running");
876 static ValveCall *pCall = NULL;
879 ValvePassInfo pass[3];
880 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
881 InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
882 InitPass(pass[2], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
883 if (!CreateBaseCall("CreateEntityByName", ValveCall_Static, &pass[2], pass, 2, &pCall))
885 return pContext->ThrowNativeError("\"CreateEntityByName\" not supported by this mod");
887 return pContext->ThrowNativeError("\"CreateEntityByName\" wrapper failed to initialize");
891 CBaseEntity *pEntity = NULL;
893 DECODE_VALVE_PARAM(1, vparams, 0);
894 DECODE_VALVE_PARAM(2, vparams, 1);
895 FINISH_CALL_SIMPLE(&pEntity);
897 return gamehelpers->EntityToBCompatRef(pEntity);
901 #if SOURCE_ENGINE >= SE_ORANGEBOX
902 static cell_t DispatchSpawn(IPluginContext *pContext, const cell_t *params)
904 CBaseEntity *pEntity = gamehelpers->ReferenceToEntity(params[1]);
907 return pContext->ThrowNativeError("Entity %d (%d) is invalid", gamehelpers->ReferenceToIndex(params[1]), params[1]);
910 servertools->DispatchSpawn(pEntity);
915 static cell_t DispatchSpawn(IPluginContext *pContext, const cell_t *params)
917 static ValveCall *pCall = NULL;
920 ValvePassInfo pass[2];
921 InitPass(pass[0], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
922 InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
923 if (!CreateBaseCall("DispatchSpawn", ValveCall_Static, &pass[1], pass, 1, &pCall))
925 return pContext->ThrowNativeError("\"DispatchSpawn\" not supported by this mod");
927 return pContext->ThrowNativeError("\"DispatchSpawn\" wrapper failed to initialize");
933 DECODE_VALVE_PARAM(1, vparams, 0);
934 FINISH_CALL_SIMPLE(&ret);
936 return (ret == -1) ? 0 : 1;
940 #if SOURCE_ENGINE >= SE_ORANGEBOX
941 static cell_t DispatchKeyValue(IPluginContext *pContext, const cell_t *params)
943 CBaseEntity *pEntity = gamehelpers->ReferenceToEntity(params[1]);
946 return pContext->ThrowNativeError("Entity %d (%d) is invalid", gamehelpers->ReferenceToIndex(params[1]), params[1]);
951 pContext->LocalToString(params[2], &key);
952 pContext->LocalToString(params[3], &value);
954 return (servertools->SetKeyValue(pEntity, key, value) ? 1 : 0);
957 static cell_t DispatchKeyValue(IPluginContext *pContext, const cell_t *params)
959 static ValveCall *pCall = NULL;
962 ValvePassInfo pass[3];
963 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
964 InitPass(pass[1], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
965 InitPass(pass[2], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
966 if (!CreateBaseCall("DispatchKeyValue", ValveCall_Entity, &pass[2], pass, 2, &pCall))
968 return pContext->ThrowNativeError("\"DispatchKeyValue\" not supported by this mod");
970 return pContext->ThrowNativeError("\"DispatchKeyValue\" wrapper failed to initialize");
976 DECODE_VALVE_PARAM(1, thisinfo, 0);
977 DECODE_VALVE_PARAM(2, vparams, 0);
978 DECODE_VALVE_PARAM(3, vparams, 1);
979 FINISH_CALL_SIMPLE(&ret);
985 #if SOURCE_ENGINE >= SE_ORANGEBOX
986 static cell_t DispatchKeyValueFloat(IPluginContext *pContext, const cell_t *params)
988 CBaseEntity *pEntity = gamehelpers->ReferenceToEntity(params[1]);
991 return pContext->ThrowNativeError("Entity %d (%d) is invalid", gamehelpers->ReferenceToIndex(params[1]), params[1]);
995 pContext->LocalToString(params[2], &key);
997 float value = sp_ctof(params[3]);
999 return (servertools->SetKeyValue(pEntity, key, value) ? 1 : 0);
1002 static cell_t DispatchKeyValueFloat(IPluginContext *pContext, const cell_t *params)
1004 static ValveCall *pCall = NULL;
1007 ValvePassInfo pass[3];
1008 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
1009 InitPass(pass[1], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
1010 InitPass(pass[2], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
1011 if (!CreateBaseCall("DispatchKeyValueFloat", ValveCall_Entity, &pass[2], pass, 2, &pCall))
1013 return pContext->ThrowNativeError("\"DispatchKeyValueFloat\" not supported by this mod");
1014 } else if (!pCall) {
1015 return pContext->ThrowNativeError("\"DispatchKeyValueFloat\" wrapper failed to initialize");
1021 DECODE_VALVE_PARAM(1, thisinfo, 0);
1022 DECODE_VALVE_PARAM(2, vparams, 0);
1023 DECODE_VALVE_PARAM(3, vparams, 1);
1024 FINISH_CALL_SIMPLE(&ret);
1030 #if SOURCE_ENGINE >= SE_ORANGEBOX
1031 static cell_t DispatchKeyValueVector(IPluginContext *pContext, const cell_t *params)
1033 CBaseEntity *pEntity = gamehelpers->ReferenceToEntity(params[1]);
1036 return pContext->ThrowNativeError("Entity %d (%d) is invalid", gamehelpers->ReferenceToIndex(params[1]), params[1]);
1040 pContext->LocalToString(params[2], &key);
1043 pContext->LocalToPhysAddr(params[3], &vec);
1045 const Vector *v = new Vector(
1050 return (servertools->SetKeyValue(pEntity, key, *v) ? 1 : 0);
1053 static cell_t DispatchKeyValueVector(IPluginContext *pContext, const cell_t *params)
1055 static ValveCall *pCall = NULL;
1058 ValvePassInfo pass[3];
1059 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
1060 #if SOURCE_ENGINE >= SE_ORANGEBOX
1061 InitPass(pass[1], Valve_Vector, PassType_Basic, PASSFLAG_BYVAL);
1063 InitPass(pass[1], Valve_Vector, PassType_Object, PASSFLAG_BYVAL|PASSFLAG_OCTOR|PASSFLAG_OASSIGNOP);
1065 InitPass(pass[2], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
1066 if (!CreateBaseCall("DispatchKeyValueVector", ValveCall_Entity, &pass[2], pass, 2, &pCall))
1068 return pContext->ThrowNativeError("\"DispatchKeyValueVector\" not supported by this mod");
1069 } else if (!pCall) {
1070 return pContext->ThrowNativeError("\"DispatchKeyValueVector\" wrapper failed to initialize");
1076 DECODE_VALVE_PARAM(1, thisinfo, 0);
1077 DECODE_VALVE_PARAM(2, vparams, 0);
1078 DECODE_VALVE_PARAM(3, vparams, 1);
1079 FINISH_CALL_SIMPLE(&ret);
1085 static cell_t sm_GetClientAimTarget(IPluginContext *pContext, const cell_t *params)
1087 IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(params[1]);
1091 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
1093 else if (!pPlayer->IsInGame())
1095 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
1098 return GetClientAimTarget(pPlayer->GetEdict(), params[2] ? true : false);
1101 static cell_t sm_SetEntityModel(IPluginContext *pContext, const cell_t *params)
1103 static ValveCall *pCall = NULL;
1106 ValvePassInfo pass[1];
1107 InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
1108 if (!CreateBaseCall("SetEntityModel", ValveCall_Entity, NULL, pass, 1, &pCall))
1110 return pContext->ThrowNativeError("\"SetEntityModel\" not supported by this mod");
1111 } else if (!pCall) {
1112 return pContext->ThrowNativeError("\"SetEntityModel\" wrapper failed to initialize");
1117 DECODE_VALVE_PARAM(1, thisinfo, 0);
1118 DECODE_VALVE_PARAM(2, vparams, 0);
1119 FINISH_CALL_SIMPLE(NULL);
1124 static cell_t GetPlayerDecalFile(IPluginContext *pContext, const cell_t *params)
1126 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
1129 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
1131 if (!player->IsInGame())
1133 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
1139 if (!GetPlayerInfo(params[1], &info) || !info.customFiles[0])
1144 pContext->LocalToString(params[2], &buffer);
1145 Q_binarytohex((byte *)&info.customFiles[0], sizeof(info.customFiles[0]), buffer, params[3]);
1150 static cell_t GetPlayerJingleFile(IPluginContext *pContext, const cell_t *params)
1152 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
1155 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
1157 if (!player->IsInGame())
1159 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
1165 if (!GetPlayerInfo(params[1], &info) || !info.customFiles[1])
1170 pContext->LocalToString(params[2], &buffer);
1171 Q_binarytohex((byte *)&info.customFiles[1], sizeof(info.customFiles[1]), buffer, params[3]);
1176 static cell_t GetServerNetStats(IPluginContext *pContext, const cell_t *params)
1178 if (iserver == NULL)
1180 return pContext->ThrowNativeError("IServer interface not supported, file a bug report.");
1186 pContext->LocalToPhysAddr(params[1], &pIn);
1187 pContext->LocalToPhysAddr(params[2], &pOut);
1188 iserver->GetNetStats(in, out);
1191 *pOut = sp_ftoc(out);
1196 static cell_t WeaponEquip(IPluginContext *pContext, const cell_t *params)
1198 static ValveCall *pCall = NULL;
1201 ValvePassInfo pass[1];
1202 InitPass(pass[0], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
1203 if (!CreateBaseCall("WeaponEquip", ValveCall_Player, NULL, pass, 1, &pCall))
1205 return pContext->ThrowNativeError("\"WeaponEquip\" not supported by this mod");
1206 } else if (!pCall) {
1207 return pContext->ThrowNativeError("\"WeaponEquip\" wrapper failed to initialize");
1212 DECODE_VALVE_PARAM(1, thisinfo, 0);
1213 DECODE_VALVE_PARAM(2, vparams, 0);
1214 FINISH_CALL_SIMPLE(NULL);
1219 static cell_t ActivateEntity(IPluginContext *pContext, const cell_t *params)
1221 static ValveCall *pCall = NULL;
1224 if (!CreateBaseCall("Activate", ValveCall_Entity, NULL, NULL, 0, &pCall))
1226 return pContext->ThrowNativeError("\"Activate\" not supported by this mod");
1230 return pContext->ThrowNativeError("\"Activate\" wrapper failed to initialize");
1235 DECODE_VALVE_PARAM(1, thisinfo, 0);
1236 FINISH_CALL_SIMPLE(NULL);
1241 static cell_t SetClientInfo(IPluginContext *pContext, const cell_t *params)
1243 if (iserver == NULL)
1245 return pContext->ThrowNativeError("IServer interface not supported, file a bug report.");
1248 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
1249 IClient *pClient = iserver->GetClient(params[1] - 1);
1251 if (player == NULL || pClient == NULL)
1253 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
1255 if (!player->IsConnected())
1257 return pContext->ThrowNativeError("Client %d is not connected", params[1]);
1260 static ValveCall *pCall = NULL;
1263 ValvePassInfo params[2];
1264 InitPass(params[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
1265 InitPass(params[1], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
1267 if (!CreateBaseCall("SetUserCvar", ValveCall_Entity, NULL, params, 2, &pCall))
1269 return pContext->ThrowNativeError("\"SetUserCvar\" not supported by this mod");
1273 return pContext->ThrowNativeError("\"SetUserCvar\" wrapper failed to initialize");
1277 /* TODO: Use UpdateUserSettings function for all engines */
1278 #if SOURCE_ENGINE == SE_DARKMESSIAH
1279 static ValveCall *pUpdateSettings = NULL;
1280 if (!pUpdateSettings)
1282 if (!CreateBaseCall("UpdateUserSettings", ValveCall_Entity, NULL, NULL, 0, &pUpdateSettings))
1284 return pContext->ThrowNativeError("\"SetUserCvar\" not supported by this mod");
1286 else if (!pUpdateSettings)
1288 return pContext->ThrowNativeError("\"SetUserCvar\" wrapper failed to initialize");
1292 static int changedOffset = -1;
1294 if (changedOffset == -1)
1296 if (!g_pGameConf->GetOffset("InfoChanged", &changedOffset))
1298 return pContext->ThrowNativeError("\"SetUserCvar\" not supported by this mod");
1303 unsigned char *CGameClient = (unsigned char *)pClient - 4;
1306 /* Not really a CBaseEntity* but this works */
1307 CBaseEntity **ebuf = (CBaseEntity **)vptr;
1308 *ebuf = (CBaseEntity *)CGameClient;
1309 DECODE_VALVE_PARAM(2, vparams, 0);
1310 DECODE_VALVE_PARAM(3, vparams, 1);
1311 FINISH_CALL_SIMPLE(NULL);
1313 #if SOURCE_ENGINE == SE_DARKMESSIAH
1314 unsigned char *args = pUpdateSettings->stk_get();
1315 *(void **)args = CGameClient;
1316 pUpdateSettings->call->Execute(args, NULL);
1317 pUpdateSettings->stk_put(args);
1319 uint8_t* changed = (uint8_t *)(CGameClient + changedOffset);
1326 static cell_t GetPlayerResourceEntity(IPluginContext *pContext, const cell_t *params)
1328 if (gamehelpers->GetHandleEntity(g_ResourceEntity) != NULL)
1330 return g_ResourceEntity.GetEntryIndex();
1336 static cell_t GivePlayerAmmo(IPluginContext *pContext, const cell_t *params)
1338 static ValveCall *pCall = NULL;
1341 ValvePassInfo pass[3];
1342 InitPass(pass[0], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
1343 InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
1344 InitPass(pass[2], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
1345 if (!CreateBaseCall("GiveAmmo", ValveCall_Player, &pass[0], pass, 3, &pCall))
1347 return pContext->ThrowNativeError("\"GiveAmmo\" not supported by this mod");
1348 } else if (!pCall) {
1349 return pContext->ThrowNativeError("\"GiveAmmo\" wrapper failed to initialize");
1355 DECODE_VALVE_PARAM(1, thisinfo, 0);
1356 DECODE_VALVE_PARAM(2, vparams, 0);
1357 DECODE_VALVE_PARAM(3, vparams, 1);
1358 DECODE_VALVE_PARAM(4, vparams, 2);
1359 FINISH_CALL_SIMPLE(&ammoGiven);
1364 sp_nativeinfo_t g_Natives[] =
1366 {"ExtinguishEntity", ExtinguishEntity},
1367 {"ForcePlayerSuicide", ForcePlayerSuicide},
1368 {"GivePlayerItem", GiveNamedItem},
1369 {"GetPlayerWeaponSlot", GetPlayerWeaponSlot},
1370 {"IgniteEntity", IgniteEntity},
1371 {"RemovePlayerItem", RemovePlayerItem},
1372 {"TeleportEntity", TeleportEntity},
1373 {"SetClientViewEntity", SetClientViewEntity},
1374 {"SetLightStyle", SetLightStyle},
1375 {"SlapPlayer", SlapPlayer},
1376 {"GetClientEyePosition", GetClientEyePosition},
1377 {"GetClientEyeAngles", GetClientEyeAngles},
1378 {"FindEntityByClassname", FindEntityByClassname},
1379 {"CreateEntityByName", CreateEntityByName},
1380 {"DispatchSpawn", DispatchSpawn},
1381 {"DispatchKeyValue", DispatchKeyValue},
1382 {"DispatchKeyValueFloat", DispatchKeyValueFloat},
1383 {"DispatchKeyValueVector", DispatchKeyValueVector},
1384 {"GetClientAimTarget", sm_GetClientAimTarget},
1385 {"SetEntityModel", sm_SetEntityModel},
1386 {"GetPlayerDecalFile", GetPlayerDecalFile},
1387 {"GetPlayerJingleFile", GetPlayerJingleFile},
1388 {"GetServerNetStats", GetServerNetStats},
1389 {"EquipPlayerWeapon", WeaponEquip},
1390 {"ActivateEntity", ActivateEntity},
1391 {"SetClientInfo", SetClientInfo},
1392 {"GetPlayerResourceEntity", GetPlayerResourceEntity},
1393 {"GivePlayerAmmo", GivePlayerAmmo},