y_uvar.inc 13 KB

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