once.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /**--------------------------------------------------------------------------**\
  2. ===================
  3. YSI - Master Core
  4. ===================
  5. Description:
  6. There seems to be a bug with a compiler when using #emit in files included
  7. more than once, so this code has been moved to a file only included once.
  8. Legal:
  9. Version: MPL 1.1
  10. The contents of this file are subject to the Mozilla Public License Version
  11. 1.1 (the "License"); you may not use this file except in compliance with
  12. the License. You may obtain a copy of the License at
  13. http://www.mozilla.org/MPL/
  14. Software distributed under the License is distributed on an "AS IS" basis,
  15. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  16. for the specific language governing rights and limitations under the
  17. License.
  18. The Original Code is the YSI master include.
  19. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  20. Portions created by the Initial Developer are Copyright (C) 2011
  21. the Initial Developer. All Rights Reserved.
  22. Contributors:
  23. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  24. Thanks:
  25. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  26. ZeeX - 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. 0.2
  44. Changelog:
  45. 03/01/13:
  46. Added "@foreign" and "@global" for EXPLICIT Master_Caller use.
  47. Added "void:" return tag to simplify common code.
  48. 07/08/10:
  49. Split in to y_masteronce.
  50. Added assembly for publics scanning.
  51. 06/08/10:
  52. Managed the ultimate - got completely transparent inclusion!
  53. 05/08/10:
  54. Completed new master system, now does all syncing and clients.
  55. 06/10/09:
  56. Rewrote using states.
  57. Added remote function macros.
  58. Reduced the number of masters to a single one. It must have them all.
  59. Vastly simplified the master model - ending the master ends everything.
  60. 06/01/08:
  61. Added code to not pass data if no other script exists.
  62. 17/11/07:
  63. Added code for a script to remove itself from the global list.
  64. 11/10/07:
  65. Not first version but added documentation months late.
  66. Functions:
  67. Public:
  68. -
  69. Core:
  70. -
  71. Stock:
  72. -
  73. Static:
  74. -
  75. Inline:
  76. -
  77. API:
  78. -
  79. Hooks:
  80. OnGameModeInit
  81. OnGameModeExit
  82. Callbacks:
  83. -
  84. Definitions:
  85. -
  86. Enums:
  87. -
  88. Macros:
  89. -
  90. Tags:
  91. -
  92. Variables:
  93. Global:
  94. _@ - ID of this script.
  95. Static:
  96. -
  97. Commands:
  98. -
  99. Compile options:
  100. -
  101. Operators:
  102. -
  103. Natives:
  104. -
  105. </remarks>
  106. \**--------------------------------------------------------------------------**/
  107. #define _Y_MASTER_ONCE
  108. // These parts should only be defined once ever. This code is not dependent on
  109. // the current MASTER value, it's all generic.
  110. #include "internal\y_version"
  111. #include "y_debug"
  112. #include "y_hooks"
  113. #include "internal\y_shortfunc"
  114. #include "internal\y_natives"
  115. #define _YSIM_COMPARE -1
  116. #define MASTER_DATA<%0> static stock Bit:YSI_g_sMasterData[_:(%0)];
  117. #define MASTER_RESET<%0> YSI_g_sMasterData[_:(%0)] = Bit:0;
  118. #define MASTER_SET<%0> YSI_g_sMasterData[_:(%0)] = Bit:(1 << Master_Caller());
  119. #define MASTER_ADD<%0> YSI_g_sMasterData[_:(%0)] |= Bit:(1 << Master_Caller());
  120. #define MASTER_REMOVE<%0> YSI_g_sMasterData[_:(%0)] &= Bit:(~(1 << Master_Caller()));
  121. #define MASTER_EMPTY<%0> if(!YSI_g_sMasterData[_:(%0)])
  122. #if defined YSI_IS_CLIENT
  123. #if NO_VALUE(YSI_IS_CLIENT)
  124. #undef YSI_IS_CLIENT
  125. #define YSI_IS_CLIENT 100
  126. #endif
  127. #endif
  128. #if defined YSI_IS_SERVER
  129. #if NO_VALUE(YSI_IS_SERVER)
  130. #undef YSI_IS_SERVER
  131. #define YSI_IS_SERVER 100
  132. #endif
  133. #endif
  134. #if defined YSI_IS_STUB
  135. #if NO_VALUE(YSI_IS_STUB)
  136. #undef YSI_IS_STUB
  137. #define YSI_IS_STUB 100
  138. #endif
  139. #endif
  140. // Define all the alternate spellings once.
  141. #define RF@cp RF@pc
  142. #define RF@pvc RF@pcv
  143. #define RF@cpv RF@pcv
  144. #define RF@cvp RF@pcv
  145. #define RF@vpc RF@pcv
  146. #define RF@vcp RF@pcv
  147. #define RF@vp RF@pv
  148. #define RF@cv RF@vc
  149. #define RF@cpt RF@pct
  150. #define RF@tcp RF@pct
  151. #define RF@tpc RF@pct
  152. #define RF@ctp RF@pct
  153. #define RF@ptc RF@pct
  154. #define RF@tp RF@pt
  155. #define RF@tc RF@ct
  156. #define mhook master_hook
  157. #define OnScriptClose Master_OnScriptClose
  158. forward Master_OnScriptClose(script);
  159. // These are the macros used by y_master for the recursive function definition
  160. // structure, but they are not used unless a cloud-based "foreign" or "global"
  161. // are used.
  162. #define W@(@Zu:#%0:%1#,%2);return%3(%8) W@(@Zu:#%1,%2);return%0:%3(%0:%8)
  163. #define Z@(%0string:i) S@(i)
  164. #define V@(%0string:%1) F@()
  165. // Second "foreign" declaration.
  166. //#define @Zu:Y@();W@(#%0##,_YM@CX:,,);return%1(%2);}%3(%4);%5(%6)<%7> Y@();W@(#%0##,#);return%1(%2);}%3(%4);stock %5(%6)<%7>return @Zv:@Zq:%5&H@();
  167. #define @Zu:#%0,_YM@C%8:,,);return%1(%2);}%5(%6)<%7> @Z%8:#%0,#);return%1(%2);}stock%5(%6)<%7>return @Zv:@Zq:%5&H@()
  168. //#define @Zu:#%0,_YM@CX:,,) @Zl:#%0,#)
  169. // Second "foreign" declaration.
  170. // This has been updated to check for arrays, then check if the array is a
  171. // string, instead of the old method which checked for those separately.
  172. #define @Zn:@Zo:%2:#%0#%1|||%3[%4]|||%5,%6);%7}%8&H@(%9||| @Zm:@Zr:%2:#%0#%1|||%3|||%5,%6);%7}%8&H@(%9|||
  173. #define @Zm:@Zr:%2:#%0#%1|||%4string:%3|||%5,%6);%7}%8&H@(%9||| @Zn:@Zo:%2:#%0s#%1,%3|||%5|||%6);%7}%8&H@(%9,%3|||
  174. #define @Zr:%2:#%0#%1|||%3|||%5,%6);%7}%8&H@(%9||| @Zn:@Zo:%2:#%0a#%1,%3|||%5|||%6);%7}%8&H@(%9,%3|||
  175. #define @Zo:%2:#%0#%1|||%3|||%5,%6);%7}%8&H@(%9||| @Zn:@Zo:%2:#%0i#%1,%3|||%5|||%6);%7}%8&H@(%9,%3|||
  176. #define @Zp:%0||||||);%1}%2&H@(,%4||| @Zl:%0);%1}%2&H@(%4
  177. #define @Zy:%0||||||);%1}%2&H@(,%4||| @Zj:%0);%1}%2&H@(%4
  178. #define _YM@Cl:%0,%1);%2}%3(%9)<%4> @Zn:@Zo:@Zp:##|||%0|||%1);%2}stock%3(%9)<%4>return @Zv:@Zq:%3&H@(|||)
  179. #define _YM@Cj:%0,%1);%2}%3(%9)<%4> @Zn:@Zo:@Zy:##|||%0|||%1);%2}stock%3(%9)<%4>return @Zv:@Zq:%3&H@(|||)
  180. #define @Zl:%0);%8void:%3(%1);}%5<%6>return%4&%7; %0);}%5<%6>%7;
  181. #define @Zj:%0);%8void:%3(%1);}%5<%6>return%4&%7; %0);%3(%1);}%5<%6>%7;
  182. #define @Zv:@Zq:%9:%1&H@( %9:@Zw:@Zx:%9&H@(
  183. #define @Zw:@Zx:%0string%2&H@( G@(
  184. #define @Zx:%0&H@( H@(
  185. #define @Zq:%1&H@( H@(
  186. /*
  187. // Second "foreign" declaration.
  188. // This has been updated to check for arrays, then check if the array is a
  189. // string, instead of the old method which checked for those separately.
  190. #define @Zn:@Zo:@Zp:#%0#%1|||%3[%4]|||%5,%6);%7}%8&H@(%9||| @Zm:@Zr:#%0#%1|||%3|||%5,%6);%7}%8&H@(%9|||
  191. #define @Zm:@Zr:#%0#%1|||%2string:%3|||%5,%6);%7}%8&H@(%9||| @Zn:@Zo:@Zp:#%0s#%1,%3|||%5|||%6);%7}%8&H@(%9,%3|||
  192. #define @Zr:#%0#%1|||%3|||%5,%6);%7}%8&H@(%9||| @Zn:@Zo:@Zp:#%0a#%1,%3|||%5|||%6);%7}%8&H@(%9,%3|||
  193. #define @Zo:@Zp:#%0#%1|||%3|||%5,%6);%7}%8&H@(%9||| @Zn:@Zo:@Zp:#%0i#%1,%3|||%5|||%6);%7}%8&H@(%9,%3|||
  194. #define @Zp:%0||||||);%1}%2&H@(,%4||| %0);%1}%2&H@(%4
  195. #define _YM@CX:%0,%1);%2}%8;%3(%9)<%4> @Zn:@Zo:@Zp:##|||%0|||%1);%2}%8;stock %3(%9)<%4>return @Zv:@Zq:%3&H@(|||);
  196. #define @Zv:@Zq:%9:%1&H@( %9:@Zw:@Zx:%9&H@(
  197. #define @Zw:@Zx:%0string%2&H@( G@(
  198. #define @Zx:%0&H@( H@(
  199. #define @Zq:%1&H@( H@(
  200. */
  201. // "global" declaration.
  202. #define @Zt:@Zb:@Zh:@Zi:%0(%1|||%2:%3|||%5,%6) @Zb:@Zh:@Zi:%0(%1|||%3|||%5,%6)
  203. #define @Zb:@Zh:@Zi:%0(%1|||%3[%4]|||%5,%6) @Zt:@Zb:@Zh:@Zi:%0(%1,%3|||%5|||%6)
  204. #define @Zh:@Zi:%0(%1|||%3|||%5,%6) @Zt:@Zb:@Zh:@Zi:%0(%1,%3|||%5|||%6)
  205. #define @Zi:%0(,%1||||||) %0(%1)
  206. #define @Zk:_YM@CP:%0(,,) %0()
  207. #define _YM@CP:%0(%1,%2) @Zt:@Zb:@Zh:@Zi:%0(|||%1|||%2)
  208. //#define X@(%0string:%1) R@(%1)
  209. #define X@(@Zk:_YM@CP:%0string:%1(%2)) R@(@Zk:_YM@CP:%1(%2))
  210. // This is nearly all I needed to add to "global" for "void:". I doubt that
  211. // "foreign" will be as simple!
  212. #define @Zz:X@(@Zk:_YM@CP:%0void:%1(%2)) @Zk:_YM@CP:%1(%2)
  213. #define YSIM_NOT_CLIENT (!YSIM_HAS_MASTER || !_YSIM_IS_CLIENT)
  214. #define @global global
  215. #include "y_shortfunc"
  216. #if defined YSI_NO_MASTER
  217. #endinput
  218. #endif
  219. #define MAX_MASTERS 26
  220. forward Master_Reassert();
  221. enum _E_YCM
  222. {
  223. _E_YCM@y,
  224. _E_YCM@n,
  225. _E_YCM@m,
  226. _E_YCM@p,
  227. _E_YCM@u
  228. }
  229. #define _YCM@y (_YCM@==_E_YCM@y)
  230. #define _YCM@n (_YCM@==_E_YCM@n)
  231. #define _YCM@m (_YCM@==_E_YCM@m)
  232. #define _YCM@p (_YCM@==_E_YCM@p)
  233. #define _YCM@u (_YCM@==_E_YCM@u)
  234. static
  235. YSI_g_sMasterCount,
  236. YSI_g_sMasterData[MAX_MASTERS];
  237. /**--------------------------------------------------------------------------**\
  238. <summary>Master_GetNext</summary>
  239. <returns>
  240. Next master ID to be assigned.
  241. </returns>
  242. <remarks>
  243. -
  244. </remarks>
  245. \**--------------------------------------------------------------------------**/
  246. stock Master_GetNext()
  247. {
  248. P:3("Master_GetNext called");
  249. new
  250. masters = getproperty(8, YSIM_MASTER),
  251. i = 0;
  252. while (i != 32)
  253. {
  254. if (!(masters & (1 << i)))
  255. {
  256. return i;
  257. }
  258. ++i;
  259. }
  260. return -1;
  261. }
  262. /**--------------------------------------------------------------------------**\
  263. Hook:
  264. OnGameModeInit
  265. </returns>
  266. <remarks>
  267. Constructor. Gets the script a master ID. Now ALWAYS gets an ID, even if
  268. the master system is disabled - doing otherwise is just too complicated.
  269. </remarks>
  270. \**--------------------------------------------------------------------------**/
  271. public OnScriptInit()
  272. {
  273. P:1("Master_OGM");
  274. if (!existproperty(8, YSIM_MASTER))
  275. {
  276. setproperty(8, YSIM_MASTER, 0);
  277. }
  278. // Properties get lost between script changes so we need to force a rebuild.
  279. CallRemoteFunction("Master_Reassert", "");
  280. new
  281. masters = getproperty(8, YSIM_MASTER),
  282. i = 0;
  283. while (i != 32)
  284. {
  285. if (!(masters & (1 << i)))
  286. {
  287. _@ = i;
  288. masters |= 1 << i;
  289. break;
  290. }
  291. ++i;
  292. }
  293. if (i != 32)
  294. {
  295. setproperty(8, YSIM_MASTER, masters);
  296. }
  297. C:1(if (_@ == -1) P:E("MasterID not assigned"););
  298. // Make sure this is called before all other YSI initialisations, at least
  299. // all the ones which use the master system.
  300. #if defined YSIM_OnMasterSystemInit
  301. YSIM_OnMasterSystemInit();
  302. #endif
  303. // Just use one name...
  304. #if defined Master_OnScriptInit
  305. return Master_OnScriptInit();
  306. #else
  307. return 1;
  308. #endif
  309. }
  310. #undef OnScriptInit
  311. #define OnScriptInit Master_OnScriptInit
  312. #if defined Master_OnScriptInit
  313. forward Master_OnScriptInit();
  314. #endif
  315. #define OnMasterSystemInit YSIM_OnMasterSystemInit
  316. #if defined YSIM_OnMasterSystemInit
  317. forward YSIM_OnMasterSystemInit();
  318. #endif
  319. /**--------------------------------------------------------------------------**\
  320. Hook:
  321. OnGameModeExit
  322. </returns>
  323. <remarks>
  324. Destructor.
  325. </remarks>
  326. \**--------------------------------------------------------------------------**/
  327. public OnScriptExit()
  328. {
  329. P:1("MasterOnce_OnScriptExit called");
  330. // Loop through everything this script is master for and call the remote
  331. // function for it. EXCEPT for this script itself!
  332. new
  333. func[4];
  334. for (new i = 0; i != YSI_g_sMasterCount; ++i)
  335. {
  336. // This is slightly slower for ending and starting scripts, but uses far
  337. // less heap space, and these values are rarely used, so may as well
  338. // pack them (which is what has happened here).
  339. func[0] = YSI_g_sMasterData[i] & 0xFF;
  340. func[1] = (YSI_g_sMasterData[i] >> 8) & 0xFF;
  341. func[2] = YSI_g_sMasterData[i] >> 16;
  342. CallLocalFunction(func, "");
  343. // The properties currently clear instantly, but that may not always be
  344. // the case.
  345. }
  346. setproperty(8, YSIM_MASTER, getproperty(8, YSIM_MASTER) & ~(1 << _@));
  347. #if defined YSIM_OnScriptExit
  348. YSIM_OnScriptExit();
  349. #endif
  350. CallRemoteFunction("Master_OnScriptClose", "i", _@);
  351. return 1;
  352. }
  353. #undef OnScriptExit
  354. #define OnScriptExit YSIM_OnScriptExit
  355. #if defined YSIM_OnScriptExit
  356. forward YSIM_OnScriptExit();
  357. #endif
  358. /**--------------------------------------------------------------------------**\
  359. <summary>Master_Reassert</summary>
  360. <returns>
  361. -
  362. </returns>
  363. <remarks>
  364. Rebuilds the collection of master data whenever a script is restarted.
  365. </remarks>
  366. \**--------------------------------------------------------------------------**/
  367. public Master_Reassert()
  368. {
  369. // Make sure that the caller parameter is always -1 by default.
  370. U@(8, YSIM_CALLER, -1);
  371. if (_@ != -1)
  372. {
  373. // Read this script's master value.
  374. setproperty(8, YSIM_MASTER, getproperty(8, YSIM_MASTER) | (1 << _@));
  375. // Readd this script's owned scripts.
  376. new
  377. func[4];
  378. for (new i = 0; i != YSI_g_sMasterCount; ++i)
  379. {
  380. // This is slightly slower for ending and starting scripts, but uses far
  381. // less heap space, and these values are rarely used, so may as well
  382. // pack them (which is what has happened here).
  383. func[0] = YSI_g_sMasterData[i] & 0xFF;
  384. func[1] = (YSI_g_sMasterData[i] >> 8) & 0xFF;
  385. func[2] = YSI_g_sMasterData[i] >> 16;
  386. setproperty(9, func, _@);
  387. }
  388. }
  389. }
  390. /**--------------------------------------------------------------------------**\
  391. <summary>_Master_Get</summary>
  392. <param name="library[]">The name of the library to try become master for.</param>
  393. <returns>
  394. -
  395. </returns>
  396. <remarks>
  397. -
  398. </remarks>
  399. \**--------------------------------------------------------------------------**/
  400. stock bool:_Master_Get(library[], bool:force = false)
  401. {
  402. P:3("bool:_Master_Get called: \"%s\", %i", library, _:force);
  403. P:2("_Master_Get called");
  404. if (!force && existproperty(9, library))
  405. {
  406. new
  407. master = getproperty(9, library);
  408. P:4("_Master_Get: Prop exists: %d %d", master, _@);
  409. if (master != -1)
  410. {
  411. if (master == _@)
  412. {
  413. return true;
  414. }
  415. P:4("_Master_Get: Prop set");
  416. return false;
  417. }
  418. }
  419. P:4("_Master_Get: Get master.");
  420. setproperty(9, library, _@);
  421. P:4("_Master_Get: Set master.");
  422. // Add this library to the list. The list is designed to only deal with
  423. // two or three character master names now!
  424. if (YSI_g_sMasterCount < MAX_MASTERS)
  425. {
  426. P:4("_Master_Get: Set master string.");
  427. YSI_g_sMasterData[YSI_g_sMasterCount++] = library[0] | (library[1] << 8) | (library[2] << 16);
  428. P:4("_Master_Get: Set master string.");
  429. }
  430. P:C(else P:E("Too many master scripts"););
  431. return true;
  432. }