y_uvar.inc 14 KB

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