y_languages.inc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*----------------------------------------------------------------------------*\
  2. ==================================
  3. Y Sever Includes - Language Code
  4. ==================================
  5. Description:
  6. Handles language features - loading, listing and which one a player is.
  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 ini 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. 1.0
  43. Changelog:
  44. 02/12/11:
  45. Updated Langs_Add to accept the parameters in either order.
  46. 01/05/11:
  47. First version.
  48. Functions:
  49. Public:
  50. -
  51. Core:
  52. -
  53. Stock:
  54. -
  55. Static:
  56. -
  57. Inline:
  58. -
  59. API:
  60. -
  61. Callbacks:
  62. -
  63. Definitions:
  64. -
  65. Enums:
  66. -
  67. Macros:
  68. -
  69. Tags:
  70. -
  71. Variables:
  72. Global:
  73. -
  74. Static:
  75. -
  76. Commands:
  77. -
  78. Compile options:
  79. -
  80. Operators:
  81. -
  82. \*----------------------------------------------------------------------------*/
  83. #include "internal\y_version"
  84. #define YSIM_U_DISABLE
  85. #include "y_master"
  86. #include "y_hooks"
  87. #if !defined MAX_LANGUAGES
  88. #define MAX_LANGUAGES (4)
  89. #endif
  90. #if !defined YSI_MAX_LANGUAGES
  91. #define YSI_MAX_LANGUAGES (Language:MAX_LANGUAGES)
  92. #endif
  93. #define NO_LANGUAGE (Language:-1)
  94. #define MAX_LANGUAGE_NAME (16)
  95. #define Language_IsValid(%0) (Language:0 <= (%0) < YSI_MAX_LANGUAGES && YSI_g_sLanguages[(%0)][0])
  96. //#define FOREIGN forward
  97. //#define GLOBAL stock
  98. static stock
  99. Language:YSI_g_sPlayerLanguage[MAX_PLAYERS] = {NO_LANGUAGE, ...},
  100. YSI_g_sLanguageCodes[YSI_MAX_STRING],
  101. YSI_g_sLanguages[YSI_MAX_LANGUAGES][MAX_LANGUAGE_NAME],
  102. Language:YSI_g_sLanguageCount;
  103. #define Langs_Add Langs_AddLanguage
  104. #define Languages_Add Langs_AddLanguage
  105. #define Languages_AddLanguage Langs_AddLanguage
  106. foreign Language:_Langs_AddLanguage(string:code[], string:name[]);
  107. global Language:_Langs_AddLanguage(string:code[], string:name[])
  108. {
  109. // Get the 2 character language short code.
  110. P:2("_Langs_AddLanguage called: %s %s", code, name);
  111. static
  112. ins[4] = "XX|";
  113. if ('a' <= code[0] <= 'z')
  114. {
  115. ins[0] = code[0] & ~0x20;
  116. }
  117. else
  118. {
  119. if (!(ins[0] = code[0]))
  120. {
  121. if (strlen(name) == 2)
  122. {
  123. return _Langs_AddLanguage(name, code);
  124. }
  125. else
  126. {
  127. P:E("Langs_AddLanguage must have a 2 letter code");
  128. return NO_LANGUAGE;
  129. }
  130. }
  131. }
  132. if ('a' <= code[1] <= 'z')
  133. {
  134. ins[1] = code[1] & ~0x20;
  135. }
  136. else
  137. {
  138. if (!(ins[1] = code[1]))
  139. {
  140. if (strlen(name) == 2)
  141. {
  142. return _Langs_AddLanguage(name, code);
  143. }
  144. else
  145. {
  146. P:E("Langs_AddLanguage must have a 2 letter code");
  147. return NO_LANGUAGE;
  148. }
  149. }
  150. }
  151. if (code[2])
  152. {
  153. if (strlen(name) == 2)
  154. {
  155. return _Langs_AddLanguage(name, code);
  156. }
  157. else
  158. {
  159. P:E("Langs_AddLanguage must have a 2 letter code");
  160. return NO_LANGUAGE;
  161. }
  162. }
  163. new
  164. Language:add = YSI_g_sLanguageCount;
  165. for (new Language:i = Language:0; i != YSI_g_sLanguageCount; ++i)
  166. {
  167. if (!YSI_g_sLanguages[i][0])
  168. {
  169. // Add the new language here.
  170. add = i;
  171. }
  172. else if (!strcmp(YSI_g_sLanguages[i], name))
  173. {
  174. // Already exists.
  175. return i;
  176. }
  177. }
  178. if (add < YSI_MAX_LANGUAGES)
  179. {
  180. strins(YSI_g_sLanguageCodes, ins, _:add * 3);
  181. P:5("Langs_AddLanguage: Codes = %s", YSI_g_sLanguageCodes);
  182. strcpy(YSI_g_sLanguages[add], name, MAX_LANGUAGE_NAME);
  183. P:5("Langs_AddLanguage: New = %s (%d, %d)", YSI_g_sLanguages[add], _:add, Language_IsValid(add));
  184. ++YSI_g_sLanguageCount;
  185. return add;
  186. }
  187. return NO_LANGUAGE;
  188. }
  189. #if _YSIM_IS_CLIENT
  190. // Don't give a warning if this is a client script. Note that stubs are
  191. // disabled for this script so this is the only state that makes sense.
  192. stock
  193. #endif
  194. Language:Langs_AddLanguage(string:code[], string:name[])
  195. {
  196. // This is done this way to give a compiler warning when it's not used.
  197. return _Langs_AddLanguage(code, name);
  198. }
  199. #pragma tabsize 4
  200. foreign Language:Langs_GetLanguageAt(id);
  201. global Language:Langs_GetLanguageAt(id)
  202. {
  203. for (new Language:x = Language:0; x != YSI_MAX_LANGUAGES; ++x)
  204. {
  205. if (YSI_g_sLanguages[x][0])
  206. {
  207. if (!id--)
  208. {
  209. return x;
  210. }
  211. }
  212. }
  213. return NO_LANGUAGE;
  214. }
  215. foreign Langs_RemoveLanguage(Language:l);
  216. global Langs_RemoveLanguage(Language:l)
  217. {
  218. if (Language_IsValid(l))
  219. {
  220. // Interestingly, this library is one of the few times I have used
  221. // native string functions rather than hand-rolled ones (except for
  222. // strcat used in strcpy). It just so happens that they map well here.
  223. strdel(YSI_g_sLanguageCodes, _:l * 3, _:l * 3 + 3);
  224. YSI_g_sLanguages[l][0] = '\0';
  225. return 1;
  226. }
  227. return 0;
  228. }
  229. foreign Language:Langs_GetLanguage(string:identifier[]);
  230. global Language:Langs_GetLanguage(string:identifier[])
  231. {
  232. if (strlen(identifier) > 2)
  233. {
  234. // Search for the language by full name.
  235. for (new Language:i = Language:0; i != YSI_g_sLanguageCount; ++i)
  236. {
  237. if (YSI_g_sLanguages[i][0] && !strcmp(YSI_g_sLanguages[i], identifier, true))
  238. {
  239. return i;
  240. }
  241. }
  242. }
  243. else
  244. {
  245. // Search for the language by short name.
  246. new
  247. pos = strfind(YSI_g_sLanguageCodes, identifier, true);
  248. if (pos != -1)
  249. {
  250. return Language:(pos / 3);
  251. }
  252. }
  253. return NO_LANGUAGE;
  254. }
  255. foreign Language:Langs_GetLanguageCount();
  256. global Language:Langs_GetLanguageCount()
  257. {
  258. return YSI_g_sLanguageCount;
  259. }
  260. foreign string:Langs_GetLanguageCodes();
  261. global string:Langs_GetLanguageCodes()
  262. {
  263. // I don't quite know what this will do yet, as in how it will work...
  264. return YSI_g_sLanguageCodes;
  265. }
  266. foreign string:Langs_GetName(Language:l);
  267. global string:Langs_GetName(Language:l)
  268. {
  269. // I don't quite know what this will do yet, as in how it will work...
  270. new
  271. ret[YSI_MAX_STRING];
  272. if (Language_IsValid(l))
  273. {
  274. strcpy(ret, YSI_g_sLanguages[l]);
  275. }
  276. return ret;
  277. }
  278. foreign string:Langs_GetCode(Language:l);
  279. global string:Langs_GetCode(Language:l)
  280. {
  281. new
  282. ret[YSI_MAX_STRING];
  283. if (Language_IsValid(l))
  284. {
  285. strcpy(ret, YSI_g_sLanguageCodes[_:l * 3], 3);
  286. }
  287. return ret;
  288. }
  289. foreign bool:Langs_IsValid(Language:l);
  290. global bool:Langs_IsValid(Language:l)
  291. {
  292. return Language_IsValid(l);
  293. }
  294. foreign Language:Langs_GetPlayerLanguage(playerid);
  295. global Language:Langs_GetPlayerLanguage(playerid)
  296. {
  297. #if defined _YSI_SPECIAL_DEBUG
  298. #pragma unused playerid
  299. return Language:0;
  300. #else
  301. return YSI_g_sPlayerLanguage[playerid];
  302. #endif
  303. }
  304. foreign Language:Langs_SetPlayerLanguage(playerid, Language:l);
  305. global Language:Langs_SetPlayerLanguage(playerid, Language:l)
  306. {
  307. if (Language_IsValid(l))
  308. {
  309. return YSI_g_sPlayerLanguage[playerid] = l;
  310. }
  311. return NO_LANGUAGE;
  312. }
  313. foreign Language:Langs_SetPlayerCode(playerid, string:code[]);
  314. global Language:Langs_SetPlayerCode(playerid, string:code[])
  315. {
  316. return Langs_SetPlayerLanguage(playerid, Langs_GetLanguage(code));
  317. }
  318. mhook OnPlayerConnect(playerid)
  319. {
  320. for (new Language:i = Language:0; i != YSI_g_sLanguageCount; ++i)
  321. {
  322. //printf("%s", YSI_g_sLanguages[i]);
  323. if (YSI_g_sLanguages[i][0])
  324. {
  325. // They have to default to some language, may as well be the first
  326. // valid one.
  327. YSI_g_sPlayerLanguage[playerid] = i;
  328. // May also add some special functions here to display the language
  329. // messages to a player in all languages, but I think that would be
  330. // better left to the mode writer, then they can integrate it in to
  331. // their own mode introduction.
  332. return 1;
  333. }
  334. }
  335. P:E("No languages set");
  336. return 0;
  337. }
  338. //#define YSI_SET_LAST_GROUP 21
  339. #include "internal\y_grouprevert"