text.c 48 KB

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