GameText.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. //----------------------------------------------------------------------------
  19. //
  20. // Westwood Studios Pacific.
  21. //
  22. // Confidential Information
  23. // Copyright(C) 2001 - All Rights Reserved
  24. //
  25. //----------------------------------------------------------------------------
  26. //
  27. // Project: RTS3
  28. //
  29. // File name: GameText.cpp
  30. //
  31. // Created: 11/07/01
  32. //
  33. //----------------------------------------------------------------------------
  34. //----------------------------------------------------------------------------
  35. // Includes
  36. //----------------------------------------------------------------------------
  37. #include <stdlib.h>
  38. #include <stdio.h>
  39. #include <ctype.h>
  40. #include <string.h>
  41. #include <string>
  42. #include <Lib/BaseType.h>
  43. #include "GameText.h"
  44. #define DEBUG_LOG(x) {}
  45. #define DEBUG_ASSERTCRASH(x, y) {}
  46. //#include <Common/Language.h>
  47. //#include <Common/Debug.h>
  48. //#include <Common/std::wstring.h>
  49. //#include <Common/std::string.h>
  50. //#include <Common/GlobalData.h>
  51. #include "WSYS_File.h"
  52. #include "WSYS_RAMFile.h"
  53. //----------------------------------------------------------------------------
  54. // Externals
  55. //----------------------------------------------------------------------------
  56. //----------------------------------------------------------------------------
  57. // Defines
  58. //----------------------------------------------------------------------------
  59. #define CSF_ID ( ('C'<<24) | ('S'<<16) | ('F'<<8) | (' ') )
  60. #define CSF_LABEL ( ('L'<<24) | ('B'<<16) | ('L'<<8) | (' ') )
  61. #define CSF_STRING ( ('S'<<24) | ('T'<<16) | ('R'<<8) | (' ') )
  62. #define CSF_STRINGWITHWAVE ( ('S'<<24) | ('T'<<16) | ('R'<<8) | ('W') )
  63. #define CSF_VERSION 3
  64. #define STRING_FILE 0
  65. #define CSF_FILE 1
  66. #define MAX_UITEXT_LENGTH (10*1024)
  67. //----------------------------------------------------------------------------
  68. // Private Types
  69. //----------------------------------------------------------------------------
  70. //===============================
  71. // StringInfo
  72. //===============================
  73. struct StringInfo
  74. {
  75. std::string label;
  76. std::wstring text;
  77. std::string speech;
  78. };
  79. struct StringLookUp
  80. {
  81. std::string *label;
  82. StringInfo *info;
  83. };
  84. //===============================
  85. // CSFHeader
  86. //===============================
  87. struct CSFHeader
  88. {
  89. Int id;
  90. Int version;
  91. Int num_labels;
  92. Int num_strings;
  93. Int skip;
  94. Int langid;
  95. };
  96. //===============================
  97. // struct NoString
  98. //===============================
  99. struct NoString
  100. {
  101. struct NoString *next;
  102. std::wstring text;
  103. };
  104. //===============================
  105. // GameTextManager
  106. //===============================
  107. class GameTextManager : public GameTextInterface
  108. {
  109. public:
  110. GameTextManager();
  111. virtual ~GameTextManager();
  112. virtual void init( void ); ///< Initlaizes the text system
  113. virtual void deinit( void ); ///< De-initlaizes the text system
  114. virtual void update( void ) {}; ///< update text manager
  115. virtual void reset( void ); ///< Resets the text system
  116. virtual const wchar_t * fetch( const Char *label ); ///< Returns the associated labeled unicode text
  117. protected:
  118. Int m_textCount;
  119. Int m_maxLabelLen;
  120. Char m_buffer[MAX_UITEXT_LENGTH];
  121. Char m_buffer2[MAX_UITEXT_LENGTH];
  122. Char m_buffer3[MAX_UITEXT_LENGTH];
  123. WideChar m_tbuffer[MAX_UITEXT_LENGTH*2];
  124. StringInfo *m_stringInfo;
  125. StringLookUp *m_stringLUT;
  126. Bool m_initialized;
  127. Bool m_jabberWockie;
  128. Bool m_munkee;
  129. NoString *m_noStringList;
  130. Int m_useStringFile;
  131. std::wstring m_failed;
  132. void stripSpaces ( WideChar *string );
  133. void removeLeadingAndTrailing ( Char *m_buffer );
  134. void readToEndOfQuote( File *file, Char *in, Char *out, Char *wavefile, Int maxBufLen );
  135. void reverseWord ( Char *file, Char *lp );
  136. void translateCopy( WideChar *outbuf, Char *inbuf );
  137. Bool getStringCount( Char *filename);
  138. Bool getCSFInfo ( Char *filename );
  139. Bool parseCSF( Char *filename );
  140. Bool parseStringFile( char *filename );
  141. Bool readLine( char *buffer, Int max, File *file );
  142. Char readChar( File *file );
  143. };
  144. static int _cdecl compareLUT ( const void *, const void*);
  145. //----------------------------------------------------------------------------
  146. // Private Data
  147. //----------------------------------------------------------------------------
  148. //----------------------------------------------------------------------------
  149. // Public Data
  150. //----------------------------------------------------------------------------
  151. GameTextInterface *TheGameText = NULL;
  152. //----------------------------------------------------------------------------
  153. // Private Prototypes
  154. //----------------------------------------------------------------------------
  155. //----------------------------------------------------------------------------
  156. // Private Functions
  157. //----------------------------------------------------------------------------
  158. //----------------------------------------------------------------------------
  159. // Public Functions
  160. //----------------------------------------------------------------------------
  161. //============================================================================
  162. // CreateGameTextInterface
  163. //============================================================================
  164. GameTextInterface* CreateGameTextInterface( void )
  165. {
  166. return new GameTextManager;
  167. }
  168. //============================================================================
  169. // GameTextManager::GameTextManager
  170. //============================================================================
  171. GameTextManager::GameTextManager()
  172. : m_textCount(0),
  173. m_maxLabelLen(0),
  174. m_stringInfo(NULL),
  175. m_stringLUT(NULL),
  176. m_initialized(FALSE),
  177. m_jabberWockie(FALSE),
  178. m_munkee(FALSE),
  179. m_noStringList(NULL),
  180. m_useStringFile(TRUE),
  181. m_failed(L"***FATAL*** String Manager failed to initilaized properly")
  182. {
  183. }
  184. //============================================================================
  185. // GameTextManager::~GameTextManager
  186. //============================================================================
  187. GameTextManager::~GameTextManager()
  188. {
  189. deinit();
  190. }
  191. //============================================================================
  192. // GameTextManager::init
  193. //============================================================================
  194. extern char szArgvPath[];
  195. void GameTextManager::init( void )
  196. {
  197. Char *strFile = "autorun.str";
  198. Char *csfFile = "autorun.csf";
  199. Int format;
  200. Char realStrFile[_MAX_PATH];
  201. Char realCsfFile[_MAX_PATH];
  202. strncpy(realStrFile, szArgvPath, _MAX_PATH);
  203. strncpy(realCsfFile, szArgvPath, _MAX_PATH);
  204. strncat(realStrFile, strFile, _MAX_PATH - strlen(realStrFile));
  205. strncat(realCsfFile, csfFile, _MAX_PATH - strlen(realCsfFile));
  206. if ( m_initialized )
  207. {
  208. return;
  209. }
  210. m_initialized = TRUE;
  211. m_maxLabelLen = 0;
  212. m_jabberWockie = FALSE;
  213. m_munkee = FALSE;
  214. if ( m_useStringFile && getStringCount( realStrFile) )
  215. {
  216. format = STRING_FILE;
  217. }
  218. else if ( getCSFInfo ( realCsfFile ) )
  219. {
  220. format = CSF_FILE;
  221. }
  222. else
  223. {
  224. return;
  225. }
  226. if( (m_textCount == 0) )
  227. {
  228. return;
  229. }
  230. //Allocate StringInfo Array
  231. m_stringInfo = new StringInfo[m_textCount];
  232. if( m_stringInfo == NULL )
  233. {
  234. deinit();
  235. return;
  236. }
  237. if ( format == STRING_FILE )
  238. {
  239. if( parseStringFile( realStrFile ) == FALSE )
  240. {
  241. deinit();
  242. return;
  243. }
  244. }
  245. else
  246. {
  247. if ( !parseCSF ( realCsfFile ) )
  248. {
  249. deinit();
  250. return;
  251. }
  252. }
  253. m_stringLUT = new StringLookUp[m_textCount];
  254. StringLookUp *lut = m_stringLUT;
  255. StringInfo *info = m_stringInfo;
  256. for ( Int i = 0; i < m_textCount; i++ )
  257. {
  258. lut->info = info;
  259. lut->label = &info->label;
  260. lut++;
  261. info++;
  262. }
  263. qsort( m_stringLUT, m_textCount, sizeof(StringLookUp), compareLUT );
  264. }
  265. //============================================================================
  266. // GameTextManager::deinit
  267. //============================================================================
  268. void GameTextManager::deinit( void )
  269. {
  270. if( m_stringInfo != NULL )
  271. {
  272. delete [] m_stringInfo;
  273. m_stringInfo = NULL;
  274. }
  275. if( m_stringLUT != NULL )
  276. {
  277. delete [] m_stringLUT;
  278. m_stringLUT = NULL;
  279. }
  280. m_textCount = 0;
  281. NoString *noString = m_noStringList;
  282. DEBUG_LOG(("\n*** Missing strings ***\n"));
  283. while ( noString )
  284. {
  285. DEBUG_LOG(("*** %ls ***\n", noString->text.str()));
  286. NoString *next = noString->next;
  287. delete noString;
  288. noString = next;
  289. }
  290. DEBUG_LOG(("*** End missing strings ***\n\n"));
  291. m_noStringList = NULL;
  292. m_initialized = FALSE;
  293. }
  294. //============================================================================
  295. // GameTextManager::reset
  296. //============================================================================
  297. void GameTextManager::reset( void )
  298. {
  299. }
  300. //============================================================================
  301. // GameTextManager::stripSpaces
  302. //============================================================================
  303. void GameTextManager::stripSpaces ( WideChar *string )
  304. {
  305. WideChar *str, *ptr;
  306. WideChar ch, last = 0;
  307. Int skipall = TRUE;
  308. str = ptr = string;
  309. while ( (ch = *ptr++) != 0 )
  310. {
  311. if ( ch == ' ' )
  312. {
  313. if ( last == ' ' || skipall )
  314. {
  315. continue;
  316. }
  317. }
  318. if ( ch == '\n' || ch == '\t' )
  319. {
  320. // remove last space
  321. if ( last == ' ' )
  322. {
  323. str--;
  324. }
  325. skipall = TRUE; // skip all spaces
  326. last = *str++ = ch;
  327. continue;
  328. }
  329. last = *str++ = ch;
  330. skipall = FALSE;
  331. }
  332. if ( last == ' ' )
  333. {
  334. str--;
  335. }
  336. *str = 0;
  337. }
  338. //============================================================================
  339. // GameTextManager::removeLeadingAndTrailing
  340. //============================================================================
  341. void GameTextManager::removeLeadingAndTrailing ( Char *buffer )
  342. {
  343. Char *first, *ptr;
  344. Char ch;
  345. ptr = first = buffer;
  346. while ( (ch = *first) != 0 && iswspace ( ch ))
  347. {
  348. first++;
  349. }
  350. while ( (*ptr++ = *first++) != 0 );
  351. ptr -= 2;;
  352. while ( (ptr > buffer) && (ch = *ptr) != 0 && iswspace ( ch ) )
  353. {
  354. ptr--;
  355. }
  356. ptr++;
  357. *ptr = 0;
  358. }
  359. //============================================================================
  360. // GameTextManager::readToEndOfQuote
  361. //============================================================================
  362. void GameTextManager::readToEndOfQuote( File *file, Char *in, Char *out, Char *wavefile, Int maxBufLen )
  363. {
  364. Int slash = FALSE;
  365. Int state = 0;
  366. Int line_start = FALSE;
  367. Char ch;
  368. Int ccount = 0;
  369. Int len = 0;
  370. Int done = FALSE;
  371. while ( maxBufLen )
  372. {
  373. // get next Char
  374. if ( in )
  375. {
  376. if ( (ch = *in++) == 0 )
  377. {
  378. in = NULL; // have exhausted the input m_buffer
  379. ch = readChar ( file );
  380. }
  381. }
  382. else
  383. {
  384. ch = readChar ( file );
  385. }
  386. if ( ch == EOF )
  387. {
  388. return ;
  389. }
  390. if ( ch == '\n' )
  391. {
  392. line_start = TRUE;
  393. slash = FALSE;
  394. ccount = 0;
  395. ch = ' ';
  396. }
  397. else if ( ch == '\\' && !slash)
  398. {
  399. slash = TRUE;
  400. }
  401. else if ( ch == '\\' && slash)
  402. {
  403. slash = FALSE;
  404. }
  405. else if ( ch == '"' && !slash )
  406. {
  407. break; // done
  408. }
  409. else
  410. {
  411. slash = FALSE;
  412. }
  413. if ( iswspace ( ch ))
  414. {
  415. ch = ' ';
  416. }
  417. *out++ = ch;
  418. maxBufLen--;
  419. }
  420. *out = 0;
  421. while ( !done )
  422. {
  423. // get next Char
  424. if ( in )
  425. {
  426. if ( (ch = *in++) == 0 )
  427. {
  428. in = NULL; // have exhausted the input m_buffer
  429. ch = readChar ( file );
  430. }
  431. }
  432. else
  433. {
  434. ch = readChar ( file );
  435. }
  436. if ( ch == '\n' || ch == EOF )
  437. {
  438. break;
  439. }
  440. switch ( state )
  441. {
  442. case 0:
  443. if ( iswspace ( ch ) || ch == '=' )
  444. {
  445. break;
  446. }
  447. state = 1;
  448. case 1:
  449. if ( ( ch >= 'a' && ch <= 'z') || ( ch >= 'A' && ch <='Z') || (ch >= '0' && ch <= '9') || ch == '_' )
  450. {
  451. *wavefile++ = ch;
  452. len++;
  453. break;
  454. }
  455. state = 2;
  456. case 2:
  457. break;
  458. }
  459. }
  460. *wavefile = 0;
  461. if ( len )
  462. {
  463. if ( ( ch = *(wavefile-1)) >= '0' && ch <= '9' )
  464. {
  465. *wavefile++ = 'e';
  466. *wavefile = 0;
  467. }
  468. }
  469. }
  470. //============================================================================
  471. // GameTextManager::reverseWord
  472. //============================================================================
  473. void GameTextManager::reverseWord ( Char *file, Char *lp )
  474. {
  475. Int first = TRUE;
  476. Char f, l;
  477. Int ok = TRUE ;
  478. while ( ok )
  479. {
  480. if ( file >= lp )
  481. {
  482. return;
  483. }
  484. f = *file;
  485. l = *lp;
  486. if ( first )
  487. {
  488. if ( f >= 'A' && f <= 'Z' )
  489. {
  490. if ( l >= 'a' && l <= 'z' )
  491. {
  492. f = (f - 'A') + 'a';
  493. l = (l - 'a') + 'A';
  494. }
  495. }
  496. first = FALSE;
  497. }
  498. *lp-- = f;
  499. *file++ = l;
  500. }
  501. }
  502. //============================================================================
  503. // GameTextManager::translateCopy
  504. //============================================================================
  505. void GameTextManager::translateCopy( WideChar *outbuf, Char *inbuf )
  506. {
  507. Bool slash = FALSE;
  508. if ( m_jabberWockie )
  509. {
  510. static Char buffer[MAX_UITEXT_LENGTH*2];
  511. Char *firstLetter = NULL, *lastLetter;
  512. Char *b = buffer;
  513. Int formatWord = FALSE;
  514. Char ch;
  515. while ( (ch = *inbuf++) != 0 )
  516. {
  517. if ( ! (( ch >= 'a' && ch <= 'z') || ( ch >= 'A' && ch <= 'Z' )))
  518. {
  519. if ( firstLetter )
  520. {
  521. if ( !formatWord )
  522. {
  523. lastLetter = b-1;
  524. reverseWord ( firstLetter, lastLetter );
  525. }
  526. firstLetter = NULL;
  527. formatWord = FALSE;
  528. }
  529. *b++ = ch;
  530. if ( ch == '\\' )
  531. {
  532. *b++ = *inbuf++;
  533. }
  534. if ( ch == '%' )
  535. {
  536. while ( (ch = *inbuf++) != 0 && !( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')))
  537. {
  538. *b++ = ch;
  539. }
  540. *b++ = ch;
  541. }
  542. }
  543. else
  544. {
  545. if ( !firstLetter )
  546. {
  547. firstLetter = b;
  548. }
  549. *b++ = ch;
  550. }
  551. }
  552. if ( firstLetter )
  553. {
  554. lastLetter = b-1;
  555. reverseWord ( firstLetter, lastLetter );
  556. }
  557. *b++ = 0;
  558. inbuf = buffer;
  559. }
  560. else if( m_munkee )
  561. {
  562. wcscpy(outbuf, L"Munkee");
  563. return;
  564. }
  565. while( *inbuf != '\0' )
  566. {
  567. if( slash == TRUE )
  568. {
  569. slash = FALSE;
  570. switch( *inbuf )
  571. {
  572. // in case end of string is reached
  573. // should never happen!!!
  574. case '\0':
  575. return;
  576. case '\\':
  577. *outbuf++ = '\\';
  578. break;
  579. case '\'':
  580. *outbuf++ = '\'';
  581. break;
  582. case '\"':
  583. *outbuf++ = '\"';
  584. break;
  585. case '\?':
  586. *outbuf++ = '\?';
  587. break;
  588. case 't':
  589. *outbuf++ = '\t';
  590. break;
  591. case 'n':
  592. *outbuf++ = '\n';
  593. break;
  594. default:
  595. *outbuf++ = *inbuf & 0x00FF;
  596. break;
  597. }
  598. }
  599. else if( *inbuf != '\\' )
  600. {
  601. *outbuf++ = *inbuf & 0x00FF;
  602. }
  603. else
  604. slash = TRUE;
  605. inbuf++;
  606. }
  607. *outbuf= 0;
  608. }
  609. //============================================================================
  610. // GameTextManager::getStringCount
  611. //============================================================================
  612. Bool GameTextManager::getStringCount( char *filename )
  613. {
  614. Int ok = TRUE;
  615. m_textCount = 0;
  616. RAMFile file;
  617. if ( !file.open( filename, File::READ | File::TEXT ))
  618. {
  619. return FALSE;
  620. }
  621. while(ok)
  622. {
  623. if( !readLine( m_buffer, sizeof( m_buffer) -1, &file ) )
  624. break;
  625. removeLeadingAndTrailing ( m_buffer );
  626. if( m_buffer[0] == '"' )
  627. {
  628. Int len = strlen(m_buffer);
  629. m_buffer[ len ] = '\n';
  630. m_buffer[ len+1] = 0;
  631. readToEndOfQuote( &file, &m_buffer[1], m_buffer2, m_buffer3, MAX_UITEXT_LENGTH );
  632. }
  633. else if( !stricmp( m_buffer, "END") )
  634. {
  635. m_textCount++;
  636. }
  637. }
  638. m_textCount += 500;
  639. file.close();
  640. return TRUE;
  641. }
  642. //============================================================================
  643. // GameTextManager::getCSFInfo
  644. //============================================================================
  645. Bool GameTextManager::getCSFInfo ( Char *filename )
  646. {
  647. CSFHeader header;
  648. Int ok = FALSE;
  649. RAMFile file;
  650. if ( file.open( filename, File::READ | File::BINARY ) )
  651. {
  652. if ( file.read( &header, sizeof ( header )) == sizeof ( header ) )
  653. {
  654. if ( header.id == CSF_ID )
  655. {
  656. m_textCount = header.num_labels;
  657. ok = TRUE;
  658. }
  659. }
  660. file.close();
  661. }
  662. return ok;
  663. }
  664. //============================================================================
  665. // GameTextManager::parseCSF
  666. //============================================================================
  667. Bool GameTextManager::parseCSF( Char *filename )
  668. {
  669. RAMFile file;
  670. Int id;
  671. Int len;
  672. Int listCount = 0;
  673. Bool ok = FALSE;
  674. CSFHeader header;
  675. if ( !file.open( filename, File::READ | File::BINARY ) )
  676. {
  677. return FALSE;
  678. }
  679. if ( file.read ( &header, sizeof ( CSFHeader)) != sizeof ( CSFHeader) )
  680. {
  681. return FALSE;
  682. }
  683. while( file.read ( &id, sizeof (id)) == sizeof ( id) )
  684. {
  685. Int num;
  686. Int num_strings;
  687. if ( id != CSF_LABEL )
  688. {
  689. goto quit;
  690. }
  691. file.read ( &num_strings, sizeof ( Int ));
  692. file.read ( &len, sizeof ( Int ) );
  693. if ( len )
  694. {
  695. file.read ( m_buffer, len );
  696. }
  697. m_buffer[len] = 0;
  698. m_stringInfo[listCount].label = m_buffer;
  699. if ( len > m_maxLabelLen )
  700. {
  701. m_maxLabelLen = len;
  702. }
  703. num = 0;
  704. while ( num < num_strings )
  705. {
  706. file.read ( &id, sizeof ( Int ) );
  707. if ( id != CSF_STRING && id != CSF_STRINGWITHWAVE )
  708. {
  709. goto quit;
  710. }
  711. file.read ( &len, sizeof ( Int ) );
  712. if ( len )
  713. {
  714. file.read ( m_tbuffer, len*sizeof(WideChar) );
  715. }
  716. if ( num == 0 )
  717. {
  718. // only use the first string found
  719. m_tbuffer[len] = 0;
  720. {
  721. WideChar *ptr;
  722. ptr = m_tbuffer;
  723. while ( *ptr )
  724. {
  725. *ptr = ~*ptr;
  726. ptr++;
  727. }
  728. }
  729. stripSpaces ( m_tbuffer );
  730. m_stringInfo[listCount].text = m_tbuffer;
  731. }
  732. if ( id == CSF_STRINGWITHWAVE )
  733. {
  734. file.read ( &len, sizeof ( Int ) );
  735. if ( len )
  736. {
  737. file.read ( m_buffer, len );
  738. }
  739. m_buffer[len] = 0;
  740. if ( num == 0 && len )
  741. {
  742. // only use the first string found
  743. m_stringInfo[listCount].speech = m_buffer;
  744. }
  745. }
  746. num++;
  747. }
  748. listCount++;
  749. }
  750. ok = TRUE;
  751. quit:
  752. file.close();
  753. return ok;
  754. }
  755. //============================================================================
  756. // GameTextManager::parseStringFile
  757. //============================================================================
  758. Bool GameTextManager::parseStringFile( char *filename )
  759. {
  760. Int listCount = 0;
  761. Int ok = TRUE;
  762. RAMFile file;
  763. if ( !file.open( filename, File::READ | File::TEXT ) )
  764. {
  765. return FALSE;
  766. }
  767. while( ok )
  768. {
  769. Int len;
  770. if( !readLine( m_buffer, MAX_UITEXT_LENGTH, &file ))
  771. {
  772. break;
  773. }
  774. removeLeadingAndTrailing ( m_buffer );
  775. if( ( *(unsigned short *)m_buffer == 0x2F2F) || !m_buffer[0]) // 0x2F2F is Hex for //
  776. continue;
  777. // make sure label is unique
  778. for ( Int i = 0; i < listCount; i++ )
  779. {
  780. if ( !stricmp ( m_stringInfo[i].label.c_str(), m_buffer ))
  781. {
  782. DEBUG_ASSERTCRASH ( FALSE, ("String label '%s' multiply defined!", m_buffer ));
  783. }
  784. }
  785. m_stringInfo[listCount].label = m_buffer;
  786. len = strlen ( m_buffer );
  787. if ( len > m_maxLabelLen )
  788. {
  789. m_maxLabelLen = len;
  790. }
  791. Bool readString = FALSE;
  792. while( ok )
  793. {
  794. if (!readLine ( m_buffer, sizeof(m_buffer)-1, &file ))
  795. {
  796. DEBUG_ASSERTCRASH (FALSE, ("Unexpected end of string file"));
  797. ok = FALSE;
  798. goto quit;
  799. }
  800. removeLeadingAndTrailing ( m_buffer );
  801. if( m_buffer[0] == '"' )
  802. {
  803. len = strlen(m_buffer);
  804. m_buffer[ len ] = '\n';
  805. m_buffer[ len+1] = 0;
  806. readToEndOfQuote( &file, &m_buffer[1], m_buffer2, m_buffer3, MAX_UITEXT_LENGTH );
  807. if ( readString )
  808. {
  809. // only one string per label allows
  810. DEBUG_ASSERTCRASH ( FALSE, ("String label '%s' has more than one string defined!", m_stringInfo[listCount].label.str()));
  811. }
  812. else
  813. {
  814. // Copy string into new home
  815. translateCopy( m_tbuffer, m_buffer2 );
  816. stripSpaces ( m_tbuffer );
  817. m_stringInfo[listCount].text = m_tbuffer ;
  818. m_stringInfo[listCount].speech = m_buffer3;
  819. readString = TRUE;
  820. }
  821. }
  822. else if ( !stricmp ( m_buffer, "END" ))
  823. {
  824. break;
  825. }
  826. }
  827. listCount++;
  828. }
  829. quit:
  830. file.close();
  831. return ok;
  832. }
  833. //============================================================================
  834. // *GameTextManager::fetch
  835. //============================================================================
  836. const wchar_t * GameTextManager::fetch( const Char *label )
  837. {
  838. DEBUG_ASSERTCRASH ( m_initialized, ("String Manager has not been m_initialized") );
  839. if( m_stringInfo == NULL )
  840. {
  841. return m_failed.c_str();
  842. }
  843. StringLookUp *lookUp;
  844. StringLookUp key;
  845. std::string lb;
  846. lb = label;
  847. key.info = NULL;
  848. key.label = &lb;
  849. lookUp = (StringLookUp *) bsearch( &key, (void*) m_stringLUT, m_textCount, sizeof(StringLookUp), compareLUT );
  850. if( lookUp == NULL )
  851. {
  852. // See if we already have the missing string
  853. wchar_t tmp[256];
  854. _snwprintf(tmp, 256, L"MISSING: '%hs'", label);
  855. tmp[255] = 0;
  856. std::wstring missingString = tmp;
  857. NoString *noString = m_noStringList;
  858. while ( noString )
  859. {
  860. if (noString->text == missingString)
  861. return missingString.c_str();
  862. noString = noString->next;
  863. }
  864. //DEBUG_LOG(("*** MISSING:'%s' ***\n", label));
  865. // Remember file could have been altered at this point.
  866. noString = new NoString;
  867. noString->text = missingString;
  868. noString->next = m_noStringList;
  869. m_noStringList = noString;
  870. return noString->text.c_str();
  871. }
  872. return lookUp->info->text.c_str();
  873. }
  874. //============================================================================
  875. // GameTextManager::readLine
  876. //============================================================================
  877. Bool GameTextManager::readLine( char *buffer, Int max, File *file )
  878. {
  879. Int ok = FALSE;
  880. while ( max && file->read( buffer, 1 ) == 1 )
  881. {
  882. ok = TRUE;
  883. if ( *buffer == '\n' )
  884. {
  885. break;
  886. }
  887. buffer++;
  888. max--;
  889. }
  890. *buffer = 0;
  891. return ok;
  892. }
  893. //============================================================================
  894. // GameTextManager::readChar
  895. //============================================================================
  896. Char GameTextManager::readChar( File *file )
  897. {
  898. Char ch;
  899. if ( file->read( &ch, 1 ) == 1 )
  900. {
  901. return ch;
  902. }
  903. return 0;
  904. }
  905. //============================================================================
  906. // GameTextManager::compareLUT
  907. //============================================================================
  908. static int __cdecl compareLUT ( const void *i1, const void*i2)
  909. {
  910. StringLookUp *lut1 = (StringLookUp*) i1;
  911. StringLookUp *lut2 = (StringLookUp*) i2;
  912. return stricmp( lut1->label->c_str(), lut2->label->c_str());
  913. }