assetManager.cpp 111 KB

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