ini.inc 10 KB

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