assetManager.cpp 102 KB

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