Burgershot
  • Home
  • Members
  • Team
  • Help
  • Search
  • Register
  • Login
  • Home
  • Members
  • Help
  • Search
Burgershot SA-MP Releases Libraries [Library] y_hooks 4 - Hook callbacks, functions, and natives.

 
  • 0 Vote(s) - 0 Average
Library y_hooks 4 - Hook callbacks, functions, and natives.
Y_Less
Offline

Administrator

Posts: 323
Threads: 16
Joined: Feb 2019
Reputation: 90
#1
2019-06-02, 09:14 PM (This post was last modified: 2019-06-02, 09:17 PM by Y_Less.)
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(playerid, COLOUR_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(playerid, Float:x, Float:y, Float:z)
{
    return continue(
playerid, x, y, z + 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(playerid, COLOUR_GREETING, "Welcome!");
    return continue(
playerid);
} 

Note that I haven't ACTUALLY written this last one 1yet.
JustMichael
Offline

Forum Manager

Posts: 189
Threads: 3
Joined: Feb 2019
Reputation: 17
Location: England
#2
2019-06-02, 09:18 PM
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)
Alpine RP (Owner/Founder)
Aluminium Network (Maintainer)
AlphaDM (Tech Support)

# Services
Burgershot.gg (Forum Manager)
open.mp (Member)

~ Languages/Frameworks
Pawn, C, C++, C#, Javascript, Typescript, Lua, Python, Go, Rust, PHP, SQL,
Angular, React, Vue, Svelte, Laravel, Rocket
Y_Less
Offline

Administrator

Posts: 323
Threads: 16
Joined: Feb 2019
Reputation: 90
#3
2019-06-02, 10:20 PM
Now uploaded. More information in these links:

https://github.com/pawn-lang/YSI-Includes/blob/5.x/YSI_Coding/y_hooks/quick-start.md
https://github.com/pawn-lang/YSI-Includes/blob/5.x/YSI_Coding/y_hooks/features.md
https://github.com/pawn-lang/YSI-Includes/blob/5.x/YSI_Coding/y_hooks/faqs.md
Stanislav
Offline

Burgershot Member
Posts: 6
Threads: 0
Joined: Apr 2019
Reputation: 0
#4
2019-06-02, 10:43 PM
Thank you! a great improvement :)
Dayrion
Offline

Burgershot Member
Posts: 18
Threads: 0
Joined: Apr 2019
Reputation: 0
#5
2019-06-03, 10:29 AM
Nice work, thanks
spacemud
Offline

open.mp Developer

Posts: 33
Threads: 1
Joined: Feb 2019
Reputation: 3
#6
2019-06-03, 05:04 PM
Fantastic! Time to start converting all my ALS hooks...
Y_Less
Offline

Administrator

Posts: 323
Threads: 16
Joined: Feb 2019
Reputation: 90
#7
2019-08-04, 07:22 AM
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(playerid, Float:x, Float:y, Float:z)
{
    return continue(
playerid, x, y, z + 0.1);
} 
Logan
Offline

Burgershot Member
Posts: 21
Threads: 5
Joined: Jun 2019
Reputation: 1
Location: Croatia
#8
2019-08-09, 12:58 PM
Dope shit. Well done sir.
Chaprnks
Offline

Burgershot Member
Posts: 9
Threads: 0
Joined: May 2019
Reputation: 0
Location: Soviet America
#9
2020-03-31, 03:05 PM (This post was last modified: 2020-04-05, 02:04 PM by Chaprnks.)
(2019-06-02, 09:14 PM)Y_Less Wrote:
PHP Code:
#include <YSI_Coding\y_hooks>

hook function SetPlayerPos(playerid, Float:x, Float:y, Float:z)
{
return continue(
playerid, x, y, z + 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.
Y_Less
Offline

Administrator

Posts: 323
Threads: 16
Joined: Feb 2019
Reputation: 90
#10
2020-04-23, 10:24 AM
"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.
« Next Oldest | Next Newest »



  • View a Printable Version
  • Subscribe to this thread
Forum Jump:

© Burgershot - Powered by our Community and MyBB Original Theme by Emerald

Linear Mode
Threaded Mode