assetManager.cpp 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "assetManager.h"
  23. #ifndef _ASSET_PTR_H_
  24. #include "assetPtr.h"
  25. #endif
  26. #ifndef _REFERENCED_ASSETS_H_
  27. #include "assets/referencedAssets.h"
  28. #endif
  29. #ifndef _DECLARED_ASSETS_H_
  30. #include "assets/declaredAssets.h"
  31. #endif
  32. #ifndef _TAML_ASSET_REFERENCED_VISITOR_H_
  33. #include "tamlAssetReferencedVisitor.h"
  34. #endif
  35. #ifndef _TAML_ASSET_DECLARED_VISITOR_H_
  36. #include "tamlAssetDeclaredVisitor.h"
  37. #endif
  38. #ifndef _TAML_ASSET_DECLARED_UPDATE_VISITOR_H_
  39. #include "tamlAssetDeclaredUpdateVisitor.h"
  40. #endif
  41. #ifndef _TAML_ASSET_REFERENCED_UPDATE_VISITOR_H_
  42. #include "tamlAssetReferencedUpdateVisitor.h"
  43. #endif
  44. #ifndef _CONSOLETYPES_H_
  45. #include "console/consoleTypes.h"
  46. #endif
  47. #ifndef _AUTOLOAD_ASSETS_H_
  48. #include "assets/autoloadAssets.h"
  49. #endif
  50. #ifndef GUI_ASSET_H
  51. #include "T3D/assets/GUIAsset.h"
  52. #endif
  53. #ifndef SCRIPT_ASSET_H
  54. #include "T3D/assets/ScriptAsset.h"
  55. #endif
  56. #ifndef MATERIALASSET_H
  57. #include "T3D/assets/MaterialAsset.h"
  58. #endif
  59. #ifndef GAME_OBJECT_ASSET_H
  60. #include "T3D/assets/GameObjectAsset.h"
  61. #endif
  62. // Script bindings.
  63. #include "assetManager_ScriptBinding.h"
  64. //-----------------------------------------------------------------------------
  65. IMPLEMENT_CONOBJECT( AssetManager );
  66. //-----------------------------------------------------------------------------
  67. AssetManager AssetDatabase;
  68. //-----------------------------------------------------------------------------
  69. AssetManager::AssetManager() :
  70. mLoadedInternalAssetsCount( 0 ),
  71. mLoadedExternalAssetsCount( 0 ),
  72. mLoadedPrivateAssetsCount( 0 ),
  73. mMaxLoadedInternalAssetsCount( 0 ),
  74. mMaxLoadedExternalAssetsCount( 0 ),
  75. mEchoInfo( false ),
  76. mAcquiredReferenceCount( 0 ),
  77. mMaxLoadedPrivateAssetsCount( 0 ),
  78. mIgnoreAutoUnload( true )
  79. {
  80. }
  81. //-----------------------------------------------------------------------------
  82. bool AssetManager::onAdd()
  83. {
  84. // Call parent.
  85. if ( !Parent::onAdd() )
  86. return false;
  87. return true;
  88. }
  89. //-----------------------------------------------------------------------------
  90. void AssetManager::onRemove()
  91. {
  92. // Do we have an asset tags manifest?
  93. if ( !mAssetTagsManifest.isNull() )
  94. {
  95. // Yes, so remove it.
  96. mAssetTagsManifest->deleteObject();
  97. }
  98. // Call parent.
  99. Parent::onRemove();
  100. }
  101. //-----------------------------------------------------------------------------
  102. void AssetManager::initPersistFields()
  103. {
  104. docsURL;
  105. // Call parent.
  106. Parent::initPersistFields();
  107. addField( "EchoInfo", TypeBool, false, Offset(mEchoInfo, AssetManager), "Whether the asset manager echos extra information to the console or not." );
  108. addField( "IgnoreAutoUnload", TypeBool, true, Offset(mIgnoreAutoUnload, AssetManager), "Whether the asset manager should ignore unloading of auto-unload assets or not." );
  109. }
  110. //-----------------------------------------------------------------------------
  111. bool AssetManager::compileReferencedAssets( ModuleDefinition* pModuleDefinition )
  112. {
  113. // Debug Profiling.
  114. PROFILE_SCOPE(AssetManager_CompileReferencedAsset);
  115. // Sanity!
  116. AssertFatal( pModuleDefinition != NULL, "Cannot add declared assets using a NULL module definition" );
  117. // Clear referenced assets.
  118. mReferencedAssets.clear();
  119. // Iterate the module definition children.
  120. for( SimSet::iterator itr = pModuleDefinition->begin(); itr != pModuleDefinition->end(); ++itr )
  121. {
  122. // Fetch the referenced assets.
  123. ReferencedAssets* pReferencedAssets = dynamic_cast<ReferencedAssets*>( *itr );
  124. // Skip if it's not a referenced assets location.
  125. if ( pReferencedAssets == NULL )
  126. continue;
  127. // Expand asset manifest location.
  128. char filePathBuffer[1024];
  129. dSprintf( filePathBuffer, sizeof(filePathBuffer), "%s/%s", pModuleDefinition->getModulePath(), pReferencedAssets->getPath() );
  130. // Scan referenced assets at location.
  131. if ( !scanReferencedAssets( filePathBuffer, pReferencedAssets->getExtension(), pReferencedAssets->getRecurse() ) )
  132. {
  133. // Warn.
  134. Con::warnf( "AssetManager::compileReferencedAssets() - Could not scan for referenced assets at location '%s' with extension '%s'.", filePathBuffer, pReferencedAssets->getExtension() );
  135. }
  136. }
  137. return true;
  138. }
  139. //-----------------------------------------------------------------------------
  140. bool AssetManager::addModuleDeclaredAssets( ModuleDefinition* pModuleDefinition )
  141. {
  142. // Debug Profiling.
  143. PROFILE_SCOPE(AssetManager_AddDeclaredAssets);
  144. // Sanity!
  145. AssertFatal( pModuleDefinition != NULL, "Cannot add declared assets using a NULL module definition" );
  146. // Does the module have any assets associated with it?
  147. if ( pModuleDefinition->getModuleAssets().size() > 0 )
  148. {
  149. // Yes, so warn.
  150. Con::warnf( "Asset Manager: Cannot add declared assets to module '%s' as it already has existing assets.", pModuleDefinition->getSignature() );
  151. return false;
  152. }
  153. // Iterate the module definition children.
  154. for( SimSet::iterator itr = pModuleDefinition->begin(); itr != pModuleDefinition->end(); ++itr )
  155. {
  156. // Fetch the declared assets.
  157. DeclaredAssets* pDeclaredAssets = dynamic_cast<DeclaredAssets*>( *itr );
  158. // Skip if it's not a declared assets location.
  159. if ( pDeclaredAssets == NULL )
  160. continue;
  161. // Expand asset manifest location.
  162. char filePathBuffer[1024], extensionBuffer[256];
  163. String mdldfpth = pModuleDefinition->getModulePath();
  164. String astfpth = pDeclaredAssets->getPath();
  165. dSprintf(filePathBuffer, sizeof(filePathBuffer), "%s/%s", pModuleDefinition->getModulePath(), pDeclaredAssets->getPath());
  166. dSprintf(extensionBuffer, sizeof(extensionBuffer), "*.%s", pDeclaredAssets->getExtension());
  167. // Scan declared assets at location.
  168. if ( !scanDeclaredAssets( filePathBuffer, extensionBuffer, pDeclaredAssets->getRecurse(), pModuleDefinition ) && mEchoInfo)
  169. {
  170. // Warn.
  171. Con::warnf( "AssetManager::addModuleDeclaredAssets() - No assets found at location '%s' with extension '%s'.", filePathBuffer, pDeclaredAssets->getExtension() );
  172. }
  173. }
  174. return true;
  175. }
  176. bool AssetManager::loadModuleAutoLoadAssets(ModuleDefinition* pModuleDefinition)
  177. {
  178. // Debug Profiling.
  179. PROFILE_SCOPE(AssetManager_loadModuleAutoLoadAssets);
  180. // Sanity!
  181. AssertFatal(pModuleDefinition != NULL, "Cannot auto load assets using a NULL module definition");
  182. // Does the module have any assets associated with it?
  183. if (pModuleDefinition->getModuleAssets().empty() && mEchoInfo)
  184. {
  185. // Yes, so warn.
  186. Con::warnf("Asset Manager: Cannot auto load assets to module '%s' as it has no existing assets.", pModuleDefinition->getSignature());
  187. return false;
  188. }
  189. U32 assetCount = pModuleDefinition->getModuleAssets().size();
  190. // Iterate the module definition children.
  191. for (SimSet::iterator itr = pModuleDefinition->begin(); itr != pModuleDefinition->end(); ++itr)
  192. {
  193. // Fetch the declared assets.
  194. AutoloadAssets* pAutoloadAssets = dynamic_cast<AutoloadAssets*>(*itr);
  195. // Skip if it's not a declared assets location.
  196. if (pAutoloadAssets == NULL)
  197. continue;
  198. for (U32 i = 0; i < assetCount; ++i)
  199. {
  200. AssetDefinition* assetDef = pModuleDefinition->getModuleAssets()[i];
  201. if (assetDef->mAssetType == pAutoloadAssets->getAssetType())
  202. {
  203. String assetPath = assetDef->mAssetBaseFilePath;
  204. assetPath.replace("//", "/");
  205. String autoLoadPath = pModuleDefinition->getModulePath();
  206. autoLoadPath += "/";
  207. autoLoadPath += pAutoloadAssets->getPath();
  208. if (pAutoloadAssets->getPath() == StringTable->EmptyString() || assetPath.startsWith(autoLoadPath.c_str()))
  209. {
  210. AssetBase* assetBase = dynamic_cast<AssetBase*>(mTaml.read(assetDef->mAssetBaseFilePath));
  211. //load the asset now if valid
  212. if (assetBase)
  213. {
  214. assetBase->setOwned(this, assetDef);
  215. }
  216. }
  217. }
  218. }
  219. }
  220. return true;
  221. }
  222. //-----------------------------------------------------------------------------
  223. bool AssetManager::addDeclaredAsset( ModuleDefinition* pModuleDefinition, const char* pAssetFilePath )
  224. {
  225. // Debug Profiling.
  226. PROFILE_SCOPE(AssetManager_AddSingleDeclaredAsset);
  227. // Sanity!
  228. AssertFatal( pModuleDefinition != NULL, "Cannot add single declared asset using a NULL module definition" );
  229. AssertFatal( pAssetFilePath != NULL, "Cannot add single declared asset using a NULL asset file-path." );
  230. // Expand asset file-path.
  231. char assetFilePathBuffer[1024];
  232. dStrcpy(assetFilePathBuffer, Platform::makeRelativePathName(pAssetFilePath, NULL), sizeof(assetFilePathBuffer));
  233. // Find the final slash which should be just before the file.
  234. char* pFileStart = dStrrchr( assetFilePathBuffer, '/' );
  235. // Did we find the final slash?
  236. if ( pFileStart == NULL )
  237. {
  238. // No, so warn.
  239. Con::warnf( "AssetManager::addDeclaredAsset() - Could not add single declared asset file '%s' as file-path '%s' is not valid.",
  240. assetFilePathBuffer,
  241. pModuleDefinition->getModulePath() );
  242. return false;
  243. }
  244. // Terminate path at slash.
  245. *pFileStart = 0;
  246. // Move to next character which should be the file start.
  247. pFileStart++;
  248. // Scan declared assets at location.
  249. if ( !scanDeclaredAssets( assetFilePathBuffer, pFileStart, false, pModuleDefinition ) )
  250. {
  251. // Warn.
  252. Con::warnf( "AssetManager::addDeclaredAsset() - Could not scan declared assets at location '%s' with extension '%s'.", assetFilePathBuffer, pFileStart );
  253. return false;
  254. }
  255. return true;
  256. }
  257. //-----------------------------------------------------------------------------
  258. StringTableEntry AssetManager::addPrivateAsset( AssetBase* pAssetBase )
  259. {
  260. // Debug Profiling.
  261. PROFILE_SCOPE(AssetManager_AddPrivateAsset);
  262. // Sanity!
  263. AssertFatal( pAssetBase != NULL, "Cannot add a NULL private asset." );
  264. // Is the asset already added?
  265. if (pAssetBase->mpAssetDefinition->mAssetId != StringTable->EmptyString())
  266. {
  267. // Yes, so warn.
  268. Con::warnf( "Cannot add private asset '%d' as it has already been assigned.", pAssetBase->mpAssetDefinition->mAssetId );
  269. return StringTable->EmptyString();
  270. }
  271. static U32 masterPrivateAssetId = 1;
  272. // Create asset definition.
  273. AssetDefinition* pAssetDefinition = new AssetDefinition();
  274. // Fetch source asset definition.
  275. AssetDefinition* pSourceAssetDefinition = pAssetBase->mpAssetDefinition;
  276. // Configure asset.
  277. pAssetDefinition->mpAssetBase = pAssetBase;
  278. pAssetDefinition->mAssetDescription = pSourceAssetDefinition->mAssetDescription;
  279. pAssetDefinition->mAssetCategory = pSourceAssetDefinition->mAssetCategory;
  280. pAssetDefinition->mAssetAutoUnload = true;
  281. pAssetDefinition->mAssetRefreshEnable = false;
  282. pAssetDefinition->mAssetType = StringTable->insert( pAssetBase->getClassName() );
  283. pAssetDefinition->mAssetLoadedCount = 0;
  284. pAssetDefinition->mAssetInternal = false;
  285. pAssetDefinition->mAssetPrivate = true;
  286. // Format asset name.
  287. char assetNameBuffer[256];
  288. dSprintf(assetNameBuffer, sizeof(assetNameBuffer), "%s_%d", pAssetDefinition->mAssetType, masterPrivateAssetId++ );
  289. // Set asset identity.
  290. pAssetDefinition->mAssetName = StringTable->insert( assetNameBuffer );
  291. pAssetDefinition->mAssetId = pAssetDefinition->mAssetName;
  292. // Ensure that the source asset is fully synchronized with the new asset definition.
  293. pSourceAssetDefinition->mAssetName = pAssetDefinition->mAssetName;
  294. pSourceAssetDefinition->mAssetAutoUnload = pAssetDefinition->mAssetAutoUnload;
  295. pSourceAssetDefinition->mAssetInternal = pAssetDefinition->mAssetInternal;
  296. // Set ownership by asset manager.
  297. pAssetDefinition->mpAssetBase->setOwned( this, pAssetDefinition );
  298. // Store in declared assets.
  299. mDeclaredAssets.insert( pAssetDefinition->mAssetId, pAssetDefinition );
  300. // Increase the private loaded asset count.
  301. if ( ++mLoadedPrivateAssetsCount > mMaxLoadedPrivateAssetsCount )
  302. mMaxLoadedPrivateAssetsCount = mLoadedPrivateAssetsCount;
  303. return pAssetDefinition->mAssetId;
  304. }
  305. //-----------------------------------------------------------------------------
  306. bool AssetManager::removeDeclaredAssets( ModuleDefinition* pModuleDefinition )
  307. {
  308. // Debug Profiling.
  309. PROFILE_SCOPE(AssetManager_RemoveDeclaredAssets);
  310. // Sanity!
  311. AssertFatal( pModuleDefinition != NULL, "Cannot remove declared assets using a NULL module definition" );
  312. // Fetch module assets.
  313. ModuleDefinition::typeModuleAssetsVector& moduleAssets = pModuleDefinition->getModuleAssets();
  314. // Remove all module assets.
  315. while ( moduleAssets.size() > 0 )
  316. {
  317. // Fetch asset definition.
  318. AssetDefinition* pAssetDefinition = *moduleAssets.begin();
  319. // Remove this asset.
  320. removeDeclaredAsset( pAssetDefinition->mAssetId );
  321. }
  322. // Info.
  323. if ( mEchoInfo )
  324. Con::printSeparator();
  325. return true;
  326. }
  327. //-----------------------------------------------------------------------------
  328. bool AssetManager::removeDeclaredAsset( const char* pAssetId )
  329. {
  330. // Debug Profiling.
  331. PROFILE_SCOPE(AssetManager_RemoveSingleDeclaredAsset);
  332. // Sanity!
  333. AssertFatal( pAssetId != NULL, "Cannot remove single declared asset using NULL asset Id." );
  334. // Fetch asset Id.
  335. StringTableEntry assetId = StringTable->insert( pAssetId );
  336. // Find declared asset.
  337. typeDeclaredAssetsHash::iterator declaredAssetItr = mDeclaredAssets.find( assetId );
  338. // Did we find the declared asset?
  339. if ( declaredAssetItr == mDeclaredAssets.end() )
  340. {
  341. // No, so warn.
  342. Con::warnf( "Asset Manager: Cannot remove single asset Id '%s' it could not be found.", assetId );
  343. return false;
  344. }
  345. // Fetch asset definition.
  346. AssetDefinition* pAssetDefinition = declaredAssetItr->value;
  347. // Is the asset private?
  348. if ( !pAssetDefinition->mAssetPrivate )
  349. {
  350. // No, so fetch module assets.
  351. ModuleDefinition::typeModuleAssetsVector& moduleAssets = pAssetDefinition->mpModuleDefinition->getModuleAssets();
  352. // Remove module asset.
  353. for ( ModuleDefinition::typeModuleAssetsVector::iterator moduleAssetItr = moduleAssets.begin(); moduleAssetItr != moduleAssets.end(); ++moduleAssetItr )
  354. {
  355. if ( *moduleAssetItr == pAssetDefinition )
  356. {
  357. moduleAssets.erase( moduleAssetItr );
  358. break;
  359. }
  360. }
  361. // Remove asset dependencies.
  362. removeAssetDependencies( pAssetId );
  363. }
  364. // Do we have an asset loaded?
  365. if ( pAssetDefinition->mpAssetBase != NULL )
  366. {
  367. // Yes, so delete it.
  368. // NOTE: If anything is using this then this'll cause a crash. Objects should always use safe reference methods however.
  369. pAssetDefinition->mpAssetBase->deleteObject();
  370. }
  371. // Remove from declared assets.
  372. mDeclaredAssets.erase( declaredAssetItr );
  373. // Info.
  374. if ( mEchoInfo )
  375. {
  376. Con::printf( "Asset Manager: Removing Asset Id '%s' of type '%s' in asset file '%s'.",
  377. pAssetDefinition->mAssetId,
  378. pAssetDefinition->mAssetType,
  379. pAssetDefinition->mAssetBaseFilePath );
  380. }
  381. // Destroy asset definition.
  382. delete pAssetDefinition;
  383. return true;
  384. }
  385. //-----------------------------------------------------------------------------
  386. StringTableEntry AssetManager::getAssetName( const char* pAssetId )
  387. {
  388. // Find asset definition.
  389. AssetDefinition* pAssetDefinition = findAsset( pAssetId );
  390. // Did we find the asset?
  391. if ( pAssetDefinition == NULL )
  392. {
  393. // No, so warn.
  394. Con::warnf( "Asset Manager: Cannot find asset Id '%s'.", pAssetId );
  395. return NULL;
  396. }
  397. return pAssetDefinition->mAssetName;
  398. }
  399. //-----------------------------------------------------------------------------
  400. StringTableEntry AssetManager::getAssetDescription( const char* pAssetId )
  401. {
  402. // Find asset definition.
  403. AssetDefinition* pAssetDefinition = findAsset( pAssetId );
  404. // Did we find the asset?
  405. if ( pAssetDefinition == NULL )
  406. {
  407. // No, so warn.
  408. Con::warnf( "Asset Manager: Cannot find asset Id '%s'.", pAssetId );
  409. return NULL;
  410. }
  411. return pAssetDefinition->mAssetDescription;
  412. }
  413. //-----------------------------------------------------------------------------
  414. StringTableEntry AssetManager::getAssetCategory( const char* pAssetId )
  415. {
  416. // Find asset definition.
  417. AssetDefinition* pAssetDefinition = findAsset( pAssetId );
  418. // Did we find the asset?
  419. if ( pAssetDefinition == NULL )
  420. {
  421. // No, so warn.
  422. Con::warnf( "Asset Manager: Cannot find asset Id '%s'.", pAssetId );
  423. return NULL;
  424. }
  425. return pAssetDefinition->mAssetCategory;
  426. }
  427. //-----------------------------------------------------------------------------
  428. StringTableEntry AssetManager::getAssetType( const char* pAssetId )
  429. {
  430. // Find asset definition.
  431. AssetDefinition* pAssetDefinition = findAsset( pAssetId );
  432. // Did we find the asset?
  433. if ( pAssetDefinition == NULL )
  434. {
  435. // No, so warn.
  436. Con::warnf( "Asset Manager: Cannot find asset Id '%s'.", pAssetId );
  437. return NULL;
  438. }
  439. return pAssetDefinition->mAssetType;
  440. }
  441. //-----------------------------------------------------------------------------
  442. StringTableEntry AssetManager::getAssetFilePath( const char* pAssetId )
  443. {
  444. // Find asset definition.
  445. AssetDefinition* pAssetDefinition = findAsset( pAssetId );
  446. // Did we find the asset?
  447. if ( pAssetDefinition == NULL )
  448. {
  449. // No, so warn.
  450. Con::warnf( "Asset Manager: Cannot find asset Id '%s'.", pAssetId );
  451. return StringTable->EmptyString();
  452. }
  453. return pAssetDefinition->mAssetBaseFilePath;
  454. }
  455. //-----------------------------------------------------------------------------
  456. StringTableEntry AssetManager::getAssetPath( const char* pAssetId )
  457. {
  458. // Debug Profiling.
  459. PROFILE_SCOPE(AssetManager_GetAssetPath);
  460. // Fetch asset file-path.
  461. StringTableEntry assetFilePath = getAssetFilePath( pAssetId );
  462. // Finish if no file-path.
  463. if ( assetFilePath == StringTable->EmptyString() )
  464. return assetFilePath;
  465. // Find the final slash which should be just before the file.
  466. const char* pFinalSlash = dStrrchr( assetFilePath, '/' );
  467. // Sanity!
  468. AssertFatal( pFinalSlash != NULL, "Should always be able to find final slash in the asset file-path." );
  469. // Fetch asset path.
  470. return StringTable->insertn( assetFilePath, (U32)(pFinalSlash - assetFilePath) );
  471. }
  472. //-----------------------------------------------------------------------------
  473. ModuleDefinition* AssetManager::getAssetModuleDefinition( const char* pAssetId )
  474. {
  475. // Find asset definition.
  476. AssetDefinition* pAssetDefinition = findAsset( pAssetId );
  477. // Did we find the asset?
  478. if ( pAssetDefinition == NULL )
  479. {
  480. // No, so warn.
  481. Con::warnf( "Asset Manager: Cannot find asset Id '%s'.", pAssetId );
  482. return NULL;
  483. }
  484. return pAssetDefinition->mpModuleDefinition;
  485. }
  486. //-----------------------------------------------------------------------------
  487. bool AssetManager::isAssetInternal( const char* pAssetId )
  488. {
  489. // Find asset definition.
  490. AssetDefinition* pAssetDefinition = findAsset( pAssetId );
  491. // Did we find the asset?
  492. if ( pAssetDefinition == NULL )
  493. {
  494. // No, so warn.
  495. Con::warnf( "Asset Manager: Cannot find asset Id '%s'.", pAssetId );
  496. return false;
  497. }
  498. return pAssetDefinition->mAssetInternal;
  499. }
  500. //-----------------------------------------------------------------------------
  501. bool AssetManager::isAssetPrivate( const char* pAssetId )
  502. {
  503. // Find asset definition.
  504. AssetDefinition* pAssetDefinition = findAsset( pAssetId );
  505. // Did we find the asset?
  506. if ( pAssetDefinition == NULL )
  507. {
  508. // No, so warn.
  509. Con::warnf( "Asset Manager: Cannot find asset Id '%s'.", pAssetId );
  510. return false;
  511. }
  512. return pAssetDefinition->mAssetPrivate;
  513. }
  514. //-----------------------------------------------------------------------------
  515. bool AssetManager::isAssetAutoUnload( const char* pAssetId )
  516. {
  517. // Find asset definition.
  518. AssetDefinition* pAssetDefinition = findAsset( pAssetId );
  519. // Did we find the asset?
  520. if ( pAssetDefinition == NULL )
  521. {
  522. // No, so warn.
  523. Con::warnf( "Asset Manager: Cannot find asset Id '%s'.", pAssetId );
  524. return false;
  525. }
  526. return pAssetDefinition->mAssetAutoUnload;
  527. }
  528. //-----------------------------------------------------------------------------
  529. bool AssetManager::isAssetLoaded( const char* pAssetId )
  530. {
  531. // Find asset definition.
  532. AssetDefinition* pAssetDefinition = findAsset( pAssetId );
  533. // Did we find the asset?
  534. if ( pAssetDefinition == NULL )
  535. {
  536. // No, so warn.
  537. Con::warnf( "Asset Manager: Cannot find asset Id '%s'.", pAssetId );
  538. return false;
  539. }
  540. return pAssetDefinition->mpAssetBase != NULL;
  541. }
  542. //-----------------------------------------------------------------------------
  543. bool AssetManager::isDeclaredAsset( const char* pAssetId )
  544. {
  545. return findAsset( pAssetId ) != NULL;
  546. }
  547. //-----------------------------------------------------------------------------
  548. bool AssetManager::doesAssetDependOn( const char* pAssetId, const char* pDependsOnAssetId )
  549. {
  550. // Debug Profiling.
  551. PROFILE_SCOPE(AssetManager_DoesAssetDependOn);
  552. // Sanity!
  553. AssertFatal( pAssetId != NULL, "Cannot use NULL asset Id." );
  554. AssertFatal( pDependsOnAssetId != NULL, "Cannot use NULL depends-on asset Id." );
  555. // Fetch asset Id.
  556. StringTableEntry assetId = StringTable->insert( pAssetId );
  557. // Fetch depends-on asset Id.
  558. StringTableEntry dependsOnAssetId = StringTable->insert( pDependsOnAssetId );
  559. // Find depends-on entry.
  560. typeAssetDependsOnHash::Iterator dependsOnItr = mAssetDependsOn.find( assetId );
  561. // Iterate all dependencies.
  562. while( dependsOnItr != mAssetDependsOn.end() && dependsOnItr->key == assetId )
  563. {
  564. // Finish if a depends on.
  565. if ( dependsOnItr->value == dependsOnAssetId )
  566. return true;
  567. // Next dependency.
  568. dependsOnItr++;
  569. }
  570. return false;
  571. }
  572. //-----------------------------------------------------------------------------
  573. bool AssetManager::isAssetDependedOn( const char* pAssetId, const char* pDependedOnByAssetId )
  574. {
  575. // Debug Profiling.
  576. PROFILE_SCOPE(AssetManager_IsAssetDependedOn);
  577. // Sanity!
  578. AssertFatal( pAssetId != NULL, "Cannot use NULL asset Id." );
  579. AssertFatal( pDependedOnByAssetId != NULL, "Cannot use NULL depended-on-by asset Id." );
  580. // Fetch asset Id.
  581. StringTableEntry assetId = StringTable->insert( pAssetId );
  582. // Fetch depended-on-by asset Id.
  583. StringTableEntry dependedOnByAssetId = StringTable->insert( pDependedOnByAssetId );
  584. // Find depended-on-by entry.
  585. typeAssetDependsOnHash::Iterator dependedOnItr = mAssetIsDependedOn.find(assetId);
  586. // Iterate all dependencies.
  587. while( dependedOnItr != mAssetIsDependedOn.end() && dependedOnItr->key == assetId )
  588. {
  589. // Finish if depended-on.
  590. if ( dependedOnItr->value == dependedOnByAssetId )
  591. return true;
  592. // Next dependency.
  593. dependedOnItr++;
  594. }
  595. return false;
  596. }
  597. //-----------------------------------------------------------------------------
  598. bool AssetManager::isReferencedAsset( const char* pAssetId )
  599. {
  600. // Debug Profiling.
  601. PROFILE_SCOPE(AssetManager_IsReferencedAsset);
  602. // Sanity!
  603. AssertFatal( pAssetId != NULL, "Cannot check if NULL asset Id is referenced." );
  604. // Fetch asset Id.
  605. StringTableEntry assetId = StringTable->insert( pAssetId );
  606. // Is asset Id the correct format?
  607. if ( StringUnit::getUnitCount( assetId, ASSET_SCOPE_TOKEN ) != 2 )
  608. {
  609. // No, so warn.
  610. Con::warnf( "Asset Manager: Cannot check if asset Id '%s' is referenced as it is not the correct format.", assetId );
  611. return false;
  612. }
  613. return mReferencedAssets.count( assetId ) > 0;
  614. }
  615. //-----------------------------------------------------------------------------
  616. bool AssetManager::renameDeclaredAsset( const char* pAssetIdFrom, const char* pAssetIdTo )
  617. {
  618. // Debug Profiling.
  619. PROFILE_SCOPE(AssetManager_RenameDeclaredAsset);
  620. // Sanity!
  621. AssertFatal( pAssetIdFrom != NULL, "Cannot rename from NULL asset Id." );
  622. AssertFatal( pAssetIdTo != NULL, "Cannot rename to NULL asset Id." );
  623. // Fetch asset Ids.
  624. StringTableEntry assetIdFrom = StringTable->insert( pAssetIdFrom );
  625. StringTableEntry assetIdTo = StringTable->insert( pAssetIdTo );
  626. // Is asset Id from the correct format?
  627. if ( StringUnit::getUnitCount( assetIdFrom, ASSET_SCOPE_TOKEN ) != 2 )
  628. {
  629. // No, so warn.
  630. Con::warnf("Asset Manager: Cannot rename declared asset Id '%s' to asset Id '%s' as source asset Id is not the correct format.", assetIdFrom, assetIdTo );
  631. return false;
  632. }
  633. // Is asset Id to the correct format?
  634. if ( StringUnit::getUnitCount( assetIdTo, ASSET_SCOPE_TOKEN ) != 2 )
  635. {
  636. // No, so warn.
  637. Con::warnf("Asset Manager: Cannot rename declared asset Id '%s' to asset Id '%s' as target asset Id is not the correct format.", assetIdFrom, assetIdTo );
  638. return false;
  639. }
  640. // Does the asset Id from exist?
  641. if ( !mDeclaredAssets.contains( assetIdFrom ) )
  642. {
  643. // No, so warn.
  644. Con::warnf("Asset Manager: Cannot rename declared asset Id '%s' to asset Id '%s' as source asset Id is not declared.", assetIdFrom, assetIdTo );
  645. return false;
  646. }
  647. // Does the asset Id to exist?
  648. if ( mDeclaredAssets.contains( assetIdTo ) )
  649. {
  650. // No, so warn.
  651. Con::warnf("Asset Manager: Cannot rename declared asset Id '%s' to asset Id '%s' as target asset Id is already declared.", assetIdFrom, assetIdTo );
  652. return false;
  653. }
  654. // Split module Ids from asset Ids.
  655. StringTableEntry moduleIdFrom = StringTable->insert( StringUnit::getUnit( assetIdFrom, 0, ASSET_SCOPE_TOKEN ) );
  656. StringTableEntry moduleIdTo = StringTable->insert( StringUnit::getUnit( assetIdTo, 0, ASSET_SCOPE_TOKEN ) );
  657. // Are the module Ids the same?
  658. if ( moduleIdFrom != moduleIdTo )
  659. {
  660. // No, so warn.
  661. Con::warnf("Asset Manager: Cannot rename declared asset Id '%s' to asset Id '%s' as the module Id cannot be changed.", assetIdFrom, assetIdTo );
  662. return false;
  663. }
  664. // Find asset definition.
  665. typeDeclaredAssetsHash::iterator assetDefinitionItr = mDeclaredAssets.find( assetIdFrom );
  666. // Sanity!
  667. AssertFatal( assetDefinitionItr != mDeclaredAssets.end(), "Asset Manager: Failed to find asset." );
  668. // Fetch asset definition.
  669. AssetDefinition* pAssetDefinition = assetDefinitionItr->value;
  670. // Is this a private asset?
  671. if ( pAssetDefinition->mAssetPrivate )
  672. {
  673. // Yes, so warn.
  674. Con::warnf("Asset Manager: Cannot rename declared asset Id '%s' to asset Id '%s' as the source asset is private.", assetIdFrom, assetIdTo );
  675. return false;
  676. }
  677. // Setup declared update visitor.
  678. TamlAssetDeclaredUpdateVisitor assetDeclaredUpdateVisitor;
  679. assetDeclaredUpdateVisitor.setAssetIdFrom( assetIdFrom );
  680. assetDeclaredUpdateVisitor.setAssetIdTo( assetIdTo );
  681. // Update asset file declaration.
  682. if ( !mTaml.parse( pAssetDefinition->mAssetBaseFilePath, assetDeclaredUpdateVisitor ) )
  683. {
  684. // No, so warn.
  685. Con::warnf("Asset Manager: Cannot rename declared asset Id '%s' to asset Id '%s' as the declared asset file could not be parsed: %s",
  686. assetIdFrom, assetIdTo, pAssetDefinition->mAssetBaseFilePath );
  687. return false;
  688. }
  689. // Update asset definition.
  690. pAssetDefinition->mAssetId = assetIdTo;
  691. pAssetDefinition->mAssetName = StringTable->insert( StringUnit::getUnit( assetIdTo, 1, ASSET_SCOPE_TOKEN ) );
  692. // Reinsert declared asset.
  693. mDeclaredAssets.erase( assetIdFrom );
  694. mDeclaredAssets.insert( assetIdTo, pAssetDefinition );
  695. // Info.
  696. if ( mEchoInfo )
  697. {
  698. Con::printf( "Asset Manager: Renaming declared Asset Id '%s' to Asset Id '%s'.", assetIdFrom, assetIdTo );
  699. Con::printSeparator();
  700. }
  701. // Rename asset dependencies.
  702. renameAssetDependencies( assetIdFrom, assetIdTo );
  703. // Do we have an asset tags manifest?
  704. if ( !mAssetTagsManifest.isNull() )
  705. {
  706. // Yes, so rename any assets.
  707. mAssetTagsManifest->renameAssetId( pAssetIdFrom, pAssetIdTo );
  708. // Save the asset tags.
  709. saveAssetTags();
  710. }
  711. return true;
  712. }
  713. //-----------------------------------------------------------------------------
  714. bool AssetManager::renameReferencedAsset( const char* pAssetIdFrom, const char* pAssetIdTo )
  715. {
  716. // Debug Profiling.
  717. PROFILE_SCOPE(AssetManager_RenameReferencedAsset);
  718. // Sanity!
  719. AssertFatal( pAssetIdFrom != NULL, "Cannot rename from NULL asset Id." );
  720. AssertFatal( pAssetIdTo != NULL, "Cannot rename to NULL asset Id." );
  721. // Fetch asset Ids.
  722. StringTableEntry assetIdFrom = StringTable->insert( pAssetIdFrom );
  723. StringTableEntry assetIdTo = StringTable->insert( pAssetIdTo );
  724. // Is asset Id from the correct format?
  725. if ( StringUnit::getUnitCount( assetIdFrom, ASSET_SCOPE_TOKEN ) != 2 )
  726. {
  727. // No, so warn.
  728. Con::warnf("Asset Manager: Cannot rename referenced asset Id '%s' to asset Id '%s' as source asset Id is not the correct format.", assetIdFrom, assetIdTo );
  729. return false;
  730. }
  731. // Is asset Id to the correct format?
  732. if ( StringUnit::getUnitCount( assetIdTo, ASSET_SCOPE_TOKEN ) != 2 )
  733. {
  734. // No, so warn.
  735. Con::warnf("Asset Manager: Cannot rename referenced asset Id '%s' to asset Id '%s' as target asset Id is not the correct format.", assetIdFrom, assetIdTo );
  736. return false;
  737. }
  738. // Does the asset Id to exist?
  739. if ( !mDeclaredAssets.contains( assetIdTo ) )
  740. {
  741. // No, so warn.
  742. Con::warnf("Asset Manager: Cannot rename referenced asset Id '%s' to asset Id '%s' as target asset Id is not declared.", assetIdFrom, assetIdTo );
  743. return false;
  744. }
  745. // Rename asset references.
  746. renameAssetReferences( assetIdFrom, assetIdTo );
  747. // Info.
  748. if ( mEchoInfo )
  749. Con::printSeparator();
  750. return true;
  751. }
  752. //-----------------------------------------------------------------------------
  753. bool AssetManager::releaseAsset( const char* pAssetId )
  754. {
  755. // Debug Profiling.
  756. PROFILE_SCOPE(AssetManager_ReleaseAsset);
  757. // Sanity!
  758. AssertFatal( pAssetId != NULL, "Cannot release NULL asset Id." );
  759. // Find asset.
  760. AssetDefinition* pAssetDefinition = findAsset( pAssetId );
  761. // Did we find the asset?
  762. if ( pAssetDefinition == NULL )
  763. {
  764. // No, so warn.
  765. Con::warnf( "Asset Manager: Failed to release asset Id '%s' as it does not exist.", pAssetId );
  766. return false;
  767. }
  768. // Is the asset loaded?
  769. if ( pAssetDefinition->mpAssetBase == NULL )
  770. {
  771. // No, so warn.
  772. Con::warnf( "Asset Manager: Failed to release asset Id '%s' as it is not acquired.", pAssetId );
  773. return false;
  774. }
  775. // Info.
  776. if ( mEchoInfo )
  777. {
  778. Con::printSeparator();
  779. Con::printf( "Asset Manager: Started releasing Asset Id '%s'...", pAssetId );
  780. }
  781. // Release asset reference.
  782. if ( pAssetDefinition->mpAssetBase->releaseAssetReference() )
  783. {
  784. // Are we ignoring auto-unloaded assets?
  785. if ( mIgnoreAutoUnload )
  786. {
  787. // Yes, so info.
  788. if ( mEchoInfo )
  789. {
  790. Con::printf( "Asset Manager: > Releasing to idle state." );
  791. }
  792. }
  793. else
  794. {
  795. // No, so info.
  796. if ( mEchoInfo )
  797. {
  798. Con::printf( "Asset Manager: > Unload the asset from memory." );
  799. }
  800. // Unload the asset.
  801. unloadAsset( pAssetDefinition );
  802. }
  803. }
  804. // Info.
  805. else if ( mEchoInfo )
  806. {
  807. Con::printf( "Asset Manager: > Reference count now '%d'.", pAssetDefinition->mpAssetBase->getAcquiredReferenceCount() );
  808. }
  809. // Info.
  810. if ( mEchoInfo )
  811. {
  812. Con::printf( "Asset Manager: > Finished releasing Asset Id '%s'.", pAssetId );
  813. Con::printSeparator();
  814. }
  815. return true;
  816. }
  817. //-----------------------------------------------------------------------------
  818. void AssetManager::purgeAssets( void )
  819. {
  820. // Debug Profiling.
  821. PROFILE_SCOPE(AssetManager_PurgeAssets);
  822. // Info.
  823. if ( mEchoInfo )
  824. {
  825. Con::printf( "Asset Manager: Started purging assets..." );
  826. }
  827. // Iterate asset definitions.
  828. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  829. {
  830. // Fetch asset definition.
  831. AssetDefinition* pAssetDefinition = assetItr->value;
  832. // Skip asset if private, not loaded or referenced.
  833. if ( pAssetDefinition->mAssetPrivate ||
  834. pAssetDefinition->mpAssetBase == NULL ||
  835. pAssetDefinition->mpAssetBase->getAcquiredReferenceCount() > 0 )
  836. continue;
  837. // Info.
  838. if ( mEchoInfo )
  839. {
  840. Con::printf( "Asset Manager: Purging asset Id '%s'...", pAssetDefinition->mAssetId );
  841. }
  842. // Unload the asset.
  843. unloadAsset( pAssetDefinition );
  844. }
  845. // Info.
  846. if ( mEchoInfo )
  847. {
  848. Con::printf( "Asset Manager: ... Finished purging assets." );
  849. }
  850. }
  851. //-----------------------------------------------------------------------------
  852. bool AssetManager::deleteAsset( const char* pAssetId, const bool deleteLooseFiles, const bool deleteDependencies )
  853. {
  854. // Debug Profiling.
  855. PROFILE_SCOPE(AssetManager_DeleteAsset);
  856. // Sanity!
  857. AssertFatal( pAssetId != NULL, "Cannot delete NULL asset Id." );
  858. // Find asset.
  859. AssetDefinition* pAssetDefinition = findAsset( pAssetId );
  860. // Did we find the asset?
  861. if ( pAssetDefinition == NULL )
  862. {
  863. // No, so warn.
  864. Con::warnf( "Asset Manager: Failed to delete asset Id '%s' as it does not exist.", pAssetId );
  865. return false;
  866. }
  867. // Info.
  868. if ( mEchoInfo )
  869. {
  870. Con::printSeparator();
  871. Con::printf( "Asset Manager: Started deleting Asset Id '%s'...", pAssetId );
  872. }
  873. // Fetch asset Id.
  874. StringTableEntry assetId = StringTable->insert( pAssetId );
  875. // Are we deleting dependencies?
  876. if ( deleteDependencies )
  877. {
  878. Vector<typeAssetId> dependantAssets;
  879. // Yes, so find depended-on-by entry.
  880. typeAssetDependsOnHash::Iterator dependedOnItr = mAssetIsDependedOn.find( assetId );
  881. // Iterate all dependencies.
  882. while( dependedOnItr != mAssetIsDependedOn.end() && dependedOnItr->key == assetId )
  883. {
  884. // Store asset Id.
  885. dependantAssets.push_back( dependedOnItr->value );
  886. // Next dependency.
  887. dependedOnItr++;
  888. }
  889. // Do we have any dependants?
  890. if ( dependantAssets.size() > 0 )
  891. {
  892. // Yes, so iterate dependants.
  893. for( Vector<typeAssetId>::const_iterator assetItr = dependantAssets.begin(); assetItr != dependantAssets.end(); ++assetItr )
  894. {
  895. StringTableEntry dependentAssetId = *assetItr;
  896. // Info.
  897. if ( mEchoInfo )
  898. {
  899. Con::printSeparator();
  900. Con::printf( "Asset Manager: Deleting Asset Id '%s' dependant of '%s.'", pAssetId, dependentAssetId );
  901. }
  902. // Delete dependant.
  903. deleteAsset( dependentAssetId, deleteLooseFiles, deleteDependencies );
  904. }
  905. }
  906. }
  907. // Remove asset references.
  908. removeAssetReferences( assetId );
  909. // Are we deleting loose files?
  910. if ( deleteLooseFiles )
  911. {
  912. // Yes, so remove loose files.
  913. Vector<StringTableEntry>& assetLooseFiles = pAssetDefinition->mAssetLooseFiles;
  914. for( Vector<StringTableEntry>::iterator looseFileItr = assetLooseFiles.begin(); looseFileItr != assetLooseFiles.end(); ++looseFileItr )
  915. {
  916. // Fetch loose file.
  917. StringTableEntry looseFile = *looseFileItr;
  918. // Delete the loose file.
  919. if ( !dFileDelete( looseFile ) )
  920. {
  921. // Failed so warn.
  922. Con::warnf( "Asset Manager: Failed to delete the loose file '%s' while deleting asset Id '%s'.", looseFile, pAssetId );
  923. }
  924. }
  925. }
  926. // Fetch asset definition file.
  927. StringTableEntry assetDefinitionFile = pAssetDefinition->mAssetBaseFilePath;
  928. // Remove reference here as we're about to remove the declared asset.
  929. pAssetDefinition = NULL;
  930. // Remove asset.
  931. removeDeclaredAsset( pAssetId );
  932. // Delete the asset definition file.
  933. if ( !dFileDelete( assetDefinitionFile ) )
  934. {
  935. // Failed so warn.
  936. Con::warnf( "Asset Manager: Failed to delete the asset definition file '%s' while deleting asset Id '%s'.", assetDefinitionFile, pAssetId );
  937. }
  938. // Info.
  939. if ( mEchoInfo )
  940. {
  941. Con::printSeparator();
  942. Con::printf( "Asset Manager: Finished deleting Asset Id '%s'.", pAssetId );
  943. }
  944. return true;
  945. }
  946. //-----------------------------------------------------------------------------
  947. bool AssetManager::refreshAsset( const char* pAssetId )
  948. {
  949. // Debug Profiling.
  950. PROFILE_SCOPE(AssetManager_RefreshAsset);
  951. // Sanity!
  952. AssertFatal( pAssetId != NULL, "Cannot refresh NULL asset Id." );
  953. // Find asset.
  954. AssetDefinition* pAssetDefinition = findAsset( pAssetId );
  955. // Did we find the asset?
  956. if ( pAssetDefinition == NULL )
  957. {
  958. // No, so warn.
  959. Con::warnf( "Asset Manager: Failed to refresh asset Id '%s' as it does not exist.", pAssetId );
  960. return false;
  961. }
  962. // Info.
  963. if ( mEchoInfo )
  964. {
  965. Con::printSeparator();
  966. Con::printf( "Asset Manager: Started refreshing Asset Id '%s'...", pAssetId );
  967. }
  968. // Fetch asset Id.
  969. StringTableEntry assetId = StringTable->insert( pAssetId );
  970. // Is the asset private?
  971. if ( pAssetDefinition->mAssetPrivate )
  972. {
  973. // Yes, so notify asset of asset refresh only.
  974. pAssetDefinition->mpAssetBase->onAssetRefresh();
  975. // Asset refresh notifications.
  976. for( typeAssetPtrRefreshHash::iterator refreshNotifyItr = mAssetPtrRefreshNotifications.begin(); refreshNotifyItr != mAssetPtrRefreshNotifications.end(); ++refreshNotifyItr )
  977. {
  978. // Fetch pointed asset.
  979. StringTableEntry pointedAsset = refreshNotifyItr->key->getAssetId();
  980. // Ignore if the pointed asset is not the asset or a dependency.
  981. if ( pointedAsset == StringTable->EmptyString() || ( pointedAsset != assetId && !doesAssetDependOn( pointedAsset, assetId ) ) )
  982. continue;
  983. // Perform refresh notification callback.
  984. refreshNotifyItr->value->onAssetRefreshed( refreshNotifyItr->key );
  985. }
  986. }
  987. // Is the asset definition allowed to refresh?
  988. else if ( pAssetDefinition->mAssetRefreshEnable )
  989. {
  990. // Yes, so fetch the asset.
  991. AssetBase* pAssetBase = pAssetDefinition->mpAssetBase;
  992. // Is the asset loaded?
  993. if ( pAssetBase != NULL )
  994. {
  995. // Yes, so notify asset of asset refresh.
  996. pAssetBase->onAssetRefresh();
  997. // Save asset.
  998. mTaml.write( pAssetBase, pAssetDefinition->mAssetBaseFilePath );
  999. // Remove asset dependencies.
  1000. removeAssetDependencies( pAssetId );
  1001. // Find any new dependencies.
  1002. TamlAssetDeclaredVisitor assetDeclaredVisitor;
  1003. // Parse the filename.
  1004. if ( !mTaml.parse( pAssetDefinition->mAssetBaseFilePath, assetDeclaredVisitor ) )
  1005. {
  1006. // Warn.
  1007. Con::warnf( "Asset Manager: Failed to parse file containing asset declaration: '%s'.\nDependencies are now incorrect!", pAssetDefinition->mAssetBaseFilePath );
  1008. return false;
  1009. }
  1010. // Fetch asset dependencies.
  1011. TamlAssetDeclaredVisitor::typeAssetIdVector& assetDependencies = assetDeclaredVisitor.getAssetDependencies();
  1012. // Are there any asset dependences?
  1013. if ( assetDependencies.size() > 0 )
  1014. {
  1015. // Yes, so iterate dependencies.
  1016. for( TamlAssetDeclaredVisitor::typeAssetIdVector::iterator assetDependencyItr = assetDependencies.begin(); assetDependencyItr != assetDependencies.end(); ++assetDependencyItr )
  1017. {
  1018. // Fetch dependency asset Id.
  1019. StringTableEntry dependencyAssetId = *assetDependencyItr;
  1020. // Insert depends-on.
  1021. mAssetDependsOn.insertEqual( assetId, dependencyAssetId );
  1022. // Insert is-depended-on.
  1023. mAssetIsDependedOn.insertEqual( dependencyAssetId, assetId );
  1024. }
  1025. }
  1026. // Fetch asset loose files.
  1027. TamlAssetDeclaredVisitor::typeLooseFileVector& assetLooseFiles = assetDeclaredVisitor.getAssetLooseFiles();
  1028. // Clear any existing loose files.
  1029. pAssetDefinition->mAssetLooseFiles.clear();
  1030. // Are there any loose files?
  1031. if ( assetLooseFiles.size() > 0 )
  1032. {
  1033. // Yes, so iterate loose files.
  1034. for( TamlAssetDeclaredVisitor::typeLooseFileVector::iterator assetLooseFileItr = assetLooseFiles.begin(); assetLooseFileItr != assetLooseFiles.end(); ++assetLooseFileItr )
  1035. {
  1036. // Store loose file.
  1037. pAssetDefinition->mAssetLooseFiles.push_back( *assetLooseFileItr );
  1038. }
  1039. }
  1040. // Asset refresh notifications.
  1041. for( typeAssetPtrRefreshHash::iterator refreshNotifyItr = mAssetPtrRefreshNotifications.begin(); refreshNotifyItr != mAssetPtrRefreshNotifications.end(); ++refreshNotifyItr )
  1042. {
  1043. // Fetch pointed asset.
  1044. StringTableEntry pointedAsset = refreshNotifyItr->key->getAssetId();
  1045. // Ignore if the pointed asset is not the asset or a dependency.
  1046. if ( pointedAsset == StringTable->EmptyString() || ( pointedAsset != assetId && !doesAssetDependOn( pointedAsset, assetId ) ) )
  1047. continue;
  1048. // Perform refresh notification callback.
  1049. refreshNotifyItr->value->onAssetRefreshed( refreshNotifyItr->key );
  1050. }
  1051. // Find is-depends-on entry.
  1052. typeAssetIsDependedOnHash::Iterator isDependedOnItr = mAssetIsDependedOn.find( assetId );
  1053. // Is asset depended on?
  1054. if ( isDependedOnItr != mAssetIsDependedOn.end() )
  1055. {
  1056. // Yes, so compiled them.
  1057. Vector<typeAssetId> dependedOn;
  1058. // Iterate all dependencies.
  1059. while( isDependedOnItr != mAssetIsDependedOn.end() && isDependedOnItr->key == assetId )
  1060. {
  1061. dependedOn.push_back( isDependedOnItr->value );
  1062. // Next dependency.
  1063. isDependedOnItr++;
  1064. }
  1065. // Refresh depended-on assets.
  1066. for ( Vector<typeAssetId>::iterator refreshItr = dependedOn.begin(); refreshItr != dependedOn.end(); ++refreshItr)
  1067. {
  1068. // Refresh dependency asset.
  1069. refreshAsset( *refreshItr);
  1070. }
  1071. }
  1072. }
  1073. }
  1074. // Info.
  1075. if ( mEchoInfo )
  1076. {
  1077. Con::printSeparator();
  1078. Con::printf( "Asset Manager: Finished refreshing Asset Id '%s'.", pAssetId );
  1079. }
  1080. return true;
  1081. }
  1082. //-----------------------------------------------------------------------------
  1083. void AssetManager::refreshAllAssets( const bool includeUnloaded )
  1084. {
  1085. // Debug Profiling.
  1086. PROFILE_SCOPE(AssetManager_RefreshAllAssets);
  1087. // Info.
  1088. if ( mEchoInfo )
  1089. {
  1090. Con::printSeparator();
  1091. Con::printf( "Asset Manager: Started refreshing ALL assets." );
  1092. }
  1093. Vector<typeAssetId> assetsToRelease;
  1094. // Are we including unloaded assets?
  1095. if ( includeUnloaded )
  1096. {
  1097. // Yes, so prepare a list of assets to release and load them.
  1098. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1099. {
  1100. // Fetch asset Id.
  1101. typeAssetId assetId = assetItr->key;
  1102. // Skip if asset is loaded.
  1103. if ( assetItr->value->mpAssetBase != NULL )
  1104. continue;
  1105. // Note asset as needing a release.
  1106. assetsToRelease.push_back( assetId );
  1107. // Acquire the asset.
  1108. acquireAsset<AssetBase>( assetId );
  1109. }
  1110. }
  1111. // Refresh the current loaded assets.
  1112. // NOTE: This will result in some assets being refreshed more than once due to asset dependencies.
  1113. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1114. {
  1115. // Skip private assets.
  1116. if ( assetItr->value->mAssetPrivate )
  1117. continue;
  1118. // Refresh asset if it's loaded.
  1119. refreshAsset( assetItr->key );
  1120. }
  1121. // Are we including unloaded assets?
  1122. if ( includeUnloaded )
  1123. {
  1124. // Yes, so release the assets we loaded.
  1125. for( Vector<typeAssetId>::iterator assetItr = assetsToRelease.begin(); assetItr != assetsToRelease.end(); ++assetItr )
  1126. {
  1127. releaseAsset( *assetItr );
  1128. }
  1129. }
  1130. // Info.
  1131. if ( mEchoInfo )
  1132. {
  1133. Con::printSeparator();
  1134. Con::printf( "Asset Manager: Finished refreshing ALL assets." );
  1135. }
  1136. }
  1137. //-----------------------------------------------------------------------------
  1138. void AssetManager::registerAssetPtrRefreshNotify( AssetPtrBase* pAssetPtrBase, AssetPtrCallback* pCallback )
  1139. {
  1140. // Find an existing notification iterator.
  1141. typeAssetPtrRefreshHash::iterator notificationItr = mAssetPtrRefreshNotifications.find( pAssetPtrBase );
  1142. // Do we have one?
  1143. if ( notificationItr != mAssetPtrRefreshNotifications.end() )
  1144. {
  1145. // Yes, so update the callback.
  1146. notificationItr->value = pCallback;
  1147. return;
  1148. }
  1149. // No, so add one.
  1150. mAssetPtrRefreshNotifications.insert( pAssetPtrBase, pCallback );
  1151. }
  1152. //-----------------------------------------------------------------------------
  1153. void AssetManager::unregisterAssetPtrRefreshNotify( AssetPtrBase* pAssetPtrBase )
  1154. {
  1155. mAssetPtrRefreshNotifications.erase( pAssetPtrBase );
  1156. }
  1157. //-----------------------------------------------------------------------------
  1158. bool AssetManager::loadAssetTags( ModuleDefinition* pModuleDefinition )
  1159. {
  1160. // Sanity!
  1161. AssertFatal( pModuleDefinition != NULL, "Cannot load asset tags manifest using a NULL module definition" );
  1162. // Expand manifest location.
  1163. char assetTagsManifestFilePathBuffer[1024];
  1164. Con::expandPath( assetTagsManifestFilePathBuffer, sizeof(assetTagsManifestFilePathBuffer), pModuleDefinition->getAssetTagsManifest() );
  1165. // Do we already have a manifest?
  1166. if ( !mAssetTagsManifest.isNull() )
  1167. {
  1168. // Yes, so warn.
  1169. Con::warnf( "Asset Manager: Cannot load asset tags manifest from module '%s' as one is already loaded.", pModuleDefinition->getSignature() );
  1170. return false;
  1171. }
  1172. // Is the specified file valid?
  1173. if (Torque::FS::IsFile( assetTagsManifestFilePathBuffer ) )
  1174. {
  1175. // Yes, so read asset tags manifest.
  1176. mAssetTagsManifest = mTaml.read<AssetTagsManifest>( assetTagsManifestFilePathBuffer );
  1177. // Did we read the manifest?
  1178. if ( mAssetTagsManifest.isNull() )
  1179. {
  1180. // No, so warn.
  1181. Con::warnf( "Asset Manager: Failed to load asset tags manifest '%s' from module '%s'.", assetTagsManifestFilePathBuffer, pModuleDefinition->getSignature() );
  1182. return false;
  1183. }
  1184. // Set asset tags module definition.
  1185. mAssetTagsModuleDefinition = pModuleDefinition;
  1186. }
  1187. else
  1188. {
  1189. // No, so generate a new asset tags manifest.
  1190. mAssetTagsManifest = new AssetTagsManifest();
  1191. mAssetTagsManifest->registerObject();
  1192. // Set asset tags module definition.
  1193. mAssetTagsModuleDefinition = pModuleDefinition;
  1194. // Save the asset tags.
  1195. saveAssetTags();
  1196. }
  1197. return true;
  1198. }
  1199. //-----------------------------------------------------------------------------
  1200. bool AssetManager::saveAssetTags( void )
  1201. {
  1202. // Do we have an asset tags manifest?
  1203. if ( mAssetTagsManifest.isNull() || mAssetTagsModuleDefinition.isNull() )
  1204. {
  1205. // No, so warn.
  1206. Con::warnf( "Asset Manager: Failed to save asset tags manifest as one is not loaded." );
  1207. return false;
  1208. }
  1209. // Expand manifest location.
  1210. char assetTagsManifestFilePathBuffer[1024];
  1211. Con::expandPath( assetTagsManifestFilePathBuffer, sizeof(assetTagsManifestFilePathBuffer), mAssetTagsModuleDefinition->getAssetTagsManifest() );
  1212. // Save asset tags manifest.
  1213. if ( !mTaml.write( mAssetTagsManifest, assetTagsManifestFilePathBuffer ) )
  1214. {
  1215. // Failed so warn.
  1216. Con::warnf( "Asset Manager: Failed to save asset tags manifest '%s' from module '%s'.", assetTagsManifestFilePathBuffer, mAssetTagsModuleDefinition->getSignature() );
  1217. return false;
  1218. }
  1219. return true;
  1220. }
  1221. //-----------------------------------------------------------------------------
  1222. bool AssetManager::restoreAssetTags( void )
  1223. {
  1224. // Do we already have a manifest?
  1225. if ( mAssetTagsManifest.isNull() )
  1226. {
  1227. // No, so warn.
  1228. Con::warnf( "Asset Manager: Cannot restore asset tags manifest as one is not already loaded." );
  1229. return false;
  1230. }
  1231. // Sanity!
  1232. AssertFatal( mAssetTagsModuleDefinition != NULL, "Cannot restore asset tags manifest as module definition is NULL." );
  1233. // Delete existing asset tags manifest.
  1234. mAssetTagsManifest->deleteObject();
  1235. // Reload asset tags manifest.
  1236. return loadAssetTags( mAssetTagsModuleDefinition );
  1237. }
  1238. //-----------------------------------------------------------------------------
  1239. S32 QSORT_CALLBACK descendingAssetDefinitionLoadCount(const void* a, const void* b)
  1240. {
  1241. // Debug Profiling.
  1242. PROFILE_SCOPE(AssetManager_DescendingAssetDefinitionLoadCount);
  1243. // Fetch asset definitions.
  1244. const AssetDefinition* pAssetDefinitionA = *(AssetDefinition**)a;
  1245. const AssetDefinition* pAssetDefinitionB = *(AssetDefinition**)b;
  1246. // Sort.
  1247. return pAssetDefinitionB->mAssetLoadedCount - pAssetDefinitionA->mAssetLoadedCount;
  1248. }
  1249. //-----------------------------------------------------------------------------
  1250. void AssetManager::dumpDeclaredAssets( void ) const
  1251. {
  1252. Vector<const AssetDefinition*> assetDefinitions;
  1253. // Iterate asset definitions.
  1254. for( typeDeclaredAssetsHash::const_iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1255. {
  1256. assetDefinitions.push_back( assetItr->value );
  1257. }
  1258. // Sort asset definitions.
  1259. dQsort( assetDefinitions.address(), assetDefinitions.size(), sizeof(const AssetDefinition*), descendingAssetDefinitionLoadCount );
  1260. // Info.
  1261. Con::printSeparator();
  1262. Con::printf( "Asset Manager: %d declared asset(s) dump as follows:", mDeclaredAssets.size() );
  1263. Con::printBlankLine();
  1264. // Iterate sorted asset definitions.
  1265. for ( Vector<const AssetDefinition*>::iterator assetItr = assetDefinitions.begin(); assetItr != assetDefinitions.end(); ++assetItr )
  1266. {
  1267. // Fetch asset definition.
  1268. const AssetDefinition* pAssetDefinition = *assetItr;
  1269. // Info.
  1270. Con::printf( "AssetId:'%s', RefCount:%d, LoadCount:%d, UnloadCount:%d, AutoUnload:%d, Loaded:%d, Internal:%d, Private: %d, Type:'%s', Module/Version:'%s'/'%d', File:'%s'",
  1271. pAssetDefinition->mAssetId,
  1272. pAssetDefinition->mpAssetBase == NULL ? 0 : pAssetDefinition->mpAssetBase->getAcquiredReferenceCount(),
  1273. pAssetDefinition->mAssetLoadedCount,
  1274. pAssetDefinition->mAssetUnloadedCount,
  1275. pAssetDefinition->mAssetAutoUnload,
  1276. pAssetDefinition->mpAssetBase != NULL,
  1277. pAssetDefinition->mAssetInternal,
  1278. pAssetDefinition->mAssetPrivate,
  1279. pAssetDefinition->mAssetType,
  1280. pAssetDefinition->mpModuleDefinition->getModuleId(),
  1281. pAssetDefinition->mpModuleDefinition->getVersionId(),
  1282. pAssetDefinition->mAssetBaseFilePath );
  1283. }
  1284. // Info.
  1285. Con::printSeparator();
  1286. Con::printBlankLine();
  1287. }
  1288. //-----------------------------------------------------------------------------
  1289. S32 AssetManager::findAllAssets( AssetQuery* pAssetQuery, const bool ignoreInternal, const bool ignorePrivate )
  1290. {
  1291. // Debug Profiling.
  1292. PROFILE_SCOPE(AssetManager_FindAllAssets);
  1293. // Sanity!
  1294. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1295. // Reset result count.
  1296. S32 resultCount = 0;
  1297. // Iterate declared assets.
  1298. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1299. {
  1300. // Fetch asset definition.
  1301. AssetDefinition* pAssetDefinition = assetItr->value;
  1302. // Skip if internal and we're ignoring them.
  1303. if ( ignoreInternal && pAssetDefinition->mAssetInternal )
  1304. continue;
  1305. // Skip if private and we're ignoring them.
  1306. if ( ignorePrivate && pAssetDefinition->mAssetPrivate )
  1307. continue;
  1308. // Store as result.
  1309. pAssetQuery->mAssetList.push_back( pAssetDefinition->mAssetId );
  1310. // Increase result count.
  1311. resultCount++;
  1312. }
  1313. return resultCount;
  1314. }
  1315. //-----------------------------------------------------------------------------
  1316. S32 AssetManager::findAssetName( AssetQuery* pAssetQuery, const char* pAssetName, const bool partialName )
  1317. {
  1318. // Debug Profiling.
  1319. PROFILE_SCOPE(AssetManager_FindAssetName);
  1320. // Sanity!
  1321. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1322. AssertFatal( pAssetName != NULL, "Cannot use NULL asset name." );
  1323. // Reset asset name.
  1324. StringTableEntry assetName = NULL;
  1325. S32 partialAssetNameLength = 0;
  1326. // Are we doing partial name search?
  1327. if ( partialName )
  1328. {
  1329. // Yes, so fetch length of partial name.
  1330. partialAssetNameLength = dStrlen( pAssetName );
  1331. }
  1332. else
  1333. {
  1334. // No, so fetch asset name.
  1335. assetName = StringTable->insert( pAssetName );
  1336. }
  1337. // Reset result count.
  1338. S32 resultCount = 0;
  1339. // Iterate declared assets.
  1340. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1341. {
  1342. // Fetch asset definition.
  1343. AssetDefinition* pAssetDefinition = assetItr->value;
  1344. // Are we doing partial name search?
  1345. if ( partialName )
  1346. {
  1347. // Yes, so fetch the length of this asset name.
  1348. const S32 currentAssetNameLength = dStrlen( pAssetDefinition->mAssetName );
  1349. // Skip if the query asset name is longer than the current asset name.
  1350. if ( partialAssetNameLength > currentAssetNameLength )
  1351. continue;
  1352. // Skip if this is not the asset we want.
  1353. if ( dStrnicmp( pAssetDefinition->mAssetName, pAssetName, partialAssetNameLength ) != 0 )
  1354. continue;
  1355. }
  1356. else
  1357. {
  1358. // No, so skip if this is not the asset we want.
  1359. if ( assetName != pAssetDefinition->mAssetName )
  1360. continue;
  1361. }
  1362. // Store as result.
  1363. pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
  1364. // Increase result count.
  1365. resultCount++;
  1366. }
  1367. return resultCount;
  1368. }
  1369. //-----------------------------------------------------------------------------
  1370. S32 AssetManager::findAssetCategory( AssetQuery* pAssetQuery, const char* pAssetCategory, const bool assetQueryAsSource )
  1371. {
  1372. // Debug Profiling.
  1373. PROFILE_SCOPE(AssetManager_FindAssetCategory);
  1374. // Sanity!
  1375. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1376. AssertFatal( pAssetCategory != NULL, "Cannot use NULL asset category." );
  1377. // Fetch asset category.
  1378. StringTableEntry assetCategory = StringTable->insert( pAssetCategory );
  1379. // Reset result count.
  1380. S32 resultCount = 0;
  1381. // Use asset-query as the source?
  1382. if ( assetQueryAsSource )
  1383. {
  1384. AssetQuery filteredAssets;
  1385. // Yes, so iterate asset query.
  1386. for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
  1387. {
  1388. // Fetch asset definition.
  1389. AssetDefinition* pAssetDefinition = findAsset( *assetItr );
  1390. // Skip if this is not the asset we want.
  1391. if ( pAssetDefinition == NULL ||
  1392. pAssetDefinition->mAssetCategory != assetCategory )
  1393. continue;
  1394. // Store as result.
  1395. filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
  1396. // Increase result count.
  1397. resultCount++;
  1398. }
  1399. // Set asset query.
  1400. pAssetQuery->set( filteredAssets );
  1401. }
  1402. else
  1403. {
  1404. // No, so iterate declared assets.
  1405. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1406. {
  1407. // Fetch asset definition.
  1408. AssetDefinition* pAssetDefinition = assetItr->value;
  1409. // Skip if this is not the asset we want.
  1410. if ( assetCategory != pAssetDefinition->mAssetCategory )
  1411. continue;
  1412. // Store as result.
  1413. pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
  1414. // Increase result count.
  1415. resultCount++;
  1416. }
  1417. }
  1418. return resultCount;
  1419. }
  1420. S32 AssetManager::findAssetAutoUnload( AssetQuery* pAssetQuery, const bool assetAutoUnload, const bool assetQueryAsSource )
  1421. {
  1422. // Debug Profiling.
  1423. PROFILE_SCOPE(AssetManager_FindAssetAutoUnload);
  1424. // Sanity!
  1425. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1426. // Reset result count.
  1427. S32 resultCount = 0;
  1428. // Use asset-query as the source?
  1429. if ( assetQueryAsSource )
  1430. {
  1431. AssetQuery filteredAssets;
  1432. // Yes, so iterate asset query.
  1433. for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
  1434. {
  1435. // Fetch asset definition.
  1436. AssetDefinition* pAssetDefinition = findAsset( *assetItr );
  1437. // Skip if this is not the asset we want.
  1438. if ( pAssetDefinition == NULL ||
  1439. pAssetDefinition->mAssetAutoUnload != assetAutoUnload )
  1440. continue;
  1441. // Store as result.
  1442. filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
  1443. // Increase result count.
  1444. resultCount++;
  1445. }
  1446. // Set asset query.
  1447. pAssetQuery->set( filteredAssets );
  1448. }
  1449. else
  1450. {
  1451. // No, so iterate declared assets.
  1452. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1453. {
  1454. // Fetch asset definition.
  1455. AssetDefinition* pAssetDefinition = assetItr->value;
  1456. // Skip if this is not the asset we want.
  1457. if ( assetAutoUnload != pAssetDefinition->mAssetAutoUnload )
  1458. continue;
  1459. // Store as result.
  1460. pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
  1461. // Increase result count.
  1462. resultCount++;
  1463. }
  1464. }
  1465. return resultCount;
  1466. }
  1467. //-----------------------------------------------------------------------------
  1468. S32 AssetManager::findAssetInternal( AssetQuery* pAssetQuery, const bool assetInternal, const bool assetQueryAsSource )
  1469. {
  1470. // Debug Profiling.
  1471. PROFILE_SCOPE(AssetManager_FindAssetInternal);
  1472. // Sanity!
  1473. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1474. // Reset result count.
  1475. S32 resultCount = 0;
  1476. // Use asset-query as the source?
  1477. if ( assetQueryAsSource )
  1478. {
  1479. AssetQuery filteredAssets;
  1480. // Yes, so iterate asset query.
  1481. for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
  1482. {
  1483. // Fetch asset definition.
  1484. AssetDefinition* pAssetDefinition = findAsset( *assetItr );
  1485. // Skip if this is not the asset we want.
  1486. if ( pAssetDefinition == NULL ||
  1487. pAssetDefinition->mAssetInternal != assetInternal )
  1488. continue;
  1489. // Store as result.
  1490. filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
  1491. // Increase result count.
  1492. resultCount++;
  1493. }
  1494. // Set asset query.
  1495. pAssetQuery->set( filteredAssets );
  1496. }
  1497. else
  1498. {
  1499. // No, so iterate declared assets.
  1500. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1501. {
  1502. // Fetch asset definition.
  1503. AssetDefinition* pAssetDefinition = assetItr->value;
  1504. // Skip if this is not the asset we want.
  1505. if ( assetInternal != pAssetDefinition->mAssetInternal )
  1506. continue;
  1507. // Store as result.
  1508. pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
  1509. // Increase result count.
  1510. resultCount++;
  1511. }
  1512. }
  1513. return resultCount;
  1514. }
  1515. //-----------------------------------------------------------------------------
  1516. S32 AssetManager::findAssetPrivate( AssetQuery* pAssetQuery, const bool assetPrivate, const bool assetQueryAsSource )
  1517. {
  1518. // Debug Profiling.
  1519. PROFILE_SCOPE(AssetManager_FindAssetPrivate);
  1520. // Sanity!
  1521. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1522. // Reset result count.
  1523. S32 resultCount = 0;
  1524. // Use asset-query as the source?
  1525. if ( assetQueryAsSource )
  1526. {
  1527. AssetQuery filteredAssets;
  1528. // Yes, so iterate asset query.
  1529. for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
  1530. {
  1531. // Fetch asset definition.
  1532. AssetDefinition* pAssetDefinition = findAsset( *assetItr );
  1533. // Skip if this is not the asset we want.
  1534. if ( pAssetDefinition == NULL ||
  1535. pAssetDefinition->mAssetPrivate != assetPrivate )
  1536. continue;
  1537. // Store as result.
  1538. filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
  1539. // Increase result count.
  1540. resultCount++;
  1541. }
  1542. // Set asset query.
  1543. pAssetQuery->set( filteredAssets );
  1544. }
  1545. else
  1546. {
  1547. // No, so iterate declared assets.
  1548. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1549. {
  1550. // Fetch asset definition.
  1551. AssetDefinition* pAssetDefinition = assetItr->value;
  1552. // Skip if this is not the asset we want.
  1553. if ( assetPrivate != pAssetDefinition->mAssetPrivate )
  1554. continue;
  1555. // Store as result.
  1556. pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
  1557. // Increase result count.
  1558. resultCount++;
  1559. }
  1560. }
  1561. return resultCount;
  1562. }
  1563. //-----------------------------------------------------------------------------
  1564. S32 AssetManager::findAssetType( AssetQuery* pAssetQuery, const char* pAssetType, const bool assetQueryAsSource )
  1565. {
  1566. // Debug Profiling.
  1567. PROFILE_SCOPE(AssetManager_FindAssetType);
  1568. // Sanity!
  1569. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1570. AssertFatal( pAssetType != NULL, "Cannot use NULL asset type." );
  1571. // Fetch asset type.
  1572. StringTableEntry assetType = StringTable->insert( pAssetType );
  1573. // Reset result count.
  1574. S32 resultCount = 0;
  1575. // Use asset-query as the source?
  1576. if ( assetQueryAsSource )
  1577. {
  1578. AssetQuery filteredAssets;
  1579. // Yes, so iterate asset query.
  1580. for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
  1581. {
  1582. // Fetch asset definition.
  1583. AssetDefinition* pAssetDefinition = findAsset( *assetItr );
  1584. // Skip if this is not the asset we want.
  1585. if ( pAssetDefinition == NULL ||
  1586. pAssetDefinition->mAssetType != assetType )
  1587. continue;
  1588. // Store as result.
  1589. filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
  1590. // Increase result count.
  1591. resultCount++;
  1592. }
  1593. // Set asset query.
  1594. pAssetQuery->set( filteredAssets );
  1595. }
  1596. else
  1597. {
  1598. // No, so iterate declared assets.
  1599. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1600. {
  1601. // Fetch asset definition.
  1602. AssetDefinition* pAssetDefinition = assetItr->value;
  1603. // Skip if this is not the asset we want.
  1604. if ( assetType != pAssetDefinition->mAssetType )
  1605. continue;
  1606. // Store as result.
  1607. pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
  1608. // Increase result count.
  1609. resultCount++;
  1610. }
  1611. }
  1612. return resultCount;
  1613. }
  1614. //-----------------------------------------------------------------------------
  1615. S32 AssetManager::findAssetDependsOn( AssetQuery* pAssetQuery, const char* pAssetId )
  1616. {
  1617. // Debug Profiling.
  1618. PROFILE_SCOPE(AssetManager_FindAssetDependsOn);
  1619. // Sanity!
  1620. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1621. AssertFatal( pAssetId != NULL, "Cannot use NULL asset Id." );
  1622. // Fetch asset Id.
  1623. StringTableEntry assetId = StringTable->insert( pAssetId );
  1624. // Reset result count.
  1625. S32 resultCount = 0;
  1626. // Find depends-on entry.
  1627. typeAssetDependsOnHash::Iterator dependsOnItr = mAssetDependsOn.find( assetId );
  1628. // Iterate all dependencies.
  1629. while( dependsOnItr != mAssetDependsOn.end() && dependsOnItr->key == assetId )
  1630. {
  1631. // Store as result.
  1632. pAssetQuery->mAssetList.push_back(dependsOnItr->value);
  1633. // Next dependency.
  1634. dependsOnItr++;
  1635. // Increase result count.
  1636. resultCount++;
  1637. }
  1638. return resultCount;
  1639. }
  1640. //-----------------------------------------------------------------------------
  1641. S32 AssetManager::findAssetIsDependedOn( AssetQuery* pAssetQuery, const char* pAssetId )
  1642. {
  1643. // Debug Profiling.
  1644. PROFILE_SCOPE(AssetManager_FindAssetIsDependedOn);
  1645. // Sanity!
  1646. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1647. AssertFatal( pAssetId != NULL, "Cannot use NULL asset Id." );
  1648. // Fetch asset Id.
  1649. StringTableEntry assetId = StringTable->insert( pAssetId );
  1650. // Reset result count.
  1651. S32 resultCount = 0;
  1652. // Find depended-on entry.
  1653. typeAssetIsDependedOnHash::Iterator dependedOnItr = mAssetIsDependedOn.find( assetId );
  1654. // Iterate all dependencies.
  1655. while( dependedOnItr != mAssetIsDependedOn.end() && dependedOnItr->key == assetId )
  1656. {
  1657. // Store as result.
  1658. pAssetQuery->mAssetList.push_back(dependedOnItr->value);
  1659. // Next dependency.
  1660. dependedOnItr++;
  1661. // Increase result count.
  1662. resultCount++;
  1663. }
  1664. return resultCount;
  1665. }
  1666. //-----------------------------------------------------------------------------
  1667. S32 AssetManager::findInvalidAssetReferences( AssetQuery* pAssetQuery )
  1668. {
  1669. // Debug Profiling.
  1670. PROFILE_SCOPE(AssetManager_FindInvalidAssetReferences);
  1671. // Sanity!
  1672. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1673. // Reset result count.
  1674. S32 resultCount = 0;
  1675. // Iterate referenced assets.
  1676. for( typeReferencedAssetsHash::Iterator assetItr = mReferencedAssets.begin(); assetItr != mReferencedAssets.end(); ++assetItr )
  1677. {
  1678. // Find asset definition.
  1679. AssetDefinition* pAssetDefinition = findAsset( assetItr->key );
  1680. // Skip if the asset definition was found.
  1681. if ( pAssetDefinition != NULL )
  1682. continue;
  1683. // Store as result.
  1684. pAssetQuery->mAssetList.push_back(assetItr->key);
  1685. // Increase result count.
  1686. resultCount++;
  1687. }
  1688. return resultCount;
  1689. }
  1690. //-----------------------------------------------------------------------------
  1691. S32 AssetManager::findTaggedAssets( AssetQuery* pAssetQuery, const char* pAssetTagNames, const bool assetQueryAsSource )
  1692. {
  1693. // Debug Profiling.
  1694. PROFILE_SCOPE(AssetManager_FindTaggedAssets);
  1695. // Sanity!
  1696. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1697. AssertFatal( pAssetTagNames != NULL, "Cannot use NULL asset tag name(s)." );
  1698. // Do we have an asset tag manifest?
  1699. if ( mAssetTagsManifest.isNull() )
  1700. {
  1701. // No, so warn.
  1702. Con::warnf( "Asset Manager: Cannot find tagged assets as no asset tag manifest is present." );
  1703. return 0;
  1704. }
  1705. // Reset result count.
  1706. S32 resultCount = 0;
  1707. const char* pTagSeparators = " ,\t\n";
  1708. // Fetch tag count.
  1709. U32 assetTagCount = StringUnit::getUnitCount( pAssetTagNames, pTagSeparators );
  1710. // Fetch asset tags.
  1711. Vector<AssetTagsManifest::AssetTag*> assetTags;
  1712. for( U32 tagIndex = 0; tagIndex < assetTagCount; ++tagIndex )
  1713. {
  1714. // Fetch asset tag name.
  1715. const char* pTagName = StringUnit::getUnit( pAssetTagNames, tagIndex, pTagSeparators );
  1716. // Fetch asset tag.
  1717. AssetTagsManifest::AssetTag* pAssetTag = mAssetTagsManifest->findAssetTag( pTagName );
  1718. // Did we find the asset tag?
  1719. if ( pAssetTag == NULL )
  1720. {
  1721. // No, so warn.
  1722. Con::warnf( "AssetTagsManifest: Asset Manager: Cannot find tagged assets of '%s' as it does not exist. Ignoring tag.", pTagName );
  1723. continue;
  1724. }
  1725. assetTags.push_back( pAssetTag );
  1726. }
  1727. // Fetch found asset tag count.
  1728. assetTagCount = assetTags.size();
  1729. // Did we find any tags?
  1730. if ( assetTagCount == 0 )
  1731. {
  1732. // No, so warn.
  1733. Con::warnf( "AssetTagsManifest: Asset Manager: No specified tagged assets found in '%s'.", pAssetTagNames );
  1734. return 0;
  1735. }
  1736. // Use asset-query as the source?
  1737. if ( assetQueryAsSource )
  1738. {
  1739. AssetQuery filteredAssets;
  1740. // Yes, so iterate asset query.
  1741. for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
  1742. {
  1743. // Fetch asset Id.
  1744. StringTableEntry assetId = *assetItr;
  1745. // Skip if asset is not valid.
  1746. if ( !isDeclaredAsset( assetId ) )
  1747. continue;
  1748. // Reset matched flag.
  1749. bool assetTagMatched = false;
  1750. // Iterate asset tags.
  1751. for ( Vector<AssetTagsManifest::AssetTag*>::iterator assetTagItr = assetTags.begin(); assetTagItr != assetTags.end(); ++assetTagItr )
  1752. {
  1753. // Fetch asset tag.
  1754. AssetTagsManifest::AssetTag* pAssetTag = *assetTagItr;
  1755. // Skip if asset is not tagged.
  1756. if ( !pAssetTag->containsAsset( assetId ) )
  1757. continue;
  1758. // Flag as matched.
  1759. assetTagMatched = true;
  1760. break;
  1761. }
  1762. // Did we find a match?
  1763. if ( assetTagMatched )
  1764. {
  1765. // Yes, so is asset already present?
  1766. if ( !filteredAssets.containsAsset( assetId ) )
  1767. {
  1768. // No, so store as result.
  1769. filteredAssets.mAssetList.push_back(assetId);
  1770. // Increase result count.
  1771. resultCount++;
  1772. }
  1773. }
  1774. }
  1775. // Set asset query.
  1776. pAssetQuery->set( filteredAssets );
  1777. }
  1778. else
  1779. {
  1780. // Iterate asset tags.
  1781. for ( Vector<AssetTagsManifest::AssetTag*>::iterator assetTagItr = assetTags.begin(); assetTagItr != assetTags.end(); ++assetTagItr )
  1782. {
  1783. // Fetch asset tag.
  1784. AssetTagsManifest::AssetTag* pAssetTag = *assetTagItr;
  1785. // Iterate tagged assets.
  1786. for ( Vector<typeAssetId>::iterator assetItr = pAssetTag->mAssets.begin(); assetItr != pAssetTag->mAssets.end(); ++assetItr )
  1787. {
  1788. // Fetch asset Id.
  1789. StringTableEntry assetId = *assetItr;
  1790. // Skip if asset Id is already present.
  1791. if ( pAssetQuery->containsAsset( assetId ) )
  1792. continue;
  1793. // Store as result.
  1794. pAssetQuery->mAssetList.push_back(assetId);
  1795. // Increase result count.
  1796. resultCount++;
  1797. }
  1798. }
  1799. }
  1800. return resultCount;
  1801. }
  1802. //-----------------------------------------------------------------------------
  1803. S32 AssetManager::findAssetLooseFile( AssetQuery* pAssetQuery, const char* pLooseFile, const bool assetQueryAsSource )
  1804. {
  1805. // Debug Profiling.
  1806. PROFILE_SCOPE(AssetManager_FindAssetLooseFile);
  1807. // Sanity!
  1808. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1809. AssertFatal( pLooseFile != NULL, "Cannot use NULL loose file." );
  1810. // Make game relative path for loose file.
  1811. StringTableEntry looseFile = Platform::makeRelativePathName(pLooseFile, NULL);;
  1812. // Reset result count.
  1813. S32 resultCount = 0;
  1814. // Use asset-query as the source?
  1815. if ( assetQueryAsSource )
  1816. {
  1817. AssetQuery filteredAssets;
  1818. // Yes, so iterate asset query.
  1819. for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
  1820. {
  1821. // Fetch asset definition.
  1822. AssetDefinition* pAssetDefinition = findAsset( *assetItr );
  1823. // Fetch loose files.
  1824. Vector<StringTableEntry>& assetLooseFiles = pAssetDefinition->mAssetLooseFiles;
  1825. // Skip if this asset has no loose files.
  1826. if ( assetLooseFiles.size() == 0 )
  1827. continue;
  1828. // Search the assets loose files.
  1829. for( Vector<StringTableEntry>::iterator looseFileItr = assetLooseFiles.begin(); looseFileItr != assetLooseFiles.end(); ++looseFileItr )
  1830. {
  1831. // Is this the loose file we are searching for?
  1832. if ( *looseFileItr != looseFile )
  1833. continue;
  1834. // Store as result.
  1835. filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
  1836. // Increase result count.
  1837. resultCount++;
  1838. break;
  1839. }
  1840. }
  1841. // Set asset query.
  1842. pAssetQuery->set( filteredAssets );
  1843. }
  1844. else
  1845. {
  1846. // No, so iterate declared assets.
  1847. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1848. {
  1849. // Fetch asset definition.
  1850. AssetDefinition* pAssetDefinition = assetItr->value;
  1851. // Fetch loose files.
  1852. Vector<StringTableEntry>& assetLooseFiles = pAssetDefinition->mAssetLooseFiles;
  1853. // Skip if this asset has no loose files.
  1854. if ( assetLooseFiles.size() == 0 )
  1855. continue;
  1856. // Search the assets loose files.
  1857. for( Vector<StringTableEntry>::iterator looseFileItr = assetLooseFiles.begin(); looseFileItr != assetLooseFiles.end(); ++looseFileItr )
  1858. {
  1859. // Is this the loose file we are searching for?
  1860. if ( *looseFileItr != looseFile )
  1861. continue;
  1862. // Store as result.
  1863. pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
  1864. // Increase result count.
  1865. resultCount++;
  1866. break;
  1867. }
  1868. }
  1869. }
  1870. return resultCount;
  1871. }
  1872. //-----------------------------------------------------------------------------
  1873. AssetManager::typeAssetDependsOnHash* AssetManager::getDependedOnAssets()
  1874. {
  1875. // Find any asset dependencies.
  1876. return &mAssetDependsOn;
  1877. }
  1878. //-----------------------------------------------------------------------------
  1879. bool AssetManager::scanDeclaredAssets( const char* pPath, const char* pExtension, const bool recurse, ModuleDefinition* pModuleDefinition )
  1880. {
  1881. // Debug Profiling.
  1882. PROFILE_SCOPE(AssetManager_ScanDeclaredAssets);
  1883. // Sanity!
  1884. AssertFatal( pPath != NULL, "Cannot scan declared assets with NULL path." );
  1885. AssertFatal( pExtension != NULL, "Cannot scan declared assets with NULL extension." );
  1886. // Expand path location.
  1887. String relativePath = Platform::makeRelativePathName(pPath, NULL);
  1888. // Strip any trailing slash off the path.
  1889. if (relativePath.endsWith("/"))
  1890. relativePath = relativePath.substr(0, relativePath.length() - 1);
  1891. Torque::Path scanPath = Torque::FS::GetCwd();
  1892. scanPath.setPath(relativePath);
  1893. // Find files.
  1894. Vector<String> files;
  1895. S32 numAssets = Torque::FS::FindByPattern(scanPath, pExtension, recurse, files, true);
  1896. if (numAssets <= 0)
  1897. {
  1898. // Failed so warn. or don't... Common error when scanning modules with no assets
  1899. //Con::warnf( "Asset Manager: No declared assets found in directory '%s'.", relativePath.c_str());
  1900. return false;
  1901. }
  1902. // Is the asset file-path located within the specified module?
  1903. if ( !Con::isBasePath(relativePath.c_str(), pModuleDefinition->getModulePath()) )
  1904. {
  1905. // No, so warn.
  1906. Con::warnf( "Asset Manager: Could not add declared asset file '%s' as file does not exist with module path '%s'",
  1907. pPath,
  1908. pModuleDefinition->getModulePath() );
  1909. return false;
  1910. }
  1911. // Info.
  1912. if ( mEchoInfo )
  1913. {
  1914. Con::printSeparator();
  1915. Con::printf( "Asset Manager: Scanning for declared assets in path '%s' for files with extension '%s'...", relativePath.c_str(), pExtension );
  1916. }
  1917. // Fetch module assets.
  1918. ModuleDefinition::typeModuleAssetsVector& moduleAssets = pModuleDefinition->getModuleAssets();
  1919. TamlAssetDeclaredVisitor assetDeclaredVisitor;
  1920. // Iterate files.
  1921. for (S32 i = 0; i < numAssets; ++i)
  1922. {
  1923. Torque::Path assetPath = files[i];
  1924. // Clear declared assets.
  1925. assetDeclaredVisitor.clear();
  1926. // Format full file-path.
  1927. char assetFileBuffer[1024];
  1928. dSprintf( assetFileBuffer, sizeof(assetFileBuffer), "%s/%s", assetPath.getPath().c_str(), assetPath.getFullFileName().c_str());
  1929. // Parse the filename.
  1930. if ( !mTaml.parse( assetFileBuffer, assetDeclaredVisitor ) )
  1931. {
  1932. // Warn.
  1933. Con::warnf( "Asset Manager: Failed to parse file containing asset declaration: '%s'.", assetFileBuffer );
  1934. continue;
  1935. }
  1936. // Fetch asset definition.
  1937. AssetDefinition& foundAssetDefinition = assetDeclaredVisitor.getAssetDefinition();
  1938. // Did we get an asset name?
  1939. if ( foundAssetDefinition.mAssetName == StringTable->EmptyString() )
  1940. {
  1941. // No, so warn.
  1942. Con::warnf( "Asset Manager: Parsed file '%s' but did not encounter an asset.", assetFileBuffer );
  1943. continue;
  1944. }
  1945. // Set module definition.
  1946. foundAssetDefinition.mpModuleDefinition = pModuleDefinition;
  1947. // Format asset Id.
  1948. char assetIdBuffer[1024];
  1949. dSprintf(assetIdBuffer, sizeof(assetIdBuffer), "%s%s%s",
  1950. pModuleDefinition->getModuleId(),
  1951. ASSET_SCOPE_TOKEN,
  1952. foundAssetDefinition.mAssetName );
  1953. // Set asset Id.
  1954. foundAssetDefinition.mAssetId = StringTable->insert( assetIdBuffer );
  1955. // Does this asset already exist?
  1956. if ( mDeclaredAssets.contains( foundAssetDefinition.mAssetId ) )
  1957. {
  1958. // Yes, so warn.
  1959. Con::warnf( "Asset Manager: Encountered asset Id '%s' in asset file '%s' but it conflicts with existing asset Id in asset file '%s'.",
  1960. foundAssetDefinition.mAssetId,
  1961. foundAssetDefinition.mAssetBaseFilePath,
  1962. mDeclaredAssets.find( foundAssetDefinition.mAssetId )->value->mAssetBaseFilePath );
  1963. continue;
  1964. }
  1965. // Create new asset definition.
  1966. AssetDefinition* pAssetDefinition = new AssetDefinition( foundAssetDefinition );
  1967. // Store in declared assets.
  1968. mDeclaredAssets.insert( pAssetDefinition->mAssetId, pAssetDefinition );
  1969. // Store in module assets.
  1970. moduleAssets.push_back( pAssetDefinition );
  1971. // Info.
  1972. if ( mEchoInfo )
  1973. {
  1974. Con::printSeparator();
  1975. Con::printf( "Asset Manager: Adding Asset Id '%s' of type '%s' in asset file '%s'.",
  1976. pAssetDefinition->mAssetId,
  1977. pAssetDefinition->mAssetType,
  1978. pAssetDefinition->mAssetBaseFilePath );
  1979. }
  1980. // Fetch asset Id.
  1981. StringTableEntry assetId = pAssetDefinition->mAssetId;
  1982. // Fetch asset dependencies.
  1983. TamlAssetDeclaredVisitor::typeAssetIdVector& assetDependencies = assetDeclaredVisitor.getAssetDependencies();
  1984. // Are there any asset dependencies?
  1985. if ( assetDependencies.size() > 0 )
  1986. {
  1987. // Yes, so iterate dependencies.
  1988. for( TamlAssetDeclaredVisitor::typeAssetIdVector::iterator assetDependencyItr = assetDependencies.begin(); assetDependencyItr != assetDependencies.end(); ++assetDependencyItr )
  1989. {
  1990. // Fetch asset Ids.
  1991. StringTableEntry dependencyAssetId = *assetDependencyItr;
  1992. // Insert depends-on.
  1993. mAssetDependsOn.insertEqual( assetId, dependencyAssetId );
  1994. // Insert is-depended-on.
  1995. mAssetIsDependedOn.insertEqual( dependencyAssetId, assetId );
  1996. // Info.
  1997. if ( mEchoInfo )
  1998. {
  1999. Con::printf( "Asset Manager: Asset Id '%s' has dependency of Asset Id '%s'", assetId, dependencyAssetId );
  2000. }
  2001. }
  2002. }
  2003. // Fetch asset loose files.
  2004. TamlAssetDeclaredVisitor::typeLooseFileVector& assetLooseFiles = assetDeclaredVisitor.getAssetLooseFiles();
  2005. // Are there any loose files?
  2006. if ( assetLooseFiles.size() > 0 )
  2007. {
  2008. // Yes, so iterate loose files.
  2009. for( TamlAssetDeclaredVisitor::typeLooseFileVector::iterator assetLooseFileItr = assetLooseFiles.begin(); assetLooseFileItr != assetLooseFiles.end(); ++assetLooseFileItr )
  2010. {
  2011. // Fetch loose file.
  2012. StringTableEntry looseFile = *assetLooseFileItr;
  2013. // Info.
  2014. if ( mEchoInfo )
  2015. {
  2016. Con::printf( "Asset Manager: Asset Id '%s' has loose file '%s'.", assetId, looseFile );
  2017. }
  2018. // Store loose file.
  2019. pAssetDefinition->mAssetLooseFiles.push_back( looseFile );
  2020. }
  2021. }
  2022. }
  2023. // Info.
  2024. if ( mEchoInfo )
  2025. {
  2026. Con::printSeparator();
  2027. Con::printf( "Asset Manager: ... Finished scanning for declared assets in path '%s' for files with extension '%s'.", relativePath.c_str(), pExtension );
  2028. Con::printSeparator();
  2029. Con::printBlankLine();
  2030. }
  2031. return true;
  2032. }
  2033. //-----------------------------------------------------------------------------
  2034. bool AssetManager::scanReferencedAssets( const char* pPath, const char* pExtension, const bool recurse )
  2035. {
  2036. // Debug Profiling.
  2037. PROFILE_SCOPE(AssetManager_ScanReferencedAssets);
  2038. // Sanity!
  2039. AssertFatal( pPath != NULL, "Cannot scan referenced assets with NULL path." );
  2040. AssertFatal( pExtension != NULL, "Cannot scan referenced assets with NULL extension." );
  2041. // Expand path location.
  2042. String relativePath = Platform::makeRelativePathName(pPath, NULL);
  2043. String pattern = "*.";
  2044. pattern += pExtension;
  2045. Torque::Path scanPath = Torque::FS::GetCwd();
  2046. scanPath.setPath(relativePath);
  2047. // Find files.
  2048. Vector<String> files;
  2049. S32 numAssets = Torque::FS::FindByPattern(scanPath, pattern, recurse, files, true);
  2050. if (numAssets <= 0)
  2051. {
  2052. // Failed so warn.
  2053. Con::warnf( "Asset Manager: Failed to scan referenced assets in directory '%s'.", pPath );
  2054. return false;
  2055. }
  2056. // Info.
  2057. if ( mEchoInfo )
  2058. {
  2059. Con::printSeparator();
  2060. Con::printf( "Asset Manager: Scanning for referenced assets in path '%s' for files with extension '%s'...", pPath, pExtension );
  2061. }
  2062. TamlAssetReferencedVisitor assetReferencedVisitor;
  2063. // Iterate files.
  2064. for (S32 i = 0; i < numAssets; ++i)
  2065. {
  2066. Torque::Path assetPath = files[i];
  2067. // Clear referenced assets.
  2068. assetReferencedVisitor.clear();
  2069. // Format full file-path.
  2070. char assetFileBuffer[1024];
  2071. dSprintf( assetFileBuffer, sizeof(assetFileBuffer), "%s/%s", assetPath.getPath().c_str(), assetPath.getFullFileName().c_str());
  2072. // Format reference file-path.
  2073. typeReferenceFilePath referenceFilePath = StringTable->insert( assetFileBuffer );
  2074. // Parse the filename.
  2075. if ( !mTaml.parse( referenceFilePath, assetReferencedVisitor ) )
  2076. {
  2077. // Warn.
  2078. Con::warnf( "Asset Manager: Failed to parse file containing asset references: '%s'.", referenceFilePath );
  2079. continue;
  2080. }
  2081. // Fetch usage map.
  2082. const TamlAssetReferencedVisitor::typeAssetReferencedHash& assetReferencedMap = assetReferencedVisitor.getAssetReferencedMap();
  2083. // Do we have any asset references?
  2084. if ( assetReferencedMap.size() > 0 )
  2085. {
  2086. // Info.
  2087. if ( mEchoInfo )
  2088. {
  2089. Con::printSeparator();
  2090. }
  2091. // Iterate usage.
  2092. for( TamlAssetReferencedVisitor::typeAssetReferencedHash::const_iterator usageItr = assetReferencedMap.begin(); usageItr != assetReferencedMap.end(); ++usageItr )
  2093. {
  2094. // Fetch asset name.
  2095. typeAssetId assetId = usageItr->key;
  2096. // Info.
  2097. if ( mEchoInfo )
  2098. {
  2099. Con::printf( "Asset Manager: Found referenced Asset Id '%s' in file '%s'.", assetId, referenceFilePath );
  2100. }
  2101. // Add referenced asset.
  2102. addReferencedAsset( assetId, referenceFilePath );
  2103. }
  2104. }
  2105. }
  2106. // Info.
  2107. if ( mEchoInfo )
  2108. {
  2109. Con::printf( "Asset Manager: ... Finished scanning for referenced assets in path '%s' for files with extension '%s'.", relativePath.c_str(), pExtension );
  2110. Con::printSeparator();
  2111. Con::printBlankLine();
  2112. }
  2113. return true;
  2114. }
  2115. //-----------------------------------------------------------------------------
  2116. AssetDefinition* AssetManager::findAsset( const char* pAssetId )
  2117. {
  2118. // Debug Profiling.
  2119. PROFILE_SCOPE(AssetManager_FindAsset);
  2120. // Sanity!
  2121. AssertFatal( pAssetId != NULL, "Cannot find NULL asset Id." );
  2122. // Fetch asset Id.
  2123. StringTableEntry assetId = StringTable->insert( pAssetId );
  2124. // Find declared asset.
  2125. typeDeclaredAssetsHash::iterator declaredAssetItr = mDeclaredAssets.find( assetId );
  2126. // Find if we didn't find a declared asset Id.
  2127. if ( declaredAssetItr == mDeclaredAssets.end() )
  2128. return NULL;
  2129. return declaredAssetItr->value;
  2130. }
  2131. //-----------------------------------------------------------------------------
  2132. void AssetManager::addReferencedAsset( StringTableEntry assetId, StringTableEntry referenceFilePath )
  2133. {
  2134. // Debug Profiling.
  2135. PROFILE_SCOPE(AssetManager_AddReferencedAsset);
  2136. // Sanity!
  2137. AssertFatal( assetId != NULL, "Cannot add referenced asset with NULL asset Id." );
  2138. AssertFatal( referenceFilePath != NULL, "Cannot add referenced asset with NULL reference file-path." );
  2139. // Find referenced asset.
  2140. typeReferencedAssetsHash::Iterator referencedAssetItr = mReferencedAssets.find( assetId );
  2141. // Did we find the asset?
  2142. if ( referencedAssetItr == mReferencedAssets.end() )
  2143. {
  2144. // No, so add asset Id.
  2145. mReferencedAssets.insertEqual( assetId, referenceFilePath );
  2146. }
  2147. else
  2148. {
  2149. // Yes, so add asset Id with a unique file.
  2150. while( true )
  2151. {
  2152. // Finish if this file is already present.
  2153. if ( referencedAssetItr->value == referenceFilePath )
  2154. return;
  2155. // Move to next asset Id.
  2156. referencedAssetItr++;
  2157. // Is this the end of referenced assets or a different asset Id?
  2158. if ( referencedAssetItr == mReferencedAssets.end() ||
  2159. referencedAssetItr->key != assetId )
  2160. {
  2161. // Yes, so add asset reference.
  2162. mReferencedAssets.insertEqual( assetId, referenceFilePath );
  2163. return;
  2164. }
  2165. };
  2166. }
  2167. }
  2168. //-----------------------------------------------------------------------------
  2169. void AssetManager::renameAssetReferences( StringTableEntry assetIdFrom, StringTableEntry assetIdTo )
  2170. {
  2171. // Debug Profiling.
  2172. PROFILE_SCOPE(AssetManager_RenameAssetReferences);
  2173. // Sanity!
  2174. AssertFatal( assetIdFrom != NULL, "Cannot rename asset references using NULL asset Id from." );
  2175. AssertFatal( assetIdTo != NULL, "Cannot rename asset references using NULL asset Id to." );
  2176. // Finish if the asset is not referenced.
  2177. if ( !mReferencedAssets.count( assetIdFrom ) )
  2178. return;
  2179. // Setup referenced update visitor.
  2180. TamlAssetReferencedUpdateVisitor assetReferencedUpdateVisitor;
  2181. assetReferencedUpdateVisitor.setAssetIdFrom( assetIdFrom );
  2182. assetReferencedUpdateVisitor.setAssetIdTo( assetIdTo );
  2183. // Find first referenced asset Id.
  2184. typeReferencedAssetsHash::Iterator referencedAssetItr = mReferencedAssets.find( assetIdFrom );
  2185. // Iterate references.
  2186. while( true )
  2187. {
  2188. // Finish if end of references.
  2189. if ( referencedAssetItr == mReferencedAssets.end() || referencedAssetItr->key != assetIdFrom )
  2190. return;
  2191. // Info.
  2192. if ( mEchoInfo )
  2193. {
  2194. Con::printf( "Asset Manager: Renaming declared Asset Id '%s' to Asset Id '%s'. Updating referenced file '%s'",
  2195. assetIdFrom,
  2196. assetIdTo,
  2197. referencedAssetItr->value );
  2198. }
  2199. // Update asset file declaration.
  2200. if ( !mTaml.parse( referencedAssetItr->value, assetReferencedUpdateVisitor ) )
  2201. {
  2202. // No, so warn.
  2203. Con::warnf("Asset Manager: Cannot rename referenced asset Id '%s' to asset Id '%s' as the referenced asset file could not be parsed: %s",
  2204. assetIdFrom, assetIdTo, referencedAssetItr->value );
  2205. }
  2206. // Move to next reference.
  2207. referencedAssetItr++;
  2208. }
  2209. }
  2210. //-----------------------------------------------------------------------------
  2211. void AssetManager::removeAssetReferences( StringTableEntry assetId )
  2212. {
  2213. // Debug Profiling.
  2214. PROFILE_SCOPE(AssetManager_RemoveAssetReferences);
  2215. // Sanity!
  2216. AssertFatal( assetId != NULL, "Cannot rename asset references using NULL asset Id." );
  2217. // Finish if the asset is not referenced.
  2218. if ( !mReferencedAssets.count( assetId ) )
  2219. return;
  2220. // Setup referenced update visitor.
  2221. TamlAssetReferencedUpdateVisitor assetReferencedUpdateVisitor;
  2222. assetReferencedUpdateVisitor.setAssetIdFrom( assetId );
  2223. assetReferencedUpdateVisitor.setAssetIdTo( StringTable->EmptyString() );
  2224. // Find first referenced asset Id.
  2225. typeReferencedAssetsHash::Iterator referencedAssetItr = mReferencedAssets.find(assetId);
  2226. // Iterate references.
  2227. while( true )
  2228. {
  2229. // Finish if end of references.
  2230. if ( referencedAssetItr == mReferencedAssets.end() || referencedAssetItr->key != assetId )
  2231. break;
  2232. // Info.
  2233. if ( mEchoInfo )
  2234. {
  2235. Con::printf( "Asset Manager: Removing Asset Id '%s' references from file '%s'",
  2236. assetId,
  2237. referencedAssetItr->value );
  2238. }
  2239. // Update asset file declaration.
  2240. if ( !mTaml.parse( referencedAssetItr->value, assetReferencedUpdateVisitor ) )
  2241. {
  2242. // No, so warn.
  2243. Con::warnf("Asset Manager: Cannot remove referenced asset Id '%s' as the referenced asset file could not be parsed: %s",
  2244. assetId,
  2245. referencedAssetItr->value );
  2246. }
  2247. // Move to next reference.
  2248. referencedAssetItr++;
  2249. }
  2250. // Remove asset references.
  2251. mReferencedAssets.erase( assetId );
  2252. }
  2253. //-----------------------------------------------------------------------------
  2254. void AssetManager::renameAssetDependencies( StringTableEntry assetIdFrom, StringTableEntry assetIdTo )
  2255. {
  2256. // Debug Profiling.
  2257. PROFILE_SCOPE(AssetManager_RenameAssetDependencies);
  2258. // Sanity!
  2259. AssertFatal( assetIdFrom != NULL, "Cannot rename asset dependencies using NULL asset Id from." );
  2260. AssertFatal( assetIdTo != NULL, "Cannot rename asset dependencies using NULL asset Id to." );
  2261. // Rename via depends-on...
  2262. while( mAssetDependsOn.count( assetIdFrom ) > 0 )
  2263. {
  2264. // Find depends-on.
  2265. typeAssetDependsOnHash::Iterator dependsOnItr = mAssetDependsOn.find(assetIdFrom);
  2266. // Fetch dependency asset Id.
  2267. StringTableEntry dependencyAssetId = dependsOnItr->value;
  2268. // Find is-depends-on entry.
  2269. typeAssetIsDependedOnHash::Iterator isDependedOnItr = mAssetIsDependedOn.find(dependencyAssetId);
  2270. // Sanity!
  2271. AssertFatal( isDependedOnItr != mAssetIsDependedOn.end(), "Asset dependencies are corrupt!" );
  2272. while( isDependedOnItr != mAssetIsDependedOn.end() && isDependedOnItr->key == dependencyAssetId && isDependedOnItr->value != assetIdFrom )
  2273. {
  2274. isDependedOnItr++;
  2275. }
  2276. // Sanity!
  2277. AssertFatal( isDependedOnItr->key == dependencyAssetId && isDependedOnItr->value == assetIdFrom, "Asset dependencies are corrupt!" );
  2278. // Remove is-depended-on.
  2279. mAssetIsDependedOn.erase( isDependedOnItr );
  2280. // Remove depends-on.
  2281. mAssetDependsOn.erase( dependsOnItr );
  2282. // Insert depends-on.
  2283. mAssetDependsOn.insertEqual( assetIdTo, dependencyAssetId );
  2284. // Insert is-depended-on.
  2285. mAssetIsDependedOn.insertEqual( dependencyAssetId, assetIdTo );
  2286. }
  2287. // Rename via is-depended-on...
  2288. while( mAssetIsDependedOn.count( assetIdFrom ) > 0 )
  2289. {
  2290. // Find is-depended-on.
  2291. typeAssetIsDependedOnHash::Iterator isdependedOnItr = mAssetIsDependedOn.find(assetIdFrom);
  2292. // Fetch dependency asset Id.
  2293. StringTableEntry dependencyAssetId = isdependedOnItr->value;
  2294. // Find depends-on entry.
  2295. typeAssetDependsOnHash::Iterator dependsOnItr = mAssetDependsOn.find(dependencyAssetId);
  2296. // Sanity!
  2297. AssertFatal( dependsOnItr != mAssetDependsOn.end(), "Asset dependencies are corrupt!" );
  2298. while( dependsOnItr != mAssetDependsOn.end() && dependsOnItr->key == dependencyAssetId && dependsOnItr->value != assetIdFrom )
  2299. {
  2300. dependsOnItr++;
  2301. }
  2302. // Sanity!
  2303. AssertFatal( dependsOnItr->key == dependencyAssetId && dependsOnItr->value == assetIdFrom, "Asset dependencies are corrupt!" );
  2304. // Remove is-depended-on.
  2305. mAssetIsDependedOn.erase( isdependedOnItr );
  2306. // Remove depends-on.
  2307. mAssetDependsOn.erase( dependsOnItr );
  2308. // Insert depends-on.
  2309. mAssetDependsOn.insertEqual( dependencyAssetId, assetIdTo );
  2310. // Insert is-depended-on.
  2311. mAssetIsDependedOn.insertEqual( assetIdTo, dependencyAssetId );
  2312. }
  2313. }
  2314. //-----------------------------------------------------------------------------
  2315. void AssetManager::removeAssetDependencies( const char* pAssetId )
  2316. {
  2317. // Debug Profiling.
  2318. PROFILE_SCOPE(AssetManager_RemvoeAsetDependencies);
  2319. // Sanity!
  2320. AssertFatal( pAssetId != NULL, "Cannot remove asset dependencies using NULL asset Id." );
  2321. // Fetch asset Id.
  2322. StringTableEntry assetId = StringTable->insert( pAssetId );
  2323. // Remove from depends-on assets.
  2324. while( mAssetDependsOn.count( assetId ) > 0 )
  2325. {
  2326. // Find depends-on.
  2327. typeAssetDependsOnHash::Iterator dependsOnItr = mAssetDependsOn.find(assetId);
  2328. // Fetch dependency asset Id.
  2329. StringTableEntry dependencyAssetId = dependsOnItr->value;
  2330. // Find is-depends-on entry.
  2331. typeAssetIsDependedOnHash::Iterator isDependedOnItr = mAssetIsDependedOn.find(dependencyAssetId);
  2332. // Sanity!
  2333. AssertFatal( isDependedOnItr != mAssetIsDependedOn.end(), "Asset dependencies are corrupt!" );
  2334. while( isDependedOnItr != mAssetIsDependedOn.end() && isDependedOnItr->key == dependencyAssetId && isDependedOnItr->value != assetId )
  2335. {
  2336. isDependedOnItr++;
  2337. }
  2338. // Sanity!
  2339. AssertFatal( isDependedOnItr->key == dependencyAssetId && isDependedOnItr->value == assetId, "Asset dependencies are corrupt!" );
  2340. // Remove is-depended-on.
  2341. mAssetIsDependedOn.erase( isDependedOnItr );
  2342. // Remove depends-on.
  2343. mAssetDependsOn.erase( dependsOnItr );
  2344. }
  2345. }
  2346. //-----------------------------------------------------------------------------
  2347. void AssetManager::unloadAsset( AssetDefinition* pAssetDefinition )
  2348. {
  2349. // Debug Profiling.
  2350. PROFILE_SCOPE(AssetManager_UnloadAsset);
  2351. pAssetDefinition->mpAssetBase->unloadAsset();
  2352. // Destroy the asset.
  2353. if(pAssetDefinition->mpAssetBase->isProperlyAdded())
  2354. pAssetDefinition->mpAssetBase->deleteObject();
  2355. // Increase unloaded count.
  2356. pAssetDefinition->mAssetUnloadedCount++;
  2357. // Is the asset internal?
  2358. if ( pAssetDefinition->mAssetInternal )
  2359. {
  2360. // Yes, so decrease internal loaded asset count.
  2361. mLoadedInternalAssetsCount--;
  2362. }
  2363. else
  2364. {
  2365. // No, so decrease external loaded assets count.
  2366. mLoadedExternalAssetsCount--;
  2367. }
  2368. // Is the asset private?
  2369. if ( pAssetDefinition->mAssetPrivate )
  2370. {
  2371. // Yes, so decrease private loaded asset count.
  2372. mLoadedPrivateAssetsCount--;
  2373. // Remove it completely.
  2374. removeDeclaredAsset( pAssetDefinition->mAssetId );
  2375. }
  2376. }
  2377. //-----------------------------------------------------------------------------
  2378. void AssetManager::onModulePreLoad( ModuleDefinition* pModuleDefinition )
  2379. {
  2380. // Debug Profiling.
  2381. PROFILE_SCOPE(AssetManager_OnModulePreLoad);
  2382. // Add module declared assets.
  2383. addModuleDeclaredAssets( pModuleDefinition );
  2384. // Load any auto-loaded asset types
  2385. loadModuleAutoLoadAssets(pModuleDefinition);
  2386. // Is an asset tags manifest specified?
  2387. if ( pModuleDefinition->getAssetTagsManifest() != StringTable->EmptyString() )
  2388. {
  2389. // Yes, so load the asset tags manifest.
  2390. loadAssetTags( pModuleDefinition );
  2391. }
  2392. }
  2393. //-----------------------------------------------------------------------------
  2394. void AssetManager::onModulePreUnload( ModuleDefinition* pModuleDefinition )
  2395. {
  2396. // Debug Profiling.
  2397. PROFILE_SCOPE(AssetManager_OnModulePreUnload);
  2398. // Is an asset tags manifest specified?
  2399. if ( pModuleDefinition->getAssetTagsManifest() != StringTable->EmptyString() )
  2400. {
  2401. // Yes, so save the asset tags manifest.
  2402. saveAssetTags();
  2403. // Do we have an asset tags manifest?
  2404. if ( !mAssetTagsManifest.isNull() )
  2405. {
  2406. // Yes, so remove it.
  2407. mAssetTagsManifest->deleteObject();
  2408. mAssetTagsModuleDefinition = NULL;
  2409. }
  2410. }
  2411. }
  2412. //-----------------------------------------------------------------------------
  2413. void AssetManager::onModulePostUnload( ModuleDefinition* pModuleDefinition )
  2414. {
  2415. // Debug Profiling.
  2416. PROFILE_SCOPE(AssetManager_OnModulePostUnload);
  2417. // Remove declared assets.
  2418. removeDeclaredAssets( pModuleDefinition );
  2419. }