• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Library] y_hooks 4 - Hook callbacks, functions, and natives.
#1
Introduction



After some discussion recently, I have finally gotten around to updating y_hooks. You can now hook callbacks (publics), functions (your own code), and natives! The full documentation is on github:



https://github.com/pawn-lang/YSI-Includes/tree/5.x



But in short, there are three types of hooks: original, callback, and function.



Original



Originals are still the fastest (faster even than a normal function call) and simplest, but can only hook publics:



PHP Code:
#include <YSI_Coding\y_hooks>



hook OnPlayerConnect(playerid)

{

    
SendClientMessage(playeridCOLOUR_GREETING"Welcome!");





The system will call each hook of a public in the order they are declared. When one ends the next starts. There is a way to stop the chain entirely, such that no more hooks are called, but this is all the control given. These can hook callbacks that don't even exist, you don't need `public OnPlayerConnect` anywhere in code for this example to work.



Function



A function hook hooks a function - i.e. something called within the script. This includes pawn functions and natives:



PHP Code:
#include <YSI_Coding\y_hooks>



hook function SetPlayerPos(playeridFloat:xFloat:yFloat:z)

{

    return continue(
playeridxyz  0.1);





Every time `SetPlayerPos` is used in the script, this hook will be called first. `continue` is an explicit call to the next function in the chain - this may be another hook or the original code (a native in this case).



Callback



A callback hook combines the hooking of callbacks from original hooks with the explicit chaining of function hooks:



PHP Code:
#include <YSI_Coding\y_hooks>



hook callback OnPlayerConnect(playerid)

{

    
SendClientMessage(playeridCOLOUR_GREETING"Welcome!");

    return continue(
playerid);





Note that I haven't ACTUALLY written this last one 1yet.
  Reply
#2
Will definitely start using this immediately, will be waiting for you to finish the callback one :)



Great work as always ;)
Remember to always refer to J0sh as `J0sh...`



@ Networks/Servers

San Andreas Gaming Network (Owner/Founder)

San Andreas Gaming (Owner/Founder)

Grand Theft Cop's n Robber's (Owner)

Britannia Roleplay (Owner/Founder) [Retired]

Alpine RP (Owner/Founder)

Aluminium Network (Maintainer) [Disbanded]

AlphaDM (Tech Support) [Disbanded]



# Services

forum.open.mp (Forum Manager) (Formerly Burgershot.gg

open.mp (Member)



~ Languages/Frameworks

Pawn, C, C, C#, Javascript, Typescript, Lua, Python, Go, Rust, PHP, SQL,

Angular, React, Vue, Svelte, Laravel, Rocket
  Reply
#3
Now uploaded. More information in these links:



https://github.com/pawn-lang/YSI-Include...k-start.md

https://github.com/pawn-lang/YSI-Include...eatures.md

https://github.com/pawn-lang/YSI-Include...ks/faqs.md
  Reply
#4
Thank you! a great improvement :)
  Reply
#5
Nice work, thanks
  Reply
#6
Fantastic! Time to start converting all my ALS hooks...
  Reply
#7
v0.2: Fixed Linux. Turns out SYSREQ.pri is broken there.



Also, how to hook long function names. `DEFINE_HOOK_REPLACEMENT` doesn't work with these (and sadly can't). But this does work:



PHP Code:
#define SPP SetPlayerPos



hook function SPP(playeridFloat:xFloat:yFloat:z)

{

    return continue(
playeridxyz  0.1);


  Reply
#8
Dope shit. Well done sir.
  Reply
#9
(2019-06-02, 09:14 PM)Y_Less Wrote:
PHP Code:
#include <YSI_Coding\y_hooks>



hook function SetPlayerPos(playeridFloat:xFloat:yFloat:z)

{

return continue(
playeridxyz  0.1);









Last time I talked to you, I was told to do "hook native SetPlayerPos". Does that still work? or should I switch those to "hook function SetPlayerPos"?









EDIT

I got a response via Discord. For anyone else who might want to know this; yes "hook native" & "hook function" both do the same thing for hooking natives.
  Reply
#10
"hook stock" also does the same thing, because many people incorrectly call functions "stocks", so I supported that as well. It turns out this works because the code to hook those different function types is identical.
  Reply


Forum Jump: