text.c 48 KB

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