Skip to content

Commit

Permalink
Add new return type for amxx forwards
Browse files Browse the repository at this point in the history
  • Loading branch information
s1lentq committed Aug 7, 2024
1 parent 8f9e7c1 commit 592ebca
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
5 changes: 4 additions & 1 deletion reapi/extra/amxmodx/scripting/include/reapi.inc
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,11 @@ enum
{
HC_CONTINUE = 0, // Plugin didn't take any action
HC_SUPERCEDE, // Skip real function, use my return value
HC_BREAK // Skip all forwards and real function, use my return value
HC_BREAK, // Skip all forwards and real function, use my return value
// @note Warning: Be very careful, using this type of return will skip calls for all following AMXX plugins

HC_BYPASS // Skip calls for all following AMXX plugins, but call the original function
// @note Warning: In PRE skips all forwards including POST forwards
};

/**
Expand Down
38 changes: 23 additions & 15 deletions reapi/src/hook_callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
// hookchain return type
enum HookChainState
{
HC_CONTINUE = 0, // plugin didn't take any action
HC_SUPERCEDE, // skip real function, use my return value
HC_BREAK // skip all forwards and real function, use my return value
// @note Warning: Be very careful using this type of return will skip calls for all following AMXX the plugins.
HC_CONTINUE = 0, // Plugin didn't take any action
HC_SUPERCEDE, // Skip real function, use my return value
HC_BREAK, // Skip all forwards and real function, use my return value
// @note Warning: Be very careful using this type of return will skip calls for all following AMXX the plugins

HC_BYPASS // Skip calls for all following AMXX plugins, but call the original function
// @note Warning: In PRE skips all forwards including POST forwards
};

// api types
Expand Down Expand Up @@ -168,15 +171,17 @@ NOINLINE void DLLEXPORT _callVoidForward(hook_t* hook, original_t original, f_ar
if (likely(fwd->GetState() == FSTATE_ENABLED))
{
hookCtx->SetId(fwd->GetIndex()); // set current handler hook
auto ret = g_amxxapi.ExecuteForward(fwd->GetFwdIndex(), std::forward<f_args &&>(args)...);
int ret = g_amxxapi.ExecuteForward(fwd->GetFwdIndex(), std::forward<f_args &&>(args)...);
hookCtx->ResetId();

if (unlikely(ret == HC_BREAK)) {
if (unlikely(ret == HC_BREAK))
return;
}

if (unlikely(ret > hc_state))
hc_state = ret;

if (unlikely(ret == HC_BYPASS))
break;
}
}

Expand All @@ -187,16 +192,19 @@ NOINLINE void DLLEXPORT _callVoidForward(hook_t* hook, original_t original, f_ar
hook->wasCalled = true;
}

for (auto fwd : hook->post)
if (hc_state != HC_BYPASS)
{
if (likely(fwd->GetState() == FSTATE_ENABLED))
for (auto fwd : hook->post)
{
hookCtx->SetId(fwd->GetIndex()); // set current handler hook
auto ret = g_amxxapi.ExecuteForward(fwd->GetFwdIndex(), std::forward<f_args &&>(args)...);
hookCtx->ResetId();

if (unlikely(ret == HC_BREAK))
break;
if (likely(fwd->GetState() == FSTATE_ENABLED))
{
hookCtx->SetId(fwd->GetIndex()); // set current handler hook
int ret = g_amxxapi.ExecuteForward(fwd->GetFwdIndex(), std::forward<f_args &&>(args)...);
hookCtx->ResetId();

if (unlikely(ret == HC_BREAK || ret == HC_BYPASS))
break;
}
}
}

Expand Down

0 comments on commit 592ebca

Please sign in to comment.