New hooks (stocks, natives, improved publics)
#1

OK, question:

I’ve been thinking about extensions to y_hooks for a while now (I’ve mentioned this a few times already). Namely the ability to hook regular and native functions. In theory the code would look something like this:

Code:

hook native SetPlayerPos(playerid, Float:x, Float:y, Float:z)
{
    // What goes in here?
}
There are two options:
  1. Explicit chaining. So your hook would look like this:

    Code:
    
    hook native SetPlayerPos(playerid, Float:x, Float:y, Float:z)
    {
        printf("cool");
        return SetPlayerPos(playerid, x, y z); // NOT recursive, calls the next hook, or the native.
    }
    
    This has several advantages:
    • Control flow is obvious and explicit.
    • Preventing and manipulating when the next function is called is easy.
    • Return values are easy to deal with. This wasn’t an issue with publics, because they only return 0 or 1, but other function types can return anything.
    • The generated code is MUCH simpler.
    • Compile-time errors if the original function you are hooking (for hook stock) doesn’t exist. You can hook publics that don’t exist, but not natives and stocks.
  2. Implicit chaining.

    So within the hook you don’t call the original SetPlayerPos, all hooks and the original are called one after the other. I really don’t like this idea - it makes writing hook code almost impossible. HOWEVER, this is how hooked publics currently work, and I’m a big fan of consistency. On the other hand, since you can hook publics that don’t actually exist, requiring them to exist breaks the whole situation entirely.
I’m thinking about having both methods available for publics. The legacy code will continue to operate exactly as it did before:

Code:

hook OnPlayerConnect(playerid)
{
    printf("cool");
    return 1;
}
Or using the newer syntax will require explicit chaining, but would also require that the next callback exists already:

Code:

hook public OnPlayerConnect(playerid)
{
    printf("cool");
    return OnPlayerConnect(playerid);
}
Thoughts?
Reply


Messages In This Thread
New hooks (stocks, natives, improved publics) - by Y_Less - 06.05.2018, 15:19
Re: New hooks (stocks, natives, improved publics) - by IS4 - 06.05.2018, 16:41
Re: New hooks (stocks, natives, improved publics) - by Gr00t - 17.05.2018, 19:50
Re: New hooks (stocks, natives, improved publics) - by Graber - 20.05.2018, 21:06
Re: New hooks (stocks, natives, improved publics) - by Gr00t - 24.07.2018, 14:35
Re: New hooks (stocks, natives, improved publics) - by Logic_ - 13.09.2018, 16:14
Re: New hooks (stocks, natives, improved publics) - by Sasino97 - 03.12.2018, 15:30

Forum Jump:


Users browsing this thread: 1 Guest(s)