extensions/cstrike/natives.cpp
changeset 3519 a116c415c73e
parent 3405 f3404440ef4a
child 3530 c7421e302b11
     1.1 --- a/extensions/cstrike/natives.cpp	Sun May 27 16:51:29 2012 -0400
     1.2 +++ b/extensions/cstrike/natives.cpp	Sat May 26 18:31:23 2012 -0400
     1.3 @@ -31,8 +31,12 @@
     1.4  
     1.5  #include "extension.h"
     1.6  #include "RegNatives.h"
     1.7 +#include "forwards.h"
     1.8 +#include "util_cstrike.h"
     1.9  #include <server_class.h>
    1.10  
    1.11 +int g_iPriceOffset = -1;
    1.12 +
    1.13  #define REGISTER_NATIVE_ADDR(name, code) \
    1.14  	void *addr; \
    1.15  	if (!g_pGameConf->GetMemSig(name, &addr) || !addr) \
    1.16 @@ -44,7 +48,7 @@
    1.17  
    1.18  inline CBaseEntity *GetCBaseEntity(int num, bool isplayer)
    1.19  {
    1.20 -	edict_t *pEdict = engine->PEntityOfEntIndex(num);
    1.21 +	edict_t *pEdict = gamehelpers->EdictOfIndex(num);
    1.22  	if (!pEdict || pEdict->IsFree())
    1.23  	{
    1.24  		return NULL;
    1.25 @@ -292,35 +296,13 @@
    1.26  
    1.27  static cell_t CS_GetTranslatedWeaponAlias(IPluginContext *pContext, const cell_t *params)
    1.28  {
    1.29 -	static ICallWrapper *pWrapper = NULL;
    1.30 -
    1.31 -	if (!pWrapper)
    1.32 -	{
    1.33 -		REGISTER_NATIVE_ADDR("GetTranslatedWeaponAlias",
    1.34 -			PassInfo pass[1]; \
    1.35 -			PassInfo retpass[1]; \
    1.36 -			pass[0].flags = PASSFLAG_BYVAL; \
    1.37 -			pass[0].type = PassType_Basic; \
    1.38 -			pass[0].size = sizeof(char *); \
    1.39 -			retpass[0].flags = PASSFLAG_BYVAL; \
    1.40 -			retpass[0].type = PassType_Basic; \
    1.41 -			retpass[0].size = sizeof(char *); \
    1.42 -			pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retpass[0], pass, 1))
    1.43 -	}
    1.44 -	char *dest, *weapon;
    1.45 +	char *weapon;
    1.46 +	char *dest;
    1.47  
    1.48  	pContext->LocalToString(params[2], &dest);
    1.49  	pContext->LocalToString(params[1], &weapon);
    1.50  
    1.51 -	char *ret = new char[128];
    1.52 -
    1.53 -	unsigned char vstk[sizeof(char *)];
    1.54 -	unsigned char *vptr = vstk;
    1.55 -
    1.56 -	*(char **)vptr = weapon;
    1.57 -
    1.58 -	pWrapper->Execute(vstk, &ret);
    1.59 -
    1.60 +	const char *ret = GetTranslatedWeaponAlias(weapon);
    1.61  	strncopy(dest, ret, params[3]);
    1.62  	
    1.63  	return 1;
    1.64 @@ -329,34 +311,31 @@
    1.65  static cell_t CS_GetWeaponPrice(IPluginContext *pContext, const cell_t *params)
    1.66  {
    1.67  	//Hard code return values for weapons that dont call GetWeaponPrice and always use default value.
    1.68 -	if (params[2] == WEAPON_C4 || params[2] == WEAPON_KNIFE || params[2] == WEAPON_SHIELD)
    1.69 +	int id = GetRealWeaponID(params[2]);
    1.70 +#if SOURCE_ENGINE == SE_CSGO
    1.71 +	if (id == WEAPON_C4 || id == WEAPON_KNIFE || id == WEAPON_KNIFE_GG)
    1.72 +#else
    1.73 + 	if (id == WEAPON_C4 || id == WEAPON_KNIFE || id == WEAPON_SHIELD)
    1.74 +#endif
    1.75  		return 0;
    1.76 -	if (params[2] == WEAPON_KEVLAR)
    1.77 +	if (id == WEAPON_KEVLAR)
    1.78  		return 650;
    1.79 -	if (params[2] == WEAPON_ASSAULTSUIT)
    1.80 +	if (id == WEAPON_ASSAULTSUIT)
    1.81  		return 1000;
    1.82 -	if (params[2] == WEAPON_NIGHTVISION)
    1.83 +	if (id == WEAPON_NIGHTVISION)
    1.84  		return 1250;
    1.85 +#if SOURCE_ENGINE == SE_CSGO
    1.86 +	if (id == WEAPON_DEFUSER)
    1.87 +		return 200;
    1.88 +#endif
    1.89 +	if (id == 0)
    1.90 +		return 0;
    1.91  
    1.92 -	static ICallWrapper *pWrapper = NULL;
    1.93 -	static int priceOffset = -1;
    1.94 -	if (!pWrapper)
    1.95 +	if (g_iPriceOffset == -1)
    1.96  	{
    1.97 -		REGISTER_NATIVE_ADDR("GetWeaponInfo",
    1.98 -			PassInfo pass[1]; \
    1.99 -			PassInfo retpass[1]; \
   1.100 -			pass[0].flags = PASSFLAG_BYVAL; \
   1.101 -			pass[0].type = PassType_Basic; \
   1.102 -			pass[0].size = sizeof(int); \
   1.103 -			retpass[0].flags = PASSFLAG_BYVAL; \
   1.104 -			retpass[0].type = PassType_Basic; \
   1.105 -			retpass[0].size = sizeof(void *); \
   1.106 -			pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retpass[0], pass, 1))
   1.107 -	}
   1.108 -	if (priceOffset == -1)
   1.109 -	{
   1.110 -		if (!g_pGameConf->GetOffset("WeaponPrice", &priceOffset))
   1.111 +		if (!g_pGameConf->GetOffset("WeaponPrice", &g_iPriceOffset))
   1.112  		{
   1.113 +			g_iPriceOffset = -1;
   1.114  			return pContext->ThrowNativeError("Failed to get WeaponPrice offset");
   1.115  		}
   1.116  	}
   1.117 @@ -367,30 +346,31 @@
   1.118  		return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
   1.119  	}
   1.120  
   1.121 -	void *info;
   1.122 -
   1.123 -	unsigned char vstk[sizeof(int)];
   1.124 -	unsigned char *vptr = vstk;
   1.125 -
   1.126 -	*(int *)vptr = params[2];
   1.127 -
   1.128 -	pWrapper->Execute(vstk, &info);
   1.129 -
   1.130 +	void *info = GetWeaponInfo(params[2]);
   1.131  	if (!info)
   1.132  		return pContext->ThrowNativeError("Failed to get weaponinfo");
   1.133  
   1.134 -	int price = *(int *)((intptr_t)info + priceOffset);
   1.135 +	int price = *(int *)((intptr_t)info + g_iPriceOffset);
   1.136  
   1.137  	if (params[3] || weaponNameOffset == -1)
   1.138  		return price;
   1.139  
   1.140  	const char *weapon_name = (const char *)((intptr_t)info + weaponNameOffset);
   1.141  
   1.142 +#if SOURCE_ENGINE == SE_CSGO
   1.143 +	return CallPriceForwardCSGO(params[1], weapon_name, price);
   1.144 +#else
   1.145  	return CallPriceForward(params[1], weapon_name, price);
   1.146 +#endif
   1.147  }
   1.148  
   1.149  static cell_t CS_GetClientClanTag(IPluginContext *pContext, const cell_t *params)
   1.150  {
   1.151 +#if SOURCE_ENGINE == SE_CSGO
   1.152 +	size_t len;
   1.153 +	pContext->StringToLocalUTF8(params[2], params[3], "", &len);
   1.154 +	return len;
   1.155 +#else
   1.156  	static void *addr;
   1.157  	if (!addr)
   1.158  	{
   1.159 @@ -426,10 +406,14 @@
   1.160  	pContext->StringToLocalUTF8(params[2], params[3], src, &len);
   1.161  
   1.162  	return len;
   1.163 +#endif
   1.164  }
   1.165  
   1.166  static cell_t CS_SetClientClanTag(IPluginContext *pContext, const cell_t *params)
   1.167  {
   1.168 +#if SOURCE_ENGINE == SE_CSGO
   1.169 +	return 0;
   1.170 +#else
   1.171  	static ICallWrapper *pWrapper = NULL;
   1.172  
   1.173  	if (!pWrapper)
   1.174 @@ -464,6 +448,16 @@
   1.175  	pWrapper->Execute(vstk, NULL);
   1.176  
   1.177  	return 1;
   1.178 +#endif
   1.179 +}
   1.180 +
   1.181 +static cell_t CS_AliasToWeaponID(IPluginContext *pContext, const cell_t *params)
   1.182 +{
   1.183 +	char *weapon;
   1.184 +
   1.185 +	pContext->LocalToString(params[1], &weapon);
   1.186 +
   1.187 +	return GetFakeWeaponID(AliasToWeaponID(weapon));
   1.188  }
   1.189  
   1.190  sp_nativeinfo_t g_CSNatives[] = 
   1.191 @@ -476,6 +470,7 @@
   1.192  	{"CS_GetWeaponPrice",			CS_GetWeaponPrice},
   1.193  	{"CS_GetClientClanTag",			CS_GetClientClanTag},
   1.194  	{"CS_SetClientClanTag",			CS_SetClientClanTag},
   1.195 +	{"CS_AliasToWeaponID",			CS_AliasToWeaponID},
   1.196  	{NULL,							NULL}
   1.197  };
   1.198