y_svar.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*----------------------------------------------------------------------------*\
  2. =================================
  3. y_svar - Automatic data saving.
  4. =================================
  5. Description:
  6. Declares data to be automatically saved and loaded for the whole server.
  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. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  34. for me to strive to better.
  35. Pixels^ - Running XScripters where the idea was born.
  36. Matite - Pestering me to release it and using it.
  37. Very special thanks to:
  38. Thiadmer - PAWN, whose limits continue to amaze me!
  39. Kye/Kalcor - SA:MP.
  40. SA:MP Team past, present and future - SA:MP.
  41. Version:
  42. 0.1
  43. Changelog:
  44. 25/02/12:
  45. First version.
  46. Functions:
  47. Stock:
  48. -
  49. Inline:
  50. -
  51. Variables:
  52. Global:
  53. -
  54. \*----------------------------------------------------------------------------*/
  55. // y_uvars
  56. #if !defined MODE_NAME
  57. #error Please define "MODE_NAME" before including y_svar.
  58. #endif
  59. #include "internal\y_version"
  60. #include "y_amx"
  61. #include "y_debug"
  62. #include "y_utils"
  63. #include "y_ini"
  64. #include "y_hooks"
  65. #include "internal\y_stripnumbers"
  66. // Third "uvar" version.
  67. #define _YS@LE@E%0>
  68. #define _YS@LT@E%0> ;
  69. // Needs two levels of indirection to strip the excess commas (,%0,%1).
  70. #define _YS@LO(,%0,%2) %0@yS_();public %0@yS_(){M@(#...#%0,_:%2,STRIP_NUMBERS:%0|||%2:0|||);}
  71. #define _YS@LE%0[%3]%2> _YS@LO(%0,%3) _YS@LE%2>
  72. // Recursive local default string definition.
  73. #define _YS@LJ(,%0,%2) %0[%2]
  74. #define _YS@LT%0[%3]%2> ,_YS@LJ(%0,%3)_YS@LT%2>
  75. #define _YS@LA%0[%3]%2> _YS@LJ(%0,%3)_YS@LT%2>
  76. // Entry point for the loaders. The structure of stored pvar data is:
  77. //
  78. // [0] - Pointer to next pvar in list (-1 for end).
  79. // [1] - Pointer to data.
  80. // [2] - Size of enum.
  81. // [3] - Start of name.
  82. //
  83. #define svar%0[%1]%2; stock _YS@LA,%0[%1]%2@E>_YS@LE,%0[%1]%2@E>
  84. //%0@yS_();public%0@yS_()M@(_:%0,#....#%0 _YA@LT %1,@E|||);
  85. // This is a structure defining the data stored on the enum structure.
  86. /*enum E_USERS_FAKE_DATA
  87. {
  88. E_USERS_FAKE_DATA_NEXT,
  89. E_USERS_FAKE_DATA_DATA,
  90. E_USERS_FAKE_DATA_LEN,
  91. E_USERS_FAKE_DATA_STR[2]
  92. }*/
  93. static stock
  94. YSI_g_sFirstUVarData = -1,
  95. // These three variables are used to speed up data loading through caching.
  96. YSI_g_sLastName[32] = "\1\0",
  97. YSI_g_sLastAddr,
  98. YSI_g_sLastSize;
  99. forward _y_svar_include_@();
  100. public _y_svar_include_@()
  101. {
  102. memset("", 0, 0, 0);
  103. INI_WriteArray(INI_NO_FILE, "", "", 0);
  104. }
  105. static stock Svar_FindData(const name[], data[])
  106. {
  107. // This function gets passed an empty string so that we can use "data" as a
  108. // string, while secretly changing the pointer in AMX code.
  109. new
  110. p = YSI_g_sFirstUVarData;
  111. while (p != -1)
  112. {
  113. // Modify our data pointer.
  114. #emit LOAD.S.pri p
  115. #emit STOR.S.pri data
  116. if (!strcmp(data[3], name))
  117. {
  118. strcpy(YSI_g_sLastName, name);
  119. YSI_g_sLastSize = data[2];
  120. YSI_g_sLastAddr = data[1];
  121. //printf("found %s, %d, %d, %d", YSI_g_sLastName, YSI_g_sLastSize, YSI_g_sLastPlayers, YSI_g_sLastAddr);
  122. return;
  123. }
  124. p = data[0];
  125. }
  126. YSI_g_sLastAddr = -1;
  127. }
  128. forward _Config_Load_y_svar(name[], value[]);
  129. public _Config_Load_y_svar(name[], value[])
  130. {
  131. P:1("_Config_Load_y_svar called: %s %s", name, value);
  132. // See what the name of the loaded data was.
  133. new
  134. pos = strfind(name, "-");
  135. if (pos == -1)
  136. {
  137. if (strcmp(name, YSI_g_sLastName))
  138. {
  139. // Find the data.
  140. Svar_FindData(name, "");
  141. }
  142. if (YSI_g_sLastAddr == -1)
  143. {
  144. return;
  145. }
  146. // Check that the data is the right size.
  147. P:C(if (strval(value) != YSI_g_sLastSize) P:E("uvar data changed in %s", YSI_g_sLastName););
  148. }
  149. else
  150. {
  151. // Get the position in the array of this data.
  152. name[pos] = '\0';
  153. pos = strval(name[pos + 1]) * ((MAX_INI_ENTRY_TEXT - 1) / 16 * 3);
  154. if (strcmp(name[2], YSI_g_sLastName, false))
  155. {
  156. // Find the data.
  157. Svar_FindData(name[2], "");
  158. }
  159. if (YSI_g_sLastAddr == -1)
  160. {
  161. return;
  162. }
  163. // This must come BEFORE the "#emit" due to bugs with braces.
  164. new
  165. len = strlen(value),
  166. idx;
  167. // Save this pointer to an array variable for simplicity.
  168. #emit LOAD.pri YSI_g_sLastAddr
  169. #emit STOR.S.pri name
  170. // "pos" holds the offset of this data. "value" always holds a
  171. // whole number of cells worth of data.
  172. while (idx + 16 <= len)
  173. {
  174. // Do the large chunks.
  175. name[pos++] = ((value[idx + 0] - '>') << 26)
  176. | ((value[idx + 1] - '>') << 20)
  177. | ((value[idx + 2] - '>') << 14)
  178. | ((value[idx + 3] - '>') << 8)
  179. | ((value[idx + 4] - '>') << 2)
  180. | ((value[idx + 5] - '>') >> 4);
  181. // Second cell.
  182. name[pos++] = ((value[idx + 5] - '>') << 28)
  183. | ((value[idx + 6] - '>') << 22)
  184. | ((value[idx + 7] - '>') << 16)
  185. | ((value[idx + 8] - '>') << 10)
  186. | ((value[idx + 9] - '>') << 4)
  187. | ((value[idx + 10] - '>') >> 2);
  188. // Third cell.
  189. name[pos++] = ((value[idx + 10] - '>') << 30)
  190. | ((value[idx + 11] - '>') << 24)
  191. | ((value[idx + 12] - '>') << 18)
  192. | ((value[idx + 13] - '>') << 12)
  193. | ((value[idx + 14] - '>') << 6)
  194. | ((value[idx + 15] - '>') >> 0);
  195. // 16 characters are used to encode 3 cells (12 bytes) by only
  196. // saving 6 bits per character to ensure that they are always
  197. // valid characters. 7 bits may be easier, but would mean the
  198. // encoding fit less well to small numbers of cells.
  199. idx += 16;
  200. }
  201. if (idx + 6 <= len)
  202. {
  203. // Save any few extra bytes.
  204. name[pos++] = ((value[idx + 0] - '>') << 26)
  205. | ((value[idx + 1] - '>') << 20)
  206. | ((value[idx + 2] - '>') << 14)
  207. | ((value[idx + 3] - '>') << 8)
  208. | ((value[idx + 4] - '>') << 2)
  209. | ((value[idx + 5] - '>') >> 4);
  210. if (idx + 11 <= len)
  211. {
  212. name[pos++] = ((value[idx + 5] - '>') << 28)
  213. | ((value[idx + 6] - '>') << 22)
  214. | ((value[idx + 7] - '>') << 16)
  215. | ((value[idx + 8] - '>') << 10)
  216. | ((value[idx + 9] - '>') << 4)
  217. | ((value[idx + 10] - '>') >> 2);
  218. }
  219. }
  220. }
  221. }
  222. /*----------------------------------------------------------------------------*\
  223. Function:
  224. M@
  225. Params:
  226. val[][] - Handle to the PAWN data array.
  227. volatile vardata[] - Handle to the memory location in which to store info.
  228. {K@, L@, M@, N@, _}:... - Array slot size information.
  229. Return:
  230. -
  231. Notes:
  232. This function modifies "vardata" well beyond its original limits to contain
  233. information on the structure of the enum used to define "val". This code
  234. uses the name and size information passed in the additional parameters as
  235. strings, and makes assumptions about how the compiler lays out memory to
  236. combine all the passed strings in to one big string in what could be ROM,
  237. but in SA:MP isn't. This takes a human readable(ish) description of the
  238. array elements and converts it in to a much simpler to read format for the
  239. computer to use later when loading and storing data.
  240. \*----------------------------------------------------------------------------*/
  241. stock M@(volatile const vardata[], dataSize, &pointer)
  242. {
  243. P:2("M@ called");
  244. new
  245. sAddr;
  246. // Store the basic data, including linked-list pointers and a pointer to the
  247. // location at which the data is stored.
  248. #emit LOAD.S.pri vardata
  249. #emit STOR.S.pri sAddr
  250. printf("", YSI_g_sFirstUVarData);
  251. #emit LOAD.pri YSI_g_sFirstUVarData
  252. #emit SREF.S.pri sAddr
  253. YSI_g_sFirstUVarData = sAddr;
  254. sAddr += 4;
  255. #emit LOAD.S.pri pointer
  256. #emit SREF.S.pri sAddr
  257. sAddr += 4;
  258. #emit LOAD.S.pri dataSize
  259. #emit SREF.S.pri sAddr
  260. P:5("M@: %d %d %d %d %s", vardata[0], vardata[1], vardata[2], vardata[3], vardata[4]);
  261. P:5("M@: %d", YSI_g_sFirstUVarData);
  262. }
  263. hook OnScriptExit()
  264. {
  265. P:1("Svar_OnScriptExit called");
  266. // Loop through all the player data items and write them to a file.
  267. new
  268. INI:__fwrite = INI_Open("YSI/" #MODE_NAME "_config.ini");
  269. if (__fwrite == INI_NO_FILE)
  270. {
  271. P:E("y_svar could not open data file for " #MODE_NAME);
  272. return;
  273. }
  274. INI_SetTag(__fwrite, "y_svar");
  275. new
  276. p = YSI_g_sFirstUVarData,
  277. temp;
  278. while (p != -1)
  279. {
  280. // DO NOT CHANGE THE CODE BELOW HERE!!!
  281. // Get the data size.
  282. #emit LOAD.S.pri p
  283. #emit ADD.C 8
  284. #emit STOR.S.pri temp
  285. #emit LREF.S.pri temp
  286. #emit PUSH.pri
  287. // Get the data pointer.
  288. #emit LOAD.S.pri p
  289. #emit ADD.C 4
  290. #emit STOR.S.pri temp
  291. #emit LREF.S.pri temp
  292. #emit PUSH.pri
  293. // Get the function name.
  294. #emit LOAD.S.pri p
  295. #emit ADD.C 12
  296. #emit PUSH.pri
  297. // Save the next pointer.
  298. #emit LREF.S.pri p
  299. #emit STOR.S.pri p
  300. // Now push the size of data put on the stack.
  301. #emit PUSH.S __fwrite
  302. #emit PUSH.C 16
  303. // Now get the return address and push it.
  304. #emit LCTRL 6
  305. #emit ADD.C 28
  306. #emit PUSH.pri
  307. // Call "Player_WriteArray" directly.
  308. #emit CONST.pri INI_WriteArray
  309. #emit SCTRL 6
  310. // DO NOT CHANGE THE CODE ABOVE HERE!!!
  311. }
  312. INI_Close(__fwrite);
  313. }
  314. hook OnScriptInit()
  315. {
  316. P:1("hook Svar_OnScriptInit called");
  317. // List them all.
  318. YSI_g_sFirstUVarData = -1;
  319. // Call all @yS_ functions to get all required data.
  320. new
  321. idx,
  322. buffer;
  323. while ((idx = AMX_GetPublicPointerSuffix(idx, buffer, _A<@yS_>)))
  324. {
  325. #emit PUSH.C 0
  326. #emit LCTRL 6
  327. #emit ADD.C 28
  328. #emit PUSH.pri
  329. #emit LOAD.S.pri buffer
  330. #emit SCTRL 6
  331. }
  332. // We don't need any of the reset code, but we do need the loading code.
  333. #if !defined _YSI_ONLY_ONE_CONFIG_LOAD
  334. #define _YSI_ONLY_ONE_CONFIG_LOAD
  335. INI_ParseFile("YSI/" #MODE_NAME "_config.ini", "_Config_Load_%s");
  336. #endif
  337. }