assetManager.cpp 101 KB

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