2
0

assetManager.cpp 108 KB

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