impl.inc 41 KB

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