w3d_dep.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : G *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/w3d_dep.cpp $*
  25. * *
  26. * $Author:: Byon_g $*
  27. * *
  28. * $Modtime:: 7/23/01 6:17p $*
  29. * *
  30. * $Revision:: 4 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * Get_W3D_Dependencies -- Scans a W3D file to determine which other files it depends on. *
  35. * Scan_Chunk -- Chooses the correct chunk loader for this chunk. *
  36. * Scan_Mesh -- Scans a mesh for references to other files. *
  37. * Scan_Mesh_Header -- Scans a mesh's header for file references. *
  38. * Scan_Mesh_Textures -- Scans a mesh's textures. *
  39. * Scan_HTree -- Scans an HTree for references to other files. *
  40. * Scan_Anim -- Scans an animation for references to other files. *
  41. * Scan_Compressed_Anim -- Scans an animation for references to other files. *
  42. * Scan_HModel -- Scans an HModel for references to other files. *
  43. * Scan_Emitter -- Scans an emitter for references to other files. *
  44. * Scan_Aggregate -- Scans an aggregate for references to other files. *
  45. * Scan_HLOD -- Scans an HLOD for references to other files. *
  46. * Get_W3D_Name -- Gets a W3D object name from a W3D filename. *
  47. * Make_W3D_Filename -- Converts a W3D object name into a W3D filename. *
  48. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  49. //-----------------------------------------------------------------------------
  50. // srj sez: hack festival :-(
  51. class STLSpecialAlloc
  52. {
  53. public:
  54. // this one is needed for proper simple_alloc wrapping
  55. static void* allocate(size_t __n) { return ::operator new(__n); }
  56. static void deallocate(void* __p, size_t) { ::operator delete(__p); }
  57. };
  58. #include "w3d_dep.h"
  59. #include "w3d_file.h"
  60. #include <assert.h>
  61. #include <chunkio.h>
  62. #include "ffactory.h"
  63. /*
  64. ** Forward declarations.
  65. */
  66. static void Scan_Chunk (ChunkLoadClass &cload, StringList &files, const char *w3d_name);
  67. static void Scan_Mesh (ChunkLoadClass &cload, StringList &files, const char *w3d_name);
  68. static void Scan_Mesh_Header (ChunkLoadClass &cload, StringList &files, const char *w3d_name);
  69. static void Scan_Mesh_Textures (ChunkLoadClass &cload, StringList &files, const char *w3d_name);
  70. static void Scan_Anim (ChunkLoadClass &cload, StringList &files, const char *w3d_name);
  71. static void Scan_Compressed_Anim (ChunkLoadClass &cload, StringList &files, const char *w3d_name);
  72. static void Scan_HModel (ChunkLoadClass &cload, StringList &files, const char *w3d_name);
  73. static void Scan_Emitter (ChunkLoadClass &cload, StringList &files, const char *w3d_name);
  74. static void Scan_Aggregate (ChunkLoadClass &cload, StringList &files, const char *w3d_name);
  75. static void Scan_HLOD (ChunkLoadClass &cload, StringList &files, const char *w3d_name);
  76. static void Get_W3D_Name (const char *filename, char *w3d_name);
  77. static const char * Make_W3D_Filename (const char *w3d_name);
  78. /***********************************************************************************************
  79. * Get_W3D_Dependencies -- Scans a W3D file to determine which other files it depends on. *
  80. * *
  81. * INPUT: *
  82. * *
  83. * OUTPUT: *
  84. * *
  85. * WARNINGS: *
  86. * *
  87. * HISTORY: *
  88. * 4/3/00 AJA : Created. *
  89. *=============================================================================================*/
  90. bool Get_W3D_Dependencies (const char *w3d_filename, StringList &files)
  91. {
  92. assert(w3d_filename);
  93. // Open the W3D file.
  94. FileClass *file=_TheFileFactory->Get_File(w3d_filename);
  95. if ( file ) {
  96. file->Open();
  97. if ( ! file->Is_Open()) {
  98. _TheFileFactory->Return_File(file);
  99. file=NULL;
  100. return false;
  101. }
  102. } else {
  103. return false;
  104. }
  105. // Get the W3D name from the filename.
  106. char w3d_name[W3D_NAME_LEN];
  107. Get_W3D_Name(w3d_filename, w3d_name);
  108. // Create a chunk loader for this file, and scan the file.
  109. ChunkLoadClass cload(file);
  110. while (cload.Open_Chunk())
  111. {
  112. Scan_Chunk(cload, files, w3d_name);
  113. cload.Close_Chunk();
  114. }
  115. // Close the file.
  116. file->Close();
  117. _TheFileFactory->Return_File(file);
  118. file=NULL;
  119. // Sort the set of filenames, and remove any duplicates.
  120. files.sort();
  121. files.unique();
  122. return true;
  123. }
  124. /***********************************************************************************************
  125. * Scan_Chunk -- Chooses the correct chunk loader for this chunk. *
  126. * *
  127. * INPUT: *
  128. * *
  129. * OUTPUT: *
  130. * *
  131. * WARNINGS: *
  132. * *
  133. * HISTORY: *
  134. * 4/3/00 AJA : Created. *
  135. *=============================================================================================*/
  136. static void Scan_Chunk (ChunkLoadClass &cload, StringList &files, const char *w3d_name)
  137. {
  138. assert(w3d_name);
  139. switch (cload.Cur_Chunk_ID())
  140. {
  141. case W3D_CHUNK_MESH:
  142. Scan_Mesh(cload, files, w3d_name);
  143. break;
  144. case W3D_CHUNK_ANIMATION:
  145. Scan_Anim(cload, files, w3d_name);
  146. break;
  147. case W3D_CHUNK_COMPRESSED_ANIMATION:
  148. Scan_Compressed_Anim(cload, files, w3d_name);
  149. break;
  150. case W3D_CHUNK_HMODEL:
  151. Scan_HModel(cload, files, w3d_name);
  152. break;
  153. case W3D_CHUNK_EMITTER:
  154. Scan_Emitter(cload, files, w3d_name);
  155. break;
  156. case W3D_CHUNK_AGGREGATE:
  157. Scan_Aggregate(cload, files, w3d_name);
  158. break;
  159. case W3D_CHUNK_HLOD:
  160. Scan_HLOD(cload, files, w3d_name);
  161. break;
  162. }
  163. }
  164. /***********************************************************************************************
  165. * Scan_Mesh -- Scans a mesh for references to other files. *
  166. * *
  167. * INPUT: *
  168. * *
  169. * OUTPUT: *
  170. * *
  171. * WARNINGS: *
  172. * *
  173. * HISTORY: *
  174. *=============================================================================================*/
  175. static void Scan_Mesh (ChunkLoadClass &cload, StringList &files, const char *w3d_name)
  176. {
  177. // Scan the mesh's sub-chunks.
  178. while (cload.Open_Chunk())
  179. {
  180. switch (cload.Cur_Chunk_ID())
  181. {
  182. case W3D_CHUNK_MESH_HEADER3:
  183. // Ahh, the mesh header.
  184. Scan_Mesh_Header(cload, files, w3d_name);
  185. break;
  186. case W3D_CHUNK_TEXTURES:
  187. // We sure want textures...
  188. Scan_Mesh_Textures(cload, files, w3d_name);
  189. break;
  190. }
  191. cload.Close_Chunk();
  192. }
  193. }
  194. /***********************************************************************************************
  195. * Scan_Mesh_Header -- Scans a mesh's header for file references. *
  196. * *
  197. * INPUT: *
  198. * *
  199. * OUTPUT: *
  200. * *
  201. * WARNINGS: *
  202. * *
  203. * HISTORY: *
  204. * 4/3/00 AJA : Created. *
  205. *=============================================================================================*/
  206. static void Scan_Mesh_Header (ChunkLoadClass &cload, StringList &files, const char *w3d_name)
  207. {
  208. // In the mesh header, we want the 'ContainerName' string.
  209. W3dMeshHeader3Struct hdr;
  210. cload.Read(&hdr, sizeof(hdr));
  211. if (strcmp(hdr.ContainerName, w3d_name) != 0)
  212. {
  213. // The container is not this file... Create a W3D filename
  214. // for the container object and add it to the list of
  215. // files we refer to.
  216. const char *filename = Make_W3D_Filename(hdr.ContainerName);
  217. if (*filename) // don't push empty filenames
  218. files.push_back(filename);
  219. }
  220. }
  221. /***********************************************************************************************
  222. * Scan_Mesh_Textures -- Scans a mesh's textures. *
  223. * *
  224. * INPUT: *
  225. * *
  226. * OUTPUT: *
  227. * *
  228. * WARNINGS: *
  229. * *
  230. * HISTORY: *
  231. * 4/3/00 AJA : Created. *
  232. *=============================================================================================*/
  233. static void Scan_Mesh_Textures (ChunkLoadClass &cload, StringList &files, const char *w3d_name)
  234. {
  235. // Let's see which textures are used by this mesh...
  236. while (cload.Open_Chunk())
  237. {
  238. // We're interested in the TEXTURE sub-chunk.
  239. if (cload.Cur_Chunk_ID() == W3D_CHUNK_TEXTURE)
  240. {
  241. while (cload.Open_Chunk())
  242. {
  243. // We're interested in the TEXTURE_NAME sub-chunk.
  244. if (cload.Cur_Chunk_ID() == W3D_CHUNK_TEXTURE_NAME)
  245. {
  246. // This chunk's data is a NULL-terminated string
  247. // which is the texture filename. Read it and
  248. // add it to the list of files referred to.
  249. char texture[_MAX_PATH];
  250. cload.Read(texture, cload.Cur_Chunk_Length());
  251. if (*texture) // don't push empty filenames
  252. files.push_back(texture);
  253. }
  254. cload.Close_Chunk();
  255. }
  256. }
  257. cload.Close_Chunk();
  258. }
  259. }
  260. /***********************************************************************************************
  261. * Scan_Anim -- Scans an animation for references to other files. *
  262. * *
  263. * INPUT: *
  264. * *
  265. * OUTPUT: *
  266. * *
  267. * WARNINGS: *
  268. * *
  269. * HISTORY: *
  270. *=============================================================================================*/
  271. static void Scan_Anim (ChunkLoadClass &cload, StringList &files, const char *w3d_name)
  272. {
  273. while (cload.Open_Chunk())
  274. {
  275. // We only want the animation header, because it can refer to an HTree.
  276. if (cload.Cur_Chunk_ID() == W3D_CHUNK_ANIMATION_HEADER)
  277. {
  278. // Read in the header.
  279. W3dAnimHeaderStruct hdr;
  280. cload.Read(&hdr, sizeof(hdr));
  281. if (strcmp(hdr.HierarchyName, w3d_name) != 0)
  282. {
  283. const char *hierarchy = Make_W3D_Filename(hdr.HierarchyName);
  284. if (*hierarchy) // don't push an empty filename
  285. files.push_back(hierarchy);
  286. }
  287. }
  288. cload.Close_Chunk();
  289. }
  290. }
  291. /***********************************************************************************************
  292. * Scan_Compressed_Anim -- Scans a compressed animation mesh for references to other files. *
  293. * *
  294. * INPUT: *
  295. * *
  296. * OUTPUT: *
  297. * *
  298. * WARNINGS: *
  299. * *
  300. * HISTORY: *
  301. *=============================================================================================*/
  302. static void Scan_Compressed_Anim (ChunkLoadClass &cload, StringList &files, const char *w3d_name)
  303. {
  304. while (cload.Open_Chunk())
  305. {
  306. // We only want the animation header, because it can refer to an HTree.
  307. if (cload.Cur_Chunk_ID() == W3D_CHUNK_ANIMATION_HEADER)
  308. {
  309. // Read in the header.
  310. W3dCompressedAnimHeaderStruct hdr;
  311. cload.Read(&hdr, sizeof(hdr));
  312. if (strcmp(hdr.HierarchyName, w3d_name) != 0)
  313. {
  314. const char *hierarchy = Make_W3D_Filename(hdr.HierarchyName);
  315. if (*hierarchy) // don't push an empty filename
  316. files.push_back(hierarchy);
  317. }
  318. }
  319. cload.Close_Chunk();
  320. }
  321. }
  322. /***********************************************************************************************
  323. * Scan_HModel -- Scans an HModel for references to other files. *
  324. * *
  325. * INPUT: *
  326. * *
  327. * OUTPUT: *
  328. * *
  329. * WARNINGS: *
  330. * *
  331. * HISTORY: *
  332. *=============================================================================================*/
  333. static void Scan_HModel (ChunkLoadClass &cload, StringList &files, const char *w3d_name)
  334. {
  335. while (cload.Open_Chunk())
  336. {
  337. // We only want the header because it can refer to an HTree.
  338. if (cload.Cur_Chunk_ID() == W3D_CHUNK_HMODEL_HEADER)
  339. {
  340. // Read in the header.
  341. W3dHModelHeaderStruct hdr;
  342. cload.Read(&hdr, sizeof(hdr));
  343. if (strcmp(hdr.HierarchyName, w3d_name) != 0)
  344. {
  345. const char *hierarchy = Make_W3D_Filename(hdr.HierarchyName);
  346. if (*hierarchy) // don't push an empty filename
  347. files.push_back(hierarchy);
  348. }
  349. }
  350. cload.Close_Chunk();
  351. }
  352. }
  353. /***********************************************************************************************
  354. * Scan_Emitter -- Scans an emitter for references to other files. *
  355. * *
  356. * INPUT: *
  357. * *
  358. * OUTPUT: *
  359. * *
  360. * WARNINGS: *
  361. * *
  362. * HISTORY: *
  363. *=============================================================================================*/
  364. static void Scan_Emitter (ChunkLoadClass &cload, StringList &files, const char *w3d_name)
  365. {
  366. while (cload.Open_Chunk())
  367. {
  368. // We only want the emitter info chunk, it has a texture name in it.
  369. if (cload.Cur_Chunk_ID() == W3D_CHUNK_EMITTER_INFO)
  370. {
  371. // Read in the header.
  372. W3dEmitterInfoStruct hdr;
  373. cload.Read(&hdr, sizeof(hdr));
  374. if (hdr.TextureFilename[0]) // don't push an empty texture name
  375. files.push_back(hdr.TextureFilename);
  376. }
  377. cload.Close_Chunk();
  378. }
  379. }
  380. /***********************************************************************************************
  381. * Scan_Aggregate -- Scans an aggregate for references to other files. *
  382. * *
  383. * INPUT: *
  384. * *
  385. * OUTPUT: *
  386. * *
  387. * WARNINGS: *
  388. * *
  389. * HISTORY: *
  390. *=============================================================================================*/
  391. static void Scan_Aggregate (ChunkLoadClass &cload, StringList &files, const char *w3d_name)
  392. {
  393. while (cload.Open_Chunk())
  394. {
  395. // We want the aggregate info chunk.
  396. if (cload.Cur_Chunk_ID() == W3D_CHUNK_AGGREGATE_INFO)
  397. {
  398. W3dAggregateInfoStruct chunk;
  399. cload.Read(&chunk, sizeof(chunk));
  400. if (strcmp(chunk.BaseModelName, w3d_name) != 0)
  401. {
  402. // Check the name of the base model against the name of this file.
  403. const char *base_model = Make_W3D_Filename(chunk.BaseModelName);
  404. if (*base_model)
  405. files.push_back(base_model);
  406. }
  407. // Iterate through the sub-objects.
  408. unsigned int i;
  409. for (i = 0; i < chunk.SubobjectCount; ++i)
  410. {
  411. W3dAggregateSubobjectStruct subchunk;
  412. cload.Read(&subchunk, sizeof(subchunk));
  413. if (strcmp(subchunk.SubobjectName, w3d_name) != 0)
  414. {
  415. // Check the name of the subobject against the name of this file.
  416. const char *subobject = Make_W3D_Filename(subchunk.SubobjectName);
  417. if (*subobject)
  418. files.push_back(subobject);
  419. }
  420. }
  421. }
  422. cload.Close_Chunk();
  423. }
  424. }
  425. /***********************************************************************************************
  426. * Scan_HLOD -- Scans an HLOD for references to other files. *
  427. * *
  428. * INPUT: *
  429. * *
  430. * OUTPUT: *
  431. * *
  432. * WARNINGS: *
  433. * *
  434. * HISTORY: *
  435. *=============================================================================================*/
  436. static void Scan_HLOD (ChunkLoadClass &cload, StringList &files, const char *w3d_name)
  437. {
  438. while (cload.Open_Chunk())
  439. {
  440. // We only want the header.
  441. if (cload.Cur_Chunk_ID() == W3D_CHUNK_HLOD_HEADER)
  442. {
  443. W3dHLodHeaderStruct hdr;
  444. cload.Read(&hdr, sizeof(hdr));
  445. if (strcmp(hdr.HierarchyName, w3d_name) != 0)
  446. {
  447. const char *hierarchy = Make_W3D_Filename(hdr.HierarchyName);
  448. if (*hierarchy)
  449. files.push_back(hierarchy);
  450. }
  451. }
  452. cload.Close_Chunk();
  453. }
  454. }
  455. /***********************************************************************************************
  456. * Get_W3D_Name -- Gets a W3D object name from a W3D filename. *
  457. * *
  458. * INPUT: *
  459. * *
  460. * OUTPUT: *
  461. * *
  462. * WARNINGS: *
  463. * *
  464. * HISTORY: *
  465. * 4/3/00 AJA : Created. *
  466. *=============================================================================================*/
  467. static void Get_W3D_Name (const char *filename, char *w3d_name)
  468. {
  469. assert(filename);
  470. assert(w3d_name);
  471. // Figure out the first character of the name of the file
  472. // (bypass the path if it was given).
  473. const char *start = strrchr(filename, '\\');
  474. if (start)
  475. ++start; // point to first character after the last backslash
  476. else
  477. start = filename; // point to the start of the filename
  478. // We don't want to copy the .w3d extension. Find where
  479. // it occurs.
  480. const char *end = strrchr(start, '.');
  481. if (!end)
  482. end = start + strlen(start); // point to the null character
  483. // Copy all characters from start to end (excluding 'end')
  484. // into the w3d_name buffer. Then capitalize the string.
  485. memset(w3d_name, 0, W3D_NAME_LEN); // blank out the buffer
  486. int num_chars = end - start;
  487. strncpy(w3d_name, start, num_chars < W3D_NAME_LEN ? num_chars : W3D_NAME_LEN-1);
  488. strupr(w3d_name);
  489. }
  490. /***********************************************************************************************
  491. * Make_W3D_Filename -- Converts a W3D object name into a W3D filename. *
  492. * *
  493. * INPUT: *
  494. * *
  495. * OUTPUT: *
  496. * *
  497. * WARNINGS: *
  498. * *
  499. * HISTORY: *
  500. * 4/3/00 AJA : Created. *
  501. *=============================================================================================*/
  502. static const char * Make_W3D_Filename (const char *w3d_name)
  503. {
  504. assert(w3d_name);
  505. assert(strlen(w3d_name) < W3D_NAME_LEN);
  506. // Copy the w3d name into a static buffer, turn it into lowercase
  507. // letters, and append a ".w3d" file extension. That's the filename.
  508. static char buffer[64];
  509. if (*w3d_name == 0)
  510. {
  511. // Empty W3D name case.
  512. buffer[0] = 0;
  513. return buffer;
  514. }
  515. strcpy(buffer, w3d_name);
  516. char *dot = strchr(buffer, '.');
  517. if (dot)
  518. *dot = 0;
  519. strlwr(buffer);
  520. strcat(buffer, ".w3d");
  521. return buffer;
  522. }