assetManager.cpp 104 KB

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