mixfile.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. *** Confidential - Westwood Studios ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Commando *
  23. * *
  24. * $Archive:: /Commando/Code/wwlib/mixfile.cpp $*
  25. * *
  26. * $Author:: Patrick $*
  27. * *
  28. * $Modtime:: 9/12/01 7:39p $*
  29. * *
  30. * $Revision:: 4 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "mixfile.h"
  36. #include "wwdebug.h"
  37. #include "ffactory.h"
  38. #include "wwfile.h"
  39. #include "realcrc.h"
  40. #include "rawfile.h"
  41. #include "win.h"
  42. #include "bittype.h"
  43. /*
  44. **
  45. */
  46. typedef struct
  47. {
  48. char signature[4];
  49. long header_offset;
  50. long names_offset;
  51. } MIXFILE_HEADER;
  52. typedef struct
  53. {
  54. long file_count;
  55. } MIXFILE_DATA_HEADER;
  56. struct FileOffsetStruct {
  57. bool operator== (const FileOffsetStruct &src) { return false; }
  58. bool operator!= (const FileOffsetStruct &src) { return true; }
  59. StringClass Filename;
  60. unsigned long Offset;
  61. };
  62. /*
  63. **
  64. */
  65. MixFileFactoryClass::MixFileFactoryClass( const char * mix_filename, FileFactoryClass * factory ) :
  66. FileCount (0),
  67. NamesOffset (0),
  68. IsValid (false),
  69. BaseOffset (0),
  70. Factory (NULL),
  71. IsModified (false)
  72. {
  73. // WWDEBUG_SAY(( "MixFileFactory( %s )\n", mix_filename ));
  74. MixFilename = mix_filename;
  75. Factory = factory;
  76. FilenameList.Set_Growth_Step (1000);
  77. // First, open the mix file
  78. FileClass * file = factory->Get_File( mix_filename );
  79. // WWASSERT( file );
  80. if ( file && file->Is_Available() ) {
  81. file->Open();
  82. IsValid = true;
  83. //
  84. // Read the file header
  85. //
  86. MIXFILE_HEADER header = { 0 };
  87. IsValid = (file->Read( &header, sizeof( header ) ) == sizeof( header ));
  88. //
  89. // Validate the file header
  90. //
  91. if ( IsValid ) {
  92. IsValid = (::memcmp( header.signature, "MIX1", sizeof ( header.signature ) ) == 0);
  93. }
  94. //
  95. // Seek to the data start
  96. //
  97. FileCount = 0;
  98. if ( IsValid ) {
  99. file->Seek( header.header_offset, SEEK_SET );
  100. IsValid = ( file->Read( &FileCount, sizeof( FileCount ) ) == sizeof( FileCount ) );
  101. }
  102. //
  103. // Read the array of data headers
  104. //
  105. if ( IsValid ) {
  106. FileInfo.Resize( FileCount );
  107. int size = FileCount * sizeof( FileInfoStruct );
  108. IsValid = ( file->Read( &FileInfo[0], size ) == size );
  109. }
  110. //
  111. // Check for success
  112. //
  113. if ( IsValid ) {
  114. BaseOffset = 0;
  115. NamesOffset = header.names_offset;
  116. WWDEBUG_SAY(( "MixFileFactory( %s ) loaded successfully %d files\n", MixFilename, FileInfo.Length() ));
  117. } else {
  118. FileInfo.Resize(0);
  119. }
  120. factory->Return_File( file );
  121. } else {
  122. WWDEBUG_SAY(( "MixFileFactory( %s ) FAILED\n", mix_filename ));
  123. }
  124. }
  125. MixFileFactoryClass::~MixFileFactoryClass( void )
  126. {
  127. FileInfo.Resize(0);
  128. }
  129. bool MixFileFactoryClass::Build_Filename_List (DynamicVectorClass<StringClass> &list)
  130. {
  131. if (IsValid == false) {
  132. return false;
  133. }
  134. bool retval = false;
  135. //
  136. // Attempt to open the file
  137. //
  138. RawFileClass *file = (RawFileClass *)Factory->Get_File( MixFilename );
  139. if ( file != NULL && file->Open ( RawFileClass::READ ) ) {
  140. //
  141. // Seek to the names offset header
  142. //
  143. file->Seek (NamesOffset, SEEK_SET);
  144. retval = true;
  145. //
  146. // Read the count of files
  147. //
  148. int file_count = 0;
  149. if (file->Read( &file_count, sizeof( file_count) ) == sizeof( file_count )) {
  150. //
  151. // Loop over each saved filename
  152. //
  153. bool keep_going = true;
  154. for (int index = 0; index < file_count && keep_going; index ++) {
  155. keep_going = false;
  156. //
  157. // Get the length of the filename
  158. //
  159. uint8 name_len = 0;
  160. if (file->Read( &name_len, sizeof( name_len ) ) == sizeof( name_len )) {
  161. //
  162. // Read the filename
  163. //
  164. StringClass filename;
  165. if (file->Read( filename.Get_Buffer( name_len ), name_len ) == name_len ) {
  166. //
  167. // Add the filename to our list
  168. //
  169. list.Add( filename );
  170. keep_going = true;
  171. }
  172. }
  173. }
  174. }
  175. //
  176. // Close the file
  177. //
  178. Factory->Return_File( file );
  179. }
  180. return retval;
  181. }
  182. FileClass * MixFileFactoryClass::Get_File( char const *filename )
  183. {
  184. if ( FileInfo.Length() == 0 ) {
  185. return NULL;
  186. }
  187. // WWDEBUG_SAY(( "MixFileFactoryClass::Get_File( %s )\n", filename ));
  188. RawFileClass *file = NULL;
  189. // Create the key block that will be used to binary search for the file.
  190. unsigned long crc = CRC_Stringi( filename );
  191. // Binary search for the file in this mixfile. If it is found, then create the file
  192. FileInfoStruct * info = NULL;
  193. FileInfoStruct * base = &FileInfo[0];
  194. int stride = FileInfo.Length();
  195. while (stride > 0) {
  196. int pivot = stride / 2;
  197. FileInfoStruct * tryptr = base + pivot;
  198. if (crc < tryptr->CRC) {
  199. stride = pivot;
  200. } else {
  201. if (tryptr->CRC == crc) {
  202. info = tryptr;
  203. break;
  204. }
  205. base = tryptr + 1;
  206. stride -= pivot + 1;
  207. }
  208. }
  209. if ( info != NULL) {
  210. // WWDEBUG_SAY(( "MixFileFactoryClass::Get_File( %s ) FOUND\n", filename ));
  211. file = (RawFileClass *)Factory->Get_File( MixFilename );
  212. if ( file ) {
  213. file->Bias( BaseOffset + info->Offset, info->Size );
  214. }
  215. // WWDEBUG_SAY(( "MixFileFactoryClass::Get_File( %s ) FOUND\n", filename ));
  216. } else {
  217. // WWDEBUG_SAY(( "MixFileFactoryClass::Get_File( %s ) NOT FOUND\n", filename ));
  218. }
  219. return file;
  220. }
  221. void MixFileFactoryClass::Return_File( FileClass * file )
  222. {
  223. if ( file != NULL ) {
  224. Factory->Return_File( file );
  225. }
  226. }
  227. /*
  228. **
  229. */
  230. void
  231. MixFileFactoryClass::Add_File (const char *full_path, const char *filename)
  232. {
  233. AddInfoStruct info;
  234. info.FullPath = full_path;
  235. info.Filename = filename;
  236. PendingAddFileList.Add (info);
  237. IsModified = true;
  238. return ;
  239. }
  240. /*
  241. **
  242. */
  243. void
  244. MixFileFactoryClass::Delete_File (const char *filename)
  245. {
  246. //
  247. // Remove this file (if it exists) from our filename list
  248. //
  249. for (int list_index = 0; list_index < FilenameList.Count (); list_index ++) {
  250. if (FilenameList[list_index].Compare_No_Case (filename) == 0) {
  251. FilenameList.Delete (list_index);
  252. IsModified = true;
  253. break;
  254. }
  255. }
  256. return ;
  257. }
  258. /*
  259. **
  260. */
  261. void
  262. MixFileFactoryClass::Flush_Changes (void)
  263. {
  264. //
  265. // Exit if there's nothing to do.
  266. //
  267. if (IsModified == false) {
  268. return ;
  269. }
  270. //
  271. // Get the path of the mix file
  272. //
  273. char drive[_MAX_DRIVE] = { 0 };
  274. char dir[_MAX_DIR] = { 0 };
  275. ::_splitpath (MixFilename, drive, dir, NULL, NULL);
  276. StringClass path = drive;
  277. path += dir;
  278. //
  279. // Try to find a temp filename
  280. //
  281. StringClass full_path;
  282. if (Get_Temp_Filename (path, full_path)) {
  283. MixFileCreator new_mix_file (full_path);
  284. //
  285. // Add all the remaining files from our file set
  286. //
  287. for (int index = 0; index < FilenameList.Count (); index ++) {
  288. StringClass &filename = FilenameList[index];
  289. //
  290. // Copy this file data to the mix file
  291. //
  292. FileClass *file_data = Get_File (filename);
  293. if (file_data != NULL) {
  294. file_data->Open ();
  295. new_mix_file.Add_File (filename, file_data);
  296. Return_File (file_data);
  297. //
  298. // Remove this file from the pending list (if necessary)
  299. //
  300. for (int temp_index = 0; temp_index < PendingAddFileList.Count (); temp_index ++) {
  301. if (filename.Compare_No_Case (PendingAddFileList[temp_index].Filename) == 0) {
  302. PendingAddFileList.Delete (temp_index);
  303. break;
  304. }
  305. }
  306. }
  307. }
  308. //
  309. // Add the new files that are pending
  310. //
  311. for (index = 0; index < PendingAddFileList.Count (); index ++) {
  312. new_mix_file.Add_File (PendingAddFileList[index].FullPath, PendingAddFileList[index].Filename);
  313. }
  314. }
  315. //
  316. // Delete the old mix file and rename the new one
  317. //
  318. ::DeleteFile (MixFilename);
  319. ::MoveFile (full_path, MixFilename);
  320. //
  321. // Reset the lists
  322. //
  323. IsModified = false;
  324. PendingAddFileList.Delete_All ();
  325. return ;
  326. }
  327. /*
  328. **
  329. */
  330. bool
  331. MixFileFactoryClass::Get_Temp_Filename (const char *path, StringClass &full_path)
  332. {
  333. bool retval = false;
  334. StringClass temp_path = path;
  335. temp_path += "_tmpmix";
  336. //
  337. // Try to find a unique temp filename
  338. //
  339. for (int index = 0; index < 20; index ++) {
  340. full_path.Format ("%s%.2d.dat", (const char *)temp_path, index + 1);
  341. if (GetFileAttributes (full_path) == 0xFFFFFFFF) {
  342. retval = true;
  343. break;
  344. }
  345. }
  346. return retval;
  347. }
  348. //
  349. // Comparison function, used by Build_Ordered_Filename_List
  350. //
  351. int MixFileFactoryClass::File_Offset_Compare(const void * a, const void * b)
  352. {
  353. unsigned int OffsetA = ((FileOffsetStruct*)a)->Offset;
  354. unsigned int OffsetB = ((FileOffsetStruct*)b)->Offset;
  355. if ( OffsetA < OffsetB ) return -1;
  356. if ( OffsetA > OffsetB ) return 1;
  357. return 0;
  358. }
  359. //
  360. // Function builds a list of file names in the order that they are stored in the file
  361. //
  362. bool MixFileFactoryClass::Build_Ordered_Filename_List (DynamicVectorClass<StringClass> &list)
  363. {
  364. if (IsValid == false) {
  365. return false;
  366. }
  367. // get list of filenames
  368. DynamicVectorClass<StringClass> name_list;
  369. if (!Build_Filename_List(name_list)) {
  370. return false;
  371. }
  372. // associate offset with each name and add to list
  373. DynamicVectorClass<FileOffsetStruct> local_file_info;
  374. local_file_info.Resize( name_list.Count());
  375. for (int i = 0; i < name_list.Count(); ++i) {
  376. // Here, we have to assume that the names in the list are in CRC order, just like FileInfo is.
  377. FileOffsetStruct temp;
  378. temp.Filename = name_list[i];
  379. temp.Offset = FileInfo[i].Offset;
  380. local_file_info.Add( temp );
  381. }
  382. // sort name/offset by offset
  383. if (local_file_info.Count() > 1) {
  384. qsort( &local_file_info[0], local_file_info.Count(), sizeof(local_file_info[0]), &File_Offset_Compare);
  385. }
  386. // add names to output parameter
  387. list.Clear();
  388. list.Resize( name_list.Count());
  389. for (i = 0; i < local_file_info.Count(); ++i) {
  390. list.Add(local_file_info[i].Filename);
  391. }
  392. return true;
  393. }
  394. /*
  395. **
  396. */
  397. SimpleFileFactoryClass _SimpleFileFactory;
  398. MixFileCreator::MixFileCreator( const char * filename )
  399. {
  400. WWDEBUG_SAY(( "Creating Mix File %s\n", filename ));
  401. MixFile = _SimpleFileFactory.Get_File(filename);
  402. if ( MixFile != NULL ) {
  403. MixFile->Open( FileClass::WRITE );
  404. MixFile->Write( "MIX1", 4 );
  405. long header_offset = 0;
  406. MixFile->Write( &header_offset, sizeof( header_offset ) );
  407. long names_offset = 0;
  408. MixFile->Write( &names_offset, sizeof( names_offset ) );
  409. long unused = 0;
  410. MixFile->Write( &unused, sizeof( unused ) );
  411. }
  412. }
  413. int MixFileCreator::File_Info_Compare(const void * a, const void * b)
  414. {
  415. unsigned int CRCA = ((FileInfoStruct*)a)->CRC;
  416. unsigned int CRCB = ((FileInfoStruct*)b)->CRC;
  417. if ( CRCA < CRCB ) return -1;
  418. if ( CRCA > CRCB ) return 1;
  419. return 0;
  420. // return ((FileInfoStruct*)a)->CRC - ((FileInfoStruct*)b)->CRC;
  421. }
  422. MixFileCreator::~MixFileCreator( void )
  423. {
  424. if ( MixFile != NULL ) {
  425. // Save Header Data
  426. int header_offset = MixFile->Tell();
  427. // Save file count
  428. int i,num_files = FileInfo.Count();
  429. WWDEBUG_SAY(( "Closing with %d files\n", num_files ));
  430. MixFile->Write( &num_files, sizeof( num_files ) );
  431. if ( num_files > 1 ) {
  432. qsort( &FileInfo[0], num_files, sizeof(FileInfo[0]), &File_Info_Compare);
  433. }
  434. // Save file info (CRC, Offset, Size )
  435. for ( i = 0; i < num_files; i++ ) {
  436. MixFile->Write( &FileInfo[i].CRC, 4 );
  437. MixFile->Write( &FileInfo[i].Offset, 4 );
  438. MixFile->Write( &FileInfo[i].Size, 4 );
  439. // WWDEBUG_SAY(( "Write CRC %08X\n", FileInfo[i].CRC ));
  440. }
  441. // ---------------------------------------
  442. // Save Names Data
  443. int names_offset = MixFile->Tell();
  444. // Save file count
  445. MixFile->Write( &num_files, sizeof( num_files ) );
  446. // Save file info
  447. for ( i = 0; i < num_files; i++ ) {
  448. const char * filename = FileInfo[i].Filename;
  449. int size = FileInfo[i].Filename.Get_Length()+1;
  450. WWASSERT( size < 255 );
  451. unsigned char csize = size;
  452. MixFile->Write( &csize, 1 );
  453. MixFile->Write( filename, size );
  454. }
  455. // ---------------------------------------
  456. MixFile->Seek( 4, SEEK_SET );
  457. // Save header offset
  458. WWDEBUG_SAY(( "Writing header offset %d (%08X)\n", header_offset, header_offset ));
  459. MixFile->Write( &header_offset, sizeof( header_offset ) );
  460. // Save names offset
  461. WWDEBUG_SAY(( "Writing names offset %d (%08X)\n", names_offset, names_offset ));
  462. MixFile->Write( &names_offset, sizeof( names_offset ) );
  463. // ---------------------------------------
  464. MixFile->Close();
  465. _SimpleFileFactory.Return_File(MixFile);
  466. }
  467. }
  468. void MixFileCreator::Add_File( const char * source_filename, const char * saved_filename )
  469. {
  470. if ( saved_filename == NULL ) {
  471. saved_filename = source_filename;
  472. }
  473. if ( MixFile != NULL ) {
  474. FileClass * file = _SimpleFileFactory.Get_File( source_filename );
  475. if ( file && file->Is_Available() ) {
  476. file->Open();
  477. MixFileCreator::FileInfoStruct info;
  478. info.CRC = CRC_Stringi( saved_filename );
  479. info.Offset = MixFile->Tell();
  480. info.Size = file->Size();
  481. FileInfo.Add( info );
  482. FileInfo[ FileInfo.Count()-1 ].Filename = saved_filename;
  483. WWDEBUG_SAY(( "Saving File %s CRC %08X Offset %d (0x%08X) Size %d (0x%08X)\n",
  484. saved_filename, info.CRC, info.Offset, info.Offset, info.Size, info.Size ));
  485. int size = file->Size();
  486. while ( size ) {
  487. char buffer[ 4096 ];
  488. int amount = MIN( sizeof( buffer ), size );
  489. size -= amount;
  490. file->Read( buffer, amount );
  491. if ( MixFile->Write( buffer, amount ) != amount ) {
  492. WWDEBUG_SAY(( "Failed to write MixFile\n" ));
  493. }
  494. }
  495. // Pad the MixFile to make DWord Aligned
  496. int offset = MixFile->Tell();
  497. offset = (8-(offset & 7)) & 7;
  498. if ( offset != 0 ) {
  499. char zeros[8] = {0,0,0,0,0,0,0,0};
  500. if ( MixFile->Write( zeros, offset ) != offset ) {
  501. WWDEBUG_SAY(( "Failed to write padding\n" ));
  502. }
  503. }
  504. file->Close();
  505. _SimpleFileFactory.Return_File( file );
  506. } else {
  507. WWDEBUG_SAY(( "MixFileCreator::Failed to open \"%s\"\n", source_filename ));
  508. }
  509. }
  510. }
  511. void MixFileCreator::Add_File( const char * filename, FileClass *file )
  512. {
  513. if ( MixFile != NULL ) {
  514. MixFileCreator::FileInfoStruct info;
  515. info.CRC = CRC_Stringi( filename );
  516. info.Offset = MixFile->Tell();
  517. info.Size = file->Size();
  518. FileInfo.Add( info );
  519. FileInfo[ FileInfo.Count()-1 ].Filename = filename;
  520. WWDEBUG_SAY(( "Saving File %s CRC %08X Offset %d (0x%08X) Size %d (0x%08X)\n",
  521. filename, info.CRC, info.Offset, info.Offset, info.Size, info.Size ));
  522. int size = file->Size();
  523. while ( size ) {
  524. char buffer[ 4096 ];
  525. int amount = MIN( sizeof( buffer ), size );
  526. size -= amount;
  527. file->Read( buffer, amount );
  528. if ( MixFile->Write( buffer, amount ) != amount ) {
  529. WWDEBUG_SAY(( "Failed to write MixFile\n" ));
  530. }
  531. }
  532. // Pad the MixFile to make DWord Aligned
  533. int offset = MixFile->Tell();
  534. offset = (8-(offset & 7)) & 7;
  535. if ( offset != 0 ) {
  536. char zeros[8] = {0,0,0,0,0,0,0,0};
  537. if ( MixFile->Write( zeros, offset ) != offset ) {
  538. WWDEBUG_SAY(( "Failed to write padding\n" ));
  539. }
  540. }
  541. }
  542. return ;
  543. }
  544. /*
  545. **
  546. */
  547. void Add_Files( const char * dir, MixFileCreator & mix )
  548. {
  549. BOOL bcontinue = TRUE;
  550. HANDLE hfile_find;
  551. WIN32_FIND_DATA find_info = {0};
  552. StringClass path;
  553. path.Format( "data\\makemix\\%s*.*", dir );
  554. WWDEBUG_SAY(( "Adding files from %s\n", path ));
  555. for (hfile_find = ::FindFirstFile( path, &find_info);
  556. (hfile_find != INVALID_HANDLE_VALUE) && bcontinue;
  557. bcontinue = ::FindNextFile(hfile_find, &find_info)) {
  558. if ( find_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
  559. if ( find_info.cFileName[0] != '.' ) {
  560. StringClass path;
  561. path.Format( "%s%s\\", dir, find_info.cFileName );
  562. Add_Files( path, mix );
  563. }
  564. } else {
  565. StringClass name;
  566. name.Format( "%s%s", dir, find_info.cFileName );
  567. StringClass source;
  568. source.Format( "makemix\\%s", name );
  569. mix.Add_File( source, name );
  570. // WWDEBUG_SAY(( "Adding file from %s %s\n", source, name ));
  571. }
  572. }
  573. }
  574. void Setup_Mix_File( void )
  575. {
  576. _SimpleFileFactory.Set_Sub_Directory( "DATA\\" );
  577. // _SimpleFileFactory.Set_Strip_Path( true );
  578. WWDEBUG_SAY(( "Mix File Create .....\n" ));
  579. {
  580. MixFileCreator mix( "MAKEMIX.MIX" );
  581. Add_Files( "", mix );
  582. }
  583. }