text.c 48 KB

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