text.c 47 KB

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