| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2013 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #include "assetManager.h"
- #ifndef _ASSET_PTR_H_
- #include "assetPtr.h"
- #endif
- #ifndef _REFERENCED_ASSETS_H_
- #include "assets/referencedAssets.h"
- #endif
- #ifndef _DECLARED_ASSETS_H_
- #include "assets/declaredAssets.h"
- #endif
- #ifndef _TAML_ASSET_REFERENCED_VISITOR_H_
- #include "tamlAssetReferencedVisitor.h"
- #endif
- #ifndef _TAML_ASSET_DECLARED_VISITOR_H_
- #include "tamlAssetDeclaredVisitor.h"
- #endif
- #ifndef _TAML_ASSET_DECLARED_UPDATE_VISITOR_H_
- #include "tamlAssetDeclaredUpdateVisitor.h"
- #endif
- #ifndef _TAML_ASSET_REFERENCED_UPDATE_VISITOR_H_
- #include "tamlAssetReferencedUpdateVisitor.h"
- #endif
- #ifndef _CONSOLETYPES_H_
- #include "console/consoleTypes.h"
- #endif
- #ifndef _AUTOLOAD_ASSETS_H_
- #include "assets/autoloadAssets.h"
- #endif
- #ifndef GUI_ASSET_H
- #include "T3D/assets/GUIAsset.h"
- #endif
- #ifndef SCRIPT_ASSET_H
- #include "T3D/assets/ScriptAsset.h"
- #endif
- #ifndef MATERIALASSET_H
- #include "T3D/assets/MaterialAsset.h"
- #endif
- #ifndef GAME_OBJECT_ASSET_H
- #include "T3D/assets/GameObjectAsset.h"
- #endif
- // Script bindings.
- #include "assetManager_ScriptBinding.h"
- //-----------------------------------------------------------------------------
- IMPLEMENT_CONOBJECT( AssetManager );
- //-----------------------------------------------------------------------------
- AssetManager AssetDatabase;
- //-----------------------------------------------------------------------------
- AssetManager::AssetManager() :
- mLoadedInternalAssetsCount( 0 ),
- mLoadedExternalAssetsCount( 0 ),
- mLoadedPrivateAssetsCount( 0 ),
- mMaxLoadedInternalAssetsCount( 0 ),
- mMaxLoadedExternalAssetsCount( 0 ),
- mEchoInfo( false ),
- mAcquiredReferenceCount( 0 ),
- mMaxLoadedPrivateAssetsCount( 0 ),
- mIgnoreAutoUnload( true )
- {
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::onAdd()
- {
- // Call parent.
- if ( !Parent::onAdd() )
- return false;
- return true;
- }
- //-----------------------------------------------------------------------------
- void AssetManager::onRemove()
- {
- // Do we have an asset tags manifest?
- if ( !mAssetTagsManifest.isNull() )
- {
- // Yes, so remove it.
- mAssetTagsManifest->deleteObject();
- }
- purgeAssets();
- // Call parent.
- Parent::onRemove();
- }
- //-----------------------------------------------------------------------------
- void AssetManager::initPersistFields()
- {
- docsURL;
- // Call parent.
- Parent::initPersistFields();
- addField( "EchoInfo", TypeBool, false, Offset(mEchoInfo, AssetManager), "Whether the asset manager echos extra information to the console or not." );
- addField( "IgnoreAutoUnload", TypeBool, true, Offset(mIgnoreAutoUnload, AssetManager), "Whether the asset manager should ignore unloading of auto-unload assets or not." );
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::compileReferencedAssets( ModuleDefinition* pModuleDefinition )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_CompileReferencedAsset);
- // Sanity!
- AssertFatal( pModuleDefinition != NULL, "Cannot add declared assets using a NULL module definition" );
- // Clear referenced assets.
- mReferencedAssets.clear();
- // Iterate the module definition children.
- for( SimSet::iterator itr = pModuleDefinition->begin(); itr != pModuleDefinition->end(); ++itr )
- {
- // Fetch the referenced assets.
- ReferencedAssets* pReferencedAssets = dynamic_cast<ReferencedAssets*>( *itr );
- // Skip if it's not a referenced assets location.
- if ( pReferencedAssets == NULL )
- continue;
- // Expand asset manifest location.
- char filePathBuffer[1024];
- dSprintf( filePathBuffer, sizeof(filePathBuffer), "%s/%s", pModuleDefinition->getModulePath(), pReferencedAssets->getPath() );
- // Scan referenced assets at location.
- if ( !scanReferencedAssets( filePathBuffer, pReferencedAssets->getExtension(), pReferencedAssets->getRecurse() ) )
- {
- // Warn.
- Con::warnf( "AssetManager::compileReferencedAssets() - Could not scan for referenced assets at location '%s' with extension '%s'.", filePathBuffer, pReferencedAssets->getExtension() );
- }
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::addModuleDeclaredAssets( ModuleDefinition* pModuleDefinition )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_AddDeclaredAssets);
- // Sanity!
- AssertFatal( pModuleDefinition != NULL, "Cannot add declared assets using a NULL module definition" );
- // Does the module have any assets associated with it?
- if ( pModuleDefinition->getModuleAssets().size() > 0 )
- {
- // Yes, so warn.
- Con::warnf( "Asset Manager: Cannot add declared assets to module '%s' as it already has existing assets.", pModuleDefinition->getSignature() );
- return false;
- }
- // Iterate the module definition children.
- for( SimSet::iterator itr = pModuleDefinition->begin(); itr != pModuleDefinition->end(); ++itr )
- {
- // Fetch the declared assets.
- DeclaredAssets* pDeclaredAssets = dynamic_cast<DeclaredAssets*>( *itr );
- // Skip if it's not a declared assets location.
- if ( pDeclaredAssets == NULL )
- continue;
- // Expand asset manifest location.
- char filePathBuffer[1024], extensionBuffer[256];
- String mdldfpth = pModuleDefinition->getModulePath();
- String astfpth = pDeclaredAssets->getPath();
- dSprintf(filePathBuffer, sizeof(filePathBuffer), "%s/%s", pModuleDefinition->getModulePath(), pDeclaredAssets->getPath());
- dSprintf(extensionBuffer, sizeof(extensionBuffer), "*.%s", pDeclaredAssets->getExtension());
- // Scan declared assets at location.
- if ( !scanDeclaredAssets( filePathBuffer, extensionBuffer, pDeclaredAssets->getRecurse(), pModuleDefinition ) && mEchoInfo)
- {
- // Warn.
- Con::warnf( "AssetManager::addModuleDeclaredAssets() - No assets found at location '%s' with extension '%s'.", filePathBuffer, pDeclaredAssets->getExtension() );
- }
- }
- return true;
- }
- bool AssetManager::loadModuleAutoLoadAssets(ModuleDefinition* pModuleDefinition)
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_loadModuleAutoLoadAssets);
- // Sanity!
- AssertFatal(pModuleDefinition != NULL, "Cannot auto load assets using a NULL module definition");
- // Does the module have any assets associated with it?
- if (pModuleDefinition->getModuleAssets().empty() && mEchoInfo)
- {
- // Yes, so warn.
- Con::warnf("Asset Manager: Cannot auto load assets to module '%s' as it has no existing assets.", pModuleDefinition->getSignature());
- return false;
- }
- U32 assetCount = pModuleDefinition->getModuleAssets().size();
- // Iterate the module definition children.
- for (SimSet::iterator itr = pModuleDefinition->begin(); itr != pModuleDefinition->end(); ++itr)
- {
- // Fetch the declared assets.
- AutoloadAssets* pAutoloadAssets = dynamic_cast<AutoloadAssets*>(*itr);
- // Skip if it's not a declared assets location.
- if (pAutoloadAssets == NULL)
- continue;
- for (U32 i = 0; i < assetCount; ++i)
- {
- AssetDefinition* assetDef = pModuleDefinition->getModuleAssets()[i];
- if (assetDef->mAssetType == pAutoloadAssets->getAssetType())
- {
- String assetPath = assetDef->mAssetBaseFilePath;
- assetPath.replace("//", "/");
- String autoLoadPath = pModuleDefinition->getModulePath();
- autoLoadPath += "/";
- autoLoadPath += pAutoloadAssets->getPath();
- if (pAutoloadAssets->getPath() == StringTable->EmptyString() || assetPath.startsWith(autoLoadPath.c_str()))
- {
- AssetBase* assetBase = dynamic_cast<AssetBase*>(mTaml.read(assetDef->mAssetBaseFilePath));
- //load the asset now if valid
- if (assetBase)
- {
- assetBase->setOwned(this, assetDef);
- }
- }
- }
- }
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::addDeclaredAsset( ModuleDefinition* pModuleDefinition, const char* pAssetFilePath )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_AddSingleDeclaredAsset);
- // Sanity!
- AssertFatal( pModuleDefinition != NULL, "Cannot add single declared asset using a NULL module definition" );
- AssertFatal( pAssetFilePath != NULL, "Cannot add single declared asset using a NULL asset file-path." );
- // Expand asset file-path.
- char assetFilePathBuffer[1024];
- dStrcpy(assetFilePathBuffer, Platform::makeRelativePathName(pAssetFilePath, NULL), sizeof(assetFilePathBuffer));
- // Find the final slash which should be just before the file.
- char* pFileStart = dStrrchr( assetFilePathBuffer, '/' );
- // Did we find the final slash?
- if ( pFileStart == NULL )
- {
- // No, so warn.
- Con::warnf( "AssetManager::addDeclaredAsset() - Could not add single declared asset file '%s' as file-path '%s' is not valid.",
- assetFilePathBuffer,
- pModuleDefinition->getModulePath() );
- return false;
- }
- // Terminate path at slash.
- *pFileStart = 0;
- // Move to next character which should be the file start.
- pFileStart++;
- // Scan declared assets at location.
- if ( !scanDeclaredAssets( assetFilePathBuffer, pFileStart, false, pModuleDefinition ) )
- {
- // Warn.
- Con::warnf( "AssetManager::addDeclaredAsset() - Could not scan declared assets at location '%s' with extension '%s'.", assetFilePathBuffer, pFileStart );
- return false;
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- StringTableEntry AssetManager::addPrivateAsset( AssetBase* pAssetBase )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_AddPrivateAsset);
- // Sanity!
- AssertFatal( pAssetBase != NULL, "Cannot add a NULL private asset." );
- // Is the asset already added?
- if (pAssetBase->mpAssetDefinition->mAssetId != StringTable->EmptyString())
- {
- // Yes, so warn.
- Con::warnf( "Cannot add private asset '%d' as it has already been assigned.", pAssetBase->mpAssetDefinition->mAssetId );
- return StringTable->EmptyString();
- }
- static U32 masterPrivateAssetId = 1;
- // Create asset definition.
- AssetDefinition* pAssetDefinition = new AssetDefinition();
- // Fetch source asset definition.
- AssetDefinition* pSourceAssetDefinition = pAssetBase->mpAssetDefinition;
- // Configure asset.
- pAssetDefinition->mpAssetBase = pAssetBase;
- pAssetDefinition->mAssetDescription = pSourceAssetDefinition->mAssetDescription;
- pAssetDefinition->mAssetCategory = pSourceAssetDefinition->mAssetCategory;
- pAssetDefinition->mAssetAutoUnload = true;
- pAssetDefinition->mAssetRefreshEnable = false;
- pAssetDefinition->mAssetType = StringTable->insert( pAssetBase->getClassName() );
- pAssetDefinition->mAssetLoadedCount = 0;
- pAssetDefinition->mAssetInternal = false;
- pAssetDefinition->mAssetPrivate = true;
- // Format asset name.
- char assetNameBuffer[256];
- dSprintf(assetNameBuffer, sizeof(assetNameBuffer), "%s_%d", pAssetDefinition->mAssetType, masterPrivateAssetId++ );
- // Set asset identity.
- pAssetDefinition->mAssetName = StringTable->insert( assetNameBuffer );
- pAssetDefinition->mAssetId = pAssetDefinition->mAssetName;
- // Ensure that the source asset is fully synchronized with the new asset definition.
- pSourceAssetDefinition->mAssetName = pAssetDefinition->mAssetName;
- pSourceAssetDefinition->mAssetAutoUnload = pAssetDefinition->mAssetAutoUnload;
- pSourceAssetDefinition->mAssetInternal = pAssetDefinition->mAssetInternal;
- // Set ownership by asset manager.
- pAssetDefinition->mpAssetBase->setOwned( this, pAssetDefinition );
- // Store in declared assets.
- mDeclaredAssets.insert( pAssetDefinition->mAssetId, pAssetDefinition );
- // Increase the private loaded asset count.
- if ( ++mLoadedPrivateAssetsCount > mMaxLoadedPrivateAssetsCount )
- mMaxLoadedPrivateAssetsCount = mLoadedPrivateAssetsCount;
- return pAssetDefinition->mAssetId;
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::removeDeclaredAssets( ModuleDefinition* pModuleDefinition )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_RemoveDeclaredAssets);
- // Sanity!
- AssertFatal( pModuleDefinition != NULL, "Cannot remove declared assets using a NULL module definition" );
- // Fetch module assets.
- ModuleDefinition::typeModuleAssetsVector& moduleAssets = pModuleDefinition->getModuleAssets();
- // Remove all module assets.
- while ( moduleAssets.size() > 0 )
- {
- // Fetch asset definition.
- AssetDefinition* pAssetDefinition = *moduleAssets.begin();
- // Remove this asset.
- removeDeclaredAsset( pAssetDefinition->mAssetId );
- }
- // Info.
- if ( mEchoInfo )
- Con::printSeparator();
- return true;
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::removeDeclaredAsset( const char* pAssetId )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_RemoveSingleDeclaredAsset);
- // Sanity!
- AssertFatal( pAssetId != NULL, "Cannot remove single declared asset using NULL asset Id." );
- // Fetch asset Id.
- StringTableEntry assetId = StringTable->insert( pAssetId );
- // Find declared asset.
- typeDeclaredAssetsHash::iterator declaredAssetItr = mDeclaredAssets.find( assetId );
- // Did we find the declared asset?
- if ( declaredAssetItr == mDeclaredAssets.end() )
- {
- // No, so warn.
- Con::warnf( "Asset Manager: Cannot remove single asset Id '%s' it could not be found.", assetId );
- return false;
- }
- // Fetch asset definition.
- AssetDefinition* pAssetDefinition = declaredAssetItr->value;
- // Is the asset private?
- if ( !pAssetDefinition->mAssetPrivate )
- {
- // No, so fetch module assets.
- ModuleDefinition::typeModuleAssetsVector& moduleAssets = pAssetDefinition->mpModuleDefinition->getModuleAssets();
- // Remove module asset.
- for ( ModuleDefinition::typeModuleAssetsVector::iterator moduleAssetItr = moduleAssets.begin(); moduleAssetItr != moduleAssets.end(); ++moduleAssetItr )
- {
- if ( *moduleAssetItr == pAssetDefinition )
- {
- moduleAssets.erase( moduleAssetItr );
- break;
- }
- }
- // Remove asset dependencies.
- removeAssetDependencies( pAssetId );
- }
- // Do we have an asset loaded?
- if ( pAssetDefinition->mpAssetBase != NULL )
- {
- // Yes, so delete it.
- // NOTE: If anything is using this then this'll cause a crash. Objects should always use safe reference methods however.
- pAssetDefinition->mpAssetBase->deleteObject();
- }
- // Remove from declared assets.
- mDeclaredAssets.erase( declaredAssetItr );
- // Info.
- if ( mEchoInfo )
- {
- Con::printf( "Asset Manager: Removing Asset Id '%s' of type '%s' in asset file '%s'.",
- pAssetDefinition->mAssetId,
- pAssetDefinition->mAssetType,
- pAssetDefinition->mAssetBaseFilePath );
- }
- // Destroy asset definition.
- delete pAssetDefinition;
- return true;
- }
- //-----------------------------------------------------------------------------
- StringTableEntry AssetManager::getAssetName( const char* pAssetId )
- {
- // Find asset definition.
- AssetDefinition* pAssetDefinition = findAsset( pAssetId );
- // Did we find the asset?
- if ( pAssetDefinition == NULL )
- {
- // No, so warn.
- Con::warnf( "Asset Manager: Cannot find asset Id '%s'.", pAssetId );
- return NULL;
- }
- return pAssetDefinition->mAssetName;
- }
- //-----------------------------------------------------------------------------
- StringTableEntry AssetManager::getAssetDescription( const char* pAssetId )
- {
- // Find asset definition.
- AssetDefinition* pAssetDefinition = findAsset( pAssetId );
- // Did we find the asset?
- if ( pAssetDefinition == NULL )
- {
- // No, so warn.
- Con::warnf( "Asset Manager: Cannot find asset Id '%s'.", pAssetId );
- return NULL;
- }
- return pAssetDefinition->mAssetDescription;
- }
- //-----------------------------------------------------------------------------
- StringTableEntry AssetManager::getAssetCategory( const char* pAssetId )
- {
- // Find asset definition.
- AssetDefinition* pAssetDefinition = findAsset( pAssetId );
- // Did we find the asset?
- if ( pAssetDefinition == NULL )
- {
- // No, so warn.
- Con::warnf( "Asset Manager: Cannot find asset Id '%s'.", pAssetId );
- return NULL;
- }
- return pAssetDefinition->mAssetCategory;
- }
- //-----------------------------------------------------------------------------
- StringTableEntry AssetManager::getAssetType( const char* pAssetId )
- {
- // Find asset definition.
- AssetDefinition* pAssetDefinition = findAsset( pAssetId );
- // Did we find the asset?
- if ( pAssetDefinition == NULL )
- {
- // No, so warn.
- Con::warnf( "Asset Manager: Cannot find asset Id '%s'.", pAssetId );
- return NULL;
- }
- return pAssetDefinition->mAssetType;
- }
- //-----------------------------------------------------------------------------
- StringTableEntry AssetManager::getAssetFilePath( const char* pAssetId )
- {
- // Find asset definition.
- AssetDefinition* pAssetDefinition = findAsset( pAssetId );
- // Did we find the asset?
- if ( pAssetDefinition == NULL )
- {
- // No, so warn.
- Con::warnf( "Asset Manager: Cannot find asset Id '%s'.", pAssetId );
- return StringTable->EmptyString();
- }
- return pAssetDefinition->mAssetBaseFilePath;
- }
- //-----------------------------------------------------------------------------
- StringTableEntry AssetManager::getAssetPath( const char* pAssetId )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_GetAssetPath);
- // Fetch asset file-path.
- StringTableEntry assetFilePath = getAssetFilePath( pAssetId );
- // Finish if no file-path.
- if ( assetFilePath == StringTable->EmptyString() )
- return assetFilePath;
- // Find the final slash which should be just before the file.
- const char* pFinalSlash = dStrrchr( assetFilePath, '/' );
- // Sanity!
- AssertFatal( pFinalSlash != NULL, "Should always be able to find final slash in the asset file-path." );
- // Fetch asset path.
- return StringTable->insertn( assetFilePath, (U32)(pFinalSlash - assetFilePath) );
- }
- //-----------------------------------------------------------------------------
- ModuleDefinition* AssetManager::getAssetModuleDefinition( const char* pAssetId )
- {
- // Find asset definition.
- AssetDefinition* pAssetDefinition = findAsset( pAssetId );
- // Did we find the asset?
- if ( pAssetDefinition == NULL )
- {
- // No, so warn.
- Con::warnf( "Asset Manager: Cannot find asset Id '%s'.", pAssetId );
- return NULL;
- }
- return pAssetDefinition->mpModuleDefinition;
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::isAssetInternal( const char* pAssetId )
- {
- // Find asset definition.
- AssetDefinition* pAssetDefinition = findAsset( pAssetId );
- // Did we find the asset?
- if ( pAssetDefinition == NULL )
- {
- // No, so warn.
- Con::warnf( "Asset Manager: Cannot find asset Id '%s'.", pAssetId );
- return false;
- }
- return pAssetDefinition->mAssetInternal;
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::isAssetPrivate( const char* pAssetId )
- {
- // Find asset definition.
- AssetDefinition* pAssetDefinition = findAsset( pAssetId );
- // Did we find the asset?
- if ( pAssetDefinition == NULL )
- {
- // No, so warn.
- Con::warnf( "Asset Manager: Cannot find asset Id '%s'.", pAssetId );
- return false;
- }
- return pAssetDefinition->mAssetPrivate;
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::isAssetAutoUnload( const char* pAssetId )
- {
- // Find asset definition.
- AssetDefinition* pAssetDefinition = findAsset( pAssetId );
- // Did we find the asset?
- if ( pAssetDefinition == NULL )
- {
- // No, so warn.
- Con::warnf( "Asset Manager: Cannot find asset Id '%s'.", pAssetId );
- return false;
- }
- return pAssetDefinition->mAssetAutoUnload;
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::isAssetLoaded( const char* pAssetId )
- {
- // Find asset definition.
- AssetDefinition* pAssetDefinition = findAsset( pAssetId );
- // Did we find the asset?
- if ( pAssetDefinition == NULL )
- {
- // No, so warn.
- Con::warnf( "Asset Manager: Cannot find asset Id '%s'.", pAssetId );
- return false;
- }
- return pAssetDefinition->mpAssetBase != NULL;
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::isDeclaredAsset( const char* pAssetId )
- {
- return findAsset( pAssetId ) != NULL;
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::doesAssetDependOn( const char* pAssetId, const char* pDependsOnAssetId )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_DoesAssetDependOn);
- // Sanity!
- AssertFatal( pAssetId != NULL, "Cannot use NULL asset Id." );
- AssertFatal( pDependsOnAssetId != NULL, "Cannot use NULL depends-on asset Id." );
- // Fetch asset Id.
- StringTableEntry assetId = StringTable->insert( pAssetId );
- // Fetch depends-on asset Id.
- StringTableEntry dependsOnAssetId = StringTable->insert( pDependsOnAssetId );
- // Find depends-on entry.
- typeAssetDependsOnHash::Iterator dependsOnItr = mAssetDependsOn.find( assetId );
- // Iterate all dependencies.
- while( dependsOnItr != mAssetDependsOn.end() && dependsOnItr->key == assetId )
- {
- // Finish if a depends on.
- if ( dependsOnItr->value == dependsOnAssetId )
- return true;
- // Next dependency.
- dependsOnItr++;
- }
- return false;
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::isAssetDependedOn( const char* pAssetId, const char* pDependedOnByAssetId )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_IsAssetDependedOn);
- // Sanity!
- AssertFatal( pAssetId != NULL, "Cannot use NULL asset Id." );
- AssertFatal( pDependedOnByAssetId != NULL, "Cannot use NULL depended-on-by asset Id." );
- // Fetch asset Id.
- StringTableEntry assetId = StringTable->insert( pAssetId );
- // Fetch depended-on-by asset Id.
- StringTableEntry dependedOnByAssetId = StringTable->insert( pDependedOnByAssetId );
- // Find depended-on-by entry.
- typeAssetDependsOnHash::Iterator dependedOnItr = mAssetIsDependedOn.find(assetId);
- // Iterate all dependencies.
- while( dependedOnItr != mAssetIsDependedOn.end() && dependedOnItr->key == assetId )
- {
- // Finish if depended-on.
- if ( dependedOnItr->value == dependedOnByAssetId )
- return true;
- // Next dependency.
- dependedOnItr++;
- }
- return false;
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::isReferencedAsset( const char* pAssetId )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_IsReferencedAsset);
- // Sanity!
- AssertFatal( pAssetId != NULL, "Cannot check if NULL asset Id is referenced." );
- // Fetch asset Id.
- StringTableEntry assetId = StringTable->insert( pAssetId );
- // Is asset Id the correct format?
- if ( StringUnit::getUnitCount( assetId, ASSET_SCOPE_TOKEN ) != 2 )
- {
- // No, so warn.
- Con::warnf( "Asset Manager: Cannot check if asset Id '%s' is referenced as it is not the correct format.", assetId );
- return false;
- }
- return mReferencedAssets.count( assetId ) > 0;
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::renameDeclaredAsset( const char* pAssetIdFrom, const char* pAssetIdTo )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_RenameDeclaredAsset);
- // Sanity!
- AssertFatal( pAssetIdFrom != NULL, "Cannot rename from NULL asset Id." );
- AssertFatal( pAssetIdTo != NULL, "Cannot rename to NULL asset Id." );
- // Fetch asset Ids.
- StringTableEntry assetIdFrom = StringTable->insert( pAssetIdFrom );
- StringTableEntry assetIdTo = StringTable->insert( pAssetIdTo );
- // Is asset Id from the correct format?
- if ( StringUnit::getUnitCount( assetIdFrom, ASSET_SCOPE_TOKEN ) != 2 )
- {
- // No, so warn.
- 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 );
- return false;
- }
- // Is asset Id to the correct format?
- if ( StringUnit::getUnitCount( assetIdTo, ASSET_SCOPE_TOKEN ) != 2 )
- {
- // No, so warn.
- 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 );
- return false;
- }
- // Does the asset Id from exist?
- if ( !mDeclaredAssets.contains( assetIdFrom ) )
- {
- // No, so warn.
- Con::warnf("Asset Manager: Cannot rename declared asset Id '%s' to asset Id '%s' as source asset Id is not declared.", assetIdFrom, assetIdTo );
- return false;
- }
- // Does the asset Id to exist?
- if ( mDeclaredAssets.contains( assetIdTo ) )
- {
- // No, so warn.
- Con::warnf("Asset Manager: Cannot rename declared asset Id '%s' to asset Id '%s' as target asset Id is already declared.", assetIdFrom, assetIdTo );
- return false;
- }
- // Split module Ids from asset Ids.
- StringTableEntry moduleIdFrom = StringTable->insert( StringUnit::getUnit( assetIdFrom, 0, ASSET_SCOPE_TOKEN ) );
- StringTableEntry moduleIdTo = StringTable->insert( StringUnit::getUnit( assetIdTo, 0, ASSET_SCOPE_TOKEN ) );
- // Are the module Ids the same?
- if ( moduleIdFrom != moduleIdTo )
- {
- // No, so warn.
- Con::warnf("Asset Manager: Cannot rename declared asset Id '%s' to asset Id '%s' as the module Id cannot be changed.", assetIdFrom, assetIdTo );
- return false;
- }
- // Find asset definition.
- typeDeclaredAssetsHash::iterator assetDefinitionItr = mDeclaredAssets.find( assetIdFrom );
- // Sanity!
- AssertFatal( assetDefinitionItr != mDeclaredAssets.end(), "Asset Manager: Failed to find asset." );
- // Fetch asset definition.
- AssetDefinition* pAssetDefinition = assetDefinitionItr->value;
- // Is this a private asset?
- if ( pAssetDefinition->mAssetPrivate )
- {
- // Yes, so warn.
- Con::warnf("Asset Manager: Cannot rename declared asset Id '%s' to asset Id '%s' as the source asset is private.", assetIdFrom, assetIdTo );
- return false;
- }
- // Setup declared update visitor.
- TamlAssetDeclaredUpdateVisitor assetDeclaredUpdateVisitor;
- assetDeclaredUpdateVisitor.setAssetIdFrom( assetIdFrom );
- assetDeclaredUpdateVisitor.setAssetIdTo( assetIdTo );
- // Update asset file declaration.
- if ( !mTaml.parse( pAssetDefinition->mAssetBaseFilePath, assetDeclaredUpdateVisitor ) )
- {
- // No, so warn.
- Con::warnf("Asset Manager: Cannot rename declared asset Id '%s' to asset Id '%s' as the declared asset file could not be parsed: %s",
- assetIdFrom, assetIdTo, pAssetDefinition->mAssetBaseFilePath );
- return false;
- }
- // Update asset definition.
- pAssetDefinition->mAssetId = assetIdTo;
- pAssetDefinition->mAssetName = StringTable->insert( StringUnit::getUnit( assetIdTo, 1, ASSET_SCOPE_TOKEN ) );
- // Reinsert declared asset.
- mDeclaredAssets.erase( assetIdFrom );
- mDeclaredAssets.insert( assetIdTo, pAssetDefinition );
- // Info.
- if ( mEchoInfo )
- {
- Con::printf( "Asset Manager: Renaming declared Asset Id '%s' to Asset Id '%s'.", assetIdFrom, assetIdTo );
- Con::printSeparator();
- }
- // Rename asset dependencies.
- renameAssetDependencies( assetIdFrom, assetIdTo );
- // Do we have an asset tags manifest?
- if ( !mAssetTagsManifest.isNull() )
- {
- // Yes, so rename any assets.
- mAssetTagsManifest->renameAssetId( pAssetIdFrom, pAssetIdTo );
- // Save the asset tags.
- saveAssetTags();
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::renameReferencedAsset( const char* pAssetIdFrom, const char* pAssetIdTo )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_RenameReferencedAsset);
- // Sanity!
- AssertFatal( pAssetIdFrom != NULL, "Cannot rename from NULL asset Id." );
- AssertFatal( pAssetIdTo != NULL, "Cannot rename to NULL asset Id." );
- // Fetch asset Ids.
- StringTableEntry assetIdFrom = StringTable->insert( pAssetIdFrom );
- StringTableEntry assetIdTo = StringTable->insert( pAssetIdTo );
- // Is asset Id from the correct format?
- if ( StringUnit::getUnitCount( assetIdFrom, ASSET_SCOPE_TOKEN ) != 2 )
- {
- // No, so warn.
- 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 );
- return false;
- }
- // Is asset Id to the correct format?
- if ( StringUnit::getUnitCount( assetIdTo, ASSET_SCOPE_TOKEN ) != 2 )
- {
- // No, so warn.
- 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 );
- return false;
- }
- // Does the asset Id to exist?
- if ( !mDeclaredAssets.contains( assetIdTo ) )
- {
- // No, so warn.
- Con::warnf("Asset Manager: Cannot rename referenced asset Id '%s' to asset Id '%s' as target asset Id is not declared.", assetIdFrom, assetIdTo );
- return false;
- }
- // Rename asset references.
- renameAssetReferences( assetIdFrom, assetIdTo );
- // Info.
- if ( mEchoInfo )
- Con::printSeparator();
- return true;
- }
- bool AssetManager::compileAllAssets(const bool compressed, const bool includeUnloaded)
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_CompileAllAssets);
- // Info.
- if (mEchoInfo)
- {
- Con::printSeparator();
- Con::printf("Asset Manager: Started compiling ALL assets.");
- }
- Vector<typeAssetId> assetsToRelease;
- // Are we including unloaded assets?
- if (includeUnloaded)
- {
- // Yes, so prepare a list of assets to release and load them.
- for (typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr)
- {
- // Fetch asset Id.
- typeAssetId assetId = assetItr->key;
- // Skip if asset is loaded.
- if (assetItr->value->mpAssetBase != NULL)
- continue;
- // Note asset as needing a release.
- assetsToRelease.push_back(assetId);
- // Acquire the asset.
- acquireAsset<AssetBase>(assetId);
- }
- }
- bool oldCompressed = mTaml.getBinaryCompression();
- mTaml.setBinaryCompression(compressed);
- bool success = false;
- // Refresh the current loaded assets.
- // NOTE: This will result in some assets being refreshed more than once due to asset dependencies.
- for (typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr)
- {
- // Skip private assets.
- if (assetItr->value->mAssetPrivate)
- continue;
- // Refresh asset if it's loaded.
- success = compileAsset(assetItr->key);
- if (!success)
- {
- break;
- }
- }
- mTaml.setBinaryCompression(oldCompressed);
- // Are we including unloaded assets?
- if (includeUnloaded)
- {
- // Yes, so release the assets we loaded.
- for (Vector<typeAssetId>::iterator assetItr = assetsToRelease.begin(); assetItr != assetsToRelease.end(); ++assetItr)
- {
- releaseAsset(*assetItr);
- }
- }
- // Info.
- if (mEchoInfo)
- {
- Con::printSeparator();
- Con::printf("Asset Manager: Finished compiling ALL assets.");
- }
- return success;
- }
- bool AssetManager::compileModuleAssets(ModuleDefinition* pModuleDefinition)
- {
- // Sanity!
- AssertFatal(pModuleDefinition != NULL, "Cannot remove declared assets using a NULL module definition");
- // Fetch module assets.
- ModuleDefinition::typeModuleAssetsVector& moduleAssets = pModuleDefinition->getModuleAssets();
- // Remove all module assets.
- while (moduleAssets.size() > 0)
- {
- // Fetch asset definition.
- AssetDefinition* pAssetDefinition = *moduleAssets.begin();
- bool success = compileAsset(pAssetDefinition->mAssetId);
- if (!success)
- return false;
- }
- return true;
- }
- bool AssetManager::compileAsset(const char* pAssetId)
- {
- // Sanity!
- AssertFatal(pAssetId != NULL, "Cannot compile NULL asset");
- AssetDefinition* pAssetDefinition = findAsset(pAssetId);
- // Does the asset exist?
- if (pAssetDefinition == NULL)
- {
- Con::warnf("Asset Manager::compileAsset Failed to compile asset Id '%s' as it does not exist", pAssetId);
- return false;
- }
- // Info.
- if (mEchoInfo)
- {
- Con::printSeparator();
- Con::printf("Asset Manager::compileAsset Started compiling Asset Id '%s'...", pAssetId);
- }
- AssetBase* pAssetBase = pAssetDefinition->mpAssetBase;
- if (pAssetBase != NULL)
- {
- Torque::Path binaryPath = pAssetDefinition->mAssetBaseFilePath;
- binaryPath.setExtension(mTaml.getAutoFormatBinaryExtension());
- // Save asset.
- mTaml.write(pAssetBase, binaryPath.getFullPath().c_str());
- }
- else
- {
- Con::warnf("Asset Manager::compileAsset Failed to compile asset Id '%s' as it does not have an assetBase", pAssetId);
- return false;
- }
- // Info.
- if (mEchoInfo)
- {
- Con::printSeparator();
- Con::printf("Asset Manager: Finished compiling Asset Id '%s'.", pAssetId);
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::releaseAsset( const char* pAssetId )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_ReleaseAsset);
- // Sanity!
- AssertFatal( pAssetId != NULL, "Cannot release NULL asset Id." );
- // Find asset.
- AssetDefinition* pAssetDefinition = findAsset( pAssetId );
- // Did we find the asset?
- if ( pAssetDefinition == NULL )
- {
- // No, so warn.
- Con::warnf( "Asset Manager: Failed to release asset Id '%s' as it does not exist.", pAssetId );
- return false;
- }
- // Is the asset loaded?
- if ( pAssetDefinition->mpAssetBase == NULL )
- {
- // No, so warn.
- Con::warnf( "Asset Manager: Failed to release asset Id '%s' as it is not acquired.", pAssetId );
- return false;
- }
- // Info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Asset Manager: Started releasing Asset Id '%s'...", pAssetId );
- }
- // Release asset reference.
- if ( pAssetDefinition->mpAssetBase->releaseAssetReference() )
- {
- // Are we ignoring auto-unloaded assets?
- if ( mIgnoreAutoUnload )
- {
- // Yes, so info.
- if ( mEchoInfo )
- {
- Con::printf( "Asset Manager: > Releasing to idle state." );
- }
- }
- else
- {
- // No, so info.
- if ( mEchoInfo )
- {
- Con::printf( "Asset Manager: > Unload the asset from memory." );
- }
- // Unload the asset.
- unloadAsset( pAssetDefinition );
- }
- }
- // Info.
- else if ( mEchoInfo )
- {
- Con::printf( "Asset Manager: > Reference count now '%d'.", pAssetDefinition->mpAssetBase->getAcquiredReferenceCount() );
- }
- // Info.
- if ( mEchoInfo )
- {
- Con::printf( "Asset Manager: > Finished releasing Asset Id '%s'.", pAssetId );
- Con::printSeparator();
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- void AssetManager::purgeAssets( void )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_PurgeAssets);
- // Info.
- if ( mEchoInfo )
- {
- Con::printf( "Asset Manager: Started purging assets..." );
- }
- // Iterate asset definitions.
- for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
- {
- // Fetch asset definition.
- AssetDefinition* pAssetDefinition = assetItr->value;
- // Skip asset if private, not loaded or referenced.
- if ( pAssetDefinition->mAssetPrivate ||
- pAssetDefinition->mpAssetBase == NULL ||
- pAssetDefinition->mpAssetBase->getAcquiredReferenceCount() > 0 )
- continue;
- // Info.
- if ( mEchoInfo )
- {
- Con::printf( "Asset Manager: Purging asset Id '%s'...", pAssetDefinition->mAssetId );
- }
- // Unload the asset.
- unloadAsset( pAssetDefinition );
- }
- // Info.
- if ( mEchoInfo )
- {
- Con::printf( "Asset Manager: ... Finished purging assets." );
- }
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::deleteAsset( const char* pAssetId, const bool deleteLooseFiles, const bool deleteDependencies )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_DeleteAsset);
- // Sanity!
- AssertFatal( pAssetId != NULL, "Cannot delete NULL asset Id." );
- // Find asset.
- AssetDefinition* pAssetDefinition = findAsset( pAssetId );
- // Did we find the asset?
- if ( pAssetDefinition == NULL )
- {
- // No, so warn.
- Con::warnf( "Asset Manager: Failed to delete asset Id '%s' as it does not exist.", pAssetId );
- return false;
- }
- // Info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Asset Manager: Started deleting Asset Id '%s'...", pAssetId );
- }
- // Fetch asset Id.
- StringTableEntry assetId = StringTable->insert( pAssetId );
- // Are we deleting dependencies?
- if ( deleteDependencies )
- {
- Vector<typeAssetId> dependantAssets;
- // Yes, so find depended-on-by entry.
- typeAssetDependsOnHash::Iterator dependedOnItr = mAssetIsDependedOn.find( assetId );
- // Iterate all dependencies.
- while( dependedOnItr != mAssetIsDependedOn.end() && dependedOnItr->key == assetId )
- {
- // Store asset Id.
- dependantAssets.push_back( dependedOnItr->value );
- // Next dependency.
- dependedOnItr++;
- }
- // Do we have any dependants?
- if ( dependantAssets.size() > 0 )
- {
- // Yes, so iterate dependants.
- for( Vector<typeAssetId>::const_iterator assetItr = dependantAssets.begin(); assetItr != dependantAssets.end(); ++assetItr )
- {
- StringTableEntry dependentAssetId = *assetItr;
- // Info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Asset Manager: Deleting Asset Id '%s' dependant of '%s.'", pAssetId, dependentAssetId );
- }
- // Delete dependant.
- deleteAsset( dependentAssetId, deleteLooseFiles, deleteDependencies );
- }
- }
- }
- // Remove asset references.
- removeAssetReferences( assetId );
- // Are we deleting loose files?
- if ( deleteLooseFiles )
- {
- // Yes, so remove loose files.
- Vector<StringTableEntry>& assetLooseFiles = pAssetDefinition->mAssetLooseFiles;
- for( Vector<StringTableEntry>::iterator looseFileItr = assetLooseFiles.begin(); looseFileItr != assetLooseFiles.end(); ++looseFileItr )
- {
- // Fetch loose file.
- StringTableEntry looseFile = *looseFileItr;
- // Delete the loose file.
- if ( !dFileDelete( looseFile ) )
- {
- // Failed so warn.
- Con::warnf( "Asset Manager: Failed to delete the loose file '%s' while deleting asset Id '%s'.", looseFile, pAssetId );
- }
- }
- }
- // Fetch asset definition file.
- StringTableEntry assetDefinitionFile = pAssetDefinition->mAssetBaseFilePath;
- // Remove reference here as we're about to remove the declared asset.
- pAssetDefinition = NULL;
- // Remove asset.
- removeDeclaredAsset( pAssetId );
- // Delete the asset definition file.
- if ( !dFileDelete( assetDefinitionFile ) )
- {
- // Failed so warn.
- Con::warnf( "Asset Manager: Failed to delete the asset definition file '%s' while deleting asset Id '%s'.", assetDefinitionFile, pAssetId );
- }
- // Info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Asset Manager: Finished deleting Asset Id '%s'.", pAssetId );
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::refreshAsset( const char* pAssetId )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_RefreshAsset);
- // Sanity!
- AssertFatal( pAssetId != NULL, "Cannot refresh NULL asset Id." );
- // Find asset.
- AssetDefinition* pAssetDefinition = findAsset( pAssetId );
- // Did we find the asset?
- if ( pAssetDefinition == NULL )
- {
- // No, so warn.
- Con::warnf( "Asset Manager: Failed to refresh asset Id '%s' as it does not exist.", pAssetId );
- return false;
- }
- // Info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Asset Manager: Started refreshing Asset Id '%s'...", pAssetId );
- }
- // Fetch asset Id.
- StringTableEntry assetId = StringTable->insert( pAssetId );
- // Is the asset private?
- if ( pAssetDefinition->mAssetPrivate )
- {
- // Yes, so notify asset of asset refresh only.
- pAssetDefinition->mpAssetBase->onAssetRefresh();
- // Asset refresh notifications.
- for( typeAssetPtrRefreshHash::iterator refreshNotifyItr = mAssetPtrRefreshNotifications.begin(); refreshNotifyItr != mAssetPtrRefreshNotifications.end(); ++refreshNotifyItr )
- {
- // Fetch pointed asset.
- StringTableEntry pointedAsset = refreshNotifyItr->key->getAssetId();
- // Ignore if the pointed asset is not the asset or a dependency.
- if ( pointedAsset == StringTable->EmptyString() || ( pointedAsset != assetId && !doesAssetDependOn( pointedAsset, assetId ) ) )
- continue;
- // Perform refresh notification callback.
- refreshNotifyItr->value->onAssetRefreshed( refreshNotifyItr->key );
- }
- }
- // Is the asset definition allowed to refresh?
- else if ( pAssetDefinition->mAssetRefreshEnable )
- {
- // Yes, so fetch the asset.
- AssetBase* pAssetBase = pAssetDefinition->mpAssetBase;
- // Is the asset loaded?
- if ( pAssetBase != NULL )
- {
- // Yes, so notify asset of asset refresh.
- pAssetBase->onAssetRefresh();
- // Save asset.
- mTaml.write( pAssetBase, pAssetDefinition->mAssetBaseFilePath );
-
- // Remove asset dependencies.
- removeAssetDependencies( pAssetId );
- // Find any new dependencies.
- TamlAssetDeclaredVisitor assetDeclaredVisitor;
- // Parse the filename.
- if ( !mTaml.parse( pAssetDefinition->mAssetBaseFilePath, assetDeclaredVisitor ) )
- {
- // Warn.
- Con::warnf( "Asset Manager: Failed to parse file containing asset declaration: '%s'.\nDependencies are now incorrect!", pAssetDefinition->mAssetBaseFilePath );
- return false;
- }
- // Fetch asset dependencies.
- TamlAssetDeclaredVisitor::typeAssetIdVector& assetDependencies = assetDeclaredVisitor.getAssetDependencies();
- // Are there any asset dependences?
- if ( assetDependencies.size() > 0 )
- {
- // Yes, so iterate dependencies.
- for( TamlAssetDeclaredVisitor::typeAssetIdVector::iterator assetDependencyItr = assetDependencies.begin(); assetDependencyItr != assetDependencies.end(); ++assetDependencyItr )
- {
- // Fetch dependency asset Id.
- StringTableEntry dependencyAssetId = *assetDependencyItr;
- // Insert depends-on.
- mAssetDependsOn.insertEqual( assetId, dependencyAssetId );
- // Insert is-depended-on.
- mAssetIsDependedOn.insertEqual( dependencyAssetId, assetId );
- }
- }
- // Fetch asset loose files.
- TamlAssetDeclaredVisitor::typeLooseFileVector& assetLooseFiles = assetDeclaredVisitor.getAssetLooseFiles();
- // Clear any existing loose files.
- pAssetDefinition->mAssetLooseFiles.clear();
- // Are there any loose files?
- if ( assetLooseFiles.size() > 0 )
- {
- // Yes, so iterate loose files.
- for( TamlAssetDeclaredVisitor::typeLooseFileVector::iterator assetLooseFileItr = assetLooseFiles.begin(); assetLooseFileItr != assetLooseFiles.end(); ++assetLooseFileItr )
- {
- // Store loose file.
- pAssetDefinition->mAssetLooseFiles.push_back( *assetLooseFileItr );
- }
- }
- // Asset refresh notifications.
- for( typeAssetPtrRefreshHash::iterator refreshNotifyItr = mAssetPtrRefreshNotifications.begin(); refreshNotifyItr != mAssetPtrRefreshNotifications.end(); ++refreshNotifyItr )
- {
- // Fetch pointed asset.
- StringTableEntry pointedAsset = refreshNotifyItr->key->getAssetId();
- // Ignore if the pointed asset is not the asset or a dependency.
- if ( pointedAsset == StringTable->EmptyString() || ( pointedAsset != assetId && !doesAssetDependOn( pointedAsset, assetId ) ) )
- continue;
- // Perform refresh notification callback.
- refreshNotifyItr->value->onAssetRefreshed( refreshNotifyItr->key );
- }
- // Find is-depends-on entry.
- typeAssetIsDependedOnHash::Iterator isDependedOnItr = mAssetIsDependedOn.find( assetId );
- // Is asset depended on?
- if ( isDependedOnItr != mAssetIsDependedOn.end() )
- {
- // Yes, so compiled them.
- Vector<typeAssetId> dependedOn;
- // Iterate all dependencies.
- while( isDependedOnItr != mAssetIsDependedOn.end() && isDependedOnItr->key == assetId )
- {
- dependedOn.push_back( isDependedOnItr->value );
- // Next dependency.
- isDependedOnItr++;
- }
- // Refresh depended-on assets.
- for ( Vector<typeAssetId>::iterator refreshItr = dependedOn.begin(); refreshItr != dependedOn.end(); ++refreshItr)
- {
- // Refresh dependency asset.
- refreshAsset( *refreshItr);
- }
- }
- }
- }
- // Info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Asset Manager: Finished refreshing Asset Id '%s'.", pAssetId );
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- void AssetManager::refreshAllAssets( const bool includeUnloaded )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_RefreshAllAssets);
- // Info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Asset Manager: Started refreshing ALL assets." );
- }
- Vector<typeAssetId> assetsToRelease;
- // Are we including unloaded assets?
- if ( includeUnloaded )
- {
- // Yes, so prepare a list of assets to release and load them.
- for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
- {
- // Fetch asset Id.
- typeAssetId assetId = assetItr->key;
- // Skip if asset is loaded.
- if ( assetItr->value->mpAssetBase != NULL )
- continue;
- // Note asset as needing a release.
- assetsToRelease.push_back( assetId );
- // Acquire the asset.
- acquireAsset<AssetBase>( assetId );
- }
- }
- // Refresh the current loaded assets.
- // NOTE: This will result in some assets being refreshed more than once due to asset dependencies.
- for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
- {
- // Skip private assets.
- if ( assetItr->value->mAssetPrivate )
- continue;
- // Refresh asset if it's loaded.
- refreshAsset( assetItr->key );
- }
- // Are we including unloaded assets?
- if ( includeUnloaded )
- {
- // Yes, so release the assets we loaded.
- for( Vector<typeAssetId>::iterator assetItr = assetsToRelease.begin(); assetItr != assetsToRelease.end(); ++assetItr )
- {
- releaseAsset( *assetItr );
- }
- }
- // Info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Asset Manager: Finished refreshing ALL assets." );
- }
- }
- //-----------------------------------------------------------------------------
- void AssetManager::registerAssetPtrRefreshNotify( AssetPtrBase* pAssetPtrBase, AssetPtrCallback* pCallback )
- {
- // Find an existing notification iterator.
- typeAssetPtrRefreshHash::iterator notificationItr = mAssetPtrRefreshNotifications.find( pAssetPtrBase );
- // Do we have one?
- if ( notificationItr != mAssetPtrRefreshNotifications.end() )
- {
- // Yes, so update the callback.
- notificationItr->value = pCallback;
- return;
- }
- // No, so add one.
- mAssetPtrRefreshNotifications.insert( pAssetPtrBase, pCallback );
- }
- //-----------------------------------------------------------------------------
- void AssetManager::unregisterAssetPtrRefreshNotify( AssetPtrBase* pAssetPtrBase )
- {
- mAssetPtrRefreshNotifications.erase( pAssetPtrBase );
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::loadAssetTags( ModuleDefinition* pModuleDefinition )
- {
- // Sanity!
- AssertFatal( pModuleDefinition != NULL, "Cannot load asset tags manifest using a NULL module definition" );
- // Expand manifest location.
- char assetTagsManifestFilePathBuffer[1024];
- Con::expandPath( assetTagsManifestFilePathBuffer, sizeof(assetTagsManifestFilePathBuffer), pModuleDefinition->getAssetTagsManifest() );
- // Do we already have a manifest?
- if ( !mAssetTagsManifest.isNull() )
- {
- // Yes, so warn.
- Con::warnf( "Asset Manager: Cannot load asset tags manifest from module '%s' as one is already loaded.", pModuleDefinition->getSignature() );
- return false;
- }
- // Is the specified file valid?
- if (Torque::FS::IsFile( assetTagsManifestFilePathBuffer ) )
- {
- // Yes, so read asset tags manifest.
- mAssetTagsManifest = mTaml.read<AssetTagsManifest>( assetTagsManifestFilePathBuffer );
- // Did we read the manifest?
- if ( mAssetTagsManifest.isNull() )
- {
- // No, so warn.
- Con::warnf( "Asset Manager: Failed to load asset tags manifest '%s' from module '%s'.", assetTagsManifestFilePathBuffer, pModuleDefinition->getSignature() );
- return false;
- }
- // Set asset tags module definition.
- mAssetTagsModuleDefinition = pModuleDefinition;
- }
- else
- {
- // No, so generate a new asset tags manifest.
- mAssetTagsManifest = new AssetTagsManifest();
- mAssetTagsManifest->registerObject();
- // Set asset tags module definition.
- mAssetTagsModuleDefinition = pModuleDefinition;
- // Save the asset tags.
- saveAssetTags();
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::saveAssetTags( void )
- {
- // Do we have an asset tags manifest?
- if ( mAssetTagsManifest.isNull() || mAssetTagsModuleDefinition.isNull() )
- {
- // No, so warn.
- Con::warnf( "Asset Manager: Failed to save asset tags manifest as one is not loaded." );
- return false;
- }
- // Expand manifest location.
- char assetTagsManifestFilePathBuffer[1024];
- Con::expandPath( assetTagsManifestFilePathBuffer, sizeof(assetTagsManifestFilePathBuffer), mAssetTagsModuleDefinition->getAssetTagsManifest() );
- // Save asset tags manifest.
- if ( !mTaml.write( mAssetTagsManifest, assetTagsManifestFilePathBuffer ) )
- {
- // Failed so warn.
- Con::warnf( "Asset Manager: Failed to save asset tags manifest '%s' from module '%s'.", assetTagsManifestFilePathBuffer, mAssetTagsModuleDefinition->getSignature() );
- return false;
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::restoreAssetTags( void )
- {
- // Do we already have a manifest?
- if ( mAssetTagsManifest.isNull() )
- {
- // No, so warn.
- Con::warnf( "Asset Manager: Cannot restore asset tags manifest as one is not already loaded." );
- return false;
- }
- // Sanity!
- AssertFatal( mAssetTagsModuleDefinition != NULL, "Cannot restore asset tags manifest as module definition is NULL." );
- // Delete existing asset tags manifest.
- mAssetTagsManifest->deleteObject();
- // Reload asset tags manifest.
- return loadAssetTags( mAssetTagsModuleDefinition );
- }
- //-----------------------------------------------------------------------------
- const char* AssetManager::getAssetLooseFiles(const char* pAssetId)
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_getAssetLooseFIles);
- // Sanity!
- AssertFatal(pAssetId != NULL, "Cannot look up NULL asset Id.");
- // Find asset.
- AssetDefinition* pAssetDefinition = findAsset(pAssetId);
- // Did we find the asset?
- if (pAssetDefinition == NULL)
- {
- // No, so warn.
- Con::warnf("Asset Manager: Failed to find asset Id '%s' as it does not exist.", pAssetId);
- return String::EmptyString;
- }
- // Info.
- if (mEchoInfo)
- {
- Con::printSeparator();
- Con::printf("Asset Manager: Started getting loose files of Asset Id '%s'...", pAssetId);
- }
- String looseFileList = "";
- Vector<StringTableEntry>& assetLooseFiles = pAssetDefinition->mAssetLooseFiles;
- for (Vector<StringTableEntry>::iterator looseFileItr = assetLooseFiles.begin(); looseFileItr != assetLooseFiles.end(); ++looseFileItr)
- {
- // Fetch loose file.
- StringTableEntry looseFile = *looseFileItr;
- looseFileList += looseFile;
- if (looseFileItr != assetLooseFiles.end())
- looseFileList += "\t";
- }
- char* ret = Con::getReturnBuffer(1024);
- dSprintf(ret, 1024, "%s", looseFileList.c_str());
- return ret;
- }
- //-----------------------------------------------------------------------------
- S32 QSORT_CALLBACK descendingAssetDefinitionLoadCount(const void* a, const void* b)
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_DescendingAssetDefinitionLoadCount);
- // Fetch asset definitions.
- const AssetDefinition* pAssetDefinitionA = *(AssetDefinition**)a;
- const AssetDefinition* pAssetDefinitionB = *(AssetDefinition**)b;
- // Sort.
- return pAssetDefinitionB->mAssetLoadedCount - pAssetDefinitionA->mAssetLoadedCount;
- }
- //-----------------------------------------------------------------------------
- void AssetManager::dumpDeclaredAssets( void ) const
- {
- Vector<const AssetDefinition*> assetDefinitions;
- // Iterate asset definitions.
- for( typeDeclaredAssetsHash::const_iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
- {
- assetDefinitions.push_back( assetItr->value );
- }
- // Sort asset definitions.
- dQsort( assetDefinitions.address(), assetDefinitions.size(), sizeof(const AssetDefinition*), descendingAssetDefinitionLoadCount );
- // Info.
- Con::printSeparator();
- Con::printf( "Asset Manager: %d declared asset(s) dump as follows:", mDeclaredAssets.size() );
- Con::printBlankLine();
- // Iterate sorted asset definitions.
- for ( Vector<const AssetDefinition*>::iterator assetItr = assetDefinitions.begin(); assetItr != assetDefinitions.end(); ++assetItr )
- {
- // Fetch asset definition.
- const AssetDefinition* pAssetDefinition = *assetItr;
- // Info.
- 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'",
- pAssetDefinition->mAssetId,
- pAssetDefinition->mpAssetBase == NULL ? 0 : pAssetDefinition->mpAssetBase->getAcquiredReferenceCount(),
- pAssetDefinition->mAssetLoadedCount,
- pAssetDefinition->mAssetUnloadedCount,
- pAssetDefinition->mAssetAutoUnload,
- pAssetDefinition->mpAssetBase != NULL,
- pAssetDefinition->mAssetInternal,
- pAssetDefinition->mAssetPrivate,
- pAssetDefinition->mAssetType,
- pAssetDefinition->mAssetPrivate ? "Private" : pAssetDefinition->mpModuleDefinition->getModuleId(),
- pAssetDefinition->mAssetPrivate ? 0 : pAssetDefinition->mpModuleDefinition->getVersionId(),
- pAssetDefinition->mAssetBaseFilePath );
- }
- // Info.
- Con::printSeparator();
- Con::printBlankLine();
- }
- //-----------------------------------------------------------------------------
- S32 AssetManager::findAllAssets( AssetQuery* pAssetQuery, const bool ignoreInternal, const bool ignorePrivate )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_FindAllAssets);
- // Sanity!
- AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
- // Reset result count.
- S32 resultCount = 0;
- // Iterate declared assets.
- for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
- {
- // Fetch asset definition.
- AssetDefinition* pAssetDefinition = assetItr->value;
- // Skip if internal and we're ignoring them.
- if ( ignoreInternal && pAssetDefinition->mAssetInternal )
- continue;
- // Skip if private and we're ignoring them.
- if ( ignorePrivate && pAssetDefinition->mAssetPrivate )
- continue;
- // Store as result.
- pAssetQuery->mAssetList.push_back( pAssetDefinition->mAssetId );
- // Increase result count.
- resultCount++;
- }
- return resultCount;
- }
- //-----------------------------------------------------------------------------
- S32 AssetManager::findAssetName( AssetQuery* pAssetQuery, const char* pAssetName, const bool partialName )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_FindAssetName);
- // Sanity!
- AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
- AssertFatal( pAssetName != NULL, "Cannot use NULL asset name." );
- // Reset asset name.
- StringTableEntry assetName = NULL;
- S32 partialAssetNameLength = 0;
-
- // Are we doing partial name search?
- if ( partialName )
- {
- // Yes, so fetch length of partial name.
- partialAssetNameLength = dStrlen( pAssetName );
- }
- else
- {
- // No, so fetch asset name.
- assetName = StringTable->insert( pAssetName );
- }
- // Reset result count.
- S32 resultCount = 0;
- // Iterate declared assets.
- for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
- {
- // Fetch asset definition.
- AssetDefinition* pAssetDefinition = assetItr->value;
- // Are we doing partial name search?
- if ( partialName )
- {
- // Yes, so fetch the length of this asset name.
- const S32 currentAssetNameLength = dStrlen( pAssetDefinition->mAssetName );
- // Skip if the query asset name is longer than the current asset name.
- if ( partialAssetNameLength > currentAssetNameLength )
- continue;
-
- // Skip if this is not the asset we want.
- if ( dStrnicmp( pAssetDefinition->mAssetName, pAssetName, partialAssetNameLength ) != 0 )
- continue;
- }
- else
- {
- // No, so skip if this is not the asset we want.
- if ( assetName != pAssetDefinition->mAssetName )
- continue;
- }
- // Store as result.
- pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
- // Increase result count.
- resultCount++;
- }
- return resultCount;
- }
-
- //-----------------------------------------------------------------------------
- S32 AssetManager::findAssetCategory( AssetQuery* pAssetQuery, const char* pAssetCategory, const bool assetQueryAsSource )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_FindAssetCategory);
- // Sanity!
- AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
- AssertFatal( pAssetCategory != NULL, "Cannot use NULL asset category." );
- // Fetch asset category.
- StringTableEntry assetCategory = StringTable->insert( pAssetCategory );
- // Reset result count.
- S32 resultCount = 0;
- // Use asset-query as the source?
- if ( assetQueryAsSource )
- {
- AssetQuery filteredAssets;
- // Yes, so iterate asset query.
- for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
- {
- // Fetch asset definition.
- AssetDefinition* pAssetDefinition = findAsset( *assetItr );
- // Skip if this is not the asset we want.
- if ( pAssetDefinition == NULL ||
- pAssetDefinition->mAssetCategory != assetCategory )
- continue;
- // Store as result.
- filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
- // Increase result count.
- resultCount++;
- }
- // Set asset query.
- pAssetQuery->set( filteredAssets );
- }
- else
- {
- // No, so iterate declared assets.
- for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
- {
- // Fetch asset definition.
- AssetDefinition* pAssetDefinition = assetItr->value;
- // Skip if this is not the asset we want.
- if ( assetCategory != pAssetDefinition->mAssetCategory )
- continue;
- // Store as result.
- pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
- // Increase result count.
- resultCount++;
- }
- }
- return resultCount;
- }
- S32 AssetManager::findAssetAutoUnload( AssetQuery* pAssetQuery, const bool assetAutoUnload, const bool assetQueryAsSource )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_FindAssetAutoUnload);
- // Sanity!
- AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
- // Reset result count.
- S32 resultCount = 0;
- // Use asset-query as the source?
- if ( assetQueryAsSource )
- {
- AssetQuery filteredAssets;
- // Yes, so iterate asset query.
- for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
- {
- // Fetch asset definition.
- AssetDefinition* pAssetDefinition = findAsset( *assetItr );
- // Skip if this is not the asset we want.
- if ( pAssetDefinition == NULL ||
- pAssetDefinition->mAssetAutoUnload != assetAutoUnload )
- continue;
- // Store as result.
- filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
- // Increase result count.
- resultCount++;
- }
- // Set asset query.
- pAssetQuery->set( filteredAssets );
- }
- else
- {
- // No, so iterate declared assets.
- for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
- {
- // Fetch asset definition.
- AssetDefinition* pAssetDefinition = assetItr->value;
- // Skip if this is not the asset we want.
- if ( assetAutoUnload != pAssetDefinition->mAssetAutoUnload )
- continue;
- // Store as result.
- pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
- // Increase result count.
- resultCount++;
- }
- }
- return resultCount;
- }
- //-----------------------------------------------------------------------------
- S32 AssetManager::findAssetInternal( AssetQuery* pAssetQuery, const bool assetInternal, const bool assetQueryAsSource )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_FindAssetInternal);
- // Sanity!
- AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
- // Reset result count.
- S32 resultCount = 0;
- // Use asset-query as the source?
- if ( assetQueryAsSource )
- {
- AssetQuery filteredAssets;
- // Yes, so iterate asset query.
- for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
- {
- // Fetch asset definition.
- AssetDefinition* pAssetDefinition = findAsset( *assetItr );
- // Skip if this is not the asset we want.
- if ( pAssetDefinition == NULL ||
- pAssetDefinition->mAssetInternal != assetInternal )
- continue;
- // Store as result.
- filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
- // Increase result count.
- resultCount++;
- }
- // Set asset query.
- pAssetQuery->set( filteredAssets );
- }
- else
- {
- // No, so iterate declared assets.
- for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
- {
- // Fetch asset definition.
- AssetDefinition* pAssetDefinition = assetItr->value;
- // Skip if this is not the asset we want.
- if ( assetInternal != pAssetDefinition->mAssetInternal )
- continue;
- // Store as result.
- pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
- // Increase result count.
- resultCount++;
- }
- }
- return resultCount;
- }
- //-----------------------------------------------------------------------------
- S32 AssetManager::findAssetPrivate( AssetQuery* pAssetQuery, const bool assetPrivate, const bool assetQueryAsSource )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_FindAssetPrivate);
- // Sanity!
- AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
- // Reset result count.
- S32 resultCount = 0;
- // Use asset-query as the source?
- if ( assetQueryAsSource )
- {
- AssetQuery filteredAssets;
- // Yes, so iterate asset query.
- for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
- {
- // Fetch asset definition.
- AssetDefinition* pAssetDefinition = findAsset( *assetItr );
- // Skip if this is not the asset we want.
- if ( pAssetDefinition == NULL ||
- pAssetDefinition->mAssetPrivate != assetPrivate )
- continue;
- // Store as result.
- filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
- // Increase result count.
- resultCount++;
- }
- // Set asset query.
- pAssetQuery->set( filteredAssets );
- }
- else
- {
- // No, so iterate declared assets.
- for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
- {
- // Fetch asset definition.
- AssetDefinition* pAssetDefinition = assetItr->value;
- // Skip if this is not the asset we want.
- if ( assetPrivate != pAssetDefinition->mAssetPrivate )
- continue;
- // Store as result.
- pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
- // Increase result count.
- resultCount++;
- }
- }
- return resultCount;
- }
- //-----------------------------------------------------------------------------
- S32 AssetManager::findAssetType( AssetQuery* pAssetQuery, const char* pAssetType, const bool assetQueryAsSource )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_FindAssetType);
- // Sanity!
- AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
- AssertFatal( pAssetType != NULL, "Cannot use NULL asset type." );
- // Fetch asset type.
- StringTableEntry assetType = StringTable->insert( pAssetType );
- // Reset result count.
- S32 resultCount = 0;
- // Use asset-query as the source?
- if ( assetQueryAsSource )
- {
- AssetQuery filteredAssets;
- // Yes, so iterate asset query.
- for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
- {
- // Fetch asset definition.
- AssetDefinition* pAssetDefinition = findAsset( *assetItr );
- // Skip if this is not the asset we want.
- if ( pAssetDefinition == NULL ||
- pAssetDefinition->mAssetType != assetType )
- continue;
- // Store as result.
- filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
- // Increase result count.
- resultCount++;
- }
- // Set asset query.
- pAssetQuery->set( filteredAssets );
- }
- else
- {
- // No, so iterate declared assets.
- for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
- {
- // Fetch asset definition.
- AssetDefinition* pAssetDefinition = assetItr->value;
- // Skip if this is not the asset we want.
- if ( assetType != pAssetDefinition->mAssetType )
- continue;
- // Store as result.
- pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
- // Increase result count.
- resultCount++;
- }
- }
- return resultCount;
- }
- //-----------------------------------------------------------------------------
- S32 AssetManager::findAssetDependsOn( AssetQuery* pAssetQuery, const char* pAssetId )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_FindAssetDependsOn);
- // Sanity!
- AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
- AssertFatal( pAssetId != NULL, "Cannot use NULL asset Id." );
- // Fetch asset Id.
- StringTableEntry assetId = StringTable->insert( pAssetId );
- // Reset result count.
- S32 resultCount = 0;
- // Find depends-on entry.
- typeAssetDependsOnHash::Iterator dependsOnItr = mAssetDependsOn.find( assetId );
- // Iterate all dependencies.
- while( dependsOnItr != mAssetDependsOn.end() && dependsOnItr->key == assetId )
- {
- // Store as result.
- pAssetQuery->mAssetList.push_back(dependsOnItr->value);
- // Next dependency.
- dependsOnItr++;
- // Increase result count.
- resultCount++;
- }
- return resultCount;
- }
- //-----------------------------------------------------------------------------
- S32 AssetManager::findAssetIsDependedOn( AssetQuery* pAssetQuery, const char* pAssetId )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_FindAssetIsDependedOn);
- // Sanity!
- AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
- AssertFatal( pAssetId != NULL, "Cannot use NULL asset Id." );
- // Fetch asset Id.
- StringTableEntry assetId = StringTable->insert( pAssetId );
- // Reset result count.
- S32 resultCount = 0;
- // Find depended-on entry.
- typeAssetIsDependedOnHash::Iterator dependedOnItr = mAssetIsDependedOn.find( assetId );
- // Iterate all dependencies.
- while( dependedOnItr != mAssetIsDependedOn.end() && dependedOnItr->key == assetId )
- {
- // Store as result.
- pAssetQuery->mAssetList.push_back(dependedOnItr->value);
- // Next dependency.
- dependedOnItr++;
- // Increase result count.
- resultCount++;
- }
- return resultCount;
- }
- //-----------------------------------------------------------------------------
- S32 AssetManager::findInvalidAssetReferences( AssetQuery* pAssetQuery )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_FindInvalidAssetReferences);
- // Sanity!
- AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
- // Reset result count.
- S32 resultCount = 0;
- // Iterate referenced assets.
- for( typeReferencedAssetsHash::Iterator assetItr = mReferencedAssets.begin(); assetItr != mReferencedAssets.end(); ++assetItr )
- {
- // Find asset definition.
- AssetDefinition* pAssetDefinition = findAsset( assetItr->key );
- // Skip if the asset definition was found.
- if ( pAssetDefinition != NULL )
- continue;
- // Store as result.
- pAssetQuery->mAssetList.push_back(assetItr->key);
- // Increase result count.
- resultCount++;
- }
- return resultCount;
- }
- //-----------------------------------------------------------------------------
- S32 AssetManager::findTaggedAssets( AssetQuery* pAssetQuery, const char* pAssetTagNames, const bool assetQueryAsSource )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_FindTaggedAssets);
- // Sanity!
- AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
- AssertFatal( pAssetTagNames != NULL, "Cannot use NULL asset tag name(s)." );
- // Do we have an asset tag manifest?
- if ( mAssetTagsManifest.isNull() )
- {
- // No, so warn.
- Con::warnf( "Asset Manager: Cannot find tagged assets as no asset tag manifest is present." );
- return 0;
- }
- // Reset result count.
- S32 resultCount = 0;
- const char* pTagSeparators = " ,\t\n";
- // Fetch tag count.
- U32 assetTagCount = StringUnit::getUnitCount( pAssetTagNames, pTagSeparators );
- // Fetch asset tags.
- Vector<AssetTagsManifest::AssetTag*> assetTags;
- for( U32 tagIndex = 0; tagIndex < assetTagCount; ++tagIndex )
- {
- // Fetch asset tag name.
- const char* pTagName = StringUnit::getUnit( pAssetTagNames, tagIndex, pTagSeparators );
- // Fetch asset tag.
- AssetTagsManifest::AssetTag* pAssetTag = mAssetTagsManifest->findAssetTag( pTagName );
- // Did we find the asset tag?
- if ( pAssetTag == NULL )
- {
- // No, so warn.
- Con::warnf( "AssetTagsManifest: Asset Manager: Cannot find tagged assets of '%s' as it does not exist. Ignoring tag.", pTagName );
- continue;
- }
- assetTags.push_back( pAssetTag );
- }
- // Fetch found asset tag count.
- assetTagCount = assetTags.size();
- // Did we find any tags?
- if ( assetTagCount == 0 )
- {
- // No, so warn.
- Con::warnf( "AssetTagsManifest: Asset Manager: No specified tagged assets found in '%s'.", pAssetTagNames );
- return 0;
- }
- // Use asset-query as the source?
- if ( assetQueryAsSource )
- {
- AssetQuery filteredAssets;
- // Yes, so iterate asset query.
- for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
- {
- // Fetch asset Id.
- StringTableEntry assetId = *assetItr;
- // Skip if asset is not valid.
- if ( !isDeclaredAsset( assetId ) )
- continue;
- // Reset matched flag.
- bool assetTagMatched = false;
- // Iterate asset tags.
- for ( Vector<AssetTagsManifest::AssetTag*>::iterator assetTagItr = assetTags.begin(); assetTagItr != assetTags.end(); ++assetTagItr )
- {
- // Fetch asset tag.
- AssetTagsManifest::AssetTag* pAssetTag = *assetTagItr;
- // Skip if asset is not tagged.
- if ( !pAssetTag->containsAsset( assetId ) )
- continue;
-
- // Flag as matched.
- assetTagMatched = true;
- break;
- }
- // Did we find a match?
- if ( assetTagMatched )
- {
- // Yes, so is asset already present?
- if ( !filteredAssets.containsAsset( assetId ) )
- {
- // No, so store as result.
- filteredAssets.mAssetList.push_back(assetId);
- // Increase result count.
- resultCount++;
- }
- }
- }
- // Set asset query.
- pAssetQuery->set( filteredAssets );
- }
- else
- {
- // Iterate asset tags.
- for ( Vector<AssetTagsManifest::AssetTag*>::iterator assetTagItr = assetTags.begin(); assetTagItr != assetTags.end(); ++assetTagItr )
- {
- // Fetch asset tag.
- AssetTagsManifest::AssetTag* pAssetTag = *assetTagItr;
- // Iterate tagged assets.
- for ( Vector<typeAssetId>::iterator assetItr = pAssetTag->mAssets.begin(); assetItr != pAssetTag->mAssets.end(); ++assetItr )
- {
- // Fetch asset Id.
- StringTableEntry assetId = *assetItr;
- // Skip if asset Id is already present.
- if ( pAssetQuery->containsAsset( assetId ) )
- continue;
- // Store as result.
- pAssetQuery->mAssetList.push_back(assetId);
- // Increase result count.
- resultCount++;
- }
- }
- }
- return resultCount;
- }
- //-----------------------------------------------------------------------------
- S32 AssetManager::findAssetLooseFile( AssetQuery* pAssetQuery, const char* pLooseFile, const bool assetQueryAsSource )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_FindAssetLooseFile);
- // Sanity!
- AssertFatal( pAssetQuery != NULL, "Cannot use NULL asset query." );
- AssertFatal( pLooseFile != NULL, "Cannot use NULL loose file." );
- // Make game relative path for loose file.
- StringTableEntry looseFile = Platform::makeRelativePathName(pLooseFile, NULL);;
- // Reset result count.
- S32 resultCount = 0;
- // Use asset-query as the source?
- if ( assetQueryAsSource )
- {
- AssetQuery filteredAssets;
- // Yes, so iterate asset query.
- for (Vector<StringTableEntry>::iterator assetItr = pAssetQuery->mAssetList.begin(); assetItr != pAssetQuery->mAssetList.end(); ++assetItr)
- {
- // Fetch asset definition.
- AssetDefinition* pAssetDefinition = findAsset( *assetItr );
- // Fetch loose files.
- Vector<StringTableEntry>& assetLooseFiles = pAssetDefinition->mAssetLooseFiles;
- // Skip if this asset has no loose files.
- if ( assetLooseFiles.size() == 0 )
- continue;
- // Search the assets loose files.
- for( Vector<StringTableEntry>::iterator looseFileItr = assetLooseFiles.begin(); looseFileItr != assetLooseFiles.end(); ++looseFileItr )
- {
- // Is this the loose file we are searching for?
- if ( *looseFileItr != looseFile )
- continue;
- // Store as result.
- filteredAssets.mAssetList.push_back(pAssetDefinition->mAssetId);
- // Increase result count.
- resultCount++;
- break;
- }
- }
- // Set asset query.
- pAssetQuery->set( filteredAssets );
- }
- else
- {
- // No, so iterate declared assets.
- for( typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr )
- {
- // Fetch asset definition.
- AssetDefinition* pAssetDefinition = assetItr->value;
- // Fetch loose files.
- Vector<StringTableEntry>& assetLooseFiles = pAssetDefinition->mAssetLooseFiles;
- // Skip if this asset has no loose files.
- if ( assetLooseFiles.size() == 0 )
- continue;
- // Search the assets loose files.
- for( Vector<StringTableEntry>::iterator looseFileItr = assetLooseFiles.begin(); looseFileItr != assetLooseFiles.end(); ++looseFileItr )
- {
- // Is this the loose file we are searching for?
- if ( *looseFileItr != looseFile )
- continue;
- // Store as result.
- pAssetQuery->mAssetList.push_back(pAssetDefinition->mAssetId);
- // Increase result count.
- resultCount++;
- break;
- }
- }
- }
- return resultCount;
- }
- //-----------------------------------------------------------------------------
- AssetManager::typeAssetDependsOnHash* AssetManager::getDependedOnAssets()
- {
- // Find any asset dependencies.
- return &mAssetDependsOn;
- }
- //-----------------------------------------------------------------------------
- S32 AssetManager::getAssetLooseFileCount(const char* pAssetId)
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_getAssetLooseFileCount);
- // Sanity!
- AssertFatal(pAssetId != NULL, "Cannot get loose files for NULL asset Id.");
- // Find asset.
- AssetDefinition* pAssetDefinition = findAsset(pAssetId);
- // Did we find the asset?
- if (pAssetDefinition == NULL)
- {
- // No, so warn.
- Con::warnf("Asset Manager: Failed to get loose files for asset Id '%s' as it does not exist.", pAssetId);
- return false;
- }
- S32 looseFileCount = pAssetDefinition->mAssetLooseFiles.size();
- // Cleanup our reference
- pAssetDefinition = NULL;
- return looseFileCount;
- }
- //-----------------------------------------------------------------------------
- const char* AssetManager::getAssetLooseFile(const char* pAssetId, const S32& index)
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_getAssetLooseFile);
- // Sanity!
- AssertFatal(pAssetId != NULL, "Cannot get loose file for NULL asset Id.");
- // Find asset.
- AssetDefinition* pAssetDefinition = findAsset(pAssetId);
- // Did we find the asset?
- if (pAssetDefinition == NULL)
- {
- // No, so warn.
- Con::warnf("Asset Manager: Failed to get loose file for asset Id '%s' as it does not exist.", pAssetId);
- return StringTable->EmptyString();
- }
- if(index < 0 || index >= pAssetDefinition->mAssetLooseFiles.size())
- {
- Con::warnf("Asset Manager : Failed to get loose file for asset Id '%s' as the index was out of range.", pAssetId);
- }
- StringTableEntry looseFile = pAssetDefinition->mAssetLooseFiles[index];
- // Cleanup our reference
- pAssetDefinition = NULL;
- return looseFile;
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::scanDeclaredAssets( const char* pPath, const char* pExtension, const bool recurse, ModuleDefinition* pModuleDefinition )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_ScanDeclaredAssets);
- // Sanity!
- AssertFatal( pPath != NULL, "Cannot scan declared assets with NULL path." );
- AssertFatal( pExtension != NULL, "Cannot scan declared assets with NULL extension." );
- // Expand path location.
- String relativePath = Platform::makeRelativePathName(pPath, NULL);
- // Strip any trailing slash off the path.
- if (relativePath.endsWith("/"))
- relativePath = relativePath.substr(0, relativePath.length() - 1);
- Torque::Path scanPath = Torque::FS::GetCwd();
- scanPath.setPath(relativePath);
- // Find files.
- Vector<String> files;
- S32 numAssets = Torque::FS::FindByPattern(scanPath, pExtension, recurse, files, true);
- if (numAssets <= 0)
- {
- // Failed so warn. or don't... Common error when scanning modules with no assets
- //Con::warnf( "Asset Manager: No declared assets found in directory '%s'.", relativePath.c_str());
- return false;
- }
- // Is the asset file-path located within the specified module?
- if ( !Con::isBasePath(relativePath.c_str(), pModuleDefinition->getModulePath()) )
- {
- // No, so warn.
- Con::warnf( "Asset Manager: Could not add declared asset file '%s' as file does not exist with module path '%s'",
- pPath,
- pModuleDefinition->getModulePath() );
- return false;
- }
- // Info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Asset Manager: Scanning for declared assets in path '%s' for files with extension '%s'...", relativePath.c_str(), pExtension );
- }
- // Fetch module assets.
- ModuleDefinition::typeModuleAssetsVector& moduleAssets = pModuleDefinition->getModuleAssets();
- TamlAssetDeclaredVisitor assetDeclaredVisitor;
- // Iterate files.
- for (S32 i = 0; i < numAssets; ++i)
- {
- Torque::Path assetPath = files[i];
-
- Torque::Path compiledPath = assetPath;
- compiledPath.setExtension(mTaml.getAutoFormatBinaryExtension());
- if (Torque::FS::IsFile(compiledPath))
- {
- Torque::FS::FileNodeRef assetFile = Torque::FS::GetFileNode(assetPath);
- Torque::FS::FileNodeRef compiledFile = Torque::FS::GetFileNode(compiledPath);
- if (assetFile != NULL && compiledFile != NULL)
- {
- if (compiledFile->getModifiedTime() >= assetFile->getModifiedTime())
- assetPath = compiledPath;
- }
- }
- // Clear declared assets.
- assetDeclaredVisitor.clear();
- // Format full file-path.
- char assetFileBuffer[1024];
- dSprintf( assetFileBuffer, sizeof(assetFileBuffer), "%s/%s", assetPath.getPath().c_str(), assetPath.getFullFileName().c_str());
- // Parse the filename.
- if ( !mTaml.parse( assetFileBuffer, assetDeclaredVisitor ) )
- {
- // Warn.
- Con::warnf( "Asset Manager: Failed to parse file containing asset declaration: '%s'.", assetFileBuffer );
- continue;
- }
- // Fetch asset definition.
- AssetDefinition& foundAssetDefinition = assetDeclaredVisitor.getAssetDefinition();
- // Did we get an asset name?
- if ( foundAssetDefinition.mAssetName == StringTable->EmptyString() )
- {
- // No, so warn.
- Con::warnf( "Asset Manager: Parsed file '%s' but did not encounter an asset.", assetFileBuffer );
- continue;
- }
- // Set module definition.
- foundAssetDefinition.mpModuleDefinition = pModuleDefinition;
- // Format asset Id.
- char assetIdBuffer[1024];
- dSprintf(assetIdBuffer, sizeof(assetIdBuffer), "%s%s%s",
- pModuleDefinition->getModuleId(),
- ASSET_SCOPE_TOKEN,
- foundAssetDefinition.mAssetName );
- // Set asset Id.
- foundAssetDefinition.mAssetId = StringTable->insert( assetIdBuffer );
- // Does this asset already exist?
- if ( mDeclaredAssets.contains( foundAssetDefinition.mAssetId ) )
- {
- // Yes, so warn.
- Con::warnf( "Asset Manager: Encountered asset Id '%s' in asset file '%s' but it conflicts with existing asset Id in asset file '%s'.",
- foundAssetDefinition.mAssetId,
- foundAssetDefinition.mAssetBaseFilePath,
- mDeclaredAssets.find( foundAssetDefinition.mAssetId )->value->mAssetBaseFilePath );
- continue;
- }
- // Create new asset definition.
- AssetDefinition* pAssetDefinition = new AssetDefinition( foundAssetDefinition );
- // Store in declared assets.
- mDeclaredAssets.insert( pAssetDefinition->mAssetId, pAssetDefinition );
- // Store in module assets.
- moduleAssets.push_back( pAssetDefinition );
-
- // Info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Asset Manager: Adding Asset Id '%s' of type '%s' in asset file '%s'.",
- pAssetDefinition->mAssetId,
- pAssetDefinition->mAssetType,
- pAssetDefinition->mAssetBaseFilePath );
- }
- // Fetch asset Id.
- StringTableEntry assetId = pAssetDefinition->mAssetId;
- // Fetch asset dependencies.
- TamlAssetDeclaredVisitor::typeAssetIdVector& assetDependencies = assetDeclaredVisitor.getAssetDependencies();
- // Are there any asset dependencies?
- if ( assetDependencies.size() > 0 )
- {
- // Yes, so iterate dependencies.
- for( TamlAssetDeclaredVisitor::typeAssetIdVector::iterator assetDependencyItr = assetDependencies.begin(); assetDependencyItr != assetDependencies.end(); ++assetDependencyItr )
- {
- // Fetch asset Ids.
- StringTableEntry dependencyAssetId = *assetDependencyItr;
- // Insert depends-on.
- mAssetDependsOn.insertEqual( assetId, dependencyAssetId );
- // Insert is-depended-on.
- mAssetIsDependedOn.insertEqual( dependencyAssetId, assetId );
- // Info.
- if ( mEchoInfo )
- {
- Con::printf( "Asset Manager: Asset Id '%s' has dependency of Asset Id '%s'", assetId, dependencyAssetId );
- }
- }
- }
- // Fetch asset loose files.
- TamlAssetDeclaredVisitor::typeLooseFileVector& assetLooseFiles = assetDeclaredVisitor.getAssetLooseFiles();
- // Are there any loose files?
- if ( assetLooseFiles.size() > 0 )
- {
- // Yes, so iterate loose files.
- for( TamlAssetDeclaredVisitor::typeLooseFileVector::iterator assetLooseFileItr = assetLooseFiles.begin(); assetLooseFileItr != assetLooseFiles.end(); ++assetLooseFileItr )
- {
- // Fetch loose file.
- StringTableEntry looseFile = *assetLooseFileItr;
- // Info.
- if ( mEchoInfo )
- {
- Con::printf( "Asset Manager: Asset Id '%s' has loose file '%s'.", assetId, looseFile );
- }
- // Store loose file.
- pAssetDefinition->mAssetLooseFiles.push_back( looseFile );
- }
- }
- }
- // Info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Asset Manager: ... Finished scanning for declared assets in path '%s' for files with extension '%s'.", relativePath.c_str(), pExtension );
- Con::printSeparator();
- Con::printBlankLine();
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- bool AssetManager::scanReferencedAssets( const char* pPath, const char* pExtension, const bool recurse )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_ScanReferencedAssets);
- // Sanity!
- AssertFatal( pPath != NULL, "Cannot scan referenced assets with NULL path." );
- AssertFatal( pExtension != NULL, "Cannot scan referenced assets with NULL extension." );
- // Expand path location.
- String relativePath = Platform::makeRelativePathName(pPath, NULL);
- String pattern = "*.";
- pattern += pExtension;
- Torque::Path scanPath = Torque::FS::GetCwd();
- scanPath.setPath(relativePath);
- // Find files.
- Vector<String> files;
- S32 numAssets = Torque::FS::FindByPattern(scanPath, pattern, recurse, files, true);
- if (numAssets <= 0)
- {
- // Failed so warn.
- Con::warnf( "Asset Manager: Failed to scan referenced assets in directory '%s'.", pPath );
- return false;
- }
- // Info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Asset Manager: Scanning for referenced assets in path '%s' for files with extension '%s'...", pPath, pExtension );
- }
- TamlAssetReferencedVisitor assetReferencedVisitor;
- // Iterate files.
- for (S32 i = 0; i < numAssets; ++i)
- {
- Torque::Path assetPath = files[i];
- // Clear referenced assets.
- assetReferencedVisitor.clear();
- // Format full file-path.
- char assetFileBuffer[1024];
- dSprintf( assetFileBuffer, sizeof(assetFileBuffer), "%s/%s", assetPath.getPath().c_str(), assetPath.getFullFileName().c_str());
- // Format reference file-path.
- typeReferenceFilePath referenceFilePath = StringTable->insert( assetFileBuffer );
- // Parse the filename.
- if ( !mTaml.parse( referenceFilePath, assetReferencedVisitor ) )
- {
- // Warn.
- Con::warnf( "Asset Manager: Failed to parse file containing asset references: '%s'.", referenceFilePath );
- continue;
- }
- // Fetch usage map.
- const TamlAssetReferencedVisitor::typeAssetReferencedHash& assetReferencedMap = assetReferencedVisitor.getAssetReferencedMap();
- // Do we have any asset references?
- if ( assetReferencedMap.size() > 0 )
- {
- // Info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- }
- // Iterate usage.
- for( TamlAssetReferencedVisitor::typeAssetReferencedHash::const_iterator usageItr = assetReferencedMap.begin(); usageItr != assetReferencedMap.end(); ++usageItr )
- {
- // Fetch asset name.
- typeAssetId assetId = usageItr->key;
- // Info.
- if ( mEchoInfo )
- {
- Con::printf( "Asset Manager: Found referenced Asset Id '%s' in file '%s'.", assetId, referenceFilePath );
- }
- // Add referenced asset.
- addReferencedAsset( assetId, referenceFilePath );
- }
- }
- }
- // Info.
- if ( mEchoInfo )
- {
- Con::printf( "Asset Manager: ... Finished scanning for referenced assets in path '%s' for files with extension '%s'.", relativePath.c_str(), pExtension );
- Con::printSeparator();
- Con::printBlankLine();
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- AssetDefinition* AssetManager::findAsset( const char* pAssetId )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_FindAsset);
- // Sanity!
- AssertFatal( pAssetId != NULL, "Cannot find NULL asset Id." );
- // Fetch asset Id.
- StringTableEntry assetId = StringTable->insert( pAssetId );
- // Find declared asset.
- typeDeclaredAssetsHash::iterator declaredAssetItr = mDeclaredAssets.find( assetId );
- // Find if we didn't find a declared asset Id.
- if ( declaredAssetItr == mDeclaredAssets.end() )
- return NULL;
- return declaredAssetItr->value;
- }
- //-----------------------------------------------------------------------------
- void AssetManager::addReferencedAsset( StringTableEntry assetId, StringTableEntry referenceFilePath )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_AddReferencedAsset);
- // Sanity!
- AssertFatal( assetId != NULL, "Cannot add referenced asset with NULL asset Id." );
- AssertFatal( referenceFilePath != NULL, "Cannot add referenced asset with NULL reference file-path." );
- // Find referenced asset.
- typeReferencedAssetsHash::Iterator referencedAssetItr = mReferencedAssets.find( assetId );
- // Did we find the asset?
- if ( referencedAssetItr == mReferencedAssets.end() )
- {
- // No, so add asset Id.
- mReferencedAssets.insertEqual( assetId, referenceFilePath );
- }
- else
- {
- // Yes, so add asset Id with a unique file.
- while( true )
- {
- // Finish if this file is already present.
- if ( referencedAssetItr->value == referenceFilePath )
- return;
- // Move to next asset Id.
- referencedAssetItr++;
- // Is this the end of referenced assets or a different asset Id?
- if ( referencedAssetItr == mReferencedAssets.end() ||
- referencedAssetItr->key != assetId )
- {
- // Yes, so add asset reference.
- mReferencedAssets.insertEqual( assetId, referenceFilePath );
- return;
- }
- };
- }
- }
- //-----------------------------------------------------------------------------
- void AssetManager::renameAssetReferences( StringTableEntry assetIdFrom, StringTableEntry assetIdTo )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_RenameAssetReferences);
- // Sanity!
- AssertFatal( assetIdFrom != NULL, "Cannot rename asset references using NULL asset Id from." );
- AssertFatal( assetIdTo != NULL, "Cannot rename asset references using NULL asset Id to." );
- // Finish if the asset is not referenced.
- if ( !mReferencedAssets.count( assetIdFrom ) )
- return;
- // Setup referenced update visitor.
- TamlAssetReferencedUpdateVisitor assetReferencedUpdateVisitor;
- assetReferencedUpdateVisitor.setAssetIdFrom( assetIdFrom );
- assetReferencedUpdateVisitor.setAssetIdTo( assetIdTo );
- // Find first referenced asset Id.
- typeReferencedAssetsHash::Iterator referencedAssetItr = mReferencedAssets.find( assetIdFrom );
- // Iterate references.
- while( true )
- {
- // Finish if end of references.
- if ( referencedAssetItr == mReferencedAssets.end() || referencedAssetItr->key != assetIdFrom )
- return;
- // Info.
- if ( mEchoInfo )
- {
- Con::printf( "Asset Manager: Renaming declared Asset Id '%s' to Asset Id '%s'. Updating referenced file '%s'",
- assetIdFrom,
- assetIdTo,
- referencedAssetItr->value );
- }
- // Update asset file declaration.
- if ( !mTaml.parse( referencedAssetItr->value, assetReferencedUpdateVisitor ) )
- {
- // No, so warn.
- Con::warnf("Asset Manager: Cannot rename referenced asset Id '%s' to asset Id '%s' as the referenced asset file could not be parsed: %s",
- assetIdFrom, assetIdTo, referencedAssetItr->value );
- }
- // Move to next reference.
- referencedAssetItr++;
- }
- }
- //-----------------------------------------------------------------------------
- void AssetManager::removeAssetReferences( StringTableEntry assetId )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_RemoveAssetReferences);
- // Sanity!
- AssertFatal( assetId != NULL, "Cannot rename asset references using NULL asset Id." );
- // Finish if the asset is not referenced.
- if ( !mReferencedAssets.count( assetId ) )
- return;
- // Setup referenced update visitor.
- TamlAssetReferencedUpdateVisitor assetReferencedUpdateVisitor;
- assetReferencedUpdateVisitor.setAssetIdFrom( assetId );
- assetReferencedUpdateVisitor.setAssetIdTo( StringTable->EmptyString() );
- // Find first referenced asset Id.
- typeReferencedAssetsHash::Iterator referencedAssetItr = mReferencedAssets.find(assetId);
- // Iterate references.
- while( true )
- {
- // Finish if end of references.
- if ( referencedAssetItr == mReferencedAssets.end() || referencedAssetItr->key != assetId )
- break;
- // Info.
- if ( mEchoInfo )
- {
- Con::printf( "Asset Manager: Removing Asset Id '%s' references from file '%s'",
- assetId,
- referencedAssetItr->value );
- }
- // Update asset file declaration.
- if ( !mTaml.parse( referencedAssetItr->value, assetReferencedUpdateVisitor ) )
- {
- // No, so warn.
- Con::warnf("Asset Manager: Cannot remove referenced asset Id '%s' as the referenced asset file could not be parsed: %s",
- assetId,
- referencedAssetItr->value );
- }
- // Move to next reference.
- referencedAssetItr++;
- }
- // Remove asset references.
- mReferencedAssets.erase( assetId );
- }
- //-----------------------------------------------------------------------------
- void AssetManager::renameAssetDependencies( StringTableEntry assetIdFrom, StringTableEntry assetIdTo )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_RenameAssetDependencies);
- // Sanity!
- AssertFatal( assetIdFrom != NULL, "Cannot rename asset dependencies using NULL asset Id from." );
- AssertFatal( assetIdTo != NULL, "Cannot rename asset dependencies using NULL asset Id to." );
- // Rename via depends-on...
- while( mAssetDependsOn.count( assetIdFrom ) > 0 )
- {
- // Find depends-on.
- typeAssetDependsOnHash::Iterator dependsOnItr = mAssetDependsOn.find(assetIdFrom);
- // Fetch dependency asset Id.
- StringTableEntry dependencyAssetId = dependsOnItr->value;
- // Find is-depends-on entry.
- typeAssetIsDependedOnHash::Iterator isDependedOnItr = mAssetIsDependedOn.find(dependencyAssetId);
- // Sanity!
- AssertFatal( isDependedOnItr != mAssetIsDependedOn.end(), "Asset dependencies are corrupt!" );
- while( isDependedOnItr != mAssetIsDependedOn.end() && isDependedOnItr->key == dependencyAssetId && isDependedOnItr->value != assetIdFrom )
- {
- isDependedOnItr++;
- }
- // Sanity!
- AssertFatal( isDependedOnItr->key == dependencyAssetId && isDependedOnItr->value == assetIdFrom, "Asset dependencies are corrupt!" );
-
- // Remove is-depended-on.
- mAssetIsDependedOn.erase( isDependedOnItr );
- // Remove depends-on.
- mAssetDependsOn.erase( dependsOnItr );
- // Insert depends-on.
- mAssetDependsOn.insertEqual( assetIdTo, dependencyAssetId );
- // Insert is-depended-on.
- mAssetIsDependedOn.insertEqual( dependencyAssetId, assetIdTo );
- }
- // Rename via is-depended-on...
- while( mAssetIsDependedOn.count( assetIdFrom ) > 0 )
- {
- // Find is-depended-on.
- typeAssetIsDependedOnHash::Iterator isdependedOnItr = mAssetIsDependedOn.find(assetIdFrom);
- // Fetch dependency asset Id.
- StringTableEntry dependencyAssetId = isdependedOnItr->value;
- // Find depends-on entry.
- typeAssetDependsOnHash::Iterator dependsOnItr = mAssetDependsOn.find(dependencyAssetId);
- // Sanity!
- AssertFatal( dependsOnItr != mAssetDependsOn.end(), "Asset dependencies are corrupt!" );
- while( dependsOnItr != mAssetDependsOn.end() && dependsOnItr->key == dependencyAssetId && dependsOnItr->value != assetIdFrom )
- {
- dependsOnItr++;
- }
- // Sanity!
- AssertFatal( dependsOnItr->key == dependencyAssetId && dependsOnItr->value == assetIdFrom, "Asset dependencies are corrupt!" );
-
- // Remove is-depended-on.
- mAssetIsDependedOn.erase( isdependedOnItr );
- // Remove depends-on.
- mAssetDependsOn.erase( dependsOnItr );
- // Insert depends-on.
- mAssetDependsOn.insertEqual( dependencyAssetId, assetIdTo );
- // Insert is-depended-on.
- mAssetIsDependedOn.insertEqual( assetIdTo, dependencyAssetId );
- }
- }
- //-----------------------------------------------------------------------------
- void AssetManager::removeAssetDependencies( const char* pAssetId )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_RemvoeAsetDependencies);
- // Sanity!
- AssertFatal( pAssetId != NULL, "Cannot remove asset dependencies using NULL asset Id." );
- // Fetch asset Id.
- StringTableEntry assetId = StringTable->insert( pAssetId );
- // Remove from depends-on assets.
- while( mAssetDependsOn.count( assetId ) > 0 )
- {
- // Find depends-on.
- typeAssetDependsOnHash::Iterator dependsOnItr = mAssetDependsOn.find(assetId);
- // Fetch dependency asset Id.
- StringTableEntry dependencyAssetId = dependsOnItr->value;
- // Find is-depends-on entry.
- typeAssetIsDependedOnHash::Iterator isDependedOnItr = mAssetIsDependedOn.find(dependencyAssetId);
- // Sanity!
- AssertFatal( isDependedOnItr != mAssetIsDependedOn.end(), "Asset dependencies are corrupt!" );
- while( isDependedOnItr != mAssetIsDependedOn.end() && isDependedOnItr->key == dependencyAssetId && isDependedOnItr->value != assetId )
- {
- isDependedOnItr++;
- }
- // Sanity!
- AssertFatal( isDependedOnItr->key == dependencyAssetId && isDependedOnItr->value == assetId, "Asset dependencies are corrupt!" );
- // Remove is-depended-on.
- mAssetIsDependedOn.erase( isDependedOnItr );
- // Remove depends-on.
- mAssetDependsOn.erase( dependsOnItr );
- }
- }
- //-----------------------------------------------------------------------------
- void AssetManager::unloadAsset( AssetDefinition* pAssetDefinition )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_UnloadAsset);
- pAssetDefinition->mpAssetBase->unloadAsset();
- // Destroy the asset.
- if(pAssetDefinition->mpAssetBase->isProperlyAdded())
- pAssetDefinition->mpAssetBase->deleteObject();
- // Increase unloaded count.
- pAssetDefinition->mAssetUnloadedCount++;
- // Is the asset internal?
- if ( pAssetDefinition->mAssetInternal )
- {
- // Yes, so decrease internal loaded asset count.
- mLoadedInternalAssetsCount--;
- }
- else
- {
- // No, so decrease external loaded assets count.
- mLoadedExternalAssetsCount--;
- }
- // Is the asset private?
- if ( pAssetDefinition->mAssetPrivate )
- {
- // Yes, so decrease private loaded asset count.
- mLoadedPrivateAssetsCount--;
- // Remove it completely.
- removeDeclaredAsset( pAssetDefinition->mAssetId );
- }
- }
- //-----------------------------------------------------------------------------
- void AssetManager::onModulePreLoad( ModuleDefinition* pModuleDefinition )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_OnModulePreLoad);
- // Add module declared assets.
- addModuleDeclaredAssets( pModuleDefinition );
- // Load any auto-loaded asset types
- loadModuleAutoLoadAssets(pModuleDefinition);
- // Is an asset tags manifest specified?
- if ( pModuleDefinition->getAssetTagsManifest() != StringTable->EmptyString() )
- {
- // Yes, so load the asset tags manifest.
- loadAssetTags( pModuleDefinition );
- }
- }
- //-----------------------------------------------------------------------------
- void AssetManager::onModulePreUnload( ModuleDefinition* pModuleDefinition )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_OnModulePreUnload);
- // Is an asset tags manifest specified?
- if ( pModuleDefinition->getAssetTagsManifest() != StringTable->EmptyString() )
- {
- // Yes, so save the asset tags manifest.
- saveAssetTags();
- // Do we have an asset tags manifest?
- if ( !mAssetTagsManifest.isNull() )
- {
- // Yes, so remove it.
- mAssetTagsManifest->deleteObject();
- mAssetTagsModuleDefinition = NULL;
- }
- }
- }
- //-----------------------------------------------------------------------------
- void AssetManager::onModulePostUnload( ModuleDefinition* pModuleDefinition )
- {
- // Debug Profiling.
- PROFILE_SCOPE(AssetManager_OnModulePostUnload);
- // Remove declared assets.
- removeDeclaredAssets( pModuleDefinition );
- }
|