EditorAssetMgr.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. /*
  2. ** Command & Conquer Renegade(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 : LevelEdit *
  23. * *
  24. * $Archive:: /Commando/Code/Tools/LevelEdit/EditorAssetMgr.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 1/29/02 3:16p $*
  29. * *
  30. * $Revision:: 29 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "stdafx.h"
  36. #include "editorassetmgr.h"
  37. #include "texture.h"
  38. #include "proto.h"
  39. #include "editorini.h"
  40. #include "ffactory.h"
  41. #include "ww3d.h"
  42. #include "utils.h"
  43. #include "filemgr.h"
  44. #include "rawfile.h"
  45. #include "rcfile.h"
  46. #include "filelocations.h"
  47. #include "editorbuild.h"
  48. #include "assetdatabase.h"
  49. ////////////////////////////////////////////////////////////////////////
  50. // Forward declarations
  51. ////////////////////////////////////////////////////////////////////////
  52. DynamicVectorClass<StringClass> EditorFileFactoryClass::SearchPathList;
  53. ////////////////////////////////////////////////////////////////////////
  54. //
  55. // EditorAssetMgrClass
  56. //
  57. ////////////////////////////////////////////////////////////////////////
  58. EditorAssetMgrClass::EditorAssetMgrClass (void)
  59. {
  60. Set_Activate_Fog_On_Load (true);
  61. m_CurrentDir = ::Get_Startup_Directory ();
  62. return ;
  63. }
  64. ////////////////////////////////////////////////////////////////////////
  65. //
  66. // Reload_File
  67. //
  68. void
  69. EditorAssetMgrClass::Reload_File (const char *filename)
  70. {
  71. // Param OK?
  72. ASSERT (filename != NULL);
  73. if (filename != NULL) {
  74. CString extension = ::strrchr (filename, '.');
  75. // Is this a W3D file?
  76. if (extension.CompareNoCase (".W3D") == 0) {
  77. CString asset_name = ::Asset_Name_From_Filename (filename);
  78. Remove_Prototype (asset_name);
  79. Load_3D_Assets (filename);
  80. }
  81. }
  82. return ;
  83. }
  84. ////////////////////////////////////////////////////////////////////////
  85. //
  86. // Is_File_Here
  87. //
  88. ////////////////////////////////////////////////////////////////////////
  89. bool
  90. EditorAssetMgrClass::Is_File_Here (LPCTSTR full_path, bool get_from_vss)
  91. {
  92. //
  93. // Output this file lookup to the message window
  94. //
  95. CString message;
  96. message.Format ("Attempting to load: %s\r\n", full_path);
  97. ::Output_Message (message);
  98. //
  99. // Check to see if the file exists in this location
  100. //
  101. return ::Get_File_Mgr ()->Does_File_Exist (full_path, get_from_vss);
  102. }
  103. ////////////////////////////////////////////////////////////////////////
  104. //
  105. // Determine_Real_Location
  106. //
  107. ////////////////////////////////////////////////////////////////////////
  108. bool
  109. EditorAssetMgrClass::Determine_Real_Location
  110. (
  111. LPCTSTR path,
  112. CString & real_location
  113. )
  114. {
  115. bool retval = false;
  116. TCHAR full_path[MAX_PATH];
  117. //
  118. // Did the caller supply a path?
  119. //
  120. if (Is_Full_Path (path)) {
  121. ::lstrcpy (full_path, path);
  122. } else {
  123. //
  124. // Make a full path out of the filename
  125. //
  126. CString test_path = Make_Path (m_CurrentDir, path);
  127. //
  128. // If the file doesn't exist here, then try the search paths
  129. //
  130. if (::GetFileAttributes (test_path) == 0xFFFFFFFF) {
  131. //
  132. // Try to find the file in our search path list
  133. //
  134. DynamicVectorClass<StringClass> &path_list = EditorFileFactoryClass::Get_Search_Path ();
  135. for (int index = 0; index < path_list.Count (); index ++) {
  136. //
  137. // Does the file exist in this directoy?
  138. //
  139. StringClass full_path = Make_Path (path_list[index], path);
  140. if (::GetFileAttributes (full_path) != 0xFFFFFFFF) {
  141. test_path = full_path;
  142. break;
  143. }
  144. }
  145. }
  146. ::lstrcpy (full_path, test_path);
  147. }
  148. //
  149. // Find out if the file lives in the directory
  150. //
  151. if (Is_File_Here (full_path, true) == false) {
  152. //
  153. // Hmmm... not here, try up a directory level
  154. //
  155. CString parent_dir = ::Up_One_Directory (::Strip_Filename_From_Path (full_path));
  156. CString filename = ::Get_Filename_From_Path (full_path);
  157. parent_dir = ::Make_Path (parent_dir, filename);
  158. //
  159. // Does the file live in the parent directory?
  160. //
  161. if (Is_File_Here (parent_dir, true) == false) {
  162. //
  163. // Is this a texture file?
  164. //
  165. if (Is_Texture_Filename (full_path)) {
  166. #ifndef PUBLIC_EDITOR_VER
  167. //
  168. // Does the file live in the global texture directory?
  169. //
  170. CString texture_path = ::Make_Path (GLOBAL_TEXTURE_PATH, filename);
  171. if (Is_File_Here (texture_path, false)) {
  172. real_location = texture_path;
  173. retval = true;
  174. }
  175. #endif //!PUBLIC_EDITOR_VER
  176. } else if (::strnicmp ("h_a_", filename, 4) == 0) {
  177. //
  178. // We are 'assuming' this is a human animation file, so
  179. // try to find it in the human animation directory.
  180. //
  181. CString anim_path = ::Make_Path (CHAR_ANIMS_PATH, filename);
  182. if (Is_File_Here (anim_path, true)) {
  183. real_location = anim_path;
  184. retval = true;
  185. }
  186. }
  187. } else {
  188. real_location = parent_dir;
  189. retval = true;
  190. }
  191. } else {
  192. real_location = full_path;
  193. retval = true;
  194. }
  195. #ifdef PUBLIC_EDITOR_VER
  196. //
  197. // If the file does not exist locally, then use the one from the mix file
  198. //
  199. if (retval == false && ::Get_File_Mgr ()->Is_File_In_VSS (full_path)) {
  200. real_location = full_path;
  201. retval = true;
  202. }
  203. #endif //PUBLIC_EDITOR_VER
  204. return retval;
  205. }
  206. ////////////////////////////////////////////////////////////////////////
  207. //
  208. // Create_Render_Obj
  209. //
  210. RenderObjClass *
  211. EditorAssetMgrClass::Create_Render_Obj (const char * name)
  212. {
  213. // Let windows do some processing
  214. ::Pump_Messages ();
  215. // Assume failure
  216. RenderObjClass *prenderobj = NULL;
  217. // Try to find a prototype
  218. PrototypeClass * proto = Find_Prototype(name);
  219. if (WW3D_Load_On_Demand && proto == NULL) { // If we didn't find one, try to load on demand
  220. char filename[ MAX_PATH ];
  221. sprintf( filename, "%s.W3D", name);
  222. char *mesh_name = ::strchr (name, '.');
  223. if (mesh_name != NULL) {
  224. ::lstrcpyn (filename, name, ((int)mesh_name) - ((int)name) + 1);
  225. ::lstrcat (filename, ".W3D");
  226. } else {
  227. sprintf( filename, "%s.W3D", name);
  228. }
  229. // See if we can determine where this file exists
  230. CString real_location;
  231. if (Determine_Real_Location (filename, real_location)) {
  232. ::lstrcpy (filename, real_location);
  233. } else {
  234. TRACE ("Asset file not found!\n");
  235. }
  236. // Load the assets from this file into the manager
  237. Load_3D_Assets (filename);
  238. // Attempt to find the prototype loader now.
  239. proto = Find_Prototype (name);
  240. }
  241. // If we found a prototype for this object, then
  242. // use it to create a new instance of the requested asset.
  243. if (proto != NULL) {
  244. prenderobj = proto->Create ();
  245. }
  246. // Return a pointer to the render object
  247. return prenderobj;
  248. }
  249. ////////////////////////////////////////////////////////////////////////
  250. //
  251. // Get_HAnim
  252. //
  253. HAnimClass *
  254. EditorAssetMgrClass::Get_HAnim (const char * name)
  255. {
  256. // Let windows do some processing
  257. ::Pump_Messages ();
  258. // Try to find the animation
  259. HAnimClass *panim = HAnimManager.Get_Anim(name);
  260. if (WW3D_Load_On_Demand && panim == NULL) { // If we didn't find it, try to load on demand
  261. char filename[ MAX_PATH ];
  262. char *animname = strchr( name, '.');
  263. if (animname != NULL) {
  264. sprintf( filename, "%s.W3D", animname+1);
  265. } else {
  266. TRACE ("Animation has no . in the name\n");
  267. return NULL;
  268. }
  269. // See if we can determine where this file exists
  270. CString real_location;
  271. if (Determine_Real_Location (filename, real_location)) {
  272. ::lstrcpy (filename, real_location);
  273. } else {
  274. TRACE ("Asset file not found!\n");
  275. }
  276. // Load the assets from this file into the manager
  277. Load_3D_Assets (filename);
  278. // Attempt to find the prototype loader now.
  279. panim = HAnimManager.Get_Anim(name);
  280. }
  281. // Return a pointer to the animation
  282. return panim;
  283. }
  284. ////////////////////////////////////////////////////////////////////////
  285. //
  286. // Get_HTree
  287. //
  288. HTreeClass *
  289. EditorAssetMgrClass::Get_HTree (const char * name)
  290. {
  291. // Let windows do some processing
  292. ::Pump_Messages ();
  293. // Try to find the htree
  294. HTreeClass *phtree = HTreeManager.Get_Tree(name);
  295. if (WW3D_Load_On_Demand && phtree == NULL) { // If we didn't find it, try to load on demand
  296. char filename[ MAX_PATH ];
  297. sprintf( filename, "%s.W3D", name);
  298. // See if we can determine where this file exists
  299. CString real_location;
  300. if (Determine_Real_Location (filename, real_location)) {
  301. ::lstrcpy (filename, real_location);
  302. } else {
  303. TRACE ("Asset file not found!\n");
  304. }
  305. // Load the assets from this file into the manager
  306. Load_3D_Assets (filename);
  307. // Attempt to find the object loader now.
  308. phtree = HTreeManager.Get_Tree(name);
  309. }
  310. // Return a pointer to the animation
  311. return phtree;
  312. }
  313. ////////////////////////////////////////////////////////////////////////
  314. //
  315. // Get_INI
  316. //
  317. EditorINIClass *
  318. EditorAssetMgrClass::Get_INI (const char *filename)
  319. {
  320. EditorINIClass *pini = NULL;
  321. FileClass * pini_file = _TheFileFactory->Get_File (filename);
  322. if (pini_file) {
  323. pini = new EditorINIClass (*pini_file);
  324. _TheFileFactory->Return_File (pini_file);
  325. }
  326. // Return a pointer to the INI file class
  327. return pini;
  328. }
  329. ////////////////////////////////////////////////////////////////////////
  330. //
  331. // Get_Texture
  332. //
  333. ////////////////////////////////////////////////////////////////////////
  334. TextureClass *
  335. EditorAssetMgrClass::Get_Texture
  336. (
  337. const char * tga_filename,
  338. TextureClass::MipCountType mip_level_count,
  339. WW3DFormat texture_format,
  340. bool allow_compression
  341. )
  342. {
  343. //
  344. // Let windows do some processing
  345. //
  346. ::Pump_Messages ();
  347. /*
  348. ** Bail if the user isn't really asking for anything
  349. */
  350. if ((tga_filename == NULL) || (strlen(tga_filename) == 0)) {
  351. return NULL;
  352. }
  353. //
  354. // Determine what the texture's "name" should be
  355. //
  356. CString texture_name;
  357. if (::strstr (tga_filename, "+\\") != NULL) {
  358. texture_name = ::Get_Subdir_And_Filename_From_Path (tga_filename);
  359. } else {
  360. texture_name = ::Get_Filename_From_Path (tga_filename);
  361. }
  362. texture_name.MakeLower ();
  363. /*
  364. ** See if the texture has already been loaded.
  365. */
  366. TextureClass* tex = TextureHash.Get ((const char *)texture_name);
  367. if (tex && texture_format!=WW3D_FORMAT_UNKNOWN) {
  368. WWASSERT_PRINT(tex->Get_Texture_Format()==texture_format,("Texture %s has already been loaded witt different format",(const char *)texture_name));
  369. }
  370. /*
  371. ** Didn't have it so we have to create a new texture
  372. */
  373. if (!tex) {
  374. //
  375. // See if we can determine where this file exists
  376. //
  377. CString real_location;
  378. if (Determine_Real_Location (tga_filename, real_location) == false) {
  379. CString message;
  380. message.Format ("Texture file not found: %s\r\n", (LPCTSTR)texture_name);
  381. ::Output_Message (message);
  382. real_location = tga_filename;
  383. }
  384. /*
  385. ** Didn't have it so we have to create a new texture
  386. */
  387. tex = NEW_REF(TextureClass,(texture_name, real_location, mip_level_count, texture_format, allow_compression));
  388. TextureHash.Insert(tex->Get_Texture_Name(),tex);
  389. }
  390. tex->Add_Ref();
  391. return tex;
  392. }
  393. ////////////////////////////////////////////////////////////////////////
  394. //
  395. // Load_Resource_Texture
  396. //
  397. ////////////////////////////////////////////////////////////////////////
  398. void
  399. EditorAssetMgrClass::Load_Resource_Texture (const char *filename)
  400. {
  401. CString texture_path = ::Get_File_Mgr ()->Get_Texture_Cache_Path ();
  402. CString texture_filename = ::Make_Path (texture_path, filename);
  403. //
  404. // Open the resource texture and create the
  405. // real file.
  406. //
  407. ResourceFileClass res_texture (NULL, filename);
  408. RawFileClass texture_file (texture_filename);
  409. if (texture_file.Create () == 1) {
  410. texture_file.Open (FileClass::WRITE);
  411. //
  412. // Copy the contents of the resource (memory) texture
  413. // to a file.
  414. //
  415. BYTE buffer[2048];
  416. int size = res_texture.Size ();
  417. int total_written = 0;
  418. while (total_written < size) {
  419. int bytes_read = res_texture.Read (buffer, sizeof (buffer));
  420. int bytes_written = texture_file.Write (buffer, bytes_read);
  421. total_written += bytes_written;
  422. if (bytes_written == 0) {
  423. break;
  424. }
  425. }
  426. texture_file.Close ();
  427. //
  428. // Force the texture to be loaded into memory
  429. //
  430. TextureClass *texture = Get_Texture (texture_filename);
  431. REF_PTR_RELEASE (texture);
  432. }
  433. return ;
  434. }
  435. ////////////////////////////////////////////////////////////////////////
  436. //
  437. // Load_3D_Assets
  438. //
  439. ////////////////////////////////////////////////////////////////////////
  440. bool
  441. EditorAssetMgrClass::Load_3D_Assets (const char *path)
  442. {
  443. bool retval = true;
  444. //
  445. // Check to see if the asset is already in the asset manager
  446. //
  447. CString filename = ::Get_Filename_From_Path (path);
  448. CString asset_name = ::Asset_Name_From_Filename (filename);
  449. PrototypeClass *prototype = Find_Prototype (asset_name);
  450. //
  451. // Load the asset fresh if necessary
  452. //
  453. if (prototype == NULL) {
  454. CString full_path = ::Get_File_Mgr ()->Make_Full_Path (path);
  455. retval = false;
  456. #ifdef PUBLIC_EDITOR_VER
  457. //
  458. // If the file exists locally, or its coming from the mix file
  459. // then load it.
  460. //
  461. if ( ::Get_File_Mgr ()->Does_File_Exist (full_path) ||
  462. ::Get_File_Mgr ()->Is_File_In_VSS (full_path))
  463. {
  464. retval = WW3DAssetManager::Load_3D_Assets (full_path);
  465. }
  466. #else
  467. //
  468. // If the file exists, then load it.
  469. //
  470. if (::Get_File_Mgr ()->Does_File_Exist (full_path, true)) {
  471. retval = WW3DAssetManager::Load_3D_Assets (full_path);
  472. }
  473. #endif //PUBLIC_EDITOR_VER
  474. }
  475. return retval;
  476. }
  477. ////////////////////////////////////////////////////////////////////////
  478. //
  479. // Get_File
  480. //
  481. ////////////////////////////////////////////////////////////////////////
  482. FileClass *
  483. EditorFileFactoryClass::Get_File (char const *filename)
  484. {
  485. CString path;
  486. //
  487. // If the filename contains a relative path, then
  488. // turn it into an absolute path.
  489. //
  490. if (::Is_Path (filename)) {
  491. path = ::Get_File_Mgr ()->Make_Full_Path (filename);
  492. } else {
  493. //
  494. // Make a full path out of the filename
  495. //
  496. const char *curr_dir = _pThe3DAssetManager->Get_Current_Directory ();
  497. path = Make_Path (curr_dir, filename);
  498. //
  499. // If the file doesn't exist here, then try the search paths
  500. //
  501. if (::GetFileAttributes (path) == 0xFFFFFFFF) {
  502. //
  503. // Try to find the file in our search path list
  504. //
  505. for (int index = 0; index < SearchPathList.Count (); index ++) {
  506. //
  507. // Does the file exist in this directoy?
  508. //
  509. StringClass full_path = Make_Path (SearchPathList[index], filename);
  510. if (::GetFileAttributes (full_path) != 0xFFFFFFFF) {
  511. path = full_path;
  512. break;
  513. }
  514. }
  515. }
  516. }
  517. //
  518. // Get the file from the database
  519. //
  520. return ::Get_File_Mgr ()->Get_Database_Interface ().Get_File (path);
  521. }
  522. ////////////////////////////////////////////////////////////////////////
  523. //
  524. // Return_File
  525. //
  526. ////////////////////////////////////////////////////////////////////////
  527. void
  528. EditorFileFactoryClass::Return_File (FileClass *file)
  529. {
  530. if (file != NULL) {
  531. delete file;
  532. }
  533. return ;
  534. }
  535. ////////////////////////////////////////////////////////////////////////
  536. //
  537. // Open_Texture_File_Cache
  538. //
  539. ////////////////////////////////////////////////////////////////////////
  540. void
  541. EditorAssetMgrClass::Open_Texture_File_Cache (const char * /*prefix*/)
  542. {
  543. return ;
  544. }
  545. ////////////////////////////////////////////////////////////////////////
  546. //
  547. // Close_Texture_File_Cache
  548. //
  549. ////////////////////////////////////////////////////////////////////////
  550. void
  551. EditorAssetMgrClass::Close_Texture_File_Cache (void)
  552. {
  553. return ;
  554. }