1
0

y_uvar.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /**--------------------------------------------------------------------------**\
  2. =================================
  3. y_uvar - Automatic data saving.
  4. =================================
  5. Description:
  6. Declares data to be automatically saved and loaded on a per-player basis.
  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 YSI utils include.
  18. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  19. Portions created by the Initial Developer are Copyright (C) 2011
  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. ZeeX - Very productive conversations.
  26. koolk - IsPlayerinAreaEx code.
  27. TheAlpha - Danish translation.
  28. breadfish - German translation.
  29. Fireburn - Dutch translation.
  30. yom - French translation.
  31. 50p - Polish translation.
  32. Zamaroht - Spanish translation.
  33. Los - Portuguese 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. Optional plugins:
  43. Gamer_Z - GPS.
  44. Incognito - Streamer.
  45. Me - sscanf2, fixes2, Whirlpool.
  46. Version:
  47. 0.1.3
  48. Changelog:
  49. 25/02/12:
  50. First version.
  51. Functions:
  52. Stock:
  53. -
  54. Inline:
  55. -
  56. Variables:
  57. Global:
  58. -
  59. \**--------------------------------------------------------------------------**/
  60. // y_uvars
  61. #include "internal\y_version"
  62. #include "y_amx"
  63. #include "y_debug"
  64. #include "y_utils"
  65. #include "y_users"
  66. #include "y_ini"
  67. #include "y_hooks"
  68. #include "internal\y_stripnumbers"
  69. // Third "uvar" version.
  70. #define _YU@LE@E%0>
  71. #define _YU@LT@E%0> ;
  72. // Needs two levels of indirection to strip the excess commas (,%0,%1).
  73. #define _YU@LO(,%0,%1,%2) %0@yA_();public %0@yA_(){N@(#....#%0,_:%1,_:%2,STRIP_NUMBERS:%0[0]|||%2:0|||);}
  74. #define _YU@LE%0[%1][%3]%2> _YU@LO(%0,%1,%3) _YU@LE%2>
  75. // Recursive local default string definition.
  76. #define _YU@LJ(,%0,%1,%2) %0[%1][%2]
  77. #define _YU@LT%0[%1][%3]%2> ,_YU@LJ(%0,%1,%3)_YU@LT%2>
  78. #define _YU@LA%0[%1][%3]%2> _YU@LJ(%0,%1,%3)_YU@LT%2>
  79. // Entry point for the loaders. The structure of stored pvar data is:
  80. //
  81. // [0] - Pointer to next pvar in list (-1 for end).
  82. // [1] - Pointer to data.
  83. // [2] - Number of players.
  84. // [3] - Size of enum.
  85. // [4] - Start of name.
  86. //
  87. // It is VERY important to note that using "%0[0][0]" when calling "N@" instead
  88. // of "%0" gives a DIFFERENT address - we get the address of the first data
  89. // element in the array, not the address of the start of the array pointer table
  90. // which is used to index multi-dimensional arrays when the size is not known
  91. // (which in this case it is). This makes calculating offsets later possible.
  92. #define uvar%0[%8][%1]%2; stock _YU@LA,%0[%8][%1]%2@E>_YU@LE,%0[%8][%1]%2@E>
  93. //%0@yA_();public%0@yA_()N@(_:%0,#....#%0 _YA@LT %1,@E|||);
  94. // This is a structure defining the data stored on the enum structure.
  95. /*enum E_USERS_FAKE_DATA
  96. {
  97. E_USERS_FAKE_DATA_NEXT,
  98. E_USERS_FAKE_DATA_DATA,
  99. E_USERS_FAKE_DATA_LEN,
  100. E_USERS_FAKE_DATA_STR[2]
  101. }*/
  102. static stock
  103. YSI_g_sFirstUVarData = -1,
  104. // These three variables are used to speed up data loading through caching.
  105. YSI_g_sLastName[32] = "\1\0",
  106. YSI_g_sLastAddr,
  107. YSI_g_sLastPlayers,
  108. YSI_g_sLastSize;
  109. forward _y_uvar_include_@();
  110. public _y_uvar_include_@()
  111. {
  112. memset("", 0, 0);
  113. Player_WriteArray("", "", 0);
  114. }
  115. static stock Uvar_FindData(const name[], data[])
  116. {
  117. // This function gets passed an empty string so that we can use "data" as a
  118. // string, while secretly changing the pointer in AMX code.
  119. new
  120. p = YSI_g_sFirstUVarData;
  121. while (p != -1)
  122. {
  123. // Modify our data pointer.
  124. #emit LOAD.S.pri p
  125. #emit STOR.S.pri data
  126. if (!strcmp(data[4], name))
  127. {
  128. strcpy(YSI_g_sLastName, name);
  129. YSI_g_sLastSize = data[3];
  130. YSI_g_sLastPlayers = data[2];
  131. YSI_g_sLastAddr = data[1];
  132. //printf("found %s, %d, %d, %d", YSI_g_sLastName, YSI_g_sLastSize, YSI_g_sLastPlayers, YSI_g_sLastAddr);
  133. return;
  134. }
  135. p = data[0];
  136. }
  137. YSI_g_sLastAddr = -1;
  138. }
  139. forward OnUserData[y_uvar](playerid, name[], value[]);
  140. public OnUserData[y_uvar](playerid, name[], value[])
  141. {
  142. // See what the name of the loaded data was.
  143. new
  144. pos = strfind(name, "-");
  145. if (pos == -1)
  146. {
  147. if (strcmp(name, YSI_g_sLastName))
  148. {
  149. // Find the data.
  150. Uvar_FindData(name, "");
  151. }
  152. if (YSI_g_sLastAddr == -1)
  153. {
  154. return;
  155. }
  156. // Check that the data is the right size.
  157. P:C(if (strval(value) != YSI_g_sLastSize) P:E("uvar data changed in %s", YSI_g_sLastName););
  158. }
  159. else
  160. {
  161. // Get the position in the array of this data.
  162. //printf("call pos 0");
  163. name[pos] = '\0';
  164. pos = strval(name[pos + 1]) * ((MAX_INI_ENTRY_TEXT - 1) / 16 * 3);
  165. if (strcmp(name[2], YSI_g_sLastName, false))
  166. {
  167. // Find the data.
  168. Uvar_FindData(name[2], "");
  169. }
  170. if (YSI_g_sLastAddr == -1)
  171. {
  172. return;
  173. }
  174. // Get the offset in the array for this player.
  175. if (playerid < YSI_g_sLastPlayers)
  176. {
  177. new
  178. len = strlen(value),
  179. idx;
  180. pos += YSI_g_sLastSize * playerid;
  181. // Save this pointer to an array variable for simplicity.
  182. #emit LOAD.pri YSI_g_sLastAddr
  183. #emit STOR.S.pri name
  184. // "pos" holds the offset of this data. "value" always holds a
  185. // whole number of cells worth of data.
  186. while (idx + 16 <= len)
  187. {
  188. // Do the large chunks.
  189. name[pos++] = ((value[idx + 0] - '>') << 26)
  190. | ((value[idx + 1] - '>') << 20)
  191. | ((value[idx + 2] - '>') << 14)
  192. | ((value[idx + 3] - '>') << 8)
  193. | ((value[idx + 4] - '>') << 2)
  194. | ((value[idx + 5] - '>') >> 4);
  195. // Second cell.
  196. name[pos++] = ((value[idx + 5] - '>') << 28)
  197. | ((value[idx + 6] - '>') << 22)
  198. | ((value[idx + 7] - '>') << 16)
  199. | ((value[idx + 8] - '>') << 10)
  200. | ((value[idx + 9] - '>') << 4)
  201. | ((value[idx + 10] - '>') >> 2);
  202. // Third cell.
  203. name[pos++] = ((value[idx + 10] - '>') << 30)
  204. | ((value[idx + 11] - '>') << 24)
  205. | ((value[idx + 12] - '>') << 18)
  206. | ((value[idx + 13] - '>') << 12)
  207. | ((value[idx + 14] - '>') << 6)
  208. | ((value[idx + 15] - '>') >> 0);
  209. // 16 characters are used to encode 3 cells (12 bytes) by only
  210. // saving 6 bits per character to ensure that they are always
  211. // valid characters. 7 bits may be easier, but would mean the
  212. // encoding fit less well to small numbers of cells.
  213. idx += 16;
  214. }
  215. if (idx + 6 <= len)
  216. {
  217. // Save any few extra bytes.
  218. name[pos++] = ((value[idx + 0] - '>') << 26)
  219. | ((value[idx + 1] - '>') << 20)
  220. | ((value[idx + 2] - '>') << 14)
  221. | ((value[idx + 3] - '>') << 8)
  222. | ((value[idx + 4] - '>') << 2)
  223. | ((value[idx + 5] - '>') >> 4);
  224. if (idx + 11 <= len)
  225. {
  226. name[pos++] = ((value[idx + 5] - '>') << 28)
  227. | ((value[idx + 6] - '>') << 22)
  228. | ((value[idx + 7] - '>') << 16)
  229. | ((value[idx + 8] - '>') << 10)
  230. | ((value[idx + 9] - '>') << 4)
  231. | ((value[idx + 10] - '>') >> 2);
  232. }
  233. }
  234. }
  235. }
  236. }
  237. /**--------------------------------------------------------------------------**\
  238. <summary>N@</summary>
  239. <param name="val[][]">Handle to the PAWN data array.</param>
  240. <param name="volatile vardata[]">Handle to the memory location in which to store info.</param>
  241. <param name="{K@, L@, M@, N@, _}:...">Array slot size information.</param>
  242. <returns>
  243. -
  244. </returns>
  245. <remarks>
  246. This function modifies "vardata" well beyond its original limits to contain
  247. information on the structure of the enum used to define "val". This code
  248. uses the name and size information passed in the additional parameters as
  249. strings, and makes assumptions about how the compiler lays out memory to
  250. combine all the passed strings in to one big string in what could be ROM,
  251. but in SA:MP isn't. This takes a human readable(ish) description of the
  252. array elements and converts it in to a much simpler to read format for the
  253. computer to use later when loading and storing data.
  254. The description above is no longer the case. This code now just saves the
  255. size of the data, the number of players in the array, the address of the
  256. data, a pointer to another data set and the name of this data. This is by
  257. far much simpler than the old version.
  258. </remarks>
  259. \**--------------------------------------------------------------------------**/
  260. stock N@(volatile const vardata[], playerCount, dataSize, &pointer)
  261. {
  262. new
  263. sAddr;
  264. // Store the basic data, including linked-list pointers and a pointer to the
  265. // location at which the data is stored.
  266. #emit LOAD.S.pri vardata
  267. #emit STOR.S.pri sAddr
  268. printf("", YSI_g_sFirstUVarData);
  269. #emit LOAD.pri YSI_g_sFirstUVarData
  270. #emit SREF.S.pri sAddr
  271. YSI_g_sFirstUVarData = sAddr;
  272. sAddr += 4;
  273. #emit LOAD.S.pri pointer
  274. #emit SREF.S.pri sAddr
  275. sAddr += 4;
  276. #emit LOAD.S.pri playerCount
  277. #emit SREF.S.pri sAddr
  278. sAddr += 4;
  279. #emit LOAD.S.pri dataSize
  280. #emit SREF.S.pri sAddr
  281. P:5("N@: %d %d %d %d %s", vardata[0], vardata[1], vardata[2], vardata[3], vardata[4]);
  282. P:5("N@: %d", YSI_g_sFirstUVarData);
  283. }
  284. hook OnScriptInit()
  285. {
  286. // List them all.
  287. YSI_g_sFirstUVarData = -1;
  288. // Call all @yA_ functions to get all required data.
  289. new
  290. idx,
  291. buffer;
  292. while ((idx = AMX_GetPublicPointerSuffix(idx, buffer, _A<@yA_>)))
  293. {
  294. #emit PUSH.C 0
  295. #emit LCTRL 6
  296. #emit ADD.C 28
  297. #emit PUSH.pri
  298. #emit LOAD.S.pri buffer
  299. #emit SCTRL 6
  300. }
  301. }
  302. public OnPlayerLogout(playerid, uid)
  303. {
  304. // Loop through all the player data items and write them to a file.
  305. //static const
  306. // sc_cellsPerWrite =
  307. #if defined Uvar_OnPlayerLogout
  308. Uvar_OnPlayerLogout(playerid, uid);
  309. #endif
  310. Player_SetTag("y_uvar");
  311. new
  312. p = YSI_g_sFirstUVarData,
  313. temp;
  314. while (p != -1)
  315. {
  316. // DO NOT CHANGE THE CODE BELOW HERE!!!
  317. // Call a function sort of. This allows us to push an arbitrary address
  318. // as an array to a function.
  319. #emit LOAD.S.pri p
  320. // Get the max players.
  321. #emit ADD.C 8
  322. #emit STOR.S.pri temp
  323. #emit LREF.S.pri temp
  324. #emit STOR.S.pri temp
  325. if (playerid < temp)
  326. {
  327. // Get the data size.
  328. #emit LOAD.S.pri p
  329. #emit ADD.C 12
  330. #emit STOR.S.pri temp
  331. #emit LREF.S.pri temp
  332. #emit PUSH.pri
  333. // Get the data offset.
  334. #emit LOAD.S.alt playerid
  335. #emit SMUL
  336. #emit SMUL.C 4
  337. #emit MOVE.alt
  338. // Get the data pointer.
  339. #emit LOAD.S.pri p
  340. #emit ADD.C 4
  341. #emit STOR.S.pri temp
  342. #emit LREF.S.pri temp
  343. #emit ADD
  344. #emit PUSH.pri
  345. // Get the function name.
  346. #emit LOAD.S.pri p
  347. #emit ADD.C 16
  348. #emit PUSH.pri
  349. // Save the next pointer.
  350. #emit LREF.S.pri p
  351. #emit STOR.S.pri p
  352. // Now push the size of data put on the stack.
  353. #emit PUSH.C 12
  354. // Now get the return address and push it.
  355. #emit LCTRL 6
  356. #emit ADD.C 28
  357. #emit PUSH.pri
  358. // Call "Player_WriteArray" directly.
  359. #emit CONST.pri Player_WriteArray
  360. #emit SCTRL 6
  361. // DO NOT CHANGE THE CODE ABOVE HERE!!!
  362. }
  363. }
  364. }
  365. #if defined _ALS_OnPlayerLogout
  366. #undef OnPlayerLogout
  367. #else
  368. #define _ALS_OnPlayerLogout
  369. #endif
  370. #if defined Uvar_OnPlayerLogout
  371. forward Uvar_OnPlayerLogout(playerid, uid);
  372. #endif
  373. #define OnPlayerLogout(%0) Uvar_OnPlayerLogout(%0)
  374. hook OnPlayerConnect(playerid)
  375. {
  376. P:1("hook Users_OnPlayerConnect called: %i", playerid);
  377. new
  378. p = YSI_g_sFirstUVarData,
  379. temp;
  380. while (p != -1)
  381. {
  382. // DO NOT CHANGE THE CODE BELOW HERE!!!
  383. // Call a function sort of. This allows us to push an arbitrary address
  384. // as an array to a function.
  385. #emit LOAD.S.pri p
  386. // Get the max players.
  387. #emit ADD.C 8
  388. #emit STOR.S.pri temp
  389. #emit LREF.S.pri temp
  390. #emit STOR.S.pri temp
  391. if (playerid < temp)
  392. {
  393. // Get the data enum size.
  394. //#emit PUSH.C 8
  395. #emit PUSH.C 0
  396. #emit LOAD.S.pri p
  397. #emit ADD.C 12
  398. #emit STOR.S.pri temp
  399. #emit LREF.S.pri temp
  400. #emit PUSH.pri
  401. // Get the data offset.
  402. #emit LOAD.S.alt playerid
  403. #emit SMUL
  404. #emit SMUL.C 4
  405. #emit MOVE.alt
  406. // Get the data pointer.
  407. #emit LOAD.S.pri p
  408. #emit ADD.C 4
  409. #emit STOR.S.pri temp
  410. #emit LREF.S.pri temp
  411. #emit ADD
  412. #emit PUSH.pri
  413. // Save the next pointer.
  414. #emit LREF.S.pri p
  415. #emit STOR.S.pri p
  416. // Now push the size of data put on the stack.
  417. #emit PUSH.C 12
  418. // Now get the return address and push it.
  419. #emit LCTRL 6
  420. #emit ADD.C 28
  421. #emit PUSH.pri
  422. // Call "memset" directly.
  423. #emit CONST.pri memset
  424. #emit SCTRL 6
  425. // DO NOT CHANGE THE CODE ABOVE HERE!!!
  426. }
  427. }
  428. }