assetManager.cpp 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "assetManager.h"
  23. #ifndef _ASSET_PTR_H_
  24. #include "assetPtr.h"
  25. #endif
  26. #ifndef _REFERENCED_ASSETS_H_
  27. #include "assets/referencedAssets.h"
  28. #endif
  29. #ifndef _DECLARED_ASSETS_H_
  30. #include "assets/declaredAssets.h"
  31. #endif
  32. #ifndef _TAML_ASSET_REFERENCED_VISITOR_H_
  33. #include "tamlAssetReferencedVisitor.h"
  34. #endif
  35. #ifndef _TAML_ASSET_DECLARED_VISITOR_H_
  36. #include "tamlAssetDeclaredVisitor.h"
  37. #endif
  38. #ifndef _TAML_ASSET_DECLARED_UPDATE_VISITOR_H_
  39. #include "tamlAssetDeclaredUpdateVisitor.h"
  40. #endif
  41. #ifndef _TAML_ASSET_REFERENCED_UPDATE_VISITOR_H_
  42. #include "tamlAssetReferencedUpdateVisitor.h"
  43. #endif
  44. #ifndef _CONSOLETYPES_H_
  45. #include "console/consoleTypes.h"
  46. #endif
  47. #ifndef _AUTOLOAD_ASSETS_H_
  48. #include "assets/autoloadAssets.h"
  49. #endif
  50. #ifndef GUI_ASSET_H
  51. #include "T3D/assets/GUIAsset.h"
  52. #endif
  53. #ifndef SCRIPT_ASSET_H
  54. #include "T3D/assets/ScriptAsset.h"
  55. #endif
  56. #ifndef MATERIALASSET_H
  57. #include "T3D/assets/MaterialAsset.h"
  58. #endif
  59. #ifndef GAME_OBJECT_ASSET_H
  60. #include "T3D/assets/GameObjectAsset.h"
  61. #endif
  62. // Script bindings.
  63. #include "assetManager_ScriptBinding.h"
  64. //-----------------------------------------------------------------------------
  65. IMPLEMENT_CONOBJECT( AssetManager );
  66. //-----------------------------------------------------------------------------
  67. AssetManager AssetDatabase;
  68. //-----------------------------------------------------------------------------
  69. AssetManager::AssetManager() :
  70. mLoadedInternalAssetsCount( 0 ),
  71. mLoadedExternalAssetsCount( 0 ),
  72. mLoadedPrivateAssetsCount( 0 ),
  73. mMaxLoadedInternalAssetsCount( 0 ),
  74. mMaxLoadedExternalAssetsCount( 0 ),
  75. mEchoInfo( false ),
  76. mAcquiredReferenceCount( 0 ),
  77. mMaxLoadedPrivateAssetsCount( 0 ),
  78. mIgnoreAutoUnload( true )
  79. {
  80. }
  81. //-----------------------------------------------------------------------------
  82. bool AssetManager::onAdd()
  83. {
  84. // Call parent.
  85. if ( !Parent::onAdd() )
  86. return false;
  87. return true;
  88. }
  89. //-----------------------------------------------------------------------------
  90. void AssetManager::onRemove()
  91. {
  92. // Do we have an asset tags manifest?
  93. if ( !mAssetTagsManifest.isNull() )
  94. {
  95. // Yes, so remove it.
  96. mAssetTagsManifest->deleteObject();
  97. }
  98. // Call parent.
  99. Parent::onRemove();
  100. }
  101. //-----------------------------------------------------------------------------
  102. void AssetManager::initPersistFields()
  103. {
  104. // Call parent.
  105. Parent::initPersistFields();
  106. addField( "EchoInfo", TypeBool, false, Offset(mEchoInfo, AssetManager), "Whether the asset manager echos extra information to the console or not." );
  107. addField( "IgnoreAutoUnload", TypeBool, true, Offset(mIgnoreAutoUnload, AssetManager), "Whether the asset manager should ignore unloading of auto-unload assets or not." );
  108. }
  109. //-----------------------------------------------------------------------------
  110. bool AssetManager::compileReferencedAssets( ModuleDefinition* pModuleDefinition )
  111. {
  112. // Debug Profiling.
  113. PROFILE_SCOPE(AssetManager_CompileReferencedAsset);
  114. // Sanity!
  115. AssertFatal( pModuleDefinition != NULL, "Cannot add declared assets using a NULL module definition" );
  116. // Clear referenced assets.
  117. mReferencedAssets.clear();
  118. // Iterate the module definition children.
  119. for( SimSet::iterator itr = pModuleDefinition->begin(); itr != pModuleDefinition->end(); ++itr )
  120. {
  121. // Fetch the referenced assets.
  122. ReferencedAssets* pReferencedAssets = dynamic_cast<ReferencedAssets*>( *itr );
  123. // Skip if it's not a referenced assets location.
  124. if ( pReferencedAssets == NULL )
  125. continue;
  126. // Expand asset manifest location.
  127. char filePathBuffer[1024];
  128. dSprintf( filePathBuffer, sizeof(filePathBuffer), "%s/%s", pModuleDefinition->getModulePath(), pReferencedAssets->getPath() );
  129. // Scan referenced assets at location.
  130. if ( !scanReferencedAssets( filePathBuffer, pReferencedAssets->getExtension(), pReferencedAssets->getRecurse() ) )
  131. {
  132. // Warn.
  133. Con::warnf( "AssetManager::compileReferencedAssets() - Could not scan for referenced assets at location '%s' with extension '%s'.", filePathBuffer, pReferencedAssets->getExtension() );
  134. }
  135. }
  136. return true;
  137. }
  138. //-----------------------------------------------------------------------------
  139. bool AssetManager::addModuleDeclaredAssets( ModuleDefinition* pModuleDefinition )
  140. {
  141. // Debug Profiling.
  142. PROFILE_SCOPE(AssetManager_AddDeclaredAssets);
  143. // Sanity!
  144. AssertFatal( pModuleDefinition != NULL, "Cannot add declared assets using a NULL module definition" );
  145. // Does the module have any assets associated with it?
  146. if ( pModuleDefinition->getModuleAssets().size() > 0 )
  147. {
  148. // Yes, so warn.
  149. Con::warnf( "Asset Manager: Cannot add declared assets to module '%s' as it already has existing assets.", pModuleDefinition->getSignature() );
  150. return false;
  151. }
  152. // Iterate the module definition children.
  153. for( SimSet::iterator itr = pModuleDefinition->begin(); itr != pModuleDefinition->end(); ++itr )
  154. {
  155. // Fetch the declared assets.
  156. DeclaredAssets* pDeclaredAssets = dynamic_cast<DeclaredAssets*>( *itr );
  157. // Skip if it's not a declared assets location.
  158. if ( pDeclaredAssets == NULL )
  159. continue;
  160. // Expand asset manifest location.
  161. char filePathBuffer[1024];
  162. String mdldfpth = pModuleDefinition->getModulePath();
  163. String astfpth = pDeclaredAssets->getPath();
  164. //dSprintf( filePathBuffer, sizeof(filePathBuffer), "%s/%s", pModuleDefinition->getModulePath(), pDeclaredAssets->getPath() );
  165. dSprintf(filePathBuffer, sizeof(filePathBuffer), "%s/%s", pModuleDefinition->getModulePath(), pDeclaredAssets->getPath());
  166. // Scan declared assets at location.
  167. if ( !scanDeclaredAssets( filePathBuffer, pDeclaredAssets->getExtension(), pDeclaredAssets->getRecurse(), pModuleDefinition ) )
  168. {
  169. // Warn.
  170. Con::warnf( "AssetManager::addModuleDeclaredAssets() - Could not scan for declared assets at location '%s' with extension '%s'.", filePathBuffer, pDeclaredAssets->getExtension() );
  171. }
  172. }
  173. return true;
  174. }
  175. bool AssetManager::loadModuleAutoLoadAssets(ModuleDefinition* pModuleDefinition)
  176. {
  177. // Debug Profiling.
  178. PROFILE_SCOPE(AssetManager_loadModuleAutoLoadAssets);
  179. // Sanity!
  180. AssertFatal(pModuleDefinition != NULL, "Cannot auto load assets using a NULL module definition");
  181. // Does the module have any assets associated with it?
  182. if (pModuleDefinition->getModuleAssets().empty() && mEchoInfo)
  183. {
  184. // Yes, so warn.
  185. Con::warnf("Asset Manager: Cannot auto load assets to module '%s' as it has no existing assets.", pModuleDefinition->getSignature());
  186. return false;
  187. }
  188. U32 assetCount = pModuleDefinition->getModuleAssets().size();
  189. // Iterate the module definition children.
  190. for (SimSet::iterator itr = pModuleDefinition->begin(); itr != pModuleDefinition->end(); ++itr)
  191. {
  192. // Fetch the declared assets.
  193. AutoloadAssets* pAutoloadAssets = dynamic_cast<AutoloadAssets*>(*itr);
  194. // Skip if it's not a declared assets location.
  195. if (pAutoloadAssets == NULL)
  196. continue;
  197. for (U32 i = 0; i < assetCount; ++i)
  198. {
  199. AssetDefinition* assetDef = pModuleDefinition->getModuleAssets()[i];
  200. if (assetDef->mAssetType == pAutoloadAssets->getAssetType())
  201. {
  202. //TODO: this is stupid and ugly, need to properly automagically parse the class for registration
  203. AssetBase* assetBase = nullptr;
  204. if (assetDef->mAssetType == StringTable->insert("GUIAsset"))
  205. {
  206. assetBase = mTaml.read<GUIAsset>(assetDef->mAssetBaseFilePath);
  207. }
  208. else if (assetDef->mAssetType == StringTable->insert("ScriptAsset"))
  209. {
  210. assetBase = mTaml.read<ScriptAsset>(assetDef->mAssetBaseFilePath);
  211. }
  212. else if (assetDef->mAssetType == StringTable->insert("MaterialAsset"))
  213. {
  214. assetBase = mTaml.read<MaterialAsset>(assetDef->mAssetBaseFilePath);
  215. }
  216. else if (assetDef->mAssetType == StringTable->insert("GameObjectAsset"))
  217. {
  218. assetBase = mTaml.read<GameObjectAsset>(assetDef->mAssetBaseFilePath);
  219. }
  220. //load the asset now if valid
  221. if (assetBase)
  222. {
  223. assetBase->setOwned(this, assetDef);
  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. Con::expandPath( assetFilePathBuffer, sizeof(assetFilePathBuffer), pAssetFilePath );
  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 ( Platform::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. S32 QSORT_CALLBACK descendingAssetDefinitionLoadCount(const void* a, const void* b)
  1248. {
  1249. // Debug Profiling.
  1250. PROFILE_SCOPE(AssetManager_DescendingAssetDefinitionLoadCount);
  1251. // Fetch asset definitions.
  1252. const AssetDefinition* pAssetDefinitionA = *(AssetDefinition**)a;
  1253. const AssetDefinition* pAssetDefinitionB = *(AssetDefinition**)b;
  1254. // Sort.
  1255. return pAssetDefinitionB->mAssetLoadedCount - pAssetDefinitionA->mAssetLoadedCount;
  1256. }
  1257. //-----------------------------------------------------------------------------
  1258. void AssetManager::dumpDeclaredAssets( void ) const
  1259. {
  1260. Vector<const AssetDefinition*> assetDefinitions;
  1261. // Iterate asset definitions.
  1262. for( typeDeclaredAssetsHash::const_iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1263. {
  1264. assetDefinitions.push_back( assetItr->value );
  1265. }
  1266. // Sort asset definitions.
  1267. dQsort( assetDefinitions.address(), assetDefinitions.size(), sizeof(const AssetDefinition*), descendingAssetDefinitionLoadCount );
  1268. // Info.
  1269. Con::printSeparator();
  1270. Con::printf( "Asset Manager: %d declared asset(s) dump as follows:", mDeclaredAssets.size() );
  1271. Con::printBlankLine();
  1272. // Iterate sorted asset definitions.
  1273. for ( Vector<const AssetDefinition*>::iterator assetItr = assetDefinitions.begin(); assetItr != assetDefinitions.end(); ++assetItr )
  1274. {
  1275. // Fetch asset definition.
  1276. const AssetDefinition* pAssetDefinition = *assetItr;
  1277. // Info.
  1278. 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'",
  1279. pAssetDefinition->mAssetId,
  1280. pAssetDefinition->mpAssetBase == NULL ? 0 : pAssetDefinition->mpAssetBase->getAcquiredReferenceCount(),
  1281. pAssetDefinition->mAssetLoadedCount,
  1282. pAssetDefinition->mAssetUnloadedCount,
  1283. pAssetDefinition->mAssetAutoUnload,
  1284. pAssetDefinition->mpAssetBase != NULL,
  1285. pAssetDefinition->mAssetInternal,
  1286. pAssetDefinition->mAssetPrivate,
  1287. pAssetDefinition->mAssetType,
  1288. pAssetDefinition->mpModuleDefinition->getModuleId(),
  1289. pAssetDefinition->mpModuleDefinition->getVersionId(),
  1290. pAssetDefinition->mAssetBaseFilePath );
  1291. }
  1292. // Info.
  1293. Con::printSeparator();
  1294. Con::printBlankLine();
  1295. }
  1296. //-----------------------------------------------------------------------------
  1297. S32 AssetManager::findAllAssets( AssetQuery* pAssetQuery, const bool ignoreInternal, const bool ignorePrivate )
  1298. {
  1299. // Debug Profiling.
  1300. PROFILE_SCOPE(AssetManager_FindAllAssets);
  1301. // Sanity!
  1302. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1303. // Reset result count.
  1304. S32 resultCount = 0;
  1305. // Iterate declared assets.
  1306. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1307. {
  1308. // Fetch asset definition.
  1309. AssetDefinition* pAssetDefinition = assetItr->value;
  1310. // Skip if internal and we're ignoring them.
  1311. if ( ignoreInternal && pAssetDefinition->mAssetInternal )
  1312. continue;
  1313. // Skip if private and we're ignoring them.
  1314. if ( ignorePrivate && pAssetDefinition->mAssetPrivate )
  1315. continue;
  1316. // Store as result.
  1317. pAssetQuery->mAssetList.push_back( pAssetDefinition->mAssetId );
  1318. // Increase result count.
  1319. resultCount++;
  1320. }
  1321. return resultCount;
  1322. }
  1323. //-----------------------------------------------------------------------------
  1324. S32 AssetManager::findAssetName( AssetQuery* pAssetQuery, const char* pAssetName, const bool partialName )
  1325. {
  1326. // Debug Profiling.
  1327. PROFILE_SCOPE(AssetManager_FindAssetName);
  1328. // Sanity!
  1329. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1330. AssertFatal( pAssetName != NULL, "Cannot use NULL asset name." );
  1331. // Reset asset name.
  1332. StringTableEntry assetName = NULL;
  1333. S32 partialAssetNameLength = 0;
  1334. // Are we doing partial name search?
  1335. if ( partialName )
  1336. {
  1337. // Yes, so fetch length of partial name.
  1338. partialAssetNameLength = dStrlen( pAssetName );
  1339. }
  1340. else
  1341. {
  1342. // No, so fetch asset name.
  1343. assetName = StringTable->insert( pAssetName );
  1344. }
  1345. // Reset result count.
  1346. S32 resultCount = 0;
  1347. // Iterate declared assets.
  1348. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1349. {
  1350. // Fetch asset definition.
  1351. AssetDefinition* pAssetDefinition = assetItr->value;
  1352. // Are we doing partial name search?
  1353. if ( partialName )
  1354. {
  1355. // Yes, so fetch the length of this asset name.
  1356. const S32 currentAssetNameLength = dStrlen( pAssetDefinition->mAssetName );
  1357. // Skip if the query asset name is longer than the current asset name.
  1358. if ( partialAssetNameLength > currentAssetNameLength )
  1359. continue;
  1360. // Skip if this is not the asset we want.
  1361. if ( dStrnicmp( pAssetDefinition->mAssetName, pAssetName, partialAssetNameLength ) != 0 )
  1362. continue;
  1363. }
  1364. else
  1365. {
  1366. // No, so skip if this is not the asset we want.
  1367. if ( assetName != pAssetDefinition->mAssetName )
  1368. continue;
  1369. }
  1370. // Store as result.
  1371. pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
  1372. // Increase result count.
  1373. resultCount++;
  1374. }
  1375. return resultCount;
  1376. }
  1377. //-----------------------------------------------------------------------------
  1378. S32 AssetManager::findAssetCategory( AssetQuery* pAssetQuery, const char* pAssetCategory, const bool assetQueryAsSource )
  1379. {
  1380. // Debug Profiling.
  1381. PROFILE_SCOPE(AssetManager_FindAssetCategory);
  1382. // Sanity!
  1383. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1384. AssertFatal( pAssetCategory != NULL, "Cannot use NULL asset category." );
  1385. // Fetch asset category.
  1386. StringTableEntry assetCategory = StringTable->insert( pAssetCategory );
  1387. // Reset result count.
  1388. S32 resultCount = 0;
  1389. // Use asset-query as the source?
  1390. if ( assetQueryAsSource )
  1391. {
  1392. AssetQuery filteredAssets;
  1393. // Yes, so iterate asset query.
  1394. for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
  1395. {
  1396. // Fetch asset definition.
  1397. AssetDefinition* pAssetDefinition = findAsset( *assetItr );
  1398. // Skip if this is not the asset we want.
  1399. if ( pAssetDefinition == NULL ||
  1400. pAssetDefinition->mAssetCategory != assetCategory )
  1401. continue;
  1402. // Store as result.
  1403. filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
  1404. // Increase result count.
  1405. resultCount++;
  1406. }
  1407. // Set asset query.
  1408. pAssetQuery->set( filteredAssets );
  1409. }
  1410. else
  1411. {
  1412. // No, so iterate declared assets.
  1413. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1414. {
  1415. // Fetch asset definition.
  1416. AssetDefinition* pAssetDefinition = assetItr->value;
  1417. // Skip if this is not the asset we want.
  1418. if ( assetCategory != pAssetDefinition->mAssetCategory )
  1419. continue;
  1420. // Store as result.
  1421. pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
  1422. // Increase result count.
  1423. resultCount++;
  1424. }
  1425. }
  1426. return resultCount;
  1427. }
  1428. S32 AssetManager::findAssetAutoUnload( AssetQuery* pAssetQuery, const bool assetAutoUnload, const bool assetQueryAsSource )
  1429. {
  1430. // Debug Profiling.
  1431. PROFILE_SCOPE(AssetManager_FindAssetAutoUnload);
  1432. // Sanity!
  1433. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1434. // Reset result count.
  1435. S32 resultCount = 0;
  1436. // Use asset-query as the source?
  1437. if ( assetQueryAsSource )
  1438. {
  1439. AssetQuery filteredAssets;
  1440. // Yes, so iterate asset query.
  1441. for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
  1442. {
  1443. // Fetch asset definition.
  1444. AssetDefinition* pAssetDefinition = findAsset( *assetItr );
  1445. // Skip if this is not the asset we want.
  1446. if ( pAssetDefinition == NULL ||
  1447. pAssetDefinition->mAssetAutoUnload != assetAutoUnload )
  1448. continue;
  1449. // Store as result.
  1450. filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
  1451. // Increase result count.
  1452. resultCount++;
  1453. }
  1454. // Set asset query.
  1455. pAssetQuery->set( filteredAssets );
  1456. }
  1457. else
  1458. {
  1459. // No, so iterate declared assets.
  1460. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1461. {
  1462. // Fetch asset definition.
  1463. AssetDefinition* pAssetDefinition = assetItr->value;
  1464. // Skip if this is not the asset we want.
  1465. if ( assetAutoUnload != pAssetDefinition->mAssetAutoUnload )
  1466. continue;
  1467. // Store as result.
  1468. pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
  1469. // Increase result count.
  1470. resultCount++;
  1471. }
  1472. }
  1473. return resultCount;
  1474. }
  1475. //-----------------------------------------------------------------------------
  1476. S32 AssetManager::findAssetInternal( AssetQuery* pAssetQuery, const bool assetInternal, const bool assetQueryAsSource )
  1477. {
  1478. // Debug Profiling.
  1479. PROFILE_SCOPE(AssetManager_FindAssetInternal);
  1480. // Sanity!
  1481. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1482. // Reset result count.
  1483. S32 resultCount = 0;
  1484. // Use asset-query as the source?
  1485. if ( assetQueryAsSource )
  1486. {
  1487. AssetQuery filteredAssets;
  1488. // Yes, so iterate asset query.
  1489. for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
  1490. {
  1491. // Fetch asset definition.
  1492. AssetDefinition* pAssetDefinition = findAsset( *assetItr );
  1493. // Skip if this is not the asset we want.
  1494. if ( pAssetDefinition == NULL ||
  1495. pAssetDefinition->mAssetInternal != assetInternal )
  1496. continue;
  1497. // Store as result.
  1498. filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
  1499. // Increase result count.
  1500. resultCount++;
  1501. }
  1502. // Set asset query.
  1503. pAssetQuery->set( filteredAssets );
  1504. }
  1505. else
  1506. {
  1507. // No, so iterate declared assets.
  1508. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1509. {
  1510. // Fetch asset definition.
  1511. AssetDefinition* pAssetDefinition = assetItr->value;
  1512. // Skip if this is not the asset we want.
  1513. if ( assetInternal != pAssetDefinition->mAssetInternal )
  1514. continue;
  1515. // Store as result.
  1516. pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
  1517. // Increase result count.
  1518. resultCount++;
  1519. }
  1520. }
  1521. return resultCount;
  1522. }
  1523. //-----------------------------------------------------------------------------
  1524. S32 AssetManager::findAssetPrivate( AssetQuery* pAssetQuery, const bool assetPrivate, const bool assetQueryAsSource )
  1525. {
  1526. // Debug Profiling.
  1527. PROFILE_SCOPE(AssetManager_FindAssetPrivate);
  1528. // Sanity!
  1529. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1530. // Reset result count.
  1531. S32 resultCount = 0;
  1532. // Use asset-query as the source?
  1533. if ( assetQueryAsSource )
  1534. {
  1535. AssetQuery filteredAssets;
  1536. // Yes, so iterate asset query.
  1537. for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
  1538. {
  1539. // Fetch asset definition.
  1540. AssetDefinition* pAssetDefinition = findAsset( *assetItr );
  1541. // Skip if this is not the asset we want.
  1542. if ( pAssetDefinition == NULL ||
  1543. pAssetDefinition->mAssetPrivate != assetPrivate )
  1544. continue;
  1545. // Store as result.
  1546. filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
  1547. // Increase result count.
  1548. resultCount++;
  1549. }
  1550. // Set asset query.
  1551. pAssetQuery->set( filteredAssets );
  1552. }
  1553. else
  1554. {
  1555. // No, so iterate declared assets.
  1556. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1557. {
  1558. // Fetch asset definition.
  1559. AssetDefinition* pAssetDefinition = assetItr->value;
  1560. // Skip if this is not the asset we want.
  1561. if ( assetPrivate != pAssetDefinition->mAssetPrivate )
  1562. continue;
  1563. // Store as result.
  1564. pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
  1565. // Increase result count.
  1566. resultCount++;
  1567. }
  1568. }
  1569. return resultCount;
  1570. }
  1571. //-----------------------------------------------------------------------------
  1572. S32 AssetManager::findAssetType( AssetQuery* pAssetQuery, const char* pAssetType, const bool assetQueryAsSource )
  1573. {
  1574. // Debug Profiling.
  1575. PROFILE_SCOPE(AssetManager_FindAssetType);
  1576. // Sanity!
  1577. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1578. AssertFatal( pAssetType != NULL, "Cannot use NULL asset type." );
  1579. // Fetch asset type.
  1580. StringTableEntry assetType = StringTable->insert( pAssetType );
  1581. // Reset result count.
  1582. S32 resultCount = 0;
  1583. // Use asset-query as the source?
  1584. if ( assetQueryAsSource )
  1585. {
  1586. AssetQuery filteredAssets;
  1587. // Yes, so iterate asset query.
  1588. for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
  1589. {
  1590. // Fetch asset definition.
  1591. AssetDefinition* pAssetDefinition = findAsset( *assetItr );
  1592. // Skip if this is not the asset we want.
  1593. if ( pAssetDefinition == NULL ||
  1594. pAssetDefinition->mAssetType != assetType )
  1595. continue;
  1596. // Store as result.
  1597. filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
  1598. // Increase result count.
  1599. resultCount++;
  1600. }
  1601. // Set asset query.
  1602. pAssetQuery->set( filteredAssets );
  1603. }
  1604. else
  1605. {
  1606. // No, so iterate declared assets.
  1607. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1608. {
  1609. // Fetch asset definition.
  1610. AssetDefinition* pAssetDefinition = assetItr->value;
  1611. // Skip if this is not the asset we want.
  1612. if ( assetType != pAssetDefinition->mAssetType )
  1613. continue;
  1614. // Store as result.
  1615. pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
  1616. // Increase result count.
  1617. resultCount++;
  1618. }
  1619. }
  1620. return resultCount;
  1621. }
  1622. //-----------------------------------------------------------------------------
  1623. S32 AssetManager::findAssetDependsOn( AssetQuery* pAssetQuery, const char* pAssetId )
  1624. {
  1625. // Debug Profiling.
  1626. PROFILE_SCOPE(AssetManager_FindAssetDependsOn);
  1627. // Sanity!
  1628. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1629. AssertFatal( pAssetId != NULL, "Cannot use NULL asset Id." );
  1630. // Fetch asset Id.
  1631. StringTableEntry assetId = StringTable->insert( pAssetId );
  1632. // Reset result count.
  1633. S32 resultCount = 0;
  1634. // Find depends-on entry.
  1635. typeAssetDependsOnHash::Iterator dependsOnItr = mAssetDependsOn.find( assetId );
  1636. // Iterate all dependencies.
  1637. while( dependsOnItr != mAssetDependsOn.end() && dependsOnItr->key == assetId )
  1638. {
  1639. // Store as result.
  1640. pAssetQuery->mAssetList.push_back(dependsOnItr->value);
  1641. // Next dependency.
  1642. dependsOnItr++;
  1643. // Increase result count.
  1644. resultCount++;
  1645. }
  1646. return resultCount;
  1647. }
  1648. //-----------------------------------------------------------------------------
  1649. S32 AssetManager::findAssetIsDependedOn( AssetQuery* pAssetQuery, const char* pAssetId )
  1650. {
  1651. // Debug Profiling.
  1652. PROFILE_SCOPE(AssetManager_FindAssetIsDependedOn);
  1653. // Sanity!
  1654. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1655. AssertFatal( pAssetId != NULL, "Cannot use NULL asset Id." );
  1656. // Fetch asset Id.
  1657. StringTableEntry assetId = StringTable->insert( pAssetId );
  1658. // Reset result count.
  1659. S32 resultCount = 0;
  1660. // Find depended-on entry.
  1661. typeAssetIsDependedOnHash::Iterator dependedOnItr = mAssetIsDependedOn.find( assetId );
  1662. // Iterate all dependencies.
  1663. while( dependedOnItr != mAssetIsDependedOn.end() && dependedOnItr->key == assetId )
  1664. {
  1665. // Store as result.
  1666. pAssetQuery->mAssetList.push_back(dependedOnItr->value);
  1667. // Next dependency.
  1668. dependedOnItr++;
  1669. // Increase result count.
  1670. resultCount++;
  1671. }
  1672. return resultCount;
  1673. }
  1674. //-----------------------------------------------------------------------------
  1675. S32 AssetManager::findInvalidAssetReferences( AssetQuery* pAssetQuery )
  1676. {
  1677. // Debug Profiling.
  1678. PROFILE_SCOPE(AssetManager_FindInvalidAssetReferences);
  1679. // Sanity!
  1680. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1681. // Reset result count.
  1682. S32 resultCount = 0;
  1683. // Iterate referenced assets.
  1684. for( typeReferencedAssetsHash::Iterator assetItr = mReferencedAssets.begin(); assetItr != mReferencedAssets.end(); ++assetItr )
  1685. {
  1686. // Find asset definition.
  1687. AssetDefinition* pAssetDefinition = findAsset( assetItr->key );
  1688. // Skip if the asset definition was found.
  1689. if ( pAssetDefinition != NULL )
  1690. continue;
  1691. // Store as result.
  1692. pAssetQuery->mAssetList.push_back(assetItr->key);
  1693. // Increase result count.
  1694. resultCount++;
  1695. }
  1696. return resultCount;
  1697. }
  1698. //-----------------------------------------------------------------------------
  1699. S32 AssetManager::findTaggedAssets( AssetQuery* pAssetQuery, const char* pAssetTagNames, const bool assetQueryAsSource )
  1700. {
  1701. // Debug Profiling.
  1702. PROFILE_SCOPE(AssetManager_FindTaggedAssets);
  1703. // Sanity!
  1704. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1705. AssertFatal( pAssetTagNames != NULL, "Cannot use NULL asset tag name(s)." );
  1706. // Do we have an asset tag manifest?
  1707. if ( mAssetTagsManifest.isNull() )
  1708. {
  1709. // No, so warn.
  1710. Con::warnf( "Asset Manager: Cannot find tagged assets as no asset tag manifest is present." );
  1711. return 0;
  1712. }
  1713. // Reset result count.
  1714. S32 resultCount = 0;
  1715. const char* pTagSeparators = " ,\t\n";
  1716. // Fetch tag count.
  1717. U32 assetTagCount = StringUnit::getUnitCount( pAssetTagNames, pTagSeparators );
  1718. // Fetch asset tags.
  1719. Vector<AssetTagsManifest::AssetTag*> assetTags;
  1720. for( U32 tagIndex = 0; tagIndex < assetTagCount; ++tagIndex )
  1721. {
  1722. // Fetch asset tag name.
  1723. const char* pTagName = StringUnit::getUnit( pAssetTagNames, tagIndex, pTagSeparators );
  1724. // Fetch asset tag.
  1725. AssetTagsManifest::AssetTag* pAssetTag = mAssetTagsManifest->findAssetTag( pTagName );
  1726. // Did we find the asset tag?
  1727. if ( pAssetTag == NULL )
  1728. {
  1729. // No, so warn.
  1730. Con::warnf( "AssetTagsManifest: Asset Manager: Cannot find tagged assets of '%s' as it does not exist. Ignoring tag.", pTagName );
  1731. continue;
  1732. }
  1733. assetTags.push_back( pAssetTag );
  1734. }
  1735. // Fetch found asset tag count.
  1736. assetTagCount = assetTags.size();
  1737. // Did we find any tags?
  1738. if ( assetTagCount == 0 )
  1739. {
  1740. // No, so warn.
  1741. Con::warnf( "AssetTagsManifest: Asset Manager: No specified tagged assets found in '%s'.", pAssetTagNames );
  1742. return 0;
  1743. }
  1744. // Use asset-query as the source?
  1745. if ( assetQueryAsSource )
  1746. {
  1747. AssetQuery filteredAssets;
  1748. // Yes, so iterate asset query.
  1749. for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
  1750. {
  1751. // Fetch asset Id.
  1752. StringTableEntry assetId = *assetItr;
  1753. // Skip if asset is not valid.
  1754. if ( !isDeclaredAsset( assetId ) )
  1755. continue;
  1756. // Reset matched flag.
  1757. bool assetTagMatched = false;
  1758. // Iterate asset tags.
  1759. for ( Vector<AssetTagsManifest::AssetTag*>::iterator assetTagItr = assetTags.begin(); assetTagItr != assetTags.end(); ++assetTagItr )
  1760. {
  1761. // Fetch asset tag.
  1762. AssetTagsManifest::AssetTag* pAssetTag = *assetTagItr;
  1763. // Skip if asset is not tagged.
  1764. if ( !pAssetTag->containsAsset( assetId ) )
  1765. continue;
  1766. // Flag as matched.
  1767. assetTagMatched = true;
  1768. break;
  1769. }
  1770. // Did we find a match?
  1771. if ( assetTagMatched )
  1772. {
  1773. // Yes, so is asset already present?
  1774. if ( !filteredAssets.containsAsset( assetId ) )
  1775. {
  1776. // No, so store as result.
  1777. filteredAssets.mAssetList.push_back(assetId);
  1778. // Increase result count.
  1779. resultCount++;
  1780. }
  1781. }
  1782. }
  1783. // Set asset query.
  1784. pAssetQuery->set( filteredAssets );
  1785. }
  1786. else
  1787. {
  1788. // Iterate asset tags.
  1789. for ( Vector<AssetTagsManifest::AssetTag*>::iterator assetTagItr = assetTags.begin(); assetTagItr != assetTags.end(); ++assetTagItr )
  1790. {
  1791. // Fetch asset tag.
  1792. AssetTagsManifest::AssetTag* pAssetTag = *assetTagItr;
  1793. // Iterate tagged assets.
  1794. for ( Vector<typeAssetId>::iterator assetItr = pAssetTag->mAssets.begin(); assetItr != pAssetTag->mAssets.end(); ++assetItr )
  1795. {
  1796. // Fetch asset Id.
  1797. StringTableEntry assetId = *assetItr;
  1798. // Skip if asset Id is already present.
  1799. if ( pAssetQuery->containsAsset( assetId ) )
  1800. continue;
  1801. // Store as result.
  1802. pAssetQuery->mAssetList.push_back(assetId);
  1803. // Increase result count.
  1804. resultCount++;
  1805. }
  1806. }
  1807. }
  1808. return resultCount;
  1809. }
  1810. //-----------------------------------------------------------------------------
  1811. S32 AssetManager::findAssetLooseFile( AssetQuery* pAssetQuery, const char* pLooseFile, const bool assetQueryAsSource )
  1812. {
  1813. // Debug Profiling.
  1814. PROFILE_SCOPE(AssetManager_FindAssetLooseFile);
  1815. // Sanity!
  1816. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1817. AssertFatal( pLooseFile != NULL, "Cannot use NULL loose file." );
  1818. // Expand loose file.
  1819. char looseFileBuffer[1024];
  1820. Con::expandPath(looseFileBuffer, sizeof(looseFileBuffer), pLooseFile, NULL, false );
  1821. // Fetch asset loose file.
  1822. StringTableEntry looseFile = StringTable->insert( looseFileBuffer );
  1823. // Reset result count.
  1824. S32 resultCount = 0;
  1825. // Use asset-query as the source?
  1826. if ( assetQueryAsSource )
  1827. {
  1828. AssetQuery filteredAssets;
  1829. // Yes, so iterate asset query.
  1830. for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
  1831. {
  1832. // Fetch asset definition.
  1833. AssetDefinition* pAssetDefinition = findAsset( *assetItr );
  1834. // Fetch loose files.
  1835. Vector<StringTableEntry>& assetLooseFiles = pAssetDefinition->mAssetLooseFiles;
  1836. // Skip if this asset has no loose files.
  1837. if ( assetLooseFiles.size() == 0 )
  1838. continue;
  1839. // Search the assets loose files.
  1840. for( Vector<StringTableEntry>::iterator looseFileItr = assetLooseFiles.begin(); looseFileItr != assetLooseFiles.end(); ++looseFileItr )
  1841. {
  1842. // Is this the loose file we are searching for?
  1843. if ( *looseFileItr != looseFile )
  1844. continue;
  1845. // Store as result.
  1846. filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
  1847. // Increase result count.
  1848. resultCount++;
  1849. break;
  1850. }
  1851. }
  1852. // Set asset query.
  1853. pAssetQuery->set( filteredAssets );
  1854. }
  1855. else
  1856. {
  1857. // No, so iterate declared assets.
  1858. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1859. {
  1860. // Fetch asset definition.
  1861. AssetDefinition* pAssetDefinition = assetItr->value;
  1862. // Fetch loose files.
  1863. Vector<StringTableEntry>& assetLooseFiles = pAssetDefinition->mAssetLooseFiles;
  1864. // Skip if this asset has no loose files.
  1865. if ( assetLooseFiles.size() == 0 )
  1866. continue;
  1867. // Search the assets loose files.
  1868. for( Vector<StringTableEntry>::iterator looseFileItr = assetLooseFiles.begin(); looseFileItr != assetLooseFiles.end(); ++looseFileItr )
  1869. {
  1870. // Is this the loose file we are searching for?
  1871. if ( *looseFileItr != looseFile )
  1872. continue;
  1873. // Store as result.
  1874. pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
  1875. // Increase result count.
  1876. resultCount++;
  1877. break;
  1878. }
  1879. }
  1880. }
  1881. return resultCount;
  1882. }
  1883. //-----------------------------------------------------------------------------
  1884. AssetManager::typeAssetDependsOnHash* AssetManager::getDependedOnAssets()
  1885. {
  1886. // Find any asset dependencies.
  1887. return &mAssetDependsOn;
  1888. }
  1889. //-----------------------------------------------------------------------------
  1890. bool AssetManager::scanDeclaredAssets( const char* pPath, const char* pExtension, const bool recurse, ModuleDefinition* pModuleDefinition )
  1891. {
  1892. // Debug Profiling.
  1893. PROFILE_SCOPE(AssetManager_ScanDeclaredAssets);
  1894. // Sanity!
  1895. AssertFatal( pPath != NULL, "Cannot scan declared assets with NULL path." );
  1896. AssertFatal( pExtension != NULL, "Cannot scan declared assets with NULL extension." );
  1897. // Expand path location.
  1898. char pathBuffer[1024];
  1899. Con::expandPath( pathBuffer, sizeof(pathBuffer), pPath );
  1900. // Find files.
  1901. Vector<Platform::FileInfo> files;
  1902. if ( !Platform::dumpPath( pathBuffer, files, recurse ? -1 : 0 ) )
  1903. {
  1904. // Failed so warn.
  1905. Con::warnf( "Asset Manager: Failed to scan declared assets in directory '%s'.", pathBuffer );
  1906. return false;
  1907. }
  1908. // Is the asset file-path located within the specified module?
  1909. if ( !Con::isBasePath( pathBuffer, pModuleDefinition->getModulePath() ) )
  1910. {
  1911. // No, so warn.
  1912. Con::warnf( "Asset Manager: Could not add declared asset file '%s' as file does not exist with module path '%s'",
  1913. pathBuffer,
  1914. pModuleDefinition->getModulePath() );
  1915. return false;
  1916. }
  1917. // Info.
  1918. if ( mEchoInfo )
  1919. {
  1920. Con::printSeparator();
  1921. Con::printf( "Asset Manager: Scanning for declared assets in path '%s' for files with extension '%s'...", pathBuffer, pExtension );
  1922. }
  1923. // Fetch extension length.
  1924. const U32 extensionLength = dStrlen( pExtension );
  1925. // Fetch module assets.
  1926. ModuleDefinition::typeModuleAssetsVector& moduleAssets = pModuleDefinition->getModuleAssets();
  1927. TamlAssetDeclaredVisitor assetDeclaredVisitor;
  1928. // Iterate files.
  1929. for ( Vector<Platform::FileInfo>::iterator fileItr = files.begin(); fileItr != files.end(); ++fileItr )
  1930. {
  1931. // Fetch file info.
  1932. Platform::FileInfo& fileInfo = *fileItr;
  1933. // Fetch filename.
  1934. const char* pFilename = fileInfo.pFileName;
  1935. // Find filename length.
  1936. const U32 filenameLength = dStrlen( pFilename );
  1937. // Skip if extension is longer than filename.
  1938. if ( extensionLength > filenameLength )
  1939. continue;
  1940. // Skip if extension not found.
  1941. if ( dStricmp( pFilename + filenameLength - extensionLength, pExtension ) != 0 )
  1942. continue;
  1943. // Clear declared assets.
  1944. assetDeclaredVisitor.clear();
  1945. // Format full file-path.
  1946. char assetFileBuffer[1024];
  1947. dSprintf( assetFileBuffer, sizeof(assetFileBuffer), "%s/%s", fileInfo.pFullPath, fileInfo.pFileName );
  1948. // Parse the filename.
  1949. if ( !mTaml.parse( assetFileBuffer, assetDeclaredVisitor ) )
  1950. {
  1951. // Warn.
  1952. Con::warnf( "Asset Manager: Failed to parse file containing asset declaration: '%s'.", assetFileBuffer );
  1953. continue;
  1954. }
  1955. // Fetch asset definition.
  1956. AssetDefinition& foundAssetDefinition = assetDeclaredVisitor.getAssetDefinition();
  1957. // Did we get an asset name?
  1958. if ( foundAssetDefinition.mAssetName == StringTable->EmptyString() )
  1959. {
  1960. // No, so warn.
  1961. Con::warnf( "Asset Manager: Parsed file '%s' but did not encounter an asset.", assetFileBuffer );
  1962. continue;
  1963. }
  1964. // Set module definition.
  1965. foundAssetDefinition.mpModuleDefinition = pModuleDefinition;
  1966. // Format asset Id.
  1967. char assetIdBuffer[1024];
  1968. dSprintf(assetIdBuffer, sizeof(assetIdBuffer), "%s%s%s",
  1969. pModuleDefinition->getModuleId(),
  1970. ASSET_SCOPE_TOKEN,
  1971. foundAssetDefinition.mAssetName );
  1972. // Set asset Id.
  1973. foundAssetDefinition.mAssetId = StringTable->insert( assetIdBuffer );
  1974. // Does this asset already exist?
  1975. if ( mDeclaredAssets.contains( foundAssetDefinition.mAssetId ) )
  1976. {
  1977. // Yes, so warn.
  1978. Con::warnf( "Asset Manager: Encountered asset Id '%s' in asset file '%s' but it conflicts with existing asset Id in asset file '%s'.",
  1979. foundAssetDefinition.mAssetId,
  1980. foundAssetDefinition.mAssetBaseFilePath,
  1981. mDeclaredAssets.find( foundAssetDefinition.mAssetId )->value->mAssetBaseFilePath );
  1982. continue;
  1983. }
  1984. // Create new asset definition.
  1985. AssetDefinition* pAssetDefinition = new AssetDefinition( foundAssetDefinition );
  1986. // Store in declared assets.
  1987. mDeclaredAssets.insert( pAssetDefinition->mAssetId, pAssetDefinition );
  1988. // Store in module assets.
  1989. moduleAssets.push_back( pAssetDefinition );
  1990. // Info.
  1991. if ( mEchoInfo )
  1992. {
  1993. Con::printSeparator();
  1994. Con::printf( "Asset Manager: Adding Asset Id '%s' of type '%s' in asset file '%s'.",
  1995. pAssetDefinition->mAssetId,
  1996. pAssetDefinition->mAssetType,
  1997. pAssetDefinition->mAssetBaseFilePath );
  1998. }
  1999. // Fetch asset Id.
  2000. StringTableEntry assetId = pAssetDefinition->mAssetId;
  2001. // Fetch asset dependencies.
  2002. TamlAssetDeclaredVisitor::typeAssetIdVector& assetDependencies = assetDeclaredVisitor.getAssetDependencies();
  2003. // Are there any asset dependencies?
  2004. if ( assetDependencies.size() > 0 )
  2005. {
  2006. // Yes, so iterate dependencies.
  2007. for( TamlAssetDeclaredVisitor::typeAssetIdVector::iterator assetDependencyItr = assetDependencies.begin(); assetDependencyItr != assetDependencies.end(); ++assetDependencyItr )
  2008. {
  2009. // Fetch asset Ids.
  2010. StringTableEntry dependencyAssetId = *assetDependencyItr;
  2011. // Insert depends-on.
  2012. mAssetDependsOn.insertEqual( assetId, dependencyAssetId );
  2013. // Insert is-depended-on.
  2014. mAssetIsDependedOn.insertEqual( dependencyAssetId, assetId );
  2015. // Info.
  2016. if ( mEchoInfo )
  2017. {
  2018. Con::printf( "Asset Manager: Asset Id '%s' has dependency of Asset Id '%s'", assetId, dependencyAssetId );
  2019. }
  2020. }
  2021. }
  2022. // Fetch asset loose files.
  2023. TamlAssetDeclaredVisitor::typeLooseFileVector& assetLooseFiles = assetDeclaredVisitor.getAssetLooseFiles();
  2024. // Are there any loose files?
  2025. if ( assetLooseFiles.size() > 0 )
  2026. {
  2027. // Yes, so iterate loose files.
  2028. for( TamlAssetDeclaredVisitor::typeLooseFileVector::iterator assetLooseFileItr = assetLooseFiles.begin(); assetLooseFileItr != assetLooseFiles.end(); ++assetLooseFileItr )
  2029. {
  2030. // Fetch loose file.
  2031. StringTableEntry looseFile = *assetLooseFileItr;
  2032. // Info.
  2033. if ( mEchoInfo )
  2034. {
  2035. Con::printf( "Asset Manager: Asset Id '%s' has loose file '%s'.", assetId, looseFile );
  2036. }
  2037. // Store loose file.
  2038. pAssetDefinition->mAssetLooseFiles.push_back( looseFile );
  2039. }
  2040. }
  2041. }
  2042. // Info.
  2043. if ( mEchoInfo )
  2044. {
  2045. Con::printSeparator();
  2046. Con::printf( "Asset Manager: ... Finished scanning for declared assets in path '%s' for files with extension '%s'.", pathBuffer, pExtension );
  2047. Con::printSeparator();
  2048. Con::printBlankLine();
  2049. }
  2050. return true;
  2051. }
  2052. //-----------------------------------------------------------------------------
  2053. bool AssetManager::scanReferencedAssets( const char* pPath, const char* pExtension, const bool recurse )
  2054. {
  2055. // Debug Profiling.
  2056. PROFILE_SCOPE(AssetManager_ScanReferencedAssets);
  2057. // Sanity!
  2058. AssertFatal( pPath != NULL, "Cannot scan referenced assets with NULL path." );
  2059. AssertFatal( pExtension != NULL, "Cannot scan referenced assets with NULL extension." );
  2060. // Expand path location.
  2061. char pathBuffer[1024];
  2062. Con::expandPath( pathBuffer, sizeof(pathBuffer), pPath );
  2063. // Find files.
  2064. Vector<Platform::FileInfo> files;
  2065. if ( !Platform::dumpPath( pathBuffer, files, recurse ? -1 : 0 ) )
  2066. {
  2067. // Failed so warn.
  2068. Con::warnf( "Asset Manager: Failed to scan referenced assets in directory '%s'.", pathBuffer );
  2069. return false;
  2070. }
  2071. // Info.
  2072. if ( mEchoInfo )
  2073. {
  2074. Con::printSeparator();
  2075. Con::printf( "Asset Manager: Scanning for referenced assets in path '%s' for files with extension '%s'...", pathBuffer, pExtension );
  2076. }
  2077. // Fetch extension length.
  2078. const U32 extensionLength = dStrlen( pExtension );
  2079. TamlAssetReferencedVisitor assetReferencedVisitor;
  2080. // Iterate files.
  2081. for ( Vector<Platform::FileInfo>::iterator fileItr = files.begin(); fileItr != files.end(); ++fileItr )
  2082. {
  2083. // Fetch file info.
  2084. Platform::FileInfo& fileInfo = *fileItr;
  2085. // Fetch filename.
  2086. const char* pFilename = fileInfo.pFileName;
  2087. // Find filename length.
  2088. const U32 filenameLength = dStrlen( pFilename );
  2089. // Skip if extension is longer than filename.
  2090. if ( extensionLength > filenameLength )
  2091. continue;
  2092. // Skip if extension not found.
  2093. if ( dStricmp( pFilename + filenameLength - extensionLength, pExtension ) != 0 )
  2094. continue;
  2095. // Clear referenced assets.
  2096. assetReferencedVisitor.clear();
  2097. // Format full file-path.
  2098. char assetFileBuffer[1024];
  2099. dSprintf( assetFileBuffer, sizeof(assetFileBuffer), "%s/%s", fileInfo.pFullPath, fileInfo.pFileName );
  2100. // Format reference file-path.
  2101. typeReferenceFilePath referenceFilePath = StringTable->insert( assetFileBuffer );
  2102. // Parse the filename.
  2103. if ( !mTaml.parse( referenceFilePath, assetReferencedVisitor ) )
  2104. {
  2105. // Warn.
  2106. Con::warnf( "Asset Manager: Failed to parse file containing asset references: '%s'.", referenceFilePath );
  2107. continue;
  2108. }
  2109. // Fetch usage map.
  2110. const TamlAssetReferencedVisitor::typeAssetReferencedHash& assetReferencedMap = assetReferencedVisitor.getAssetReferencedMap();
  2111. // Do we have any asset references?
  2112. if ( assetReferencedMap.size() > 0 )
  2113. {
  2114. // Info.
  2115. if ( mEchoInfo )
  2116. {
  2117. Con::printSeparator();
  2118. }
  2119. // Iterate usage.
  2120. for( TamlAssetReferencedVisitor::typeAssetReferencedHash::const_iterator usageItr = assetReferencedMap.begin(); usageItr != assetReferencedMap.end(); ++usageItr )
  2121. {
  2122. // Fetch asset name.
  2123. typeAssetId assetId = usageItr->key;
  2124. // Info.
  2125. if ( mEchoInfo )
  2126. {
  2127. Con::printf( "Asset Manager: Found referenced Asset Id '%s' in file '%s'.", assetId, referenceFilePath );
  2128. }
  2129. // Add referenced asset.
  2130. addReferencedAsset( assetId, referenceFilePath );
  2131. }
  2132. }
  2133. }
  2134. // Info.
  2135. if ( mEchoInfo )
  2136. {
  2137. Con::printf( "Asset Manager: ... Finished scanning for referenced assets in path '%s' for files with extension '%s'.", pathBuffer, pExtension );
  2138. Con::printSeparator();
  2139. Con::printBlankLine();
  2140. }
  2141. return true;
  2142. }
  2143. //-----------------------------------------------------------------------------
  2144. AssetDefinition* AssetManager::findAsset( const char* pAssetId )
  2145. {
  2146. // Debug Profiling.
  2147. PROFILE_SCOPE(AssetManager_FindAsset);
  2148. // Sanity!
  2149. AssertFatal( pAssetId != NULL, "Cannot find NULL asset Id." );
  2150. // Fetch asset Id.
  2151. StringTableEntry assetId = StringTable->insert( pAssetId );
  2152. // Find declared asset.
  2153. typeDeclaredAssetsHash::iterator declaredAssetItr = mDeclaredAssets.find( assetId );
  2154. // Find if we didn't find a declared asset Id.
  2155. if ( declaredAssetItr == mDeclaredAssets.end() )
  2156. return NULL;
  2157. return declaredAssetItr->value;
  2158. }
  2159. //-----------------------------------------------------------------------------
  2160. void AssetManager::addReferencedAsset( StringTableEntry assetId, StringTableEntry referenceFilePath )
  2161. {
  2162. // Debug Profiling.
  2163. PROFILE_SCOPE(AssetManager_AddReferencedAsset);
  2164. // Sanity!
  2165. AssertFatal( assetId != NULL, "Cannot add referenced asset with NULL asset Id." );
  2166. AssertFatal( referenceFilePath != NULL, "Cannot add referenced asset with NULL reference file-path." );
  2167. // Find referenced asset.
  2168. typeReferencedAssetsHash::Iterator referencedAssetItr = mReferencedAssets.find( assetId );
  2169. // Did we find the asset?
  2170. if ( referencedAssetItr == mReferencedAssets.end() )
  2171. {
  2172. // No, so add asset Id.
  2173. mReferencedAssets.insertEqual( assetId, referenceFilePath );
  2174. }
  2175. else
  2176. {
  2177. // Yes, so add asset Id with a unique file.
  2178. while( true )
  2179. {
  2180. // Finish if this file is already present.
  2181. if ( referencedAssetItr->value == referenceFilePath )
  2182. return;
  2183. // Move to next asset Id.
  2184. referencedAssetItr++;
  2185. // Is this the end of referenced assets or a different asset Id?
  2186. if ( referencedAssetItr == mReferencedAssets.end() ||
  2187. referencedAssetItr->key != assetId )
  2188. {
  2189. // Yes, so add asset reference.
  2190. mReferencedAssets.insertEqual( assetId, referenceFilePath );
  2191. return;
  2192. }
  2193. };
  2194. }
  2195. }
  2196. //-----------------------------------------------------------------------------
  2197. void AssetManager::renameAssetReferences( StringTableEntry assetIdFrom, StringTableEntry assetIdTo )
  2198. {
  2199. // Debug Profiling.
  2200. PROFILE_SCOPE(AssetManager_RenameAssetReferences);
  2201. // Sanity!
  2202. AssertFatal( assetIdFrom != NULL, "Cannot rename asset references using NULL asset Id from." );
  2203. AssertFatal( assetIdTo != NULL, "Cannot rename asset references using NULL asset Id to." );
  2204. // Finish if the asset is not referenced.
  2205. if ( !mReferencedAssets.count( assetIdFrom ) )
  2206. return;
  2207. // Setup referenced update visitor.
  2208. TamlAssetReferencedUpdateVisitor assetReferencedUpdateVisitor;
  2209. assetReferencedUpdateVisitor.setAssetIdFrom( assetIdFrom );
  2210. assetReferencedUpdateVisitor.setAssetIdTo( assetIdTo );
  2211. // Find first referenced asset Id.
  2212. typeReferencedAssetsHash::Iterator referencedAssetItr = mReferencedAssets.find( assetIdFrom );
  2213. // Iterate references.
  2214. while( true )
  2215. {
  2216. // Finish if end of references.
  2217. if ( referencedAssetItr == mReferencedAssets.end() || referencedAssetItr->key != assetIdFrom )
  2218. return;
  2219. // Info.
  2220. if ( mEchoInfo )
  2221. {
  2222. Con::printf( "Asset Manager: Renaming declared Asset Id '%s' to Asset Id '%s'. Updating referenced file '%s'",
  2223. assetIdFrom,
  2224. assetIdTo,
  2225. referencedAssetItr->value );
  2226. }
  2227. // Update asset file declaration.
  2228. if ( !mTaml.parse( referencedAssetItr->value, assetReferencedUpdateVisitor ) )
  2229. {
  2230. // No, so warn.
  2231. Con::warnf("Asset Manager: Cannot rename referenced asset Id '%s' to asset Id '%s' as the referenced asset file could not be parsed: %s",
  2232. assetIdFrom, assetIdTo, referencedAssetItr->value );
  2233. }
  2234. // Move to next reference.
  2235. referencedAssetItr++;
  2236. }
  2237. }
  2238. //-----------------------------------------------------------------------------
  2239. void AssetManager::removeAssetReferences( StringTableEntry assetId )
  2240. {
  2241. // Debug Profiling.
  2242. PROFILE_SCOPE(AssetManager_RemoveAssetReferences);
  2243. // Sanity!
  2244. AssertFatal( assetId != NULL, "Cannot rename asset references using NULL asset Id." );
  2245. // Finish if the asset is not referenced.
  2246. if ( !mReferencedAssets.count( assetId ) )
  2247. return;
  2248. // Setup referenced update visitor.
  2249. TamlAssetReferencedUpdateVisitor assetReferencedUpdateVisitor;
  2250. assetReferencedUpdateVisitor.setAssetIdFrom( assetId );
  2251. assetReferencedUpdateVisitor.setAssetIdTo( StringTable->EmptyString() );
  2252. // Find first referenced asset Id.
  2253. typeReferencedAssetsHash::Iterator referencedAssetItr = mReferencedAssets.find(assetId);
  2254. // Iterate references.
  2255. while( true )
  2256. {
  2257. // Finish if end of references.
  2258. if ( referencedAssetItr == mReferencedAssets.end() || referencedAssetItr->key != assetId )
  2259. break;
  2260. // Info.
  2261. if ( mEchoInfo )
  2262. {
  2263. Con::printf( "Asset Manager: Removing Asset Id '%s' references from file '%s'",
  2264. assetId,
  2265. referencedAssetItr->value );
  2266. }
  2267. // Update asset file declaration.
  2268. if ( !mTaml.parse( referencedAssetItr->value, assetReferencedUpdateVisitor ) )
  2269. {
  2270. // No, so warn.
  2271. Con::warnf("Asset Manager: Cannot remove referenced asset Id '%s' as the referenced asset file could not be parsed: %s",
  2272. assetId,
  2273. referencedAssetItr->value );
  2274. }
  2275. // Move to next reference.
  2276. referencedAssetItr++;
  2277. }
  2278. // Remove asset references.
  2279. mReferencedAssets.erase( assetId );
  2280. }
  2281. //-----------------------------------------------------------------------------
  2282. void AssetManager::renameAssetDependencies( StringTableEntry assetIdFrom, StringTableEntry assetIdTo )
  2283. {
  2284. // Debug Profiling.
  2285. PROFILE_SCOPE(AssetManager_RenameAssetDependencies);
  2286. // Sanity!
  2287. AssertFatal( assetIdFrom != NULL, "Cannot rename asset dependencies using NULL asset Id from." );
  2288. AssertFatal( assetIdTo != NULL, "Cannot rename asset dependencies using NULL asset Id to." );
  2289. // Rename via depends-on...
  2290. while( mAssetDependsOn.count( assetIdFrom ) > 0 )
  2291. {
  2292. // Find depends-on.
  2293. typeAssetDependsOnHash::Iterator dependsOnItr = mAssetDependsOn.find(assetIdFrom);
  2294. // Fetch dependency asset Id.
  2295. StringTableEntry dependencyAssetId = dependsOnItr->value;
  2296. // Find is-depends-on entry.
  2297. typeAssetIsDependedOnHash::Iterator isDependedOnItr = mAssetIsDependedOn.find(dependencyAssetId);
  2298. // Sanity!
  2299. AssertFatal( isDependedOnItr != mAssetIsDependedOn.end(), "Asset dependencies are corrupt!" );
  2300. while( isDependedOnItr != mAssetIsDependedOn.end() && isDependedOnItr->key == dependencyAssetId && isDependedOnItr->value != assetIdFrom )
  2301. {
  2302. isDependedOnItr++;
  2303. }
  2304. // Sanity!
  2305. AssertFatal( isDependedOnItr->key == dependencyAssetId && isDependedOnItr->value == assetIdFrom, "Asset dependencies are corrupt!" );
  2306. // Remove is-depended-on.
  2307. mAssetIsDependedOn.erase( isDependedOnItr );
  2308. // Remove depends-on.
  2309. mAssetDependsOn.erase( dependsOnItr );
  2310. // Insert depends-on.
  2311. mAssetDependsOn.insertEqual( assetIdTo, dependencyAssetId );
  2312. // Insert is-depended-on.
  2313. mAssetIsDependedOn.insertEqual( dependencyAssetId, assetIdTo );
  2314. }
  2315. // Rename via is-depended-on...
  2316. while( mAssetIsDependedOn.count( assetIdFrom ) > 0 )
  2317. {
  2318. // Find is-depended-on.
  2319. typeAssetIsDependedOnHash::Iterator isdependedOnItr = mAssetIsDependedOn.find(assetIdFrom);
  2320. // Fetch dependency asset Id.
  2321. StringTableEntry dependencyAssetId = isdependedOnItr->value;
  2322. // Find depends-on entry.
  2323. typeAssetDependsOnHash::Iterator dependsOnItr = mAssetDependsOn.find(dependencyAssetId);
  2324. // Sanity!
  2325. AssertFatal( dependsOnItr != mAssetDependsOn.end(), "Asset dependencies are corrupt!" );
  2326. while( dependsOnItr != mAssetDependsOn.end() && dependsOnItr->key == dependencyAssetId && dependsOnItr->value != assetIdFrom )
  2327. {
  2328. dependsOnItr++;
  2329. }
  2330. // Sanity!
  2331. AssertFatal( dependsOnItr->key == dependencyAssetId && dependsOnItr->value == assetIdFrom, "Asset dependencies are corrupt!" );
  2332. // Remove is-depended-on.
  2333. mAssetIsDependedOn.erase( isdependedOnItr );
  2334. // Remove depends-on.
  2335. mAssetDependsOn.erase( dependsOnItr );
  2336. // Insert depends-on.
  2337. mAssetDependsOn.insertEqual( dependencyAssetId, assetIdTo );
  2338. // Insert is-depended-on.
  2339. mAssetIsDependedOn.insertEqual( assetIdTo, dependencyAssetId );
  2340. }
  2341. }
  2342. //-----------------------------------------------------------------------------
  2343. void AssetManager::removeAssetDependencies( const char* pAssetId )
  2344. {
  2345. // Debug Profiling.
  2346. PROFILE_SCOPE(AssetManager_RemvoeAsetDependencies);
  2347. // Sanity!
  2348. AssertFatal( pAssetId != NULL, "Cannot remove asset dependencies using NULL asset Id." );
  2349. // Fetch asset Id.
  2350. StringTableEntry assetId = StringTable->insert( pAssetId );
  2351. // Remove from depends-on assets.
  2352. while( mAssetDependsOn.count( assetId ) > 0 )
  2353. {
  2354. // Find depends-on.
  2355. typeAssetDependsOnHash::Iterator dependsOnItr = mAssetDependsOn.find(assetId);
  2356. // Fetch dependency asset Id.
  2357. StringTableEntry dependencyAssetId = dependsOnItr->value;
  2358. // Find is-depends-on entry.
  2359. typeAssetIsDependedOnHash::Iterator isDependedOnItr = mAssetIsDependedOn.find(dependencyAssetId);
  2360. // Sanity!
  2361. AssertFatal( isDependedOnItr != mAssetIsDependedOn.end(), "Asset dependencies are corrupt!" );
  2362. while( isDependedOnItr != mAssetIsDependedOn.end() && isDependedOnItr->key == dependencyAssetId && isDependedOnItr->value != assetId )
  2363. {
  2364. isDependedOnItr++;
  2365. }
  2366. // Sanity!
  2367. AssertFatal( isDependedOnItr->key == dependencyAssetId && isDependedOnItr->value == assetId, "Asset dependencies are corrupt!" );
  2368. // Remove is-depended-on.
  2369. mAssetIsDependedOn.erase( isDependedOnItr );
  2370. // Remove depends-on.
  2371. mAssetDependsOn.erase( dependsOnItr );
  2372. }
  2373. }
  2374. //-----------------------------------------------------------------------------
  2375. void AssetManager::unloadAsset( AssetDefinition* pAssetDefinition )
  2376. {
  2377. // Debug Profiling.
  2378. PROFILE_SCOPE(AssetManager_UnloadAsset);
  2379. // Destroy the asset.
  2380. pAssetDefinition->mpAssetBase->deleteObject();
  2381. // Increase unloaded count.
  2382. pAssetDefinition->mAssetUnloadedCount++;
  2383. // Is the asset internal?
  2384. if ( pAssetDefinition->mAssetInternal )
  2385. {
  2386. // Yes, so decrease internal loaded asset count.
  2387. mLoadedInternalAssetsCount--;
  2388. }
  2389. else
  2390. {
  2391. // No, so decrease external loaded assets count.
  2392. mLoadedExternalAssetsCount--;
  2393. }
  2394. // Is the asset private?
  2395. if ( pAssetDefinition->mAssetPrivate )
  2396. {
  2397. // Yes, so decrease private loaded asset count.
  2398. mLoadedPrivateAssetsCount--;
  2399. // Remove it completely.
  2400. removeDeclaredAsset( pAssetDefinition->mAssetId );
  2401. }
  2402. }
  2403. //-----------------------------------------------------------------------------
  2404. void AssetManager::onModulePreLoad( ModuleDefinition* pModuleDefinition )
  2405. {
  2406. // Debug Profiling.
  2407. PROFILE_SCOPE(AssetManager_OnModulePreLoad);
  2408. // Add module declared assets.
  2409. addModuleDeclaredAssets( pModuleDefinition );
  2410. // Load any auto-loaded asset types
  2411. loadModuleAutoLoadAssets(pModuleDefinition);
  2412. // Is an asset tags manifest specified?
  2413. if ( pModuleDefinition->getAssetTagsManifest() != StringTable->EmptyString() )
  2414. {
  2415. // Yes, so load the asset tags manifest.
  2416. loadAssetTags( pModuleDefinition );
  2417. }
  2418. }
  2419. //-----------------------------------------------------------------------------
  2420. void AssetManager::onModulePreUnload( ModuleDefinition* pModuleDefinition )
  2421. {
  2422. // Debug Profiling.
  2423. PROFILE_SCOPE(AssetManager_OnModulePreUnload);
  2424. // Is an asset tags manifest specified?
  2425. if ( pModuleDefinition->getAssetTagsManifest() != StringTable->EmptyString() )
  2426. {
  2427. // Yes, so save the asset tags manifest.
  2428. saveAssetTags();
  2429. // Do we have an asset tags manifest?
  2430. if ( !mAssetTagsManifest.isNull() )
  2431. {
  2432. // Yes, so remove it.
  2433. mAssetTagsManifest->deleteObject();
  2434. mAssetTagsModuleDefinition = NULL;
  2435. }
  2436. }
  2437. }
  2438. //-----------------------------------------------------------------------------
  2439. void AssetManager::onModulePostUnload( ModuleDefinition* pModuleDefinition )
  2440. {
  2441. // Debug Profiling.
  2442. PROFILE_SCOPE(AssetManager_OnModulePostUnload);
  2443. // Remove declared assets.
  2444. removeDeclaredAssets( pModuleDefinition );
  2445. }