y_languages_entry.inc 11 KB

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