y_masteronce.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. \*----------------------------------------------------------------------------*/
  106. // These parts should only be defined once ever. This code is not dependent on
  107. // the current MASTER value, it's all generic.
  108. #include "y_version"
  109. #include "..\y_debug"
  110. #include "..\y_hooks"
  111. #include "y_shortfunc"
  112. #include "y_natives"
  113. #define _YSIM_COMPARE -1
  114. #define MASTER_DATA<%0> static stock Bit:YSI_g_sMasterData[_:(%0)];
  115. #define MASTER_RESET<%0> YSI_g_sMasterData[_:(%0)] = Bit:0;
  116. #define MASTER_SET<%0> YSI_g_sMasterData[_:(%0)] = Bit:(1 << Master_Caller());
  117. #define MASTER_ADD<%0> YSI_g_sMasterData[_:(%0)] |= Bit:(1 << Master_Caller());
  118. #define MASTER_REMOVE<%0> YSI_g_sMasterData[_:(%0)] &= Bit:(~(1 << Master_Caller()));
  119. #define MASTER_EMPTY<%0> if(!YSI_g_sMasterData[_:(%0)])
  120. #if defined YSI_IS_CLIENT
  121. #if NO_VALUE(YSI_IS_CLIENT)
  122. #undef YSI_IS_CLIENT
  123. #define YSI_IS_CLIENT 100
  124. #endif
  125. #endif
  126. #if defined YSI_IS_SERVER
  127. #if NO_VALUE(YSI_IS_SERVER)
  128. #undef YSI_IS_SERVER
  129. #define YSI_IS_SERVER 100
  130. #endif
  131. #endif
  132. #if defined YSI_IS_STUB
  133. #if NO_VALUE(YSI_IS_STUB)
  134. #undef YSI_IS_STUB
  135. #define YSI_IS_STUB 100
  136. #endif
  137. #endif
  138. // Define all the alternate spellings once.
  139. #define RF@cp RF@pc
  140. #define RF@pvc RF@pcv
  141. #define RF@cpv RF@pcv
  142. #define RF@cvp RF@pcv
  143. #define RF@vpc RF@pcv
  144. #define RF@vcp RF@pcv
  145. #define RF@vp RF@pv
  146. #define RF@cv RF@vc
  147. #define RF@cpt RF@pct
  148. #define RF@tcp RF@pct
  149. #define RF@tpc RF@pct
  150. #define RF@ctp RF@pct
  151. #define RF@ptc RF@pct
  152. #define RF@tp RF@pt
  153. #define RF@tc RF@ct
  154. #define mhook master_hook
  155. #define OnScriptClose Master_OnScriptClose
  156. forward Master_OnScriptClose(script);
  157. // These are the macros used by y_master for the recursive function definition
  158. // structure, but they are not used unless a cloud-based "foreign" or "global"
  159. // are used.
  160. #define W@(@Zu:#%0:%1#,%2);return%3(%8) W@(@Zu:#%1,%2);return%0:%3(%0:%8)
  161. #define Z@(%0string:i) S@(i)
  162. #define V@(%0string:%1) F@()
  163. // Second "foreign" declaration.
  164. //#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@();
  165. #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@()
  166. //#define @Zu:#%0,_YM@CX:,,) @Zl:#%0,#)
  167. // Second "foreign" declaration.
  168. // This has been updated to check for arrays, then check if the array is a
  169. // string, instead of the old method which checked for those separately.
  170. #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|||
  171. #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|||
  172. #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|||
  173. #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|||
  174. #define @Zp:%0||||||);%1}%2&H@(,%4||| @Zl:%0);%1}%2&H@(%4
  175. #define @Zy:%0||||||);%1}%2&H@(,%4||| @Zj:%0);%1}%2&H@(%4
  176. #define _YM@Cl:%0,%1);%2}%3(%9)<%4> @Zn:@Zo:@Zp:##|||%0|||%1);%2}stock%3(%9)<%4>return @Zv:@Zq:%3&H@(|||)
  177. #define _YM@Cj:%0,%1);%2}%3(%9)<%4> @Zn:@Zo:@Zy:##|||%0|||%1);%2}stock%3(%9)<%4>return @Zv:@Zq:%3&H@(|||)
  178. #define @Zl:%0);%8void:%3(%1);}%5<%6>return%4&%7; %0);}%5<%6>%7;
  179. #define @Zj:%0);%8void:%3(%1);}%5<%6>return%4&%7; %0);%3(%1);}%5<%6>%7;
  180. #define @Zv:@Zq:%9:%1&H@( %9:@Zw:@Zx:%9&H@(
  181. #define @Zw:@Zx:%0string%2&H@( G@(
  182. #define @Zx:%0&H@( H@(
  183. #define @Zq:%1&H@( H@(
  184. /*
  185. // Second "foreign" declaration.
  186. // This has been updated to check for arrays, then check if the array is a
  187. // string, instead of the old method which checked for those separately.
  188. #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|||
  189. #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|||
  190. #define @Zr:#%0#%1|||%3|||%5,%6);%7}%8&H@(%9||| @Zn:@Zo:@Zp:#%0a#%1,%3|||%5|||%6);%7}%8&H@(%9,%3|||
  191. #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|||
  192. #define @Zp:%0||||||);%1}%2&H@(,%4||| %0);%1}%2&H@(%4
  193. #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@(|||);
  194. #define @Zv:@Zq:%9:%1&H@( %9:@Zw:@Zx:%9&H@(
  195. #define @Zw:@Zx:%0string%2&H@( G@(
  196. #define @Zx:%0&H@( H@(
  197. #define @Zq:%1&H@( H@(
  198. */
  199. // "global" declaration.
  200. #define @Zt:@Zb:@Zh:@Zi:%0(%1|||%2:%3|||%5,%6) @Zb:@Zh:@Zi:%0(%1|||%3|||%5,%6)
  201. #define @Zb:@Zh:@Zi:%0(%1|||%3[%4]|||%5,%6) @Zt:@Zb:@Zh:@Zi:%0(%1,%3|||%5|||%6)
  202. #define @Zh:@Zi:%0(%1|||%3|||%5,%6) @Zt:@Zb:@Zh:@Zi:%0(%1,%3|||%5|||%6)
  203. #define @Zi:%0(,%1||||||) %0(%1)
  204. #define @Zk:_YM@CP:%0(,,) %0()
  205. #define _YM@CP:%0(%1,%2) @Zt:@Zb:@Zh:@Zi:%0(|||%1|||%2)
  206. //#define X@(%0string:%1) R@(%1)
  207. #define X@(@Zk:_YM@CP:%0string:%1(%2)) R@(@Zk:_YM@CP:%1(%2))
  208. // This is nearly all I needed to add to "global" for "void:". I doubt that
  209. // "foreign" will be as simple!
  210. #define @Zz:X@(@Zk:_YM@CP:%0void:%1(%2)) @Zk:_YM@CP:%1(%2)
  211. #define YSIM_NOT_CLIENT (!YSIM_HAS_MASTER || !_YSIM_IS_CLIENT)
  212. #define @global global
  213. #include "y_shortfunc"
  214. #if defined YSI_NO_MASTER
  215. #endinput
  216. #endif
  217. #define MAX_MASTERS 26
  218. forward Master_Reassert();
  219. enum _E_YCM
  220. {
  221. _E_YCM@y,
  222. _E_YCM@n,
  223. _E_YCM@m,
  224. _E_YCM@p,
  225. _E_YCM@u
  226. }
  227. #define _YCM@y (_YCM@==_E_YCM@y)
  228. #define _YCM@n (_YCM@==_E_YCM@n)
  229. #define _YCM@m (_YCM@==_E_YCM@m)
  230. #define _YCM@p (_YCM@==_E_YCM@p)
  231. #define _YCM@u (_YCM@==_E_YCM@u)
  232. static
  233. YSI_g_sMasterCount,
  234. YSI_g_sMasterData[MAX_MASTERS];
  235. /*----------------------------------------------------------------------------*\
  236. Function:
  237. Master_GetNext
  238. Params:
  239. -
  240. Return:
  241. Next master ID to be assigned.
  242. Notes:
  243. -
  244. \*----------------------------------------------------------------------------*/
  245. stock Master_GetNext()
  246. {
  247. P:3("Master_GetNext called");
  248. new
  249. masters = getproperty(8, YSIM_MASTER),
  250. i = 0;
  251. while (i != 32)
  252. {
  253. if (!(masters & (1 << i)))
  254. {
  255. return i;
  256. }
  257. ++i;
  258. }
  259. return -1;
  260. }
  261. /*----------------------------------------------------------------------------*\
  262. Hook:
  263. OnGameModeInit
  264. Notes:
  265. Constructor. Gets the script a master ID. Now ALWAYS gets an ID, even if
  266. the master system is disabled - doing otherwise is just too complicated.
  267. \*----------------------------------------------------------------------------*/
  268. public OnScriptInit()
  269. {
  270. P:1("Master_OGM");
  271. if (!existproperty(8, YSIM_MASTER))
  272. {
  273. setproperty(8, YSIM_MASTER, 0);
  274. }
  275. // Properties get lost between script changes so we need to force a rebuild.
  276. CallRemoteFunction("Master_Reassert", "");
  277. new
  278. masters = getproperty(8, YSIM_MASTER),
  279. i = 0;
  280. while (i != 32)
  281. {
  282. if (!(masters & (1 << i)))
  283. {
  284. _@ = i;
  285. masters |= 1 << i;
  286. break;
  287. }
  288. ++i;
  289. }
  290. if (i != 32)
  291. {
  292. setproperty(8, YSIM_MASTER, masters);
  293. }
  294. C:1(if (_@ == -1) P:E("MasterID not assigned"););
  295. // Make sure this is called before all other YSI initialisations, at least
  296. // all the ones which use the master system.
  297. YSIM_OnMasterSystemInit();
  298. // Just use one name...
  299. return Master_OnScriptInit();
  300. }
  301. #undef OnScriptInit
  302. #define OnScriptInit(%0) Master_OnScriptInit(%0) <_ALS_:hooked>
  303. forward Master_OnScriptInit();public Master_OnScriptInit()<_ALS_:unhooked>{return 1;}public Master_OnScriptInit()<>{return 1;}
  304. #define OnMasterSystemInit(%0) YSIM_OnMasterSystemInit(%0) <_ALS_:hooked>
  305. forward YSIM_OnMasterSystemInit();public YSIM_OnMasterSystemInit()<_ALS_:unhooked>{return 1;}public YSIM_OnMasterSystemInit()<>{return 1;}
  306. /*----------------------------------------------------------------------------*\
  307. Hook:
  308. OnGameModeExit
  309. Notes:
  310. Destructor.
  311. \*----------------------------------------------------------------------------*/
  312. public OnScriptExit()
  313. {
  314. P:1("MasterOnce_OnScriptExit called");
  315. // Loop through everything this script is master for and call the remote
  316. // function for it. EXCEPT for this script itself!
  317. new
  318. func[4];
  319. for (new i = 0; i != YSI_g_sMasterCount; ++i)
  320. {
  321. // This is slightly slower for ending and starting scripts, but uses far
  322. // less heap space, and these values are rarely used, so may as well
  323. // pack them (which is what has happened here).
  324. func[0] = YSI_g_sMasterData[i] & 0xFF;
  325. func[1] = (YSI_g_sMasterData[i] >> 8) & 0xFF;
  326. func[2] = YSI_g_sMasterData[i] >> 16;
  327. CallLocalFunction(func, "");
  328. // The properties currently clear instantly, but that may not always be
  329. // the case.
  330. }
  331. setproperty(8, YSIM_MASTER, getproperty(8, YSIM_MASTER) & ~(1 << _@));
  332. YSIM_OnScriptExit();
  333. CallRemoteFunction("Master_OnScriptClose", "i", _@);
  334. return 1;
  335. }
  336. #undef OnScriptExit
  337. #define OnScriptExit(%0) YSIM_OnScriptExit(%0) <_ALS_:hooked>
  338. forward YSIM_OnScriptExit();public YSIM_OnScriptExit()<_ALS_:unhooked>{return 1;}public YSIM_OnScriptExit()<>{return 1;}
  339. /*----------------------------------------------------------------------------*\
  340. Function:
  341. Master_Reassert
  342. Params:
  343. -
  344. Return:
  345. -
  346. Notes:
  347. Rebuilds the collection of master data whenever a script is restarted.
  348. \*----------------------------------------------------------------------------*/
  349. public Master_Reassert()
  350. {
  351. // Make sure that the caller parameter is always -1 by default.
  352. U@(8, YSIM_CALLER, -1);
  353. if (_@ != -1)
  354. {
  355. // Read this script's master value.
  356. setproperty(8, YSIM_MASTER, getproperty(8, YSIM_MASTER) | (1 << _@));
  357. // Readd this script's owned scripts.
  358. new
  359. func[4];
  360. for (new i = 0; i != YSI_g_sMasterCount; ++i)
  361. {
  362. // This is slightly slower for ending and starting scripts, but uses far
  363. // less heap space, and these values are rarely used, so may as well
  364. // pack them (which is what has happened here).
  365. func[0] = YSI_g_sMasterData[i] & 0xFF;
  366. func[1] = (YSI_g_sMasterData[i] >> 8) & 0xFF;
  367. func[2] = YSI_g_sMasterData[i] >> 16;
  368. setproperty(9, func, _@);
  369. }
  370. }
  371. }
  372. /*----------------------------------------------------------------------------*\
  373. Function:
  374. _Master_Get
  375. Params:
  376. library[] - The name of the library to try become master for.
  377. Return:
  378. -
  379. Notes:
  380. -
  381. \*----------------------------------------------------------------------------*/
  382. stock bool:_Master_Get(library[], bool:force = false)
  383. {
  384. P:3("bool:_Master_Get called: \"%s\", %i", library, _:force);
  385. P:2("_Master_Get called");
  386. if (!force && existproperty(9, library))
  387. {
  388. new
  389. master = getproperty(9, library);
  390. P:4("_Master_Get: Prop exists: %d %d", master, _@);
  391. if (master != -1)
  392. {
  393. if (master == _@)
  394. {
  395. return true;
  396. }
  397. P:4("_Master_Get: Prop set");
  398. return false;
  399. }
  400. }
  401. P:4("_Master_Get: Get master.");
  402. setproperty(9, library, _@);
  403. P:4("_Master_Get: Set master.");
  404. // Add this library to the list. The list is designed to only deal with
  405. // two or three character master names now!
  406. if (YSI_g_sMasterCount < MAX_MASTERS)
  407. {
  408. P:4("_Master_Get: Set master string.");
  409. YSI_g_sMasterData[YSI_g_sMasterCount++] = library[0] | (library[1] << 8) | (library[2] << 16);
  410. P:4("_Master_Get: Set master string.");
  411. }
  412. P:C(else P:E("Too many master scripts"););
  413. return true;
  414. }