text.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009-2011, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // Copyright (c) 2011-2017, SDLPAL development team.
  5. // All rights reserved.
  6. //
  7. // This file is part of SDLPAL.
  8. //
  9. // SDLPAL is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU General Public License as published by
  11. // the Free Software Foundation, either version 3 of the License, or
  12. // (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. //
  22. // Portions based on PALx Project by palxex.
  23. // Copyright (c) 2006-2008, Pal Lockheart <palxex@gmail.com>.
  24. //
  25. #include "main.h"
  26. #include <errno.h>
  27. #define FONT_COLOR_DEFAULT 0x4F
  28. #define FONT_COLOR_YELLOW 0x2D
  29. #define FONT_COLOR_RED 0x1A
  30. #define FONT_COLOR_CYAN 0x8D
  31. #define FONT_COLOR_CYAN_ALT 0x8C
  32. BOOL g_fUpdatedInBattle = FALSE;
  33. #define MESSAGE_MAX_BUFFER_SIZE 512
  34. #define INCLUDE_CODEPAGE_H
  35. #include "codepage.h"
  36. #ifndef PAL_CLASSIC
  37. # define ATB_WORD_COUNT 6
  38. static LPWSTR gc_rgszAdditionalWords[CP_MAX][ATB_WORD_COUNT] = {
  39. { L"\x6230\x9B25\x901F\x5EA6", L"\x4E00", L"\x4E8C", L"\x4E09", L"\x56DB", L"\x4E94" },
  40. { L"\x6218\x6597\x901F\x5EA6", L"\x4E00", L"\x4E8C", L"\x4E09", L"\x56DB", L"\x4E94" },
  41. //{ L"\x6226\x95D8\x901F\x5EA6", L"\x4E00", L"\x4E8C", L"\x4E09", L"\x56DB", L"\x4E94" },
  42. };
  43. static LPWSTR gc_rgszDefaultAdditionalWords[ATB_WORD_COUNT] = { NULL, L"\xFF11", L"\xFF12", L"\xFF13", L"\xFF14", L"\xFF15" };
  44. #endif
  45. #define SDLPAL_EXTRA_WORD_COUNT 1
  46. static LPWSTR gc_rgszSDLPalWords[CP_MAX][SDLPAL_EXTRA_WORD_COUNT] = {
  47. { L"\x8FD4\x56DE\x8A2D\x5B9A" },
  48. { L"\x8FD4\x56DE\x8BBE\x7F6E" },
  49. };
  50. LPWSTR g_rcCredits[12];
  51. typedef struct tagTEXTLIB
  52. {
  53. LPWSTR *lpWordBuf;
  54. LPWSTR *lpMsgBuf;
  55. int **lpIndexBuf;
  56. int nWords;
  57. int nMsgs;
  58. int nIndices;
  59. int nCurrentDialogLine;
  60. BYTE bCurrentFontColor;
  61. PAL_POS posIcon;
  62. PAL_POS posDialogTitle;
  63. PAL_POS posDialogText;
  64. BYTE bDialogPosition;
  65. BYTE bIcon;
  66. int iDelayTime;
  67. BOOL fUserSkip;
  68. BOOL fPlayingRNG;
  69. BYTE bufDialogIcons[282];
  70. } TEXTLIB, *LPTEXTLIB;
  71. static TEXTLIB g_TextLib;
  72. PAL_FORCE_INLINE int
  73. PAL_ParseLine(
  74. char *line,
  75. char **value,
  76. int *length,
  77. int deltrail
  78. )
  79. {
  80. //
  81. // Remove the leading spaces
  82. //
  83. while (*line && iswspace(*line)) line++;
  84. //
  85. // Skip comments starting with '#'
  86. //
  87. if (*line && *line != '#')
  88. {
  89. //
  90. // Split the index and value
  91. //
  92. LPSTR val = strchr(line, '=');
  93. if (val)
  94. {
  95. //
  96. // Remove the trailing spaces
  97. //
  98. LPSTR end = line + strlen(line);
  99. int index;
  100. if (end > line && end[-1] == '\n') *(--end) = 0;
  101. if (deltrail) while (end > line && iswspace(end[-1])) *(--end) = 0;
  102. //
  103. // Parse the index and pass out value
  104. //
  105. if (sscanf(line, "%d", &index) == 1)
  106. {
  107. *value = val + 1;
  108. *length = end - *value;
  109. return index;
  110. }
  111. }
  112. }
  113. return 0;
  114. }
  115. PAL_FORCE_INLINE char *
  116. PAL_ReadOneLine(
  117. char *temp,
  118. int limit,
  119. FILE *fp
  120. )
  121. {
  122. if (fgets(temp, limit, fp))
  123. {
  124. int n = strlen(temp);
  125. if (n == limit - 1 && temp[n - 1] != '\n' && !feof(fp))
  126. {
  127. // Line too long, try to read it as a whole
  128. int nn = 2;
  129. char *tmp = strdup(temp);
  130. while (!feof(fp))
  131. {
  132. if (!(tmp = (char *)realloc(tmp, nn * limit)))
  133. {
  134. TerminateOnError("PAL_ReadOneLine(): failed to allocate memory for long line!");
  135. }
  136. if (fgets(tmp + n, limit + 1, fp))
  137. {
  138. n += strlen(tmp + n);
  139. if (n < limit - 1 || temp[n - 1] == '\n')
  140. break;
  141. else
  142. nn++;
  143. }
  144. }
  145. if (tmp[n - 1] == '\n') tmp[n - 1] = 0;
  146. return tmp;
  147. }
  148. else
  149. {
  150. while (n > 0 && (temp[n - 1] == '\n' || temp[n - 1] == '\r')) temp[--n] = 0;
  151. return temp;
  152. }
  153. }
  154. else
  155. return NULL;
  156. }
  157. static int
  158. PAL_ReadMessageFile(
  159. FILE *fp
  160. )
  161. {
  162. char temp[MESSAGE_MAX_BUFFER_SIZE];
  163. struct _msg_entry
  164. {
  165. struct _msg_entry *next;
  166. wchar_t *value;
  167. } *cur_val = NULL;
  168. struct _msg_list_entry
  169. {
  170. struct _msg_list_entry *next;
  171. struct _msg_entry *value;
  172. int index;
  173. int count;
  174. } *head = NULL, *item = NULL;
  175. struct _word_list_entry
  176. {
  177. struct _word_list_entry *next;
  178. wchar_t *value;
  179. int index;
  180. } whead = { NULL, NULL }, *witem = NULL;
  181. enum _message_state
  182. {
  183. ST_OUTSIDE,
  184. ST_DIALOG,
  185. ST_WORD,
  186. ST_CREDIT,
  187. ST_LAYOUT
  188. } state = ST_OUTSIDE;
  189. int idx_cnt = 0, msg_cnt = 0, word_cnt = 0, sid, eid = -1;
  190. while (!feof(fp))
  191. {
  192. char *buffer;
  193. if (buffer = PAL_ReadOneLine(temp, MESSAGE_MAX_BUFFER_SIZE, fp))
  194. {
  195. switch(state)
  196. {
  197. case ST_OUTSIDE:
  198. //
  199. // Skip comments starting with '#'
  200. //
  201. if (*buffer && *buffer != '#')
  202. {
  203. if (strncmp(buffer, "[BEGIN MESSAGE]", 15) == 0 &&
  204. sscanf(buffer + 15, "%d", &sid) == 1 && sid > eid)
  205. {
  206. state = ST_DIALOG;
  207. //
  208. // First save values (converted wide string) into a linked list
  209. //
  210. if (head)
  211. {
  212. item->next = (struct _msg_list_entry *)UTIL_malloc(sizeof(struct _msg_list_entry));
  213. item = item->next;
  214. }
  215. else
  216. {
  217. head = (struct _msg_list_entry *)UTIL_malloc(sizeof(struct _msg_list_entry));
  218. item = head;
  219. }
  220. item->value = NULL; item->index = sid;
  221. item->count = 0; item->next = NULL; cur_val = NULL;
  222. if (idx_cnt < item->index) idx_cnt = item->index;
  223. }
  224. else if (strncmp(buffer, "[BEGIN WORDS]", 13) == 0 && !witem)
  225. {
  226. state = ST_WORD;
  227. //
  228. // First save values (converted wide string) into a linked list
  229. //
  230. witem = &whead;
  231. }
  232. else if (strncmp(buffer, "[BEGIN CREDITS]", 15) == 0 && !witem)
  233. {
  234. state = ST_CREDIT;
  235. }
  236. else if (strncmp(buffer, "[BEGIN LAYOUT]", 14) == 0 && !witem)
  237. {
  238. state = ST_LAYOUT;
  239. gConfig.fUseCustomScreenLayout = TRUE;
  240. }
  241. else
  242. {
  243. // Just ignore invalid lines
  244. #ifdef ENABLE_LOG
  245. UTIL_WriteLog(LOG_ERR, "PAL_ReadMessageFile(): encounter invalid line '%s'!\n", line);
  246. #endif
  247. }
  248. }
  249. break;
  250. case ST_DIALOG:
  251. //
  252. // Check if to end one dialog
  253. //
  254. if (strncmp(buffer, "[END MESSAGE]", 13) == 0 &&
  255. sscanf(buffer + 13, "%d", &eid) == 1 && eid >= sid)
  256. {
  257. // End dialog
  258. state = ST_OUTSIDE;
  259. }
  260. else
  261. {
  262. if (cur_val)
  263. {
  264. cur_val->next = (struct _msg_entry *)UTIL_malloc(sizeof(struct _msg_entry));
  265. cur_val = cur_val->next;
  266. }
  267. else
  268. cur_val = (struct _msg_entry *)UTIL_malloc(sizeof(struct _msg_entry));
  269. if (strncmp(buffer, "[CLEAR MESSAGE]", 15) == 0)
  270. {
  271. cur_val->value = NULL;
  272. }
  273. else
  274. {
  275. int len = PAL_MultiByteToWideCharCP(CP_UTF_8, buffer, -1, NULL, 0);
  276. cur_val->value = (wchar_t *)UTIL_malloc(len * sizeof(wchar_t));
  277. PAL_MultiByteToWideCharCP(CP_UTF_8, buffer, -1, cur_val->value, len);
  278. msg_cnt++;
  279. }
  280. if (!item->value) item->value = cur_val;
  281. cur_val->next = NULL; item->count++;
  282. }
  283. break;
  284. case ST_WORD:
  285. //
  286. // Check if to end word list
  287. //
  288. if (strncmp(buffer, "[END WORDS]", 11) == 0)
  289. {
  290. // End word list
  291. state = ST_OUTSIDE;
  292. }
  293. else
  294. {
  295. char *v;
  296. int l, i = PAL_ParseLine(buffer, &v, &l, FALSE);
  297. if (i > 0)
  298. {
  299. int len = PAL_MultiByteToWideCharCP(CP_UTF_8, v, -1, NULL, 0);
  300. struct _word_list_entry *val = (struct _word_list_entry *)UTIL_malloc(sizeof(struct _word_list_entry));
  301. val->value = (wchar_t *)UTIL_malloc(len * sizeof(wchar_t));
  302. PAL_MultiByteToWideCharCP(CP_UTF_8, v, -1, val->value, len);
  303. val->index = i; val->next = NULL;
  304. witem->next = val; witem = witem->next;
  305. if (word_cnt < i) word_cnt = i;
  306. }
  307. }
  308. break;
  309. case ST_CREDIT:
  310. //
  311. // Check if to end credit list
  312. //
  313. if (strncmp(buffer, "[END CREDITS]", 13) == 0)
  314. {
  315. // End credit list
  316. state = ST_OUTSIDE;
  317. }
  318. else
  319. {
  320. char *v;
  321. int l, i = PAL_ParseLine(buffer, &v, &l, FALSE);
  322. if ((i == 1 || (i >= 6 && i <= 11)) && !g_rcCredits[i])
  323. {
  324. int limit = (i == 1) ? 24 * 8 : 40 * 8, w = 0, j = 0, len;
  325. if (i == 6 || i == 7)
  326. {
  327. if (PAL_PLATFORM && PAL_CREDIT && PAL_PORTYEAR)
  328. {
  329. const char *templates[] = { "${platform}", "${author}", "${year}" };
  330. const char *values[] = { PAL_PLATFORM, PAL_CREDIT, PAL_PORTYEAR };
  331. const int matchlen[] = { 11, 9, 7 };
  332. const int valuelen[] = { sizeof(PAL_PLATFORM) - 1, sizeof(PAL_CREDIT) - 1, sizeof(PAL_PORTYEAR) - 1 };
  333. char *tmp = (char *)alloca(valuelen[0] + valuelen[1] + valuelen[2] + l + 1);
  334. char *dst = tmp, *src = v;
  335. while (*src)
  336. {
  337. if (*src == '$')
  338. {
  339. int k;
  340. for (k = 0; k < 3 && strncmp(src, templates[k], matchlen[k]); k++);
  341. if (k < 3)
  342. {
  343. strcpy(dst, values[k]);
  344. dst += valuelen[k];
  345. src += matchlen[k];
  346. continue;
  347. }
  348. }
  349. *dst++ = *src++;
  350. }
  351. *dst = 0;
  352. len = PAL_MultiByteToWideCharCP(CP_UTF_8, tmp, -1, NULL, 0);
  353. g_rcCredits[i] = (wchar_t *)UTIL_malloc(len * sizeof(wchar_t));
  354. PAL_MultiByteToWideCharCP(CP_UTF_8, tmp, -1, g_rcCredits[i], len);
  355. }
  356. }
  357. else
  358. {
  359. len = PAL_MultiByteToWideCharCP(CP_UTF_8, v, -1, NULL, 0);
  360. g_rcCredits[i] = (wchar_t *)UTIL_malloc(len * sizeof(wchar_t));
  361. PAL_MultiByteToWideCharCP(CP_UTF_8, v, -1, g_rcCredits[i], len);
  362. }
  363. if (g_rcCredits[i])
  364. {
  365. // Limit the length of texts
  366. while (w < limit && j < len - 1) w += PAL_CharWidth(g_rcCredits[i][j++]);
  367. if (w >= limit) g_rcCredits[i][w > limit ? j - 1 : j] = 0;
  368. }
  369. }
  370. }
  371. break;
  372. case ST_LAYOUT:
  373. if (strncmp(buffer, "[END LAYOUT]", 12) == 0)
  374. {
  375. // End layout
  376. state = ST_OUTSIDE;
  377. }
  378. else
  379. {
  380. char *v;
  381. int x, y, f, n, l, i = PAL_ParseLine(buffer, &v, &l, FALSE);
  382. if (i >= 1 && i <= (sizeof(SCREENLAYOUT) / sizeof(PAL_POS)))
  383. {
  384. if ((n = sscanf(v, "%d,%d,%d", &x, &y, &f)) >= 2 && x < 320 && y < 200)
  385. {
  386. gConfig.ScreenLayoutArray[i - 1] = PAL_XY(x, y);
  387. if (n == 3) gConfig.ScreenLayoutFlag[i - 1] = f;
  388. }
  389. }
  390. }
  391. break;
  392. default:
  393. TerminateOnError("PAL_ReadMessageFile(): Reached an unknown state. Something really wrong may have happened!");
  394. break;
  395. }
  396. if (buffer != temp) free(buffer);
  397. }
  398. }
  399. if (msg_cnt > 0)
  400. {
  401. //
  402. // Move values from linked list to array
  403. //
  404. int idx_msg = 1;
  405. g_TextLib.nIndices = (idx_cnt += 1);
  406. g_TextLib.nMsgs = (msg_cnt += 1);
  407. g_TextLib.lpIndexBuf = (int **)UTIL_calloc(idx_cnt, sizeof(int *));
  408. g_TextLib.lpMsgBuf = (LPWSTR *)UTIL_calloc(msg_cnt, sizeof(LPWSTR));
  409. for (item = head; item; )
  410. {
  411. struct _msg_list_entry *temp = item->next;
  412. struct _msg_entry *msg = item->value;
  413. int index = 0;
  414. g_TextLib.lpIndexBuf[item->index] = (int *)UTIL_calloc(item->count + 1, sizeof(int));
  415. while (msg)
  416. {
  417. struct _msg_entry *tmp = msg->next;
  418. if (msg->value)
  419. {
  420. g_TextLib.lpIndexBuf[item->index][index++] = idx_msg;
  421. g_TextLib.lpMsgBuf[idx_msg++] = msg->value;
  422. }
  423. else
  424. g_TextLib.lpIndexBuf[item->index][index++] = 0;
  425. free(msg); msg = tmp;
  426. }
  427. g_TextLib.lpIndexBuf[item->index][item->count] = -1;
  428. free(item); item = temp;
  429. }
  430. }
  431. if (word_cnt > 0)
  432. {
  433. //
  434. // Move values from linked list to array
  435. //
  436. #ifndef PAL_CLASSIC
  437. int i;
  438. #endif
  439. if (word_cnt < MINIMAL_WORD_COUNT - 1) word_cnt = MINIMAL_WORD_COUNT - 1;
  440. g_TextLib.nWords = (word_cnt += 1);
  441. g_TextLib.lpWordBuf = (LPWSTR *)UTIL_calloc(word_cnt, sizeof(LPWSTR));
  442. for (witem = whead.next; witem; )
  443. {
  444. struct _word_list_entry *temp = witem->next;
  445. g_TextLib.lpWordBuf[witem->index] = witem->value;
  446. free(witem); witem = temp;
  447. }
  448. #ifndef PAL_CLASSIC
  449. for (i = 1; i < ATB_WORD_COUNT; i++)
  450. if (!g_TextLib.lpWordBuf[i + SYSMENU_LABEL_BATTLEMODE])
  451. g_TextLib.lpWordBuf[i + SYSMENU_LABEL_BATTLEMODE] = gc_rgszDefaultAdditionalWords[i];
  452. #endif
  453. }
  454. fclose(fp);
  455. return (msg_cnt > 0 && word_cnt > 0) ? 1 : 0;
  456. }
  457. INT
  458. PAL_InitText(
  459. VOID
  460. )
  461. /*++
  462. Purpose:
  463. Initialize the in-game texts.
  464. Parameters:
  465. None.
  466. Return value:
  467. 0 = success.
  468. -1 = memory allocation error.
  469. --*/
  470. {
  471. if (gConfig.pszMsgFile)
  472. {
  473. //
  474. // Open the message, index and word data files.
  475. //
  476. FILE *fp = UTIL_OpenRequiredFileForMode(gConfig.pszMsgFile, "r");
  477. //
  478. // Read the contents of the message, index and word data files.
  479. //
  480. if (!PAL_ReadMessageFile(fp))
  481. {
  482. return -1;
  483. }
  484. else
  485. {
  486. DWORD dwWordLength = 0;
  487. int i;
  488. for (i = 1; i < g_TextLib.nWords; i++)
  489. {
  490. if (g_TextLib.lpWordBuf[i])
  491. {
  492. LPWSTR ptr = g_TextLib.lpWordBuf[i];
  493. DWORD n = 0;
  494. while (*ptr) n += PAL_CharWidth(*ptr++) >> 3;
  495. if (dwWordLength < n) dwWordLength = n;
  496. }
  497. }
  498. gConfig.dwWordLength = dwWordLength;
  499. for (i = 0; i < 12; i++)
  500. {
  501. if (!g_rcCredits[i])
  502. g_rcCredits[i] = L"";
  503. }
  504. }
  505. }
  506. else
  507. {
  508. FILE *fpMsg, *fpWord;
  509. DWORD *offsets;
  510. LPWSTR tmp;
  511. LPBYTE temp;
  512. int wpos, wlen, i;
  513. //
  514. // Open the message and word data files.
  515. //
  516. fpMsg = UTIL_OpenRequiredFile("m.msg");
  517. fpWord = UTIL_OpenRequiredFile("word.dat");
  518. //
  519. // See how many words we have
  520. //
  521. fseek(fpWord, 0, SEEK_END);
  522. i = ftell(fpWord);
  523. //
  524. // Each word has 10 bytes
  525. //
  526. g_TextLib.nWords = (i + (gConfig.dwWordLength - 1)) / gConfig.dwWordLength;
  527. if (g_TextLib.nWords < MINIMAL_WORD_COUNT) g_TextLib.nWords = MINIMAL_WORD_COUNT;
  528. //
  529. // Read the words
  530. //
  531. temp = (LPBYTE)malloc(gConfig.dwWordLength * g_TextLib.nWords);
  532. if (temp == NULL)
  533. {
  534. fclose(fpWord);
  535. fclose(fpMsg);
  536. return -1;
  537. }
  538. fseek(fpWord, 0, SEEK_SET);
  539. fread(temp, i, 1, fpWord);
  540. memset(temp + i, 0, gConfig.dwWordLength * g_TextLib.nWords - i);
  541. //
  542. // Close the words file
  543. //
  544. fclose(fpWord);
  545. // Split the words and do code page conversion
  546. for (i = 0, wlen = 0; i < g_TextLib.nWords; i++)
  547. {
  548. int base = i * gConfig.dwWordLength;
  549. int pos = base + gConfig.dwWordLength - 1;
  550. while (pos >= base && temp[pos] == ' ') temp[pos--] = 0;
  551. wlen += PAL_MultiByteToWideChar((LPCSTR)temp + base, gConfig.dwWordLength, NULL, 0) + 1;
  552. }
  553. g_TextLib.lpWordBuf = (LPWSTR*)malloc(g_TextLib.nWords * sizeof(LPWSTR));
  554. if (g_TextLib.lpWordBuf == NULL)
  555. {
  556. free(temp);
  557. fclose(fpWord);
  558. fclose(fpMsg);
  559. return -1;
  560. }
  561. tmp = (LPWSTR)malloc(wlen * sizeof(WCHAR));
  562. if (tmp == NULL)
  563. {
  564. free(g_TextLib.lpWordBuf);
  565. free(temp);
  566. fclose(fpWord);
  567. fclose(fpMsg);
  568. return -1;
  569. }
  570. for (i = 0, wpos = 0; i < g_TextLib.nWords; i++)
  571. {
  572. int l;
  573. g_TextLib.lpWordBuf[i] = tmp + wpos;
  574. l = PAL_MultiByteToWideChar((LPCSTR)temp + i * gConfig.dwWordLength, gConfig.dwWordLength, g_TextLib.lpWordBuf[i], wlen - wpos);
  575. if (l > 0 && g_TextLib.lpWordBuf[i][l - 1] == '1')
  576. g_TextLib.lpWordBuf[i][l - 1] = 0;
  577. g_TextLib.lpWordBuf[i][l] = 0;
  578. wpos += l + 1;
  579. }
  580. free(temp);
  581. //
  582. // Read the message offsets. The message offsets are in SSS.MKF #3
  583. //
  584. i = PAL_MKFGetChunkSize(3, gpGlobals->f.fpSSS) / sizeof(DWORD);
  585. g_TextLib.nMsgs = i - 1;
  586. offsets = (LPDWORD)malloc(i * sizeof(DWORD));
  587. if (offsets == NULL)
  588. {
  589. free(g_TextLib.lpWordBuf);
  590. fclose(fpMsg);
  591. return -1;
  592. }
  593. PAL_MKFReadChunk((LPBYTE)(offsets), i * sizeof(DWORD), 3, gpGlobals->f.fpSSS);
  594. //
  595. // Read the messages.
  596. //
  597. fseek(fpMsg, 0, SEEK_END);
  598. i = ftell(fpMsg);
  599. temp = (LPBYTE)malloc(i);
  600. if (temp == NULL)
  601. {
  602. free(offsets);
  603. free(g_TextLib.lpWordBuf[0]);
  604. free(g_TextLib.lpWordBuf);
  605. fclose(fpMsg);
  606. return -1;
  607. }
  608. fseek(fpMsg, 0, SEEK_SET);
  609. fread(temp, 1, i, fpMsg);
  610. fclose(fpMsg);
  611. // Split messages and do code page conversion here
  612. for (i = 0, wlen = 0; i < g_TextLib.nMsgs; i++)
  613. {
  614. wlen += PAL_MultiByteToWideChar((LPCSTR)temp + SDL_SwapLE32(offsets[i]), SDL_SwapLE32(offsets[i + 1]) - SDL_SwapLE32(offsets[i]), NULL, 0) + 1;
  615. }
  616. g_TextLib.lpMsgBuf = (LPWSTR*)malloc(g_TextLib.nMsgs * sizeof(LPWSTR));
  617. if (g_TextLib.lpMsgBuf == NULL)
  618. {
  619. free(g_TextLib.lpWordBuf);
  620. free(offsets);
  621. return -1;
  622. }
  623. tmp = (LPWSTR)malloc(wlen * sizeof(WCHAR));
  624. if (tmp == NULL)
  625. {
  626. free(g_TextLib.lpMsgBuf);
  627. free(g_TextLib.lpWordBuf);
  628. free(offsets);
  629. return -1;
  630. }
  631. for (i = 0, wpos = 0; i < g_TextLib.nMsgs; i++)
  632. {
  633. int l;
  634. g_TextLib.lpMsgBuf[i] = tmp + wpos;
  635. l = PAL_MultiByteToWideChar((LPCSTR)temp + SDL_SwapLE32(offsets[i]), SDL_SwapLE32(offsets[i + 1]) - SDL_SwapLE32(offsets[i]), g_TextLib.lpMsgBuf[i], wlen - wpos);
  636. g_TextLib.lpMsgBuf[i][l] = 0;
  637. wpos += l + 1;
  638. }
  639. free(temp);
  640. free(offsets);
  641. g_TextLib.lpIndexBuf = NULL;
  642. memcpy(g_TextLib.lpWordBuf + SYSMENU_LABEL_LAUNCHSETTING, gc_rgszSDLPalWords[gConfig.uCodePage], SDLPAL_EXTRA_WORD_COUNT * sizeof(LPCWSTR));
  643. #ifndef PAL_CLASSIC
  644. memcpy(g_TextLib.lpWordBuf + SYSMENU_LABEL_BATTLEMODE, gc_rgszAdditionalWords[gConfig.uCodePage], ATB_WORD_COUNT * sizeof(LPCWSTR));
  645. #endif
  646. }
  647. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  648. g_TextLib.bIcon = 0;
  649. g_TextLib.posIcon = 0;
  650. g_TextLib.nCurrentDialogLine = 0;
  651. g_TextLib.iDelayTime = 3;
  652. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  653. g_TextLib.posDialogText = PAL_XY(44, 26);
  654. g_TextLib.bDialogPosition = kDialogUpper;
  655. g_TextLib.fUserSkip = FALSE;
  656. PAL_MKFReadChunk(g_TextLib.bufDialogIcons, 282, 12, gpGlobals->f.fpDATA);
  657. return 0;
  658. }
  659. VOID
  660. PAL_FreeText(
  661. VOID
  662. )
  663. /*++
  664. Purpose:
  665. Free the memory used by the texts.
  666. Parameters:
  667. None.
  668. Return value:
  669. None.
  670. --*/
  671. {
  672. int i;
  673. if (g_TextLib.lpMsgBuf != NULL)
  674. {
  675. if (gConfig.pszMsgFile)
  676. for(i = 0; i < g_TextLib.nMsgs; i++) free(g_TextLib.lpMsgBuf[i]);
  677. else
  678. free(g_TextLib.lpMsgBuf[0]);
  679. free(g_TextLib.lpMsgBuf);
  680. g_TextLib.lpMsgBuf = NULL;
  681. }
  682. if (g_TextLib.lpWordBuf != NULL)
  683. {
  684. if (gConfig.pszMsgFile)
  685. for(i = 0; i < g_TextLib.nWords; i++) free(g_TextLib.lpWordBuf[i]);
  686. else
  687. free(g_TextLib.lpWordBuf[0]);
  688. free(g_TextLib.lpWordBuf);
  689. g_TextLib.lpWordBuf = NULL;
  690. }
  691. if (g_TextLib.lpIndexBuf != NULL)
  692. {
  693. if (gConfig.pszMsgFile)
  694. for(i = 0; i < g_TextLib.nIndices; i++) free(g_TextLib.lpIndexBuf[i]);
  695. else
  696. free(g_TextLib.lpIndexBuf[0]);
  697. free(g_TextLib.lpIndexBuf);
  698. g_TextLib.lpIndexBuf = NULL;
  699. }
  700. }
  701. LPCWSTR
  702. PAL_GetWord(
  703. int iNumWord
  704. )
  705. /*++
  706. Purpose:
  707. Get the specified word.
  708. Parameters:
  709. [IN] wNumWord - the number of the requested word.
  710. Return value:
  711. Pointer to the requested word. NULL if not found.
  712. --*/
  713. {
  714. return (iNumWord >= g_TextLib.nWords || !g_TextLib.lpWordBuf[iNumWord]) ? L"" : g_TextLib.lpWordBuf[iNumWord];
  715. }
  716. LPCWSTR
  717. PAL_GetMsg(
  718. int iNumMsg
  719. )
  720. /*++
  721. Purpose:
  722. Get the specified message.
  723. Parameters:
  724. [IN] wNumMsg - the number of the requested message.
  725. Return value:
  726. Pointer to the requested message. NULL if not found.
  727. --*/
  728. {
  729. return (iNumMsg >= g_TextLib.nMsgs || !g_TextLib.lpMsgBuf[iNumMsg]) ? L"" : g_TextLib.lpMsgBuf[iNumMsg];
  730. }
  731. int
  732. PAL_GetMsgNum(
  733. int iIndex,
  734. int iOrder
  735. )
  736. /*++
  737. Purpose:
  738. Get the number of specified message from index & order.
  739. Parameters:
  740. [IN] iMsgIndex - index.
  741. [IN] iOrder - order inside the index.
  742. Return value:
  743. The number of message. Zero means pausing for key, and -1 means end.
  744. --*/
  745. {
  746. return (iIndex >= g_TextLib.nMsgs || !g_TextLib.lpIndexBuf[iIndex]) ? -1 : g_TextLib.lpIndexBuf[iIndex][iOrder];
  747. }
  748. VOID
  749. PAL_DrawText(
  750. LPCWSTR lpszText,
  751. PAL_POS pos,
  752. BYTE bColor,
  753. BOOL fShadow,
  754. BOOL fUpdate,
  755. BOOL fUse8x8Font
  756. )
  757. /*++
  758. Purpose:
  759. Draw text on the screen.
  760. Parameters:
  761. [IN] lpszText - the text to be drawn.
  762. [IN] pos - Position of the text.
  763. [IN] bColor - Color of the text.
  764. [IN] fShadow - TRUE if the text is shadowed or not.
  765. [IN] fUpdate - TRUE if update the screen area.
  766. [IN] fUse8x8Font - TRUE if use 8x8 font.
  767. Return value:
  768. None.
  769. --*/
  770. {
  771. SDL_Rect rect, urect;
  772. urect.x = rect.x = PAL_X(pos);
  773. urect.y = rect.y = PAL_Y(pos);
  774. urect.h = (fUse8x8Font ? 8 : PAL_FontHeight()) + (fShadow ? 1 : 0);
  775. urect.w = 0;
  776. while (*lpszText)
  777. {
  778. //
  779. // Draw the character
  780. //
  781. int char_width = fUse8x8Font ? 8 : PAL_CharWidth(*lpszText);
  782. if (fShadow)
  783. {
  784. PAL_DrawCharOnSurface(*lpszText, gpScreen, PAL_XY(rect.x + 1, rect.y + 1), 0, fUse8x8Font);
  785. PAL_DrawCharOnSurface(*lpszText, gpScreen, PAL_XY(rect.x + 1, rect.y), 0, fUse8x8Font);
  786. }
  787. PAL_DrawCharOnSurface(*lpszText++, gpScreen, PAL_XY(rect.x, rect.y), bColor, fUse8x8Font);
  788. rect.x += char_width; urect.w += char_width;
  789. }
  790. //
  791. // Update the screen area
  792. //
  793. if (fUpdate && urect.w > 0)
  794. {
  795. if (fShadow) urect.w++;
  796. if (urect.x + urect.w > 320)
  797. {
  798. urect.w = 320 - urect.x;
  799. }
  800. VIDEO_UpdateScreen(&urect);
  801. }
  802. }
  803. VOID
  804. PAL_DialogSetDelayTime(
  805. INT iDelayTime
  806. )
  807. /*++
  808. Purpose:
  809. Set the delay time for dialog.
  810. Parameters:
  811. [IN] iDelayTime - the delay time to be set.
  812. Return value:
  813. None.
  814. --*/
  815. {
  816. g_TextLib.iDelayTime = iDelayTime;
  817. }
  818. VOID
  819. PAL_StartDialog(
  820. BYTE bDialogLocation,
  821. BYTE bFontColor,
  822. INT iNumCharFace,
  823. BOOL fPlayingRNG
  824. )
  825. /*++
  826. Purpose:
  827. Start a new dialog.
  828. Parameters:
  829. [IN] bDialogLocation - the location of the text on the screen.
  830. [IN] bFontColor - the font color of the text.
  831. [IN] iNumCharFace - number of the character face in RGM.MKF.
  832. [IN] fPlayingRNG - whether we are playing a RNG video or not.
  833. Return value:
  834. None.
  835. --*/
  836. {
  837. PAL_LARGE BYTE buf[16384];
  838. SDL_Rect rect;
  839. if (gpGlobals->fInBattle && !g_fUpdatedInBattle)
  840. {
  841. //
  842. // Update the screen in battle, or the graphics may seem messed up
  843. //
  844. VIDEO_UpdateScreen(NULL);
  845. g_fUpdatedInBattle = TRUE;
  846. }
  847. g_TextLib.bIcon = 0;
  848. g_TextLib.posIcon = 0;
  849. g_TextLib.nCurrentDialogLine = 0;
  850. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  851. g_TextLib.fUserSkip = FALSE;
  852. if (bFontColor != 0)
  853. {
  854. g_TextLib.bCurrentFontColor = bFontColor;
  855. }
  856. if (fPlayingRNG && iNumCharFace)
  857. {
  858. VIDEO_BackupScreen(gpScreen);
  859. g_TextLib.fPlayingRNG = TRUE;
  860. }
  861. switch (bDialogLocation)
  862. {
  863. case kDialogUpper:
  864. if (iNumCharFace > 0)
  865. {
  866. //
  867. // Display the character face at the upper part of the screen
  868. //
  869. if (PAL_MKFReadChunk(buf, 16384, iNumCharFace, gpGlobals->f.fpRGM) > 0)
  870. {
  871. rect.w = PAL_RLEGetWidth((LPCBITMAPRLE)buf);
  872. rect.h = PAL_RLEGetHeight((LPCBITMAPRLE)buf);
  873. rect.x = 48 - rect.w / 2;
  874. rect.y = 55 - rect.h / 2;
  875. if (rect.x < 0)
  876. {
  877. rect.x = 0;
  878. }
  879. if (rect.y < 0)
  880. {
  881. rect.y = 0;
  882. }
  883. PAL_RLEBlitToSurface((LPCBITMAPRLE)buf, gpScreen, PAL_XY(rect.x, rect.y));
  884. if (rect.x < 0)
  885. {
  886. rect.x = 0;
  887. }
  888. if (rect.y < 0)
  889. {
  890. rect.y = 0;
  891. }
  892. VIDEO_UpdateScreen(&rect);
  893. }
  894. }
  895. g_TextLib.posDialogTitle = PAL_XY(iNumCharFace > 0 ? 80 : 12, 8);
  896. g_TextLib.posDialogText = PAL_XY(iNumCharFace > 0 ? 96 : 44, 26);
  897. break;
  898. case kDialogCenter:
  899. g_TextLib.posDialogText = PAL_XY(80, 40);
  900. break;
  901. case kDialogLower:
  902. if (iNumCharFace > 0)
  903. {
  904. //
  905. // Display the character face at the lower part of the screen
  906. //
  907. if (PAL_MKFReadChunk(buf, 16384, iNumCharFace, gpGlobals->f.fpRGM) > 0)
  908. {
  909. rect.x = 270 - PAL_RLEGetWidth((LPCBITMAPRLE)buf) / 2;
  910. rect.y = 144 - PAL_RLEGetHeight((LPCBITMAPRLE)buf) / 2;
  911. PAL_RLEBlitToSurface((LPCBITMAPRLE)buf, gpScreen, PAL_XY(rect.x, rect.y));
  912. VIDEO_UpdateScreen(NULL);
  913. }
  914. }
  915. g_TextLib.posDialogTitle = PAL_XY(iNumCharFace > 0 ? 4 : 12, 108);
  916. g_TextLib.posDialogText = PAL_XY(iNumCharFace > 0 ? 20 : 44, 126);
  917. break;
  918. case kDialogCenterWindow:
  919. g_TextLib.posDialogText = PAL_XY(160, 40);
  920. break;
  921. }
  922. g_TextLib.bDialogPosition = bDialogLocation;
  923. }
  924. static VOID
  925. PAL_DialogWaitForKey(
  926. VOID
  927. )
  928. /*++
  929. Purpose:
  930. Wait for player to press a key after showing a dialog.
  931. Parameters:
  932. None.
  933. Return value:
  934. None.
  935. --*/
  936. {
  937. PAL_LARGE SDL_Color palette[256];
  938. SDL_Color *pCurrentPalette, t;
  939. int i;
  940. //
  941. // get the current palette
  942. //
  943. pCurrentPalette = PAL_GetPalette(gpGlobals->wNumPalette, gpGlobals->fNightPalette);
  944. memcpy(palette, pCurrentPalette, sizeof(palette));
  945. if (g_TextLib.bDialogPosition != kDialogCenterWindow &&
  946. g_TextLib.bDialogPosition != kDialogCenter)
  947. {
  948. //
  949. // show the icon
  950. //
  951. LPCBITMAPRLE p = PAL_SpriteGetFrame(g_TextLib.bufDialogIcons, g_TextLib.bIcon);
  952. if (p != NULL)
  953. {
  954. SDL_Rect rect;
  955. rect.x = PAL_X(g_TextLib.posIcon);
  956. rect.y = PAL_Y(g_TextLib.posIcon);
  957. rect.w = 16;
  958. rect.h = 16;
  959. PAL_RLEBlitToSurface(p, gpScreen, g_TextLib.posIcon);
  960. VIDEO_UpdateScreen(&rect);
  961. }
  962. }
  963. PAL_ClearKeyState();
  964. while (TRUE)
  965. {
  966. UTIL_Delay(100);
  967. if (g_TextLib.bDialogPosition != kDialogCenterWindow &&
  968. g_TextLib.bDialogPosition != kDialogCenter)
  969. {
  970. //
  971. // palette shift
  972. //
  973. t = palette[0xF9];
  974. for (i = 0xF9; i < 0xFE; i++)
  975. {
  976. palette[i] = palette[i + 1];
  977. }
  978. palette[0xFE] = t;
  979. VIDEO_SetPalette(palette);
  980. }
  981. if (g_InputState.dwKeyPress != 0)
  982. {
  983. break;
  984. }
  985. }
  986. if (g_TextLib.bDialogPosition != kDialogCenterWindow &&
  987. g_TextLib.bDialogPosition != kDialogCenter)
  988. {
  989. PAL_SetPalette(gpGlobals->wNumPalette, gpGlobals->fNightPalette);
  990. }
  991. PAL_ClearKeyState();
  992. g_TextLib.fUserSkip = FALSE;
  993. }
  994. VOID
  995. PAL_ShowDialogText(
  996. LPCWSTR lpszText
  997. )
  998. /*++
  999. Purpose:
  1000. Show one line of the dialog text.
  1001. Parameters:
  1002. [IN] lpszText - the text to be shown.
  1003. Return value:
  1004. None.
  1005. --*/
  1006. {
  1007. SDL_Rect rect;
  1008. int x, y;
  1009. PAL_ClearKeyState();
  1010. g_TextLib.bIcon = 0;
  1011. if (gpGlobals->fInBattle && !g_fUpdatedInBattle)
  1012. {
  1013. //
  1014. // Update the screen in battle, or the graphics may seem messed up
  1015. //
  1016. VIDEO_UpdateScreen(NULL);
  1017. g_fUpdatedInBattle = TRUE;
  1018. }
  1019. if (g_TextLib.nCurrentDialogLine > 3)
  1020. {
  1021. //
  1022. // The rest dialogs should be shown in the next page.
  1023. //
  1024. PAL_DialogWaitForKey();
  1025. g_TextLib.nCurrentDialogLine = 0;
  1026. VIDEO_RestoreScreen(gpScreen);
  1027. VIDEO_UpdateScreen(NULL);
  1028. }
  1029. x = PAL_X(g_TextLib.posDialogText);
  1030. y = PAL_Y(g_TextLib.posDialogText) + g_TextLib.nCurrentDialogLine * 18;
  1031. if (g_TextLib.bDialogPosition == kDialogCenterWindow)
  1032. {
  1033. //
  1034. // The text should be shown in a small window at the center of the screen
  1035. //
  1036. #ifndef PAL_CLASSIC
  1037. if (gpGlobals->fInBattle && g_Battle.BattleResult == kBattleResultOnGoing)
  1038. {
  1039. PAL_BattleUIShowText(lpszText, 1400);
  1040. }
  1041. else
  1042. #endif
  1043. {
  1044. PAL_POS pos;
  1045. LPBOX lpBox;
  1046. int i, w = wcslen(lpszText), len = 0;
  1047. for (i = 0; i < w; i++)
  1048. len += PAL_CharWidth(lpszText[i]) >> 3;
  1049. //
  1050. // Create the window box
  1051. //
  1052. pos = PAL_XY(PAL_X(g_TextLib.posDialogText) - len * 4, PAL_Y(g_TextLib.posDialogText));
  1053. // Follow behavior of original version
  1054. lpBox = PAL_CreateSingleLineBoxWithShadow(pos, (len + 1) / 2, FALSE, 0);
  1055. rect.x = PAL_X(pos);
  1056. rect.y = PAL_Y(pos);
  1057. rect.w = 320 - rect.x * 2 + 32;
  1058. rect.h = 64;
  1059. //
  1060. // Show the text on the screen
  1061. //
  1062. pos = PAL_XY(PAL_X(pos) + 8 + ((len & 1) << 2), PAL_Y(pos) + 10);
  1063. PAL_DrawText(lpszText, pos, 0, FALSE, FALSE, FALSE);
  1064. VIDEO_UpdateScreen(&rect);
  1065. PAL_DialogWaitForKey();
  1066. //
  1067. // Delete the box
  1068. //
  1069. PAL_DeleteBox(lpBox);
  1070. VIDEO_UpdateScreen(&rect);
  1071. PAL_EndDialog();
  1072. }
  1073. }
  1074. else
  1075. {
  1076. int len = wcslen(lpszText);
  1077. if (g_TextLib.nCurrentDialogLine == 0 &&
  1078. g_TextLib.bDialogPosition != kDialogCenter &&
  1079. (lpszText[len - 1] == 0xff1a ||
  1080. lpszText[len - 1] == 0x2236 || // Special case for Pal WIN95 Simplified Chinese version
  1081. lpszText[len - 1] == ':')
  1082. )
  1083. {
  1084. //
  1085. // name of character
  1086. //
  1087. PAL_DrawText(lpszText, g_TextLib.posDialogTitle, FONT_COLOR_CYAN_ALT, TRUE, TRUE, FALSE);
  1088. }
  1089. else
  1090. {
  1091. //
  1092. // normal texts
  1093. //
  1094. WCHAR text[2];
  1095. if (!g_TextLib.fPlayingRNG && g_TextLib.nCurrentDialogLine == 0)
  1096. {
  1097. //
  1098. // Save the screen before we show the first line of dialog
  1099. //
  1100. VIDEO_BackupScreen(gpScreen);
  1101. }
  1102. while (lpszText != NULL && *lpszText != '\0')
  1103. {
  1104. switch (*lpszText)
  1105. {
  1106. case '-':
  1107. //
  1108. // Set the font color to Cyan
  1109. //
  1110. if (g_TextLib.bCurrentFontColor == FONT_COLOR_CYAN)
  1111. {
  1112. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1113. }
  1114. else
  1115. {
  1116. g_TextLib.bCurrentFontColor = FONT_COLOR_CYAN;
  1117. }
  1118. lpszText++;
  1119. break;
  1120. #if 0
  1121. /* Not used */
  1122. case '\'':
  1123. //
  1124. // Set the font color to Red
  1125. //
  1126. if (g_TextLib.bCurrentFontColor == FONT_COLOR_RED)
  1127. {
  1128. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1129. }
  1130. else
  1131. {
  1132. g_TextLib.bCurrentFontColor = FONT_COLOR_RED;
  1133. }
  1134. lpszText++;
  1135. break;
  1136. #endif
  1137. case '\"':
  1138. //
  1139. // Set the font color to Yellow
  1140. //
  1141. if (g_TextLib.bCurrentFontColor == FONT_COLOR_YELLOW)
  1142. {
  1143. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1144. }
  1145. else
  1146. {
  1147. g_TextLib.bCurrentFontColor = FONT_COLOR_YELLOW;
  1148. }
  1149. lpszText++;
  1150. break;
  1151. case '$':
  1152. //
  1153. // Set the delay time of text-displaying
  1154. //
  1155. g_TextLib.iDelayTime = wcstol(lpszText + 1, NULL, 10) * 10 / 7;
  1156. lpszText += 3;
  1157. break;
  1158. case '~':
  1159. //
  1160. // Delay for a period and quit
  1161. //
  1162. UTIL_Delay(wcstol(lpszText + 1, NULL, 10) * 80 / 7);
  1163. g_TextLib.nCurrentDialogLine = 0;
  1164. g_TextLib.fUserSkip = FALSE;
  1165. return; // don't go further
  1166. case ')':
  1167. //
  1168. // Set the waiting icon
  1169. //
  1170. g_TextLib.bIcon = 1;
  1171. lpszText++;
  1172. break;
  1173. case '(':
  1174. //
  1175. // Set the waiting icon
  1176. //
  1177. g_TextLib.bIcon = 2;
  1178. lpszText++;
  1179. break;
  1180. case '\\':
  1181. lpszText++;
  1182. default:
  1183. text[0] = *lpszText++;
  1184. text[1] = 0;
  1185. PAL_DrawText(text, PAL_XY(x, y), g_TextLib.bCurrentFontColor, TRUE, TRUE, FALSE);
  1186. x += PAL_CharWidth(text[0]);
  1187. if (!g_TextLib.fUserSkip)
  1188. {
  1189. PAL_ClearKeyState();
  1190. UTIL_Delay(g_TextLib.iDelayTime * 8);
  1191. if (g_InputState.dwKeyPress & (kKeySearch | kKeyMenu))
  1192. {
  1193. //
  1194. // User pressed a key to skip the dialog
  1195. //
  1196. g_TextLib.fUserSkip = TRUE;
  1197. }
  1198. }
  1199. }
  1200. }
  1201. g_TextLib.posIcon = PAL_XY(x, y);
  1202. g_TextLib.nCurrentDialogLine++;
  1203. }
  1204. }
  1205. }
  1206. VOID
  1207. PAL_ClearDialog(
  1208. BOOL fWaitForKey
  1209. )
  1210. /*++
  1211. Purpose:
  1212. Clear the state of the dialog.
  1213. Parameters:
  1214. [IN] fWaitForKey - whether wait for any key or not.
  1215. Return value:
  1216. None.
  1217. --*/
  1218. {
  1219. if (g_TextLib.nCurrentDialogLine > 0 && fWaitForKey)
  1220. {
  1221. PAL_DialogWaitForKey();
  1222. }
  1223. g_TextLib.nCurrentDialogLine = 0;
  1224. if (g_TextLib.bDialogPosition == kDialogCenter)
  1225. {
  1226. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  1227. g_TextLib.posDialogText = PAL_XY(44, 26);
  1228. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1229. g_TextLib.bDialogPosition = kDialogUpper;
  1230. }
  1231. }
  1232. VOID
  1233. PAL_EndDialog(
  1234. VOID
  1235. )
  1236. /*++
  1237. Purpose:
  1238. Ends a dialog.
  1239. Parameters:
  1240. None.
  1241. Return value:
  1242. None.
  1243. --*/
  1244. {
  1245. PAL_ClearDialog(TRUE);
  1246. //
  1247. // Set some default parameters, as there are some parts of script
  1248. // which doesn't have a "start dialog" instruction before showing the dialog.
  1249. //
  1250. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  1251. g_TextLib.posDialogText = PAL_XY(44, 26);
  1252. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1253. g_TextLib.bDialogPosition = kDialogUpper;
  1254. g_TextLib.fUserSkip = FALSE;
  1255. g_TextLib.fPlayingRNG = FALSE;
  1256. }
  1257. BOOL
  1258. PAL_IsInDialog(
  1259. VOID
  1260. )
  1261. /*++
  1262. Purpose:
  1263. Check if there are dialog texts on the screen.
  1264. Parameters:
  1265. None.
  1266. Return value:
  1267. TRUE if there are dialog texts on the screen, FALSE if not.
  1268. --*/
  1269. {
  1270. return (g_TextLib.nCurrentDialogLine != 0);
  1271. }
  1272. BOOL
  1273. PAL_DialogIsPlayingRNG(
  1274. VOID
  1275. )
  1276. /*++
  1277. Purpose:
  1278. Check if the script used the RNG playing parameter when displaying texts.
  1279. Parameters:
  1280. None.
  1281. Return value:
  1282. TRUE if the script used the RNG playing parameter, FALSE if not.
  1283. --*/
  1284. {
  1285. return g_TextLib.fPlayingRNG;
  1286. }
  1287. INT
  1288. PAL_MultiByteToWideCharCP(
  1289. CODEPAGE cp,
  1290. LPCSTR mbs,
  1291. int mbslength,
  1292. LPWSTR wcs,
  1293. int wcslength
  1294. )
  1295. /*++
  1296. Purpose:
  1297. Convert multi-byte string into the corresponding unicode string.
  1298. Parameters:
  1299. [IN] cp - Code page for conversion.
  1300. [IN] mbs - Pointer to the multi-byte string.
  1301. [IN] mbslength - Length of the multi-byte string, or -1 for auto-detect.
  1302. [IN] wcs - Pointer to the wide string buffer.
  1303. [IN] wcslength - Length of the wide string buffer.
  1304. Return value:
  1305. The length of converted wide string. If mbslength is set to -1, the returned
  1306. value includes the terminal null-char; otherwise, the null-char is not included.
  1307. If wcslength is set to 0, wcs can be set to NULL and the return value is the
  1308. required length of the wide string buffer.
  1309. --*/
  1310. {
  1311. int i = 0, state = 0, wlen = 0, null = 0;
  1312. if (mbslength == -1)
  1313. {
  1314. mbslength = strlen(mbs);
  1315. null = 1;
  1316. }
  1317. if (!wcs)
  1318. {
  1319. switch (cp)
  1320. {
  1321. //case CP_SHIFTJIS:
  1322. // for (i = 0; i < mbslength && mbs[i]; i++)
  1323. // {
  1324. // if (state == 0)
  1325. // {
  1326. // if ((BYTE)mbs[i] <= 0x80 || (BYTE)mbs[i] >= 0xfd || ((BYTE)mbs[i] >= 0xa0 && (BYTE)mbs[i] <= 0xdf))
  1327. // wlen++;
  1328. // else
  1329. // state = 1;
  1330. // }
  1331. // else
  1332. // {
  1333. // wlen++;
  1334. // state = 0;
  1335. // }
  1336. // }
  1337. // break;
  1338. case CP_GBK:
  1339. case CP_BIG5:
  1340. for (i = 0; i < mbslength && mbs[i]; i++)
  1341. {
  1342. if (state == 0)
  1343. {
  1344. if ((BYTE)mbs[i] <= 0x80 || (BYTE)mbs[i] == 0xff)
  1345. wlen++;
  1346. else
  1347. state = 1;
  1348. }
  1349. else
  1350. {
  1351. wlen++;
  1352. state = 0;
  1353. }
  1354. }
  1355. break;
  1356. case CP_UTF_8:
  1357. for (i = 0; i < mbslength && mbs[i]; i++)
  1358. {
  1359. if (state == 0)
  1360. {
  1361. if ((BYTE)mbs[i] >= 0x80)
  1362. {
  1363. BYTE s = (BYTE)mbs[i] << 1;
  1364. while (s >= 0x80) { state++; s <<= 1; }
  1365. if (state < 1 || state > 3)
  1366. {
  1367. state = 0;
  1368. wlen++;
  1369. }
  1370. }
  1371. else
  1372. wlen++;
  1373. }
  1374. else
  1375. {
  1376. if ((BYTE)mbs[i] >= 0x80 && (BYTE)mbs[i] < 0xc0)
  1377. {
  1378. if (--state == 0) wlen++;
  1379. }
  1380. else
  1381. {
  1382. state = 0; wlen++;
  1383. }
  1384. }
  1385. }
  1386. break;
  1387. default:
  1388. return -1;
  1389. }
  1390. if (i < mbslength && !mbs[i]) null = 1;
  1391. return wlen + null + (state != 0);
  1392. }
  1393. else
  1394. {
  1395. WCHAR invalid_char;
  1396. switch (cp)
  1397. {
  1398. //case CP_SHIFTJIS:
  1399. // invalid_char = 0x30fb;
  1400. // for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1401. // {
  1402. // if (state == 0)
  1403. // {
  1404. // if ((BYTE)mbs[i] <= 0x80)
  1405. // wcs[wlen++] = mbs[i];
  1406. // else if ((BYTE)mbs[i] >= 0xa0 && (BYTE)mbs[i] <= 0xdf)
  1407. // wcs[wlen++] = cptbl_jis_half[(BYTE)mbs[i] - 0xa0];
  1408. // else if ((BYTE)mbs[i] == 0xfd)
  1409. // wcs[wlen++] = 0xf8f1;
  1410. // else if ((BYTE)mbs[i] == 0xfe)
  1411. // wcs[wlen++] = 0xf8f2;
  1412. // else if ((BYTE)mbs[i] == 0xff)
  1413. // wcs[wlen++] = 0xf8f3;
  1414. // else
  1415. // state = 1;
  1416. // }
  1417. // else
  1418. // {
  1419. // if ((BYTE)mbs[i] < 0x40)
  1420. // wcs[wlen++] = 0x30fb;
  1421. // else if ((BYTE)mbs[i - 1] < 0xa0)
  1422. // wcs[wlen++] = cptbl_jis[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  1423. // else
  1424. // wcs[wlen++] = cptbl_jis[(BYTE)mbs[i - 1] - 0xc1][(BYTE)mbs[i] - 0x40];
  1425. // state = 0;
  1426. // }
  1427. // }
  1428. // break;
  1429. case CP_GBK:
  1430. invalid_char = 0x3f;
  1431. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1432. {
  1433. if (state == 0)
  1434. {
  1435. if ((BYTE)mbs[i] < 0x80)
  1436. wcs[wlen++] = mbs[i];
  1437. else if ((BYTE)mbs[i] == 0x80)
  1438. wcs[wlen++] = 0x20ac;
  1439. else if ((BYTE)mbs[i] == 0xff)
  1440. wcs[wlen++] = 0xf8f5;
  1441. else
  1442. state = 1;
  1443. }
  1444. else
  1445. {
  1446. if ((BYTE)mbs[i] < 0x40)
  1447. wcs[wlen++] = invalid_char;
  1448. else
  1449. wcs[wlen++] = cptbl_gbk[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  1450. state = 0;
  1451. }
  1452. }
  1453. break;
  1454. case CP_BIG5:
  1455. invalid_char = 0x3f;
  1456. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1457. {
  1458. if (state == 0)
  1459. {
  1460. if ((BYTE)mbs[i] <= 0x80)
  1461. wcs[wlen++] = mbs[i];
  1462. else if ((BYTE)mbs[i] == 0xff)
  1463. wcs[wlen++] = 0xf8f8;
  1464. else
  1465. state = 1;
  1466. }
  1467. else
  1468. {
  1469. if ((BYTE)mbs[i] < 0x40 || ((BYTE)mbs[i] >= 0x7f && (BYTE)mbs[i] <= 0xa0))
  1470. wcs[wlen++] = invalid_char;
  1471. else if ((BYTE)mbs[i] <= 0x7e)
  1472. wcs[wlen++] = cptbl_big5[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  1473. else
  1474. wcs[wlen++] = cptbl_big5[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x60];
  1475. state = 0;
  1476. }
  1477. }
  1478. break;
  1479. case CP_UTF_8:
  1480. invalid_char = 0x3f;
  1481. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1482. {
  1483. if (state == 0)
  1484. {
  1485. if ((BYTE)mbs[i] >= 0x80)
  1486. {
  1487. BYTE s = (BYTE)mbs[i] << 1;
  1488. while (s >= 0x80) { state++; s <<= 1; }
  1489. if (state < 1 || state > 3)
  1490. {
  1491. state = 0;
  1492. wcs[wlen++] = invalid_char;
  1493. }
  1494. else
  1495. {
  1496. wcs[wlen] = s >> (state + 1);
  1497. }
  1498. }
  1499. else
  1500. wcs[wlen++] = mbs[i];
  1501. }
  1502. else
  1503. {
  1504. if ((BYTE)mbs[i] >= 0x80 && (BYTE)mbs[i] < 0xc0)
  1505. {
  1506. wcs[wlen] <<= 6;
  1507. wcs[wlen] |= (BYTE)mbs[i] & 0x3f;
  1508. if (--state == 0) wlen++;
  1509. }
  1510. else
  1511. {
  1512. state = 0;
  1513. wcs[wlen++] = invalid_char;
  1514. }
  1515. }
  1516. }
  1517. break;
  1518. default:
  1519. return -1;
  1520. }
  1521. if (state != 0 && wlen < wcslength)
  1522. {
  1523. wcs[wlen++] = invalid_char;
  1524. }
  1525. if (null || (i < mbslength && !mbs[i]))
  1526. {
  1527. if (wlen < wcslength)
  1528. wcs[wlen++] = 0;
  1529. else
  1530. wcs[wlen - 1] = 0;
  1531. }
  1532. return wlen;
  1533. }
  1534. }
  1535. INT
  1536. PAL_MultiByteToWideChar(
  1537. LPCSTR mbs,
  1538. int mbslength,
  1539. LPWSTR wcs,
  1540. int wcslength
  1541. )
  1542. /*++
  1543. Purpose:
  1544. Convert multi-byte string into the corresponding unicode string.
  1545. Parameters:
  1546. [IN] mbs - Pointer to the multi-byte string.
  1547. [IN] mbslength - Length of the multi-byte string, or -1 for auto-detect.
  1548. [IN] wcs - Pointer to the wide string buffer.
  1549. [IN] wcslength - Length of the wide string buffer.
  1550. Return value:
  1551. The length of converted wide string. If mbslength is set to -1, the returned
  1552. value includes the terminal null-char; otherwise, the null-char is not included.
  1553. If wcslength is set to 0, wcs can be set to NULL and the return value is the
  1554. required length of the wide string buffer.
  1555. --*/
  1556. {
  1557. return PAL_MultiByteToWideCharCP(gConfig.uCodePage, mbs, mbslength, wcs, wcslength);
  1558. }
  1559. INT
  1560. PAL_swprintf(
  1561. LPWSTR buffer,
  1562. size_t count,
  1563. LPCWSTR format,
  1564. ...
  1565. )
  1566. /*++
  1567. Purpose:
  1568. Formatted wide-character output conversion that output Chinese characters correctly.
  1569. This function supported a subset of format strings that are commonly supported by
  1570. various C libraries, which can be formalized as following:
  1571. %[flags] [width] [.precision] [{h | l | ll}] type
  1572. When handling '%c' and '%s', this function follows the Linux's library convention,
  1573. which means '%c' and '%s' always output multi-byte strings, and '%lc' and '%ls'
  1574. always output wide-char strings.
  1575. Parameters:
  1576. [IN] buffer - Storage location for output.
  1577. [IN] count - Length of the output buffer in characters including the termination null one.
  1578. [IN] format - Format-control string.
  1579. [IN] ... - Optional arguments.
  1580. Return value:
  1581. The length of outputed wide string, not including the termination null character.
  1582. --*/
  1583. {
  1584. va_list ap;
  1585. const WCHAR * const format_end = format + wcslen(format);
  1586. const WCHAR * const buffer_end = buffer + count - 1;
  1587. WCHAR chr_buf[2] = { 0, 0 };
  1588. LPCWSTR fmt_start = NULL;
  1589. LPWSTR cur_fmt = NULL;
  1590. size_t fmt_len = 0;
  1591. int precision, width;
  1592. int state, left_aligned, wide, narrow, width_var, precision_var, precision_defined;
  1593. // Buffer & length check
  1594. if (buffer == NULL || format == NULL)
  1595. {
  1596. errno = EINVAL;
  1597. return -1;
  1598. }
  1599. if (buffer_end <= buffer)
  1600. return 0;
  1601. va_start(ap, format);
  1602. count = 0; state = 0;
  1603. while (buffer < buffer_end && format < format_end)
  1604. {
  1605. switch (state)
  1606. {
  1607. case 0: // Outside format spec
  1608. if (*format != L'%')
  1609. {
  1610. *buffer++ = *format++;
  1611. count++;
  1612. }
  1613. else
  1614. {
  1615. fmt_start = format++;
  1616. left_aligned = wide = narrow = 0;
  1617. precision_var = width_var = 0;
  1618. precision_defined = 0;
  1619. state = 1;
  1620. }
  1621. continue;
  1622. case 1: // [flags]
  1623. switch (*format)
  1624. {
  1625. case L'-':
  1626. left_aligned = 1;
  1627. case L'+':
  1628. case L' ':
  1629. case L'#':
  1630. case L'0':
  1631. format++;
  1632. continue;
  1633. default:
  1634. state = 2;
  1635. width = width_var = 0;
  1636. }
  1637. case 2: // [width]
  1638. switch (*format)
  1639. {
  1640. case '0':
  1641. case '1':
  1642. case '2':
  1643. case '3':
  1644. case '4':
  1645. case '5':
  1646. case '6':
  1647. case '7':
  1648. case '8':
  1649. case '9':
  1650. if (width >= 0)
  1651. width = width * 10 + (*format - L'0');
  1652. format++;
  1653. continue;
  1654. case '*':
  1655. if (width == 0)
  1656. width_var = 1;
  1657. format++;
  1658. continue;
  1659. case '.':
  1660. format++;
  1661. precision = precision_var = 0;
  1662. precision_defined = 1;
  1663. state = 3;
  1664. continue;
  1665. default:
  1666. state = 4;
  1667. continue;
  1668. }
  1669. case 3: // [.precision]
  1670. switch (*format)
  1671. {
  1672. case '0':
  1673. case '1':
  1674. case '2':
  1675. case '3':
  1676. case '4':
  1677. case '5':
  1678. case '6':
  1679. case '7':
  1680. case '8':
  1681. case '9':
  1682. if (precision >= 0)
  1683. precision = precision * 10 + (*format - L'0');
  1684. format++;
  1685. continue;
  1686. case '*':
  1687. if (precision == 0)
  1688. precision_var = 1;
  1689. format++;
  1690. continue;
  1691. default:
  1692. state = 4;
  1693. }
  1694. case 4: // [{h | l | ll}]
  1695. switch (*format)
  1696. {
  1697. case 'l': if (narrow == 0) wide++; format++; continue;
  1698. case 'h': if (wide == 0) narrow++; format++; continue;
  1699. default: state = 5;
  1700. }
  1701. case 5: // type
  1702. if (*format == 'c' || *format == 's')
  1703. {
  1704. // We handle char & str specially
  1705. LPWSTR buf;
  1706. size_t len;
  1707. int i;
  1708. // Check width
  1709. if (width_var)
  1710. {
  1711. width = va_arg(ap, int);
  1712. left_aligned = (width < 0);
  1713. width = left_aligned ? -width : width;
  1714. }
  1715. // Although precision has no meaning to '%c' output, however
  1716. // the argument still needs to be read if '.*' is provided
  1717. if (precision_var)
  1718. precision = va_arg(ap, int);
  1719. else if (!precision_defined)
  1720. precision = INT_MAX;
  1721. if (*format == 's')
  1722. {
  1723. // For ANSI string, convert it through PAL_MultiByteToWideChar
  1724. // To improve effciency, here just test the length and left
  1725. // actual conversion later directly into the output buffer
  1726. if (wide)
  1727. {
  1728. buf = va_arg(ap, LPWSTR);
  1729. len = wcslen(buf);
  1730. }
  1731. else
  1732. {
  1733. buf = (LPWSTR)va_arg(ap, LPSTR);
  1734. len = PAL_MultiByteToWideChar((LPCSTR)buf, -1, NULL, 0) - 1;
  1735. }
  1736. }
  1737. else
  1738. {
  1739. // For ANSI character, put it into the internal buffer
  1740. if (wide)
  1741. chr_buf[0] = va_arg(ap, WCHAR);
  1742. else
  1743. chr_buf[0] = va_arg(ap, int);
  1744. buf = chr_buf; len = 1;
  1745. }
  1746. // Limit output length no longer then precision
  1747. if (precision > (int)len)
  1748. precision = len;
  1749. // Left-side padding
  1750. for (i = 0; !left_aligned && i < width - precision && buffer < buffer_end; i++)
  1751. *buffer++ = L' ', count++;
  1752. // Do not overflow the output buffer
  1753. if (buffer + precision > buffer_end)
  1754. precision = buffer_end - buffer;
  1755. // Convert or copy string (char) into output buffer
  1756. if (*format == 's' && !wide)
  1757. PAL_MultiByteToWideChar((LPCSTR)buf, -1, buffer, precision);
  1758. else
  1759. wcsncpy(buffer, buf, precision);
  1760. buffer += precision; count += precision;
  1761. // Right-side padding
  1762. for (i = 0; left_aligned && i < width - precision && buffer < buffer_end; i++)
  1763. *buffer++ = L' ', count++;
  1764. }
  1765. else
  1766. {
  1767. // For other types, pass them directly into vswprintf
  1768. int cur_cnt = 0;
  1769. va_list apd;
  1770. // We copy this argument's format string into internal buffer
  1771. if (fmt_len < (size_t)(format - fmt_start + 1))
  1772. cur_fmt = realloc(cur_fmt, ((fmt_len = format - fmt_start + 1) + 1) * sizeof(WCHAR));
  1773. wcsncpy(cur_fmt, fmt_start, fmt_len);
  1774. cur_fmt[fmt_len] = L'\0';
  1775. // And pass it into vswprintf to get the output
  1776. va_copy(apd, ap);
  1777. cur_cnt = vswprintf(buffer, buffer_end - buffer, cur_fmt, apd);
  1778. va_end(apd);
  1779. buffer += cur_cnt; count += cur_cnt;
  1780. // Then we need to move the argument pointer into next one
  1781. // Check if width/precision should be read from arguments
  1782. if (width_var) va_arg(ap, int);
  1783. if (precision_var) va_arg(ap, int);
  1784. // Move pointer to pass the actual value argument
  1785. switch (*format)
  1786. {
  1787. case 'd':
  1788. case 'i':
  1789. case 'o':
  1790. case 'u':
  1791. case 'x':
  1792. case 'X':
  1793. if (wide == 1)
  1794. va_arg(ap, long);
  1795. else if (wide >= 2)
  1796. va_arg(ap, long long);
  1797. else
  1798. va_arg(ap, int);
  1799. break;
  1800. case 'e':
  1801. case 'E':
  1802. case 'f':
  1803. case 'g':
  1804. case 'G':
  1805. case 'a':
  1806. case 'A':
  1807. va_arg(ap, double);
  1808. break;
  1809. case 'p':
  1810. case 'n':
  1811. va_arg(ap, void*);
  1812. break;
  1813. }
  1814. }
  1815. state = 0;
  1816. format++;
  1817. break;
  1818. }
  1819. }
  1820. // If the format string is malformed, try to copy it into the dest buffer
  1821. if (state && buffer < buffer_end)
  1822. {
  1823. int fmt_len = format - fmt_start;
  1824. int buf_len = buffer_end - buffer;
  1825. if (fmt_len <= buf_len)
  1826. {
  1827. wcsncpy(buffer, fmt_start, buf_len);
  1828. buffer += fmt_len;
  1829. }
  1830. else
  1831. {
  1832. wcsncpy(buffer, fmt_start, buf_len);
  1833. buffer += buf_len;
  1834. }
  1835. }
  1836. // NULL-terminate the string
  1837. *buffer = L'\0';
  1838. va_end(ap);
  1839. return count;
  1840. }
  1841. WCHAR
  1842. PAL_GetInvalidChar(
  1843. CODEPAGE uCodePage
  1844. )
  1845. {
  1846. switch(uCodePage)
  1847. {
  1848. case CP_BIG5: return 0x3f;
  1849. case CP_GBK: return 0x3f;
  1850. //case CP_SHIFTJIS: return 0x30fb;
  1851. case CP_UTF_8: return 0x3f;
  1852. default: return 0;
  1853. }
  1854. }