Skip to content

Commit

Permalink
Items: Load reference entities from game data
Browse files Browse the repository at this point in the history
  • Loading branch information
tmp64 committed Aug 20, 2023
1 parent 621c3b9 commit d818848
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"Keys"
{
"reference_weapon" "weapon_crowbar"
"reference_ammo" "ammo_rpgclip"
"reference_ammobox" "ammo_rpgclip"
}
}
}
6 changes: 6 additions & 0 deletions src/wpnmod_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ cvar_t* cvar_mp_weaponstay = NULL;



void CConfig::LoadGameData(void)
{
m_pAmxxGameData = WpnMod_LoadGameConfigFile("weaponmod.games");
}

void CConfig::InitGameMod(void)
{
m_bInited = true;
Expand Down Expand Up @@ -172,6 +177,7 @@ void CConfig::ServerDeactivate(void)
UnsetHookVirtual(&g_PlayerPostThink_Hook);

m_pEquipEnt = NULL;
m_pAmxxGameData = nullptr;
}


Expand Down
6 changes: 6 additions & 0 deletions src/wpnmod_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#define _CONFIG_H

#include "sdk/amxxmodule.h"
#include "sdk/amxx_gameconfigs.h"

#include <map>
#include <string>
Expand Down Expand Up @@ -142,6 +143,7 @@ class CConfig
bool m_bInited;
char m_cfgpath[1024];
SUBMOD m_GameMod;
IGameConfigPtr m_pAmxxGameData;

public:
CConfig()
Expand All @@ -157,6 +159,7 @@ class CConfig
std::vector <StartAmmo*> m_pStartAmmoList;
std::vector <VirtualHookData*> m_pBlockedItemsList;

void LoadGameData (void);
void InitGameMod (void);
void SetConfigFile (void);
char* GetConfigFile (void) { return &m_cfgpath[0]; };
Expand All @@ -173,6 +176,9 @@ class CConfig
void DecalPushList (const char *name);
bool IsItemBlocked (const char *name);

//! @returns the game data config. Loaded from amxmodx/data/gamedata/weaponmod.games.
IGameConfig* GetGameData() { return m_pAmxxGameData.get(); }

static void ServerCommand (void);
static bool ClientCommand (edict_t *pEntity);
};
Expand Down
15 changes: 15 additions & 0 deletions src/wpnmod_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*
*/

#include <cassert>
#include "wpnmod_entity.h"
#include "wpnmod_parse.h"
#include "wpnmod_utils.h"
Expand Down Expand Up @@ -63,6 +64,20 @@ VirtualHookData g_PlayerSpawn_Hook = VHOOK("player", VO_Spawn, Player_Spaw
VirtualHookData g_PlayerPostThink_Hook = VHOOK("player", VO_Player_PostThink, Player_PostThink);


void Hooks_InitReferenceEntities()
{
auto fnInitArray = [](const char* name, auto& arr)
{
assert(name);

for (VirtualHookData& i : arr)
i.classname = name;
};

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

#ifdef WIN32
void __fastcall AmmoBox_Spawn(void* pvItem)
#else
Expand Down
5 changes: 3 additions & 2 deletions src/wpnmod_hooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "wpnmod_utils.h"
#include "wpnmod_vtable.h"

void Hooks_InitReferenceEntities();

#ifdef WIN32

Expand Down Expand Up @@ -169,12 +170,12 @@

#define VHOOK_WEAPON_REF(call) \
{ \
gWeaponReference, VO_##call, (void*)Weapon_##call, NULL, NULL, \
nullptr, VO_##call, (void*)Weapon_##call, NULL, NULL, \
} \

#define VHOOK_AMMOBOX_REF(call) \
{ \
gAmmoBoxReference, VO_##call, (void*)AmmoBox_##call, NULL, NULL, \
nullptr, VO_##call, (void*)AmmoBox_##call, NULL, NULL, \
} \

enum AmmoBoxRefHooks
Expand Down
26 changes: 24 additions & 2 deletions src/wpnmod_items.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@

CItems g_Items;

const char* gWeaponReference = "weapon_crowbar";
const char* gAmmoBoxReference = "ammo_rpgclip";
const char* gWeaponReference;
const char* gAmmoBoxReference;

CItems::CItems()
{
Expand All @@ -57,6 +57,28 @@ CItems::CItems()
memset(m_WeaponsInfo, 0, sizeof(m_WeaponsInfo));
}

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

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

if (!gWeaponReference || !gAmmoBoxReference)
{
WPNMOD_LOG("Invalid WeaponMod setup. gamedata is missing.\n");
WPNMOD_LOG("The server will now crash. Goodbye.\n");
std::abort();
}

Hooks_InitReferenceEntities();
}

void CItems::AllocWeaponSlots(int slots, int positions)
{
m_iMaxWeaponSlots = slots;
Expand Down
1 change: 1 addition & 0 deletions src/wpnmod_items.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ class CItems
int m_iMaxWeaponSlots;
int m_iMaxWeaponPositions;

void LoadGameData();
void AllocWeaponSlots (int slots, int positions);
void FreeWeaponSlots (void);
bool CheckSlots (int iWeaponID);
Expand Down
2 changes: 2 additions & 0 deletions src/wpnmod_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ int AmxxCheckGame(const char* game)
void OnAmxxAttach(void)
{
MF_AddNatives(Natives);
g_Config.LoadGameData();
g_Items.LoadGameData();
}

// Called by Meta_Attach.
Expand Down
6 changes: 2 additions & 4 deletions src/wpnmod_vtable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
*/

#include <string>
#include "sdk/amxx_gameconfigs.h"
#include "wpnmod_vtable.h"
#include "wpnmod_memory.h"
#include "wpnmod_config.h"
Expand Down Expand Up @@ -90,7 +89,7 @@ void Vtable_Init(void)
g_OffsetInitializers[VO_AddAmmo] = { "CBasePlayerAmmo", "AddAmmo" };

// Load offsets
IGameConfigPtr pWpnModCfg = WpnMod_LoadGameConfigFile("weaponmod.games");
IGameConfig* pWpnModCfg = g_Config.GetGameData();
IGameConfigPtr pAmxxCfg = WpnMod_LoadGameConfigFile("common.games");
bool anyNotFound = false;

Expand Down Expand Up @@ -137,10 +136,9 @@ void Vtable_Init(void)
}

// Load pev offset
IGameConfigPtr pEntityCfg = WpnMod_LoadGameConfigFile("common.games");
TypeDescription pevOffset;

if (!pEntityCfg->GetOffsetByClass("CBaseEntity", "pev", &pevOffset))
if (!pAmxxCfg->GetOffsetByClass("CBaseEntity", "pev", &pevOffset))
{
WPNMOD_LOG("CBaseEntity::pev offset not found\n");
std::abort();
Expand Down

0 comments on commit d818848

Please sign in to comment.