text.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213
  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[PAL_GetCodePage()], SDLPAL_EXTRA_WORD_COUNT * sizeof(LPCWSTR));
  655. #ifndef PAL_CLASSIC
  656. memcpy(g_TextLib.lpWordBuf + SYSMENU_LABEL_BATTLEMODE, gc_rgszAdditionalWords[PAL_GetCodePage()], 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. if (g_TextLib.fUserSkip)
  1175. {
  1176. VIDEO_UpdateScreen(NULL);
  1177. }
  1178. UTIL_Delay(wcstol(lpszText + 1, NULL, 10) * 80 / 7);
  1179. g_TextLib.nCurrentDialogLine = 0;
  1180. g_TextLib.fUserSkip = FALSE;
  1181. return; // don't go further
  1182. case ')':
  1183. //
  1184. // Set the waiting icon
  1185. //
  1186. g_TextLib.bIcon = 1;
  1187. lpszText++;
  1188. break;
  1189. case '(':
  1190. //
  1191. // Set the waiting icon
  1192. //
  1193. g_TextLib.bIcon = 2;
  1194. lpszText++;
  1195. break;
  1196. case '\\':
  1197. lpszText++;
  1198. default:
  1199. text[0] = *lpszText++;
  1200. text[1] = 0;
  1201. // Update the screen on each draw operation is time-consuming, so disable it if user want to skip
  1202. PAL_DrawText(text, PAL_XY(x, y), g_TextLib.bCurrentFontColor, TRUE, !g_TextLib.fUserSkip, FALSE);
  1203. x += PAL_CharWidth(text[0]);
  1204. if (!g_TextLib.fUserSkip)
  1205. {
  1206. PAL_ClearKeyState();
  1207. UTIL_Delay(g_TextLib.iDelayTime * 8);
  1208. if (g_InputState.dwKeyPress & (kKeySearch | kKeyMenu))
  1209. {
  1210. //
  1211. // User pressed a key to skip the dialog
  1212. //
  1213. g_TextLib.fUserSkip = TRUE;
  1214. }
  1215. }
  1216. }
  1217. }
  1218. // and update the full screen at once after all texts are drawn
  1219. if (g_TextLib.fUserSkip)
  1220. {
  1221. VIDEO_UpdateScreen(NULL);
  1222. }
  1223. g_TextLib.posIcon = PAL_XY(x, y);
  1224. g_TextLib.nCurrentDialogLine++;
  1225. }
  1226. }
  1227. }
  1228. VOID
  1229. PAL_ClearDialog(
  1230. BOOL fWaitForKey
  1231. )
  1232. /*++
  1233. Purpose:
  1234. Clear the state of the dialog.
  1235. Parameters:
  1236. [IN] fWaitForKey - whether wait for any key or not.
  1237. Return value:
  1238. None.
  1239. --*/
  1240. {
  1241. if (g_TextLib.nCurrentDialogLine > 0 && fWaitForKey)
  1242. {
  1243. PAL_DialogWaitForKey();
  1244. }
  1245. g_TextLib.nCurrentDialogLine = 0;
  1246. if (g_TextLib.bDialogPosition == kDialogCenter)
  1247. {
  1248. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  1249. g_TextLib.posDialogText = PAL_XY(44, 26);
  1250. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1251. g_TextLib.bDialogPosition = kDialogUpper;
  1252. }
  1253. }
  1254. VOID
  1255. PAL_EndDialog(
  1256. VOID
  1257. )
  1258. /*++
  1259. Purpose:
  1260. Ends a dialog.
  1261. Parameters:
  1262. None.
  1263. Return value:
  1264. None.
  1265. --*/
  1266. {
  1267. PAL_ClearDialog(TRUE);
  1268. //
  1269. // Set some default parameters, as there are some parts of script
  1270. // which doesn't have a "start dialog" instruction before showing the dialog.
  1271. //
  1272. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  1273. g_TextLib.posDialogText = PAL_XY(44, 26);
  1274. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1275. g_TextLib.bDialogPosition = kDialogUpper;
  1276. g_TextLib.fUserSkip = FALSE;
  1277. g_TextLib.fPlayingRNG = FALSE;
  1278. }
  1279. BOOL
  1280. PAL_IsInDialog(
  1281. VOID
  1282. )
  1283. /*++
  1284. Purpose:
  1285. Check if there are dialog texts on the screen.
  1286. Parameters:
  1287. None.
  1288. Return value:
  1289. TRUE if there are dialog texts on the screen, FALSE if not.
  1290. --*/
  1291. {
  1292. return (g_TextLib.nCurrentDialogLine != 0);
  1293. }
  1294. BOOL
  1295. PAL_DialogIsPlayingRNG(
  1296. VOID
  1297. )
  1298. /*++
  1299. Purpose:
  1300. Check if the script used the RNG playing parameter when displaying texts.
  1301. Parameters:
  1302. None.
  1303. Return value:
  1304. TRUE if the script used the RNG playing parameter, FALSE if not.
  1305. --*/
  1306. {
  1307. return g_TextLib.fPlayingRNG;
  1308. }
  1309. WCHAR
  1310. PAL_GetInvalidChar(
  1311. CODEPAGE uCodePage
  1312. )
  1313. {
  1314. switch (uCodePage)
  1315. {
  1316. case CP_BIG5: return 0x3f;
  1317. case CP_GBK: return 0x3f;
  1318. //case CP_SHIFTJIS: return 0x30fb;
  1319. case CP_UTF_8: return 0x3f;
  1320. default: return 0;
  1321. }
  1322. }
  1323. static CODEPAGE g_codepage = CP_UTF_8;
  1324. CODEPAGE
  1325. PAL_GetCodePage(
  1326. void
  1327. )
  1328. {
  1329. return g_codepage;
  1330. }
  1331. void
  1332. PAL_SetCodePage(
  1333. CODEPAGE uCodePage
  1334. )
  1335. {
  1336. g_codepage = uCodePage;
  1337. }
  1338. CODEPAGE
  1339. PAL_DetectCodePageForString(
  1340. const char * text,
  1341. int text_len,
  1342. CODEPAGE default_cp,
  1343. int * probability
  1344. )
  1345. {
  1346. // Try to convert the content of word.dat with different codepages,
  1347. // and use the codepage with minimal inconvertible characters
  1348. // Works fine currently for detecting Simplified Chinese & Traditional Chinese.
  1349. // Since we're using language files to support additional languages, this detection
  1350. // should be fine for us now.
  1351. int min_invalids = INT_MAX;
  1352. if (text && text_len > 0)
  1353. {
  1354. // The file to be detected should not contain characters outside these ranges
  1355. const static int valid_ranges[][2] = {
  1356. { 0x4E00, 0x9FFF }, // CJK Unified Ideographs
  1357. { 0x3400, 0x4DBF }, // CJK Unified Ideographs Extension A
  1358. { 0xF900, 0xFAFF }, // CJK Compatibility Ideographs
  1359. { 0x0020, 0x007E }, // Basic ASCII
  1360. { 0x3000, 0x301E }, // CJK Symbols
  1361. { 0xFF01, 0xFF5E }, // Fullwidth Forms
  1362. };
  1363. for (CODEPAGE i = CP_BIG5; i <= CP_GBK; i++)
  1364. {
  1365. int invalids, length = PAL_MultiByteToWideCharCP(i, text, text_len, NULL, 0);
  1366. WCHAR *wbuf = (WCHAR *)malloc(length * sizeof(WCHAR));
  1367. PAL_MultiByteToWideCharCP(i, text, text_len, wbuf, length);
  1368. for (int j = invalids = 0; j < length; j++)
  1369. {
  1370. int score = 1;
  1371. for (int k = 0; k < sizeof(valid_ranges) / sizeof(valid_ranges[0]); k++)
  1372. {
  1373. if (wbuf[j] >= valid_ranges[k][0] &&
  1374. wbuf[j] <= valid_ranges[k][1])
  1375. {
  1376. score = 0;
  1377. break;
  1378. }
  1379. }
  1380. invalids += score;
  1381. }
  1382. // code page with less invalid chars wins
  1383. if (invalids < min_invalids)
  1384. {
  1385. min_invalids = invalids;
  1386. default_cp = i;
  1387. }
  1388. free(wbuf);
  1389. }
  1390. }
  1391. if (probability)
  1392. {
  1393. if (min_invalids < text_len / 2)
  1394. *probability = (text_len / 2 - min_invalids) * 200 / text_len;
  1395. else
  1396. *probability = 0;
  1397. }
  1398. return default_cp;
  1399. }
  1400. INT
  1401. PAL_MultiByteToWideCharCP(
  1402. CODEPAGE cp,
  1403. LPCSTR mbs,
  1404. int mbslength,
  1405. LPWSTR wcs,
  1406. int wcslength
  1407. )
  1408. /*++
  1409. Purpose:
  1410. Convert multi-byte string into the corresponding unicode string.
  1411. Parameters:
  1412. [IN] cp - Code page for conversion.
  1413. [IN] mbs - Pointer to the multi-byte string.
  1414. [IN] mbslength - Length of the multi-byte string, or -1 for auto-detect.
  1415. [IN] wcs - Pointer to the wide string buffer.
  1416. [IN] wcslength - Length of the wide string buffer.
  1417. Return value:
  1418. The length of converted wide string. If mbslength is set to -1, the returned
  1419. value includes the terminal null-char; otherwise, the null-char is not included.
  1420. If wcslength is set to 0, wcs can be set to NULL and the return value is the
  1421. required length of the wide string buffer.
  1422. --*/
  1423. {
  1424. int i = 0, state = 0, wlen = 0, null = 0;
  1425. if (mbslength == -1)
  1426. {
  1427. mbslength = strlen(mbs);
  1428. null = 1;
  1429. }
  1430. if (!wcs)
  1431. {
  1432. switch (cp)
  1433. {
  1434. //case CP_SHIFTJIS:
  1435. // for (i = 0; i < mbslength && mbs[i]; i++)
  1436. // {
  1437. // if (state == 0)
  1438. // {
  1439. // if ((BYTE)mbs[i] <= 0x80 || (BYTE)mbs[i] >= 0xfd || ((BYTE)mbs[i] >= 0xa0 && (BYTE)mbs[i] <= 0xdf))
  1440. // wlen++;
  1441. // else
  1442. // state = 1;
  1443. // }
  1444. // else
  1445. // {
  1446. // wlen++;
  1447. // state = 0;
  1448. // }
  1449. // }
  1450. // break;
  1451. case CP_GBK:
  1452. case CP_BIG5:
  1453. for (i = 0; i < mbslength && mbs[i]; i++)
  1454. {
  1455. if (state == 0)
  1456. {
  1457. if ((BYTE)mbs[i] <= 0x80 || (BYTE)mbs[i] == 0xff)
  1458. wlen++;
  1459. else
  1460. state = 1;
  1461. }
  1462. else
  1463. {
  1464. wlen++;
  1465. state = 0;
  1466. }
  1467. }
  1468. break;
  1469. case CP_UTF_8:
  1470. for (i = 0; i < mbslength && mbs[i]; i++)
  1471. {
  1472. if (state == 0)
  1473. {
  1474. if ((BYTE)mbs[i] >= 0x80)
  1475. {
  1476. BYTE s = (BYTE)mbs[i] << 1;
  1477. while (s >= 0x80) { state++; s <<= 1; }
  1478. if (state < 1 || state > 3)
  1479. {
  1480. state = 0;
  1481. wlen++;
  1482. }
  1483. }
  1484. else
  1485. wlen++;
  1486. }
  1487. else
  1488. {
  1489. if ((BYTE)mbs[i] >= 0x80 && (BYTE)mbs[i] < 0xc0)
  1490. {
  1491. if (--state == 0) wlen++;
  1492. }
  1493. else
  1494. {
  1495. state = 0; wlen++;
  1496. }
  1497. }
  1498. }
  1499. break;
  1500. default:
  1501. return -1;
  1502. }
  1503. if (i < mbslength && !mbs[i]) null = 1;
  1504. return wlen + null + (state != 0);
  1505. }
  1506. else
  1507. {
  1508. WCHAR invalid_char;
  1509. switch (cp)
  1510. {
  1511. //case CP_SHIFTJIS:
  1512. // invalid_char = 0x30fb;
  1513. // for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1514. // {
  1515. // if (state == 0)
  1516. // {
  1517. // if ((BYTE)mbs[i] <= 0x80)
  1518. // wcs[wlen++] = mbs[i];
  1519. // else if ((BYTE)mbs[i] >= 0xa0 && (BYTE)mbs[i] <= 0xdf)
  1520. // wcs[wlen++] = cptbl_jis_half[(BYTE)mbs[i] - 0xa0];
  1521. // else if ((BYTE)mbs[i] == 0xfd)
  1522. // wcs[wlen++] = 0xf8f1;
  1523. // else if ((BYTE)mbs[i] == 0xfe)
  1524. // wcs[wlen++] = 0xf8f2;
  1525. // else if ((BYTE)mbs[i] == 0xff)
  1526. // wcs[wlen++] = 0xf8f3;
  1527. // else
  1528. // state = 1;
  1529. // }
  1530. // else
  1531. // {
  1532. // if ((BYTE)mbs[i] < 0x40)
  1533. // wcs[wlen++] = 0x30fb;
  1534. // else if ((BYTE)mbs[i - 1] < 0xa0)
  1535. // wcs[wlen++] = cptbl_jis[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  1536. // else
  1537. // wcs[wlen++] = cptbl_jis[(BYTE)mbs[i - 1] - 0xc1][(BYTE)mbs[i] - 0x40];
  1538. // state = 0;
  1539. // }
  1540. // }
  1541. // break;
  1542. case CP_GBK:
  1543. invalid_char = 0x3f;
  1544. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1545. {
  1546. if (state == 0)
  1547. {
  1548. if ((BYTE)mbs[i] < 0x80)
  1549. wcs[wlen++] = mbs[i];
  1550. else if ((BYTE)mbs[i] == 0x80)
  1551. wcs[wlen++] = 0x20ac;
  1552. else if ((BYTE)mbs[i] == 0xff)
  1553. wcs[wlen++] = 0xf8f5;
  1554. else
  1555. state = 1;
  1556. }
  1557. else
  1558. {
  1559. if ((BYTE)mbs[i] < 0x40)
  1560. wcs[wlen++] = invalid_char;
  1561. else
  1562. wcs[wlen++] = cptbl_gbk[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  1563. state = 0;
  1564. }
  1565. }
  1566. break;
  1567. case CP_BIG5:
  1568. invalid_char = 0x3f;
  1569. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1570. {
  1571. if (state == 0)
  1572. {
  1573. if ((BYTE)mbs[i] <= 0x80)
  1574. wcs[wlen++] = mbs[i];
  1575. else if ((BYTE)mbs[i] == 0xff)
  1576. wcs[wlen++] = 0xf8f8;
  1577. else
  1578. state = 1;
  1579. }
  1580. else
  1581. {
  1582. if ((BYTE)mbs[i] < 0x40 || ((BYTE)mbs[i] >= 0x7f && (BYTE)mbs[i] <= 0xa0))
  1583. wcs[wlen++] = invalid_char;
  1584. else if ((BYTE)mbs[i] <= 0x7e)
  1585. wcs[wlen++] = cptbl_big5[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  1586. else
  1587. wcs[wlen++] = cptbl_big5[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x60];
  1588. state = 0;
  1589. }
  1590. }
  1591. break;
  1592. case CP_UTF_8:
  1593. invalid_char = 0x3f;
  1594. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1595. {
  1596. if (state == 0)
  1597. {
  1598. if ((BYTE)mbs[i] >= 0x80)
  1599. {
  1600. BYTE s = (BYTE)mbs[i] << 1;
  1601. while (s >= 0x80) { state++; s <<= 1; }
  1602. if (state < 1 || state > 3)
  1603. {
  1604. state = 0;
  1605. wcs[wlen++] = invalid_char;
  1606. }
  1607. else
  1608. {
  1609. wcs[wlen] = s >> (state + 1);
  1610. }
  1611. }
  1612. else
  1613. wcs[wlen++] = mbs[i];
  1614. }
  1615. else
  1616. {
  1617. if ((BYTE)mbs[i] >= 0x80 && (BYTE)mbs[i] < 0xc0)
  1618. {
  1619. wcs[wlen] <<= 6;
  1620. wcs[wlen] |= (BYTE)mbs[i] & 0x3f;
  1621. if (--state == 0) wlen++;
  1622. }
  1623. else
  1624. {
  1625. state = 0;
  1626. wcs[wlen++] = invalid_char;
  1627. }
  1628. }
  1629. }
  1630. break;
  1631. default:
  1632. return -1;
  1633. }
  1634. if (state != 0 && wlen < wcslength)
  1635. {
  1636. wcs[wlen++] = invalid_char;
  1637. }
  1638. if (null || (i < mbslength && !mbs[i]))
  1639. {
  1640. if (wlen < wcslength)
  1641. wcs[wlen++] = 0;
  1642. else
  1643. wcs[wlen - 1] = 0;
  1644. }
  1645. return wlen;
  1646. }
  1647. }
  1648. INT
  1649. PAL_MultiByteToWideChar(
  1650. LPCSTR mbs,
  1651. int mbslength,
  1652. LPWSTR wcs,
  1653. int wcslength
  1654. )
  1655. /*++
  1656. Purpose:
  1657. Convert multi-byte string into the corresponding unicode string.
  1658. Parameters:
  1659. [IN] mbs - Pointer to the multi-byte string.
  1660. [IN] mbslength - Length of the multi-byte string, or -1 for auto-detect.
  1661. [IN] wcs - Pointer to the wide string buffer.
  1662. [IN] wcslength - Length of the wide string buffer.
  1663. Return value:
  1664. The length of converted wide string. If mbslength is set to -1, the returned
  1665. value includes the terminal null-char; otherwise, the null-char is not included.
  1666. If wcslength is set to 0, wcs can be set to NULL and the return value is the
  1667. required length of the wide string buffer.
  1668. --*/
  1669. {
  1670. return PAL_MultiByteToWideCharCP(g_codepage, mbs, mbslength, wcs, wcslength);
  1671. }
  1672. INT
  1673. PAL_swprintf(
  1674. LPWSTR buffer,
  1675. size_t count,
  1676. LPCWSTR format,
  1677. ...
  1678. )
  1679. /*++
  1680. Purpose:
  1681. Formatted wide-character output conversion that output Chinese characters correctly.
  1682. This function supported a subset of format strings that are commonly supported by
  1683. various C libraries, which can be formalized as following:
  1684. %[flags] [width] [.precision] [{h | l | ll}] type
  1685. When handling '%c' and '%s', this function follows the Linux's library convention,
  1686. which means '%c' and '%s' always output multi-byte strings, and '%lc' and '%ls'
  1687. always output wide-char strings.
  1688. Parameters:
  1689. [IN] buffer - Storage location for output.
  1690. [IN] count - Length of the output buffer in characters including the termination null one.
  1691. [IN] format - Format-control string.
  1692. [IN] ... - Optional arguments.
  1693. Return value:
  1694. The length of outputed wide string, not including the termination null character.
  1695. --*/
  1696. {
  1697. va_list ap;
  1698. const WCHAR * const format_end = format + wcslen(format);
  1699. const WCHAR * const buffer_end = buffer + count - 1;
  1700. WCHAR chr_buf[2] = { 0, 0 };
  1701. LPCWSTR fmt_start = NULL;
  1702. LPWSTR cur_fmt = NULL;
  1703. size_t fmt_len = 0;
  1704. int state, precision = 0, width = 0;
  1705. int left_aligned = 0, wide = 0, narrow = 0;
  1706. int width_var = 0, precision_var = 0, precision_defined = 0;
  1707. // Buffer & length check
  1708. if (buffer == NULL || format == NULL)
  1709. {
  1710. errno = EINVAL;
  1711. return -1;
  1712. }
  1713. if (buffer_end <= buffer)
  1714. return 0;
  1715. va_start(ap, format);
  1716. count = 0; state = 0;
  1717. while (buffer < buffer_end && format < format_end)
  1718. {
  1719. switch (state)
  1720. {
  1721. case 0: // Outside format spec
  1722. if (*format != L'%')
  1723. {
  1724. *buffer++ = *format++;
  1725. count++;
  1726. }
  1727. else
  1728. {
  1729. fmt_start = format++;
  1730. left_aligned = wide = narrow = 0;
  1731. precision_var = width_var = 0;
  1732. precision_defined = 0;
  1733. state = 1;
  1734. }
  1735. continue;
  1736. case 1: // [flags]
  1737. switch (*format)
  1738. {
  1739. case L'-':
  1740. left_aligned = 1;
  1741. case L'+':
  1742. case L' ':
  1743. case L'#':
  1744. case L'0':
  1745. format++;
  1746. continue;
  1747. default:
  1748. state = 2;
  1749. width = width_var = 0;
  1750. }
  1751. case 2: // [width]
  1752. switch (*format)
  1753. {
  1754. case '0':
  1755. case '1':
  1756. case '2':
  1757. case '3':
  1758. case '4':
  1759. case '5':
  1760. case '6':
  1761. case '7':
  1762. case '8':
  1763. case '9':
  1764. if (width >= 0)
  1765. width = width * 10 + (*format - L'0');
  1766. format++;
  1767. continue;
  1768. case '*':
  1769. if (width == 0)
  1770. width_var = 1;
  1771. format++;
  1772. continue;
  1773. case '.':
  1774. format++;
  1775. precision = precision_var = 0;
  1776. precision_defined = 1;
  1777. state = 3;
  1778. continue;
  1779. default:
  1780. state = 4;
  1781. continue;
  1782. }
  1783. case 3: // [.precision]
  1784. switch (*format)
  1785. {
  1786. case '0':
  1787. case '1':
  1788. case '2':
  1789. case '3':
  1790. case '4':
  1791. case '5':
  1792. case '6':
  1793. case '7':
  1794. case '8':
  1795. case '9':
  1796. if (precision >= 0)
  1797. precision = precision * 10 + (*format - L'0');
  1798. format++;
  1799. continue;
  1800. case '*':
  1801. if (precision == 0)
  1802. precision_var = 1;
  1803. format++;
  1804. continue;
  1805. default:
  1806. state = 4;
  1807. }
  1808. case 4: // [{h | l | ll}]
  1809. switch (*format)
  1810. {
  1811. case 'l': if (narrow == 0) wide++; format++; continue;
  1812. case 'h': if (wide == 0) narrow++; format++; continue;
  1813. default: state = 5;
  1814. }
  1815. case 5: // type
  1816. if (*format == 'c' || *format == 's')
  1817. {
  1818. // We handle char & str specially
  1819. LPWSTR buf;
  1820. size_t len;
  1821. int i;
  1822. // Check width
  1823. if (width_var)
  1824. {
  1825. width = va_arg(ap, int);
  1826. left_aligned = (width < 0);
  1827. width = left_aligned ? -width : width;
  1828. }
  1829. // Although precision has no meaning to '%c' output, however
  1830. // the argument still needs to be read if '.*' is provided
  1831. if (precision_var)
  1832. precision = va_arg(ap, int);
  1833. else if (!precision_defined)
  1834. precision = INT_MAX;
  1835. if (*format == 's')
  1836. {
  1837. // For ANSI string, convert it through PAL_MultiByteToWideChar
  1838. // To improve effciency, here just test the length and left
  1839. // actual conversion later directly into the output buffer
  1840. if (wide)
  1841. {
  1842. buf = va_arg(ap, LPWSTR);
  1843. len = wcslen(buf);
  1844. }
  1845. else
  1846. {
  1847. buf = (LPWSTR)va_arg(ap, LPSTR);
  1848. len = PAL_MultiByteToWideChar((LPCSTR)buf, -1, NULL, 0) - 1;
  1849. }
  1850. }
  1851. else
  1852. {
  1853. // For ANSI character, put it into the internal buffer
  1854. if (wide)
  1855. chr_buf[0] = va_arg(ap, WCHAR);
  1856. else
  1857. chr_buf[0] = va_arg(ap, int);
  1858. buf = chr_buf; len = 1;
  1859. }
  1860. // Limit output length no longer then precision
  1861. if (precision > (int)len)
  1862. precision = len;
  1863. // Left-side padding
  1864. for (i = 0; !left_aligned && i < width - precision && buffer < buffer_end; i++)
  1865. *buffer++ = L' ', count++;
  1866. // Do not overflow the output buffer
  1867. if (buffer + precision > buffer_end)
  1868. precision = buffer_end - buffer;
  1869. // Convert or copy string (char) into output buffer
  1870. if (*format == 's' && !wide)
  1871. PAL_MultiByteToWideChar((LPCSTR)buf, -1, buffer, precision);
  1872. else
  1873. wcsncpy(buffer, buf, precision);
  1874. buffer += precision; count += precision;
  1875. // Right-side padding
  1876. for (i = 0; left_aligned && i < width - precision && buffer < buffer_end; i++)
  1877. *buffer++ = L' ', count++;
  1878. }
  1879. else
  1880. {
  1881. // For other types, pass them directly into vswprintf
  1882. int cur_cnt = 0;
  1883. va_list apd;
  1884. // We copy this argument's format string into internal buffer
  1885. if (fmt_len < (size_t)(format - fmt_start + 1))
  1886. cur_fmt = realloc(cur_fmt, ((fmt_len = format - fmt_start + 1) + 1) * sizeof(WCHAR));
  1887. wcsncpy(cur_fmt, fmt_start, fmt_len);
  1888. cur_fmt[fmt_len] = L'\0';
  1889. // And pass it into vswprintf to get the output
  1890. va_copy(apd, ap);
  1891. cur_cnt = vswprintf(buffer, buffer_end - buffer, cur_fmt, apd);
  1892. va_end(apd);
  1893. buffer += cur_cnt; count += cur_cnt;
  1894. // Then we need to move the argument pointer into next one
  1895. // Check if width/precision should be read from arguments
  1896. if (width_var) va_arg(ap, int);
  1897. if (precision_var) va_arg(ap, int);
  1898. // Move pointer to pass the actual value argument
  1899. switch (*format)
  1900. {
  1901. case 'd':
  1902. case 'i':
  1903. case 'o':
  1904. case 'u':
  1905. case 'x':
  1906. case 'X':
  1907. if (wide == 1)
  1908. va_arg(ap, long);
  1909. else if (wide >= 2)
  1910. va_arg(ap, long long);
  1911. else
  1912. va_arg(ap, int);
  1913. break;
  1914. case 'e':
  1915. case 'E':
  1916. case 'f':
  1917. case 'g':
  1918. case 'G':
  1919. case 'a':
  1920. case 'A':
  1921. va_arg(ap, double);
  1922. break;
  1923. case 'p':
  1924. case 'n':
  1925. va_arg(ap, void*);
  1926. break;
  1927. }
  1928. }
  1929. state = 0;
  1930. format++;
  1931. break;
  1932. }
  1933. }
  1934. // If the format string is malformed, try to copy it into the dest buffer
  1935. if (state && buffer < buffer_end)
  1936. {
  1937. int fmt_len = format - fmt_start;
  1938. int buf_len = buffer_end - buffer;
  1939. if (fmt_len <= buf_len)
  1940. {
  1941. wcsncpy(buffer, fmt_start, buf_len);
  1942. buffer += fmt_len;
  1943. }
  1944. else
  1945. {
  1946. wcsncpy(buffer, fmt_start, buf_len);
  1947. buffer += buf_len;
  1948. }
  1949. }
  1950. // NULL-terminate the string
  1951. *buffer = L'\0';
  1952. va_end(ap);
  1953. return count;
  1954. }