text.c 53 KB

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