mixfile.cpp 16 KB

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