Backed out changeset: 7d07b4f23cd0.
3 * =============================================================================
4 * SourceMod Sample 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"
35 class FileWeaponInfo_t;
36 typedef unsigned short WEAPON_FILE_INFO_HANDLE;
38 inline edict_t *GetEdict(cell_t num)
40 edict_t *pEdict = engine->PEntityOfEntIndex(num);
41 if (!pEdict || pEdict->IsFree())
49 inline edict_t *GetEntity(cell_t num, CBaseEntity **pData)
51 edict_t *pEdict = engine->PEntityOfEntIndex(num);
52 if (!pEdict || pEdict->IsFree())
58 if ((pUnk=pEdict->GetUnknown()) == NULL)
62 *pData = pUnk->GetBaseEntity();
66 int CheckBaseHandle(CBaseHandle &hndl)
73 int index = hndl.GetEntryIndex();
75 edict_t *pStoredEdict;
76 CBaseEntity *pStoredEntity;
78 pStoredEdict = GetEntity(index, &pStoredEntity);
80 if (pStoredEdict == NULL || pStoredEntity == NULL)
85 IServerEntity *pSE = pStoredEdict->GetIServerEntity();
92 if (pSE->GetRefEHandle() != hndl)
100 //native GetStructInt(Handle:struct, const String:member[]);
101 static cell_t GetStructInt(IPluginContext *pContext, const cell_t *params)
103 Handle_t hndl = static_cast<Handle_t>(params[1]);
108 sec.pIdentity = myself->GetIdentity();
110 StructHandle *pHandle;
111 if ((err = g_pHandleSys->ReadHandle(hndl, g_StructHandle, &sec, (void **)&pHandle))
114 return pContext->ThrowNativeError("Invalid struct handle %x (error %d)", hndl, err);
118 pContext->LocalToString(params[2], &member);
122 if (!pHandle->GetInt(member, &value))
124 return pContext->ThrowNativeError("Invalid member, or incorrect data type");
130 //native SetStructInt(Handle:struct, const String:member[], value);
131 static cell_t SetStructInt(IPluginContext *pContext, const cell_t *params)
133 Handle_t hndl = static_cast<Handle_t>(params[1]);
138 sec.pIdentity = myself->GetIdentity();
140 StructHandle *pHandle;
141 if ((err = handlesys->ReadHandle(hndl, g_StructHandle, &sec, (void **)&pHandle))
144 return pContext->ThrowNativeError("Invalid struct handle %x (error %d)", hndl, err);
148 pContext->LocalToString(params[2], &member);
150 if (!pHandle->SetInt(member, params[3]))
152 return pContext->ThrowNativeError("Invalid member, or incorrect data type");
158 //native GetStructFloat(Handle:struct, const String:member[], &Float:value);
159 static cell_t GetStructFloat(IPluginContext *pContext, const cell_t *params)
161 Handle_t hndl = static_cast<Handle_t>(params[1]);
166 sec.pIdentity = myself->GetIdentity();
168 StructHandle *pHandle;
169 if ((err = g_pHandleSys->ReadHandle(hndl, g_StructHandle, &sec, (void **)&pHandle))
172 return pContext->ThrowNativeError("Invalid struct handle %x (error %d)", hndl, err);
176 pContext->LocalToString(params[2], &member);
180 if (!pHandle->GetFloat(member, &value))
182 return pContext->ThrowNativeError("Invalid member, or incorrect data type");
185 return sp_ftoc(value);
188 //native SetStructFloat(Handle:struct, const String:member[], Float:value);
189 static cell_t SetStructFloat(IPluginContext *pContext, const cell_t *params)
191 Handle_t hndl = static_cast<Handle_t>(params[1]);
196 sec.pIdentity = myself->GetIdentity();
198 StructHandle *pHandle;
199 if ((err = g_pHandleSys->ReadHandle(hndl, g_StructHandle, &sec, (void **)&pHandle))
202 return pContext->ThrowNativeError("Invalid struct handle %x (error %d)", hndl, err);
206 pContext->LocalToString(params[2], &member);
208 if (!pHandle->SetFloat(member, sp_ctof(params[3])))
210 return pContext->ThrowNativeError("Invalid member, or incorrect data type");
216 //native GetStructVector(Handle:struct, const String:member[], Float:vec[3]);
217 static cell_t GetStructVector(IPluginContext *pContext, const cell_t *params)
219 Handle_t hndl = static_cast<Handle_t>(params[1]);
224 sec.pIdentity = myself->GetIdentity();
226 StructHandle *pHandle;
227 if ((err = g_pHandleSys->ReadHandle(hndl, g_StructHandle, &sec, (void **)&pHandle))
230 return pContext->ThrowNativeError("Invalid struct handle %x (error %d)", hndl, err);
234 pContext->LocalToString(params[2], &member);
237 pContext->LocalToPhysAddr(params[3], &addr);
241 if (!pHandle->GetVector(member, &value))
243 return pContext->ThrowNativeError("Invalid member, or incorrect data type");
246 addr[0] = sp_ftoc(value.x);
247 addr[1] = sp_ftoc(value.y);
248 addr[2] = sp_ftoc(value.z);
253 //native SetStructVector(Handle:struct, const String:member[], Float:vec[3]);
254 static cell_t SetStructVector(IPluginContext *pContext, const cell_t *params)
256 Handle_t hndl = static_cast<Handle_t>(params[1]);
261 sec.pIdentity = myself->GetIdentity();
263 StructHandle *pHandle;
264 if ((err = g_pHandleSys->ReadHandle(hndl, g_StructHandle, &sec, (void **)&pHandle))
267 return pContext->ThrowNativeError("Invalid struct handle %x (error %d)", hndl, err);
271 pContext->LocalToString(params[2], &member);
276 pContext->LocalToPhysAddr(params[3], &addr);
278 value.x = sp_ctof(addr[0]);
279 value.y = sp_ctof(addr[1]);
280 value.z = sp_ctof(addr[2]);
282 if (!pHandle->SetVector(member, value))
284 return pContext->ThrowNativeError("Invalid member, or incorrect data type");
290 //native GetStructString(Handle:struct, const String:member[], String:value[], maxlen);
291 static cell_t GetStructString(IPluginContext *pContext, const cell_t *params)
293 Handle_t hndl = static_cast<Handle_t>(params[1]);
298 sec.pIdentity = myself->GetIdentity();
300 StructHandle *pHandle;
301 if ((err = g_pHandleSys->ReadHandle(hndl, g_StructHandle, &sec, (void **)&pHandle))
304 return pContext->ThrowNativeError("Invalid struct handle %x (error %d)", hndl, err);
308 pContext->LocalToString(params[2], &member);
311 if (!pHandle->GetString(member, &string))
313 return pContext->ThrowNativeError("Invalid member, or incorrect data type");
316 pContext->StringToLocal(params[3], params[4], string);
321 //native SetStructString(Handle:struct, const String:member[], String:value[]);
322 static cell_t SetStructString(IPluginContext *pContext, const cell_t *params)
324 Handle_t hndl = static_cast<Handle_t>(params[1]);
329 sec.pIdentity = myself->GetIdentity();
331 StructHandle *pHandle;
332 if ((err = g_pHandleSys->ReadHandle(hndl, g_StructHandle, &sec, (void **)&pHandle))
335 return pContext->ThrowNativeError("Invalid struct handle %x (error %d)", hndl, err);
339 pContext->LocalToString(params[2], &member);
342 pContext->LocalToString(params[3], &string);
344 if (!pHandle->SetString(member, string))
346 return pContext->ThrowNativeError("Invalid member, or incorrect data type");
352 //native GetStructEnt(Handle:struct, const String:member[], &ent);
353 static cell_t GetStructEnt(IPluginContext *pContext, const cell_t *params)
355 Handle_t hndl = static_cast<Handle_t>(params[1]);
360 sec.pIdentity = myself->GetIdentity();
362 StructHandle *pHandle;
363 if ((err = g_pHandleSys->ReadHandle(hndl, g_StructHandle, &sec, (void **)&pHandle))
366 return pContext->ThrowNativeError("Invalid struct handle %x (error %d)", hndl, err);
370 pContext->LocalToString(params[2], &member);
373 if (!pHandle->GetEHandle(member, &value))
375 return pContext->ThrowNativeError("Invalid member, or incorrect data type");
378 return CheckBaseHandle(*value);
381 //native SetStructEnt(Handle:struct, const String:member[], ent);
382 static cell_t SetStructEnt(IPluginContext *pContext, const cell_t *params)
384 Handle_t hndl = static_cast<Handle_t>(params[1]);
389 sec.pIdentity = myself->GetIdentity();
391 StructHandle *pHandle;
392 if ((err = g_pHandleSys->ReadHandle(hndl, g_StructHandle, &sec, (void **)&pHandle))
395 return pContext->ThrowNativeError("Invalid struct handle %x (error %d)", hndl, err);
399 pContext->LocalToString(params[2], &member);
402 if (!pHandle->GetEHandle(member, &value))
404 return pContext->ThrowNativeError("Invalid member, or incorrect data type");
407 edict_t *pEdict = GetEdict(params[3]);
411 return pContext->ThrowNativeError("Invalid entity %i", params[3]);
414 IServerEntity *pEntOther = pEdict->GetIServerEntity();
415 value->Set(pEntOther);
417 if (!pHandle->SetEHandle(member, value))
419 return pContext->ThrowNativeError("Invalid member, or incorrect data type");
425 static cell_t GetWeaponStruct(IPluginContext *pContext, const cell_t *params)
428 pContext->LocalToString(params[1], &weapon);
436 WEAPON_FILE_INFO_HANDLE (*LookupWeaponInfoSlot)(const char *) = NULL;
437 func = (void **)&LookupWeaponInfoSlot;
440 FileWeaponInfo_t *(*GetFileWeaponInfoFromHandle)( WEAPON_FILE_INFO_HANDLE handle ) = NULL;
442 if (!conf->GetMemSig("LookupWeaponInfoSlot", func) || *func == NULL)
444 return pContext->ThrowNativeError("Failed to locate signature LookupWeaponInfoSlot");
447 if (!conf->GetMemSig("GetFileWeaponInfoFromHandle", (void **)&GetFileWeaponInfoFromHandle) || GetFileWeaponInfoFromHandle == NULL)
449 return pContext->ThrowNativeError("Failed to locate signature GetFileWeaponInfoFromHandle");
455 if (!conf->GetOffset("m_WeaponInfoDatabase", &offset))
457 return pContext->ThrowNativeError("Failed to locate offset m_WeaponInfoDatabase");
460 addr = (unsigned char *)addr + offset;
462 CUtlDict< FileWeaponInfo_t*, unsigned short > *m_WeaponInfoDatabase = *(CUtlDict< FileWeaponInfo_t*, unsigned short > **)addr;
464 WEAPON_FILE_INFO_HANDLE handle = m_WeaponInfoDatabase->Find(weapon);
466 if (handle == -1 || handle == m_WeaponInfoDatabase->InvalidIndex())
468 return pContext->ThrowNativeError("Could not find weapon %s", weapon);
471 WEAPON_FILE_INFO_HANDLE handle = LookupWeaponInfoSlot(weapon);
473 FileWeaponInfo_t *info = GetFileWeaponInfoFromHandle(handle);
475 //g_pSM->LogMessage(myself, "%i %i %x", handle, GetInvalidWeaponInfoHandle(), info);
479 return pContext->ThrowNativeError("Weapon does not exist!");
484 g_pSM->LogMessage(myself, "Offsets for FileWeaponInfo_t");
485 g_pSM->LogMessage(myself, "bParsed: %i", (unsigned char *)&(info->bParsedScript) - (unsigned char *)info);
486 g_pSM->LogMessage(myself, "bLoadedHudElements: %i", (unsigned char *)&(info->bLoadedHudElements) - (unsigned char *)info);
487 g_pSM->LogMessage(myself, "szClassName: %i", (unsigned char *)&(info->szClassName) - (unsigned char *)info);
488 g_pSM->LogMessage(myself, "szPrintName: %i", (unsigned char *)&(info->szPrintName) - (unsigned char *)info);
489 g_pSM->LogMessage(myself, "szViewModel: %i", (unsigned char *)&(info->szViewModel) - (unsigned char *)info);
490 g_pSM->LogMessage(myself, "szWorldModel: %i", (unsigned char *)&(info->szWorldModel) - (unsigned char *)info);
491 g_pSM->LogMessage(myself, "szAnimationPrefix: %i", (unsigned char *)&(info->szAnimationPrefix) - (unsigned char *)info);
492 g_pSM->LogMessage(myself, "iSlot: %i", (unsigned char *)&(info->iSlot) - (unsigned char *)info);
493 g_pSM->LogMessage(myself, "iPosition: %i", (unsigned char *)&(info->iPosition) - (unsigned char *)info);
494 g_pSM->LogMessage(myself, "iMaxClip1: %i", (unsigned char *)&(info->iMaxClip1) - (unsigned char *)info);
495 g_pSM->LogMessage(myself, "iMaxClip2: %i", (unsigned char *)&(info->iMaxClip2) - (unsigned char *)info);
496 g_pSM->LogMessage(myself, "iDefaultClip1: %i", (unsigned char *)&(info->iDefaultClip1) - (unsigned char *)info);
497 g_pSM->LogMessage(myself, "iDefaultClip2: %i", (unsigned char *)&(info->iDefaultClip2) - (unsigned char *)info);
498 g_pSM->LogMessage(myself, "iWeight: %i", (unsigned char *)&(info->iWeight) - (unsigned char *)info);
499 g_pSM->LogMessage(myself, "iRumbleEffect: %i", (unsigned char *)&(info->iRumbleEffect) - (unsigned char *)info);
500 g_pSM->LogMessage(myself, "bAutoSwitchTo: %i", (unsigned char *)&(info->bAutoSwitchTo) - (unsigned char *)info);
501 g_pSM->LogMessage(myself, "bAutoSwitchFrom: %i", (unsigned char *)&(info->bAutoSwitchFrom) - (unsigned char *)info);
502 g_pSM->LogMessage(myself, "iFlags: %i", (unsigned char *)&(info->iFlags) - (unsigned char *)info);
503 g_pSM->LogMessage(myself, "szAmmo1: %i", (unsigned char *)&(info->szAmmo1) - (unsigned char *)info);
504 g_pSM->LogMessage(myself, "szAmmo2: %i", (unsigned char *)&(info->szAmmo2) - (unsigned char *)info);
505 g_pSM->LogMessage(myself, "aShootSounds: %i", (unsigned char *)&(info->aShootSounds) - (unsigned char *)info);
506 g_pSM->LogMessage(myself, "iAmmoType: %i", (unsigned char *)&(info->iAmmoType) - (unsigned char *)info);
507 g_pSM->LogMessage(myself, "iAmmo2Type: %i", (unsigned char *)&(info->iAmmo2Type) - (unsigned char *)info);
508 g_pSM->LogMessage(myself, "m_bMeleeWeapon: %i", (unsigned char *)&(info->m_bMeleeWeapon) - (unsigned char *)info);
509 g_pSM->LogMessage(myself, "m_bBuiltRightHanded: %i", (unsigned char *)&(info->m_bBuiltRightHanded) - (unsigned char *)info);
510 g_pSM->LogMessage(myself, "m_bAllowFlipping: %i", (unsigned char *)&(info->m_bAllowFlipping) - (unsigned char *)info);
512 return g_StructManager.CreateStructHandle("FileWeaponInfo_t", info);
515 const sp_nativeinfo_t MyNatives[] =
517 {"GetStructInt", GetStructInt},
518 {"SetStructInt", SetStructInt},
519 {"GetStructFloat", GetStructFloat},
520 {"SetStructFloat", SetStructFloat},
521 {"GetStructVector", GetStructVector},
522 {"SetStructVector", SetStructVector},
523 {"GetStructString", GetStructString},
524 {"SetStructString", SetStructString},
525 {"GetStructEnt", GetStructEnt},
526 {"SetStructEnt", SetStructEnt},
527 {"GetWeaponStruct", GetWeaponStruct},