Skip to content

Commit

Permalink
Items: Store ref entity names in std::string (#11)
Browse files Browse the repository at this point in the history
IGameConfig::GetKeyValue returns a pointer that is freed on map change
  • Loading branch information
tmp64 committed Dec 29, 2023
1 parent bc87b53 commit aca9b17
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
14 changes: 7 additions & 7 deletions src/wpnmod_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ void Hooks_InitReferenceEntities()
i.classname = name;
};

fnInitArray(gWeaponReference, g_CrowbarHooks);
fnInitArray(gAmmoBoxReference, g_AmmoBoxRefHooks);
fnInitArray(gWeaponReference.c_str(), g_CrowbarHooks);
fnInitArray(gAmmoBoxReference.c_str(), g_AmmoBoxRefHooks);
}

#ifdef WIN32
Expand Down Expand Up @@ -117,9 +117,9 @@ void Hooks_InitReferenceEntities()
return FALSE;
}

if (!stricmp(STRING(pAmmobox->v.classname), gAmmoBoxReference))
if (!stricmp(STRING(pAmmobox->v.classname), gAmmoBoxReference.c_str()))
{
if (g_Config.IsItemBlocked(gAmmoBoxReference))
if (g_Config.IsItemBlocked(gAmmoBoxReference.c_str()))
{
UTIL_RemoveEntity(pAmmobox);
return FALSE;
Expand Down Expand Up @@ -476,7 +476,7 @@ void Hooks_InitReferenceEntities()

if (WEAPON_IS_CUSTOM(iId) && IsValidPev(pPlayer))
{
if (!stricmp(STRING(pWeapon->v.classname), gWeaponReference))
if (!stricmp(STRING(pWeapon->v.classname), gWeaponReference.c_str()))
{
UTIL_RemoveEntity(pWeapon);
return 0;
Expand Down Expand Up @@ -951,11 +951,11 @@ void* WpnMod_GetDispatch(char *pname)
// Try to find custom classname in registered weapons and ammoboxes
if (WEAPON_GET_ID(pname))
{
return (void*)FindAdressInDLL(g_Memory.GetModule_GameDll(), gWeaponReference);
return (void*)FindAdressInDLL(g_Memory.GetModule_GameDll(), gWeaponReference.c_str());
}
else if (AMMOBOX_GET_ID(pname))
{
return (void*)FindAdressInDLL(g_Memory.GetModule_GameDll(), gAmmoBoxReference);
return (void*)FindAdressInDLL(g_Memory.GetModule_GameDll(), gAmmoBoxReference.c_str());
}

// Try another ways here
Expand Down
14 changes: 9 additions & 5 deletions src/wpnmod_items.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*
*/

#include <cassert>
#include "wpnmod_items.h"
#include "wpnmod_hooks.h"
#include "wpnmod_utils.h"
Expand All @@ -39,8 +40,8 @@

CItems g_Items;

const char* gWeaponReference;
const char* gAmmoBoxReference;
std::string gWeaponReference;
std::string gAmmoBoxReference;

CItems::CItems()
{
Expand All @@ -59,17 +60,20 @@ CItems::CItems()

void CItems::LoadGameData()
{
assert(gWeaponReference.empty());
assert(gAmmoBoxReference.empty());

// Load reference weapons
IGameConfig* pCfg = g_Config.GetGameData();
gWeaponReference = pCfg->GetKeyValue("reference_weapon");
if (!gWeaponReference)
if (gWeaponReference.empty())
WPNMOD_LOG(" reference_weapon not found\n");

gAmmoBoxReference = pCfg->GetKeyValue("reference_ammobox");
if (!gAmmoBoxReference)
if (gAmmoBoxReference.empty())
WPNMOD_LOG(" reference_ammobox not found\n");

if (!gWeaponReference || !gAmmoBoxReference)
if (gWeaponReference.empty() || gAmmoBoxReference.empty())
{
WPNMOD_LOG("Invalid WeaponMod setup. gamedata is missing.\n");
WPNMOD_LOG("The server will now crash. Goodbye.\n");
Expand Down
4 changes: 2 additions & 2 deletions src/wpnmod_items.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ class CItems

extern CItems g_Items;

extern const char* gWeaponReference;
extern const char* gAmmoBoxReference;
extern std::string gWeaponReference;
extern std::string gAmmoBoxReference;


#endif // _WPNMOD_ITEMS_H
2 changes: 1 addition & 1 deletion src/wpnmod_parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void OnParseBlockedItems(std::string dummy, std::string BlockedItem)
p->address = NULL;
p->classname = STRING(ALLOC_STRING(BlockedItem.c_str()));

if (!stricmp(BlockedItem.c_str(), gWeaponReference) || !stricmp(BlockedItem.c_str(), gAmmoBoxReference))
if (!stricmp(BlockedItem.c_str(), gWeaponReference.c_str()) || !stricmp(BlockedItem.c_str(), gAmmoBoxReference.c_str()))
{
g_Config.m_pBlockedItemsList.push_back(p);
return;
Expand Down

0 comments on commit aca9b17

Please sign in to comment.