assetManager.cpp 111 KB

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