Skip to content

Commit

Permalink
PrecacheItemInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
aleeperezz16 committed Jan 4, 2024
1 parent 614d3ec commit e8b639d
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 0 deletions.
6 changes: 6 additions & 0 deletions reapi/include/cssdk/dlls/regamedll_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,10 @@ typedef IHookChainRegistry<void, class CBaseEntity *, class CBasePlayer *, class
typedef IHookChainClass<void, class CBasePlayer> IReGameHook_CBasePlayer_PlayerDeathThink;
typedef IHookChainRegistryClass<void, class CBasePlayer> IReGameHookRegistry_CBasePlayer_PlayerDeathThink;

// PrecacheItemInfo hook
typedef IHookChain<void, ItemInfo *> IReGameHook_PrecacheItemInfo;
typedef IHookChainRegistry<void, ItemInfo *> IReGameHookRegistry_PrecacheItemInfo;

class IReGameHookchains {
public:
virtual ~IReGameHookchains() {}
Expand Down Expand Up @@ -782,6 +786,8 @@ class IReGameHookchains {
virtual IReGameHookRegistry_CSGameRules_SendDeathMessage *CSGameRules_SendDeathMessage() = 0;

virtual IReGameHookRegistry_CBasePlayer_PlayerDeathThink *CBasePlayer_PlayerDeathThink() = 0;

virtual IReGameHookRegistry_PrecacheItemInfo* PrecacheItemInfo() = 0;
};

struct ReGameFuncs_t {
Expand Down
10 changes: 10 additions & 0 deletions reapi/src/hook_callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1610,6 +1610,16 @@ void BuyItem(IReGameHook_BuyItem *chain, CBasePlayer *pPlayer, int iSlot)
callVoidForward(RG_AddMultiDamage, original, indexOfEdict(pPlayer->pev), iSlot);
}

void PrecacheItemInfo(IReGameHook_PrecacheItemInfo *chain, ItemInfo *info)
{
auto original = [chain](ItemInfo* _info)
{
chain->callNext(_info);
};

callVoidForward(RG_PrecacheItemInfo, original, info);
}

void CSGameRules_Think(IReGameHook_CSGameRules_Think *chain)
{
auto original = [chain]()
Expand Down
1 change: 1 addition & 0 deletions reapi/src/hook_callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ void ClearMultiDamage(IReGameHook_ClearMultiDamage *chain);
void AddMultiDamage(IReGameHook_AddMultiDamage *chain, entvars_t *pevInflictor, CBaseEntity *pEntity, float flDamage, int bitsDamageType);
void ApplyMultiDamage(IReGameHook_ApplyMultiDamage *chain, entvars_t *pevInflictor, entvars_t *pevAttacker);
void BuyItem(IReGameHook_BuyItem *chain, CBasePlayer *pPlayer, int iSlot);
void PrecacheItemInfo(IReGameHook_PrecacheItemInfo* chain, ItemInfo* info);
void CSGameRules_Think(IReGameHook_CSGameRules_Think *chain);
BOOL CSGameRules_TeamFull(IReGameHook_CSGameRules_TeamFull *chain, int team_id);
BOOL CSGameRules_TeamStacked(IReGameHook_CSGameRules_TeamStacked *chain, int newTeam_id, int curTeam_id);
Expand Down
1 change: 1 addition & 0 deletions reapi/src/hook_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ hook_t hooklist_gamedll[] = {
DLL(AddMultiDamage),
DLL(ApplyMultiDamage),
DLL(BuyItem),
DLL(PrecacheItemInfo),
};

hook_t hooklist_animating[] = {
Expand Down
2 changes: 2 additions & 0 deletions reapi/src/hook_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ enum GamedllFunc
RG_ApplyMultiDamage,
RG_BuyItem,

RG_PrecacheItemInfo,

// [...]
};

Expand Down
116 changes: 116 additions & 0 deletions reapi/src/natives/natives_misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2381,6 +2381,120 @@ cell AMX_NATIVE_CALL rg_get_global_iteminfo(AMX *amx, cell *params)
return TRUE;
}

/**
* Sets item info data from ItemInfo handle. Needs to be used in PrecacheItemInfo.
*
* @param iteminfo Item info handle.
* @param type Item info type. See ItemInfo constants.
*
* native set_iteminfo(const iteminfo, const ItemInfo:type, any:...);
*/
cell AMX_NATIVE_CALL set_iteminfo(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_handle, arg_type, arg_value };

ItemInfo *II = reinterpret_cast<ItemInfo *>(params[arg_handle]);

if (unlikely(II == nullptr))
{
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: invalid iteminfo handle", __FUNCTION__);
return FALSE;
}

char itembuf[256];
ItemInfo_e type = static_cast<ItemInfo_e>(params[arg_type]);
cell *value = getAmxAddr(amx, params[arg_value]);

switch (type)
{
case ItemInfo_iSlot: II->iSlot = *value; break;
case ItemInfo_iPosition: II->iPosition = *value; break;
case ItemInfo_iMaxAmmo1: II->iMaxAmmo1 = *value; break;
case ItemInfo_iMaxAmmo2: II->iMaxAmmo2 = *value; break;
case ItemInfo_iMaxClip: II->iMaxClip = *value; break;
case ItemInfo_iId: II->iId = *value; break;
case ItemInfo_iFlags: II->iFlags = *value; break;
case ItemInfo_iWeight: II->iWeight = *value; break;
case ItemInfo_pszAmmo1: II->pszAmmo1 = STRING(getAmxStringAlloc(amx, params[arg_value], itembuf)); break;
case ItemInfo_pszAmmo2: II->pszAmmo2 = STRING(getAmxStringAlloc(amx, params[arg_value], itembuf)); break;
case ItemInfo_pszName: II->pszName = STRING(getAmxStringAlloc(amx, params[arg_value], itembuf)); break;
default:
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: Invalid ItemInfo type %d", __FUNCTION__, type);
return FALSE;
}

return TRUE;
}

/**
* Gets ItemInfo data from ItemInfo handle. Needs to be used in PrecacheItemInfo.
*
* @param iteminfo Item info handle.
* @param type Item info type. See ItemInfo constants.
*
* native get_iteminfo(const iteminfo, const ItemInfo:type, any:...);
*/
cell AMX_NATIVE_CALL get_iteminfo(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_handle, arg_type, arg_output, arg_len };

ItemInfo* II = reinterpret_cast<ItemInfo*>(params[arg_handle]);

if (unlikely(II == nullptr))
{
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: invalid iteminfo handle", __FUNCTION__);
return FALSE;
}

ItemInfo_e type = static_cast<ItemInfo_e>(params[arg_type]);
cell* dest = getAmxAddr(amx, params[arg_output]);
size_t len = (PARAMS_COUNT == 4) ? *getAmxAddr(amx, params[arg_len]) : 0;

switch (type)
{
case ItemInfo_iSlot: return II->iSlot;
case ItemInfo_iPosition: return II->iPosition;
case ItemInfo_iMaxAmmo1: return II->iMaxAmmo1;
case ItemInfo_iMaxAmmo2: return II->iMaxAmmo2;
case ItemInfo_iMaxClip: return II->iMaxClip;
case ItemInfo_iId: return II->iId;
case ItemInfo_iFlags: return II->iFlags;
case ItemInfo_iWeight: return II->iWeight;
case ItemInfo_pszAmmo1:
if (II->pszAmmo1 == nullptr)
{
setAmxString(dest, "", 1);
break;
}

setAmxString(dest, II->pszAmmo1, len);
break;
case ItemInfo_pszAmmo2:
if (II->pszAmmo2 == nullptr)
{
setAmxString(dest, "", 1);
break;
}

setAmxString(dest, II->pszAmmo2, len);
break;
case ItemInfo_pszName:
if (II->pszName == nullptr)
{
setAmxString(dest, "", 1);
break;
}

setAmxString(dest, II->pszName, len);
break;
default:
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: Invalid ItemInfo type %d", __FUNCTION__, type);
return FALSE;
}

return TRUE;
}

/*
* Adds hint message to the queue.
*
Expand Down Expand Up @@ -3305,6 +3419,8 @@ AMX_NATIVE_INFO Misc_Natives_RG[] =
{ "rg_get_iteminfo", rg_get_iteminfo },
{ "rg_set_global_iteminfo", rg_set_global_iteminfo },
{ "rg_get_global_iteminfo", rg_get_global_iteminfo },
{ "set_iteminfo", set_iteminfo },
{ "get_iteminfo", get_iteminfo },

{ "rg_hint_message", rg_hint_message },

Expand Down

0 comments on commit e8b639d

Please sign in to comment.