assetManager.cpp 101 KB

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