assetManager.cpp 101 KB

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