y_text.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /**--------------------------------------------------------------------------**\
  2. =====================================
  3. Y Sever Includes - Language Text Core
  4. =====================================
  5. Description:
  6. Provides interfaces for displaying text from anywhere by way of native like
  7. functions using text indexes rather than text. Due to a compile problem a
  8. number of the stock functions should be static but can't be.
  9. Legal:
  10. Version: MPL 1.1
  11. The contents of this file are subject to the Mozilla Public License Version
  12. 1.1 (the "License"); you may not use this file except in compliance with
  13. the License. You may obtain a copy of the License at
  14. http://www.mozilla.org/MPL/
  15. Software distributed under the License is distributed on an "AS IS" basis,
  16. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  17. for the specific language governing rights and limitations under the
  18. License.
  19. The Original Code is the YSI text include.
  20. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  21. Portions created by the Initial Developer are Copyright (C) 2011
  22. the Initial Developer. All Rights Reserved.
  23. Contributors:
  24. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  25. Thanks:
  26. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  27. ZeeX - Very productive conversations.
  28. koolk - IsPlayerinAreaEx code.
  29. TheAlpha - Danish translation.
  30. breadfish - German translation.
  31. Fireburn - Dutch translation.
  32. yom - French translation.
  33. 50p - Polish translation.
  34. Zamaroht - Spanish translation.
  35. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  36. for me to strive to better.
  37. Pixels^ - Running XScripters where the idea was born.
  38. Matite - Pestering me to release it and using it.
  39. Very special thanks to:
  40. Thiadmer - PAWN, whose limits continue to amaze me!
  41. Kye/Kalcor - SA:MP.
  42. SA:MP Team past, present and future - SA:MP.
  43. Version:
  44. 0.1.1
  45. Changelog:
  46. 17/02/07:
  47. Added saving of style parameter for text draws.
  48. 27/12/07:
  49. Added text_draw support.
  50. 24/06/07:
  51. Modified a few functions to use Bit_GetBit for speed.
  52. 19/06/07:
  53. Added default language for alt language with no text.
  54. Added console errors.
  55. Added support for blank INI strings to ignore the text.
  56. Increased speed of non format sends by saving each language
  57. 14/06/07:
  58. Added type and data loading for strings.
  59. Altered display functions to use files defined styles.
  60. 13/06/07:
  61. Removed unfound text ignorance in group send functions.
  62. Added improved error handling to support custom messges.
  63. 02/05/07:
  64. Added YSI_ prefix to all globals.
  65. 23/03/07:
  66. First version.
  67. Functions:
  68. Public:
  69. Text_ResetAll - Resets the entire text system.
  70. Text_NewLanguage - Sets up to parse a new language.
  71. Text_Parse - Sorts loaded text into a binary tree.
  72. Text_DataSave_data - Saves text appearence data.
  73. Text_DataSave_colours - Saves file colour defines.
  74. Text_DataSave_colors - Wrapper for above function.
  75. Core:
  76. -
  77. Stock:
  78. Text_FindTextPointers - Should be static but can't be :(.
  79. Text_AddToBuffer - Saves all passed text for processing.
  80. Text_GetTextFromIndex - Gets text from an array pointer and language.
  81. Text_GetErrorMessage - Gets an unfound message.
  82. Text_GetTextStyle - Gets text's style.
  83. Text_GetTextColour - Gets text's colour.
  84. Text_GetTextTime - Gets text's time.
  85. Text_Send - Sends a message to a player.
  86. Text_SendToAll - Sends a message to all players.
  87. Text_SendToGroup - Sends a message to a defined group.
  88. Text_SendToPlayers - Sends a message to a passed group.
  89. Text_Display - Display an actual string to a player in a given style.
  90. Static:
  91. Text_AddText - Adds text to the tree after sorting.
  92. Inline:
  93. Text_Text - Constructor - Calls Text_ResetAll.
  94. Text_SetLangPointer - Sets the pointer for a language to a position.
  95. Text_ResetLangPointers - Resets all the pointers for one language.
  96. Text_GetPlayerLanguage - Gets a players language.
  97. Text_GetText - Gets text from an identifier and language.
  98. Text_GetPlayerText - Gets text from an identifier and playerid.
  99. Text_SendFormat - Sends a formatted message to a player.
  100. Text_SendToAllFormat - Sends a formatted message to all players.
  101. Text_SendToGroupFormat - Sends a formatted message to a defined group.
  102. Text_SendToPlayersFormat - Sends a formatted message to a passed group.
  103. API:
  104. -
  105. Callbacks:
  106. -
  107. Definitions:
  108. MAX_TEXT_NAME - Maximum length of a text identifier.
  109. MAX_TEXT_ENTRY - Maximum length of a text string.
  110. TEXT_NO_TEXT - Value for no text for that language.
  111. TEXT_NO_POINTERS - Value for no text found.
  112. TEXT_TYPE_CLIENT - Flag for sending a client formatted message (unused).
  113. TEXT_TYPE_GAME - Flag for sending a game text formatted message (unused).
  114. MAX_TEXT_COLOURS - Max number of defined colours in an ini file.
  115. Enums:
  116. E_TEXT_POINTERS - Structure of the language pointer array.
  117. Macros:
  118. Text_RegisterTag - Placed as a function, calls Text_AddToBuffer for tags.
  119. Tags:
  120. -
  121. Variables:
  122. Global:
  123. -
  124. Static:
  125. YSI_g_sTextTable - Array of all text entries.
  126. YSI_g_sNameTable - Array of all text names and language pointers.
  127. YSI_g_sSearchTree - Binary tree of text hashes.
  128. YSI_g_sTextInited - Flag for text binary sorted.
  129. YSI_g_sBufferIndex - Index of next text slot for the current language.
  130. YSI_g_sTextCount - Count of largest number of texts in one language.
  131. YSI_g_sBufferLang - Current language being loaded.
  132. YSI_g_sLangBuffer - Saves the current position of each language.
  133. YSI_g_sColours - Saves defined colours for use.
  134. Commands:
  135. -
  136. Compile options:
  137. -
  138. Operators:
  139. -
  140. </remarks>
  141. \**--------------------------------------------------------------------------**/
  142. #include "internal\y_version"
  143. // Include "y_unique" multiple times.
  144. #include "internal\y_unique"
  145. #if !defined _inc_y_text
  146. #error Did you do <YSI/y_text> instead of the required <YSI\y_text>?
  147. #endif
  148. #undef _inc_y_text
  149. // Need an include guard here.
  150. #if defined _INC_y_text
  151. #endinput
  152. #endif
  153. #define _INC_y_text
  154. #include <a_samp>
  155. // Make sure this is included early.
  156. #include "y_playerset"
  157. // Apparently I'd already written the internal code to support this and forgot!
  158. #define Y_TEXT_UNIQUE
  159. #if !defined Y_TEXT_MAX_SETS
  160. #define Y_TEXT_MAX_SETS (16)
  161. #endif
  162. #if !defined Y_TEXT_PER_SET
  163. #define Y_TEXT_PER_SET (64)
  164. #endif
  165. #if !defined MAX_TEXT_ENTRIES
  166. #define MAX_TEXT_ENTRIES (Y_TEXT_PER_SET * Y_TEXT_MAX_SETS)
  167. #endif
  168. #include "internal\y_styles"
  169. // This is a horribly eclectic collection of libraries from all over the place
  170. // and written at different times with different aims - frankly I'll be AMAZED
  171. // if it all comes together and works.
  172. #include "internal\y_formatin"
  173. #include "internal\y_textrender"
  174. #include "internal\y_textint"
  175. #define UNDO_MOVE|||
  176. #define DO_MOVE|||%0£££ %0DO_MOVE|||
  177. #define Text_Send(%0,%1) PSF:_Text_Send(%0,DO_TEXT_SET:%1 UNDO_MOVE|||)
  178. #define Text_Render(%0,%1) _Text_Render(%0,DO_TEXT_SET:%1 UNDO_MOVE|||)
  179. //#define _Text_Send(%0YCMD:%1) _Text_Send(%0_:YCMD_REP_0:YCMD_REP_1:%1)
  180. //#define YCMD_REP_0:YCMD_REP_1:%0, Command_GetID(%0),
  181. //#define YCMD_REP_1:%0) Command_GetID(%0))
  182. // This code allows "DEFAULT_TEXT_SET" to propogate through the text one section
  183. // at a time without detecting later matches too early. The design of "DO_MOVE"
  184. // and "UNDO_MOVE" means that the compiler will correctly detect the end of the
  185. // code, regardless of the length, and end.
  186. #define Text_MessageBox(%0,%1,%2,%3,%4,%5) PSF:_Text_DialogBox(%0,DIALOG_STYLE_MSGBOX,%1,DO_TEXT_SET:%2 DO_MOVE|||,DO_TEXT_SET:%3 £££,DO_TEXT_SET:%4 £££,DO_TEXT_SET:%5 UN£££)
  187. #define Text_InputBox(%0,%1,%2,%3,%4,%5) PSF:_Text_DialogBox(%0,DIALOG_STYLE_INPUT,%1,DO_TEXT_SET:%2 DO_MOVE|||,DO_TEXT_SET:%3 £££,DO_TEXT_SET:%4 £££,DO_TEXT_SET:%5 UN£££)
  188. #define Text_ListBox(%0,%1,%2,%3,%4,%5) PSF:_Text_DialogBox(%0,DIALOG_STYLE_LIST,%1,DO_TEXT_SET:%2 DO_MOVE|||,DO_TEXT_SET:%3 £££,DO_TEXT_SET:%4 £££,DO_TEXT_SET:%5 UN£££)
  189. #define Text_PasswordBox(%0,%1,%2,%3,%4,%5) PSF:_Text_DialogBox(%0,DIALOG_STYLE_PASSWORD,%1,DO_TEXT_SET:%2 DO_MOVE|||,DO_TEXT_SET:%3 £££,DO_TEXT_SET:%4 £££,DO_TEXT_SET:%5 UN£££)
  190. #define Text_DialogBox(%0,%9,%1,%2,%3,%4,%5) PSF:_Text_DialogBox(%0,%9,%1,DO_TEXT_SET:%2 DO_MOVE|||,DO_TEXT_SET:%3 £££,DO_TEXT_SET:%4 £££,DO_TEXT_SET:%5 UN£££)
  191. //#define Text_MessageBox(%0,%1,%2,%3) PSF:_Text_MessageBox(%0,%1,DEFAULT_TEXT_SET,#%2 DO_MOVE|||,DEFAULT_TEXT_SET,#%3 £££,DEFAULT_TEXT_SET,#%4 £££,DEFAULT_TEXT_SET,#%5 UN£££)
  192. //#define _Text_MessageBox(%0YCMD:%1) _Text_MessageBox(%0_:YCMD_REP_0:YCMD_REP_1:%1)
  193. //stock Text_GetAllIDs(
  194. stock formatex(output[], len, const format[], GLOBAL_TAG_TYPES:...)
  195. {
  196. static
  197. sFormat[1024],
  198. sTemp[1024];
  199. // "Format_Standardise" modifies "input" repeatedly.
  200. //strcpy(sTemp, format);
  201. strcpy(sFormat, format);
  202. Format_Standardise(sFormat, sTemp);
  203. new
  204. n = (numargs() - 3) * 4;
  205. if (n)
  206. {
  207. // Push all passed parameters.
  208. new
  209. arg_start,
  210. arg_end;
  211. // Load the real address of the last static parameter. Do this by
  212. // loading the address of the parameter and then adding the value of
  213. // [FRM] (frame pointer).
  214. #emit CONST.alt format
  215. #emit LCTRL 5
  216. #emit ADD
  217. #emit STOR.S.pri arg_start
  218. // Load the address of the last variable parameter. Do this by adding
  219. // the number of variables on the value just loaded.
  220. #emit LOAD.S.alt n
  221. #emit ADD
  222. #emit STOR.S.pri arg_end
  223. new
  224. arg_cur = arg_end;
  225. do
  226. {
  227. #emit LOAD.S.pri arg_cur
  228. #emit LOAD.I
  229. #emit PUSH.pri
  230. arg_cur -= 4;
  231. }
  232. while (arg_cur > arg_start);
  233. Format_Render(INVALID_PLAYER_ID, NO_LANGUAGE, output, len - 1, 0, e_FORMAT_FLAGS_NONE, sTemp, n / 4);
  234. #emit LCTRL 4
  235. #emit LOAD.S.alt n
  236. #emit ADD
  237. #emit SCTRL 4
  238. }
  239. else
  240. {
  241. Format_Render(INVALID_PLAYER_ID, NO_LANGUAGE, output, len - 1, 0, e_FORMAT_FLAGS_NONE, sTemp, 0);
  242. }
  243. }
  244. stock printfex(const format[], GLOBAL_TAG_TYPES:...)
  245. {
  246. static
  247. sFormat[1024],
  248. sTemp[1024];
  249. // "Format_Standardise" modifies "input" repeatedly.
  250. strcpy(sFormat, format);
  251. Format_Standardise(sFormat, sTemp);
  252. new
  253. n = (numargs() - 3) * 4;
  254. if (n)
  255. {
  256. // Push all passed parameters.
  257. new
  258. arg_start,
  259. arg_end;
  260. // Load the real address of the last static parameter. Do this by
  261. // loading the address of the parameter and then adding the value of
  262. // [FRM] (frame pointer).
  263. #emit CONST.alt format
  264. #emit LCTRL 5
  265. #emit ADD
  266. #emit STOR.S.pri arg_start
  267. // Load the address of the last variable parameter. Do this by adding
  268. // the number of variables on the value just loaded.
  269. #emit LOAD.S.alt n
  270. #emit ADD
  271. #emit STOR.S.pri arg_end
  272. new
  273. arg_cur = arg_end;
  274. do
  275. {
  276. #emit LOAD.S.pri arg_cur
  277. #emit LOAD.I
  278. #emit PUSH.pri
  279. arg_cur -= 4;
  280. }
  281. while (arg_cur > arg_start);
  282. Format_Render(INVALID_PLAYER_ID, NO_LANGUAGE, sFormat, sizeof (sFormat) - 1, 0, e_FORMAT_FLAGS_NONE, sTemp, n / 4);
  283. #emit LCTRL 4
  284. #emit LOAD.S.alt n
  285. #emit ADD
  286. #emit SCTRL 4
  287. }
  288. else
  289. {
  290. Format_Render(INVALID_PLAYER_ID, NO_LANGUAGE, sFormat, sizeof (sFormat) - 1, 0, e_FORMAT_FLAGS_NONE, sTemp, 0);
  291. }
  292. print(sFormat);
  293. }