assetManager.cpp 101 KB

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