SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Discussion

Reply
 
Thread Tools Display Modes
Old 06/05/2018, 03:19 PM   #1
Y_Less
Spam Machine
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 14,666
Reputation: 3061
Default New hooks (stocks, natives, improved publics)

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?

Last edited by Y_Less; 12/05/2018 at 11:59 PM.
Y_Less is offline   Reply With Quote
Old 06/05/2018, 04:41 PM   #2
IS4
Little Clucker
 
IS4's Avatar
 
Join Date: Apr 2018
Posts: 7
Reputation: 45
Default Re: New hooks (stocks, natives, improved publics)

Using implicit chaining, you would have to add syntax for both prehooks and posthooks, in case you needed to check the returned value of the actual function. You might also need to pass the arguments by reference, since a prehook could also want to change their value.
IS4 is offline   Reply With Quote
Old 17/05/2018, 07:50 PM   #3
Gr00t
Little Clucker
 
Join Date: Apr 2018
Posts: 13
Reputation: 2
Default Re: New hooks (stocks, natives, improved publics)

This would make easier script anticheat include, debug include, player config include for example:
Code:
hook native PlayAudioStreamForPlayer(playerid, url[], Float:posX = 0.0, Float:posY = 0.0, Float:posZ = 0.0, Float:distance = 50.0, usepos = 0)
{
	if(pAudioEnabled[playerid]) return PlayAudioStreamForPlayer(playerid, url, posX, posY, posZ, distance, usepos);
	return 0;
}
Gr00t is offline   Reply With Quote
Old 20/05/2018, 09:06 PM   #4
AdrianGraber
Big Clucker
 
AdrianGraber's Avatar
 
Join Date: Mar 2013
Location: Germany
Posts: 97
Reputation: 102
Default Re: New hooks (stocks, natives, improved publics)

This would be very useful. Libraries such as anticheats and so on could benefit a lot from this. Besides, it allows for cleaner code when applying, like Gr00t said, custom configs and such. It would allow to write code like that and rely on the hooks you make.

Regarding the proposed new explicit syntax for public functions, it would allow a more obvious and probably even more powerful control flow. It would be great to see this implemented.
AdrianGraber is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Needs advice for timers, stocks, publics and macros SteeLFelna Scripting Help 2 16/11/2015 09:08 PM
Combining includes with publics and stocks. HELP PaulDinam Scripting Help 6 14/05/2013 05:28 PM
[Pedido] Tutorial de stocks/natives Mandrack_FreeZe PortuguÍs/Portuguese 5 15/08/2012 03:17 AM
Question | what the different about stocks and publics avivelkayam Scripting Help 2 07/07/2012 03:38 PM


All times are GMT. The time now is 07:16 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.