assetdep.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. *** Confidential - Westwood Studios ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Commando *
  23. * *
  24. * $Archive:: /Commando/Code/Combat/assetdep.cpp $*
  25. * *
  26. * $Author:: Jani_p $*
  27. * *
  28. * $Modtime:: 11/29/01 9:48p $*
  29. * *
  30. * $Revision:: 12 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "assetdep.h"
  36. #include "chunkio.h"
  37. #include "wwstring.h"
  38. #include "assetmgr.h"
  39. #include "ffactory.h"
  40. #include "saveloadstatus.h"
  41. #include "wwprofile.h"
  42. ///////////////////////////////////////////////////////////////////////
  43. // Local prototypes
  44. ///////////////////////////////////////////////////////////////////////
  45. static void Asset_Name_From_Filename (StringClass& new_name, const char *filename);
  46. static void Get_Filename_From_Path (StringClass& new_name, const char *filename);
  47. ///////////////////////////////////////////////////////////////////////
  48. // Constants
  49. ///////////////////////////////////////////////////////////////////////
  50. static const char * ALWAYS_FILENAME = "always.dep";
  51. static const char * DEP_EXTENSION = ".dep";
  52. enum
  53. {
  54. CHUNKID_FILE_LIST = 0x04020527,
  55. VARID_ASSET_FILENAME = 0x01,
  56. };
  57. ///////////////////////////////////////////////////////////////////////
  58. //
  59. // Save_Always_Dependencies
  60. //
  61. ///////////////////////////////////////////////////////////////////////
  62. void
  63. AssetDependencyManager::Save_Always_Dependencies (const char *path, ASSET_LIST &asset_list)
  64. {
  65. //
  66. // Get a pointer to the file object
  67. //
  68. StringClass filename(path + StringClass ("\\") + StringClass (ALWAYS_FILENAME),true);
  69. FileClass * file = _TheWritingFileFactory->Get_File (filename);
  70. if (file != NULL) {
  71. //
  72. // Open or create the file
  73. //
  74. file->Open (FileClass::WRITE);
  75. //
  76. // Save the asset list to the file
  77. //
  78. ChunkSaveClass csave (file);
  79. Save_Dependencies (csave, asset_list);
  80. //
  81. // Close the file
  82. //
  83. file->Close ();
  84. _TheWritingFileFactory->Return_File (file);
  85. }
  86. return ;
  87. }
  88. ///////////////////////////////////////////////////////////////////////
  89. //
  90. // Save_Level_Dependencies
  91. //
  92. ///////////////////////////////////////////////////////////////////////
  93. void
  94. AssetDependencyManager::Save_Level_Dependencies (const char *full_path, ASSET_LIST &asset_list)
  95. {
  96. //
  97. // Get a pointer to the file object
  98. //
  99. FileClass * file = _TheWritingFileFactory->Get_File (full_path);
  100. if (file != NULL) {
  101. //
  102. // Open or create the file
  103. //
  104. file->Open (FileClass::WRITE);
  105. //
  106. // Save the asset list to the file
  107. //
  108. ChunkSaveClass csave (file);
  109. Save_Dependencies (csave, asset_list);
  110. //
  111. // Close the file
  112. //
  113. file->Close ();
  114. _TheWritingFileFactory->Return_File (file);
  115. }
  116. return ;
  117. }
  118. ///////////////////////////////////////////////////////////////////////
  119. //
  120. // Save_Dependencies
  121. //
  122. ///////////////////////////////////////////////////////////////////////
  123. void
  124. AssetDependencyManager::Save_Dependencies (ChunkSaveClass &csave, ASSET_LIST &asset_list)
  125. {
  126. csave.Begin_Chunk (CHUNKID_FILE_LIST);
  127. //
  128. // Write each filename dependency to a chunk
  129. //
  130. for (int index = 0; index < asset_list.Count (); index ++) {
  131. StringClass &filename = asset_list[index];
  132. WRITE_MICRO_CHUNK_WWSTRING (csave, VARID_ASSET_FILENAME, filename);
  133. }
  134. csave.End_Chunk ();
  135. return ;
  136. }
  137. ///////////////////////////////////////////////////////////////////////
  138. //
  139. // Load_Level_Assets
  140. //
  141. ///////////////////////////////////////////////////////////////////////
  142. void
  143. AssetDependencyManager::Load_Level_Assets (const char *level_name)
  144. {
  145. //
  146. // Strip the extension (if necessary)
  147. //
  148. StringClass base_name(level_name,true);
  149. const char *extension = ::strrchr (base_name, '.');
  150. if (extension != NULL && base_name.Get_Length () > 4) {
  151. base_name.Erase (base_name.Get_Length () - 4, 4);
  152. }
  153. //
  154. // Build a filename from the level name, and load the assets from it.
  155. //
  156. StringClass filename(base_name + StringClass (DEP_EXTENSION),true);
  157. Load_Assets (filename);
  158. return ;
  159. }
  160. ///////////////////////////////////////////////////////////////////////
  161. //
  162. // Load_Always_Assets
  163. //
  164. ///////////////////////////////////////////////////////////////////////
  165. void
  166. AssetDependencyManager::Load_Always_Assets (void)
  167. {
  168. //
  169. // Load the assets from the always file
  170. //
  171. Load_Assets (ALWAYS_FILENAME);
  172. return ;
  173. }
  174. ///////////////////////////////////////////////////////////////////////
  175. //
  176. // Load_Assets
  177. //
  178. ///////////////////////////////////////////////////////////////////////
  179. void
  180. AssetDependencyManager::Load_Assets (const char *filename)
  181. {
  182. //
  183. // Get a pointer to the file object
  184. //
  185. FileClass * file = _TheFileFactory->Get_File (filename);
  186. if (file != NULL) {
  187. if ( file->Is_Available() ) {
  188. //
  189. // Open the file
  190. //
  191. file->Open (FileClass::READ);
  192. //
  193. // Load the asset dependencies from the file
  194. //
  195. ChunkLoadClass cload (file);
  196. Load_Assets (cload);
  197. //
  198. // Close the file
  199. //
  200. file->Close ();
  201. } else {
  202. WWDEBUG_SAY(( "Failed to find %s\n", filename ));
  203. }
  204. _TheFileFactory->Return_File (file);
  205. }
  206. return ;
  207. }
  208. ///////////////////////////////////////////////////////////////////////
  209. //
  210. // Load_Assets
  211. //
  212. ///////////////////////////////////////////////////////////////////////
  213. void
  214. AssetDependencyManager::Load_Assets (ChunkLoadClass &cload)
  215. {
  216. WWLOG_PREPARE_TIME_AND_MEMORY("AssetDependencyManager::Load_Assets (ChunkLoadClass &cload)");
  217. cload.Open_Chunk ();
  218. WWASSERT (cload.Cur_Chunk_ID () == CHUNKID_FILE_LIST);
  219. if (cload.Cur_Chunk_ID () == CHUNKID_FILE_LIST) {
  220. //
  221. // Read the filename of each asset from the chunk and
  222. // load its assets into the asset manager.
  223. //
  224. while (cload.Open_Micro_Chunk ()) {
  225. switch (cload.Cur_Micro_Chunk_ID ())
  226. {
  227. case VARID_ASSET_FILENAME:
  228. {
  229. //
  230. // Read the filename from the chunk
  231. //
  232. StringClass filename(0,true);
  233. int size = cload.Cur_Micro_Chunk_Length ();
  234. cload.Read (filename.Get_Buffer (size), size);
  235. //
  236. // Determine what the render object name should be from
  237. // the filename.
  238. //
  239. StringClass render_obj_name(0,true);
  240. ::Asset_Name_From_Filename (render_obj_name,filename);
  241. INIT_SUB_STATUS(filename);
  242. //
  243. // Load the assets from this file into the asset manager
  244. //
  245. if (WW3DAssetManager::Get_Instance ()->Render_Obj_Exists (render_obj_name) == false) {
  246. WW3DAssetManager::Get_Instance ()->Load_3D_Assets (filename);
  247. }
  248. // WWLOG_INTERMEDIATE(filename);
  249. }
  250. break;
  251. default:
  252. WWDEBUG_SAY (("Unexpected chunk id %d found while preloading assets.\r\n", cload.Cur_Micro_Chunk_ID));
  253. break;
  254. }
  255. cload.Close_Micro_Chunk ();
  256. }
  257. }
  258. cload.Close_Chunk ();
  259. return ;
  260. }
  261. ////////////////////////////////////////////////////////////////////////////
  262. //
  263. // Get_Filename_From_Path
  264. //
  265. ////////////////////////////////////////////////////////////////////////////
  266. void Get_Filename_From_Path (StringClass& new_filename, const char *path)
  267. {
  268. // Find the last occurance of the directory deliminator
  269. const char *filename = ::strrchr (path, '\\');
  270. if (filename != NULL) {
  271. // Increment past the directory deliminator
  272. filename ++;
  273. } else {
  274. filename = path;
  275. }
  276. new_filename=filename;
  277. }
  278. ////////////////////////////////////////////////////////////////////////////
  279. //
  280. // Asset_Name_From_Filename
  281. //
  282. ////////////////////////////////////////////////////////////////////////////
  283. void Asset_Name_From_Filename (StringClass& asset_name, const char *filename)
  284. {
  285. // Get the filename from this path
  286. ::Get_Filename_From_Path (asset_name, filename);
  287. // Find and strip off the extension (if it exists)
  288. char *extension = ::strrchr (asset_name, '.');
  289. if (extension != NULL) {
  290. extension[0] = 0;
  291. }
  292. }