extensions/cstrike/util_cstrike.cpp
author Drifter <drifter01620@gmail.com>
Wed Jul 31 22:11:15 2013 -0400 (2013-07-31)
changeset 3965 86dedf4462d8
parent 3702 a0256b609078
child 4028 4b3d32330007
permissions -rw-r--r--
Added CS_IsValidWeaponID native and validity checks to other natives (bug 5566, r=psychonic).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Counter-Strike:Source Extension
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "util_cstrike.h"
    33 
    34 #include "extension.h"
    35 #include "RegNatives.h"
    36 
    37 #define REGISTER_ADDR(name, defaultret, code) \
    38 	void *addr; \
    39 	if (!g_pGameConf->GetMemSig(name, &addr) || !addr) \
    40 	{ \
    41 		g_pSM->LogError(myself, "Failed to locate function."); \
    42 		return defaultret; \
    43 	} \
    44 	code; \
    45 	g_RegNatives.Register(pWrapper);
    46 
    47 void *GetWeaponInfo(int weaponID)
    48 {
    49 	static ICallWrapper *pWrapper = NULL;
    50 	if (!pWrapper)
    51 	{
    52 		REGISTER_ADDR("GetWeaponInfo", NULL,
    53 			PassInfo pass[1]; \
    54 			PassInfo retpass; \
    55 			pass[0].flags = PASSFLAG_BYVAL; \
    56 			pass[0].type = PassType_Basic; \
    57 			pass[0].size = sizeof(int); \
    58 			retpass.flags = PASSFLAG_BYVAL; \
    59 			retpass.type = PassType_Basic; \
    60 			retpass.size = sizeof(void *); \
    61 			pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retpass, pass, 1))
    62 	}
    63 
    64 	void *info;
    65 
    66 	unsigned char vstk[sizeof(int)];
    67 	unsigned char *vptr = vstk;
    68 
    69 	*(int *)vptr = weaponID;
    70 
    71 	pWrapper->Execute(vstk, &info);
    72 
    73 	return info;
    74 }
    75 
    76 const char *GetTranslatedWeaponAlias(const char *weapon)
    77 {
    78 	static ICallWrapper *pWrapper = NULL;
    79 
    80 	if (!pWrapper)
    81 	{
    82 		REGISTER_ADDR("GetTranslatedWeaponAlias", weapon,
    83 			PassInfo pass[1]; \
    84 			PassInfo retpass; \
    85 			pass[0].flags = PASSFLAG_BYVAL; \
    86 			pass[0].type = PassType_Basic; \
    87 			pass[0].size = sizeof(const char *); \
    88 			retpass.flags = PASSFLAG_BYVAL; \
    89 			retpass.type = PassType_Basic; \
    90 			retpass.size = sizeof(const char *); \
    91 			pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retpass, pass, 1))
    92 	}
    93 	const char *ret = NULL;
    94 
    95 	unsigned char vstk[sizeof(const char *)];
    96 	unsigned char *vptr = vstk;
    97 
    98 	*(const char **)vptr = weapon;
    99 
   100 	pWrapper->Execute(vstk, &ret);
   101 
   102 	return ret;
   103 }
   104 
   105 int AliasToWeaponID(const char *weapon)
   106 {
   107 	static ICallWrapper *pWrapper = NULL;
   108 
   109 	if (!pWrapper)
   110 	{
   111 		REGISTER_ADDR("AliasToWeaponID", 0,
   112 			PassInfo pass[1]; \
   113 			PassInfo retpass; \
   114 			pass[0].flags = PASSFLAG_BYVAL; \
   115 			pass[0].type = PassType_Basic; \
   116 			pass[0].size = sizeof(const char *); \
   117 			retpass.flags = PASSFLAG_BYVAL; \
   118 			retpass.type = PassType_Basic; \
   119 			retpass.size = sizeof(int); \
   120 			pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retpass, pass, 1))
   121 	}
   122 	int weaponID = 0;
   123 
   124 	unsigned char vstk[sizeof(const char *)];
   125 	unsigned char *vptr = vstk;
   126 
   127 	*(const char **)vptr = weapon;
   128 
   129 	pWrapper->Execute(vstk, &weaponID);
   130 
   131 	return weaponID;
   132 }
   133 
   134 const char *WeaponIDToAlias(int weaponID)
   135 {
   136 	static ICallWrapper *pWrapper = NULL;
   137 
   138 	if (!pWrapper)
   139 	{
   140 		REGISTER_ADDR("WeaponIDToAlias", 0,
   141 			PassInfo pass[1]; \
   142 			PassInfo retpass; \
   143 			pass[0].flags = PASSFLAG_BYVAL; \
   144 			pass[0].type = PassType_Basic; \
   145 			pass[0].size = sizeof(int); \
   146 			retpass.flags = PASSFLAG_BYVAL; \
   147 			retpass.type = PassType_Basic; \
   148 			retpass.size = sizeof(const char *); \
   149 			pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retpass, pass, 1))
   150 	}
   151 	const char *ret = NULL;
   152 	
   153 	unsigned char vstk[sizeof(int)];
   154 	unsigned char *vptr = vstk;
   155 
   156 	*(int *)vptr = GetRealWeaponID(weaponID);
   157 
   158 	pWrapper->Execute(vstk, &ret);
   159 
   160 	return ret;
   161 }
   162 int GetRealWeaponID(int weaponId)
   163 {
   164 #if SOURCE_ENGINE == SE_CSGO
   165 	switch((SMCSWeapon)weaponId)
   166 	{
   167 		case SMCSWeapon_NONE:
   168 			return (int)CSGOWeapon_NONE;
   169 		case SMCSWeapon_DEAGLE:
   170 			return (int)CSGOWeapon_DEAGLE;
   171 		case SMCSWeapon_ELITE:
   172 			return (int)CSGOWeapon_ELITE;
   173 		case SMCSWeapon_FIVESEVEN:
   174 			return (int)CSGOWeapon_FIVESEVEN;
   175 		case SMCSWeapon_GLOCK:
   176 			return (int)CSGOWeapon_GLOCK;
   177 		case SMCSWeapon_P228:
   178 			return (int)CSGOWeapon_P228;
   179 		case SMCSWeapon_USP:
   180 			return (int)CSGOWeapon_USP;
   181 		case SMCSWeapon_AK47:
   182 			return (int)CSGOWeapon_AK47;
   183 		case SMCSWeapon_AUG:
   184 			return (int)CSGOWeapon_AUG;
   185 		case SMCSWeapon_AWP:
   186 			return (int)CSGOWeapon_AWP;
   187 		case SMCSWeapon_FAMAS:
   188 			return (int)CSGOWeapon_FAMAS;
   189 		case SMCSWeapon_G3SG1:
   190 			return (int)CSGOWeapon_G3SG1;
   191 		case SMCSWeapon_GALIL:
   192 			return (int)CSGOWeapon_GALIL;
   193 		case SMCSWeapon_GALILAR:
   194 			return (int)CSGOWeapon_GALILAR;
   195 		case SMCSWeapon_M249:
   196 			return (int)CSGOWeapon_M249;
   197 		case SMCSWeapon_M3:
   198 			return (int)CSGOWeapon_M3;
   199 		case SMCSWeapon_M4A1:
   200 			return (int)CSGOWeapon_M4A1;
   201 		case SMCSWeapon_MAC10:
   202 			return (int)CSGOWeapon_MAC10;
   203 		case SMCSWeapon_MP5NAVY:
   204 			return (int)CSGOWeapon_MP5NAVY;
   205 		case SMCSWeapon_P90:
   206 			return (int)CSGOWeapon_P90;
   207 		case SMCSWeapon_SCOUT:
   208 			return (int)CSGOWeapon_SCOUT;
   209 		case SMCSWeapon_SG550:
   210 			return (int)CSGOWeapon_SG550;
   211 		case SMCSWeapon_SG552:
   212 			return (int)CSGOWeapon_SG552;
   213 		case SMCSWeapon_TMP:
   214 			return (int)CSGOWeapon_TMP;
   215 		case SMCSWeapon_UMP45:
   216 			return (int)CSGOWeapon_UMP45;
   217 		case SMCSWeapon_XM1014:
   218 			return (int)CSGOWeapon_XM1014;
   219 		case SMCSWeapon_BIZON:
   220 			return (int)CSGOWeapon_BIZON;
   221 		case SMCSWeapon_MAG7:
   222 			return (int)CSGOWeapon_MAG7;
   223 		case SMCSWeapon_NEGEV:
   224 			return (int)CSGOWeapon_NEGEV;
   225 		case SMCSWeapon_SAWEDOFF:
   226 			return (int)CSGOWeapon_SAWEDOFF;
   227 		case SMCSWeapon_TEC9:
   228 			return (int)CSGOWeapon_TEC9;
   229 		case SMCSWeapon_TASER:
   230 			return (int)CSGOWeapon_TASER;
   231 		case SMCSWeapon_HKP2000:
   232 			return (int)CSGOWeapon_HKP2000;
   233 		case SMCSWeapon_MP7:
   234 			return (int)CSGOWeapon_MP7;
   235 		case SMCSWeapon_MP9:
   236 			return (int)CSGOWeapon_MP9;
   237 		case SMCSWeapon_NOVA:
   238 			return (int)CSGOWeapon_NOVA;
   239 		case SMCSWeapon_P250:
   240 			return (int)CSGOWeapon_P250;
   241 		case SMCSWeapon_SCAR17:
   242 			return (int)CSGOWeapon_SCAR17;
   243 		case SMCSWeapon_SCAR20:
   244 			return (int)CSGOWeapon_SCAR20;
   245 		case SMCSWeapon_SG556:
   246 			return (int)CSGOWeapon_SG556;
   247 		case SMCSWeapon_SSG08:
   248 			return (int)CSGOWeapon_SSG08;
   249 		case SMCSWeapon_KNIFE_GG:
   250 			return (int)CSGOWeapon_KNIFE_GG;
   251 		case SMCSWeapon_KNIFE:
   252 			return (int)CSGOWeapon_KNIFE;
   253 		case SMCSWeapon_FLASHBANG:
   254 			return (int)CSGOWeapon_FLASHBANG;
   255 		case SMCSWeapon_HEGRENADE:
   256 			return (int)CSGOWeapon_HEGRENADE;
   257 		case SMCSWeapon_SMOKEGRENADE:
   258 			return (int)CSGOWeapon_SMOKEGRENADE;
   259 		case SMCSWeapon_MOLOTOV:
   260 			return (int)CSGOWeapon_MOLOTOV;
   261 		case SMCSWeapon_DECOY:
   262 			return (int)CSGOWeapon_DECOY;
   263 		case SMCSWeapon_INCGRENADE:
   264 			return (int)CSGOWeapon_INCGRENADE;
   265 		case SMCSWeapon_C4:
   266 			return (int)CSGOWeapon_C4;
   267 		case SMCSWeapon_KEVLAR:
   268 			return (int)CSGOWeapon_KEVLAR;
   269 		case SMCSWeapon_ASSAULTSUIT:
   270 			return (int)CSGOWeapon_ASSAULTSUIT;
   271 		case SMCSWeapon_NIGHTVISION:
   272 			return (int)CSGOWeapon_NVG;
   273 		case SMCSWeapon_DEFUSER:
   274 			return (int)CSGOWeapon_DEFUSER;
   275 		default:
   276 			return 0;
   277 	}
   278 #else
   279 	if (weaponId > 33 || weaponId < 0)
   280 		return 0;
   281 	else
   282 		return weaponId;
   283 #endif
   284 }
   285 
   286 int GetFakeWeaponID(int weaponId)
   287 {
   288 #if SOURCE_ENGINE == SE_CSGO
   289 	switch((CSGOWeapon)weaponId)
   290 	{
   291 		case CSGOWeapon_NONE:
   292 			return (int)SMCSWeapon_NONE;
   293 		case CSGOWeapon_DEAGLE:
   294 			return (int)SMCSWeapon_DEAGLE;
   295 		case CSGOWeapon_ELITE:
   296 			return (int)SMCSWeapon_ELITE;
   297 		case CSGOWeapon_FIVESEVEN:
   298 			return (int)SMCSWeapon_FIVESEVEN;
   299 		case CSGOWeapon_GLOCK:
   300 			return (int)SMCSWeapon_GLOCK;
   301 		case CSGOWeapon_P228:
   302 			return (int)SMCSWeapon_P228;
   303 		case CSGOWeapon_USP:
   304 			return (int)SMCSWeapon_USP;
   305 		case CSGOWeapon_AK47:
   306 			return (int)SMCSWeapon_AK47;
   307 		case CSGOWeapon_AUG:
   308 			return (int)SMCSWeapon_AUG;
   309 		case CSGOWeapon_AWP:
   310 			return (int)SMCSWeapon_AWP;
   311 		case CSGOWeapon_FAMAS:
   312 			return (int)SMCSWeapon_FAMAS;
   313 		case CSGOWeapon_G3SG1:
   314 			return (int)SMCSWeapon_G3SG1;
   315 		case CSGOWeapon_GALIL:
   316 			return (int)SMCSWeapon_GALIL;
   317 		case CSGOWeapon_GALILAR:
   318 			return (int)SMCSWeapon_GALILAR;
   319 		case CSGOWeapon_M249:
   320 			return (int)SMCSWeapon_M249;
   321 		case CSGOWeapon_M3:
   322 			return (int)SMCSWeapon_M3;
   323 		case CSGOWeapon_M4A1:
   324 			return (int)SMCSWeapon_M4A1;
   325 		case CSGOWeapon_MAC10:
   326 			return (int)SMCSWeapon_MAC10;
   327 		case CSGOWeapon_MP5NAVY:
   328 			return (int)SMCSWeapon_MP5NAVY;
   329 		case CSGOWeapon_P90:
   330 			return (int)SMCSWeapon_P90;
   331 		case CSGOWeapon_SCOUT:
   332 			return (int)SMCSWeapon_SCOUT;
   333 		case CSGOWeapon_SG550:
   334 			return (int)SMCSWeapon_SG550;
   335 		case CSGOWeapon_SG552:
   336 			return (int)SMCSWeapon_SG552;
   337 		case CSGOWeapon_TMP:
   338 			return (int)SMCSWeapon_TMP;
   339 		case CSGOWeapon_UMP45:
   340 			return (int)SMCSWeapon_UMP45;
   341 		case CSGOWeapon_XM1014:
   342 			return (int)SMCSWeapon_XM1014;
   343 		case CSGOWeapon_BIZON:
   344 			return (int)SMCSWeapon_BIZON;
   345 		case CSGOWeapon_MAG7:
   346 			return (int)SMCSWeapon_MAG7;
   347 		case CSGOWeapon_NEGEV:
   348 			return (int)SMCSWeapon_NEGEV;
   349 		case CSGOWeapon_SAWEDOFF:
   350 			return (int)SMCSWeapon_SAWEDOFF;
   351 		case CSGOWeapon_TEC9:
   352 			return (int)SMCSWeapon_TEC9;
   353 		case CSGOWeapon_TASER:
   354 			return (int)SMCSWeapon_TASER;
   355 		case CSGOWeapon_HKP2000:
   356 			return (int)SMCSWeapon_HKP2000;
   357 		case CSGOWeapon_MP7:
   358 			return (int)SMCSWeapon_MP7;
   359 		case CSGOWeapon_MP9:
   360 			return (int)SMCSWeapon_MP9;
   361 		case CSGOWeapon_NOVA:
   362 			return (int)SMCSWeapon_NOVA;
   363 		case CSGOWeapon_P250:
   364 			return (int)SMCSWeapon_P250;
   365 		case CSGOWeapon_SCAR17:
   366 			return (int)SMCSWeapon_SCAR17;
   367 		case CSGOWeapon_SCAR20:
   368 			return (int)SMCSWeapon_SCAR20;
   369 		case CSGOWeapon_SG556:
   370 			return (int)SMCSWeapon_SG556;
   371 		case CSGOWeapon_SSG08:
   372 			return (int)SMCSWeapon_SSG08;
   373 		case CSGOWeapon_KNIFE_GG:
   374 			return (int)SMCSWeapon_KNIFE_GG;
   375 		case CSGOWeapon_KNIFE:
   376 			return (int)SMCSWeapon_KNIFE;
   377 		case CSGOWeapon_FLASHBANG:
   378 			return (int)SMCSWeapon_FLASHBANG;
   379 		case CSGOWeapon_HEGRENADE:
   380 			return (int)SMCSWeapon_HEGRENADE;
   381 		case CSGOWeapon_SMOKEGRENADE:
   382 			return (int)SMCSWeapon_SMOKEGRENADE;
   383 		case CSGOWeapon_MOLOTOV:
   384 			return (int)SMCSWeapon_MOLOTOV;
   385 		case CSGOWeapon_DECOY:
   386 			return (int)SMCSWeapon_DECOY;
   387 		case CSGOWeapon_INCGRENADE:
   388 			return (int)SMCSWeapon_INCGRENADE;
   389 		case CSGOWeapon_C4:
   390 			return (int)SMCSWeapon_C4;
   391 		case CSGOWeapon_KEVLAR:
   392 			return (int)SMCSWeapon_KEVLAR;
   393 		case CSGOWeapon_ASSAULTSUIT:
   394 			return (int)SMCSWeapon_ASSAULTSUIT;
   395 		case CSGOWeapon_NVG:
   396 			return (int)SMCSWeapon_NIGHTVISION;
   397 		case CSGOWeapon_DEFUSER:
   398 			return (int)SMCSWeapon_DEFUSER;
   399 		default:
   400 			return 0;
   401 	}
   402 #else
   403 	if (weaponId > 33 || weaponId < 0)
   404 		return 0;
   405 	else
   406 		return weaponId;
   407 #endif
   408 }
   409 bool IsValidWeaponID(int id)
   410 {
   411 	if (id == (int)SMCSWeapon_NONE || id > (int)SMCSWeapon_DEFUSER)
   412 		return false;
   413 	//Why are these even HERE!?! They dont exist in CS:GO but have valid ID's still
   414 #if SOURCE_ENGINE == SE_CSGO
   415 	else if (id == (int)SMCSWeapon_P228 || id == (int)SMCSWeapon_SCOUT || id == (int)SMCSWeapon_SG550 || id == (int)SMCSWeapon_GALIL || id == (int)SMCSWeapon_SCAR17 ||
   416 			id == (int)SMCSWeapon_USP || id == (int)SMCSWeapon_M3 || id == (int)SMCSWeapon_MP5NAVY || id == (int)SMCSWeapon_TMP || id == (int)SMCSWeapon_SG552)
   417 			return false;
   418 #else
   419 	else if (id > (int)SMCSWeapon_NIGHTVISION || id < (int)SMCSWeapon_NONE)
   420 		return false;
   421 #endif
   422 	return true;
   423 }