assetManager.cpp 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423
  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. void AssetManager::packDataAsset(BitStream* stream, const char* pAssetId)
  754. {
  755. if(stream->writeFlag(isDeclaredAsset(pAssetId)))
  756. {
  757. stream->write(mAssetToNetId.find(pAssetId)->value);
  758. }
  759. }
  760. const char* AssetManager::unpackDataAsset(BitStream* stream)
  761. {
  762. if (stream->readFlag())
  763. {
  764. typeAssetNetId netId;
  765. stream->read(&netId);
  766. typeNetIdToAssetMap::iterator netChar = mNetIdToAsset.find(netId);
  767. if (netChar != mNetIdToAsset.end())
  768. {
  769. return netChar->value;
  770. }
  771. }
  772. return StringTable->EmptyString();
  773. }
  774. void AssetManager::packUpdateAsset(NetConnection* con, U32 mask, BitStream* stream, const char* pAssetId)
  775. {
  776. if (stream->writeFlag(isDeclaredAsset(pAssetId)))
  777. {
  778. stream->write(mAssetToNetId.find(pAssetId)->value);
  779. }
  780. }
  781. const char* AssetManager::unpackUpdateAsset(NetConnection* con, BitStream* stream)
  782. {
  783. if (stream->readFlag())
  784. {
  785. typeAssetNetId netId;
  786. stream->read(&netId);
  787. typeNetIdToAssetMap::iterator netChar = mNetIdToAsset.find(netId);
  788. if (netChar != mNetIdToAsset.end())
  789. {
  790. return netChar->value;
  791. }
  792. }
  793. return StringTable->EmptyString();
  794. }
  795. bool AssetManager::compileAllAssets(const bool compressed, const bool includeUnloaded)
  796. {
  797. // Debug Profiling.
  798. PROFILE_SCOPE(AssetManager_CompileAllAssets);
  799. // Info.
  800. if (mEchoInfo)
  801. {
  802. Con::printSeparator();
  803. Con::printf("Asset Manager: Started compiling ALL assets.");
  804. }
  805. Vector<typeAssetId> assetsToRelease;
  806. // Are we including unloaded assets?
  807. if (includeUnloaded)
  808. {
  809. // Yes, so prepare a list of assets to release and load them.
  810. for (typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr)
  811. {
  812. // Fetch asset Id.
  813. typeAssetId assetId = assetItr->key;
  814. // Skip if asset is loaded.
  815. if (assetItr->value->mpAssetBase != NULL)
  816. continue;
  817. // Note asset as needing a release.
  818. assetsToRelease.push_back(assetId);
  819. // Acquire the asset.
  820. acquireAsset<AssetBase>(assetId);
  821. }
  822. }
  823. bool oldCompressed = mTaml.getBinaryCompression();
  824. mTaml.setBinaryCompression(compressed);
  825. bool success = false;
  826. // Refresh the current loaded assets.
  827. // NOTE: This will result in some assets being refreshed more than once due to asset dependencies.
  828. for (typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr)
  829. {
  830. // Skip private assets.
  831. if (assetItr->value->mAssetPrivate)
  832. continue;
  833. // Refresh asset if it's loaded.
  834. success = compileAsset(assetItr->key);
  835. if (!success)
  836. {
  837. break;
  838. }
  839. }
  840. mTaml.setBinaryCompression(oldCompressed);
  841. // Are we including unloaded assets?
  842. if (includeUnloaded)
  843. {
  844. // Yes, so release the assets we loaded.
  845. for (Vector<typeAssetId>::iterator assetItr = assetsToRelease.begin(); assetItr != assetsToRelease.end(); ++assetItr)
  846. {
  847. releaseAsset(*assetItr);
  848. }
  849. }
  850. // Info.
  851. if (mEchoInfo)
  852. {
  853. Con::printSeparator();
  854. Con::printf("Asset Manager: Finished compiling ALL assets.");
  855. }
  856. return success;
  857. }
  858. bool AssetManager::compileModuleAssets(ModuleDefinition* pModuleDefinition)
  859. {
  860. // Sanity!
  861. AssertFatal(pModuleDefinition != NULL, "Cannot remove declared assets using a NULL module definition");
  862. // Fetch module assets.
  863. ModuleDefinition::typeModuleAssetsVector& moduleAssets = pModuleDefinition->getModuleAssets();
  864. // Remove all module assets.
  865. while (moduleAssets.size() > 0)
  866. {
  867. // Fetch asset definition.
  868. AssetDefinition* pAssetDefinition = *moduleAssets.begin();
  869. bool success = compileAsset(pAssetDefinition->mAssetId);
  870. if (!success)
  871. return false;
  872. }
  873. return true;
  874. }
  875. bool AssetManager::compileAsset(const char* pAssetId)
  876. {
  877. // Sanity!
  878. AssertFatal(pAssetId != NULL, "Cannot compile NULL asset");
  879. AssetDefinition* pAssetDefinition = findAsset(pAssetId);
  880. // Does the asset exist?
  881. if (pAssetDefinition == NULL)
  882. {
  883. Con::warnf("Asset Manager::compileAsset Failed to compile asset Id '%s' as it does not exist", pAssetId);
  884. return false;
  885. }
  886. // Info.
  887. if (mEchoInfo)
  888. {
  889. Con::printSeparator();
  890. Con::printf("Asset Manager::compileAsset Started compiling Asset Id '%s'...", pAssetId);
  891. }
  892. AssetBase* pAssetBase = pAssetDefinition->mpAssetBase;
  893. if (pAssetBase != NULL)
  894. {
  895. Torque::Path binaryPath = pAssetDefinition->mAssetBaseFilePath;
  896. binaryPath.setExtension(mTaml.getAutoFormatBinaryExtension());
  897. // Save asset.
  898. mTaml.write(pAssetBase, binaryPath.getFullPath().c_str());
  899. }
  900. else
  901. {
  902. Con::warnf("Asset Manager::compileAsset Failed to compile asset Id '%s' as it does not have an assetBase", pAssetId);
  903. return false;
  904. }
  905. // Info.
  906. if (mEchoInfo)
  907. {
  908. Con::printSeparator();
  909. Con::printf("Asset Manager: Finished compiling Asset Id '%s'.", pAssetId);
  910. }
  911. return true;
  912. }
  913. //-----------------------------------------------------------------------------
  914. bool AssetManager::releaseAsset( const char* pAssetId )
  915. {
  916. // Debug Profiling.
  917. PROFILE_SCOPE(AssetManager_ReleaseAsset);
  918. // Sanity!
  919. AssertFatal( pAssetId != NULL, "Cannot release NULL asset Id." );
  920. // Find asset.
  921. AssetDefinition* pAssetDefinition = findAsset( pAssetId );
  922. // Did we find the asset?
  923. if ( pAssetDefinition == NULL )
  924. {
  925. // No, so warn.
  926. Con::warnf( "Asset Manager: Failed to release asset Id '%s' as it does not exist.", pAssetId );
  927. return false;
  928. }
  929. // Is the asset loaded?
  930. if ( pAssetDefinition->mpAssetBase == NULL )
  931. {
  932. // No, so warn.
  933. Con::warnf( "Asset Manager: Failed to release asset Id '%s' as it is not acquired.", pAssetId );
  934. return false;
  935. }
  936. // Info.
  937. if ( mEchoInfo )
  938. {
  939. Con::printSeparator();
  940. Con::printf( "Asset Manager: Started releasing Asset Id '%s'...", pAssetId );
  941. }
  942. // Release asset reference.
  943. if ( pAssetDefinition->mpAssetBase->releaseAssetReference() )
  944. {
  945. // Are we ignoring auto-unloaded assets?
  946. if ( mIgnoreAutoUnload )
  947. {
  948. // Yes, so info.
  949. if ( mEchoInfo )
  950. {
  951. Con::printf( "Asset Manager: > Releasing to idle state." );
  952. }
  953. }
  954. else
  955. {
  956. // No, so info.
  957. if ( mEchoInfo )
  958. {
  959. Con::printf( "Asset Manager: > Unload the asset from memory." );
  960. }
  961. // Unload the asset.
  962. unloadAsset( pAssetDefinition );
  963. }
  964. }
  965. // Info.
  966. else if ( mEchoInfo )
  967. {
  968. Con::printf( "Asset Manager: > Reference count now '%d'.", pAssetDefinition->mpAssetBase->getAcquiredReferenceCount() );
  969. }
  970. // Info.
  971. if ( mEchoInfo )
  972. {
  973. Con::printf( "Asset Manager: > Finished releasing Asset Id '%s'.", pAssetId );
  974. Con::printSeparator();
  975. }
  976. return true;
  977. }
  978. //-----------------------------------------------------------------------------
  979. void AssetManager::purgeAssets( void )
  980. {
  981. // Debug Profiling.
  982. PROFILE_SCOPE(AssetManager_PurgeAssets);
  983. // Info.
  984. if ( mEchoInfo )
  985. {
  986. Con::printf( "Asset Manager: Started purging assets..." );
  987. }
  988. // Iterate asset definitions.
  989. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  990. {
  991. // Fetch asset definition.
  992. AssetDefinition* pAssetDefinition = assetItr->value;
  993. // Skip asset if private, not loaded or referenced.
  994. if ( pAssetDefinition->mAssetPrivate ||
  995. pAssetDefinition->mpAssetBase == NULL ||
  996. pAssetDefinition->mpAssetBase->getAcquiredReferenceCount() > 0 )
  997. continue;
  998. // Info.
  999. if ( mEchoInfo )
  1000. {
  1001. Con::printf( "Asset Manager: Purging asset Id '%s'...", pAssetDefinition->mAssetId );
  1002. }
  1003. // Unload the asset.
  1004. unloadAsset( pAssetDefinition );
  1005. }
  1006. // Info.
  1007. if ( mEchoInfo )
  1008. {
  1009. Con::printf( "Asset Manager: ... Finished purging assets." );
  1010. }
  1011. }
  1012. //-----------------------------------------------------------------------------
  1013. bool AssetManager::deleteAsset( const char* pAssetId, const bool deleteLooseFiles, const bool deleteDependencies )
  1014. {
  1015. // Debug Profiling.
  1016. PROFILE_SCOPE(AssetManager_DeleteAsset);
  1017. // Sanity!
  1018. AssertFatal( pAssetId != NULL, "Cannot delete NULL asset Id." );
  1019. // Find asset.
  1020. AssetDefinition* pAssetDefinition = findAsset( pAssetId );
  1021. // Did we find the asset?
  1022. if ( pAssetDefinition == NULL )
  1023. {
  1024. // No, so warn.
  1025. Con::warnf( "Asset Manager: Failed to delete asset Id '%s' as it does not exist.", pAssetId );
  1026. return false;
  1027. }
  1028. // Info.
  1029. if ( mEchoInfo )
  1030. {
  1031. Con::printSeparator();
  1032. Con::printf( "Asset Manager: Started deleting Asset Id '%s'...", pAssetId );
  1033. }
  1034. // Fetch asset Id.
  1035. StringTableEntry assetId = StringTable->insert( pAssetId );
  1036. // Are we deleting dependencies?
  1037. if ( deleteDependencies )
  1038. {
  1039. Vector<typeAssetId> dependantAssets;
  1040. // Yes, so find depended-on-by entry.
  1041. typeAssetDependsOnHash::Iterator dependedOnItr = mAssetIsDependedOn.find( assetId );
  1042. // Iterate all dependencies.
  1043. while( dependedOnItr != mAssetIsDependedOn.end() && dependedOnItr->key == assetId )
  1044. {
  1045. // Store asset Id.
  1046. dependantAssets.push_back( dependedOnItr->value );
  1047. // Next dependency.
  1048. dependedOnItr++;
  1049. }
  1050. // Do we have any dependants?
  1051. if ( dependantAssets.size() > 0 )
  1052. {
  1053. // Yes, so iterate dependants.
  1054. for( Vector<typeAssetId>::const_iterator assetItr = dependantAssets.begin(); assetItr != dependantAssets.end(); ++assetItr )
  1055. {
  1056. StringTableEntry dependentAssetId = *assetItr;
  1057. // Info.
  1058. if ( mEchoInfo )
  1059. {
  1060. Con::printSeparator();
  1061. Con::printf( "Asset Manager: Deleting Asset Id '%s' dependant of '%s.'", pAssetId, dependentAssetId );
  1062. }
  1063. // Delete dependant.
  1064. deleteAsset( dependentAssetId, deleteLooseFiles, deleteDependencies );
  1065. }
  1066. }
  1067. }
  1068. // Remove asset references.
  1069. removeAssetReferences( assetId );
  1070. // Are we deleting loose files?
  1071. if ( deleteLooseFiles )
  1072. {
  1073. // Yes, so remove loose files.
  1074. Vector<StringTableEntry>& assetLooseFiles = pAssetDefinition->mAssetLooseFiles;
  1075. for( Vector<StringTableEntry>::iterator looseFileItr = assetLooseFiles.begin(); looseFileItr != assetLooseFiles.end(); ++looseFileItr )
  1076. {
  1077. // Fetch loose file.
  1078. StringTableEntry looseFile = *looseFileItr;
  1079. // Delete the loose file.
  1080. if ( !dFileDelete( looseFile ) )
  1081. {
  1082. // Failed so warn.
  1083. Con::warnf( "Asset Manager: Failed to delete the loose file '%s' while deleting asset Id '%s'.", looseFile, pAssetId );
  1084. }
  1085. }
  1086. }
  1087. // Fetch asset definition file.
  1088. StringTableEntry assetDefinitionFile = pAssetDefinition->mAssetBaseFilePath;
  1089. // Remove reference here as we're about to remove the declared asset.
  1090. pAssetDefinition = NULL;
  1091. // Remove asset.
  1092. removeDeclaredAsset( pAssetId );
  1093. // Delete the asset definition file.
  1094. if ( !dFileDelete( assetDefinitionFile ) )
  1095. {
  1096. // Failed so warn.
  1097. Con::warnf( "Asset Manager: Failed to delete the asset definition file '%s' while deleting asset Id '%s'.", assetDefinitionFile, pAssetId );
  1098. }
  1099. // Info.
  1100. if ( mEchoInfo )
  1101. {
  1102. Con::printSeparator();
  1103. Con::printf( "Asset Manager: Finished deleting Asset Id '%s'.", pAssetId );
  1104. }
  1105. return true;
  1106. }
  1107. //-----------------------------------------------------------------------------
  1108. bool AssetManager::refreshAsset( const char* pAssetId )
  1109. {
  1110. // Debug Profiling.
  1111. PROFILE_SCOPE(AssetManager_RefreshAsset);
  1112. // Sanity!
  1113. AssertFatal( pAssetId != NULL, "Cannot refresh NULL asset Id." );
  1114. // Find asset.
  1115. AssetDefinition* pAssetDefinition = findAsset( pAssetId );
  1116. // Did we find the asset?
  1117. if ( pAssetDefinition == NULL )
  1118. {
  1119. // No, so warn.
  1120. Con::warnf( "Asset Manager: Failed to refresh asset Id '%s' as it does not exist.", pAssetId );
  1121. return false;
  1122. }
  1123. // Info.
  1124. if ( mEchoInfo )
  1125. {
  1126. Con::printSeparator();
  1127. Con::printf( "Asset Manager: Started refreshing Asset Id '%s'...", pAssetId );
  1128. }
  1129. // Fetch asset Id.
  1130. StringTableEntry assetId = StringTable->insert( pAssetId );
  1131. // Is the asset private?
  1132. if ( pAssetDefinition->mAssetPrivate )
  1133. {
  1134. // Yes, so notify asset of asset refresh only.
  1135. pAssetDefinition->mpAssetBase->onAssetRefresh();
  1136. // Asset refresh notifications.
  1137. for( typeAssetPtrRefreshHash::iterator refreshNotifyItr = mAssetPtrRefreshNotifications.begin(); refreshNotifyItr != mAssetPtrRefreshNotifications.end(); ++refreshNotifyItr )
  1138. {
  1139. // Fetch pointed asset.
  1140. StringTableEntry pointedAsset = refreshNotifyItr->key->getAssetId();
  1141. // Ignore if the pointed asset is not the asset or a dependency.
  1142. if ( pointedAsset == StringTable->EmptyString() || ( pointedAsset != assetId && !doesAssetDependOn( pointedAsset, assetId ) ) )
  1143. continue;
  1144. // Perform refresh notification callback.
  1145. refreshNotifyItr->value->onAssetRefreshed( refreshNotifyItr->key );
  1146. }
  1147. }
  1148. // Is the asset definition allowed to refresh?
  1149. else if ( pAssetDefinition->mAssetRefreshEnable )
  1150. {
  1151. // Yes, so fetch the asset.
  1152. AssetBase* pAssetBase = pAssetDefinition->mpAssetBase;
  1153. // Is the asset loaded?
  1154. if ( pAssetBase != NULL )
  1155. {
  1156. // Yes, so notify asset of asset refresh.
  1157. pAssetBase->onAssetRefresh();
  1158. // Save asset.
  1159. mTaml.write( pAssetBase, pAssetDefinition->mAssetBaseFilePath );
  1160. // Remove asset dependencies.
  1161. removeAssetDependencies( pAssetId );
  1162. // Find any new dependencies.
  1163. TamlAssetDeclaredVisitor assetDeclaredVisitor;
  1164. // Parse the filename.
  1165. if ( !mTaml.parse( pAssetDefinition->mAssetBaseFilePath, assetDeclaredVisitor ) )
  1166. {
  1167. // Warn.
  1168. Con::warnf( "Asset Manager: Failed to parse file containing asset declaration: '%s'.\nDependencies are now incorrect!", pAssetDefinition->mAssetBaseFilePath );
  1169. return false;
  1170. }
  1171. // Fetch asset dependencies.
  1172. TamlAssetDeclaredVisitor::typeAssetIdVector& assetDependencies = assetDeclaredVisitor.getAssetDependencies();
  1173. // Are there any asset dependences?
  1174. if ( assetDependencies.size() > 0 )
  1175. {
  1176. // Yes, so iterate dependencies.
  1177. for( TamlAssetDeclaredVisitor::typeAssetIdVector::iterator assetDependencyItr = assetDependencies.begin(); assetDependencyItr != assetDependencies.end(); ++assetDependencyItr )
  1178. {
  1179. // Fetch dependency asset Id.
  1180. StringTableEntry dependencyAssetId = *assetDependencyItr;
  1181. // Insert depends-on.
  1182. mAssetDependsOn.insertEqual( assetId, dependencyAssetId );
  1183. // Insert is-depended-on.
  1184. mAssetIsDependedOn.insertEqual( dependencyAssetId, assetId );
  1185. }
  1186. }
  1187. // Fetch asset loose files.
  1188. TamlAssetDeclaredVisitor::typeLooseFileVector& assetLooseFiles = assetDeclaredVisitor.getAssetLooseFiles();
  1189. // Clear any existing loose files.
  1190. pAssetDefinition->mAssetLooseFiles.clear();
  1191. // Are there any loose files?
  1192. if ( assetLooseFiles.size() > 0 )
  1193. {
  1194. // Yes, so iterate loose files.
  1195. for( TamlAssetDeclaredVisitor::typeLooseFileVector::iterator assetLooseFileItr = assetLooseFiles.begin(); assetLooseFileItr != assetLooseFiles.end(); ++assetLooseFileItr )
  1196. {
  1197. // Store loose file.
  1198. pAssetDefinition->mAssetLooseFiles.push_back( *assetLooseFileItr );
  1199. }
  1200. }
  1201. // Asset refresh notifications.
  1202. for( typeAssetPtrRefreshHash::iterator refreshNotifyItr = mAssetPtrRefreshNotifications.begin(); refreshNotifyItr != mAssetPtrRefreshNotifications.end(); ++refreshNotifyItr )
  1203. {
  1204. // Fetch pointed asset.
  1205. StringTableEntry pointedAsset = refreshNotifyItr->key->getAssetId();
  1206. // Ignore if the pointed asset is not the asset or a dependency.
  1207. if ( pointedAsset == StringTable->EmptyString() || ( pointedAsset != assetId && !doesAssetDependOn( pointedAsset, assetId ) ) )
  1208. continue;
  1209. // Perform refresh notification callback.
  1210. refreshNotifyItr->value->onAssetRefreshed( refreshNotifyItr->key );
  1211. }
  1212. // Find is-depends-on entry.
  1213. typeAssetIsDependedOnHash::Iterator isDependedOnItr = mAssetIsDependedOn.find( assetId );
  1214. // Is asset depended on?
  1215. if ( isDependedOnItr != mAssetIsDependedOn.end() )
  1216. {
  1217. // Yes, so compiled them.
  1218. Vector<typeAssetId> dependedOn;
  1219. // Iterate all dependencies.
  1220. while( isDependedOnItr != mAssetIsDependedOn.end() && isDependedOnItr->key == assetId )
  1221. {
  1222. dependedOn.push_back( isDependedOnItr->value );
  1223. // Next dependency.
  1224. isDependedOnItr++;
  1225. }
  1226. // Refresh depended-on assets.
  1227. for ( Vector<typeAssetId>::iterator refreshItr = dependedOn.begin(); refreshItr != dependedOn.end(); ++refreshItr)
  1228. {
  1229. // Refresh dependency asset.
  1230. refreshAsset( *refreshItr);
  1231. }
  1232. }
  1233. }
  1234. }
  1235. // Info.
  1236. if ( mEchoInfo )
  1237. {
  1238. Con::printSeparator();
  1239. Con::printf( "Asset Manager: Finished refreshing Asset Id '%s'.", pAssetId );
  1240. }
  1241. return true;
  1242. }
  1243. //-----------------------------------------------------------------------------
  1244. void AssetManager::refreshAllAssets( const bool includeUnloaded )
  1245. {
  1246. // Debug Profiling.
  1247. PROFILE_SCOPE(AssetManager_RefreshAllAssets);
  1248. // Info.
  1249. if ( mEchoInfo )
  1250. {
  1251. Con::printSeparator();
  1252. Con::printf( "Asset Manager: Started refreshing ALL assets." );
  1253. }
  1254. Vector<typeAssetId> assetsToRelease;
  1255. // Are we including unloaded assets?
  1256. if ( includeUnloaded )
  1257. {
  1258. // Yes, so prepare a list of assets to release and load them.
  1259. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1260. {
  1261. // Fetch asset Id.
  1262. typeAssetId assetId = assetItr->key;
  1263. // Skip if asset is loaded.
  1264. if ( assetItr->value->mpAssetBase != NULL )
  1265. continue;
  1266. // Note asset as needing a release.
  1267. assetsToRelease.push_back( assetId );
  1268. // Acquire the asset.
  1269. acquireAsset<AssetBase>( assetId );
  1270. }
  1271. }
  1272. // Refresh the current loaded assets.
  1273. // NOTE: This will result in some assets being refreshed more than once due to asset dependencies.
  1274. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1275. {
  1276. // Skip private assets.
  1277. if ( assetItr->value->mAssetPrivate )
  1278. continue;
  1279. // Refresh asset if it's loaded.
  1280. refreshAsset( assetItr->key );
  1281. }
  1282. // Are we including unloaded assets?
  1283. if ( includeUnloaded )
  1284. {
  1285. // Yes, so release the assets we loaded.
  1286. for( Vector<typeAssetId>::iterator assetItr = assetsToRelease.begin(); assetItr != assetsToRelease.end(); ++assetItr )
  1287. {
  1288. releaseAsset( *assetItr );
  1289. }
  1290. }
  1291. // Info.
  1292. if ( mEchoInfo )
  1293. {
  1294. Con::printSeparator();
  1295. Con::printf( "Asset Manager: Finished refreshing ALL assets." );
  1296. }
  1297. }
  1298. //-----------------------------------------------------------------------------
  1299. void AssetManager::registerAssetPtrRefreshNotify( AssetPtrBase* pAssetPtrBase, AssetPtrCallback* pCallback )
  1300. {
  1301. // Find an existing notification iterator.
  1302. typeAssetPtrRefreshHash::iterator notificationItr = mAssetPtrRefreshNotifications.find( pAssetPtrBase );
  1303. // Do we have one?
  1304. if ( notificationItr != mAssetPtrRefreshNotifications.end() )
  1305. {
  1306. // Yes, so update the callback.
  1307. notificationItr->value = pCallback;
  1308. return;
  1309. }
  1310. // No, so add one.
  1311. mAssetPtrRefreshNotifications.insert( pAssetPtrBase, pCallback );
  1312. }
  1313. //-----------------------------------------------------------------------------
  1314. void AssetManager::unregisterAssetPtrRefreshNotify( AssetPtrBase* pAssetPtrBase )
  1315. {
  1316. mAssetPtrRefreshNotifications.erase( pAssetPtrBase );
  1317. }
  1318. //-----------------------------------------------------------------------------
  1319. bool AssetManager::loadAssetTags( ModuleDefinition* pModuleDefinition )
  1320. {
  1321. // Sanity!
  1322. AssertFatal( pModuleDefinition != NULL, "Cannot load asset tags manifest using a NULL module definition" );
  1323. // Expand manifest location.
  1324. char assetTagsManifestFilePathBuffer[1024];
  1325. Con::expandPath( assetTagsManifestFilePathBuffer, sizeof(assetTagsManifestFilePathBuffer), pModuleDefinition->getAssetTagsManifest() );
  1326. // Do we already have a manifest?
  1327. if ( !mAssetTagsManifest.isNull() )
  1328. {
  1329. // Yes, so warn.
  1330. Con::warnf( "Asset Manager: Cannot load asset tags manifest from module '%s' as one is already loaded.", pModuleDefinition->getSignature() );
  1331. return false;
  1332. }
  1333. // Is the specified file valid?
  1334. if (Torque::FS::IsFile( assetTagsManifestFilePathBuffer ) )
  1335. {
  1336. // Yes, so read asset tags manifest.
  1337. mAssetTagsManifest = mTaml.read<AssetTagsManifest>( assetTagsManifestFilePathBuffer );
  1338. // Did we read the manifest?
  1339. if ( mAssetTagsManifest.isNull() )
  1340. {
  1341. // No, so warn.
  1342. Con::warnf( "Asset Manager: Failed to load asset tags manifest '%s' from module '%s'.", assetTagsManifestFilePathBuffer, pModuleDefinition->getSignature() );
  1343. return false;
  1344. }
  1345. // Set asset tags module definition.
  1346. mAssetTagsModuleDefinition = pModuleDefinition;
  1347. }
  1348. else
  1349. {
  1350. // No, so generate a new asset tags manifest.
  1351. mAssetTagsManifest = new AssetTagsManifest();
  1352. mAssetTagsManifest->registerObject();
  1353. // Set asset tags module definition.
  1354. mAssetTagsModuleDefinition = pModuleDefinition;
  1355. // Save the asset tags.
  1356. saveAssetTags();
  1357. }
  1358. return true;
  1359. }
  1360. //-----------------------------------------------------------------------------
  1361. bool AssetManager::saveAssetTags( void )
  1362. {
  1363. // Do we have an asset tags manifest?
  1364. if ( mAssetTagsManifest.isNull() || mAssetTagsModuleDefinition.isNull() )
  1365. {
  1366. // No, so warn.
  1367. Con::warnf( "Asset Manager: Failed to save asset tags manifest as one is not loaded." );
  1368. return false;
  1369. }
  1370. // Expand manifest location.
  1371. char assetTagsManifestFilePathBuffer[1024];
  1372. Con::expandPath( assetTagsManifestFilePathBuffer, sizeof(assetTagsManifestFilePathBuffer), mAssetTagsModuleDefinition->getAssetTagsManifest() );
  1373. // Save asset tags manifest.
  1374. if ( !mTaml.write( mAssetTagsManifest, assetTagsManifestFilePathBuffer ) )
  1375. {
  1376. // Failed so warn.
  1377. Con::warnf( "Asset Manager: Failed to save asset tags manifest '%s' from module '%s'.", assetTagsManifestFilePathBuffer, mAssetTagsModuleDefinition->getSignature() );
  1378. return false;
  1379. }
  1380. return true;
  1381. }
  1382. //-----------------------------------------------------------------------------
  1383. bool AssetManager::restoreAssetTags( void )
  1384. {
  1385. // Do we already have a manifest?
  1386. if ( mAssetTagsManifest.isNull() )
  1387. {
  1388. // No, so warn.
  1389. Con::warnf( "Asset Manager: Cannot restore asset tags manifest as one is not already loaded." );
  1390. return false;
  1391. }
  1392. // Sanity!
  1393. AssertFatal( mAssetTagsModuleDefinition != NULL, "Cannot restore asset tags manifest as module definition is NULL." );
  1394. // Delete existing asset tags manifest.
  1395. mAssetTagsManifest->deleteObject();
  1396. // Reload asset tags manifest.
  1397. return loadAssetTags( mAssetTagsModuleDefinition );
  1398. }
  1399. //-----------------------------------------------------------------------------
  1400. const char* AssetManager::getAssetLooseFiles(const char* pAssetId)
  1401. {
  1402. // Debug Profiling.
  1403. PROFILE_SCOPE(AssetManager_getAssetLooseFIles);
  1404. // Sanity!
  1405. AssertFatal(pAssetId != NULL, "Cannot look up NULL asset Id.");
  1406. // Find asset.
  1407. AssetDefinition* pAssetDefinition = findAsset(pAssetId);
  1408. // Did we find the asset?
  1409. if (pAssetDefinition == NULL)
  1410. {
  1411. // No, so warn.
  1412. Con::warnf("Asset Manager: Failed to find asset Id '%s' as it does not exist.", pAssetId);
  1413. return String::EmptyString;
  1414. }
  1415. // Info.
  1416. if (mEchoInfo)
  1417. {
  1418. Con::printSeparator();
  1419. Con::printf("Asset Manager: Started getting loose files of Asset Id '%s'...", pAssetId);
  1420. }
  1421. String looseFileList = "";
  1422. Vector<StringTableEntry>& assetLooseFiles = pAssetDefinition->mAssetLooseFiles;
  1423. for (Vector<StringTableEntry>::iterator looseFileItr = assetLooseFiles.begin(); looseFileItr != assetLooseFiles.end(); ++looseFileItr)
  1424. {
  1425. // Fetch loose file.
  1426. StringTableEntry looseFile = *looseFileItr;
  1427. looseFileList += looseFile;
  1428. if (looseFileItr != assetLooseFiles.end())
  1429. looseFileList += "\t";
  1430. }
  1431. char* ret = Con::getReturnBuffer(1024);
  1432. dSprintf(ret, 1024, "%s", looseFileList.c_str());
  1433. return ret;
  1434. }
  1435. //-----------------------------------------------------------------------------
  1436. S32 QSORT_CALLBACK descendingAssetDefinitionLoadCount(const void* a, const void* b)
  1437. {
  1438. // Debug Profiling.
  1439. PROFILE_SCOPE(AssetManager_DescendingAssetDefinitionLoadCount);
  1440. // Fetch asset definitions.
  1441. const AssetDefinition* pAssetDefinitionA = *(AssetDefinition**)a;
  1442. const AssetDefinition* pAssetDefinitionB = *(AssetDefinition**)b;
  1443. // Sort.
  1444. return pAssetDefinitionB->mAssetLoadedCount - pAssetDefinitionA->mAssetLoadedCount;
  1445. }
  1446. //-----------------------------------------------------------------------------
  1447. void AssetManager::dumpDeclaredAssets( void ) const
  1448. {
  1449. Vector<const AssetDefinition*> assetDefinitions;
  1450. // Iterate asset definitions.
  1451. for( typeDeclaredAssetsHash::const_iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1452. {
  1453. assetDefinitions.push_back( assetItr->value );
  1454. }
  1455. // Sort asset definitions.
  1456. dQsort( assetDefinitions.address(), assetDefinitions.size(), sizeof(const AssetDefinition*), descendingAssetDefinitionLoadCount );
  1457. // Info.
  1458. Con::printSeparator();
  1459. Con::printf( "Asset Manager: %d declared asset(s) dump as follows:", mDeclaredAssets.size() );
  1460. Con::printBlankLine();
  1461. // Iterate sorted asset definitions.
  1462. for ( Vector<const AssetDefinition*>::iterator assetItr = assetDefinitions.begin(); assetItr != assetDefinitions.end(); ++assetItr )
  1463. {
  1464. // Fetch asset definition.
  1465. const AssetDefinition* pAssetDefinition = *assetItr;
  1466. // Info.
  1467. 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'",
  1468. pAssetDefinition->mAssetId,
  1469. pAssetDefinition->mpAssetBase == NULL ? 0 : pAssetDefinition->mpAssetBase->getAcquiredReferenceCount(),
  1470. pAssetDefinition->mAssetLoadedCount,
  1471. pAssetDefinition->mAssetUnloadedCount,
  1472. pAssetDefinition->mAssetAutoUnload,
  1473. pAssetDefinition->mpAssetBase != NULL,
  1474. pAssetDefinition->mAssetInternal,
  1475. pAssetDefinition->mAssetPrivate,
  1476. pAssetDefinition->mAssetType,
  1477. pAssetDefinition->mAssetPrivate ? "Private" : pAssetDefinition->mpModuleDefinition->getModuleId(),
  1478. pAssetDefinition->mAssetPrivate ? 0 : pAssetDefinition->mpModuleDefinition->getVersionId(),
  1479. pAssetDefinition->mAssetBaseFilePath );
  1480. }
  1481. // Info.
  1482. Con::printSeparator();
  1483. Con::printBlankLine();
  1484. }
  1485. //-----------------------------------------------------------------------------
  1486. S32 AssetManager::findAllAssets( AssetQuery* pAssetQuery, const bool ignoreInternal, const bool ignorePrivate )
  1487. {
  1488. // Debug Profiling.
  1489. PROFILE_SCOPE(AssetManager_FindAllAssets);
  1490. // Sanity!
  1491. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  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. // Skip if internal and we're ignoring them.
  1500. if ( ignoreInternal && pAssetDefinition->mAssetInternal )
  1501. continue;
  1502. // Skip if private and we're ignoring them.
  1503. if ( ignorePrivate && pAssetDefinition->mAssetPrivate )
  1504. continue;
  1505. // Store as result.
  1506. pAssetQuery->mAssetList.push_back( pAssetDefinition->mAssetId );
  1507. // Increase result count.
  1508. resultCount++;
  1509. }
  1510. return resultCount;
  1511. }
  1512. //-----------------------------------------------------------------------------
  1513. S32 AssetManager::findAssetName( AssetQuery* pAssetQuery, const char* pAssetName, const bool partialName )
  1514. {
  1515. // Debug Profiling.
  1516. PROFILE_SCOPE(AssetManager_FindAssetName);
  1517. // Sanity!
  1518. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1519. AssertFatal( pAssetName != NULL, "Cannot use NULL asset name." );
  1520. // Reset asset name.
  1521. StringTableEntry assetName = NULL;
  1522. S32 partialAssetNameLength = 0;
  1523. // Are we doing partial name search?
  1524. if ( partialName )
  1525. {
  1526. // Yes, so fetch length of partial name.
  1527. partialAssetNameLength = dStrlen( pAssetName );
  1528. }
  1529. else
  1530. {
  1531. // No, so fetch asset name.
  1532. assetName = StringTable->insert( pAssetName );
  1533. }
  1534. // Reset result count.
  1535. S32 resultCount = 0;
  1536. // Iterate declared assets.
  1537. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1538. {
  1539. // Fetch asset definition.
  1540. AssetDefinition* pAssetDefinition = assetItr->value;
  1541. // Are we doing partial name search?
  1542. if ( partialName )
  1543. {
  1544. // Yes, so fetch the length of this asset name.
  1545. const S32 currentAssetNameLength = dStrlen( pAssetDefinition->mAssetName );
  1546. // Skip if the query asset name is longer than the current asset name.
  1547. if ( partialAssetNameLength > currentAssetNameLength )
  1548. continue;
  1549. // Skip if this is not the asset we want.
  1550. if ( dStrnicmp( pAssetDefinition->mAssetName, pAssetName, partialAssetNameLength ) != 0 )
  1551. continue;
  1552. }
  1553. else
  1554. {
  1555. // No, so skip if this is not the asset we want.
  1556. if ( assetName != pAssetDefinition->mAssetName )
  1557. continue;
  1558. }
  1559. // Store as result.
  1560. pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
  1561. // Increase result count.
  1562. resultCount++;
  1563. }
  1564. return resultCount;
  1565. }
  1566. //-----------------------------------------------------------------------------
  1567. S32 AssetManager::findAssetCategory( AssetQuery* pAssetQuery, const char* pAssetCategory, const bool assetQueryAsSource )
  1568. {
  1569. // Debug Profiling.
  1570. PROFILE_SCOPE(AssetManager_FindAssetCategory);
  1571. // Sanity!
  1572. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1573. AssertFatal( pAssetCategory != NULL, "Cannot use NULL asset category." );
  1574. // Fetch asset category.
  1575. StringTableEntry assetCategory = StringTable->insert( pAssetCategory );
  1576. // Reset result count.
  1577. S32 resultCount = 0;
  1578. // Use asset-query as the source?
  1579. if ( assetQueryAsSource )
  1580. {
  1581. AssetQuery filteredAssets;
  1582. // Yes, so iterate asset query.
  1583. for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
  1584. {
  1585. // Fetch asset definition.
  1586. AssetDefinition* pAssetDefinition = findAsset( *assetItr );
  1587. // Skip if this is not the asset we want.
  1588. if ( pAssetDefinition == NULL ||
  1589. pAssetDefinition->mAssetCategory != assetCategory )
  1590. continue;
  1591. // Store as result.
  1592. filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
  1593. // Increase result count.
  1594. resultCount++;
  1595. }
  1596. // Set asset query.
  1597. pAssetQuery->set( filteredAssets );
  1598. }
  1599. else
  1600. {
  1601. // No, so iterate declared assets.
  1602. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1603. {
  1604. // Fetch asset definition.
  1605. AssetDefinition* pAssetDefinition = assetItr->value;
  1606. // Skip if this is not the asset we want.
  1607. if ( assetCategory != pAssetDefinition->mAssetCategory )
  1608. continue;
  1609. // Store as result.
  1610. pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
  1611. // Increase result count.
  1612. resultCount++;
  1613. }
  1614. }
  1615. return resultCount;
  1616. }
  1617. S32 AssetManager::findAssetAutoUnload( AssetQuery* pAssetQuery, const bool assetAutoUnload, const bool assetQueryAsSource )
  1618. {
  1619. // Debug Profiling.
  1620. PROFILE_SCOPE(AssetManager_FindAssetAutoUnload);
  1621. // Sanity!
  1622. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1623. // Reset result count.
  1624. S32 resultCount = 0;
  1625. // Use asset-query as the source?
  1626. if ( assetQueryAsSource )
  1627. {
  1628. AssetQuery filteredAssets;
  1629. // Yes, so iterate asset query.
  1630. for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
  1631. {
  1632. // Fetch asset definition.
  1633. AssetDefinition* pAssetDefinition = findAsset( *assetItr );
  1634. // Skip if this is not the asset we want.
  1635. if ( pAssetDefinition == NULL ||
  1636. pAssetDefinition->mAssetAutoUnload != assetAutoUnload )
  1637. continue;
  1638. // Store as result.
  1639. filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
  1640. // Increase result count.
  1641. resultCount++;
  1642. }
  1643. // Set asset query.
  1644. pAssetQuery->set( filteredAssets );
  1645. }
  1646. else
  1647. {
  1648. // No, so iterate declared assets.
  1649. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1650. {
  1651. // Fetch asset definition.
  1652. AssetDefinition* pAssetDefinition = assetItr->value;
  1653. // Skip if this is not the asset we want.
  1654. if ( assetAutoUnload != pAssetDefinition->mAssetAutoUnload )
  1655. continue;
  1656. // Store as result.
  1657. pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
  1658. // Increase result count.
  1659. resultCount++;
  1660. }
  1661. }
  1662. return resultCount;
  1663. }
  1664. //-----------------------------------------------------------------------------
  1665. S32 AssetManager::findAssetInternal( AssetQuery* pAssetQuery, const bool assetInternal, const bool assetQueryAsSource )
  1666. {
  1667. // Debug Profiling.
  1668. PROFILE_SCOPE(AssetManager_FindAssetInternal);
  1669. // Sanity!
  1670. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1671. // Reset result count.
  1672. S32 resultCount = 0;
  1673. // Use asset-query as the source?
  1674. if ( assetQueryAsSource )
  1675. {
  1676. AssetQuery filteredAssets;
  1677. // Yes, so iterate asset query.
  1678. for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
  1679. {
  1680. // Fetch asset definition.
  1681. AssetDefinition* pAssetDefinition = findAsset( *assetItr );
  1682. // Skip if this is not the asset we want.
  1683. if ( pAssetDefinition == NULL ||
  1684. pAssetDefinition->mAssetInternal != assetInternal )
  1685. continue;
  1686. // Store as result.
  1687. filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
  1688. // Increase result count.
  1689. resultCount++;
  1690. }
  1691. // Set asset query.
  1692. pAssetQuery->set( filteredAssets );
  1693. }
  1694. else
  1695. {
  1696. // No, so iterate declared assets.
  1697. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1698. {
  1699. // Fetch asset definition.
  1700. AssetDefinition* pAssetDefinition = assetItr->value;
  1701. // Skip if this is not the asset we want.
  1702. if ( assetInternal != pAssetDefinition->mAssetInternal )
  1703. continue;
  1704. // Store as result.
  1705. pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
  1706. // Increase result count.
  1707. resultCount++;
  1708. }
  1709. }
  1710. return resultCount;
  1711. }
  1712. //-----------------------------------------------------------------------------
  1713. S32 AssetManager::findAssetPrivate( AssetQuery* pAssetQuery, const bool assetPrivate, const bool assetQueryAsSource )
  1714. {
  1715. // Debug Profiling.
  1716. PROFILE_SCOPE(AssetManager_FindAssetPrivate);
  1717. // Sanity!
  1718. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1719. // Reset result count.
  1720. S32 resultCount = 0;
  1721. // Use asset-query as the source?
  1722. if ( assetQueryAsSource )
  1723. {
  1724. AssetQuery filteredAssets;
  1725. // Yes, so iterate asset query.
  1726. for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
  1727. {
  1728. // Fetch asset definition.
  1729. AssetDefinition* pAssetDefinition = findAsset( *assetItr );
  1730. // Skip if this is not the asset we want.
  1731. if ( pAssetDefinition == NULL ||
  1732. pAssetDefinition->mAssetPrivate != assetPrivate )
  1733. continue;
  1734. // Store as result.
  1735. filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
  1736. // Increase result count.
  1737. resultCount++;
  1738. }
  1739. // Set asset query.
  1740. pAssetQuery->set( filteredAssets );
  1741. }
  1742. else
  1743. {
  1744. // No, so iterate declared assets.
  1745. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1746. {
  1747. // Fetch asset definition.
  1748. AssetDefinition* pAssetDefinition = assetItr->value;
  1749. // Skip if this is not the asset we want.
  1750. if ( assetPrivate != pAssetDefinition->mAssetPrivate )
  1751. continue;
  1752. // Store as result.
  1753. pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
  1754. // Increase result count.
  1755. resultCount++;
  1756. }
  1757. }
  1758. return resultCount;
  1759. }
  1760. //-----------------------------------------------------------------------------
  1761. S32 AssetManager::findAssetType( AssetQuery* pAssetQuery, const char* pAssetType, const bool assetQueryAsSource )
  1762. {
  1763. // Debug Profiling.
  1764. PROFILE_SCOPE(AssetManager_FindAssetType);
  1765. // Sanity!
  1766. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1767. AssertFatal( pAssetType != NULL, "Cannot use NULL asset type." );
  1768. // Fetch asset type.
  1769. StringTableEntry assetType = StringTable->insert( pAssetType );
  1770. // Reset result count.
  1771. S32 resultCount = 0;
  1772. // Use asset-query as the source?
  1773. if ( assetQueryAsSource )
  1774. {
  1775. AssetQuery filteredAssets;
  1776. // Yes, so iterate asset query.
  1777. for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
  1778. {
  1779. // Fetch asset definition.
  1780. AssetDefinition* pAssetDefinition = findAsset( *assetItr );
  1781. // Skip if this is not the asset we want.
  1782. if ( pAssetDefinition == NULL ||
  1783. pAssetDefinition->mAssetType != assetType )
  1784. continue;
  1785. // Store as result.
  1786. filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
  1787. // Increase result count.
  1788. resultCount++;
  1789. }
  1790. // Set asset query.
  1791. pAssetQuery->set( filteredAssets );
  1792. }
  1793. else
  1794. {
  1795. // No, so iterate declared assets.
  1796. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  1797. {
  1798. // Fetch asset definition.
  1799. AssetDefinition* pAssetDefinition = assetItr->value;
  1800. // Skip if this is not the asset we want.
  1801. if ( assetType != pAssetDefinition->mAssetType )
  1802. continue;
  1803. // Store as result.
  1804. pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
  1805. // Increase result count.
  1806. resultCount++;
  1807. }
  1808. }
  1809. return resultCount;
  1810. }
  1811. //-----------------------------------------------------------------------------
  1812. S32 AssetManager::findAssetDependsOn( AssetQuery* pAssetQuery, const char* pAssetId )
  1813. {
  1814. // Debug Profiling.
  1815. PROFILE_SCOPE(AssetManager_FindAssetDependsOn);
  1816. // Sanity!
  1817. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1818. AssertFatal( pAssetId != NULL, "Cannot use NULL asset Id." );
  1819. // Fetch asset Id.
  1820. StringTableEntry assetId = StringTable->insert( pAssetId );
  1821. // Reset result count.
  1822. S32 resultCount = 0;
  1823. // Find depends-on entry.
  1824. typeAssetDependsOnHash::Iterator dependsOnItr = mAssetDependsOn.find( assetId );
  1825. // Iterate all dependencies.
  1826. while( dependsOnItr != mAssetDependsOn.end() && dependsOnItr->key == assetId )
  1827. {
  1828. // Store as result.
  1829. pAssetQuery->mAssetList.push_back(dependsOnItr->value);
  1830. // Next dependency.
  1831. dependsOnItr++;
  1832. // Increase result count.
  1833. resultCount++;
  1834. }
  1835. return resultCount;
  1836. }
  1837. //-----------------------------------------------------------------------------
  1838. S32 AssetManager::findAssetIsDependedOn( AssetQuery* pAssetQuery, const char* pAssetId )
  1839. {
  1840. // Debug Profiling.
  1841. PROFILE_SCOPE(AssetManager_FindAssetIsDependedOn);
  1842. // Sanity!
  1843. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1844. AssertFatal( pAssetId != NULL, "Cannot use NULL asset Id." );
  1845. // Fetch asset Id.
  1846. StringTableEntry assetId = StringTable->insert( pAssetId );
  1847. // Reset result count.
  1848. S32 resultCount = 0;
  1849. // Find depended-on entry.
  1850. typeAssetIsDependedOnHash::Iterator dependedOnItr = mAssetIsDependedOn.find( assetId );
  1851. // Iterate all dependencies.
  1852. while( dependedOnItr != mAssetIsDependedOn.end() && dependedOnItr->key == assetId )
  1853. {
  1854. // Store as result.
  1855. pAssetQuery->mAssetList.push_back(dependedOnItr->value);
  1856. // Next dependency.
  1857. dependedOnItr++;
  1858. // Increase result count.
  1859. resultCount++;
  1860. }
  1861. return resultCount;
  1862. }
  1863. //-----------------------------------------------------------------------------
  1864. S32 AssetManager::findInvalidAssetReferences( AssetQuery* pAssetQuery )
  1865. {
  1866. // Debug Profiling.
  1867. PROFILE_SCOPE(AssetManager_FindInvalidAssetReferences);
  1868. // Sanity!
  1869. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1870. // Reset result count.
  1871. S32 resultCount = 0;
  1872. // Iterate referenced assets.
  1873. for( typeReferencedAssetsHash::Iterator assetItr = mReferencedAssets.begin(); assetItr != mReferencedAssets.end(); ++assetItr )
  1874. {
  1875. // Find asset definition.
  1876. AssetDefinition* pAssetDefinition = findAsset( assetItr->key );
  1877. // Skip if the asset definition was found.
  1878. if ( pAssetDefinition != NULL )
  1879. continue;
  1880. // Store as result.
  1881. pAssetQuery->mAssetList.push_back(assetItr->key);
  1882. // Increase result count.
  1883. resultCount++;
  1884. }
  1885. return resultCount;
  1886. }
  1887. //-----------------------------------------------------------------------------
  1888. S32 AssetManager::findTaggedAssets( AssetQuery* pAssetQuery, const char* pAssetTagNames, const bool assetQueryAsSource )
  1889. {
  1890. // Debug Profiling.
  1891. PROFILE_SCOPE(AssetManager_FindTaggedAssets);
  1892. // Sanity!
  1893. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  1894. AssertFatal( pAssetTagNames != NULL, "Cannot use NULL asset tag name(s)." );
  1895. // Do we have an asset tag manifest?
  1896. if ( mAssetTagsManifest.isNull() )
  1897. {
  1898. // No, so warn.
  1899. Con::warnf( "Asset Manager: Cannot find tagged assets as no asset tag manifest is present." );
  1900. return 0;
  1901. }
  1902. // Reset result count.
  1903. S32 resultCount = 0;
  1904. const char* pTagSeparators = " ,\t\n";
  1905. // Fetch tag count.
  1906. U32 assetTagCount = StringUnit::getUnitCount( pAssetTagNames, pTagSeparators );
  1907. // Fetch asset tags.
  1908. Vector<AssetTagsManifest::AssetTag*> assetTags;
  1909. for( U32 tagIndex = 0; tagIndex < assetTagCount; ++tagIndex )
  1910. {
  1911. // Fetch asset tag name.
  1912. const char* pTagName = StringUnit::getUnit( pAssetTagNames, tagIndex, pTagSeparators );
  1913. // Fetch asset tag.
  1914. AssetTagsManifest::AssetTag* pAssetTag = mAssetTagsManifest->findAssetTag( pTagName );
  1915. // Did we find the asset tag?
  1916. if ( pAssetTag == NULL )
  1917. {
  1918. // No, so warn.
  1919. Con::warnf( "AssetTagsManifest: Asset Manager: Cannot find tagged assets of '%s' as it does not exist. Ignoring tag.", pTagName );
  1920. continue;
  1921. }
  1922. assetTags.push_back( pAssetTag );
  1923. }
  1924. // Fetch found asset tag count.
  1925. assetTagCount = assetTags.size();
  1926. // Did we find any tags?
  1927. if ( assetTagCount == 0 )
  1928. {
  1929. // No, so warn.
  1930. Con::warnf( "AssetTagsManifest: Asset Manager: No specified tagged assets found in '%s'.", pAssetTagNames );
  1931. return 0;
  1932. }
  1933. // Use asset-query as the source?
  1934. if ( assetQueryAsSource )
  1935. {
  1936. AssetQuery filteredAssets;
  1937. // Yes, so iterate asset query.
  1938. for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
  1939. {
  1940. // Fetch asset Id.
  1941. StringTableEntry assetId = *assetItr;
  1942. // Skip if asset is not valid.
  1943. if ( !isDeclaredAsset( assetId ) )
  1944. continue;
  1945. // Reset matched flag.
  1946. bool assetTagMatched = false;
  1947. // Iterate asset tags.
  1948. for ( Vector<AssetTagsManifest::AssetTag*>::iterator assetTagItr = assetTags.begin(); assetTagItr != assetTags.end(); ++assetTagItr )
  1949. {
  1950. // Fetch asset tag.
  1951. AssetTagsManifest::AssetTag* pAssetTag = *assetTagItr;
  1952. // Skip if asset is not tagged.
  1953. if ( !pAssetTag->containsAsset( assetId ) )
  1954. continue;
  1955. // Flag as matched.
  1956. assetTagMatched = true;
  1957. break;
  1958. }
  1959. // Did we find a match?
  1960. if ( assetTagMatched )
  1961. {
  1962. // Yes, so is asset already present?
  1963. if ( !filteredAssets.containsAsset( assetId ) )
  1964. {
  1965. // No, so store as result.
  1966. filteredAssets.mAssetList.push_back(assetId);
  1967. // Increase result count.
  1968. resultCount++;
  1969. }
  1970. }
  1971. }
  1972. // Set asset query.
  1973. pAssetQuery->set( filteredAssets );
  1974. }
  1975. else
  1976. {
  1977. // Iterate asset tags.
  1978. for ( Vector<AssetTagsManifest::AssetTag*>::iterator assetTagItr = assetTags.begin(); assetTagItr != assetTags.end(); ++assetTagItr )
  1979. {
  1980. // Fetch asset tag.
  1981. AssetTagsManifest::AssetTag* pAssetTag = *assetTagItr;
  1982. // Iterate tagged assets.
  1983. for ( Vector<typeAssetId>::iterator assetItr = pAssetTag->mAssets.begin(); assetItr != pAssetTag->mAssets.end(); ++assetItr )
  1984. {
  1985. // Fetch asset Id.
  1986. StringTableEntry assetId = *assetItr;
  1987. // Skip if asset Id is already present.
  1988. if ( pAssetQuery->containsAsset( assetId ) )
  1989. continue;
  1990. // Store as result.
  1991. pAssetQuery->mAssetList.push_back(assetId);
  1992. // Increase result count.
  1993. resultCount++;
  1994. }
  1995. }
  1996. }
  1997. return resultCount;
  1998. }
  1999. //-----------------------------------------------------------------------------
  2000. S32 AssetManager::findAssetLooseFile( AssetQuery* pAssetQuery, const char* pLooseFile, const bool assetQueryAsSource )
  2001. {
  2002. // Debug Profiling.
  2003. PROFILE_SCOPE(AssetManager_FindAssetLooseFile);
  2004. // Sanity!
  2005. AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
  2006. AssertFatal( pLooseFile != NULL, "Cannot use NULL loose file." );
  2007. // Make game relative path for loose file.
  2008. StringTableEntry looseFile = Platform::makeRelativePathName(pLooseFile, NULL);;
  2009. // Reset result count.
  2010. S32 resultCount = 0;
  2011. // Use asset-query as the source?
  2012. if ( assetQueryAsSource )
  2013. {
  2014. AssetQuery filteredAssets;
  2015. // Yes, so iterate asset query.
  2016. for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
  2017. {
  2018. // Fetch asset definition.
  2019. AssetDefinition* pAssetDefinition = findAsset( *assetItr );
  2020. // Fetch loose files.
  2021. Vector<StringTableEntry>& assetLooseFiles = pAssetDefinition->mAssetLooseFiles;
  2022. // Skip if this asset has no loose files.
  2023. if ( assetLooseFiles.size() == 0 )
  2024. continue;
  2025. // Search the assets loose files.
  2026. for( Vector<StringTableEntry>::iterator looseFileItr = assetLooseFiles.begin(); looseFileItr != assetLooseFiles.end(); ++looseFileItr )
  2027. {
  2028. // Is this the loose file we are searching for?
  2029. if ( *looseFileItr != looseFile )
  2030. continue;
  2031. // Store as result.
  2032. filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
  2033. // Increase result count.
  2034. resultCount++;
  2035. break;
  2036. }
  2037. }
  2038. // Set asset query.
  2039. pAssetQuery->set( filteredAssets );
  2040. }
  2041. else
  2042. {
  2043. // No, so iterate declared assets.
  2044. for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
  2045. {
  2046. // Fetch asset definition.
  2047. AssetDefinition* pAssetDefinition = assetItr->value;
  2048. // Fetch loose files.
  2049. Vector<StringTableEntry>& assetLooseFiles = pAssetDefinition->mAssetLooseFiles;
  2050. // Skip if this asset has no loose files.
  2051. if ( assetLooseFiles.size() == 0 )
  2052. continue;
  2053. // Search the assets loose files.
  2054. for( Vector<StringTableEntry>::iterator looseFileItr = assetLooseFiles.begin(); looseFileItr != assetLooseFiles.end(); ++looseFileItr )
  2055. {
  2056. // Is this the loose file we are searching for?
  2057. if ( *looseFileItr != looseFile )
  2058. continue;
  2059. // Store as result.
  2060. pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
  2061. // Increase result count.
  2062. resultCount++;
  2063. break;
  2064. }
  2065. }
  2066. }
  2067. return resultCount;
  2068. }
  2069. //-----------------------------------------------------------------------------
  2070. AssetManager::typeAssetDependsOnHash* AssetManager::getDependedOnAssets()
  2071. {
  2072. // Find any asset dependencies.
  2073. return &mAssetDependsOn;
  2074. }
  2075. //-----------------------------------------------------------------------------
  2076. S32 AssetManager::getAssetLooseFileCount(const char* pAssetId)
  2077. {
  2078. // Debug Profiling.
  2079. PROFILE_SCOPE(AssetManager_getAssetLooseFileCount);
  2080. // Sanity!
  2081. AssertFatal(pAssetId != NULL, "Cannot get loose files for NULL asset Id.");
  2082. // Find asset.
  2083. AssetDefinition* pAssetDefinition = findAsset(pAssetId);
  2084. // Did we find the asset?
  2085. if (pAssetDefinition == NULL)
  2086. {
  2087. // No, so warn.
  2088. Con::warnf("Asset Manager: Failed to get loose files for asset Id '%s' as it does not exist.", pAssetId);
  2089. return false;
  2090. }
  2091. S32 looseFileCount = pAssetDefinition->mAssetLooseFiles.size();
  2092. // Cleanup our reference
  2093. pAssetDefinition = NULL;
  2094. return looseFileCount;
  2095. }
  2096. //-----------------------------------------------------------------------------
  2097. const char* AssetManager::getAssetLooseFile(const char* pAssetId, const S32& index)
  2098. {
  2099. // Debug Profiling.
  2100. PROFILE_SCOPE(AssetManager_getAssetLooseFile);
  2101. // Sanity!
  2102. AssertFatal(pAssetId != NULL, "Cannot get loose file for NULL asset Id.");
  2103. // Find asset.
  2104. AssetDefinition* pAssetDefinition = findAsset(pAssetId);
  2105. // Did we find the asset?
  2106. if (pAssetDefinition == NULL)
  2107. {
  2108. // No, so warn.
  2109. Con::warnf("Asset Manager: Failed to get loose file for asset Id '%s' as it does not exist.", pAssetId);
  2110. return StringTable->EmptyString();
  2111. }
  2112. if(index < 0 || index >= pAssetDefinition->mAssetLooseFiles.size())
  2113. {
  2114. Con::warnf("Asset Manager : Failed to get loose file for asset Id '%s' as the index was out of range.", pAssetId);
  2115. }
  2116. StringTableEntry looseFile = pAssetDefinition->mAssetLooseFiles[index];
  2117. // Cleanup our reference
  2118. pAssetDefinition = NULL;
  2119. return looseFile;
  2120. }
  2121. //-----------------------------------------------------------------------------
  2122. static U32 HashAssetId(const char* str)
  2123. {
  2124. U32 hash = 2166136261u;
  2125. while (*str)
  2126. {
  2127. hash ^= (U8)*str++;
  2128. hash *= 16777619u;
  2129. }
  2130. return hash;
  2131. }
  2132. bool AssetManager::scanDeclaredAssets( const char* pPath, const char* pExtension, const bool recurse, ModuleDefinition* pModuleDefinition )
  2133. {
  2134. // Debug Profiling.
  2135. PROFILE_SCOPE(AssetManager_ScanDeclaredAssets);
  2136. // Sanity!
  2137. AssertFatal( pPath != NULL, "Cannot scan declared assets with NULL path." );
  2138. AssertFatal( pExtension != NULL, "Cannot scan declared assets with NULL extension." );
  2139. // Expand path location.
  2140. String relativePath = Platform::makeRelativePathName(pPath, NULL);
  2141. // Strip any trailing slash off the path.
  2142. if (relativePath.endsWith("/"))
  2143. relativePath = relativePath.substr(0, relativePath.length() - 1);
  2144. Torque::Path scanPath = Torque::FS::GetCwd();
  2145. scanPath.setPath(relativePath);
  2146. // Find files.
  2147. Vector<String> files;
  2148. S32 numAssets = Torque::FS::FindByPattern(scanPath, pExtension, recurse, files, true);
  2149. if (numAssets <= 0)
  2150. {
  2151. // Failed so warn. or don't... Common error when scanning modules with no assets
  2152. //Con::warnf( "Asset Manager: No declared assets found in directory '%s'.", relativePath.c_str());
  2153. return false;
  2154. }
  2155. // Is the asset file-path located within the specified module?
  2156. if ( !Con::isBasePath(relativePath.c_str(), pModuleDefinition->getModulePath()) )
  2157. {
  2158. // No, so warn.
  2159. Con::warnf( "Asset Manager: Could not add declared asset file '%s' as file does not exist with module path '%s'",
  2160. pPath,
  2161. pModuleDefinition->getModulePath() );
  2162. return false;
  2163. }
  2164. // Info.
  2165. if ( mEchoInfo )
  2166. {
  2167. Con::printSeparator();
  2168. Con::printf( "Asset Manager: Scanning for declared assets in path '%s' for files with extension '%s'...", relativePath.c_str(), pExtension );
  2169. }
  2170. // Fetch module assets.
  2171. ModuleDefinition::typeModuleAssetsVector& moduleAssets = pModuleDefinition->getModuleAssets();
  2172. TamlAssetDeclaredVisitor assetDeclaredVisitor;
  2173. // Iterate files.
  2174. for (S32 i = 0; i < numAssets; ++i)
  2175. {
  2176. Torque::Path assetPath = files[i];
  2177. Torque::Path compiledPath = assetPath;
  2178. compiledPath.setExtension(mTaml.getAutoFormatBinaryExtension());
  2179. if (Torque::FS::IsFile(compiledPath))
  2180. {
  2181. Torque::FS::FileNodeRef assetFile = Torque::FS::GetFileNode(assetPath);
  2182. Torque::FS::FileNodeRef compiledFile = Torque::FS::GetFileNode(compiledPath);
  2183. if (assetFile != NULL && compiledFile != NULL)
  2184. {
  2185. if (compiledFile->getModifiedTime() >= assetFile->getModifiedTime())
  2186. assetPath = compiledPath;
  2187. }
  2188. }
  2189. // Clear declared assets.
  2190. assetDeclaredVisitor.clear();
  2191. // Format full file-path.
  2192. char assetFileBuffer[1024];
  2193. dSprintf( assetFileBuffer, sizeof(assetFileBuffer), "%s/%s", assetPath.getPath().c_str(), assetPath.getFullFileName().c_str());
  2194. // Parse the filename.
  2195. if ( !mTaml.parse( assetFileBuffer, assetDeclaredVisitor ) )
  2196. {
  2197. // Warn.
  2198. Con::warnf( "Asset Manager: Failed to parse file containing asset declaration: '%s'.", assetFileBuffer );
  2199. continue;
  2200. }
  2201. // Fetch asset definition.
  2202. AssetDefinition& foundAssetDefinition = assetDeclaredVisitor.getAssetDefinition();
  2203. // Did we get an asset name?
  2204. if ( foundAssetDefinition.mAssetName == StringTable->EmptyString() )
  2205. {
  2206. // No, so warn.
  2207. Con::warnf( "Asset Manager: Parsed file '%s' but did not encounter an asset.", assetFileBuffer );
  2208. continue;
  2209. }
  2210. // Set module definition.
  2211. foundAssetDefinition.mpModuleDefinition = pModuleDefinition;
  2212. // Format asset Id.
  2213. char assetIdBuffer[1024];
  2214. dSprintf(assetIdBuffer, sizeof(assetIdBuffer), "%s%s%s",
  2215. pModuleDefinition->getModuleId(),
  2216. ASSET_SCOPE_TOKEN,
  2217. foundAssetDefinition.mAssetName );
  2218. // Set asset Id.
  2219. foundAssetDefinition.mAssetId = StringTable->insert( assetIdBuffer );
  2220. // Does this asset already exist?
  2221. if ( mDeclaredAssets.contains( foundAssetDefinition.mAssetId ) )
  2222. {
  2223. // Yes, so warn.
  2224. Con::warnf( "Asset Manager: Encountered asset Id '%s' in asset file '%s' but it conflicts with existing asset Id in asset file '%s'.",
  2225. foundAssetDefinition.mAssetId,
  2226. foundAssetDefinition.mAssetBaseFilePath,
  2227. mDeclaredAssets.find( foundAssetDefinition.mAssetId )->value->mAssetBaseFilePath );
  2228. continue;
  2229. }
  2230. U32 netId = HashAssetId(assetIdBuffer);
  2231. // Collision detection
  2232. typeNetIdToAssetMap::iterator netIterator = mNetIdToAsset.find(netId);
  2233. if (netIterator != mNetIdToAsset.end())
  2234. {
  2235. Con::errorf(
  2236. "AssetManager: Hash collision for '%s' and '%s'",
  2237. assetIdBuffer,
  2238. mNetIdToAsset.find(netId)->value
  2239. );
  2240. AssertFatal(false, "Asset hash collision detected.");
  2241. }
  2242. mNetIdToAsset.insert(netId, foundAssetDefinition.mAssetId);
  2243. mAssetToNetId.insert(foundAssetDefinition.mAssetId, netId);
  2244. // Create new asset definition.
  2245. AssetDefinition* pAssetDefinition = new AssetDefinition( foundAssetDefinition );
  2246. // Store in declared assets.
  2247. mDeclaredAssets.insert( pAssetDefinition->mAssetId, pAssetDefinition );
  2248. // Store in module assets.
  2249. moduleAssets.push_back( pAssetDefinition );
  2250. // Info.
  2251. if ( mEchoInfo )
  2252. {
  2253. Con::printSeparator();
  2254. Con::printf( "Asset Manager: Adding Asset Id '%s' of type '%s' in asset file '%s'.",
  2255. pAssetDefinition->mAssetId,
  2256. pAssetDefinition->mAssetType,
  2257. pAssetDefinition->mAssetBaseFilePath );
  2258. }
  2259. // Fetch asset Id.
  2260. StringTableEntry assetId = pAssetDefinition->mAssetId;
  2261. // Fetch asset dependencies.
  2262. TamlAssetDeclaredVisitor::typeAssetIdVector& assetDependencies = assetDeclaredVisitor.getAssetDependencies();
  2263. // Are there any asset dependencies?
  2264. if ( assetDependencies.size() > 0 )
  2265. {
  2266. // Yes, so iterate dependencies.
  2267. for( TamlAssetDeclaredVisitor::typeAssetIdVector::iterator assetDependencyItr = assetDependencies.begin(); assetDependencyItr != assetDependencies.end(); ++assetDependencyItr )
  2268. {
  2269. // Fetch asset Ids.
  2270. StringTableEntry dependencyAssetId = *assetDependencyItr;
  2271. // Insert depends-on.
  2272. mAssetDependsOn.insertEqual( assetId, dependencyAssetId );
  2273. // Insert is-depended-on.
  2274. mAssetIsDependedOn.insertEqual( dependencyAssetId, assetId );
  2275. // Info.
  2276. if ( mEchoInfo )
  2277. {
  2278. Con::printf( "Asset Manager: Asset Id '%s' has dependency of Asset Id '%s'", assetId, dependencyAssetId );
  2279. }
  2280. }
  2281. }
  2282. // Fetch asset loose files.
  2283. TamlAssetDeclaredVisitor::typeLooseFileVector& assetLooseFiles = assetDeclaredVisitor.getAssetLooseFiles();
  2284. // Are there any loose files?
  2285. if ( assetLooseFiles.size() > 0 )
  2286. {
  2287. // Yes, so iterate loose files.
  2288. for( TamlAssetDeclaredVisitor::typeLooseFileVector::iterator assetLooseFileItr = assetLooseFiles.begin(); assetLooseFileItr != assetLooseFiles.end(); ++assetLooseFileItr )
  2289. {
  2290. // Fetch loose file.
  2291. StringTableEntry looseFile = *assetLooseFileItr;
  2292. // Info.
  2293. if ( mEchoInfo )
  2294. {
  2295. Con::printf( "Asset Manager: Asset Id '%s' has loose file '%s'.", assetId, looseFile );
  2296. }
  2297. // Store loose file.
  2298. pAssetDefinition->mAssetLooseFiles.push_back( looseFile );
  2299. }
  2300. }
  2301. }
  2302. // Info.
  2303. if ( mEchoInfo )
  2304. {
  2305. Con::printSeparator();
  2306. Con::printf( "Asset Manager: ... Finished scanning for declared assets in path '%s' for files with extension '%s'.", relativePath.c_str(), pExtension );
  2307. Con::printSeparator();
  2308. Con::printBlankLine();
  2309. }
  2310. return true;
  2311. }
  2312. //-----------------------------------------------------------------------------
  2313. bool AssetManager::scanReferencedAssets( const char* pPath, const char* pExtension, const bool recurse )
  2314. {
  2315. // Debug Profiling.
  2316. PROFILE_SCOPE(AssetManager_ScanReferencedAssets);
  2317. // Sanity!
  2318. AssertFatal( pPath != NULL, "Cannot scan referenced assets with NULL path." );
  2319. AssertFatal( pExtension != NULL, "Cannot scan referenced assets with NULL extension." );
  2320. // Expand path location.
  2321. String relativePath = Platform::makeRelativePathName(pPath, NULL);
  2322. String pattern = "*.";
  2323. pattern += pExtension;
  2324. Torque::Path scanPath = Torque::FS::GetCwd();
  2325. scanPath.setPath(relativePath);
  2326. // Find files.
  2327. Vector<String> files;
  2328. S32 numAssets = Torque::FS::FindByPattern(scanPath, pattern, recurse, files, true);
  2329. if (numAssets <= 0)
  2330. {
  2331. // Failed so warn.
  2332. Con::warnf( "Asset Manager: Failed to scan referenced assets in directory '%s'.", pPath );
  2333. return false;
  2334. }
  2335. // Info.
  2336. if ( mEchoInfo )
  2337. {
  2338. Con::printSeparator();
  2339. Con::printf( "Asset Manager: Scanning for referenced assets in path '%s' for files with extension '%s'...", pPath, pExtension );
  2340. }
  2341. TamlAssetReferencedVisitor assetReferencedVisitor;
  2342. // Iterate files.
  2343. for (S32 i = 0; i < numAssets; ++i)
  2344. {
  2345. Torque::Path assetPath = files[i];
  2346. // Clear referenced assets.
  2347. assetReferencedVisitor.clear();
  2348. // Format full file-path.
  2349. char assetFileBuffer[1024];
  2350. dSprintf( assetFileBuffer, sizeof(assetFileBuffer), "%s/%s", assetPath.getPath().c_str(), assetPath.getFullFileName().c_str());
  2351. // Format reference file-path.
  2352. typeReferenceFilePath referenceFilePath = StringTable->insert( assetFileBuffer );
  2353. // Parse the filename.
  2354. if ( !mTaml.parse( referenceFilePath, assetReferencedVisitor ) )
  2355. {
  2356. // Warn.
  2357. Con::warnf( "Asset Manager: Failed to parse file containing asset references: '%s'.", referenceFilePath );
  2358. continue;
  2359. }
  2360. // Fetch usage map.
  2361. const TamlAssetReferencedVisitor::typeAssetReferencedHash& assetReferencedMap = assetReferencedVisitor.getAssetReferencedMap();
  2362. // Do we have any asset references?
  2363. if ( assetReferencedMap.size() > 0 )
  2364. {
  2365. // Info.
  2366. if ( mEchoInfo )
  2367. {
  2368. Con::printSeparator();
  2369. }
  2370. // Iterate usage.
  2371. for( TamlAssetReferencedVisitor::typeAssetReferencedHash::const_iterator usageItr = assetReferencedMap.begin(); usageItr != assetReferencedMap.end(); ++usageItr )
  2372. {
  2373. // Fetch asset name.
  2374. typeAssetId assetId = usageItr->key;
  2375. // Info.
  2376. if ( mEchoInfo )
  2377. {
  2378. Con::printf( "Asset Manager: Found referenced Asset Id '%s' in file '%s'.", assetId, referenceFilePath );
  2379. }
  2380. // Add referenced asset.
  2381. addReferencedAsset( assetId, referenceFilePath );
  2382. }
  2383. }
  2384. }
  2385. // Info.
  2386. if ( mEchoInfo )
  2387. {
  2388. Con::printf( "Asset Manager: ... Finished scanning for referenced assets in path '%s' for files with extension '%s'.", relativePath.c_str(), pExtension );
  2389. Con::printSeparator();
  2390. Con::printBlankLine();
  2391. }
  2392. return true;
  2393. }
  2394. //-----------------------------------------------------------------------------
  2395. AssetDefinition* AssetManager::findAsset( const char* pAssetId )
  2396. {
  2397. // Debug Profiling.
  2398. PROFILE_SCOPE(AssetManager_FindAsset);
  2399. // Sanity!
  2400. AssertFatal( pAssetId != NULL, "Cannot find NULL asset Id." );
  2401. // Fetch asset Id.
  2402. StringTableEntry assetId = StringTable->insert( pAssetId );
  2403. // Find declared asset.
  2404. typeDeclaredAssetsHash::iterator declaredAssetItr = mDeclaredAssets.find( assetId );
  2405. // Find if we didn't find a declared asset Id.
  2406. if ( declaredAssetItr == mDeclaredAssets.end() )
  2407. return NULL;
  2408. return declaredAssetItr->value;
  2409. }
  2410. //-----------------------------------------------------------------------------
  2411. void AssetManager::addReferencedAsset( StringTableEntry assetId, StringTableEntry referenceFilePath )
  2412. {
  2413. // Debug Profiling.
  2414. PROFILE_SCOPE(AssetManager_AddReferencedAsset);
  2415. // Sanity!
  2416. AssertFatal( assetId != NULL, "Cannot add referenced asset with NULL asset Id." );
  2417. AssertFatal( referenceFilePath != NULL, "Cannot add referenced asset with NULL reference file-path." );
  2418. // Find referenced asset.
  2419. typeReferencedAssetsHash::Iterator referencedAssetItr = mReferencedAssets.find( assetId );
  2420. // Did we find the asset?
  2421. if ( referencedAssetItr == mReferencedAssets.end() )
  2422. {
  2423. // No, so add asset Id.
  2424. mReferencedAssets.insertEqual( assetId, referenceFilePath );
  2425. }
  2426. else
  2427. {
  2428. // Yes, so add asset Id with a unique file.
  2429. while( true )
  2430. {
  2431. // Finish if this file is already present.
  2432. if ( referencedAssetItr->value == referenceFilePath )
  2433. return;
  2434. // Move to next asset Id.
  2435. referencedAssetItr++;
  2436. // Is this the end of referenced assets or a different asset Id?
  2437. if ( referencedAssetItr == mReferencedAssets.end() ||
  2438. referencedAssetItr->key != assetId )
  2439. {
  2440. // Yes, so add asset reference.
  2441. mReferencedAssets.insertEqual( assetId, referenceFilePath );
  2442. return;
  2443. }
  2444. };
  2445. }
  2446. }
  2447. //-----------------------------------------------------------------------------
  2448. void AssetManager::renameAssetReferences( StringTableEntry assetIdFrom, StringTableEntry assetIdTo )
  2449. {
  2450. // Debug Profiling.
  2451. PROFILE_SCOPE(AssetManager_RenameAssetReferences);
  2452. // Sanity!
  2453. AssertFatal( assetIdFrom != NULL, "Cannot rename asset references using NULL asset Id from." );
  2454. AssertFatal( assetIdTo != NULL, "Cannot rename asset references using NULL asset Id to." );
  2455. // Finish if the asset is not referenced.
  2456. if ( !mReferencedAssets.count( assetIdFrom ) )
  2457. return;
  2458. // Setup referenced update visitor.
  2459. TamlAssetReferencedUpdateVisitor assetReferencedUpdateVisitor;
  2460. assetReferencedUpdateVisitor.setAssetIdFrom( assetIdFrom );
  2461. assetReferencedUpdateVisitor.setAssetIdTo( assetIdTo );
  2462. // Find first referenced asset Id.
  2463. typeReferencedAssetsHash::Iterator referencedAssetItr = mReferencedAssets.find( assetIdFrom );
  2464. // Iterate references.
  2465. while( true )
  2466. {
  2467. // Finish if end of references.
  2468. if ( referencedAssetItr == mReferencedAssets.end() || referencedAssetItr->key != assetIdFrom )
  2469. return;
  2470. // Info.
  2471. if ( mEchoInfo )
  2472. {
  2473. Con::printf( "Asset Manager: Renaming declared Asset Id '%s' to Asset Id '%s'. Updating referenced file '%s'",
  2474. assetIdFrom,
  2475. assetIdTo,
  2476. referencedAssetItr->value );
  2477. }
  2478. // Update asset file declaration.
  2479. if ( !mTaml.parse( referencedAssetItr->value, assetReferencedUpdateVisitor ) )
  2480. {
  2481. // No, so warn.
  2482. Con::warnf("Asset Manager: Cannot rename referenced asset Id '%s' to asset Id '%s' as the referenced asset file could not be parsed: %s",
  2483. assetIdFrom, assetIdTo, referencedAssetItr->value );
  2484. }
  2485. // Move to next reference.
  2486. referencedAssetItr++;
  2487. }
  2488. }
  2489. //-----------------------------------------------------------------------------
  2490. void AssetManager::removeAssetReferences( StringTableEntry assetId )
  2491. {
  2492. // Debug Profiling.
  2493. PROFILE_SCOPE(AssetManager_RemoveAssetReferences);
  2494. // Sanity!
  2495. AssertFatal( assetId != NULL, "Cannot rename asset references using NULL asset Id." );
  2496. // Finish if the asset is not referenced.
  2497. if ( !mReferencedAssets.count( assetId ) )
  2498. return;
  2499. // Setup referenced update visitor.
  2500. TamlAssetReferencedUpdateVisitor assetReferencedUpdateVisitor;
  2501. assetReferencedUpdateVisitor.setAssetIdFrom( assetId );
  2502. assetReferencedUpdateVisitor.setAssetIdTo( StringTable->EmptyString() );
  2503. // Find first referenced asset Id.
  2504. typeReferencedAssetsHash::Iterator referencedAssetItr = mReferencedAssets.find(assetId);
  2505. // Iterate references.
  2506. while( true )
  2507. {
  2508. // Finish if end of references.
  2509. if ( referencedAssetItr == mReferencedAssets.end() || referencedAssetItr->key != assetId )
  2510. break;
  2511. // Info.
  2512. if ( mEchoInfo )
  2513. {
  2514. Con::printf( "Asset Manager: Removing Asset Id '%s' references from file '%s'",
  2515. assetId,
  2516. referencedAssetItr->value );
  2517. }
  2518. // Update asset file declaration.
  2519. if ( !mTaml.parse( referencedAssetItr->value, assetReferencedUpdateVisitor ) )
  2520. {
  2521. // No, so warn.
  2522. Con::warnf("Asset Manager: Cannot remove referenced asset Id '%s' as the referenced asset file could not be parsed: %s",
  2523. assetId,
  2524. referencedAssetItr->value );
  2525. }
  2526. // Move to next reference.
  2527. referencedAssetItr++;
  2528. }
  2529. // Remove asset references.
  2530. mReferencedAssets.erase( assetId );
  2531. }
  2532. //-----------------------------------------------------------------------------
  2533. void AssetManager::renameAssetDependencies( StringTableEntry assetIdFrom, StringTableEntry assetIdTo )
  2534. {
  2535. // Debug Profiling.
  2536. PROFILE_SCOPE(AssetManager_RenameAssetDependencies);
  2537. // Sanity!
  2538. AssertFatal( assetIdFrom != NULL, "Cannot rename asset dependencies using NULL asset Id from." );
  2539. AssertFatal( assetIdTo != NULL, "Cannot rename asset dependencies using NULL asset Id to." );
  2540. // Rename via depends-on...
  2541. while( mAssetDependsOn.count( assetIdFrom ) > 0 )
  2542. {
  2543. // Find depends-on.
  2544. typeAssetDependsOnHash::Iterator dependsOnItr = mAssetDependsOn.find(assetIdFrom);
  2545. // Fetch dependency asset Id.
  2546. StringTableEntry dependencyAssetId = dependsOnItr->value;
  2547. // Find is-depends-on entry.
  2548. typeAssetIsDependedOnHash::Iterator isDependedOnItr = mAssetIsDependedOn.find(dependencyAssetId);
  2549. // Sanity!
  2550. AssertFatal( isDependedOnItr != mAssetIsDependedOn.end(), "Asset dependencies are corrupt!" );
  2551. while( isDependedOnItr != mAssetIsDependedOn.end() && isDependedOnItr->key == dependencyAssetId && isDependedOnItr->value != assetIdFrom )
  2552. {
  2553. isDependedOnItr++;
  2554. }
  2555. // Sanity!
  2556. AssertFatal( isDependedOnItr->key == dependencyAssetId && isDependedOnItr->value == assetIdFrom, "Asset dependencies are corrupt!" );
  2557. // Remove is-depended-on.
  2558. mAssetIsDependedOn.erase( isDependedOnItr );
  2559. // Remove depends-on.
  2560. mAssetDependsOn.erase( dependsOnItr );
  2561. // Insert depends-on.
  2562. mAssetDependsOn.insertEqual( assetIdTo, dependencyAssetId );
  2563. // Insert is-depended-on.
  2564. mAssetIsDependedOn.insertEqual( dependencyAssetId, assetIdTo );
  2565. }
  2566. // Rename via is-depended-on...
  2567. while( mAssetIsDependedOn.count( assetIdFrom ) > 0 )
  2568. {
  2569. // Find is-depended-on.
  2570. typeAssetIsDependedOnHash::Iterator isdependedOnItr = mAssetIsDependedOn.find(assetIdFrom);
  2571. // Fetch dependency asset Id.
  2572. StringTableEntry dependencyAssetId = isdependedOnItr->value;
  2573. // Find depends-on entry.
  2574. typeAssetDependsOnHash::Iterator dependsOnItr = mAssetDependsOn.find(dependencyAssetId);
  2575. // Sanity!
  2576. AssertFatal( dependsOnItr != mAssetDependsOn.end(), "Asset dependencies are corrupt!" );
  2577. while( dependsOnItr != mAssetDependsOn.end() && dependsOnItr->key == dependencyAssetId && dependsOnItr->value != assetIdFrom )
  2578. {
  2579. dependsOnItr++;
  2580. }
  2581. // Sanity!
  2582. AssertFatal( dependsOnItr->key == dependencyAssetId && dependsOnItr->value == assetIdFrom, "Asset dependencies are corrupt!" );
  2583. // Remove is-depended-on.
  2584. mAssetIsDependedOn.erase( isdependedOnItr );
  2585. // Remove depends-on.
  2586. mAssetDependsOn.erase( dependsOnItr );
  2587. // Insert depends-on.
  2588. mAssetDependsOn.insertEqual( dependencyAssetId, assetIdTo );
  2589. // Insert is-depended-on.
  2590. mAssetIsDependedOn.insertEqual( assetIdTo, dependencyAssetId );
  2591. }
  2592. }
  2593. //-----------------------------------------------------------------------------
  2594. void AssetManager::removeAssetDependencies( const char* pAssetId )
  2595. {
  2596. // Debug Profiling.
  2597. PROFILE_SCOPE(AssetManager_RemvoeAsetDependencies);
  2598. // Sanity!
  2599. AssertFatal( pAssetId != NULL, "Cannot remove asset dependencies using NULL asset Id." );
  2600. // Fetch asset Id.
  2601. StringTableEntry assetId = StringTable->insert( pAssetId );
  2602. // Remove from depends-on assets.
  2603. while( mAssetDependsOn.count( assetId ) > 0 )
  2604. {
  2605. // Find depends-on.
  2606. typeAssetDependsOnHash::Iterator dependsOnItr = mAssetDependsOn.find(assetId);
  2607. // Fetch dependency asset Id.
  2608. StringTableEntry dependencyAssetId = dependsOnItr->value;
  2609. // Find is-depends-on entry.
  2610. typeAssetIsDependedOnHash::Iterator isDependedOnItr = mAssetIsDependedOn.find(dependencyAssetId);
  2611. // Sanity!
  2612. AssertFatal( isDependedOnItr != mAssetIsDependedOn.end(), "Asset dependencies are corrupt!" );
  2613. while( isDependedOnItr != mAssetIsDependedOn.end() && isDependedOnItr->key == dependencyAssetId && isDependedOnItr->value != assetId )
  2614. {
  2615. isDependedOnItr++;
  2616. }
  2617. // Sanity!
  2618. AssertFatal( isDependedOnItr->key == dependencyAssetId && isDependedOnItr->value == assetId, "Asset dependencies are corrupt!" );
  2619. // Remove is-depended-on.
  2620. mAssetIsDependedOn.erase( isDependedOnItr );
  2621. // Remove depends-on.
  2622. mAssetDependsOn.erase( dependsOnItr );
  2623. }
  2624. }
  2625. //-----------------------------------------------------------------------------
  2626. void AssetManager::unloadAsset( AssetDefinition* pAssetDefinition )
  2627. {
  2628. // Debug Profiling.
  2629. PROFILE_SCOPE(AssetManager_UnloadAsset);
  2630. pAssetDefinition->mpAssetBase->unloadAsset();
  2631. // Destroy the asset.
  2632. if(pAssetDefinition->mpAssetBase->isProperlyAdded())
  2633. pAssetDefinition->mpAssetBase->deleteObject();
  2634. // Increase unloaded count.
  2635. pAssetDefinition->mAssetUnloadedCount++;
  2636. // Is the asset internal?
  2637. if ( pAssetDefinition->mAssetInternal )
  2638. {
  2639. // Yes, so decrease internal loaded asset count.
  2640. mLoadedInternalAssetsCount--;
  2641. }
  2642. else
  2643. {
  2644. // No, so decrease external loaded assets count.
  2645. mLoadedExternalAssetsCount--;
  2646. }
  2647. // Is the asset private?
  2648. if ( pAssetDefinition->mAssetPrivate )
  2649. {
  2650. // Yes, so decrease private loaded asset count.
  2651. mLoadedPrivateAssetsCount--;
  2652. // Remove it completely.
  2653. removeDeclaredAsset( pAssetDefinition->mAssetId );
  2654. }
  2655. }
  2656. //-----------------------------------------------------------------------------
  2657. void AssetManager::onModulePreLoad( ModuleDefinition* pModuleDefinition )
  2658. {
  2659. // Debug Profiling.
  2660. PROFILE_SCOPE(AssetManager_OnModulePreLoad);
  2661. // Add module declared assets.
  2662. addModuleDeclaredAssets( pModuleDefinition );
  2663. // Load any auto-loaded asset types
  2664. loadModuleAutoLoadAssets(pModuleDefinition);
  2665. // Is an asset tags manifest specified?
  2666. if ( pModuleDefinition->getAssetTagsManifest() != StringTable->EmptyString() )
  2667. {
  2668. // Yes, so load the asset tags manifest.
  2669. loadAssetTags( pModuleDefinition );
  2670. }
  2671. }
  2672. //-----------------------------------------------------------------------------
  2673. void AssetManager::onModulePreUnload( ModuleDefinition* pModuleDefinition )
  2674. {
  2675. // Debug Profiling.
  2676. PROFILE_SCOPE(AssetManager_OnModulePreUnload);
  2677. // Is an asset tags manifest specified?
  2678. if ( pModuleDefinition->getAssetTagsManifest() != StringTable->EmptyString() )
  2679. {
  2680. // Yes, so save the asset tags manifest.
  2681. saveAssetTags();
  2682. // Do we have an asset tags manifest?
  2683. if ( !mAssetTagsManifest.isNull() )
  2684. {
  2685. // Yes, so remove it.
  2686. mAssetTagsManifest->deleteObject();
  2687. mAssetTagsModuleDefinition = NULL;
  2688. }
  2689. }
  2690. }
  2691. //-----------------------------------------------------------------------------
  2692. void AssetManager::onModulePostUnload( ModuleDefinition* pModuleDefinition )
  2693. {
  2694. // Debug Profiling.
  2695. PROFILE_SCOPE(AssetManager_OnModulePostUnload);
  2696. // Remove declared assets.
  2697. removeDeclaredAssets( pModuleDefinition );
  2698. }