|
|
#1 |
|
High-roller
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2008
Location: Hong Kong, China
Posts: 1,677
Reputation: 123
|
Transparent Hooking Method with States(THMS) Better Method Better methods of hooking functions are now found. It makes use of the behaviour of macros to modify the name of the hooked functions. It's simpler and faster, so I recommend that method (by ipsBruno): http://forum.sa-mp.com/showpost.php?...2&postcount=14 What is it? This is an improved method of the original transparent hooking method (named by Y_Less) for the library scripters, so that it is more efficient than the original one, while they still do not need to tell the users to put the initialisation code into their scripts. Inspiration Yesterday I was inspired by Y_Less' automata tutorial:(Click). I wonder if we can use automata to change the states so that we don't need to add any prefix to the hooked callbacks when we write libraries, without duplication of the callback. However I failed to make that to become true. I still need a suffix. (Click) Then I tried to mix that idea with the ALS method this morning and I succeeded to make this. I think this should be the same as the one Y_Less released recently as most of the code here is developed by him. ![]() Code pawn Code:
Explanation I'll explain it a little if you don't understand why it works: pawn Code:
pawn Code:
pawn Code:
The first function created, pawn Code:
The second one, pawn Code:
The last one, pawn Code:
Note that the "return 1" can be changed to "return 0" or some other values in pawn Code:
pawn Code:
The pawn Code:
pawn Code:
Again, check it above for what it does. pawn Code:
pawn Code:
Testing I've tested this method that no errors were found and multiple hooking does work(i've tested it with triple hooking ). It also have backward compatibility that it supports includes with the original ALS method. Thanks to Y_Less, the run-time errors are fixed. It should work now, but if you find any bugs/problems on this please feel free to tell it here. Note Due to the removal of CallLocalFunction, you don't need to use funcidx to check if the callback exists and call it IMHO. I've made a benchmark on it and it's as slow as the original ALS hooking. Benchmark I used Slice's benchmark macro to test it here, and I test the speed by calling the original OnFilterScriptInit callback: Testing filterscript: pawn Code:
testhook.inc(ALS version): pawn Code:
testhook.inc(THMS version): pawn Code:
Here's the benchmarking result: Code:
Bench for THMS: executes, by average, 516.88 times/ms. Bench for ALS: executes, by average, 401.54 times/ms. My server crashed when I try to test it with y_hooks so feel free to test it. Issues
Credits
Last edited by leong124; 27/03/2013 at 08:27 PM. Reason: recommend a better method |
|
|
|
|
#2 |
|
Banned
![]() Join Date: May 2011
Posts: 64
Reputation: 0
|
You're point?
|
|
|
|
|
#3 |
|
High-roller
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2009
Posts: 1,602
Reputation: 61
|
|
|
|
|
|
#4 |
|
Beta Tester
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2008
Location: 629
Posts: 13,296
Reputation: 1909
|
Have you tried this when a user DOESN'T have the callback? I'm pretty sure you'll get a run-time error for calling a function with an undefined state! It MAY be doable, but not easy however:
pawn Code:
That code has an explicit state on the second version which is called if the function exists, and the fall-back is used if it doesn't. The macro allows you to define all the possible states and use one of them (that's never set) to define "prefix_OnFilterScriptInit" as using the "AnyStateName" automata. Last edited by Y_Less; 24/05/2011 at 03:06 PM. |
|
|
|
|
#5 |
|
High-roller
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2008
Location: Hong Kong, China
Posts: 1,677
Reputation: 123
|
I've tested that and both my method and yours work.
I can't get any compile-time or run-time error at all. I guess it will not get an error, because the function only check if the state matches. |
|
|
|
|
#6 |
|
Beta Tester
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2008
Location: 629
Posts: 13,296
Reputation: 1909
|
Hmm, I thought it gave a run-time error. It doesn't seem to but does still fail. Compare this:
pawn Code:
And this: pawn Code:
The second version is one in which the user hasn't included the callback, in that case "4" is never printed - it is crashing silently. As a contrast, based on yours (with a few more macros to make it (IMHO) look nicer), try running both of these: pawn Code:
pawn Code:
|
|
|
|
|
#7 |
|
High-roller
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2008
Location: Hong Kong, China
Posts: 1,677
Reputation: 123
|
I've thought of that last night. You are right then.
I'll have a look on it, though the libraries are still loaded. Edit: Alright I've edited it and it should work now. Thanks for your help
Last edited by leong124; 25/05/2011 at 05:41 AM. |
|
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [HELP] What is the best method to do this. | [BKR]LUCAGRABACR | Help Archive | 1 | 10/03/2011 09:36 AM |
| What is a better method of saving? | RealCop228 | Help Archive | 16 | 12/02/2011 05:51 PM |
| No run method | matdav | Help Archive | 2 | 10/02/2011 10:31 PM |
| Will the Steam version run with this method? | Asop3 | Client Support | 1 | 01/11/2009 02:35 AM |