text.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209
  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. CODEPAGE
  1335. PAL_DetectCodePageForString(
  1336. const char * text,
  1337. int text_len,
  1338. CODEPAGE default_cp,
  1339. int * probability
  1340. )
  1341. {
  1342. // Try to convert the content of word.dat with different codepages,
  1343. // and use the codepage with minimal inconvertible characters
  1344. // Works fine currently for detecting Simplified Chinese & Traditional Chinese.
  1345. // Since we're using language files to support additional languages, this detection
  1346. // should be fine for us now.
  1347. int min_invalids = INT_MAX;
  1348. if (text && text_len > 0)
  1349. {
  1350. // The file to be detected should not contain characters outside these ranges
  1351. const static int valid_ranges[][2] = {
  1352. { 0x4E00, 0x9FFF }, // CJK Unified Ideographs
  1353. { 0x3400, 0x4DBF }, // CJK Unified Ideographs Extension A
  1354. { 0xF900, 0xFAFF }, // CJK Compatibility Ideographs
  1355. { 0x0020, 0x007E }, // Basic ASCII
  1356. { 0x3000, 0x301E }, // CJK Symbols
  1357. { 0xFF01, 0xFF5E }, // Fullwidth Forms
  1358. };
  1359. for (CODEPAGE i = CP_BIG5; i <= CP_GBK; i++)
  1360. {
  1361. int invalids, length = PAL_MultiByteToWideCharCP(i, text, text_len, NULL, 0);
  1362. WCHAR *wbuf = (WCHAR *)malloc(length * sizeof(WCHAR));
  1363. PAL_MultiByteToWideCharCP(i, text, text_len, wbuf, length);
  1364. for (int j = invalids = 0; j < length; j++)
  1365. {
  1366. int score = 1;
  1367. for (int k = 0; k < sizeof(valid_ranges) / sizeof(valid_ranges[0]); k++)
  1368. {
  1369. if (wbuf[j] >= valid_ranges[k][0] &&
  1370. wbuf[j] <= valid_ranges[k][1])
  1371. {
  1372. score = 0;
  1373. break;
  1374. }
  1375. }
  1376. invalids += score;
  1377. }
  1378. // code page with less invalid chars wins
  1379. if (invalids < min_invalids)
  1380. {
  1381. min_invalids = invalids;
  1382. default_cp = i;
  1383. }
  1384. free(wbuf);
  1385. }
  1386. }
  1387. if (probability)
  1388. {
  1389. if (min_invalids < text_len / 2)
  1390. *probability = (text_len / 2 - min_invalids) * 200 / text_len;
  1391. else
  1392. *probability = 0;
  1393. }
  1394. return default_cp;
  1395. }
  1396. INT
  1397. PAL_MultiByteToWideCharCP(
  1398. CODEPAGE cp,
  1399. LPCSTR mbs,
  1400. int mbslength,
  1401. LPWSTR wcs,
  1402. int wcslength
  1403. )
  1404. /*++
  1405. Purpose:
  1406. Convert multi-byte string into the corresponding unicode string.
  1407. Parameters:
  1408. [IN] cp - Code page for conversion.
  1409. [IN] mbs - Pointer to the multi-byte string.
  1410. [IN] mbslength - Length of the multi-byte string, or -1 for auto-detect.
  1411. [IN] wcs - Pointer to the wide string buffer.
  1412. [IN] wcslength - Length of the wide string buffer.
  1413. Return value:
  1414. The length of converted wide string. If mbslength is set to -1, the returned
  1415. value includes the terminal null-char; otherwise, the null-char is not included.
  1416. If wcslength is set to 0, wcs can be set to NULL and the return value is the
  1417. required length of the wide string buffer.
  1418. --*/
  1419. {
  1420. int i = 0, state = 0, wlen = 0, null = 0;
  1421. if (mbslength == -1)
  1422. {
  1423. mbslength = strlen(mbs);
  1424. null = 1;
  1425. }
  1426. if (!wcs)
  1427. {
  1428. switch (cp)
  1429. {
  1430. //case CP_SHIFTJIS:
  1431. // for (i = 0; i < mbslength && mbs[i]; i++)
  1432. // {
  1433. // if (state == 0)
  1434. // {
  1435. // if ((BYTE)mbs[i] <= 0x80 || (BYTE)mbs[i] >= 0xfd || ((BYTE)mbs[i] >= 0xa0 && (BYTE)mbs[i] <= 0xdf))
  1436. // wlen++;
  1437. // else
  1438. // state = 1;
  1439. // }
  1440. // else
  1441. // {
  1442. // wlen++;
  1443. // state = 0;
  1444. // }
  1445. // }
  1446. // break;
  1447. case CP_GBK:
  1448. case CP_BIG5:
  1449. for (i = 0; i < mbslength && mbs[i]; i++)
  1450. {
  1451. if (state == 0)
  1452. {
  1453. if ((BYTE)mbs[i] <= 0x80 || (BYTE)mbs[i] == 0xff)
  1454. wlen++;
  1455. else
  1456. state = 1;
  1457. }
  1458. else
  1459. {
  1460. wlen++;
  1461. state = 0;
  1462. }
  1463. }
  1464. break;
  1465. case CP_UTF_8:
  1466. for (i = 0; i < mbslength && mbs[i]; i++)
  1467. {
  1468. if (state == 0)
  1469. {
  1470. if ((BYTE)mbs[i] >= 0x80)
  1471. {
  1472. BYTE s = (BYTE)mbs[i] << 1;
  1473. while (s >= 0x80) { state++; s <<= 1; }
  1474. if (state < 1 || state > 3)
  1475. {
  1476. state = 0;
  1477. wlen++;
  1478. }
  1479. }
  1480. else
  1481. wlen++;
  1482. }
  1483. else
  1484. {
  1485. if ((BYTE)mbs[i] >= 0x80 && (BYTE)mbs[i] < 0xc0)
  1486. {
  1487. if (--state == 0) wlen++;
  1488. }
  1489. else
  1490. {
  1491. state = 0; wlen++;
  1492. }
  1493. }
  1494. }
  1495. break;
  1496. default:
  1497. return -1;
  1498. }
  1499. if (i < mbslength && !mbs[i]) null = 1;
  1500. return wlen + null + (state != 0);
  1501. }
  1502. else
  1503. {
  1504. WCHAR invalid_char;
  1505. switch (cp)
  1506. {
  1507. //case CP_SHIFTJIS:
  1508. // invalid_char = 0x30fb;
  1509. // for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1510. // {
  1511. // if (state == 0)
  1512. // {
  1513. // if ((BYTE)mbs[i] <= 0x80)
  1514. // wcs[wlen++] = mbs[i];
  1515. // else if ((BYTE)mbs[i] >= 0xa0 && (BYTE)mbs[i] <= 0xdf)
  1516. // wcs[wlen++] = cptbl_jis_half[(BYTE)mbs[i] - 0xa0];
  1517. // else if ((BYTE)mbs[i] == 0xfd)
  1518. // wcs[wlen++] = 0xf8f1;
  1519. // else if ((BYTE)mbs[i] == 0xfe)
  1520. // wcs[wlen++] = 0xf8f2;
  1521. // else if ((BYTE)mbs[i] == 0xff)
  1522. // wcs[wlen++] = 0xf8f3;
  1523. // else
  1524. // state = 1;
  1525. // }
  1526. // else
  1527. // {
  1528. // if ((BYTE)mbs[i] < 0x40)
  1529. // wcs[wlen++] = 0x30fb;
  1530. // else if ((BYTE)mbs[i - 1] < 0xa0)
  1531. // wcs[wlen++] = cptbl_jis[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  1532. // else
  1533. // wcs[wlen++] = cptbl_jis[(BYTE)mbs[i - 1] - 0xc1][(BYTE)mbs[i] - 0x40];
  1534. // state = 0;
  1535. // }
  1536. // }
  1537. // break;
  1538. case CP_GBK:
  1539. invalid_char = 0x3f;
  1540. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1541. {
  1542. if (state == 0)
  1543. {
  1544. if ((BYTE)mbs[i] < 0x80)
  1545. wcs[wlen++] = mbs[i];
  1546. else if ((BYTE)mbs[i] == 0x80)
  1547. wcs[wlen++] = 0x20ac;
  1548. else if ((BYTE)mbs[i] == 0xff)
  1549. wcs[wlen++] = 0xf8f5;
  1550. else
  1551. state = 1;
  1552. }
  1553. else
  1554. {
  1555. if ((BYTE)mbs[i] < 0x40)
  1556. wcs[wlen++] = invalid_char;
  1557. else
  1558. wcs[wlen++] = cptbl_gbk[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  1559. state = 0;
  1560. }
  1561. }
  1562. break;
  1563. case CP_BIG5:
  1564. invalid_char = 0x3f;
  1565. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1566. {
  1567. if (state == 0)
  1568. {
  1569. if ((BYTE)mbs[i] <= 0x80)
  1570. wcs[wlen++] = mbs[i];
  1571. else if ((BYTE)mbs[i] == 0xff)
  1572. wcs[wlen++] = 0xf8f8;
  1573. else
  1574. state = 1;
  1575. }
  1576. else
  1577. {
  1578. if ((BYTE)mbs[i] < 0x40 || ((BYTE)mbs[i] >= 0x7f && (BYTE)mbs[i] <= 0xa0))
  1579. wcs[wlen++] = invalid_char;
  1580. else if ((BYTE)mbs[i] <= 0x7e)
  1581. wcs[wlen++] = cptbl_big5[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  1582. else
  1583. wcs[wlen++] = cptbl_big5[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x60];
  1584. state = 0;
  1585. }
  1586. }
  1587. break;
  1588. case CP_UTF_8:
  1589. invalid_char = 0x3f;
  1590. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1591. {
  1592. if (state == 0)
  1593. {
  1594. if ((BYTE)mbs[i] >= 0x80)
  1595. {
  1596. BYTE s = (BYTE)mbs[i] << 1;
  1597. while (s >= 0x80) { state++; s <<= 1; }
  1598. if (state < 1 || state > 3)
  1599. {
  1600. state = 0;
  1601. wcs[wlen++] = invalid_char;
  1602. }
  1603. else
  1604. {
  1605. wcs[wlen] = s >> (state + 1);
  1606. }
  1607. }
  1608. else
  1609. wcs[wlen++] = mbs[i];
  1610. }
  1611. else
  1612. {
  1613. if ((BYTE)mbs[i] >= 0x80 && (BYTE)mbs[i] < 0xc0)
  1614. {
  1615. wcs[wlen] <<= 6;
  1616. wcs[wlen] |= (BYTE)mbs[i] & 0x3f;
  1617. if (--state == 0) wlen++;
  1618. }
  1619. else
  1620. {
  1621. state = 0;
  1622. wcs[wlen++] = invalid_char;
  1623. }
  1624. }
  1625. }
  1626. break;
  1627. default:
  1628. return -1;
  1629. }
  1630. if (state != 0 && wlen < wcslength)
  1631. {
  1632. wcs[wlen++] = invalid_char;
  1633. }
  1634. if (null || (i < mbslength && !mbs[i]))
  1635. {
  1636. if (wlen < wcslength)
  1637. wcs[wlen++] = 0;
  1638. else
  1639. wcs[wlen - 1] = 0;
  1640. }
  1641. return wlen;
  1642. }
  1643. }
  1644. INT
  1645. PAL_MultiByteToWideChar(
  1646. LPCSTR mbs,
  1647. int mbslength,
  1648. LPWSTR wcs,
  1649. int wcslength
  1650. )
  1651. /*++
  1652. Purpose:
  1653. Convert multi-byte string into the corresponding unicode string.
  1654. Parameters:
  1655. [IN] mbs - Pointer to the multi-byte string.
  1656. [IN] mbslength - Length of the multi-byte string, or -1 for auto-detect.
  1657. [IN] wcs - Pointer to the wide string buffer.
  1658. [IN] wcslength - Length of the wide string buffer.
  1659. Return value:
  1660. The length of converted wide string. If mbslength is set to -1, the returned
  1661. value includes the terminal null-char; otherwise, the null-char is not included.
  1662. If wcslength is set to 0, wcs can be set to NULL and the return value is the
  1663. required length of the wide string buffer.
  1664. --*/
  1665. {
  1666. return PAL_MultiByteToWideCharCP(g_codepage, mbs, mbslength, wcs, wcslength);
  1667. }
  1668. INT
  1669. PAL_swprintf(
  1670. LPWSTR buffer,
  1671. size_t count,
  1672. LPCWSTR format,
  1673. ...
  1674. )
  1675. /*++
  1676. Purpose:
  1677. Formatted wide-character output conversion that output Chinese characters correctly.
  1678. This function supported a subset of format strings that are commonly supported by
  1679. various C libraries, which can be formalized as following:
  1680. %[flags] [width] [.precision] [{h | l | ll}] type
  1681. When handling '%c' and '%s', this function follows the Linux's library convention,
  1682. which means '%c' and '%s' always output multi-byte strings, and '%lc' and '%ls'
  1683. always output wide-char strings.
  1684. Parameters:
  1685. [IN] buffer - Storage location for output.
  1686. [IN] count - Length of the output buffer in characters including the termination null one.
  1687. [IN] format - Format-control string.
  1688. [IN] ... - Optional arguments.
  1689. Return value:
  1690. The length of outputed wide string, not including the termination null character.
  1691. --*/
  1692. {
  1693. va_list ap;
  1694. const WCHAR * const format_end = format + wcslen(format);
  1695. const WCHAR * const buffer_end = buffer + count - 1;
  1696. WCHAR chr_buf[2] = { 0, 0 };
  1697. LPCWSTR fmt_start = NULL;
  1698. LPWSTR cur_fmt = NULL;
  1699. size_t fmt_len = 0;
  1700. int state, precision = 0, width = 0;
  1701. int left_aligned = 0, wide = 0, narrow = 0;
  1702. int width_var = 0, precision_var = 0, precision_defined = 0;
  1703. // Buffer & length check
  1704. if (buffer == NULL || format == NULL)
  1705. {
  1706. errno = EINVAL;
  1707. return -1;
  1708. }
  1709. if (buffer_end <= buffer)
  1710. return 0;
  1711. va_start(ap, format);
  1712. count = 0; state = 0;
  1713. while (buffer < buffer_end && format < format_end)
  1714. {
  1715. switch (state)
  1716. {
  1717. case 0: // Outside format spec
  1718. if (*format != L'%')
  1719. {
  1720. *buffer++ = *format++;
  1721. count++;
  1722. }
  1723. else
  1724. {
  1725. fmt_start = format++;
  1726. left_aligned = wide = narrow = 0;
  1727. precision_var = width_var = 0;
  1728. precision_defined = 0;
  1729. state = 1;
  1730. }
  1731. continue;
  1732. case 1: // [flags]
  1733. switch (*format)
  1734. {
  1735. case L'-':
  1736. left_aligned = 1;
  1737. case L'+':
  1738. case L' ':
  1739. case L'#':
  1740. case L'0':
  1741. format++;
  1742. continue;
  1743. default:
  1744. state = 2;
  1745. width = width_var = 0;
  1746. }
  1747. case 2: // [width]
  1748. switch (*format)
  1749. {
  1750. case '0':
  1751. case '1':
  1752. case '2':
  1753. case '3':
  1754. case '4':
  1755. case '5':
  1756. case '6':
  1757. case '7':
  1758. case '8':
  1759. case '9':
  1760. if (width >= 0)
  1761. width = width * 10 + (*format - L'0');
  1762. format++;
  1763. continue;
  1764. case '*':
  1765. if (width == 0)
  1766. width_var = 1;
  1767. format++;
  1768. continue;
  1769. case '.':
  1770. format++;
  1771. precision = precision_var = 0;
  1772. precision_defined = 1;
  1773. state = 3;
  1774. continue;
  1775. default:
  1776. state = 4;
  1777. continue;
  1778. }
  1779. case 3: // [.precision]
  1780. switch (*format)
  1781. {
  1782. case '0':
  1783. case '1':
  1784. case '2':
  1785. case '3':
  1786. case '4':
  1787. case '5':
  1788. case '6':
  1789. case '7':
  1790. case '8':
  1791. case '9':
  1792. if (precision >= 0)
  1793. precision = precision * 10 + (*format - L'0');
  1794. format++;
  1795. continue;
  1796. case '*':
  1797. if (precision == 0)
  1798. precision_var = 1;
  1799. format++;
  1800. continue;
  1801. default:
  1802. state = 4;
  1803. }
  1804. case 4: // [{h | l | ll}]
  1805. switch (*format)
  1806. {
  1807. case 'l': if (narrow == 0) wide++; format++; continue;
  1808. case 'h': if (wide == 0) narrow++; format++; continue;
  1809. default: state = 5;
  1810. }
  1811. case 5: // type
  1812. if (*format == 'c' || *format == 's')
  1813. {
  1814. // We handle char & str specially
  1815. LPWSTR buf;
  1816. size_t len;
  1817. int i;
  1818. // Check width
  1819. if (width_var)
  1820. {
  1821. width = va_arg(ap, int);
  1822. left_aligned = (width < 0);
  1823. width = left_aligned ? -width : width;
  1824. }
  1825. // Although precision has no meaning to '%c' output, however
  1826. // the argument still needs to be read if '.*' is provided
  1827. if (precision_var)
  1828. precision = va_arg(ap, int);
  1829. else if (!precision_defined)
  1830. precision = INT_MAX;
  1831. if (*format == 's')
  1832. {
  1833. // For ANSI string, convert it through PAL_MultiByteToWideChar
  1834. // To improve effciency, here just test the length and left
  1835. // actual conversion later directly into the output buffer
  1836. if (wide)
  1837. {
  1838. buf = va_arg(ap, LPWSTR);
  1839. len = wcslen(buf);
  1840. }
  1841. else
  1842. {
  1843. buf = (LPWSTR)va_arg(ap, LPSTR);
  1844. len = PAL_MultiByteToWideChar((LPCSTR)buf, -1, NULL, 0) - 1;
  1845. }
  1846. }
  1847. else
  1848. {
  1849. // For ANSI character, put it into the internal buffer
  1850. if (wide)
  1851. chr_buf[0] = va_arg(ap, WCHAR);
  1852. else
  1853. chr_buf[0] = va_arg(ap, int);
  1854. buf = chr_buf; len = 1;
  1855. }
  1856. // Limit output length no longer then precision
  1857. if (precision > (int)len)
  1858. precision = len;
  1859. // Left-side padding
  1860. for (i = 0; !left_aligned && i < width - precision && buffer < buffer_end; i++)
  1861. *buffer++ = L' ', count++;
  1862. // Do not overflow the output buffer
  1863. if (buffer + precision > buffer_end)
  1864. precision = buffer_end - buffer;
  1865. // Convert or copy string (char) into output buffer
  1866. if (*format == 's' && !wide)
  1867. PAL_MultiByteToWideChar((LPCSTR)buf, -1, buffer, precision);
  1868. else
  1869. wcsncpy(buffer, buf, precision);
  1870. buffer += precision; count += precision;
  1871. // Right-side padding
  1872. for (i = 0; left_aligned && i < width - precision && buffer < buffer_end; i++)
  1873. *buffer++ = L' ', count++;
  1874. }
  1875. else
  1876. {
  1877. // For other types, pass them directly into vswprintf
  1878. int cur_cnt = 0;
  1879. va_list apd;
  1880. // We copy this argument's format string into internal buffer
  1881. if (fmt_len < (size_t)(format - fmt_start + 1))
  1882. cur_fmt = realloc(cur_fmt, ((fmt_len = format - fmt_start + 1) + 1) * sizeof(WCHAR));
  1883. wcsncpy(cur_fmt, fmt_start, fmt_len);
  1884. cur_fmt[fmt_len] = L'\0';
  1885. // And pass it into vswprintf to get the output
  1886. va_copy(apd, ap);
  1887. cur_cnt = vswprintf(buffer, buffer_end - buffer, cur_fmt, apd);
  1888. va_end(apd);
  1889. buffer += cur_cnt; count += cur_cnt;
  1890. // Then we need to move the argument pointer into next one
  1891. // Check if width/precision should be read from arguments
  1892. if (width_var) va_arg(ap, int);
  1893. if (precision_var) va_arg(ap, int);
  1894. // Move pointer to pass the actual value argument
  1895. switch (*format)
  1896. {
  1897. case 'd':
  1898. case 'i':
  1899. case 'o':
  1900. case 'u':
  1901. case 'x':
  1902. case 'X':
  1903. if (wide == 1)
  1904. va_arg(ap, long);
  1905. else if (wide >= 2)
  1906. va_arg(ap, long long);
  1907. else
  1908. va_arg(ap, int);
  1909. break;
  1910. case 'e':
  1911. case 'E':
  1912. case 'f':
  1913. case 'g':
  1914. case 'G':
  1915. case 'a':
  1916. case 'A':
  1917. va_arg(ap, double);
  1918. break;
  1919. case 'p':
  1920. case 'n':
  1921. va_arg(ap, void*);
  1922. break;
  1923. }
  1924. }
  1925. state = 0;
  1926. format++;
  1927. break;
  1928. }
  1929. }
  1930. // If the format string is malformed, try to copy it into the dest buffer
  1931. if (state && buffer < buffer_end)
  1932. {
  1933. int fmt_len = format - fmt_start;
  1934. int buf_len = buffer_end - buffer;
  1935. if (fmt_len <= buf_len)
  1936. {
  1937. wcsncpy(buffer, fmt_start, buf_len);
  1938. buffer += fmt_len;
  1939. }
  1940. else
  1941. {
  1942. wcsncpy(buffer, fmt_start, buf_len);
  1943. buffer += buf_len;
  1944. }
  1945. }
  1946. // NULL-terminate the string
  1947. *buffer = L'\0';
  1948. va_end(ap);
  1949. return count;
  1950. }