Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
theAsmodai committed Jun 7, 2016
1 parent 6fb4fc6 commit 4c6f3c3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 35 deletions.
67 changes: 39 additions & 28 deletions reapi/src/hook_callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ struct retval_t
{
char* _string;
float _float;
int _interger;
CBaseEntity* _classptr;
int _integer;
CBaseEntity* _classptr;
edict_t* _edict;
entvars_t* _pev;
};
Expand All @@ -39,45 +39,53 @@ struct retval_t
inline AType getApiType(int) { return ATYPE_INTEGER; }
inline AType getApiType(unsigned) { return ATYPE_INTEGER; }
inline AType getApiType(float) { return ATYPE_FLOAT; }
inline AType getApiType(const char *) { return ATYPE_STRING; }
inline AType getApiType(char []) { return ATYPE_STRING; }
inline AType getApiType(CBaseEntity *) { return ATYPE_CLASSPTR; }
inline AType getApiType(edict_t *) { return ATYPE_CLASSPTR; }
inline AType getApiType(entvars_t *) { return ATYPE_EVARS; }
inline AType getApiType(const char *) { return ATYPE_STRING; }
inline AType getApiType(char []) { return ATYPE_STRING; }
inline AType getApiType(CBaseEntity *) { return ATYPE_CLASSPTR; }
inline AType getApiType(edict_t *) { return ATYPE_CLASSPTR; }
inline AType getApiType(entvars_t *) { return ATYPE_EVARS; }

template<typename T>
inline AType getApiType(T *) { return ATYPE_INTEGER; }

#define MAX_ARGS 12u
#define MAX_HOOKCHAIN_ARGS 12u

template<size_t current = 0, typename T1, typename T2, typename T3, typename T4, typename ...t_args>
void setupArgTypes(AType args_type[MAX_ARGS], T1, T2, T3, T4, t_args... args)
void setupArgTypes(AType args_type[], T1, T2, T3, T4, t_args... args)
{
*(uint32 *)&args_type[current] = getApiType(T1()) | (getApiType(T2()) << 8) | (getApiType(T3()) << 16) | (getApiType(T4()) << 24);
if (sizeof...(args) && current + 1 < MAX_ARGS)
if (current + 4 <= MAX_HOOKCHAIN_ARGS)
*(uint32 *)&args_type[current] = getApiType(T1()) | (getApiType(T2()) << 8) | (getApiType(T3()) << 16) | (getApiType(T4()) << 24);
if (sizeof...(args) && current + 4 < MAX_HOOKCHAIN_ARGS)
setupArgTypes<current + 4>(args_type, args...);
}

template<size_t current = 0, typename T1, typename T2, typename T3>
void setupArgTypes(AType args_type[MAX_ARGS], T1, T2, T3)
void setupArgTypes(AType args_type[], T1, T2, T3)
{
*(uint32 *)&args_type[current] = getApiType(T1()) | (getApiType(T2()) << 8) | (getApiType(T3()) << 16);
if (current + 3 <= MAX_HOOKCHAIN_ARGS)
*(uint32 *)&args_type[current] = getApiType(T1()) | (getApiType(T2()) << 8) | (getApiType(T3()) << 16);
else
setupArgTypes(args_type, T1(), T2());
}

template<size_t current = 0, typename T1, typename T2>
void setupArgTypes(AType args_type[MAX_ARGS], T1, T2)
void setupArgTypes(AType args_type[], T1, T2)
{
*(uint16 *)&args_type[current] = getApiType(T1()) | (getApiType(T2()) << 8);
if (current + 2 <= MAX_HOOKCHAIN_ARGS)
*(uint16 *)&args_type[current] = getApiType(T1()) | (getApiType(T2()) << 8);
else
setupArgTypes(args_type, T1());
}

template<size_t current = 0, typename T>
void setupArgTypes(AType args_type[MAX_ARGS], T)
void setupArgTypes(AType args_type[], T)
{
args_type[current] = getApiType(T());
if (current + 1 <= MAX_HOOKCHAIN_ARGS)
args_type[current] = getApiType(T());
}

template<size_t current = 0>
void setupArgTypes(AType args_type[MAX_ARGS])
void setupArgTypes(AType args_type[])
{
}

Expand All @@ -86,7 +94,7 @@ struct hookctx_t
template<typename ...t_args>
hookctx_t(size_t arg_count, t_args... args)
{
args_count = min(arg_count, MAX_ARGS);
args_count = min(arg_count, MAX_HOOKCHAIN_ARGS);
setupArgTypes(args_type, args...);
}

Expand All @@ -100,7 +108,7 @@ struct hookctx_t
retval_t retVal;
size_t args_count;
size_t args_ptr;
AType args_type[MAX_ARGS];
AType args_type[MAX_HOOKCHAIN_ARGS];
};

extern hookctx_t* g_hookCtx;
Expand All @@ -112,7 +120,7 @@ NOINLINE void DLLEXPORT _callVoidForward(const hook_t* hook, original_t original
hookCtx->reset(size_t(&original) + sizeof(original));
int hc_state = HC_CONTINUE;

for (auto& fwd : hook->pre)
for (auto fwd : hook->pre)
{
if (fwd->GetState() == FSTATE_ENABLED)
{
Expand All @@ -133,7 +141,7 @@ NOINLINE void DLLEXPORT _callVoidForward(const hook_t* hook, original_t original
g_hookCtx = hookCtx;
}

for (auto& fwd : hook->post) {
for (auto fwd : hook->post) {
if (fwd->GetState() == FSTATE_ENABLED) {
auto ret = g_amxxapi.ExecuteForward(fwd->GetIndex(), args...);

Expand All @@ -160,22 +168,23 @@ NOINLINE R DLLEXPORT _callForward(const hook_t* hook, original_t original, volat
hookCtx->reset(size_t(&original) + sizeof(original), getApiType(R()));
int hc_state = HC_CONTINUE;

for (auto& fwd : hook->pre)
for (auto fwd : hook->pre)
{
if (fwd->GetState() == FSTATE_ENABLED)
{
auto ret = g_amxxapi.ExecuteForward(fwd->GetIndex(), args...);

if (ret == HC_CONTINUE)
if (ret == HC_CONTINUE) {
continue;
}

if (!hookCtx->retVal.set) {
g_amxxapi.LogError(fwd->GetAmx(), AMX_ERR_CALLBACK, "%s", "can't suppress original function call without new return value set");
continue;
}

if (ret == HC_BREAK) {
return *(R *)&hookCtx->retVal._interger;
return *(R *)&hookCtx->retVal._integer;
}

if (ret > hc_state)
Expand All @@ -190,10 +199,12 @@ NOINLINE R DLLEXPORT _callForward(const hook_t* hook, original_t original, volat
g_hookCtx = hookCtx;

if (hc_state != HC_OVERRIDE)
hookCtx->retVal._interger = *(int *)&retVal;
hookCtx->retVal._integer = *(int *)&retVal;

hookCtx->retVal.set = true;
}

for (auto& fwd : hook->post) {
for (auto fwd : hook->post) {
if (fwd->GetState() == FSTATE_ENABLED) {
auto ret = g_amxxapi.ExecuteForward(fwd->GetIndex(), args...);

Expand All @@ -202,7 +213,7 @@ NOINLINE R DLLEXPORT _callForward(const hook_t* hook, original_t original, volat
}
}

return *(R *)&hookCtx->retVal._interger;
return *(R *)&hookCtx->retVal._integer;
}

template <typename R, typename original_t, typename ...f_args>
Expand Down
16 changes: 11 additions & 5 deletions reapi/src/natives/natives_hookchains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ cell AMX_NATIVE_CALL RegisterHookChain(AMX *amx, cell *params)

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

Expand Down Expand Up @@ -130,7 +130,7 @@ cell AMX_NATIVE_CALL SetHookChainReturn(AMX *amx, cell *params)
{
case ATYPE_INTEGER:
case ATYPE_FLOAT:
retVal._interger = *srcAddr;
retVal._integer = *srcAddr;
break;

case ATYPE_STRING:
Expand Down Expand Up @@ -178,15 +178,21 @@ cell AMX_NATIVE_CALL GetHookChainReturn(AMX *amx, cell *params)
}

enum args_e { arg_count, arg_value, arg_maxlen };

auto& retVal = g_hookCtx->retVal;

if (!retVal.set)
{
MF_LogError(amx, AMX_ERR_NATIVE, "%s: return value isn't set.", __FUNCTION__);
return FALSE;
}

cell* dstAddr = getAmxAddr(amx, params[arg_value]);

switch (retVal.type)
{
case ATYPE_INTEGER:
case ATYPE_FLOAT:
return retVal._interger;
return retVal._integer;
case ATYPE_STRING:
{
if (PARAMS_COUNT != 2)
Expand Down Expand Up @@ -244,7 +250,7 @@ cell AMX_NATIVE_CALL SetHookChainArg(AMX *amx, cell *params)
return FALSE;
}

static char temp_strings[MAX_ARGS][1024];
static char temp_strings[MAX_HOOKCHAIN_ARGS][1024];

cell* srcAddr = getAmxAddr(amx, params[arg_value]);
size_t destAddr = g_hookCtx->args_ptr + number * sizeof(int);
Expand Down
4 changes: 2 additions & 2 deletions reapi/src/sdk_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
void __declspec(noreturn) UTIL_SysError(const char *fmt, ...)
{
va_list argptr;
static char string[8192];
char string[8192];

va_start(argptr, fmt);
vsnprintf(string, sizeof(string), fmt, argptr);
Expand Down Expand Up @@ -32,7 +32,7 @@ char *UTIL_VarArgs(char *format, ...)
void UTIL_LogPrintf(const char *fmt, ...)
{
va_list argptr;
static char string[1024];
char string[1024];

va_start(argptr, fmt);
vsprintf(string, fmt, argptr);
Expand Down

0 comments on commit 4c6f3c3

Please sign in to comment.