| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702 |
- /*
- Legal:
- Version: MPL 1.1
-
- The contents of this file are subject to the Mozilla Public License Version
- 1.1 the "License"; you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
- http://www.mozilla.org/MPL/
-
- Software distributed under the License is distributed on an "AS IS" basis,
- WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- for the specific language governing rights and limitations under the
- License.
-
- The Original Code is the YSI framework.
-
- The Initial Developer of the Original Code is Alex "Y_Less" Cole.
- Portions created by the Initial Developer are Copyright C 2011
- the Initial Developer. All Rights Reserved.
- Contributors:
- Y_Less
- koolk
- JoeBullet/Google63
- g_aSlice/Slice
- Misiur
- samphunter
- tianmeta
- maddinat0r
- spacemud
- Crayder
- Dayvison
- Ahmad45123
- Zeex
- irinel1996
- Yiin-
- Chaprnks
- Konstantinos
- Masterchen09
- Southclaws
- PatchwerkQWER
- m0k1
- paulommu
- udan111
- Thanks:
- JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
- ZeeX - Very productive conversations.
- koolk - IsPlayerinAreaEx code.
- TheAlpha - Danish translation.
- breadfish - German translation.
- Fireburn - Dutch translation.
- yom - French translation.
- 50p - Polish translation.
- Zamaroht - Spanish translation.
- Los - Portuguese translation.
- Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes for
- me to strive to better.
- Pixels^ - Running XScripters where the idea was born.
- Matite - Pestering me to release it and using it.
- Very special thanks to:
- Thiadmer - PAWN, whose limits continue to amaze me!
- Kye/Kalcor - SA:MP.
- SA:MP Team past, present and future - SA:MP.
- Optional plugins:
- Gamer_Z - GPS.
- Incognito - Streamer.
- Me - sscanf2, fixes2, Whirlpool.
- */
- static
- YSI_g_sValue;
- #if defined YSI_FAILING_TESTS
- DEFINE_HOOK_REPLACEMENT(VeryLongTarget_, YyYy);
- hook y_hooks_YyYy0()
- {
- // The length after replacement here will be longer than the hook name,
- // even with prefix/suffixes. This means you can't generate the
- // original name because there is no original.
- ASSERT(true);
- YSI_g_sValue = 101;
- }
- hook y_hooks_YyYy1()
- {
- // The length after replacement here will be longer than the hook name,
- // even with prefix/suffixes. This means you can't generate the
- // original name because there is no original.
- ASSERT(true);
- YSI_g_sValue = 202;
- }
- hook y_hooks_YyYz1()
- {
- // Will come very next in the header.
- ASSERT(true);
- YSI_g_sValue = 303;
- }
- forward y_hooks_VeryLongTarget_0();
- public y_hooks_VeryLongTarget_0()
- {
- // This one should work, because we have the original function to use
- // for a pointer, not a newly generated version.
- ASSERT(YSI_g_sValue == 101);
- YSI_g_sValue += 404;
- }
- Test:y_hooks_TooLongReplacements()
- {
- YSI_g_sValue = 0;
- CallLocalFunction("y_hooks_VeryLongTarget_0", "");
- ASSERT(YSI_g_sValue == 505);
- YSI_g_sValue = 0;
- CallLocalFunction("y_hooks_YyYz1", "");
- ASSERT(YSI_g_sValue == 303);
- YSI_g_sValue = 0;
- // Currently fails.
- CallLocalFunction("y_hooks_VeryLongTarget_1", "");
- ASSERT(YSI_g_sValue == 202);
- }
- #endif
- hook Ony_hooksShortUpdate()
- {
- ++YSI_g_sValue;
- }
- hook Ony_hooksShortUpd()
- {
- ++YSI_g_sValue;
- }
- Test:Ony_hooksShortUpdate()
- {
- YSI_g_sValue = 0;
- CallLocalFunction("Ony_hooksShortUpdate", "");
- ASSERT(YSI_g_sValue == 2);
- }
- #if defined YSI_FAILING_TESTS
- DEFINE_HOOK_REPLACEMENT(TTTTT_YYYYY, YHook);
- forward Ony_hooksTTTTT_YYYYY_WtExtras();
- hook Ony_hooksYHook()
- {
- ++YSI_g_sValue;
- }
- hook Ony_hooksTTTTT_YYYYY()
- {
- ++YSI_g_sValue;
- }
- Test:y_hooksReplacement2()
- {
- YSI_g_sValue = 0;
- CallLocalFunction("Ony_hooksTTTTT_YYYYY", "");
- ASSERT(YSI_g_sValue == 2);
- }
- public Ony_hooksTTTTT_YYYYY_WtExtras()
- {
- ++YSI_g_sValue;
- }
- hook Ony_hooksYHook_WtExtras()
- {
- ++YSI_g_sValue;
- }
- Test:y_hooksReplacement3()
- {
- YSI_g_sValue = 0;
- CallLocalFunction("Ony_hooksTTTTT_YYYYY_WtExtras", "");
- ASSERT(YSI_g_sValue == 2);
- }
- #endif
- forward Ony_hooksTest2(cmd[]);
- hook Ony_hooksTest2@000(cmd[])
- {
- ++YSI_g_sValue;
- return 1;
- }
- hook Ony_hooksTest2@001(cmd[])
- {
- // Called second due to forced orderings.
- ++YSI_g_sValue;
- return 1;
- }
- public Ony_hooksTest2(cmd[])
- {
- // Called second due to forced orderings.
- //if (YSI_g_sValue) return YSI_g_sValue - 7;
- return 1;
- }
- Test:y_hooks_Ony_hooksTest2()
- {
- // Check both hooks are called.
- YSI_g_sValue = 0;
- ASSERT(CallLocalFunction("Ony_hooksTest2", "s", "IGNORE_ME") != 0);
- ASSERT(YSI_g_sValue == 2);
- YSI_g_sValue = 0;
- }
- forward Ony_hooksTest3(cmd[]);
- hook Ony_hooksTest3@000(cmd[])
- {
- ++YSI_g_sValue;
- //printf("a");
- return 1;
- }
- hook Ony_hooksTest3@001(cmd[])
- {
- // Called second due to forced orderings.
- ++YSI_g_sValue;
- //printf("b");
- return 1;
- }
- Test:y_hooks_Ony_hooksTest3()
- {
- // Check both hooks are called.
- YSI_g_sValue = 0;
- ASSERT(CallLocalFunction("Ony_hooksTest3", "s", "IGNORE_ME") != 0);
- ASSERT(YSI_g_sValue == 2);
- YSI_g_sValue = 0;
- }
- forward Ony_hooksTest1(cmd[]);
- hook Ony_hooksTest1@000(cmd[])
- {
- ++YSI_g_sValue;
- return 1;
- }
- hook Ony_hooksTest1@001(cmd[])
- {
- // Called second due to forced orderings.
- ++YSI_g_sValue;
- return 0;
- }
- Test:y_hooks_Ony_hooksTest1()
- {
- // Check both hooks are called.
- YSI_g_sValue = 0;
- ASSERT(CallLocalFunction("Ony_hooksTest1", "s", "IGNORE_ME") == 0);
- ASSERT(YSI_g_sValue == 2);
- YSI_g_sValue = 0;
- }
- forward Ony_hooksTest4(cmd[]);
- hook Ony_hooksTest4@000(cmd[])
- {
- ++YSI_g_sValue;
- return -1;
- }
- hook Ony_hooksTest4@001(cmd[])
- {
- // Called second due to forced orderings.
- ++YSI_g_sValue;
- return 0;
- }
- Test:y_hooks_Ony_hooksTest4()
- {
- // Check only one hook is called.
- YSI_g_sValue = 0;
- ASSERT(CallLocalFunction("Ony_hooksTest4", "s", "IGNORE_ME") == 0);
- ASSERT(YSI_g_sValue == 1);
- YSI_g_sValue = 0;
- }
- forward Ony_hooksTest5(cmd[]);
- hook Ony_hooksTest5@000(cmd[])
- {
- ++YSI_g_sValue;
- return -2;
- }
- hook Ony_hooksTest5@001(cmd[])
- {
- // Called second due to forced orderings.
- ++YSI_g_sValue;
- return 0;
- }
- Test:y_hooks_Ony_hooksTest5()
- {
- // Check only one hook is called.
- YSI_g_sValue = 0;
- ASSERT(CallLocalFunction("Ony_hooksTest5", "s", "IGNORE_ME") == 1);
- ASSERT(YSI_g_sValue == 1);
- YSI_g_sValue = 0;
- }
- static stock y_hooks_funcidx2(str[])
- {
- new
- func[32],
- buf,
- idx;
- while ((idx = AMX_GetPublicName(idx, func, str)))
- {
- if (!strcmp(func, str))
- {
- AMX_GetPublicEntry(idx - 1, buf);
- return buf;
- }
- }
- return -1;
- }
- forward y_hooks_Invalidate1();
- public y_hooks_Invalidate1() {}
- Test:y_hooks_Invalidate1()
- {
- ASSERT(funcidx("y_hooks_Invalidate1") != -1);
- Hooks_InvalidateName(y_hooks_funcidx2("y_hooks_Invalidate1"));
- Hooks_SortPublics();
- ASSERT(funcidx("y_hooks_Invalidate1") == -1);
- }
- forward y_hooks_Invalidate2();
- public y_hooks_Invalidate2() {}
- forward y_hooks_Invalidate3();
- public y_hooks_Invalidate3() {}
- Test:y_hooks_Invalidate23()
- {
- new
- base0,
- count0;
- AMX_GetBaseCount(AMX_TABLE_PUBLICS, base0, count0);
- ASSERT(funcidx("y_hooks_Invalidate2") != -1);
- ASSERT(funcidx("y_hooks_Invalidate3") != -1);
- Hooks_InvalidateName(y_hooks_funcidx2("y_hooks_Invalidate2"));
- Hooks_InvalidateName(y_hooks_funcidx2("y_hooks_Invalidate3"));
- Hooks_SortPublics();
- ASSERT(funcidx("y_hooks_Invalidate2") == -1);
- ASSERT(funcidx("y_hooks_Invalidate3") == -1);
- new
- base1,
- count1;
- AMX_GetBaseCount(AMX_TABLE_PUBLICS, base1, count1);
- ASSERT(base1 > base0);
- ASSERT(count1 < count0);
- }
- Test:y_hooks_IsolateName1()
- {
- new
- name[32];
- name = "@yH_Hello@Other";
- Hooks_IsolateName(name);
- ASSERT_FALSE(strcmp(name, "Hello"));
- name = "@yH_Hi@@Other";
- Hooks_IsolateName(name);
- ASSERT_FALSE(strcmp(name, "Hi"));
- name = "@yH_No@Other@More";
- Hooks_IsolateName(name);
- ASSERT_FALSE(strcmp(name, "No"));
- name = "@yH_Yes_Other@N";
- Hooks_IsolateName(name);
- ASSERT_FALSE(strcmp(name, "Yes_Other"));
- }
- Test:y_hooks_IsolateName2()
- {
- new
- func[32];
- #define FUNC:%0; func = "_yH@"#%0"@";Hooks_IsolateName(func);ASSERT(!strcmp(#%0, func));
- FUNC:A;
- FUNC:AAA;
- FUNC:AAB;
- FUNC:ABA;
- FUNC:ABB;
- FUNC:aB;
- FUNC:ab;
- FUNC:Ab;
- #undef FUNC
- }
- static g_y_hooksVar;
- Test:y_hooks_Customs()
- {
- g_y_hooksVar = 0;
- CallLocalFunction("y_hooks_MyFunc1", "ii", 4, 5);
- ASSERT(g_y_hooksVar == (4 + 5) * 6);
- }
- forward y_hooks_MyFunc1(a, b);
- public y_hooks_MyFunc1(a, b)
- {
- //printf("Part 1 %d %d", a, b);
- g_y_hooksVar += a + b;
- }
- #tryinclude "..\YSI_Internal\y_unique"
- #tryinclude "..\..\YSI_Internal\y_unique"
- hook y_hooks_MyFunc1(a, b)
- {
- //printf("Part 2 %d", a);
- g_y_hooksVar += a + b;
- }
- #tryinclude "..\YSI_Internal\y_unique"
- #tryinclude "..\..\YSI_Internal\y_unique"
- hook y_hooks_MyFunc1(a, b)
- {
- //printf("Part 3 %d", b);
- g_y_hooksVar += a + b;
- }
- #tryinclude "..\YSI_Internal\y_unique"
- #tryinclude "..\..\YSI_Internal\y_unique"
- hook y_hooks_MyFunc1(a, b)
- {
- //printf("Part 3 %d", b);
- g_y_hooksVar += a + b;
- }
- #tryinclude "..\YSI_Internal\y_unique"
- #tryinclude "..\..\YSI_Internal\y_unique"
- hook y_hooks_MyFunc1(a, b)
- {
- //printf("Part 3 %d", b);
- g_y_hooksVar += a + b;
- }
- #tryinclude "..\YSI_Internal\y_unique"
- #tryinclude "..\..\YSI_Internal\y_unique"
- hook y_hooks_MyFunc1(a, b)
- {
- //printf("Part 3 %d", b);
- g_y_hooksVar += a + b;
- }
- forward OnPlayerEnterDynamicArea(p, a);
- public OnPlayerEnterDynamicArea(p, a) { g_y_hooksVar += 100; }
- hook OnPlayerEnterDynArea(p, a) { g_y_hooksVar += 10; }
- Test:y_hooks_SpecialCases()
- {
- g_y_hooksVar = 0;
- CallLocalFunction("OnPlayerEnterDynamicArea", "ii", 0, 0);
- ASSERT(g_y_hooksVar == 110);
- }
- Test:Hooks_MakeLongName0()
- {
- new
- name[64];
- name = "OnUpd";
- ASSERT(Hooks_MakeLongName(name) == 8);
- ASSERT(!strcmp(name, "OnUpdate"));
- name = "OnUpdD";
- ASSERT(Hooks_MakeLongName(name) == 9);
- ASSERT(!strcmp(name, "OnUpdateD"));
- name = "OnUpda";
- ASSERT(Hooks_MakeLongName(name) == 6);
- ASSERT(!strcmp(name, "OnUpda"));
- }
- Test:Hooks_MakeLongName1()
- {
- new
- name[64];
- name = "CP";
- ASSERT(Hooks_MakeLongName(name) == 10);
- ASSERT(!strcmp(name, "Checkpoint"));
- name = "TD";
- ASSERT(Hooks_MakeLongName(name) == 8);
- ASSERT(!strcmp(name, "TextDraw"));
- name = "Upd";
- ASSERT(Hooks_MakeLongName(name) == 6);
- ASSERT(!strcmp(name, "Update"));
- name = "Dyn";
- ASSERT(Hooks_MakeLongName(name) == 7);
- ASSERT(!strcmp(name, "Dynamic"));
- name = "Obj";
- ASSERT(Hooks_MakeLongName(name) == 6);
- ASSERT(!strcmp(name, "Object"));
- name = "Cmd";
- ASSERT(Hooks_MakeLongName(name) == 7);
- ASSERT(!strcmp(name, "Command"));
- name = "DynamicCP";
- ASSERT(Hooks_MakeLongName(name) == 9);
- ASSERT(!strcmp(name, "DynamicCP"));
- }
- Test:Hooks_MakeLongName2()
- {
- new
- name[64];
- name = "OnUpdTDCP";
- ASSERT(Hooks_MakeLongName(name) == 26);
- ASSERT(!strcmp(name, "OnUpdateTextDrawCheckpoint"));
- name = "OnUpdTDObjCmd";
- ASSERT(Hooks_MakeLongName(name) == 29);
- ASSERT(!strcmp(name, "OnUpdateTextDrawObjectCommand"));
- name = "OnUpdTDCPObjCmd";
- ASSERT(Hooks_MakeLongName(name) == 31);
- ASSERT(!strcmp(name, "OnUpdateTextDrawCPObjectCommand"));
- }
- Test:Hooks_MakeShortName1()
- {
- new
- name[64];
- name = "OnPlayerEnterRaceCP";
- ASSERT(Hooks_MakeShortName(name) == 19);
- ASSERT(!strcmp(name, "OnPlayerEnterRaceCP"));
- name = "OnPlayerEnterRaceCheckpoint";
- ASSERT(Hooks_MakeShortName(name) == 27);
- ASSERT(!strcmp(name, "OnPlayerEnterRaceCheckpoint"));
- name = "OnPlayerEnterDynamicCP";
- ASSERT(Hooks_MakeShortName(name) == 22);
- ASSERT(!strcmp(name, "OnPlayerEnterDynamicCP"));
- name = "OnPlayerEnterDynamicRaceCheckpoint";
- ASSERT(Hooks_MakeShortName(name) == 26);
- ASSERT(!strcmp(name, "OnPlayerEnterDynamicRaceCP"));
- name = "AAAAAAAAAAAAAAAAAAAUpdateTextDraw";
- Hooks_MakeShortName(name);
- ASSERT(!strcmp(name, "AAAAAAAAAAAAAAAAAAAUpdateTD"));
- name = "BBBBBAAAAAAAAAAAAAAAAAAAUpdateTextDraw";
- Hooks_MakeShortName(name);
- ASSERT(!strcmp(name, "BBBBBAAAAAAAAAAAAAAAAAAAUpdTD"));
- }
- Test:Hooks_MakeShortName2()
- {
- new
- name[64];
- name = "AAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCC";
- ASSERT(Hooks_MakeShortName(name) == 43);
- ASSERT(!strcmp(name, "AAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCC"));
- name = "AAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCCCCCommandCCCC";
- ASSERT(Hooks_MakeShortName(name) == 45);
- ASSERT(!strcmp(name, "AAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCCCCCmdCCCC"));
- }
- Test:Hooks_GetDefaultReturn()
- {
- ASSERT(Hooks_GetDefaultReturn("OnHookRet\0\0\0\0\0\0\0\0") == 1);
- ASSERT(Hooks_GetDefaultReturn("OnPlayerCommandText\0\0\0\0\0\0\0\0") == 0);
- ASSERT(Hooks_GetDefaultReturn("OnHookRet2\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0") == 0);
- ASSERT(Hooks_GetDefaultReturn("OnPlayerConnect\0\0\0\0\0\0\0\0") == 1);
- }
- HOOK_RET:OnHookRet2() return 0;
- DEFINE_HOOK_REPLACEMENT(Replacement, Rpl);
- forward OnPlayerCustomReplacement();
- public OnPlayerCustomReplacement() { g_y_hooksVar += 500; }
- hook OnPlayerCustomRpl() { g_y_hooksVar += 55; }
- Test:y_hooks_CustomReplacement()
- {
- g_y_hooksVar = 0;
- CallLocalFunction("OnPlayerCustomReplacement", "");
- ASSERT(g_y_hooksVar == 555);
- }
- #if !defined BAD_numargs
- #error y_hooks tests require BAD_numargs
- #endif
- hook OnTestNumArgs0(a, b, c, d, e, f)
- {
- ASSERT(Hooks_NumArgs() == 6);
- ASSERT(numargs() == 6);
- }
- hook OnTestNumArgs1(a, b, c, d, f)
- {
- ASSERT(Hooks_NumArgs() == 5);
- ASSERT(BAD_numargs() != 5);
- ASSERT(numargs() == 5);
- }
- forward OnTestNumArgs1(a, b, c, d, f);
- public OnTestNumArgs1(a, b, c, d, f)
- {
- ASSERT(Hooks_NumArgs() == 5);
- ASSERT(BAD_numargs() != 5);
- ASSERT(numargs() == 5);
- }
- hook OnTestNumArgs2(a, b, c, f)
- {
- ASSERT(Hooks_NumArgs() == 4);
- ASSERT(BAD_numargs() != 4);
- ASSERT(numargs() == 4);
- }
- #tryinclude "..\YSI_Internal\y_unique"
- #tryinclude "..\..\YSI_Internal\y_unique"
- hook OnTestNumArgs2(a, b, c, f)
- {
- ASSERT(Hooks_NumArgs() == 4);
- ASSERT(BAD_numargs() != 4);
- ASSERT(numargs() == 4);
- }
- forward OnTestNumArgs2(a, b, c, f);
- public OnTestNumArgs2(a, b, c, f)
- {
- ASSERT(Hooks_NumArgs() == 4);
- ASSERT(BAD_numargs() != 4);
- ASSERT(numargs() == 4);
- }
- static OnTestNumArgs4(total, ...)
- {
- ASSERT(numargs() == total + 1);
- }
- Test:y_hooks_numargs()
- {
- CallLocalFunction("OnTestNumArgs0", "iiiiii", 0, 0, 0, 0, 0, 0);
- CallLocalFunction("OnTestNumArgs1", "iiiii", 0, 0, 0, 0, 0);
- CallLocalFunction("OnTestNumArgs2", "iiii", 0, 0, 0, 0);
- OnTestNumArgs4(0);
- OnTestNumArgs4(1, 1);
- OnTestNumArgs4(2, 1, 2);
- OnTestNumArgs4(3, 1, 2, 3);
- OnTestNumArgs4(4, 1, 2, 3, 4);
- OnTestNumArgs4(5, 1, 2, 3, 4, 5);
- OnTestNumArgs4(6, 1, 2, 3, 4, 5, 6);
- }
- hook y_hooks_State1(string:c[]) <y_hooks_state : y_hooks_state_1>
- {
- g_y_hooksVar = strval(c[0]);
- }
- hook y_hooks_State1(string:c[]) <y_hooks_state : y_hooks_state_2>
- {
- g_y_hooksVar = strval(c[5]);
- }
- rehook y_hooks_State1(string:c[]) <y_hooks_state : y_hooks_state_3>
- {
- g_y_hooksVar += strval(c[5]);
- }
- hook public y_hooks_State1(string:c[]) <y_hooks_state : y_hooks_state_4>
- {
- g_y_hooksVar = strval(c[5]);
- }
- Test:y_hooks_State1()
- {
- state y_hooks_state : y_hooks_state_1;
- CallLocalFunction("y_hooks_State1", "s", "1000 2000");
- ASSERT(g_y_hooksVar == 1000);
- state y_hooks_state : y_hooks_state_2;
- CallLocalFunction("y_hooks_State1", "s", "1000 2000");
- ASSERT(g_y_hooksVar == 2000);
- state y_hooks_state : y_hooks_state_1;
- CallLocalFunction("y_hooks_State1", "s", "1000 2000");
- state y_hooks_state : y_hooks_state_3;
- CallLocalFunction("y_hooks_State1", "s", "1000 2000");
- ASSERT(g_y_hooksVar == 3000);
- }
|