assetManager.cpp 102 KB

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