ini.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. Legal:
  3. Version: MPL 1.1
  4. The contents of this file are subject to the Mozilla Public License Version
  5. 1.1 the "License"; you may not use this file except in compliance with
  6. the License. You may obtain a copy of the License at
  7. http://www.mozilla.org/MPL/
  8. Software distributed under the License is distributed on an "AS IS" basis,
  9. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  10. for the specific language governing rights and limitations under the
  11. License.
  12. The Original Code is the YSI framework.
  13. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  14. Portions created by the Initial Developer are Copyright C 2011
  15. the Initial Developer. All Rights Reserved.
  16. Contributors:
  17. Y_Less
  18. koolk
  19. JoeBullet/Google63
  20. g_aSlice/Slice
  21. Misiur
  22. samphunter
  23. tianmeta
  24. maddinat0r
  25. spacemud
  26. Crayder
  27. Dayvison
  28. Ahmad45123
  29. Zeex
  30. irinel1996
  31. Yiin-
  32. Chaprnks
  33. Konstantinos
  34. Masterchen09
  35. Southclaws
  36. PatchwerkQWER
  37. m0k1
  38. paulommu
  39. udan111
  40. Thanks:
  41. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  42. ZeeX - Very productive conversations.
  43. koolk - IsPlayerinAreaEx code.
  44. TheAlpha - Danish translation.
  45. breadfish - German translation.
  46. Fireburn - Dutch translation.
  47. yom - French translation.
  48. 50p - Polish translation.
  49. Zamaroht - Spanish translation.
  50. Los - Portuguese translation.
  51. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes for
  52. me to strive to better.
  53. Pixels^ - Running XScripters where the idea was born.
  54. Matite - Pestering me to release it and using it.
  55. Very special thanks to:
  56. Thiadmer - PAWN, whose limits continue to amaze me!
  57. Kye/Kalcor - SA:MP.
  58. SA:MP Team past, present and future - SA:MP.
  59. Optional plugins:
  60. Gamer_Z - GPS.
  61. Incognito - Streamer.
  62. Me - sscanf2, fixes2, Whirlpool.
  63. */
  64. // Third "uvar" version.
  65. #define _YS@LE@E%0>
  66. #define _YS@LT@E%0> ;
  67. #define _YS@LM:%0[%1][%2] STRIP_NUMBERS:%0[%1]|||%2:0|||,sizeof(%0[])
  68. // Needs two levels of indirection to strip the excess commas (,%0,%1).
  69. #define _YS@LO(,%0,[%2]%4) %0@yS_();public %0@yS_(){M@(#...#%0,_:%2,STRIP_NUMBERS:_YS@LM:%0|||%2:0|||%4);}
  70. #define _YS@LE%0[%3]%4,%2> _YS@LO(%0,[%3]%4) _YS@LE%2>
  71. // Recursive local default string definition.
  72. #define _YS@LJ(,%0,%2) %0%2
  73. #define _YS@LT%0[%3]%4,%2> ,_YS@LJ(%0,[%3]%4)_YS@LT%2>
  74. #define _YS@LA%0[%3]%4,%2> _YS@LJ(%0,[%3]%4)_YS@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] - Size of enum.
  80. // [3] - Start of name.
  81. //
  82. //#define svar%0[%1]%2; stock _YS@LA,%0[%1]%2@E>_YS@LE,%0[%1]%2@E>
  83. #define svar%0; stock _YS@LA,%0,@E>_YS@LE,%0,@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:7("_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. * <param name="vardata">Handle to the memory location in which to store info.</param>
  224. * <param name="dataSize">The size of the array (first dimension).</param>
  225. * <param name="pointer">The start of the data.</param>
  226. * <param name="ds2">The second dimension of the data.</param>
  227. * <remarks>
  228. * This function modifies "vardata" well beyond its original limits to contain
  229. * information on the structure of the enum used to define "val". This code
  230. * uses the name and size information passed in the additional parameters as
  231. * strings, and makes assumptions about how the compiler lays out memory to
  232. * combine all the passed strings in to one big string in what could be ROM,
  233. * but in SA:MP isn't. This takes a human readable(ish) description of the
  234. * array elements and converts it in to a much simpler to read format for the
  235. * computer to use later when loading and storing data.
  236. * </remarks>
  237. *//*------------------------------------------------------------------------**/
  238. stock M@(volatile const vardata[], dataSize, &pointer, ds2 = 1)
  239. {
  240. dataSize *= ds2;
  241. P:2("M@ called");
  242. new
  243. sAddr;
  244. // Store the basic data, including linked-list pointers and a pointer to the
  245. // location at which the data is stored.
  246. #emit LOAD.S.pri vardata
  247. #emit STOR.S.pri sAddr
  248. printf("", YSI_g_sFirstUVarData);
  249. #emit LOAD.pri YSI_g_sFirstUVarData
  250. #emit SREF.S.pri sAddr
  251. YSI_g_sFirstUVarData = sAddr;
  252. sAddr += 4;
  253. #emit LOAD.S.pri pointer
  254. #emit SREF.S.pri sAddr
  255. sAddr += 4;
  256. #emit LOAD.S.pri dataSize
  257. #emit SREF.S.pri sAddr
  258. P:5("M@: %d %d %d %d %s", vardata[0], vardata[1], vardata[2], vardata[3], vardata[4]);
  259. P:5("M@: %d", YSI_g_sFirstUVarData);
  260. }
  261. hook OnScriptExit()
  262. {
  263. Svar_Save();
  264. }
  265. Svar_Save()
  266. {
  267. P:1("Svar_OnScriptExit called");
  268. // Loop through all the player data items and write them to a file.
  269. new
  270. INI:__fwrite = INI_Open("YSI/" #MODE_NAME "_config.ini");
  271. if (__fwrite == INI_NO_FILE)
  272. {
  273. P:E("y_svar could not open data file for " #MODE_NAME);
  274. return;
  275. }
  276. INI_SetTag(__fwrite, "y_svar");
  277. new
  278. p = YSI_g_sFirstUVarData,
  279. temp;
  280. while (p != -1)
  281. {
  282. // DO NOT CHANGE THE CODE BELOW HERE!!!
  283. // Get the data size.
  284. #emit LOAD.S.pri p
  285. #emit ADD.C 8
  286. #emit STOR.S.pri temp
  287. #emit LREF.S.pri temp
  288. #emit PUSH.pri
  289. // Get the data pointer.
  290. #emit LOAD.S.pri p
  291. #emit ADD.C 4
  292. #emit STOR.S.pri temp
  293. #emit LREF.S.pri temp
  294. #emit PUSH.pri
  295. // Get the function name.
  296. #emit LOAD.S.pri p
  297. #emit ADD.C 12
  298. #emit PUSH.pri
  299. // Save the next pointer.
  300. #emit LREF.S.pri p
  301. #emit STOR.S.pri p
  302. // Now push the size of data put on the stack.
  303. #emit PUSH.S __fwrite
  304. #emit PUSH.C 16
  305. // Now get the return address and push it.
  306. #emit LCTRL 6
  307. #emit ADD.C 28
  308. #emit PUSH.pri
  309. // Call "Player_WriteArray" directly.
  310. #emit CONST.pri INI_WriteArray
  311. #emit SCTRL 6
  312. // DO NOT CHANGE THE CODE ABOVE HERE!!!
  313. }
  314. INI_Close(__fwrite);
  315. }
  316. hook OnScriptInit()
  317. {
  318. P:1("hook Svar_OnScriptInit called");
  319. // List them all.
  320. YSI_g_sFirstUVarData = -1;
  321. // Call all @yS_ functions to get all required data.
  322. new
  323. idx,
  324. buffer;
  325. while ((idx = AMX_GetPublicPointerSuffix(idx, buffer, _A<@yS_>)))
  326. {
  327. #emit PUSH.C 0
  328. #emit LCTRL 6
  329. #emit ADD.C 28
  330. #emit PUSH.pri
  331. #emit LOAD.S.pri buffer
  332. #emit SCTRL 6
  333. }
  334. // We don't need any of the reset code, but we do need the loading code.
  335. #if !defined _YSI_ONLY_ONE_CONFIG_LOAD
  336. #define _YSI_ONLY_ONE_CONFIG_LOAD
  337. INI_ParseFile("YSI/" #MODE_NAME "_config.ini", "_Config_Load_%s");
  338. #endif
  339. }