assetManager.cpp 102 KB

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