assetManager.cpp 102 KB

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