Update CS:GO team score gamedata.
3 * =============================================================================
4 * SourceMod Counter-Strike:Source 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>.
32 #include "extension.h"
33 #include "RegNatives.h"
35 #include "util_cstrike.h"
36 #include <server_class.h>
37 #include <iplayerinfo.h>
39 int g_iPriceOffset = -1;
41 #define REGISTER_NATIVE_ADDR(name, code) \
43 if (!g_pGameConf->GetMemSig(name, &addr) || !addr) \
45 return pContext->ThrowNativeError("Failed to locate function"); \
48 g_RegNatives.Register(pWrapper);
50 #define GET_MEMSIG(name) \
51 if (!g_pGameConf->GetMemSig(name, &addr) || !addr) \
53 return pContext->ThrowNativeError("Failed to locate function"); \
56 inline CBaseEntity *GetCBaseEntity(int num, bool isplayer)
58 edict_t *pEdict = gamehelpers->EdictOfIndex(num);
59 if (!pEdict || pEdict->IsFree())
64 if (num > 0 && num <= playerhelpers->GetMaxClients())
66 IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(pEdict);
67 if (!pPlayer || !pPlayer->IsConnected())
78 if ((pUnk=pEdict->GetUnknown()) == NULL)
83 return pUnk->GetBaseEntity();
86 bool UTIL_FindDataTable(SendTable *pTable, const char *name, sm_sendprop_info_t *info, unsigned int offset)
89 int props = pTable->GetNumProps();
93 for (int i=0; i<props; i++)
95 prop = pTable->GetProp(i);
97 if ((table = prop->GetDataTable()) != NULL)
99 pname = table->GetName();
100 if (pname && strcmp(name, pname) == 0)
103 info->actual_offset = offset + info->prop->GetOffset();
107 if (UTIL_FindDataTable(table,
110 offset + prop->GetOffset())
121 //Taken From Sourcemod Core
122 unsigned int strncopy(char *dest, const char *src, size_t count)
130 while ((*src) && (--count))
136 return (dest - start);
139 static cell_t CS_RespawnPlayer(IPluginContext *pContext, const cell_t *params)
141 static ICallWrapper *pWrapper = NULL;
144 REGISTER_NATIVE_ADDR("RoundRespawn",
145 pWrapper = g_pBinTools->CreateCall(addr, CallConv_ThisCall, NULL, NULL, 0));
148 CBaseEntity *pEntity;
149 if (!(pEntity=GetCBaseEntity(params[1], true)))
151 return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
153 pWrapper->Execute(&pEntity, NULL);
158 static cell_t CS_SwitchTeam(IPluginContext *pContext, const cell_t *params)
160 #if SOURCE_ENGINE != SE_CSGO || !defined(WIN32)
161 static ICallWrapper *pWrapper = NULL;
164 REGISTER_NATIVE_ADDR("SwitchTeam",
166 pass[0].flags = PASSFLAG_BYVAL; \
167 pass[0].size = sizeof(int); \
168 pass[0].type = PassType_Basic; \
169 pWrapper = g_pBinTools->CreateCall(addr, CallConv_ThisCall, NULL, pass, 1))
172 CBaseEntity *pEntity;
173 if (!(pEntity=GetCBaseEntity(params[1], true)))
175 return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
178 unsigned char vstk[sizeof(CBaseEntity *) + sizeof(int)];
179 unsigned char *vptr = vstk;
181 *(CBaseEntity **)vptr = pEntity;
182 vptr += sizeof(CBaseEntity *);
183 *(int *)vptr = params[2];
184 pWrapper->Execute(vstk, NULL);
186 if (g_pSDKTools == NULL)
188 return pContext->ThrowNativeError("SDKTools interface not found. SwitchTeam native disabled.");
191 static void *addr = NULL;
194 GET_MEMSIG("SwitchTeam");
197 void *gamerules = g_pSDKTools->GetGameRules();
198 if (gamerules == NULL)
200 return pContext->ThrowNativeError("GameRules not available. SwitchTeam native disabled.");
203 CBaseEntity *pEntity;
204 if (!(pEntity=GetCBaseEntity(params[1], true)))
206 return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
209 int team = params[2];
224 static cell_t CS_DropWeapon(IPluginContext *pContext, const cell_t *params)
226 static ICallWrapper *pWrapper = NULL;
229 REGISTER_NATIVE_ADDR("CSWeaponDrop",
231 pass[0].flags = PASSFLAG_BYVAL; \
232 pass[0].type = PassType_Basic; \
233 pass[0].size = sizeof(CBaseEntity *); \
234 pass[1].flags = PASSFLAG_BYVAL; \
235 pass[1].type = PassType_Basic; \
236 pass[1].size = sizeof(bool); \
237 pass[2].flags = PASSFLAG_BYVAL; \
238 pass[2].type = PassType_Basic; \
239 pass[2].size = sizeof(bool); \
240 pWrapper = g_pBinTools->CreateCall(addr, CallConv_ThisCall, NULL, pass, 3))
243 CBaseEntity *pEntity;
244 if (!(pEntity = GetCBaseEntity(params[1], true)))
246 return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
249 CBaseEntity *pWeapon;
250 if (!(pWeapon = GetCBaseEntity(params[2], false)))
252 return pContext->ThrowNativeError("Weapon index %d is not valid", params[2]);
255 //Psychonic is awesome for this
256 sm_sendprop_info_t spi;
257 IServerUnknown *pUnk = (IServerUnknown *)pWeapon;
258 IServerNetworkable *pNet = pUnk->GetNetworkable();
260 if (!UTIL_FindDataTable(pNet->GetServerClass()->m_pTable, "DT_WeaponCSBase", &spi, 0))
261 return pContext->ThrowNativeError("Entity index %d is not a weapon", params[2]);
263 if (!gamehelpers->FindSendPropInfo("CBaseCombatWeapon", "m_hOwnerEntity", &spi))
264 return pContext->ThrowNativeError("Invalid entity index %d for weapon", params[2]);
266 CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pWeapon + spi.actual_offset);
267 if (params[1] != hndl.GetEntryIndex() || hndl != ((IServerEntity *)pEntity)->GetRefEHandle())
268 return pContext->ThrowNativeError("Weapon %d is not owned by client %d", params[2], params[1]);
270 if (params[4] == 1 && g_pCSWeaponDropDetoured)
271 g_pIgnoreCSWeaponDropDetour = true;
273 unsigned char vstk[sizeof(CBaseEntity *) * 2 + sizeof(bool) * 2];
274 unsigned char *vptr = vstk;
276 // <psychonic> first one is always false. second is true to toss, false to just drop
277 *(CBaseEntity **)vptr = pEntity;
278 vptr += sizeof(CBaseEntity *);
279 *(CBaseEntity **)vptr = pWeapon;
280 vptr += sizeof(CBaseEntity *);
281 *(bool *)vptr = false;
282 vptr += sizeof(bool);
283 *(bool *)vptr = (params[3]) ? true : false;
285 pWrapper->Execute(vstk, NULL);
290 static cell_t CS_TerminateRound(IPluginContext *pContext, const cell_t *params)
292 if (g_pSDKTools == NULL)
294 return pContext->ThrowNativeError("SDKTools interface not found. TerminateRound native disabled.");
296 else if (g_pSDKTools->GetInterfaceVersion() < 2)
298 //<psychonic> THIS ISN'T DA LIMBO STICK. LOW IS BAD
299 return pContext->ThrowNativeError("SDKTools interface is outdated. TerminateRound native disabled.");
302 void *gamerules = g_pSDKTools->GetGameRules();
303 if (gamerules == NULL)
305 return pContext->ThrowNativeError("GameRules not available. TerminateRound native disabled.");
308 #if SOURCE_ENGINE != SE_CSGO || !defined(WIN32)
309 static ICallWrapper *pWrapper = NULL;
313 REGISTER_NATIVE_ADDR("TerminateRound",
315 pass[0].flags = PASSFLAG_BYVAL; \
316 pass[0].type = PassType_Basic; \
317 pass[0].size = sizeof(float); \
318 pass[1].flags = PASSFLAG_BYVAL; \
319 pass[1].type = PassType_Basic; \
320 pass[1].size = sizeof(int); \
321 pWrapper = g_pBinTools->CreateCall(addr, CallConv_ThisCall, NULL, pass, 2))
324 if (params[3] == 1 && g_pTerminateRoundDetoured)
325 g_pIgnoreTerminateDetour = true;
327 unsigned char vstk[sizeof(void *) + sizeof(float)+ sizeof(int)];
328 unsigned char *vptr = vstk;
330 *(void **)vptr = gamerules;
331 vptr += sizeof(void *);
332 *(float *)vptr = sp_ctof(params[1]);
333 vptr += sizeof(float);
334 *(int*)vptr = params[2];
336 pWrapper->Execute(vstk, NULL);
338 static void *addr = NULL;
342 GET_MEMSIG("TerminateRound");
345 if (params[3] == 1 && g_pTerminateRoundDetoured)
346 g_pIgnoreTerminateDetour = true;
348 float delay = sp_ctof(params[1]);
349 int reason = params[2];
350 signed int unknown = 1;//We might want to find what this is?
364 static cell_t CS_WeaponIDToAlias(IPluginContext *pContext, const cell_t *params)
366 if (!IsValidWeaponID(params[1]))
367 return pContext->ThrowNativeError("Invalid WeaponID passed for this game");
371 pContext->LocalToString(params[2], &dest);
373 const char *ret = WeaponIDToAlias(params[1]);
380 return strncopy(dest, ret, params[3]);
383 static cell_t CS_GetTranslatedWeaponAlias(IPluginContext *pContext, const cell_t *params)
388 pContext->LocalToString(params[2], &dest);
389 pContext->LocalToString(params[1], &weapon);
391 const char *ret = GetTranslatedWeaponAlias(weapon);
392 strncopy(dest, ret, params[3]);
397 static cell_t CS_GetWeaponPrice(IPluginContext *pContext, const cell_t *params)
399 if (!IsValidWeaponID(params[2]))
400 return pContext->ThrowNativeError("Invalid WeaponID passed for this game");
402 int id = GetRealWeaponID(params[2]);
404 //Hard code return values for weapons that dont call GetWeaponPrice and always use default value.
405 #if SOURCE_ENGINE == SE_CSGO
406 if (id == WEAPON_C4 || id == WEAPON_KNIFE || id == WEAPON_KNIFE_GG)
409 if (id == WEAPON_C4 || id == WEAPON_KNIFE || id == WEAPON_SHIELD)
411 else if (id == WEAPON_KEVLAR)
413 else if (id == WEAPON_ASSAULTSUIT)
416 else if (id == WEAPON_NIGHTVISION)
418 #if SOURCE_ENGINE == SE_CSGO
419 else if (id == WEAPON_DEFUSER)
423 void *info = GetWeaponInfo(id);
427 return pContext->ThrowNativeError("Failed to get weaponinfo");
430 CBaseEntity *pEntity;
431 if (!(pEntity = GetCBaseEntity(params[1], true)))
433 return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
435 #if SOURCE_ENGINE == SE_CSGO
436 static ICallWrapper *pWrapper = NULL;
440 REGISTER_NATIVE_ADDR("GetAttributeInt",
443 pass[0].flags = PASSFLAG_BYVAL; \
444 pass[0].type = PassType_Basic; \
445 pass[0].size = sizeof(char *); \
446 pass[1].flags = PASSFLAG_BYVAL; \
447 pass[1].type = PassType_Basic; \
448 pass[1].size = sizeof(CEconItemView *); \
449 ret.flags = PASSFLAG_BYVAL; \
450 ret.type = PassType_Basic; \
451 ret.size = sizeof(int); \
452 pWrapper = g_pBinTools->CreateCall(addr, CallConv_ThisCall, &ret, pass, 2))
455 // Get a CEconItemView for the m4
456 // Found in CCSPlayer::HandleCommand_Buy_Internal
457 // Linux a1 - CCSPlayer *pEntity, v5 - Player Team, a3 - ItemLoadoutSlot -1 use default loadoutslot:
458 // v4 = *(int (__cdecl **)(_DWORD, _DWORD, _DWORD))(*(_DWORD *)(a1 + 9492) + 36); // offset 9
459 // v6 = v4(a1 + 9492, v5, a3);
460 // Windows v5 - CCSPlayer *pEntity a4 - ItemLoadoutSlot -1 use default loadoutslot:
461 // v8 = (*(int (__stdcall **)(_DWORD, int))(*(_DWORD *)(v5 + 9472) + 32))(*(_DWORD *)(v5 + 760), a4); // offset 8
462 // The function is CCSPlayerInventory::GetItemInLoadout(int, int)
463 // We can pass NULL view to the GetAttribute to use default loadoutslot.
464 // We only really care about m4a1/m4a4 as price differs between them
465 // thisPtrOffset = 9472/9492
467 static ICallWrapper *pGetView = NULL;
468 static int thisPtrOffset = -1;
469 CEconItemView *view = NULL;
475 void *pHandleCommandBuy = NULL;
476 if (!g_pGameConf->GetOffset("GetItemInLoadout", &offset) || offset == -1)
478 smutils->LogError(myself, "Failed to get GetItemInLoadout offset. Reverting to NULL ItemView");
480 else if (!g_pGameConf->GetOffset("CCSPlayerInventoryOffset", &byteOffset) || byteOffset == -1)
482 smutils->LogError(myself, "Failed to get CCSPlayerInventoryOffset offset. Reverting to NULL ItemView");
484 else if (!g_pGameConf->GetMemSig("HandleCommand_Buy_Internal", &pHandleCommandBuy) || !pHandleCommandBuy)
486 smutils->LogError(myself, "Failed to get HandleCommand_Buy_Internal function. Reverting to NULL ItemView");
490 thisPtrOffset = *(int *)((intptr_t)pHandleCommandBuy + byteOffset);
494 pass[0].flags = PASSFLAG_BYVAL;
495 pass[0].type = PassType_Basic;
496 pass[0].size = sizeof(int);
497 pass[1].flags = PASSFLAG_BYVAL;
498 pass[1].type = PassType_Basic;
499 pass[1].size = sizeof(int);
501 ret.flags = PASSFLAG_BYVAL;
502 ret.type = PassType_Basic;
503 ret.size = sizeof(void *);
505 pGetView = g_pBinTools->CreateVCall(offset, 0, 0, &ret, pass, 2);
506 g_RegNatives.Register(pGetView);
510 IPlayerInfo *playerinfo = playerhelpers->GetGamePlayer(params[1])->GetPlayerInfo();
511 if(pGetView && thisPtrOffset != -1 && playerinfo)
513 //If the gun isnt an M4 we ignore this as M4 is the only one that differs in price based on Loadout item.
514 int iLoadoutSlot = -1;
520 unsigned char vstk_view[sizeof(void *) + sizeof(int) * 2];
521 unsigned char *vptr_view = vstk_view;
523 *(void **)vptr_view = (void *)((intptr_t)pEntity + thisPtrOffset);
524 vptr_view += sizeof(void *);
525 *(int *)vptr_view = playerinfo->GetTeamIndex();
526 vptr_view += sizeof(int);
527 *(int *)vptr_view = iLoadoutSlot;
529 pGetView->Execute(vstk_view, &view);
532 unsigned char vstk[sizeof(void *) * 2 + sizeof(char *)];
533 unsigned char *vptr = vstk;
535 *(void **)vptr = info;
536 vptr += sizeof(void *);
537 *(const char **)vptr = "in game price";
538 vptr += sizeof(const char *);
539 *(CEconItemView **)vptr = view;
542 pWrapper->Execute(vstk, &price);
544 if (g_iPriceOffset == -1)
546 if (!g_pGameConf->GetOffset("WeaponPrice", &g_iPriceOffset))
549 return pContext->ThrowNativeError("Failed to get WeaponPrice offset");
553 int price = *(int *)((intptr_t)info + g_iPriceOffset);
556 if (params[3] || weaponNameOffset == -1)
559 const char *weapon_name = (const char *)((intptr_t)info + weaponNameOffset);
561 return CallPriceForward(params[1], weapon_name, price);
564 static cell_t CS_GetClientClanTag(IPluginContext *pContext, const cell_t *params)
569 if (!g_pGameConf->GetMemSig("SetClanTag", &addr) || !addr)
571 return pContext->ThrowNativeError("Failed to locate function");
575 CBaseEntity *pEntity;
576 if (!(pEntity = GetCBaseEntity(params[1], true)))
578 return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
581 static int tagOffsetOffset = -1;
582 static int tagOffset;
584 if (tagOffsetOffset == -1)
586 if (!g_pGameConf->GetOffset("ClanTagOffset", &tagOffsetOffset))
588 tagOffsetOffset = -1;
589 return pContext->ThrowNativeError("Unable to find ClanTagOffset gamedata");
592 tagOffset = *(int *)((intptr_t)addr + tagOffsetOffset);
597 const char *src = (char *)((intptr_t)pEntity + tagOffset);
598 pContext->StringToLocalUTF8(params[2], params[3], src, &len);
603 static cell_t CS_SetClientClanTag(IPluginContext *pContext, const cell_t *params)
605 static ICallWrapper *pWrapper = NULL;
609 REGISTER_NATIVE_ADDR("SetClanTag",
611 pass[0].flags = PASSFLAG_BYVAL; \
612 pass[0].type = PassType_Basic; \
613 pass[0].size = sizeof(CBaseEntity *); \
614 pass[1].flags = PASSFLAG_BYVAL; \
615 pass[1].type = PassType_Basic; \
616 pass[1].size = sizeof(char *); \
617 pWrapper = g_pBinTools->CreateCall(addr, CallConv_ThisCall, NULL, pass, 2))
620 CBaseEntity *pEntity;
621 if (!(pEntity = GetCBaseEntity(params[1], true)))
623 return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
627 pContext->LocalToString(params[2], &szNewTag);
629 unsigned char vstk[sizeof(CBaseEntity *) + sizeof(char *)];
630 unsigned char *vptr = vstk;
632 *(CBaseEntity **)vptr = pEntity;
633 vptr += sizeof(CBaseEntity *);
634 *(char **)vptr = szNewTag;
636 pWrapper->Execute(vstk, NULL);
641 static cell_t CS_AliasToWeaponID(IPluginContext *pContext, const cell_t *params)
645 pContext->LocalToString(params[1], &weapon);
647 #if SOURCE_ENGINE == SE_CSGO
648 if (strstr(weapon, "usp_silencer") != NULL)
650 return SMCSWeapon_HKP2000;
652 else if(strstr(weapon, "cz75a") != NULL)
654 return SMCSWeapon_P250;
658 int id = GetFakeWeaponID(AliasToWeaponID(weapon));
660 if (!IsValidWeaponID(id))
661 return SMCSWeapon_NONE;
666 static cell_t CS_SetTeamScore(IPluginContext *pContext, const cell_t *params)
668 if (g_pSDKTools == NULL)
670 smutils->LogError(myself, "SDKTools interface not found. CS_SetTeamScore native disabled.");
672 else if (g_pSDKTools->GetInterfaceVersion() < 2)
674 //<psychonic> THIS ISN'T DA LIMBO STICK. LOW IS BAD
675 return pContext->ThrowNativeError("SDKTools interface is outdated. CS_SetTeamScore native disabled.");
681 if (!g_pGameConf->GetMemSig("CheckWinLimit", &addr) || !addr)
683 return pContext->ThrowNativeError("Failed to locate CheckWinLimit function");
687 static int ctTeamOffsetOffset = -1;
688 static int ctTeamOffset;
690 static int tTeamOffsetOffset = -1;
691 static int tTeamOffset;
693 if (ctTeamOffsetOffset == -1)
695 if (!g_pGameConf->GetOffset("CTTeamScoreOffset", &ctTeamOffsetOffset))
697 ctTeamOffsetOffset = -1;
698 return pContext->ThrowNativeError("Unable to find CTTeamOffset gamedata");
701 ctTeamOffset = *(int *)((intptr_t)addr + ctTeamOffsetOffset);
704 if (tTeamOffsetOffset == -1)
706 if (!g_pGameConf->GetOffset("TTeamScoreOffset", &tTeamOffsetOffset))
708 tTeamOffsetOffset = -1;
709 return pContext->ThrowNativeError("Unable to find CTTeamOffset gamedata");
712 tTeamOffset = *(int *)((intptr_t)addr + tTeamOffsetOffset);
715 void *gamerules = g_pSDKTools->GetGameRules();
716 if (gamerules == NULL)
718 return pContext->ThrowNativeError("GameRules not available. CS_SetTeamScore native disabled.");
723 *(int16_t *)((intptr_t)gamerules+ctTeamOffset) = params[2];
726 else if (params[1] == 2)
728 *(int16_t *)((intptr_t)gamerules+tTeamOffset) = params[2];
731 return pContext->ThrowNativeError("Invalid team index passed (%i).", params[1]);
734 static cell_t CS_GetTeamScore(IPluginContext *pContext, const cell_t *params)
736 if (g_pSDKTools == NULL)
738 smutils->LogError(myself, "SDKTools interface not found. CS_GetTeamScore native disabled.");
740 else if (g_pSDKTools->GetInterfaceVersion() < 2)
742 //<psychonic> THIS ISN'T DA LIMBO STICK. LOW IS BAD
743 return pContext->ThrowNativeError("SDKTools interface is outdated. CS_GetTeamScore native disabled.");
749 if (!g_pGameConf->GetMemSig("CheckWinLimit", &addr) || !addr)
751 return pContext->ThrowNativeError("Failed to locate CheckWinLimit function");
755 static int ctTeamOffsetOffset = -1;
756 static int ctTeamOffset;
758 static int tTeamOffsetOffset = -1;
759 static int tTeamOffset;
761 if (ctTeamOffsetOffset == -1)
763 if (!g_pGameConf->GetOffset("CTTeamScoreOffset", &ctTeamOffsetOffset))
765 ctTeamOffsetOffset = -1;
766 return pContext->ThrowNativeError("Unable to find CTTeamOffset gamedata");
769 ctTeamOffset = *(int *)((intptr_t)addr + ctTeamOffsetOffset);
772 if (tTeamOffsetOffset == -1)
774 if (!g_pGameConf->GetOffset("TTeamScoreOffset", &tTeamOffsetOffset))
776 tTeamOffsetOffset = -1;
777 return pContext->ThrowNativeError("Unable to find CTTeamOffset gamedata");
780 tTeamOffset = *(int *)((intptr_t)addr + tTeamOffsetOffset);
783 void *gamerules = g_pSDKTools->GetGameRules();
784 if (gamerules == NULL)
786 return pContext->ThrowNativeError("GameRules not available. CS_GetTeamScore native disabled.");
791 return *(int16_t *)((intptr_t)gamerules+ctTeamOffset);
793 else if (params[1] == 2)
795 return *(int16_t *)((intptr_t)gamerules+tTeamOffset);
797 return pContext->ThrowNativeError("Invalid team index passed (%i).", params[1]);
800 static cell_t CS_IsValidWeaponID(IPluginContext *pContext, const cell_t *params)
802 return IsValidWeaponID(params[1]) ? 1 : 0;
805 template <typename T>
806 static inline T *GetPlayerVarAddressOrError(const char *pszGamedataName, IPluginContext *pContext, CBaseEntity *pPlayerEntity)
808 char szBaseName[128];
809 g_pSM->Format(szBaseName, sizeof(szBaseName), "%sBase", pszGamedataName);
811 const char *pszBaseVar = g_pGameConf->GetKeyValue(szBaseName);
812 if (pszBaseVar == NULL)
814 pContext->ThrowNativeError("Failed to locate %s key in gamedata", szBaseName);
818 int interimOffset = 0;
819 sm_sendprop_info_t info;
820 if (gamehelpers->FindSendPropInfo("CCSPlayer", pszBaseVar, &info))
822 interimOffset = info.actual_offset;
826 datamap_t *pMap = gamehelpers->GetDataMap(pPlayerEntity);
827 typedescription_t *td = gamehelpers->FindInDataMap(pMap, pszBaseVar);
830 #if SOURCE_ENGINE >= SE_LEFT4DEAD
831 interimOffset = td->fieldOffset;
833 interimOffset = td->fieldOffset[TD_OFFSET_NORMAL];
838 if (interimOffset == 0)
840 pContext->ThrowNativeError("Failed to find property \"%s\" on player.", pszBaseVar);
846 if (!g_pGameConf->GetOffset(pszGamedataName, &tempOffset))
848 pContext->ThrowNativeError("Failed to locate %s offset in gamedata", pszGamedataName);
852 return (T *)((intptr_t)pPlayerEntity + interimOffset + tempOffset);
855 template <typename T>
856 static inline cell_t GetPlayerVar(IPluginContext *pContext, const cell_t *params, const char *varName)
858 CBaseEntity *pPlayer = GetCBaseEntity(params[1], true);
861 return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
864 T *pVar = GetPlayerVarAddressOrError<T>(varName, pContext, pPlayer);
867 return (cell_t)*pVar;
873 template <typename T>
874 static inline cell_t SetPlayerVar(IPluginContext *pContext, const cell_t *params, const char *varName)
876 CBaseEntity *pPlayer = GetCBaseEntity(params[1], true);
879 return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
882 T *pVar = GetPlayerVarAddressOrError<T>(varName, pContext, pPlayer);
885 *pVar = (T)params[2];
891 static cell_t CS_SetMVPCount(IPluginContext *pContext, const cell_t *params)
893 return SetPlayerVar<int>(pContext, params, "MVPs");
896 static cell_t CS_GetMVPCount(IPluginContext *pContext, const cell_t *params)
898 return GetPlayerVar<int>(pContext, params, "MVPs");
901 static cell_t CS_SetClientContributionScore(IPluginContext *pContext, const cell_t *params)
903 #if SOURCE_ENGINE == SE_CSGO
904 return SetPlayerVar<int>(pContext, params, "CScore");
906 return pContext->ThrowNativeError("SetClientContributionScore is not supported on this game");
910 static cell_t CS_GetClientContributionScore(IPluginContext *pContext, const cell_t *params)
912 #if SOURCE_ENGINE == SE_CSGO
913 return GetPlayerVar<int>(pContext, params, "CScore");
915 return pContext->ThrowNativeError("GetClientContributionScore is not supported on this game");
919 static cell_t CS_SetClientAssists(IPluginContext *pContext, const cell_t *params)
921 #if SOURCE_ENGINE == SE_CSGO
922 return SetPlayerVar<int>(pContext, params, "Assists");
924 return pContext->ThrowNativeError("SetClientAssists is not supported on this game");
928 static cell_t CS_GetClientAssists(IPluginContext *pContext, const cell_t *params)
930 #if SOURCE_ENGINE == SE_CSGO
931 return GetPlayerVar<int>(pContext, params, "Assists");
933 return pContext->ThrowNativeError("GetClientAssists is not supported on this game");
937 static cell_t CS_UpdateClientModel(IPluginContext *pContext, const cell_t *params)
939 static ICallWrapper *pWrapper = NULL;
942 REGISTER_NATIVE_ADDR("SetModelFromClass",
943 pWrapper = g_pBinTools->CreateCall(addr, CallConv_ThisCall, NULL, NULL, 0));
946 CBaseEntity *pEntity;
947 if (!(pEntity=GetCBaseEntity(params[1], true)))
949 return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
952 pWrapper->Execute(&pEntity, NULL);
956 sp_nativeinfo_t g_CSNatives[] =
958 {"CS_RespawnPlayer", CS_RespawnPlayer},
959 {"CS_SwitchTeam", CS_SwitchTeam},
960 {"CS_DropWeapon", CS_DropWeapon},
961 {"CS_TerminateRound", CS_TerminateRound},
962 {"CS_GetTranslatedWeaponAlias", CS_GetTranslatedWeaponAlias},
963 {"CS_GetWeaponPrice", CS_GetWeaponPrice},
964 {"CS_GetClientClanTag", CS_GetClientClanTag},
965 {"CS_SetClientClanTag", CS_SetClientClanTag},
966 {"CS_AliasToWeaponID", CS_AliasToWeaponID},
967 {"CS_GetTeamScore", CS_GetTeamScore},
968 {"CS_SetTeamScore", CS_SetTeamScore},
969 {"CS_GetMVPCount", CS_GetMVPCount},
970 {"CS_SetMVPCount", CS_SetMVPCount},
971 {"CS_WeaponIDToAlias", CS_WeaponIDToAlias},
972 {"CS_GetClientContributionScore", CS_GetClientContributionScore},
973 {"CS_SetClientContributionScore", CS_SetClientContributionScore},
974 {"CS_GetClientAssists", CS_GetClientAssists},
975 {"CS_SetClientAssists", CS_SetClientAssists},
976 {"CS_UpdateClientModel", CS_UpdateClientModel},
977 {"CS_IsValidWeaponID", CS_IsValidWeaponID},