1
0

y_languages.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. #if defined _INC_y_languages
  2. #endinput
  3. #endif
  4. #define _INC_y_languages
  5. /**
  6. * <library name="y_languages">
  7. * <section>
  8. * Description
  9. * </section>
  10. * Handles language features - loading, listing and which one a player is.
  11. * <section>
  12. * Version
  13. * </section>
  14. * 1.0
  15. * </library>
  16. *//** *//*
  17. Legal:
  18. Version: MPL 1.1
  19. The contents of this file are subject to the Mozilla Public License Version
  20. 1.1 the "License"; you may not use this file except in compliance with
  21. the License. You may obtain a copy of the License at
  22. http://www.mozilla.org/MPL/
  23. Software distributed under the License is distributed on an "AS IS" basis,
  24. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  25. for the specific language governing rights and limitations under the
  26. License.
  27. The Original Code is the YSI framework.
  28. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  29. Portions created by the Initial Developer are Copyright C 2011
  30. the Initial Developer. All Rights Reserved.
  31. Contributors:
  32. Y_Less
  33. koolk
  34. JoeBullet/Google63
  35. g_aSlice/Slice
  36. Misiur
  37. samphunter
  38. tianmeta
  39. maddinat0r
  40. spacemud
  41. Crayder
  42. Dayvison
  43. Ahmad45123
  44. Zeex
  45. irinel1996
  46. Yiin-
  47. Chaprnks
  48. Konstantinos
  49. Masterchen09
  50. Southclaws
  51. PatchwerkQWER
  52. m0k1
  53. paulommu
  54. udan111
  55. Thanks:
  56. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  57. ZeeX - Very productive conversations.
  58. koolk - IsPlayerinAreaEx code.
  59. TheAlpha - Danish translation.
  60. breadfish - German translation.
  61. Fireburn - Dutch translation.
  62. yom - French translation.
  63. 50p - Polish translation.
  64. Zamaroht - Spanish translation.
  65. Los - Portuguese translation.
  66. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes for
  67. me to strive to better.
  68. Pixels^ - Running XScripters where the idea was born.
  69. Matite - Pestering me to release it and using it.
  70. Very special thanks to:
  71. Thiadmer - PAWN, whose limits continue to amaze me!
  72. Kye/Kalcor - SA:MP.
  73. SA:MP Team past, present and future - SA:MP.
  74. Optional plugins:
  75. Gamer_Z - GPS.
  76. Incognito - Streamer.
  77. Me - sscanf2, fixes2, Whirlpool.
  78. */
  79. #include "..\YSI_Internal\y_version"
  80. #define YSIM_U_DISABLE
  81. #define MASTER 56
  82. #include "..\YSI_Core\y_master"
  83. #include "..\YSI_Coding\y_hooks"
  84. #if !defined MAX_LANGUAGES
  85. #define MAX_LANGUAGES (4)
  86. #endif
  87. #if !defined YSI_MAX_LANGUAGES
  88. #define YSI_MAX_LANGUAGES (Language:MAX_LANGUAGES)
  89. #endif
  90. #define NO_LANGUAGE (Language:-1)
  91. #define MAX_LANGUAGE_NAME (16)
  92. #define Language_IsValid(%0) (Language:0 <= (%0) < YSI_MAX_LANGUAGES && YSI_g_sLanguages[(%0)][0])
  93. //#define FOREIGN forward
  94. //#define GLOBAL stock
  95. static stock
  96. Language:YSI_g_sPlayerLanguage[MAX_PLAYERS] = {NO_LANGUAGE, ...},
  97. YSI_g_sLanguageCodes[YSI_MAX_STRING],
  98. YSI_g_sLanguages[YSI_MAX_LANGUAGES][MAX_LANGUAGE_NAME],
  99. Language:YSI_g_sLanguageCount;
  100. /*-------------------------------------------------------------------------*//**
  101. * <summary>-</summary>
  102. *//*------------------------------------------------------------------------**/
  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. /*-------------------------------------------------------------------------*//**
  190. * <summary>-</summary>
  191. *//*------------------------------------------------------------------------**/
  192. #if _YSIM_IS_CLIENT
  193. // Don't give a warning if this is a client script. Note that stubs are
  194. // disabled for this script so this is the only state that makes sense.
  195. stock
  196. #endif
  197. Language:Langs_AddLanguage(string:code[], string:name[])
  198. {
  199. // This is done this way to give a compiler warning when it's not used.
  200. return _Langs_AddLanguage(code, name);
  201. }
  202. #pragma tabsize 4
  203. /*-------------------------------------------------------------------------*//**
  204. * <summary>-</summary>
  205. *//*------------------------------------------------------------------------**/
  206. foreign Language:Langs_GetLanguageAt(id);
  207. global Language:Langs_GetLanguageAt(id)
  208. {
  209. for (new Language:x = Language:0; x != YSI_MAX_LANGUAGES; ++x)
  210. {
  211. if (YSI_g_sLanguages[x][0])
  212. {
  213. if (!id--)
  214. {
  215. return x;
  216. }
  217. }
  218. }
  219. return NO_LANGUAGE;
  220. }
  221. /*-------------------------------------------------------------------------*//**
  222. * <summary>-</summary>
  223. *//*------------------------------------------------------------------------**/
  224. foreign void:Langs_RemoveLanguage(Language:l);
  225. global void:Langs_RemoveLanguage(Language:l)
  226. {
  227. if (Language_IsValid(l))
  228. {
  229. // Interestingly, this library is one of the few times I have used
  230. // native string functions rather than hand-rolled ones (except for
  231. // strcat used in strcpy). It just so happens that they map well here.
  232. strdel(YSI_g_sLanguageCodes, _:l * 3, _:l * 3 + 3);
  233. YSI_g_sLanguages[l][0] = '\0';
  234. //return 1;
  235. }
  236. //return 0;
  237. }
  238. /*-------------------------------------------------------------------------*//**
  239. * <summary>-</summary>
  240. *//*------------------------------------------------------------------------**/
  241. foreign Language:Langs_GetLanguage(string:identifier[]);
  242. global Language:Langs_GetLanguage(string:identifier[])
  243. {
  244. if (strlen(identifier) > 2)
  245. {
  246. // Search for the language by full name.
  247. for (new Language:i = Language:0; i != YSI_g_sLanguageCount; ++i)
  248. {
  249. if (YSI_g_sLanguages[i][0] && !strcmp(YSI_g_sLanguages[i], identifier, true))
  250. {
  251. return i;
  252. }
  253. }
  254. }
  255. else
  256. {
  257. // Search for the language by short name.
  258. new
  259. pos = strfind(YSI_g_sLanguageCodes, identifier, true);
  260. if (pos != -1)
  261. {
  262. return Language:(pos / 3);
  263. }
  264. }
  265. return NO_LANGUAGE;
  266. }
  267. /*-------------------------------------------------------------------------*//**
  268. * <summary>-</summary>
  269. *//*------------------------------------------------------------------------**/
  270. foreign Language:Langs_GetLanguageCount();
  271. global Language:Langs_GetLanguageCount()
  272. {
  273. return YSI_g_sLanguageCount;
  274. }
  275. /*-------------------------------------------------------------------------*//**
  276. * <summary>-</summary>
  277. *//*------------------------------------------------------------------------**/
  278. foreign string:Langs_GetLanguageCodes();
  279. global string:Langs_GetLanguageCodes()
  280. {
  281. // I don't quite know what this will do yet, as in how it will work...
  282. return YSI_g_sLanguageCodes;
  283. }
  284. /*-------------------------------------------------------------------------*//**
  285. * <summary>-</summary>
  286. *//*------------------------------------------------------------------------**/
  287. foreign string:Langs_GetName(Language:l);
  288. global string:Langs_GetName(Language:l)
  289. {
  290. // I don't quite know what this will do yet, as in how it will work...
  291. new
  292. ret[YSI_MAX_STRING];
  293. if (Language_IsValid(l))
  294. {
  295. strcpy(ret, YSI_g_sLanguages[l]);
  296. }
  297. return ret;
  298. }
  299. /*-------------------------------------------------------------------------*//**
  300. * <summary>-</summary>
  301. *//*------------------------------------------------------------------------**/
  302. foreign string:Langs_GetCode(Language:l);
  303. global string:Langs_GetCode(Language:l)
  304. {
  305. new
  306. ret[YSI_MAX_STRING];
  307. if (Language_IsValid(l))
  308. {
  309. strcpy(ret, YSI_g_sLanguageCodes[_:l * 3], 3);
  310. }
  311. return ret;
  312. }
  313. /*-------------------------------------------------------------------------*//**
  314. * <summary>-</summary>
  315. *//*------------------------------------------------------------------------**/
  316. foreign bool:Langs_IsValid(Language:l);
  317. global bool:Langs_IsValid(Language:l)
  318. {
  319. return Language_IsValid(l);
  320. }
  321. /*-------------------------------------------------------------------------*//**
  322. * <summary>-</summary>
  323. *//*------------------------------------------------------------------------**/
  324. foreign Language:Langs_GetPlayerLanguage(playerid);
  325. global Language:Langs_GetPlayerLanguage(playerid)
  326. {
  327. #if defined _YSI_SPECIAL_DEBUG
  328. #pragma unused playerid
  329. return Language:0;
  330. #else
  331. return YSI_g_sPlayerLanguage[playerid];
  332. #endif
  333. }
  334. /*-------------------------------------------------------------------------*//**
  335. * <summary>-</summary>
  336. *//*------------------------------------------------------------------------**/
  337. foreign Language:Langs_SetPlayerLanguage(playerid, Language:l);
  338. global Language:Langs_SetPlayerLanguage(playerid, Language:l)
  339. {
  340. if (Language_IsValid(l))
  341. {
  342. return YSI_g_sPlayerLanguage[playerid] = l;
  343. }
  344. return NO_LANGUAGE;
  345. }
  346. /*-------------------------------------------------------------------------*//**
  347. * <summary>-</summary>
  348. *//*------------------------------------------------------------------------**/
  349. foreign Language:Langs_SetPlayerCode(playerid, string:code[]);
  350. global Language:Langs_SetPlayerCode(playerid, string:code[])
  351. {
  352. return Langs_SetPlayerLanguage(playerid, Langs_GetLanguage(code));
  353. }
  354. /*-------------------------------------------------------------------------*//**
  355. * <summary>-</summary>
  356. *//*------------------------------------------------------------------------**/
  357. mhook OnPlayerConnect(playerid)
  358. {
  359. for (new Language:i = Language:0; i != YSI_g_sLanguageCount; ++i)
  360. {
  361. //printf("%s", YSI_g_sLanguages[i]);
  362. if (YSI_g_sLanguages[i][0])
  363. {
  364. // They have to default to some language, may as well be the first
  365. // valid one.
  366. YSI_g_sPlayerLanguage[playerid] = i;
  367. // May also add some special functions here to display the language
  368. // messages to a player in all languages, but I think that would be
  369. // better left to the mode writer, then they can integrate it in to
  370. // their own mode introduction.
  371. return 1;
  372. }
  373. }
  374. P:E("No languages set");
  375. return 0;
  376. }
  377. #include "..\YSI_Core\y_master"