SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 18/03/2011, 01:17 AM   #1
Y_Less
Beta Tester
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 17,857
Reputation: 2478
Default Ultimate callback hooks!

I think this may literally be the ultimate callback hooking method EVER! Those of you who write libraries should know about ALS:

pawn Code:
public OnGameModeInit()
{
    // Do your code here...
    CallLocalFunction("S@@_OnGameModeInit", "");
}
#if defined _ALS_OnGameModeInit
    #undef OnGameModeInit
#else
    #define _ALS_OnGameModeInit
#endif
#define OnGameModeInit S@@_OnGameModeInit

forward OnGameModeInit();

This chunk of code allows people to use "OnGameModeInit" in multiple scripts, so that library writers don't need to worry about people correctly calling their setup functions.

This works quite well but it's VERY cumbersome, as a result "y_hooks" was developed:

pawn Code:
Hook:S@@_OnGameModeInit()
{
    // Do your code here...
}

This is VASTLY nicer code to read and write, but it is slower than ALS - this is part of YSI but the YSI libraries themselves don't actually use it for speed reasons.

However the other day I was contacted by JoeBullet/Google63 about the use of the "SCTRL" OpCode to modify the current instruction pointer which I combined with existing knowledge of the AMX data structure to create the ULTIMATE callback hook system. This is nicer than y_hooks (or the old y_hooks, this is an update) and MUCH faster than ALS (no benchmarks available):

pawn Code:
hook OnGameModeInit()
{
    // Do your code here...
}

That is now it! This completely bypasses CallLocalFunction, which is where the speed comes from, and you don't even need to define a prefix as with the old version (unless you have multiple versions of a single callback in one file). Note that the old syntax still works. If you are interested the code is here, but note also that you can't compile it because it uses some new libraries I've not released yet. The interesting code is lines 60-297, everything beyond that is just the same thing over and over and over again!

http://pastebin.com/KHtzcWVf

Note that you will need to get YSI from SVN - the official version hasn't been updated yet because I'm working on some big things!

Last edited by Y_Less; 19/03/2011 at 01:24 AM.
Y_Less is offline   Reply With Quote
Old 18/03/2011, 02:08 AM   #2
Kyosaur
High-roller
 
Kyosaur's Avatar
 
Join Date: Mar 2008
Location: USA - California
Posts: 1,057
Reputation: 303
Default Re: Ultimate callback hooks!

Dude, you're amazing man! This is really awesome, i can now stop being so hesitant to hook callbacks now.


Really nice idea, thank both you and google63.
__________________
[Tutorial]: Plugin development guide.
[Tutorial]: An indepth look at binary.


Follow me on twitter for updates on all my projects.
Kyosaur is offline   Reply With Quote
Old 18/03/2011, 06:06 AM   #3
Y_Less
Beta Tester
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 17,857
Reputation: 2478
Default Re: Ultimate callback hooks!

I just did some basic test which put this at around 10x faster than the old y_hooks system, however it is only just faster than ALS - as in almost not faster:

Code:
1514 - New
15371 - Old
1732 - ALS
However the fact remains that it IS faster and it IS easier...
Y_Less is offline   Reply With Quote
Old 18/03/2011, 01:36 PM   #4
Rivera
Big Clucker
 
Join Date: Feb 2011
Posts: 146
Reputation: 7
Default Re: Ultimate callback hooks!

what does this do? I mean, it's function...
__________________
My works

[Tutorial]Full usage of dcmd(updated)
[Tutorial]Login/Register System with dialogs


// Prefered Servers------------------------------

US-RP.INFO / Best Roleplay Server / 213.5.176.156:7777

^^__^^
Rivera is offline   Reply With Quote
Old 18/03/2011, 02:26 PM   #5
JaTochNietDan
High-roller
 
JaTochNietDan's Avatar
 
Join Date: Apr 2007
Location: Republic of Ireland
Posts: 4,302
Reputation: 357
Default Re: Ultimate callback hooks!

Quote:
Originally Posted by Rivera View Post
what does this do? I mean, it's function...
It allows you to hook callbacks easily and with better performance than previous callback hooking methods, this page gives a fairly good explanation of what you would need to use a callback hook for.

http://forum.sa-mp.com/showthread.php?t=166016
__________________
Releases:
[Includes]: [jBan] | [jBanLite]
[Plugins]: [FileManager] | [Execute]
[Applications]: [Remote Administrator *NEW] | [Script King] | [RCON GUI]
[Tools]: [Random Spawn Array Generator]

Learn PAWN and more:
http://www.jatochnietdan.com


JaTochNietDan is offline   Reply With Quote
Old 18/03/2011, 02:37 PM   #6
Y_Less
Beta Tester
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 17,857
Reputation: 2478
Default Re: Ultimate callback hooks!

Quote:
Originally Posted by Rivera View Post
what does this do? I mean, it's function...
It's basically a development tool for library writers. Take this library for example:

http://forum.sa-mp.com/showthread.php?t=240436

Quote:
Originally Posted by AH.1990 View Post
  • Features / Functions
  • AHSkin_GameModeInit();

    You Must put it under OnGameModeInit if its not a filterscript in order to use the include

  • AHSkin_FilterScriptInit();

    You Must put it under OnFilterScriptInit if its not a game mode in order to use the include
Those two functions MUST be called for the script to work, but I know from experience that many users will not call those functions in the right places or at the right times and then will come complaining that their code doesn't work. Currently AHSkin_GameModeInit is defined as:

pawn Code:
AHSkin_GameModeInit()
{  
    GrooveSkins[0] = 105;
    GrooveSkins[1] = 106;
    GrooveSkins[2] = 107;
    // [...]
    return 1;
}

If they were to define the function as:

pawn Code:
#include <YSI\y_hooks>

hook OnGameModeInit()
{  
    GrooveSkins[0] = 105;
    GrooveSkins[1] = 106;
    GrooveSkins[2] = 107;
    // [...]
    return 1;
}

That would instantly remove the need for the user to have to manually call the function, so then you and they don't need to worry about it.

Also, the code for this is on the YSI SVN so if you do want to use it you can get a working copy from there:

Address: https://dedi.y-less.com/svn/ysi
Username: public
Password: public

That won't let you upload, but if you want to upload just ask me for an upload account.
Y_Less is offline   Reply With Quote
Old 18/03/2011, 02:30 PM   #7
XFlawless
Guest
 
Posts: n/a
Default Re: Ultimate callback hooks!

Very nice efficient and fast, thanks
  Reply With Quote
Old 18/03/2011, 02:54 PM   #8
Zh3r0
Godfather
 
Join Date: Mar 2008
Posts: 5,597
Reputation: 381
Default Re: Ultimate callback hooks!

Good job, you finally made it faster! You always impress me with your marvelous work.
This thing is really something new and hard for me to understand.

Keep on releasing more and more and impress people!
__________________
Zh3r0 is offline   Reply With Quote
Old 18/03/2011, 04:09 PM   #9
Calgon
Godfather
 
Join Date: Jan 2009
Posts: 6,189
Reputation: 826
Default Re: Ultimate callback hooks!

Brilliant, time to try understand how you've done it and time to abuse it in a million includes I plan to make.
__________________
If you don't know then Don't post here.

Quote:
(10:27:18 PM) dugi: hello is this the police?
(10:27:25 PM) dugi: Calgon is being mean
(10:27:31 PM) dugi: arrest him pls
Calgon is offline   Reply With Quote
Old 18/03/2011, 09:04 PM   #10
AH.1990
Big Clucker
 
Join Date: Mar 2011
Posts: 50
Reputation: 2
Default Re: Ultimate callback hooks!

i downloaded YSI and i found y_hooks in the folder

thats it?? i mean thats the right include??
__________________
Scripting For Money!!
I Can Script ANY THING!!!
You Can Add My E-mail And Offer Me
We Will Use Western Union For Money Transfer
E-mail :AH.1990@rocketmail.com
--------------------------------------------------------------
My Work:
AH.1990 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
is there any callback for ..... MrIncredible Help Archive 4 15/03/2010 05:52 AM
Callback Bomber Help Archive 4 04/03/2010 09:08 PM
Is there such callback as JoeDaDude Help Archive 12 21/01/2010 06:29 PM


All times are GMT. The time now is 06:50 PM.


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