Skip to content

Commit

Permalink
Branch prediction optimization #3
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
theAsmodai committed Oct 18, 2016
1 parent cc739f1 commit 0a03f20
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 35 deletions.
19 changes: 9 additions & 10 deletions reapi/src/hook_callback.cpp
Expand Up @@ -275,8 +275,7 @@ CBasePlayer *CBasePlayer_Observer_IsValidTarget(IReGameHook_CBasePlayer_Observer
{
auto original = [chain](int _pthis, int _iPlayerIndex, bool _bSameTeam)
{
auto pPlayer = chain->callNext(getPrivate<CBasePlayer>(_pthis), _iPlayerIndex, _bSameTeam);
return pPlayer ? indexOfEdict(pPlayer->pev) : 0;
return indexOfPDataAmx(chain->callNext(getPrivate<CBasePlayer>(_pthis), _iPlayerIndex, _bSameTeam));
};

return getPrivate<CBasePlayer>(callForward<size_t>(RG_CBasePlayer_Observer_IsValidTarget, original, indexOfEdict(pthis->pev), iPlayerIndex, bSameTeam));
Expand Down Expand Up @@ -526,24 +525,24 @@ void CSGameRules_ClientUserInfoChanged(IReGameHook_CSGameRules_ClientUserInfoCha
callVoidForward(RG_CSGameRules_ClientUserInfoChanged, original, indexOfEdict(pPlayer->pev), infobuffer);
}

void CSGameRules_PlayerKilled(IReGameHook_CSGameRules_PlayerKilled *chain, CBasePlayer *pVictim, entvars_t *pKiller, entvars_t *pInflictor)
void CSGameRules_PlayerKilled(IReGameHook_CSGameRules_PlayerKilled *chain, CBasePlayer *pVictim, entvars_t *pevKiller, entvars_t *pevInflictor)
{
auto original = [chain](int _pVictim, int _pKiller, int _pInflictor)
auto original = [chain](int _pVictim, int _pevKiller, int _pevInflictor)
{
chain->callNext(getPrivate<CBasePlayer>(_pVictim), PEV(_pKiller), PEV(_pInflictor));
chain->callNext(getPrivate<CBasePlayer>(_pVictim), PEV(_pevKiller), PEV(_pevInflictor));
};

callVoidForward(RG_CSGameRules_PlayerKilled, original, indexOfEdict(pVictim->pev), indexOfEdict(pKiller), pInflictor ? indexOfEdict(pInflictor) : -1);
callVoidForward(RG_CSGameRules_PlayerKilled, original, indexOfEdict(pVictim->pev), indexOfEdict(pevKiller), indexOfEdictAmx(pevInflictor));
}

void CSGameRules_DeathNotice(IReGameHook_CSGameRules_DeathNotice *chain, CBasePlayer *pVictim, entvars_t *pKiller, entvars_t *pevInflictor)
void CSGameRules_DeathNotice(IReGameHook_CSGameRules_DeathNotice *chain, CBasePlayer *pVictim, entvars_t *pevKiller, entvars_t *pevInflictor)
{
auto original = [chain](int _pVictim, int _pKiller, int _pevInflictor)
auto original = [chain](int _pVictim, int _pevKiller, int _pevInflictor)
{
chain->callNext(getPrivate<CBasePlayer>(_pVictim), PEV(_pKiller), PEV(_pevInflictor));
chain->callNext(getPrivate<CBasePlayer>(_pVictim), PEV(_pevKiller), PEV(_pevInflictor));
};

callVoidForward(RG_CSGameRules_DeathNotice, original, indexOfEdict(pVictim->pev), indexOfEdict(pKiller), pevInflictor ? indexOfEdict(pevInflictor) : -1);
callVoidForward(RG_CSGameRules_DeathNotice, original, indexOfEdict(pVictim->pev), indexOfEdict(pevKiller), indexOfEdictAmx(pevInflictor));
}

int CSGameRules_CanHavePlayerItem(IReGameHook_CSGameRules_CanHavePlayerItem *chain, CBasePlayer *pPlayer, CBasePlayerItem *pItem)
Expand Down
4 changes: 2 additions & 2 deletions reapi/src/hook_callback.h
Expand Up @@ -338,8 +338,8 @@ void CSGameRules_PlayerSpawn(IReGameHook_CSGameRules_PlayerSpawn *chain, CBasePl
BOOL CSGameRules_FPlayerCanRespawn(IReGameHook_CSGameRules_FPlayerCanRespawn *chain, CBasePlayer *pPlayer);
edict_t *CSGameRules_GetPlayerSpawnSpot(IReGameHook_CSGameRules_GetPlayerSpawnSpot *chain, CBasePlayer *pPlayer);
void CSGameRules_ClientUserInfoChanged(IReGameHook_CSGameRules_ClientUserInfoChanged *chain, CBasePlayer *pPlayer, char *infobuffer);
void CSGameRules_PlayerKilled(IReGameHook_CSGameRules_PlayerKilled *chain, CBasePlayer *pVictim, entvars_t *pKiller, entvars_t *pInflictor);
void CSGameRules_DeathNotice(IReGameHook_CSGameRules_DeathNotice *chain, CBasePlayer *pVictim, entvars_t *pKiller, entvars_t *pevInflictor);
void CSGameRules_PlayerKilled(IReGameHook_CSGameRules_PlayerKilled *chain, CBasePlayer *pVictim, entvars_t *pevKiller, entvars_t *pevInflictor);
void CSGameRules_DeathNotice(IReGameHook_CSGameRules_DeathNotice *chain, CBasePlayer *pVictim, entvars_t *pevKiller, entvars_t *pevInflictor);
int CSGameRules_CanHavePlayerItem(IReGameHook_CSGameRules_CanHavePlayerItem *chain, CBasePlayer *pPlayer, CBasePlayerItem *pItem);
int CSGameRules_DeadPlayerWeapons(IReGameHook_CSGameRules_DeadPlayerWeapons *chain, CBasePlayer *pPlayer);
void CSGameRules_ServerDeactivate(IReGameHook_CSGameRules_ServerDeactivate *chain);
Expand Down
5 changes: 5 additions & 0 deletions reapi/src/hook_list.h
Expand Up @@ -31,6 +31,11 @@ extern hook_t hooklist_animating[];
extern hook_t hooklist_player[];
extern hook_t hooklist_gamerules[];

enum
{
INVALID_HOOKCHAIN = 0
};

struct hooklist_t
{
hook_t *operator[](size_t hook) const
Expand Down
8 changes: 4 additions & 4 deletions reapi/src/natives/natives_hookchains.cpp
Expand Up @@ -22,28 +22,28 @@ cell AMX_NATIVE_CALL RegisterHookChain(AMX *amx, cell *params)
if (unlikely(hook == nullptr))
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s: function with id (%d) doesn't exist in current API version.", __FUNCTION__, func);
return 0;
return INVALID_HOOKCHAIN;
}

if (unlikely(!hook->checkRequirements()))
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s: function (%s) is not available, %s required.", __FUNCTION__, hook->func_name, hook->depend_name);
return 0;
return INVALID_HOOKCHAIN;
}

int funcid;
const char *funcname = getAmxString(amx, params[arg_handler]);
if (unlikely(g_amxxapi.amx_FindPublic(amx, funcname, &funcid) != AMX_ERR_NONE))
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s: public function \"%s\" not found.", __FUNCTION__, funcname);
return 0;
return INVALID_HOOKCHAIN;
}

int fwid = hook->registerForward(amx, funcname);
if (unlikely(fwid == -1))
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s: register forward failed.", __FUNCTION__);
return 0;
return INVALID_HOOKCHAIN;
}

return g_hookManager.addHandler(amx, func, fwid, post != 0);
Expand Down
27 changes: 16 additions & 11 deletions reapi/src/precompiled.h
@@ -1,54 +1,59 @@
#pragma once

// C
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h> // for strncpy(), etc

// C++
#include <vector> // std::vector

// platform defs
#include <platform.h>

// cssdk
#include <extdll.h>
#include <eiface.h>
#include <meta_api.h>

#include <cbase.h>
#include <pm_defs.h>
#include <pm_defs.h>
#include <pm_movevars.h>
#include <com_progdefs.h>
#include <gamerules.h>

#include <osdep.h> // win32 vsnprintf, etc
#include <osconfig.h>
#include <sdk_util.h>
#include <platform.h>
// metamod SDK
#include <meta_api.h>

// regamedll API
#include "gamerules.h"
#include "regamedll_api.h"
#include <regamedll_api.h>
#include "mod_regamedll_api.h"

// rehlds API
#include "rehlds_api.h"
#include <rehlds_api.h>
#include "mod_rehlds_api.h"

// VTC API
#include "vtc_api.h"
#include <vtc_api.h>
#include "mod_vtc_api.h"

// Reunion API
#include "reunion_api.h"
#include <reunion_api.h>
#include "mod_reunion_api.h"

// AmxModX API
#include "amxxmodule.h"

// reapi main
#include "main.h"
#include "reapi_utils.h"
#include "api_config.h"
#include "hook_manager.h"
#include "hook_callback.h"
#include "member_list.h"

// natives
#include "natives_hookchains.h"
#include "natives_members.h"
#include "natives_misc.h"
Expand Down
35 changes: 27 additions & 8 deletions reapi/src/reapi_utils.h
Expand Up @@ -6,6 +6,7 @@ char(&ArraySizeHelper(T(&array)[N]))[N];

#define INDEXENT edictByIndex
#define ENTINDEX indexOfEdict
#define AMX_NULLENT -1

extern enginefuncs_t* g_pengfuncsTable;
extern DLL_FUNCTIONS *g_pFunctionTable;
Expand Down Expand Up @@ -35,13 +36,13 @@ inline size_t indexOfEdict(entvars_t* pev)
return indexOfEdict(pev->pContainingEntity);
}

// safe to index -1
inline edict_t* edictByIndexAmx(int index)
// safe to nullptr
inline size_t indexOfEdictAmx(entvars_t* pev)
{
auto ed = g_pEdicts + index;
if (unlikely(index < 0))
ed = nullptr;
return ed;
size_t index = AMX_NULLENT;
if (likely(pev != nullptr))
index = indexOfEdict(pev);
return index;
}

// fast
Expand All @@ -50,23 +51,41 @@ inline edict_t* edictByIndex(int index)
return g_pEdicts + index;
}

// safe to index -1
inline edict_t* edictByIndexAmx(int index)
{
auto ed = g_pEdicts + index;
if (unlikely(index < 0)) // == AMX_NULLENT
ed = nullptr;
return ed;
}

template<typename T>
inline T* getPrivate(int index)
{
T* pdata = nullptr;
if (likely(index >= 0))
if (likely(index >= 0)) // != AMX_NULLENT
pdata = (T *)g_pEdicts[index].pvPrivateData;
return pdata;
}

inline entvars_t* PEV(int index)
{
entvars_t* pvars = nullptr;
if (likely(index >= 0))
if (likely(index >= 0)) // != AMX_NULLENT
pvars = &g_pEdicts[index].v;
return pvars;
}

template<typename T>
inline size_t indexOfPDataAmx(T* pdata)
{
size_t index = AMX_NULLENT;
if (likely(pdata != nullptr))
index = indexOfEdict(pdata->pev);
return index;
}

// HLTypeConversion.h -> AMXModX
template <typename T>
inline T &ref_member(void *ptr, int offset, int element = 0)
Expand Down

0 comments on commit 0a03f20

Please sign in to comment.