text.c 48 KB

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