impl.inc 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. /*
  2. Legal:
  3. Version: MPL 1.1
  4. The contents of this file are subject to the Mozilla Public License Version
  5. 1.1 the "License"; you may not use this file except in compliance with
  6. the License. You may obtain a copy of the License at
  7. http://www.mozilla.org/MPL/
  8. Software distributed under the License is distributed on an "AS IS" basis,
  9. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  10. for the specific language governing rights and limitations under the
  11. License.
  12. The Original Code is the YSI framework.
  13. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  14. Portions created by the Initial Developer are Copyright C 2011
  15. the Initial Developer. All Rights Reserved.
  16. Contributors:
  17. Y_Less
  18. koolk
  19. JoeBullet/Google63
  20. g_aSlice/Slice
  21. Misiur
  22. samphunter
  23. tianmeta
  24. maddinat0r
  25. spacemud
  26. Crayder
  27. Dayvison
  28. Ahmad45123
  29. Zeex
  30. irinel1996
  31. Yiin-
  32. Chaprnks
  33. Konstantinos
  34. Masterchen09
  35. Southclaws
  36. PatchwerkQWER
  37. m0k1
  38. paulommu
  39. udan111
  40. Thanks:
  41. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  42. ZeeX - Very productive conversations.
  43. koolk - IsPlayerinAreaEx code.
  44. TheAlpha - Danish translation.
  45. breadfish - German translation.
  46. Fireburn - Dutch translation.
  47. yom - French translation.
  48. 50p - Polish translation.
  49. Zamaroht - Spanish translation.
  50. Los - Portuguese translation.
  51. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes for
  52. me to strive to better.
  53. Pixels^ - Running XScripters where the idea was born.
  54. Matite - Pestering me to release it and using it.
  55. Very special thanks to:
  56. Thiadmer - PAWN, whose limits continue to amaze me!
  57. Kye/Kalcor - SA:MP.
  58. SA:MP Team past, present and future - SA:MP.
  59. Optional plugins:
  60. Gamer_Z - GPS.
  61. Incognito - Streamer.
  62. Me - sscanf2, fixes2, Whirlpool.
  63. */
  64. /*
  65. ad88888ba
  66. d8" "8b ,d
  67. Y8, 88
  68. `Y8aaaaa, ,adPPYba, MM88MMM 88 88 8b,dPPYba,
  69. `"""""8b, a8P_____88 88 88 88 88P' "8a
  70. `8b 8PP""""""" 88 88 88 88 d8
  71. Y8a a8P "8b, ,aa 88, "8a, ,a88 88b, ,a8"
  72. "Y88888P" `"Ybbd8"' "Y888 `"YbbdP'Y8 88`YbbdP"'
  73. 88
  74. 88
  75. */
  76. PRE_HOOK(HookChain)
  77. #undef CHAIN_ORDER
  78. #define CHAIN_ORDER @CO_HookChain
  79. static stock _HookChain_IncludeStates() <_ALS : _ALS_x0, _ALS : _ALS_x1, _ALS : _ALS_x2, _ALS : _ALS_x3>
  80. {
  81. }
  82. static stock _HookChain_IncludeStates() <_ALS : _ALS_go>
  83. {
  84. }
  85. HOOK_RET:OnPlayerCommandText()
  86. {
  87. return 0;
  88. }
  89. HOOK_RET:OnRconCommand()
  90. {
  91. return 0;
  92. }
  93. enum E_PRE_HOOK
  94. {
  95. E_PRE_HOOK_NAME[16],
  96. E_PRE_HOOK_VALUE
  97. }
  98. // Create the default replacements.
  99. DEFINE_HOOK_REPLACEMENT(Checkpoint, CP );
  100. DEFINE_HOOK_REPLACEMENT(Container , Cnt);
  101. DEFINE_HOOK_REPLACEMENT(Inventory , Inv);
  102. DEFINE_HOOK_REPLACEMENT(Dynamic , Dyn);
  103. DEFINE_HOOK_REPLACEMENT(TextDraw , TD );
  104. DEFINE_HOOK_REPLACEMENT(Update , Upd);
  105. DEFINE_HOOK_REPLACEMENT(Object , Obj);
  106. DEFINE_HOOK_REPLACEMENT(Command , Cmd);
  107. DEFINE_HOOK_REPLACEMENT(DynamicCP , DynamicCP);
  108. enum E_HOOK_NAME_REPLACEMENT_DATA
  109. {
  110. E_HOOK_NAME_REPLACEMENT_SHORT[16],
  111. E_HOOK_NAME_REPLACEMENT_LONG[16],
  112. E_HOOK_NAME_REPLACEMENT_MIN,
  113. E_HOOK_NAME_REPLACEMENT_MAX
  114. }
  115. static stock
  116. YSI_g_sReplacements[MAX_HOOK_REPLACEMENTS][E_HOOK_NAME_REPLACEMENT_DATA],
  117. YSI_g_sReplacementsLongOrder[MAX_HOOK_REPLACEMENTS],
  118. YSI_g_sReplacementsShortOrder[MAX_HOOK_REPLACEMENTS],
  119. YSI_g_sReplacePtr;
  120. /*-------------------------------------------------------------------------*//**
  121. * <param name="name">Function name to modify.</param>
  122. * <remarks>
  123. * Expands all name parts like "CP" and "Obj" to their full versions (in this
  124. * example "Checkpoint" and "Object").
  125. * </remarks>
  126. *//*------------------------------------------------------------------------**/
  127. stock Hooks_MakeLongName(name[64])
  128. {
  129. new
  130. end = 0,
  131. i = 0,
  132. pos = -1,
  133. idx = YSI_g_sReplacementsShortOrder[0];
  134. while (i != YSI_g_sReplacePtr)
  135. {
  136. // Allow for multiple replacements of the same string.
  137. if ((pos = strfind(name, YSI_g_sReplacements[idx][E_HOOK_NAME_REPLACEMENT_SHORT], false, pos + 1)) == -1)
  138. {
  139. ++i,
  140. idx = YSI_g_sReplacementsShortOrder[i];
  141. }
  142. // This assumes CamelCase. If the letter immediately following the end
  143. // of the string is lower-case, then the short word found is not a
  144. // complete word and should not be replaced.
  145. else if ('a' <= name[(end = pos + YSI_g_sReplacements[idx][E_HOOK_NAME_REPLACEMENT_MIN])] <= 'z')
  146. continue;
  147. else
  148. {
  149. P:5("Found hook name replacement: %d, %s", pos, YSI_g_sReplacements[idx][E_HOOK_NAME_REPLACEMENT_SHORT]);
  150. // Found a complete word according to CamelCase rules.
  151. strdel(name, pos + 1, end),
  152. name[pos] = 0x80000000 | idx;
  153. }
  154. }
  155. // All the replacements have been found and marked. Now actually insert.
  156. // If "end" is "0", it means that it was never assigned to within the loop
  157. // above, which means that no replacements were found.
  158. if (end)
  159. {
  160. // "pos" must be "-1" at the start of this loop.
  161. while (name[++pos])
  162. {
  163. P:7("Hooks_MakeLongName main loop: [%d] = 0x%04x%04x \"%s\"", pos, name[pos] >>> 16, name[pos] & 0xFFFF, name);
  164. if (name[pos] < '\0')
  165. {
  166. // Negative number instead of a character. Used to indicate
  167. // where a replacement should be inserted. They are not done
  168. // inline in the loop above because some replacements may be
  169. // contained within others - we don't want those to be both done
  170. // within each other.
  171. P:5("Inserting hook name replacement: %d, %s", pos, YSI_g_sReplacements[name[pos] & ~0x80000000][E_HOOK_NAME_REPLACEMENT_LONG]);
  172. P:6("Current character: 0x%04x%04x", name[pos] >>> 16, name[pos] & 0xFFFF);
  173. // It took me ages to find a bug in this code. For some reason,
  174. // "strins" was packing the result. This was because the value
  175. // in "name[pos]" was not a real character, so was seen as an
  176. // indication that the string was packed. I fixed it by moving
  177. // the assignment to "name[pos]" above "strins". So while the
  178. // two lines look independent, they aren't!
  179. i = name[pos] & ~0x80000000,
  180. name[pos] = YSI_g_sReplacements[i][E_HOOK_NAME_REPLACEMENT_LONG];
  181. // Use the original "strins" - we know this string is not
  182. // packed, but it might sometimes look like it is. The "fixed"
  183. // version is designed for working with packed strings, but thus
  184. // breaks on not packed strings that just look packed.
  185. #if defined BAD_strins
  186. BAD_strins
  187. #else
  188. strins
  189. #endif
  190. (name, YSI_g_sReplacements[i][E_HOOK_NAME_REPLACEMENT_LONG + E_HOOK_NAME_REPLACEMENT_DATA:1], pos + 1);
  191. pos += YSI_g_sReplacements[i][E_HOOK_NAME_REPLACEMENT_MAX] - 1;
  192. P:6("New string: %s", name);
  193. }
  194. }
  195. }
  196. // It is possible for the expansions to become TOO big.
  197. return Hooks_MakeShortName(name);
  198. }
  199. /*-------------------------------------------------------------------------*//**
  200. * <param name="name">Function name to modify.</param>
  201. * <remarks>
  202. * Compresses function names when required to fit within 32 characters
  203. * according to well defined rules (see "YSI_g_sReplacements").
  204. * </remarks>
  205. *//*------------------------------------------------------------------------**/
  206. stock Hooks_MakeShortName(name[64])
  207. {
  208. // Easy one.
  209. new
  210. len,
  211. pos = -1,
  212. idx = YSI_g_sReplacementsLongOrder[0];
  213. for (new i = 0; (len = strlen(name)) >= 32 && i != YSI_g_sReplacePtr; )
  214. {
  215. if ((pos = strfind(name, YSI_g_sReplacements[idx][E_HOOK_NAME_REPLACEMENT_LONG], false, pos + 1)) == -1)
  216. {
  217. ++i,
  218. idx = YSI_g_sReplacementsLongOrder[i];
  219. }
  220. else
  221. {
  222. strdel(name, pos, pos + YSI_g_sReplacements[idx][E_HOOK_NAME_REPLACEMENT_MAX]),
  223. strins(name, YSI_g_sReplacements[idx][E_HOOK_NAME_REPLACEMENT_SHORT], pos);
  224. }
  225. }
  226. return len;
  227. }
  228. /*-------------------------------------------------------------------------*//**
  229. * <param name="name">The string to get the hooked function name from.</param>
  230. * <returns>
  231. * The input string without y_hooks name decorations.
  232. * </returns>
  233. *//*------------------------------------------------------------------------**/
  234. _Y_HOOKS_STATIC Hooks_IsolateName(string:name[])
  235. {
  236. P:4("Hooks_IsolateName called");
  237. new
  238. pos = strfind(name, "@", false, 4);
  239. // Make "pos" a legal value inside the error message.
  240. if (pos == -1) P:E("Invalid hook name: %s", unpack(name), ++pos);
  241. name[pos] = '\0',
  242. strdel(name, 0, 4);
  243. }
  244. /*-------------------------------------------------------------------------*//**
  245. * <param name="preloads">Desination in which to store all the preloads.</param>
  246. * <param name="precount">Number of found preload libraries.</param>
  247. * <param name="size">Maximum number of libraries to store.</param>
  248. * <remarks>
  249. * Some includes, like "fixes.inc" and anti-cheats MUST come before all other
  250. * includes in order for everything to function correctly (at least fixes.inc
  251. * must). This function looks for these definitions:
  252. *
  253. * PRE_HOOK(FIXES)
  254. *
  255. * Which tell y_hooks that any "FIXES_" prefixed callbacks are part of one of
  256. * these chains.
  257. * </remarks>
  258. *//*------------------------------------------------------------------------**/
  259. _Y_HOOKS_STATIC stock Hooks_GetPreloadLibraries(preloads[][E_PRE_HOOK], &precount, size = sizeof (preloads))
  260. {
  261. P:4("Hooks_GetPreloadLibraries called");
  262. --size,
  263. precount = 0;
  264. new
  265. entry,
  266. idx;
  267. {
  268. new
  269. name[32 char],
  270. addr;
  271. while ((idx = AMX_GetPublicEntryPrefix(idx, entry, _A<@CO_>)))
  272. {
  273. if (precount == size)
  274. {
  275. P:E("y_hooks prehook array filled");
  276. break;
  277. }
  278. addr = AMX_Read(entry);
  279. AMX_ReadString(AMX_Read(entry + 4) + AMX_BASE_ADDRESS + 4, name),
  280. strunpack(preloads[precount][E_PRE_HOOK_NAME], name, 16),
  281. preloads[precount][E_PRE_HOOK_VALUE] = CallFunction(addr),
  282. ++precount;
  283. // Remove this public from the publics table. Means that future public
  284. // function calls will be faster by having a smaller search space.
  285. Hooks_InvalidateName(entry);
  286. }
  287. }
  288. // Sort the preload libraries.
  289. {
  290. new
  291. tmp[E_PRE_HOOK];
  292. for (entry = precount - 1; entry > 0; --entry)
  293. {
  294. for (idx = 0; idx != entry; ++idx)
  295. {
  296. if (preloads[idx][E_PRE_HOOK_VALUE] > preloads[idx + 1][E_PRE_HOOK_VALUE])
  297. {
  298. tmp = preloads[idx],
  299. preloads[idx] = preloads[idx + 1],
  300. preloads[idx + 1] = tmp;
  301. }
  302. }
  303. }
  304. }
  305. }
  306. /*-------------------------------------------------------------------------*//**
  307. * <param name="preloads">Names of libraries that come before y_hooks.</param>
  308. * <param name="precount">Number of pre libraries.</param>
  309. * <param name="name">Name of the callback.</param>
  310. * <param name="hooks">Destination in which to store the headers.</param>
  311. * <param name="count">Number of headers found.</param>
  312. * <remarks>
  313. * Finds all the AMX file headers for functions with a similar name to the
  314. * given callback that should be called before (or near) the given callback.
  315. * </remarks>
  316. *//*------------------------------------------------------------------------**/
  317. _Y_HOOKS_STATIC stock Hooks_GetPreHooks(preloads[][E_PRE_HOOK], precount, name[64], hooks[], &count)
  318. {
  319. P:4("Hooks_GetPreHooks called");
  320. new
  321. idx,
  322. lfunc[64];
  323. // Collect all the functions with something like this name.
  324. do
  325. {
  326. strcat(lfunc, name),
  327. Hooks_MakeShortName(lfunc);
  328. if (AMX_GetPublicEntry(0, hooks[count], lfunc, true)) ++count;
  329. strcpy(lfunc, preloads[idx][E_PRE_HOOK_NAME]),
  330. strcat(lfunc, "_");
  331. }
  332. while (++idx <= precount);
  333. }
  334. /*-------------------------------------------------------------------------*//**
  335. * <param name="hooks">All the prehooks for this callback.</param>
  336. * <param name="num">The number of prehooks.</param>
  337. * <param name="ptr">A pointer to write the new stub address to.</param>
  338. * <param name="next">The pointer for the function called after y_hooks.</param>
  339. * <param name="name">The name of the callback being processed.</param>
  340. * <param name="nlen">Space available in the header to write text in.</param>
  341. *//*------------------------------------------------------------------------**/
  342. _Y_HOOKS_STATIC stock Hooks_GetPointerRewrite(hooks[], num, &ptr, &next, name[], nlen)
  343. {
  344. P:4("Hooks_GetPointerRewrite called");
  345. switch (num)
  346. {
  347. case 0:
  348. {
  349. next = 0;
  350. new
  351. len = strlen(name);
  352. if (nlen >= len)
  353. {
  354. // We don't have an existing callback with this name, only hooks.
  355. // We need to add the name of the callback to the AMX header,
  356. // and we have enough space in which to do so.
  357. new
  358. str[32];
  359. strpack(str, name),
  360. AMX_WriteString(AMX_BASE_ADDRESS + AMX_Read(ptr + 4), str, len);
  361. }
  362. else
  363. {
  364. P:F("Could not write function name in \"Hooks_MakePublicPointer\".");
  365. // TODO: Fix this. Use an alternate memory location (the actual
  366. // code segment in which we are writing seems like a good
  367. // choice).
  368. }
  369. }
  370. case 1:
  371. {
  372. // No "fixes.inc", but this callback already exists. In that case,
  373. // just replace the pointer address.
  374. next = ptr = hooks[0];
  375. }
  376. default:
  377. {
  378. // Special hooks. Optimise them.
  379. for (new cur = 1; cur != num; ++cur)
  380. {
  381. ptr = hooks[cur];
  382. new
  383. tmp = AMX_Read(ptr),
  384. nt = Hooks_GetStubEntry(tmp);
  385. tmp += AMX_HEADER_COD,
  386. AMX_Write(tmp, _:RelocateOpcode(OP_JUMP));
  387. switch (nt)
  388. {
  389. case -1: ptr = tmp + 4, next = 0;
  390. case 0: next = 0;
  391. default:
  392. {
  393. ptr = tmp + 4,
  394. next = tmp + nt,
  395. nt = AMX_Read(next),
  396. // Chain those not hooked.
  397. AMX_Write(ptr, nt),
  398. // Store the possible next address.
  399. AMX_Write(next, nt - (AMX_REAL_DATA + AMX_HEADER_COD));
  400. }
  401. }
  402. }
  403. }
  404. }
  405. }
  406. /*-------------------------------------------------------------------------*//**
  407. * <param name="stub">Starting address of the function.</param>
  408. * <returns>
  409. * The address at which the actual code in this function starts.
  410. * </returns>
  411. * <remarks>
  412. * This handles three cases. Regular functions end instantly as found.
  413. * Functions that start with a switch (even before "PROC") are assumed to be
  414. * state-based functions, and we find the most likely state to be used (i.e. we
  415. * remove all future state changes).
  416. * </remarks>
  417. *//*------------------------------------------------------------------------**/
  418. _Y_HOOKS_STATIC stock Hooks_GetStubEntry(stub)
  419. {
  420. P:4("Hooks_GetStubEntry called");
  421. // Get the address of the next function from the ALS state stub.
  422. new
  423. ctx[DisasmContext];
  424. DisasmInit(ctx, stub, stub + 64);
  425. switch (DisasmNextInsn(ctx))
  426. {
  427. case OP_LOAD_PRI:
  428. {
  429. if (DisasmNextInsn(ctx) == OP_SWITCH && DisasmNextInsn(ctx) == OP_CASETBL)
  430. {
  431. // Get the number of items in the casetable.
  432. if (DisasmGetNumOperands(ctx) == 3) // 2 means no used hook.
  433. {
  434. // Got a hook to return. Find it.
  435. new
  436. h0 = DisasmGetOperand(ctx, 3),
  437. h1 = DisasmGetOperand(ctx, 5),
  438. h2 = DisasmGetOperand(ctx, 7);
  439. if (h1 == h2) return 8 * 4; // Most likely.
  440. else if (h0 == h2) return 10 * 4;
  441. else if (h0 == h1) return 12 * 4;
  442. else P:E("y_hooks could not extract state stub jump");
  443. }
  444. else return -1;
  445. }
  446. }
  447. case OP_JUMP:
  448. {
  449. // Already replaced once (shouldn't happen, but may if two different
  450. // hooks use two different short versions of a callback).
  451. return 4; // DisasmGetOperand(ctx, 0);
  452. }
  453. case OP_PROC:
  454. {
  455. //return stub;
  456. P:E("y_hooks attempting to redirect a PROC hook");
  457. }
  458. }
  459. return 0;
  460. }
  461. /*-------------------------------------------------------------------------*//**
  462. * <param name="name">The name of the callback (with y_hooks prefix).</param>
  463. * <param name="hooks">Array in which to store the function headers.</param>
  464. * <param name="idx">Current position in the AMX header.</param>
  465. * <param name="namelen">Min bound of space used by all these names.</param>
  466. * <returns>
  467. * The number of hooks found.
  468. * </returns>
  469. * <remarks>
  470. * The name of the function currently being processed is derived from the first
  471. * found hook. This means we already know of one hook, but to simplify the
  472. * code we get that one again here. Above we only know the name not the
  473. * address. Hence the "- 1" in "i = idx - 1" (to go back one function name).
  474. *
  475. * Our "namelen" variable already contains the full length of the first found
  476. * hook - this is the length of "name", plus N extra characters. The following
  477. * are all valid, and may occur when orders are played with:
  478. *
  479. * @yH_OnX@
  480. * @yH_OnX@1
  481. * @yH_OnX@01
  482. * @yH_OnX@024
  483. * @yH_OnX@ZZZ
  484. * @yH_OnX@999@024
  485. *
  486. * If we want to get the EXACT space taken up by all these hook names we would
  487. * need to get the string of the name in this function then measure it. There
  488. * is really no point in doing this - if we have a second we will always have
  489. * enough space for our new names. Instead, we assume that they are all just
  490. *
  491. * @yH_OnX@
  492. *
  493. * And add on that minimum length accordingly (plus 1 for the NULL character).
  494. *
  495. * This length is used if the original callback doesn't exist but hooks do. In
  496. * that case we need to add the callback to the AMX header, and there is a tiny
  497. * chance that the original name will be longer than one hook's name. In that
  498. * case, having two or more hooks will (AFAIK) always ensure that we have
  499. * enough space to write the longer name.
  500. *
  501. * If there is only one hook, no original function, and the name of the hook is
  502. * shorter than the name of the original function then we have an issue and
  503. * will have to do something else instead.
  504. * </remarks>
  505. *//*------------------------------------------------------------------------**/
  506. _Y_HOOKS_STATIC stock Hooks_GetAllHooks(const name[], hooks[128], idx, &namelen)
  507. {
  508. P:4("Hooks_GetAllHooks called: %s %d", name, idx);
  509. // Start from the very start - repeats the first item.
  510. new
  511. len = strlen(name) + 1,
  512. count,
  513. tmpName[64];
  514. while ((idx = AMX_GetPublicEntryPrefix(idx, hooks[count], _A<@yH_>)))
  515. {
  516. AMX_GetStringFromEntry(hooks[count], tmpName),
  517. len = strlen(tmpName),
  518. strunpack(tmpName, tmpName),
  519. Hooks_IsolateName(tmpName),
  520. Hooks_MakeLongName(tmpName);
  521. // Check if the fully expanded name is the name.
  522. P:6("Hooks_GetAllHooks found: %s ?= %s %d", name, tmpName, idx);
  523. if (!strcmp(tmpName, name))
  524. {
  525. Hooks_InvalidateName(hooks[count]);
  526. // Record how much consecutive space we have to play with in the
  527. // AMX. I'm slightly concerned that this code assumes that the
  528. // hooks are all consecutive, when they might not be thanks to
  529. // replacements.
  530. if (count) namelen += len; // The first hook was already counted.
  531. // Increment and count how many hooks of this type we have.
  532. if (++count == sizeof (hooks))
  533. {
  534. P:W("Hooks_GetAllHooks: Potential overflow.");
  535. break;
  536. }
  537. }
  538. }
  539. return count;
  540. }
  541. _Y_HOOKS_STATIC stock Hooks_DoAllHooks()
  542. {
  543. P:4("Hooks_DoAllHooks called");
  544. // Get the preloaders.
  545. new
  546. precount = 0,
  547. preloads[8][E_PRE_HOOK];
  548. Hooks_GetPreloadLibraries(preloads, precount);
  549. // Main loop
  550. new
  551. name[32],
  552. idx;
  553. // Get the next hook type.
  554. while ((idx = AMX_GetPublicNamePrefix(idx, name, _A<@yH_>)))
  555. {
  556. // Collect all the hooks of this function, and rewrite the call code.
  557. Hooks_Collate(preloads, precount, name, idx - 1);
  558. }
  559. Hooks_SortPublics();
  560. }
  561. _Y_HOOKS_STATIC stock Hooks_Collate(preloads[][E_PRE_HOOK], precount, name[32], idx)
  562. {
  563. P:4("Hooks_Collate called: %s %d", unpack(name), idx);
  564. // This records the amount of space available in the nametable, currently
  565. // taken up by the names of hooks that we are about to destroy.
  566. new
  567. namelen = strlen(name);
  568. // For this function, note that:
  569. //
  570. // hook OnPlayerConnect(playerid)
  571. //
  572. // Compiles as:
  573. //
  574. // public @yH_OnPlayerConnect@XXX(playerid)
  575. //
  576. // Where "XXX" is some unique number (the exact value is irrelevant, it just
  577. // means that multiple hooks of the same function have different names).
  578. static
  579. sName[64],
  580. sHooks[128];
  581. // The above now becomes:
  582. //
  583. // OnPlayerConnect
  584. //
  585. // This also handles cases such as:
  586. //
  587. // @yH_OnPlayerEnterRaceCheckpoint@162
  588. //
  589. // Being invalid (too long), so instead converts the shortened:
  590. //
  591. // @yH_OnPlayerEnterRaceCP@162
  592. //
  593. // To:
  594. //
  595. // OnPlayerEnterRaceCheckpoint
  596. //
  597. // Thus expanding common name length reductions.
  598. strunpack(sName, name),
  599. Hooks_IsolateName(sName),
  600. Hooks_MakeLongName(sName);
  601. new
  602. // Get all the hooks of this type. They are stored alphabetically.
  603. hookCount = Hooks_GetAllHooks(sName, sHooks, idx, namelen),
  604. writePtr = sHooks[0], // Header for the first found hook.
  605. nextPtr,
  606. pc, ph[4];
  607. // Get the preloads.
  608. Hooks_GetPreHooks(preloads, precount, sName, ph, pc),
  609. // Get where in the chain we are being inserted.
  610. Hooks_GetPointerRewrite(ph, pc, writePtr, nextPtr, sName, namelen);
  611. // Add ALS hooks to the end of the list.
  612. if ((sHooks[hookCount] = nextPtr)) ++hookCount;
  613. // Write the code.
  614. Hooks_GenerateCode(sName, sHooks, hookCount, writePtr, pc > 1);
  615. }
  616. /*-------------------------------------------------------------------------*//**
  617. * <param name="name">Name of the function to generate.</param>
  618. * <param name="hooks">All the functions to call.</param>
  619. * <param name="count">Number of functions to call.</param>
  620. * <param name="write">Where to write the new function's pointer.</param>
  621. * <param name="hasprehooks">Needs to call other stuff first.</param>
  622. *//*------------------------------------------------------------------------**/
  623. _Y_HOOKS_STATIC Hooks_GenerateCode(name[64], hooks[], count, write, bool:hasprehooks)
  624. {
  625. P:4("Hooks_GenerateCode called");
  626. // We now have:
  627. //
  628. // 1) All the hooks of this function.
  629. // 2) The original function if it exists.
  630. // 3) Special ALS chained functions if they exists.
  631. //
  632. // This took huge chunks of complex code in the old version. Now not so
  633. // much! I don't know if this code is faster (I suspect it is), but it is
  634. // absolutely simpler!
  635. new
  636. size = Hooks_WriteFunction(hooks, count, Hooks_GetDefaultReturn(name));
  637. P:4("Hooks_GenerateCode %32s called: %6d %6d %08x %d", name[4], hasprehooks, size, hasprehooks ? (write - AMX_HEADER_COD) : (write - AMX_BASE_ADDRESS), CGen_GetCodeSpace());
  638. if (size)
  639. {
  640. //AMX_Write(write, 40);
  641. if (hasprehooks) AMX_Write(write, CGen_GetCodeSpace() + AMX_REAL_DATA);
  642. else AMX_Write(write, CGen_GetCodeSpace() - AMX_HEADER_COD);
  643. CGen_AddCodeSpace(size);
  644. }
  645. else
  646. {
  647. if (hasprehooks) AMX_Write(write, AMX_Read(hooks[0]) + (AMX_REAL_DATA + AMX_HEADER_COD));
  648. else AMX_Write(write, AMX_Read(hooks[0]));
  649. }
  650. }
  651. /*-------------------------------------------------------------------------*//**
  652. * <param name="entry">The public function slot to destroy.</param>
  653. * <remarks>
  654. * Basically, once we know a function has been included, wipe it from the AMX
  655. * header.
  656. * </remarks>
  657. *//*------------------------------------------------------------------------**/
  658. _Y_HOOKS_STATIC Hooks_InvalidateName(entry)
  659. {
  660. P:4("Hooks_InvalidateName called");
  661. AMX_Write(AMX_BASE_ADDRESS + AMX_Read(entry + 4), 0);
  662. }
  663. /*
  664. 88888888888 88 88 ad88
  665. 88 ,d "" 88 d8"
  666. 88 88 88 88
  667. 88aaaaa 88 88 8b,dPPYba, ,adPPYba, MM88MMM 88 ,adPPYba, 8b,dPPYba, 88 8b,dPPYba, MM88MMM ,adPPYba,
  668. 88""""" 88 88 88P' `"8a a8" "" 88 88 a8" "8a 88P' `"8a 88 88P' `"8a 88 a8" "8a
  669. 88 88 88 88 88 8b 88 88 8b d8 88 88 88 88 88 88 8b d8
  670. 88 "8a, ,a88 88 88 "8a, ,aa 88, 88 "8a, ,a8" 88 88 88 88 88 88 "8a, ,a8"
  671. 88 `"YbbdP'Y8 88 88 `"Ybbd8"' "Y888 88 `"YbbdP"' 88 88 88 88 88 88 `"YbbdP"'
  672. */
  673. /*-------------------------------------------------------------------------*//**
  674. * <param name="name">The function to get the address pointer of.</param>
  675. * <param name="write">Destination variable.</param>
  676. * <returns>
  677. * The address at which this function's pointer is stored in the AMX header, if
  678. * the function exists of course.
  679. * </returns>
  680. *//*------------------------------------------------------------------------**/
  681. _Y_HOOKS_STATIC stock Hooks_GetFunctionWritePoint(name[], &write)
  682. {
  683. P:4("Hooks_GetFunctionWritePoint called");
  684. AMX_GetPublicEntry(0, write, name, true);
  685. }
  686. /*-------------------------------------------------------------------------*//**
  687. * <param name="name">The function to get the default return of.</param>
  688. * <returns>
  689. * The default return for a callback, normally 1.
  690. * </returns>
  691. *//*------------------------------------------------------------------------**/
  692. _Y_HOOKS_STATIC stock Hooks_GetDefaultReturn(name[64])
  693. {
  694. P:4("Hooks_GetDefaultReturn called");
  695. strins(name, "@RET", 0);
  696. Hooks_MakeShortName(name);
  697. new
  698. ptr;
  699. if (AMX_GetPublicEntry(0, ptr, name, true))
  700. {
  701. // A "RET_OnWhatever" function exists - rationalise the return.
  702. return CallFunction(AMX_Read(ptr)) ? 1 : 0;
  703. }
  704. return 1;
  705. }
  706. /*
  707. ,ad8888ba, 88
  708. d8"' `"8b 88
  709. d8' 88
  710. 88 ,adPPYba, ,adPPYb,88 ,adPPYba, ,adPPYb,d8 ,adPPYba, 8b,dPPYba,
  711. 88 a8" "8a a8" `Y88 a8P_____88 a8" `Y88 a8P_____88 88P' `"8a
  712. Y8, 8b d8 8b 88 8PP""""""" 8b 88 8PP""""""" 88 88
  713. Y8a. .a8P "8a, ,a8" "8a, ,d88 "8b, ,aa "8a, ,d88 "8b, ,aa 88 88
  714. `"Y8888Y"' `"YbbdP"' `"8bbdP"Y8 `"Ybbd8"' `"YbbdP"Y8 `"Ybbd8"' 88 88
  715. aa, ,88
  716. "Y8bbdP"
  717. */
  718. /*-------------------------------------------------------------------------*//**
  719. * <param name="pointers">The hooks to link together.</param>
  720. * <param name="size">The number of functions in the array.</param>
  721. * <param name="ret">The default return.</param>
  722. * <param name="skipable">Can future hooks be ignored on -1?</param>
  723. * <returns>
  724. * The number of bytes written to memory.
  725. * </returns>
  726. * <remarks>
  727. * Generate some new code, very nicely :D.
  728. * </remarks>
  729. *//*------------------------------------------------------------------------**/
  730. _Y_HOOKS_STATIC Hooks_WriteFunction(const pointers[], const size, const ret = 1, const skipable = true)
  731. {
  732. P:4("Hooks_WriteFunction called");
  733. if (size == 0)
  734. {
  735. P:E("Hooks_WriteFunction: size is 0");
  736. return 0;
  737. }
  738. new
  739. bool:multiple = size != 1,
  740. base = (AMX_HEADER_COD - AMX_BASE_ADDRESS) + AMX_REAL_ADDRESS,
  741. ctx[AsmContext];
  742. // Make sure the underlying system doesn't change without us. Now supported
  743. // natively.
  744. CGen_UseCodeSpace(ctx);
  745. // Start of the function.
  746. @emit PROC // 1
  747. // Allocate space for our "ret" variable at "frm - 4".
  748. if (multiple) @emit PUSH.C ret // 3
  749. // Copy the stack to itself (MOVS).
  750. // Allocate space.
  751. @emit LOAD.S.alt 8 // 5
  752. @emit LCTRL 4 // 7
  753. @emit SUB // 8
  754. @emit SCTRL 4 // 10
  755. @emit XCHG // 11
  756. // The "MOVS" OpCode only takes a constant, not a variable, so we need to
  757. // generate self-modifying code (just to be UBER meta)! This code is
  758. // generated AFTER the file is loaded so we bypass the data segment checks
  759. // and can freely write wherever we want.
  760. @emit STOR.pri (CGen_GetCodeSpace() + (18 * 4) - (multiple ? 0 : 8)) // 13
  761. // Do the copying. "alt" is already "STK", load the "FRM" offset.
  762. @emit LCTRL 5 // 15
  763. @emit ADD.C 12 // 17
  764. // This is the instruction we want to modify...
  765. @emit MOVS 0 // 19 (- 1)
  766. // Push the (fake) number of parameters.
  767. @emit PUSH.C -4
  768. // Now loop over all our functions and insert "CALL" opcodes for them.
  769. if (multiple)
  770. {
  771. for (new i = 0; ; )
  772. {
  773. // Get the absolute offset from here.
  774. @emit CALL (AMX_Read(pointers[i]) + base) // 2
  775. if (skipable)
  776. {
  777. // =====================================
  778. // THIS SECTION IS CURRENTLY 10 CELLS.
  779. // =====================================
  780. // Note: Including the original call...
  781. //
  782. // if (func() < 0) break;
  783. // else ret = ret & func();
  784. //
  785. @emit ZERO.alt // 3
  786. @emit JSLESS.rel ((size - i) * (10 * 4) - (5 * 4)) // 5
  787. // =========================
  788. // JUMP OVER THIS SECTION.
  789. // =========================
  790. }
  791. @emit LOAD.S.alt -4 // 7
  792. if (ret) @emit AND // 8
  793. else @emit OR // 8
  794. // Loop and do the very first items last.
  795. if (++i == size) break;
  796. else @emit STOR.S.pri -4 // 10
  797. }
  798. if (skipable)
  799. {
  800. @emit JUMP.rel 4 // 10
  801. // This is the point the large "JSLESS" above goes to.
  802. // -1 = 0, -2 = 1
  803. @emit INVERT
  804. }
  805. }
  806. else if (skipable)
  807. {
  808. // Still need this code as they may hook a function that doesn't exist,
  809. // but we still need to correctly process -1 or -2.
  810. @emit CALL (AMX_Read(pointers[0]) + base)
  811. @emit ZERO.alt
  812. @emit JSGEQ.rel 4
  813. @emit INVERT
  814. }
  815. else
  816. {
  817. // Just replace the original (turns out, this takes no code). Basically
  818. // just discard everything we've written so far (reclaims the memory).
  819. return 0;
  820. }
  821. // This is the point the small "JUMP" above goes to.
  822. @emit MOVE.alt
  823. // Remove the whole stack then get the return value.
  824. @emit LCTRL 5
  825. @emit SCTRL 4
  826. //@emit LOAD.S.pri -4
  827. @emit MOVE.pri
  828. // Return.
  829. @emit RETN
  830. // Return the number of bytes written.
  831. return ctx[AsmContext_buffer_offset];
  832. }
  833. /*
  834. ad88888ba 88
  835. d8" "8b ,d ""
  836. Y8, 88
  837. `Y8aaaaa, ,adPPYba, 8b,dPPYba, MM88MMM 88 8b,dPPYba, ,adPPYb,d8
  838. `"""""8b, a8" "8a 88P' "Y8 88 88 88P' `"8a a8" `Y88
  839. `8b 8b d8 88 88 88 88 88 8b 88
  840. Y8a a8P "8a, ,a8" 88 88, 88 88 88 "8a, ,d88
  841. "Y88888P" `"YbbdP"' 88 "Y888 88 88 88 `"YbbdP"Y8
  842. aa, ,88
  843. "Y8bbdP"
  844. */
  845. /*-------------------------------------------------------------------------*//**
  846. * <param name="addr0">The 1st address to read.</param>
  847. * <param name="addr1">The 2nd address to read.</param>
  848. * <returns>
  849. * -1 - The first address is bigger.
  850. * 0 - The addresses are the same
  851. * 1 - The second address is bigger.
  852. * </returns>
  853. * <remarks>
  854. * Reads two addresses, converts them to big endian, and compares them as four
  855. * characters of a string at once.
  856. * </remarks>
  857. *//*------------------------------------------------------------------------**/
  858. _Y_HOOKS_STATIC Hooks_CompareNextCell(addr0, addr1)
  859. {
  860. // P:4("Hooks_CompareNextCell called");
  861. new
  862. s0 = Cell_ReverseBytes(AMX_Read(addr0)),
  863. s1 = Cell_ReverseBytes(AMX_Read(addr1));
  864. // Propogate NULLs.
  865. if (!(s0 & 0xFF000000)) s0 = 0;
  866. else if (!(s0 & 0x00FF0000)) s0 &= 0xFF000000;
  867. else if (!(s0 & 0x0000FF00)) s0 &= 0xFFFF0000;
  868. else if (!(s0 & 0x000000FF)) s0 &= 0xFFFFFF00;
  869. if (!(s1 & 0xFF000000)) s1 = 0;
  870. else if (!(s1 & 0x00FF0000)) s1 &= 0xFF000000;
  871. else if (!(s1 & 0x0000FF00)) s1 &= 0xFFFF0000;
  872. else if (!(s1 & 0x000000FF)) s1 &= 0xFFFFFF00;
  873. // We need the numbers to be compared as big-endian. Now any trailing NULLs
  874. // don't matter at all.
  875. if (s1 > s0) return 1;
  876. else if (s1 < s0) return -1;
  877. else return 0;
  878. }
  879. /*-------------------------------------------------------------------------*//**
  880. * <param name="idx0">The index of the 1st public.</param>
  881. * <param name="idx1">The index of the 2nd public.</param>
  882. * <remarks>
  883. * Compares two public function entries, and if need-be, swaps them over.
  884. * </remarks>
  885. *//*------------------------------------------------------------------------**/
  886. _Y_HOOKS_STATIC Hooks_ComparePublics(idx0, idx1)
  887. {
  888. // P:4("Hooks_ComparePublics called");
  889. idx0 = idx0 * 8 + AMX_HEADER_PUBLICS;
  890. idx1 = idx1 * 8 + AMX_HEADER_PUBLICS;
  891. new
  892. addr0 = AMX_BASE_ADDRESS + AMX_Read(idx0 + 4),
  893. addr1 = AMX_BASE_ADDRESS + AMX_Read(idx1 + 4);
  894. for ( ; ; )
  895. {
  896. switch (Hooks_CompareNextCell(addr0, addr1))
  897. {
  898. case -1:
  899. {
  900. // Swap them over.
  901. new
  902. tmpFunc = AMX_Read(idx0),
  903. tmpName = AMX_Read(idx0 + 4);
  904. AMX_Write(idx0, AMX_Read(idx1));
  905. AMX_Write(idx0 + 4, AMX_Read(idx1 + 4));
  906. AMX_Write(idx1, tmpFunc);
  907. AMX_Write(idx1 + 4, tmpName);
  908. return;
  909. }
  910. case 1:
  911. {
  912. // Already in order - good.
  913. return;
  914. }
  915. // case 0: // Loop.
  916. }
  917. addr0 += 4;
  918. addr1 += 4;
  919. }
  920. }
  921. /*-------------------------------------------------------------------------*//**
  922. * <remarks>
  923. * Goes through the whole of the public functions table and sorts them all in
  924. * to alphabetical order. This is done as we move and rename some so we need
  925. * to fix the virtual machine's binary search.
  926. * </remarks>
  927. *//*------------------------------------------------------------------------**/
  928. _Y_HOOKS_STATIC Hooks_SortPublics()
  929. {
  930. P:4("Hooks_SortPublics called");
  931. // Count the number of still active functions.
  932. new
  933. diff = Hooks_CountInvalidPublics() * 8,
  934. oldCount = (AMX_HEADER_NATIVES - AMX_HEADER_PUBLICS) / 8;
  935. // Now I need to SORT the functions, and I have honestly no idea how to do
  936. // that. Fortunately I don't actually need to move the strings themselves
  937. // around as they just sit nicely in the nametable; I only need to sort the
  938. // pointers.
  939. for (new i = oldCount - 1; i > 0; --i)
  940. {
  941. for (new j = 0; j != i; ++j)
  942. {
  943. // This neatly moves all the functions with blanked names to the
  944. // start of the public functions table (which will soon be moved).
  945. Hooks_ComparePublics(j, j + 1);
  946. }
  947. }
  948. // Move the start address UP to reduce the VM's search space.
  949. if (diff)
  950. {
  951. // Update stored values in y_amx so they reflect the new structure.
  952. AMX_Write(AMX_BASE_ADDRESS + 32, AMX_Read(AMX_BASE_ADDRESS + 32) + diff);
  953. AMX_HEADER_PUBLICS += diff;
  954. // I'd love to be able to update ZeeX's code as well, but I can't.
  955. // Issue pull request for this.
  956. ResetStaticAmxHeader();
  957. }
  958. // TODO: Inform the fixes2 plugin of the change. That stores indexes, not
  959. // addresses so it needs to update itself (somehow - I don't actually know
  960. // HOW it will do this...) Probably inform it first, store the addresses,
  961. // then inform it again to track down and replace those addresses.
  962. }
  963. /*-------------------------------------------------------------------------*//**
  964. * <remarks>
  965. * Counts the number of public functions that have had their names erased.
  966. * </remarks>
  967. *//*------------------------------------------------------------------------**/
  968. _Y_HOOKS_STATIC Hooks_CountInvalidPublics()
  969. {
  970. P:4("Hooks_CountInvalidPublics called");
  971. new
  972. idx,
  973. buf,
  974. count;
  975. // Search for functions whose names start with nothing.
  976. while ((idx = AMX_GetPublicEntryPrefix(idx, buf, 0)))
  977. ++count;
  978. P:4("Hooks_CountInvalidPublics: Invalid = %d", count);
  979. return count;
  980. }
  981. /*-------------------------------------------------------------------------*//**
  982. * <remarks>
  983. * Call the main hook run code, then advance the ALS chain.
  984. * </remarks>
  985. *//*------------------------------------------------------------------------**/
  986. // New stuff.
  987. stock _Hooks_AddReplacement(const longName[], const shortName[])
  988. {
  989. // MAY need to strip spaces off the input strings, but I don't think so.
  990. if (YSI_g_sReplacePtr == MAX_HOOK_REPLACEMENTS)
  991. {
  992. P:E("Insufficient space in the replacements table.");
  993. return;
  994. }
  995. strcpy(YSI_g_sReplacements[YSI_g_sReplacePtr][E_HOOK_NAME_REPLACEMENT_SHORT], shortName, 16),
  996. strcpy(YSI_g_sReplacements[YSI_g_sReplacePtr][E_HOOK_NAME_REPLACEMENT_LONG] , longName , 16),
  997. YSI_g_sReplacements[YSI_g_sReplacePtr][E_HOOK_NAME_REPLACEMENT_MIN] = strlen(shortName),
  998. YSI_g_sReplacements[YSI_g_sReplacePtr][E_HOOK_NAME_REPLACEMENT_MAX] = strlen(longName),
  999. YSI_g_sReplacementsLongOrder[YSI_g_sReplacePtr] = YSI_g_sReplacePtr,
  1000. YSI_g_sReplacementsShortOrder[YSI_g_sReplacePtr] = YSI_g_sReplacePtr,
  1001. ++YSI_g_sReplacePtr;
  1002. }
  1003. /*-------------------------------------------------------------------------*//**
  1004. * <remarks>
  1005. * Once all the replacement strings have been found, sort them by the length of
  1006. * the short versions of the strings. This is so that the longest (and special
  1007. * case, e.g. "DynamicCP"-> "DynamicCP") replacements are always done first.
  1008. * </remarks>
  1009. *//*------------------------------------------------------------------------**/
  1010. static stock Hooks_SortReplacements()
  1011. {
  1012. new
  1013. idx0,
  1014. idx1,
  1015. temp;
  1016. for (new i = YSI_g_sReplacePtr - 1; i > 0; --i)
  1017. {
  1018. for (new j = 0; j != i; ++j)
  1019. {
  1020. // Sort the strings in order of their short replacement.
  1021. idx0 = YSI_g_sReplacementsShortOrder[j],
  1022. idx1 = YSI_g_sReplacementsShortOrder[j + 1];
  1023. if (YSI_g_sReplacements[idx0][E_HOOK_NAME_REPLACEMENT_MIN] < YSI_g_sReplacements[idx1][E_HOOK_NAME_REPLACEMENT_MIN])
  1024. {
  1025. temp = YSI_g_sReplacementsShortOrder[j],
  1026. YSI_g_sReplacementsShortOrder[j] = YSI_g_sReplacementsShortOrder[j + 1],
  1027. YSI_g_sReplacementsShortOrder[j + 1] = temp;
  1028. }
  1029. // Sort the strings in order of their long replacement.
  1030. idx0 = YSI_g_sReplacementsLongOrder[j],
  1031. idx1 = YSI_g_sReplacementsLongOrder[j + 1];
  1032. if (YSI_g_sReplacements[idx0][E_HOOK_NAME_REPLACEMENT_MAX] < YSI_g_sReplacements[idx1][E_HOOK_NAME_REPLACEMENT_MAX])
  1033. {
  1034. temp = YSI_g_sReplacementsLongOrder[j],
  1035. YSI_g_sReplacementsLongOrder[j] = YSI_g_sReplacementsLongOrder[j + 1],
  1036. YSI_g_sReplacementsLongOrder[j + 1] = temp;
  1037. }
  1038. }
  1039. }
  1040. P:C(for (new i = 0 ; i != YSI_g_sReplacePtr; ++i) P:0("Hook Replacement: %d, %d, %s, %s", YSI_g_sReplacements[i][E_HOOK_NAME_REPLACEMENT_MIN], YSI_g_sReplacements[i][E_HOOK_NAME_REPLACEMENT_MAX], YSI_g_sReplacements[i][E_HOOK_NAME_REPLACEMENT_SHORT], YSI_g_sReplacements[i][E_HOOK_NAME_REPLACEMENT_LONG]););
  1041. }
  1042. /*-------------------------------------------------------------------------*//**
  1043. * <remarks>
  1044. * Call the main hook run code, then advance the ALS chain.
  1045. * </remarks>
  1046. * <transition keep="true" target="_ALS : _ALS_go"/>
  1047. *//*------------------------------------------------------------------------**/
  1048. public OnScriptInit()
  1049. {
  1050. P:1("Hooks_OnScriptInit called");
  1051. state _ALS : _ALS_go;
  1052. // Get the replacements.
  1053. new
  1054. idx,
  1055. entry;
  1056. // Loop over the redefinition functions and call them to have them call the
  1057. // "_Hooks_AddReplacement" function above. If we were being REALLY clever,
  1058. // these functions could be removed from the public functions table
  1059. // afterwards (there is already code in y_hooks for this) to reduce is size.
  1060. while ((idx = AMX_GetPublicEntryPrefix(idx, entry, _A<@_yH>)))
  1061. {
  1062. // From "amx\dynamic_call.inc" - check it is included in "y_hooks.inc".
  1063. CallFunction(AMX_Read(entry));
  1064. Hooks_InvalidateName(entry);
  1065. }
  1066. Hooks_SortReplacements();
  1067. Hooks_DoAllHooks();
  1068. P:1("Hooks_OnScriptInit chain");
  1069. // Dump the generated callbacks for debugging.
  1070. //DisasmDump("YSI_TEST.asm");
  1071. HookChain_OnScriptInit();
  1072. P:1("Hooks_OnScriptInit end");
  1073. return 1;
  1074. }
  1075. HOOK_FORWARD:HookChain_OnScriptInit();
  1076. #if defined _ALS_OnScriptInit
  1077. #undef OnScriptInit
  1078. #else
  1079. #define _ALS_OnScriptInit
  1080. #endif
  1081. #define OnScriptInit(%0) HookChain_OnScriptInit(%0) <_ALS : _ALS_go>
  1082. #if !defined _ALS_numargs
  1083. native BAD_numargs() = numargs;
  1084. #endif
  1085. stock Hooks_NumArgs()
  1086. {
  1087. // Awkward code that allows us to jump backwards. It might appear that this
  1088. // code pushes something on the stack before the JZER that is never removed,
  1089. // but labels reset the stack always so it gets unconditionally wiped out.
  1090. #emit LOAD.S.alt 0
  1091. Hooks_NumArgs_load:
  1092. #emit CONST.pri 8
  1093. #emit ADD
  1094. #emit LOAD.I
  1095. #emit ZERO.alt
  1096. #emit PUSH.pri
  1097. #emit SGEQ
  1098. #emit LREF.S.alt 0
  1099. #emit JZER Hooks_NumArgs_load
  1100. #emit POP.pri
  1101. #emit SHR.C.pri 2
  1102. #emit RETN
  1103. // Ideal code, that can't be used due to having a forward jump.
  1104. /* #emit LOAD.S.pri 0
  1105. #emit ADD.C 8
  1106. #emit LOAD.I
  1107. #emit ZERO.alt
  1108. #emit JSLESS Hooks_NumArgs_negative
  1109. #emit SHR.C.pri 2
  1110. #emit RETN
  1111. Hooks_NumArgs_negative:
  1112. #emit LREF.S.pri 0
  1113. #emit ADD.C 8
  1114. #emit LOAD.I
  1115. #emit SHR.C.pri 2
  1116. #emit RETN*/
  1117. return 0;
  1118. }
  1119. #if defined _ALS_numargs
  1120. #undef numargs
  1121. #else
  1122. #define _ALS_numargs
  1123. #endif
  1124. #define numargs Hooks_NumArgs