1
0

impl.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. // Disable this version!
  65. static stock
  66. Alloc:YSI_g_sLastSlot = NO_ALLOC,
  67. Alloc:YSI_g_sFirstSlot = NO_ALLOC,
  68. YSI_g_sPlayerTimers = -1;
  69. hook OnScriptInit()
  70. {
  71. P:1("hook Timers_OnScriptInit called");
  72. new
  73. pointer,
  74. time,
  75. idx,
  76. entry;
  77. while ((idx = AMX_GetPublicEntryPrefix(idx, entry, _A<@yT_>)))
  78. //while ((idx = AMX_GetPublicPointerSuffix(idx, pointer, _A<@yT_>)))
  79. {
  80. P:6("Timer_OnScriptInit: entry: %d", entry);
  81. #emit LREF.S.pri entry
  82. #emit STOR.S.pri pointer
  83. //YSI_g_sCurFunc = pointer;
  84. // Don't bother with the real name, call the function by address to get
  85. // the time the function runs for.
  86. P:7("Timer_OnScriptInit: pointer: %d", pointer);
  87. // Push the address of the current function.
  88. #emit PUSH.S pointer
  89. #emit PUSH.C 0xFFFFFFFF
  90. #emit PUSH.C 8
  91. #emit LCTRL 6
  92. #emit ADD.C 28
  93. #emit PUSH.pri
  94. #emit LOAD.S.pri pointer
  95. #emit SCTRL 6
  96. #emit STOR.S.pri time
  97. //YSI_g_sCurFunc = 0;
  98. P:7("Timer_OnScriptInit: time: %d", time);
  99. if (time != -1)
  100. {
  101. // Find all the functions with the same time. This is less
  102. // efficient than previous implementations (it is O(N^2)), but also
  103. // more robust as it won't fail no matter how many different times
  104. // there are - old ones relied on an array with a finite size.
  105. new
  106. pointer2,
  107. time2,
  108. idx2,
  109. total,
  110. pre;
  111. while ((idx2 = AMX_GetPublicPointerPrefix(idx2, pointer2, _A<@yT_>)))
  112. {
  113. // Call the functions a second time to guarantee getting
  114. #emit PUSH.C 0
  115. #emit PUSH.C 0xFFFFFFFF
  116. #emit PUSH.C 8
  117. #emit LCTRL 6
  118. #emit ADD.C 28
  119. #emit PUSH.pri
  120. #emit LOAD.S.pri pointer2
  121. #emit SCTRL 6
  122. #emit STOR.S.pri time2
  123. // Check if the new time is a FACTOR, SAME, or MULTIPLE of this
  124. // task, so we don't start different timers together.
  125. if (time2 == time || time / time2 * time2 == time || time2 / time * time == time2)
  126. {
  127. ++total;
  128. if (idx2 < idx)
  129. {
  130. ++pre;
  131. }
  132. }
  133. }
  134. P:7("Timer_OnScriptInit: total: %d, time: %d, pre: %d", total, time, pre);
  135. // Now we know what time this function has, how many others have
  136. // that time and how many have already been started.
  137. new
  138. buffer[32];
  139. entry += 4;
  140. #emit LREF.S.pri entry
  141. #emit STOR.S.pri pointer
  142. AMX_ReadString(AMX_BASE_ADDRESS + pointer, buffer);
  143. P:7("Timer_OnScriptInit: %s", unpack(buffer));
  144. // Get the time offset for the current call. This should mean that
  145. // all the functions are nicely spread out.
  146. SetTimerEx(buffer, time * pre / total, 0, "ii", 1, -1);
  147. }
  148. }
  149. P:1("hook Timers_OnScriptInit ended");
  150. return 1;
  151. }
  152. hook OnPlayerConnect(playerid)
  153. {
  154. P:1("hook Timers_OnPlayerConnect called: %d", playerid);
  155. // Loop through all the per-player timers. Correctly finds them all from a
  156. // linked list hidden in static variables (which are really global).
  157. new
  158. cur = YSI_g_sPlayerTimers,
  159. data;
  160. while (cur != -1)
  161. {
  162. #emit LREF.S.pri cur
  163. #emit STOR.S.pri data
  164. P:6("Timers_OnPlayerConnect: func: %x", data);
  165. // Start this timer for this player.
  166. #emit PUSH.S playerid
  167. #emit PUSH.C 1
  168. // Push the parameter count (in bytes). This is actually passed to
  169. // native functions directly.
  170. #emit PUSH.C 8
  171. // Call the function currently in the list to trigger the repeating
  172. // timer. This involves getting the current "cip" address, modifying it
  173. // to get the return address then modifying "cip" to call the function.
  174. #emit LCTRL 6
  175. #emit ADD.C 28
  176. #emit PUSH.pri
  177. #emit LOAD.S.pri data
  178. #emit SCTRL 6
  179. // Returned, get the next list element.
  180. cur += 4;
  181. #emit LREF.S.pri cur
  182. #emit STOR.S.pri cur
  183. }
  184. P:1("hook Timers_OnPlayerConnect ended");
  185. return 1;
  186. }
  187. hook OnPlayerDisconnect(playerid, reason)
  188. {
  189. P:1("hook Timers_OnPlayerDisconnect called: %d, %d, playerid, reason");
  190. // Loop through all the per-player timers. Correctly finds them all from a
  191. // linked list hidden in static variables (which are really global).
  192. new
  193. cur = YSI_g_sPlayerTimers,
  194. data;
  195. while (cur != -1)
  196. {
  197. #emit LREF.S.pri cur
  198. #emit STOR.S.pri data
  199. P:6("Timers_OnPlayerDisconnect: func: %x", data);
  200. // End this timer for this player.
  201. #emit PUSH.S playerid
  202. #emit PUSH.C 0
  203. // Push the parameter count (in bytes). This is actually passed to
  204. // native functions directly.
  205. #emit PUSH.C 8
  206. // Call the function currently in the list to trigger the repeating
  207. // timer. This involves getting the current "cip" address, modifying it
  208. // to get the return address then modifying "cip" to call the function.
  209. #emit LCTRL 6
  210. #emit ADD.C 28
  211. #emit PUSH.pri
  212. #emit LOAD.S.pri data
  213. #emit SCTRL 6
  214. // Returned, get the next list element.
  215. cur += 4;
  216. #emit LREF.S.pri cur
  217. #emit STOR.S.pri cur
  218. }
  219. P:1("hook Timers_OnPlayerDisconnect ended");
  220. return 1;
  221. }
  222. stock _Timer_I(func[], interval, action, &result)
  223. {
  224. P:3("_Timer_I called");
  225. switch (action)
  226. {
  227. case 0:
  228. {
  229. if (result != -1)
  230. {
  231. KillTimer(result),
  232. result =- 1;
  233. }
  234. }
  235. case 1:
  236. {
  237. if (result == -1)
  238. {
  239. result = SetTimer(func, interval, 1);
  240. }
  241. }
  242. }
  243. return interval;
  244. }
  245. // Attempt to stop or start a task, possibly for a single player.
  246. stock _Timer_D(func[], interval, const action, who, results[MAX_PLAYERS], a[2])
  247. {
  248. P:3("_Timer_D called");
  249. switch (action)
  250. {
  251. case -1:
  252. {
  253. if (who)
  254. {
  255. // Add this timer to the global linked list.
  256. a[0] = who;
  257. a[1] = YSI_g_sPlayerTimers;
  258. // Store the address of the global array.
  259. #emit LOAD.S.pri a
  260. #emit STOR.pri YSI_g_sPlayerTimers
  261. }
  262. }
  263. case 0:
  264. {
  265. // Stop the timer.
  266. if (who == -1)
  267. {
  268. foreach (who : Player)
  269. {
  270. if (results[who] != -1)
  271. {
  272. KillTimer(results[who]);
  273. results[who] = -1;
  274. }
  275. }
  276. }
  277. else if (results[who] != -1)
  278. {
  279. KillTimer(results[who]);
  280. results[who] = -1;
  281. }
  282. }
  283. case 1:
  284. {
  285. // Start the timer.
  286. if (who == -1)
  287. {
  288. foreach (who : Player)
  289. {
  290. if (results[who] == -1)
  291. {
  292. results[who] = SetTimerEx(func, interval, true, "i", who);
  293. }
  294. }
  295. }
  296. else if (results[who] == -1)
  297. {
  298. results[who] = SetTimerEx(func, interval, true, "i", who);
  299. }
  300. }
  301. }
  302. // No global interval for per-player timers.
  303. return -1;
  304. }
  305. static stock Alloc:Timer_GetSingleSlot(len)
  306. {
  307. // Allocates memory and secretly appends data to the start.
  308. P:4("Timer_GetSingleSlot called: %d", len);
  309. new
  310. Alloc:slot = malloc(len + 1);
  311. if (slot == NO_ALLOC)
  312. {
  313. return NO_ALLOC;
  314. }
  315. P:5("Timer_GetSingleSlot: %d, %d, %d", _:YSI_g_sFirstSlot, _:YSI_g_sLastSlot, _:slot);
  316. // Standard linked list.
  317. if (YSI_g_sFirstSlot == NO_ALLOC)
  318. {
  319. YSI_g_sFirstSlot = slot;
  320. }
  321. else
  322. {
  323. mset(YSI_g_sLastSlot, 0, _:slot);
  324. }
  325. YSI_g_sLastSlot = slot;
  326. mset(YSI_g_sLastSlot, 0, -1);
  327. return slot;// + Alloc:1;
  328. }
  329. // Allocate memory to store a string.
  330. stock _Timer_S(string:str[])
  331. {
  332. P:3("_Timer_S called");
  333. new
  334. len = strlen(str);
  335. if (len & 0x0F)
  336. {
  337. len = (len & ~0x0F) + 32;
  338. }
  339. new
  340. Alloc:slot = Timer_GetSingleSlot(len + 1);
  341. if (slot != NO_ALLOC)
  342. {
  343. msets(slot, 1, str);
  344. }
  345. P:5("str: %d", _:slot);
  346. return _:slot + 1;
  347. }
  348. // Allocate memory to store an array.
  349. stock _Timer_A(str[], len)
  350. {
  351. P:3("_Timer_A called");
  352. new
  353. Alloc:slot = Timer_GetSingleSlot(len);
  354. if (slot != NO_ALLOC)
  355. {
  356. mseta(slot, 1, str, len);
  357. }
  358. P:5("str: %d", _:slot);
  359. return _:slot + 1;
  360. }
  361. //stock
  362. // I@ = -1;
  363. // Create the timer setup.
  364. stock _Timer_C(tt, g)
  365. {
  366. P:3("_Timer_C called: %d, %d", tt, g);
  367. //P:3("_Timer_C called: %d", tt);
  368. // This is done here for convenience.
  369. I@ = -1;
  370. // Only repeating timers are freed like this.
  371. // UPDATE: Now all timers with array parameters, regardless of repeat status
  372. // are freed like this. Only timers with no malloc aren't.
  373. if (g)
  374. {
  375. new
  376. Alloc:slot = Timer_GetSingleSlot(1);
  377. P:5("_Timer_C: slot = %d", _:slot);
  378. if (slot == NO_ALLOC)
  379. {
  380. // Not a graceful fail!
  381. return 0;
  382. }
  383. mset(slot, 1, tt);
  384. // Just so it isn't a real timer ID (or possibly isn't).
  385. slot = ~YSI_g_sFirstSlot;// ^ Alloc:-1;
  386. YSI_g_sFirstSlot = NO_ALLOC;
  387. YSI_g_sLastSlot = NO_ALLOC;
  388. return _:slot;
  389. }
  390. // Reset these variables on all timers, including self-cleaning ones.
  391. YSI_g_sFirstSlot = NO_ALLOC;
  392. YSI_g_sLastSlot = NO_ALLOC;
  393. return tt;
  394. }
  395. // Free all timer resources.
  396. stock _Timer_F(slot)
  397. {
  398. P:3("_Timer_F called");
  399. // This is done here for convenience.
  400. if (slot & 0x80000000)
  401. {
  402. new
  403. next;
  404. slot = ~slot; //^= -1;
  405. for ( ; ; )
  406. {
  407. next = mget(Alloc:slot, 0);
  408. P:6("_Timer_F: slot = %d, next = %d", slot, next);
  409. // Out of stored strings and arrays.
  410. if (next == -1)
  411. {
  412. KillTimer(mget(Alloc:slot, 1));
  413. free(Alloc:slot);
  414. break;
  415. }
  416. free(Alloc:slot);
  417. slot = next;
  418. }
  419. }
  420. else
  421. {
  422. KillTimer(slot);
  423. }
  424. return 1;
  425. }
  426. stock _Timer_H(slot)
  427. {
  428. _Timer_F(~(slot - 1));
  429. }
  430. #define task%0[%1](%2) @yT_%0(g,p);@yT_%0(g,p){static s=-1;return _Timer_I(#%0,%1,g,s);}%0();public%0()
  431. //
  432. #define ptask%0[%1](%2) @yT_%0(g,p);@yT_%0(g,p){static s[MAX_PLAYERS]={-1,...},a[2];return _Timer_D(#%0,%1,g,p,s,a);}%0(%2);public%0(%2)
  433. #define @yT_%0\32;%1(%2) @yT_%0%1(%2)
  434. //#define @_yT%0;\32%1(%2) @_yT%0%1(%2)
  435. #define pause%0; {J@=_:@Ym:@yT_%0(0,-1);}
  436. #define resume%0; {J@=_:@Ym:@yT_%0(1,-1);}
  437. #define @Ym:%0[%1](%2,-1) %0(%2,%1)
  438. #define timerfunc YSI_timer
  439. #if !defined YSI_NO_timer
  440. #define timer YSI_timer
  441. #endif