extensions/cstrike/util_cstrike.cpp
author Ruben Gonzalez <drifter01620@gmail.com>
Sat Feb 08 20:04:12 2014 -0500 (2014-02-08)
changeset 4237 5e3965aa4498
parent 4028 4b3d32330007
permissions -rw-r--r--
Update CStrike extension and gamedata on windows for latest CS:GO update (irc, 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 #define GET_MEMSIG(name, defaultret) \
    48 	if (!g_pGameConf->GetMemSig(name, &addr) || !addr) \
    49 	{ \
    50 		g_pSM->LogError(myself, "Failed to locate function."); \
    51 		return defaultret;\
    52 	}
    53 
    54 void *GetWeaponInfo(int weaponID)
    55 {
    56 	void *info;
    57 #if SOURCE_ENGINE != SE_CSGO || !defined(WIN32)
    58 	static ICallWrapper *pWrapper = NULL;
    59 	if (!pWrapper)
    60 	{
    61 		REGISTER_ADDR("GetWeaponInfo", NULL,
    62 			PassInfo pass[1]; \
    63 			PassInfo retpass; \
    64 			pass[0].flags = PASSFLAG_BYVAL; \
    65 			pass[0].type = PassType_Basic; \
    66 			pass[0].size = sizeof(int); \
    67 			retpass.flags = PASSFLAG_BYVAL; \
    68 			retpass.type = PassType_Basic; \
    69 			retpass.size = sizeof(void *); \
    70 			pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retpass, pass, 1))
    71 	}
    72 
    73 	unsigned char vstk[sizeof(int)];
    74 	unsigned char *vptr = vstk;
    75 
    76 	*(int *)vptr = weaponID;
    77 
    78 	pWrapper->Execute(vstk, &info);
    79 
    80 	
    81 #else
    82 	static void *addr = NULL;
    83 
    84 	if(!addr)
    85 	{
    86 		GET_MEMSIG("GetWeaponInfo", 0);
    87 	}
    88 
    89 	__asm
    90 	{
    91 		mov ecx, weaponID
    92 		call addr
    93 		mov info, eax
    94 	}
    95 #endif
    96 	return info;
    97 }
    98 
    99 const char *GetTranslatedWeaponAlias(const char *weapon)
   100 {
   101 	const char *alias = NULL;
   102 
   103 #if SOURCE_ENGINE != SE_CSGO || !defined(WIN32)
   104 	static ICallWrapper *pWrapper = NULL;
   105 
   106 	if (!pWrapper)
   107 	{
   108 		REGISTER_ADDR("GetTranslatedWeaponAlias", weapon,
   109 			PassInfo pass[1]; \
   110 			PassInfo retpass; \
   111 			pass[0].flags = PASSFLAG_BYVAL; \
   112 			pass[0].type = PassType_Basic; \
   113 			pass[0].size = sizeof(const char *); \
   114 			retpass.flags = PASSFLAG_BYVAL; \
   115 			retpass.type = PassType_Basic; \
   116 			retpass.size = sizeof(const char *); \
   117 			pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retpass, pass, 1))
   118 	}
   119 
   120 	unsigned char vstk[sizeof(const char *)];
   121 	unsigned char *vptr = vstk;
   122 
   123 	*(const char **)vptr = weapon;
   124 
   125 	pWrapper->Execute(vstk, &alias);
   126 #else
   127 	static void *addr = NULL;
   128 
   129 	if(!addr)
   130 	{
   131 		GET_MEMSIG("GetTranslatedWeaponAlias", weapon);
   132 	}
   133 
   134 	__asm
   135 	{
   136 		mov ecx, weapon
   137 		call addr
   138 		mov alias, eax
   139 	}
   140 #endif
   141 	return alias;
   142 }
   143 
   144 int AliasToWeaponID(const char *weapon)
   145 {
   146 	int weaponID = 0;
   147 #if SOURCE_ENGINE != SE_CSGO || !defined(WIN32)
   148 	static ICallWrapper *pWrapper = NULL;
   149 
   150 	if (!pWrapper)
   151 	{
   152 		REGISTER_ADDR("AliasToWeaponID", 0,
   153 			PassInfo pass[1]; \
   154 			PassInfo retpass; \
   155 			pass[0].flags = PASSFLAG_BYVAL; \
   156 			pass[0].type = PassType_Basic; \
   157 			pass[0].size = sizeof(const char *); \
   158 			retpass.flags = PASSFLAG_BYVAL; \
   159 			retpass.type = PassType_Basic; \
   160 			retpass.size = sizeof(int); \
   161 			pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retpass, pass, 1))
   162 	}
   163 
   164 	unsigned char vstk[sizeof(const char *)];
   165 	unsigned char *vptr = vstk;
   166 
   167 	*(const char **)vptr = weapon;
   168 
   169 	pWrapper->Execute(vstk, &weaponID);
   170 #else
   171 	static void *addr = NULL;
   172 
   173 	if(!addr)
   174 	{
   175 		GET_MEMSIG("AliasToWeaponID", 0);
   176 	}
   177 
   178 	__asm
   179 	{
   180 		mov ecx, weapon
   181 		call addr
   182 		mov weaponID, eax
   183 	}
   184 #endif
   185 	return weaponID;
   186 }
   187 
   188 const char *WeaponIDToAlias(int weaponID)
   189 {
   190 	const char *alias = NULL;
   191 #if SOURCE_ENGINE != SE_CSGO || !defined(WIN32)
   192 	static ICallWrapper *pWrapper = NULL;
   193 
   194 	if (!pWrapper)
   195 	{
   196 		REGISTER_ADDR("WeaponIDToAlias", 0,
   197 			PassInfo pass[1]; \
   198 			PassInfo retpass; \
   199 			pass[0].flags = PASSFLAG_BYVAL; \
   200 			pass[0].type = PassType_Basic; \
   201 			pass[0].size = sizeof(int); \
   202 			retpass.flags = PASSFLAG_BYVAL; \
   203 			retpass.type = PassType_Basic; \
   204 			retpass.size = sizeof(const char *); \
   205 			pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retpass, pass, 1))
   206 	}
   207 	
   208 	unsigned char vstk[sizeof(int)];
   209 	unsigned char *vptr = vstk;
   210 
   211 	*(int *)vptr = GetRealWeaponID(weaponID);
   212 
   213 	pWrapper->Execute(vstk, &alias);
   214 #else
   215 	static void *addr = NULL;
   216 
   217 	if(!addr)
   218 	{
   219 		GET_MEMSIG("WeaponIDToAlias", 0);
   220 	}
   221 
   222 	int realWeaponID = GetRealWeaponID(weaponID);
   223 	__asm
   224 	{
   225 		mov ecx, realWeaponID
   226 		call addr
   227 		mov alias, eax
   228 	}
   229 #endif
   230 	return alias;
   231 }
   232 int GetRealWeaponID(int weaponId)
   233 {
   234 #if SOURCE_ENGINE == SE_CSGO
   235 	switch((SMCSWeapon)weaponId)
   236 	{
   237 		case SMCSWeapon_NONE:
   238 			return (int)CSGOWeapon_NONE;
   239 		case SMCSWeapon_DEAGLE:
   240 			return (int)CSGOWeapon_DEAGLE;
   241 		case SMCSWeapon_ELITE:
   242 			return (int)CSGOWeapon_ELITE;
   243 		case SMCSWeapon_FIVESEVEN:
   244 			return (int)CSGOWeapon_FIVESEVEN;
   245 		case SMCSWeapon_GLOCK:
   246 			return (int)CSGOWeapon_GLOCK;
   247 		case SMCSWeapon_P228:
   248 			return (int)CSGOWeapon_P228;
   249 		case SMCSWeapon_USP:
   250 			return (int)CSGOWeapon_USP;
   251 		case SMCSWeapon_AK47:
   252 			return (int)CSGOWeapon_AK47;
   253 		case SMCSWeapon_AUG:
   254 			return (int)CSGOWeapon_AUG;
   255 		case SMCSWeapon_AWP:
   256 			return (int)CSGOWeapon_AWP;
   257 		case SMCSWeapon_FAMAS:
   258 			return (int)CSGOWeapon_FAMAS;
   259 		case SMCSWeapon_G3SG1:
   260 			return (int)CSGOWeapon_G3SG1;
   261 		case SMCSWeapon_GALIL:
   262 			return (int)CSGOWeapon_GALIL;
   263 		case SMCSWeapon_GALILAR:
   264 			return (int)CSGOWeapon_GALILAR;
   265 		case SMCSWeapon_M249:
   266 			return (int)CSGOWeapon_M249;
   267 		case SMCSWeapon_M3:
   268 			return (int)CSGOWeapon_M3;
   269 		case SMCSWeapon_M4A1:
   270 			return (int)CSGOWeapon_M4A1;
   271 		case SMCSWeapon_MAC10:
   272 			return (int)CSGOWeapon_MAC10;
   273 		case SMCSWeapon_MP5NAVY:
   274 			return (int)CSGOWeapon_MP5NAVY;
   275 		case SMCSWeapon_P90:
   276 			return (int)CSGOWeapon_P90;
   277 		case SMCSWeapon_SCOUT:
   278 			return (int)CSGOWeapon_SCOUT;
   279 		case SMCSWeapon_SG550:
   280 			return (int)CSGOWeapon_SG550;
   281 		case SMCSWeapon_SG552:
   282 			return (int)CSGOWeapon_SG552;
   283 		case SMCSWeapon_TMP:
   284 			return (int)CSGOWeapon_TMP;
   285 		case SMCSWeapon_UMP45:
   286 			return (int)CSGOWeapon_UMP45;
   287 		case SMCSWeapon_XM1014:
   288 			return (int)CSGOWeapon_XM1014;
   289 		case SMCSWeapon_BIZON:
   290 			return (int)CSGOWeapon_BIZON;
   291 		case SMCSWeapon_MAG7:
   292 			return (int)CSGOWeapon_MAG7;
   293 		case SMCSWeapon_NEGEV:
   294 			return (int)CSGOWeapon_NEGEV;
   295 		case SMCSWeapon_SAWEDOFF:
   296 			return (int)CSGOWeapon_SAWEDOFF;
   297 		case SMCSWeapon_TEC9:
   298 			return (int)CSGOWeapon_TEC9;
   299 		case SMCSWeapon_TASER:
   300 			return (int)CSGOWeapon_TASER;
   301 		case SMCSWeapon_HKP2000:
   302 			return (int)CSGOWeapon_HKP2000;
   303 		case SMCSWeapon_MP7:
   304 			return (int)CSGOWeapon_MP7;
   305 		case SMCSWeapon_MP9:
   306 			return (int)CSGOWeapon_MP9;
   307 		case SMCSWeapon_NOVA:
   308 			return (int)CSGOWeapon_NOVA;
   309 		case SMCSWeapon_P250:
   310 			return (int)CSGOWeapon_P250;
   311 		case SMCSWeapon_SCAR17:
   312 			return (int)CSGOWeapon_SCAR17;
   313 		case SMCSWeapon_SCAR20:
   314 			return (int)CSGOWeapon_SCAR20;
   315 		case SMCSWeapon_SG556:
   316 			return (int)CSGOWeapon_SG556;
   317 		case SMCSWeapon_SSG08:
   318 			return (int)CSGOWeapon_SSG08;
   319 		case SMCSWeapon_KNIFE_GG:
   320 			return (int)CSGOWeapon_KNIFE_GG;
   321 		case SMCSWeapon_KNIFE:
   322 			return (int)CSGOWeapon_KNIFE;
   323 		case SMCSWeapon_FLASHBANG:
   324 			return (int)CSGOWeapon_FLASHBANG;
   325 		case SMCSWeapon_HEGRENADE:
   326 			return (int)CSGOWeapon_HEGRENADE;
   327 		case SMCSWeapon_SMOKEGRENADE:
   328 			return (int)CSGOWeapon_SMOKEGRENADE;
   329 		case SMCSWeapon_MOLOTOV:
   330 			return (int)CSGOWeapon_MOLOTOV;
   331 		case SMCSWeapon_DECOY:
   332 			return (int)CSGOWeapon_DECOY;
   333 		case SMCSWeapon_INCGRENADE:
   334 			return (int)CSGOWeapon_INCGRENADE;
   335 		case SMCSWeapon_C4:
   336 			return (int)CSGOWeapon_C4;
   337 		case SMCSWeapon_KEVLAR:
   338 			return (int)CSGOWeapon_KEVLAR;
   339 		case SMCSWeapon_ASSAULTSUIT:
   340 			return (int)CSGOWeapon_ASSAULTSUIT;
   341 		case SMCSWeapon_NIGHTVISION:
   342 			return (int)CSGOWeapon_NVG;
   343 		case SMCSWeapon_DEFUSER:
   344 			return (int)CSGOWeapon_DEFUSER;
   345 		default:
   346 			return (int)CSGOWeapon_NONE;
   347 	}
   348 #else
   349 	if (weaponId > (int)SMCSWeapon_NIGHTVISION || weaponId < (int)SMCSWeapon_NONE)
   350 		return (int)SMCSWeapon_NONE;
   351 	else
   352 		return weaponId;
   353 #endif
   354 }
   355 
   356 int GetFakeWeaponID(int weaponId)
   357 {
   358 #if SOURCE_ENGINE == SE_CSGO
   359 	switch((CSGOWeapon)weaponId)
   360 	{
   361 		case CSGOWeapon_NONE:
   362 			return (int)SMCSWeapon_NONE;
   363 		case CSGOWeapon_DEAGLE:
   364 			return (int)SMCSWeapon_DEAGLE;
   365 		case CSGOWeapon_ELITE:
   366 			return (int)SMCSWeapon_ELITE;
   367 		case CSGOWeapon_FIVESEVEN:
   368 			return (int)SMCSWeapon_FIVESEVEN;
   369 		case CSGOWeapon_GLOCK:
   370 			return (int)SMCSWeapon_GLOCK;
   371 		case CSGOWeapon_P228:
   372 			return (int)SMCSWeapon_P228;
   373 		case CSGOWeapon_USP:
   374 			return (int)SMCSWeapon_USP;
   375 		case CSGOWeapon_AK47:
   376 			return (int)SMCSWeapon_AK47;
   377 		case CSGOWeapon_AUG:
   378 			return (int)SMCSWeapon_AUG;
   379 		case CSGOWeapon_AWP:
   380 			return (int)SMCSWeapon_AWP;
   381 		case CSGOWeapon_FAMAS:
   382 			return (int)SMCSWeapon_FAMAS;
   383 		case CSGOWeapon_G3SG1:
   384 			return (int)SMCSWeapon_G3SG1;
   385 		case CSGOWeapon_GALIL:
   386 			return (int)SMCSWeapon_GALIL;
   387 		case CSGOWeapon_GALILAR:
   388 			return (int)SMCSWeapon_GALILAR;
   389 		case CSGOWeapon_M249:
   390 			return (int)SMCSWeapon_M249;
   391 		case CSGOWeapon_M3:
   392 			return (int)SMCSWeapon_M3;
   393 		case CSGOWeapon_M4A1:
   394 			return (int)SMCSWeapon_M4A1;
   395 		case CSGOWeapon_MAC10:
   396 			return (int)SMCSWeapon_MAC10;
   397 		case CSGOWeapon_MP5NAVY:
   398 			return (int)SMCSWeapon_MP5NAVY;
   399 		case CSGOWeapon_P90:
   400 			return (int)SMCSWeapon_P90;
   401 		case CSGOWeapon_SCOUT:
   402 			return (int)SMCSWeapon_SCOUT;
   403 		case CSGOWeapon_SG550:
   404 			return (int)SMCSWeapon_SG550;
   405 		case CSGOWeapon_SG552:
   406 			return (int)SMCSWeapon_SG552;
   407 		case CSGOWeapon_TMP:
   408 			return (int)SMCSWeapon_TMP;
   409 		case CSGOWeapon_UMP45:
   410 			return (int)SMCSWeapon_UMP45;
   411 		case CSGOWeapon_XM1014:
   412 			return (int)SMCSWeapon_XM1014;
   413 		case CSGOWeapon_BIZON:
   414 			return (int)SMCSWeapon_BIZON;
   415 		case CSGOWeapon_MAG7:
   416 			return (int)SMCSWeapon_MAG7;
   417 		case CSGOWeapon_NEGEV:
   418 			return (int)SMCSWeapon_NEGEV;
   419 		case CSGOWeapon_SAWEDOFF:
   420 			return (int)SMCSWeapon_SAWEDOFF;
   421 		case CSGOWeapon_TEC9:
   422 			return (int)SMCSWeapon_TEC9;
   423 		case CSGOWeapon_TASER:
   424 			return (int)SMCSWeapon_TASER;
   425 		case CSGOWeapon_HKP2000:
   426 			return (int)SMCSWeapon_HKP2000;
   427 		case CSGOWeapon_MP7:
   428 			return (int)SMCSWeapon_MP7;
   429 		case CSGOWeapon_MP9:
   430 			return (int)SMCSWeapon_MP9;
   431 		case CSGOWeapon_NOVA:
   432 			return (int)SMCSWeapon_NOVA;
   433 		case CSGOWeapon_P250:
   434 			return (int)SMCSWeapon_P250;
   435 		case CSGOWeapon_SCAR17:
   436 			return (int)SMCSWeapon_SCAR17;
   437 		case CSGOWeapon_SCAR20:
   438 			return (int)SMCSWeapon_SCAR20;
   439 		case CSGOWeapon_SG556:
   440 			return (int)SMCSWeapon_SG556;
   441 		case CSGOWeapon_SSG08:
   442 			return (int)SMCSWeapon_SSG08;
   443 		case CSGOWeapon_KNIFE_GG:
   444 			return (int)SMCSWeapon_KNIFE_GG;
   445 		case CSGOWeapon_KNIFE:
   446 			return (int)SMCSWeapon_KNIFE;
   447 		case CSGOWeapon_FLASHBANG:
   448 			return (int)SMCSWeapon_FLASHBANG;
   449 		case CSGOWeapon_HEGRENADE:
   450 			return (int)SMCSWeapon_HEGRENADE;
   451 		case CSGOWeapon_SMOKEGRENADE:
   452 			return (int)SMCSWeapon_SMOKEGRENADE;
   453 		case CSGOWeapon_MOLOTOV:
   454 			return (int)SMCSWeapon_MOLOTOV;
   455 		case CSGOWeapon_DECOY:
   456 			return (int)SMCSWeapon_DECOY;
   457 		case CSGOWeapon_INCGRENADE:
   458 			return (int)SMCSWeapon_INCGRENADE;
   459 		case CSGOWeapon_C4:
   460 			return (int)SMCSWeapon_C4;
   461 		case CSGOWeapon_KEVLAR:
   462 			return (int)SMCSWeapon_KEVLAR;
   463 		case CSGOWeapon_ASSAULTSUIT:
   464 			return (int)SMCSWeapon_ASSAULTSUIT;
   465 		case CSGOWeapon_NVG:
   466 			return (int)SMCSWeapon_NIGHTVISION;
   467 		case CSGOWeapon_DEFUSER:
   468 			return (int)SMCSWeapon_DEFUSER;
   469 		default:
   470 			return (int)SMCSWeapon_NONE;
   471 	}
   472 #else
   473 	if (weaponId > (int)SMCSWeapon_NIGHTVISION || weaponId < (int)SMCSWeapon_NONE)
   474 		return (int)SMCSWeapon_NONE;
   475 	else
   476 		return weaponId;
   477 #endif
   478 }
   479 bool IsValidWeaponID(int id)
   480 {
   481 	if (id <= (int)SMCSWeapon_NONE)
   482 		return false;
   483 	//Why are these even HERE!?! They dont exist in CS:GO but have valid ID's still
   484 #if SOURCE_ENGINE == SE_CSGO
   485 	else if (id > (int)SMCSWeapon_DEFUSER || id == (int)SMCSWeapon_P228 || id == (int)SMCSWeapon_SCOUT || id == (int)SMCSWeapon_SG550 || id == (int)SMCSWeapon_GALIL ||
   486 			id == (int)SMCSWeapon_SCAR17 || id == (int)SMCSWeapon_USP || id == (int)SMCSWeapon_M3 || id == (int)SMCSWeapon_MP5NAVY || id == (int)SMCSWeapon_TMP || id == (int)SMCSWeapon_SG552)
   487 			return false;
   488 #else
   489 	else if (id > (int)SMCSWeapon_NIGHTVISION)
   490 		return false;
   491 #endif
   492 	return true;
   493 }