1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644 |
- //-----------------------------------------------------------------------------
- // 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 "moduleManager.h"
- #ifndef _MODULE_MERGE_DEFINITION_H
- #include "moduleMergeDefinition.h"
- #endif
- #ifndef _TAML_MODULE_ID_UPDATE_VISITOR_H_
- #include "tamlModuleIdUpdateVisitor.h"
- #endif
- #ifndef _MODULE_CALLBACKS_H_
- #include "moduleCallbacks.h"
- #endif
- #ifndef _CONSOLETYPES_H_
- #include "console/consoleTypes.h"
- #endif
- #include "cinterface/cinterface.h"
- #ifndef _MODULE_DEFINITION_H
- #include "module/moduleDefinition.h"
- #endif
- #ifndef _STRINGFUNCTIONS_H_
- #include "core/strings/stringFunctions.h"
- #endif
- // Script bindings.
- #include "moduleManager_ScriptBinding.h"
- //-----------------------------------------------------------------------------
- IMPLEMENT_CONOBJECT( ModuleManager );
- //-----------------------------------------------------------------------------
- ModuleManager ModuleDatabase;
- //-----------------------------------------------------------------------------
- S32 QSORT_CALLBACK moduleDefinitionVersionIdSort( const void* a, const void* b )
- {
- // Fetch module definitions.
- ModuleDefinition* pDefinition1 = *(ModuleDefinition**)a;
- ModuleDefinition* pDefinition2 = *(ModuleDefinition**)b;
- // Fetch version Ids.
- const U32 versionId1 = pDefinition1->getVersionId();
- const U32 versionId2 = pDefinition2->getVersionId();
- // We sort higher version Id first.
- return versionId1 > versionId2 ? -1 : versionId1 < versionId2 ? 1 : 0;
- }
- S32 QSORT_CALLBACK moduleDependencySort(const void* a, const void* b)
- {
- // Fetch module definitions.
- ModuleDefinition* pDefinition1 = *(ModuleDefinition * *)a;
- ModuleDefinition* pDefinition2 = *(ModuleDefinition * *)b;
- // Fetch version Ids.
- ModuleDefinition::typeModuleDependencyVector moduleDependencies = pDefinition1->getDependencies();
- bool foundDependant = false;
- for (ModuleDefinition::typeModuleDependencyVector::const_iterator dependencyItr = moduleDependencies.begin(); dependencyItr != moduleDependencies.end(); ++dependencyItr)
- {
- if (String::compare(dependencyItr->mModuleId, pDefinition2->getModuleId())
- && (dependencyItr->mVersionId == pDefinition2->getVersionId()))
- foundDependant = true;
- }
- return foundDependant ? 1 : -1;
- }
- //-----------------------------------------------------------------------------
- ModuleManager::ModuleManager() :
- mEnforceDependencies(true),
- mEchoInfo(false),
- mDatabaseLocks( 0 ),
- mIgnoreLoadedGroups(false)
- {
- // Set module extension.
- dStrcpy( mModuleExtension, MODULE_MANAGER_MODULE_DEFINITION_EXTENSION, 256 );
- }
- //-----------------------------------------------------------------------------
- bool ModuleManager::onAdd()
- {
- if( !Parent::onAdd() )
- return false;
- // Register listeners.
- mNotificationListeners.registerObject();
- return true;
- }
- //-----------------------------------------------------------------------------
- void ModuleManager::onRemove()
- {
- // Clear database.
- clearDatabase();
- // Unregister object.
- mNotificationListeners.unregisterObject();
- // Call parent.
- Parent::onRemove();
- }
- //-----------------------------------------------------------------------------
- void ModuleManager::initPersistFields()
- {
- // Call parent.
- Parent::initPersistFields();
- addField( "EnforceDependencies", TypeBool, Offset(mEnforceDependencies, ModuleManager), "Whether the module manager enforces any dependencies on module definitions it discovers or not." );
- addField( "EchoInfo", TypeBool, Offset(mEchoInfo, ModuleManager), "Whether the module manager echos extra information to the console or not." );
- }
- //-----------------------------------------------------------------------------
- void ModuleManager::onDeleteNotify( SimObject *object )
- {
- // Cast to a module definition.
- ModuleDefinition* pModuleDefinition = dynamic_cast<ModuleDefinition*>( object );
- // Ignore if not appropriate.
- if ( pModuleDefinition == NULL )
- return;
- // Warn.
- Con::warnf( "Module Manager::onDeleteNotify() - Notified of a module definition deletion for module Id '%s' of version Id '%d' however this should not happen and can cause module database corruption.",
- pModuleDefinition->getModuleId(), pModuleDefinition->getVersionId() );
- }
- //-----------------------------------------------------------------------------
- bool ModuleManager::setModuleExtension( const char* pExtension )
- {
- // Sanity!
- AssertFatal( pExtension != NULL, "Cannot set module extension with NULL extension." );
- // Did we find an extension period?
- if ( *pExtension == '.' )
- {
- // Yes, so warn.
- Con::warnf("Module Manager: Failed to set extension as supplied extension contains an initial period: '%s'.", pExtension );
- return false;
- }
- // Is the extension too large?
- if ( dStrlen( pExtension ) > sizeof( mModuleExtension ) )
- {
- // Yes, so warn.
- Con::warnf("Module Manager: Failed to set extension as supplied extension is too large: '%s'.", pExtension );
- return false;
- }
- // Set module extension.
- dStrcpy( mModuleExtension, pExtension, 256 );
- return true;
- }
- //-----------------------------------------------------------------------------
- bool ModuleManager::scanModules( const char* pPath, const bool rootOnly )
- {
- // Lock database.
- LockDatabase( this );
- // Sanity!
- AssertFatal( pPath != NULL, "Cannot scan module with NULL path." );
- String relBasePath = Platform::makeRelativePathName(pPath, NULL);
- // Info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf("Module Manager: Started scanning '%s'...", relBasePath.c_str());
- }
- String pattern = "*.";
- pattern += mModuleExtension;
- Torque::Path scanPath = Torque::FS::GetCwd();
- scanPath.setPath(relBasePath);
- Vector<String> fileList;
- S32 numModules = Torque::FS::FindByPattern(scanPath, pattern, !rootOnly, fileList, true);
- for (S32 i = 0; i < numModules; ++i)
- {
- Torque::Path modulePath = fileList[i];
- registerModule(modulePath.getPath(), modulePath.getFullFileName());
- }
- // Info.
- if ( mEchoInfo )
- {
- Con::printf("Module Manager: Finished scanning '%s'.", relBasePath.c_str());
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- bool ModuleManager::loadModuleGroup( const char* pModuleGroup )
- {
- // Lock database.
- LockDatabase( this );
- // Sanity!
- AssertFatal( pModuleGroup != NULL, "Cannot load module group with NULL group name." );
- typeModuleLoadEntryVector moduleResolvingQueue;
- typeModuleLoadEntryVector moduleReadyQueue;
- // Fetch module group.
- StringTableEntry moduleGroup = StringTable->insert( pModuleGroup );
- // Info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Module Manager: Loading group '%s':" ,moduleGroup );
- }
- // Is the module group already loaded?
- if ( findGroupLoaded( moduleGroup ) != NULL )
- {
- // Yes, so warn.
- Con::warnf( "Module Manager: Cannot load group '%s' as it is already loaded.", moduleGroup );
- return false;
- }
- // Find module group.
- typeGroupModuleHash::iterator moduleGroupItr = mGroupModules.find( moduleGroup );
- // Did we find the module group?
- if ( moduleGroupItr == mGroupModules.end() )
- {
- // No, so info.
- if ( mEchoInfo )
- {
- Con::printf( "Module Manager: No modules found for module group '%s'.", moduleGroup );
- }
-
- return true;
- }
- // Yes, so fetch the module Ids.
- typeModuleIdVector* pModuleIds = moduleGroupItr->value;
- // Iterate module groups.
- for( typeModuleIdVector::iterator moduleIdItr = pModuleIds->begin(); moduleIdItr != pModuleIds->end(); ++moduleIdItr )
- {
- // Fetch module Id.
- StringTableEntry moduleId = *moduleIdItr;
- // Finish if we could not resolve the dependencies for module Id (of any version Id).
- if ( !resolveModuleDependencies( moduleId, 0, moduleGroup, false, moduleResolvingQueue, moduleReadyQueue ) )
- return false;
- }
- // Check the modules we want to load to ensure that we do not have incompatible modules loaded already.
- for ( typeModuleLoadEntryVector::iterator moduleReadyItr = moduleReadyQueue.begin(); moduleReadyItr != moduleReadyQueue.end(); ++moduleReadyItr )
- {
- // Fetch load ready module definition.
- ModuleDefinition* pLoadReadyModuleDefinition = moduleReadyItr->mpModuleDefinition;;
- // Fetch the module Id loaded entry.
- ModuleLoadEntry* pLoadedModuleEntry = findModuleLoaded( pLoadReadyModuleDefinition->getModuleId() );
- // Did we find a loaded entry?
- if ( pLoadedModuleEntry != NULL )
- {
- // Yes, so is it the one we need to load?
- if ( pLoadedModuleEntry->mpModuleDefinition != pLoadReadyModuleDefinition )
- {
- // Yes, so warn.
- Con::warnf( "Module Manager: Cannot load module group '%s' as the module Id '%s' at version Id '%d' is required but the module Id is already loaded but at version Id '%d'.",
- moduleGroup, pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId(), pLoadedModuleEntry->mpModuleDefinition->getVersionId() );
- return false;
- }
- }
- }
- // Info.
- if ( mEchoInfo )
- {
- // Info.
- Con::printf( "Module Manager: Group '%s' and its dependencies is comprised of the following '%d' module(s):", moduleGroup, moduleReadyQueue.size() );
- // Iterate the modules echoing them.
- for ( typeModuleLoadEntryVector::iterator moduleReadyItr = moduleReadyQueue.begin(); moduleReadyItr != moduleReadyQueue.end(); ++moduleReadyItr )
- {
- // Fetch the ready entry.
- ModuleDefinition* pModuleDefinition = moduleReadyItr->mpModuleDefinition;
- // Info.
- Con::printf( "> module Id '%s' at version Id '%d':", pModuleDefinition->getModuleId(), pModuleDefinition->getVersionId() );
- }
- }
- // Add module group.
- mGroupsLoaded.push_back( moduleGroup );
- // Reset modules loaded count.
- U32 modulesLoadedCount = 0;
- // Iterate the modules, executing their script files and call their create function.
- for ( typeModuleLoadEntryVector::iterator moduleReadyItr = moduleReadyQueue.begin(); moduleReadyItr != moduleReadyQueue.end(); ++moduleReadyItr )
- {
- // Fetch the ready entry.
- ModuleLoadEntry* pReadyEntry = moduleReadyItr;
- // Fetch load ready module definition.
- ModuleDefinition* pLoadReadyModuleDefinition = pReadyEntry->mpModuleDefinition;
- // Fetch any loaded entry for the module Id.
- ModuleLoadEntry* pLoadedEntry = findModuleLoaded( pLoadReadyModuleDefinition->getModuleId() );
- // Is the module already loaded.
- if ( pLoadedEntry != NULL )
- {
- // Yes, so increase load count.
- pLoadedEntry->mpModuleDefinition->increaseLoadCount();
- // Skip.
- continue;
- }
- // No, so info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Module Manager: Loading group '%s' : module Id '%s' at version Id '%d' in group '%s' using the script file '%s'.",
- moduleGroup, pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId(), pLoadReadyModuleDefinition->getModuleGroup(), pLoadReadyModuleDefinition->getModuleScriptFilePath() );
- }
- // Is the module deprecated?
- if ( pLoadReadyModuleDefinition->getDeprecated() )
- {
- // Yes, so warn.
- Con::warnf( "Module Manager: Caution: module Id '%s' at version Id '%d' in group '%s' is deprecated. You should use a newer version!",
- pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId(), pLoadReadyModuleDefinition->getModuleGroup() );
- }
- // Add the path expando for module.
- Con::addPathExpando( pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getModulePath() );
- // Create a scope set.
- SimSet* pScopeSet = new SimSet;
- pScopeSet->registerObject( pLoadReadyModuleDefinition->getModuleId() );
- pReadyEntry->mpModuleDefinition->mScopeSet = pScopeSet->getId();
- // Increase load count.
- pReadyEntry->mpModuleDefinition->increaseLoadCount();
- // Queue module loaded.
- mModulesLoaded.push_back( *pReadyEntry );
- // Bump modules loaded count.
- modulesLoadedCount++;
- // Raise notifications.
- raiseModulePreLoadNotifications( pLoadReadyModuleDefinition );
- // Do we have a script file-path specified?
- if ( pLoadReadyModuleDefinition->getModuleScriptFilePath() != StringTable->EmptyString() )
- {
- // Yes, so execute the script file.
- ConsoleValue cValue = Con::executef("exec", pLoadReadyModuleDefinition->getModuleScriptFilePath());
- const bool scriptFileExecuted = cValue.getBool();
- // Did we execute the script file?
- if ( scriptFileExecuted )
- {
- // Yes, so is the create method available?
- if ( pScopeSet->isMethod( pLoadReadyModuleDefinition->getCreateFunction() ) )
- {
- // Yes, so call the create method.
- //But first, check if we're overriding objects, and if so, set our console var to make that happen while we exec our create function
- if (pLoadReadyModuleDefinition->getOverrideExistingObjects())
- {
- String redefineBehaviorPrev = Con::getVariable("$Con::redefineBehavior");
- Con::setVariable("$Con::redefineBehavior", "replaceExisting");
- Con::executef(pScopeSet, pLoadReadyModuleDefinition->getCreateFunction());
-
- //And now that we've executed, switch back to the prior behavior
- Con::setVariable("$Con::redefineBehavior", redefineBehaviorPrev.c_str());
- }
- else
- {
- //Nothing to do, just run the create function
- Con::executef(pScopeSet, pLoadReadyModuleDefinition->getCreateFunction());
- }
- }
- }
- else
- {
- // No, so warn.
- Con::errorf( "Module Manager: Cannot load module group '%s' as the module Id '%s' at version Id '%d' as it failed to have the script file '%s' loaded.",
- moduleGroup, pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId(), pLoadReadyModuleDefinition->getModuleScriptFilePath() );
- }
- }
- else
- {
- // Is the create method available?
- if (pScopeSet->isMethod(pLoadReadyModuleDefinition->getCreateFunction()))
- {
- // Yes, so call the create method.
- Con::executef(pScopeSet, pLoadReadyModuleDefinition->getCreateFunction());
- }
- }
- // Raise notifications.
- raiseModulePostLoadNotifications( pLoadReadyModuleDefinition );
- }
- // Info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Module Manager: Finish loading '%d' module(s) for group '%s'.", modulesLoadedCount, moduleGroup );
- Con::printSeparator();
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- bool ModuleManager::unloadModuleGroup( const char* pModuleGroup )
- {
- // Lock database.
- LockDatabase( this );
- // Sanity!
- AssertFatal( pModuleGroup != NULL, "Cannot unload module group with NULL group name." );
- typeModuleLoadEntryVector moduleResolvingQueue;
- typeModuleLoadEntryVector moduleReadyQueue;
- // Fetch module group.
- StringTableEntry moduleGroup = StringTable->insert( pModuleGroup );
- // Info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Module Manager: Unloading group '%s':" , moduleGroup );
- }
- // Find the group loaded iterator.
- typeGroupVector::iterator groupLoadedItr = findGroupLoaded( moduleGroup );
- // Is the module group already unloaded?
- if ( groupLoadedItr == NULL )
- {
- // No, so warn.
- Con::warnf( "Module Manager: Cannot unload group '%s' as it is not loaded.", moduleGroup );
- return false;
- }
- // Find module group.
- typeGroupModuleHash::iterator moduleGroupItr = mGroupModules.find( moduleGroup );
- // Did we find the module group?
- if ( moduleGroupItr == mGroupModules.end() )
- {
- // No, so info.
- if ( mEchoInfo )
- {
- Con::printf( "Module Manager: No modules found for module group '%s'.", moduleGroup );
- return true;
- }
- }
- // Yes, so fetch the module Ids.
- typeModuleIdVector* pModuleIds = moduleGroupItr->value;
- // Iterate module groups.
- for( typeModuleIdVector::iterator moduleIdItr = pModuleIds->begin(); moduleIdItr != pModuleIds->end(); ++moduleIdItr )
- {
- // Fetch module Id.
- StringTableEntry moduleId = *moduleIdItr;
- // Finish if we could not resolve the dependencies for module Id (of any version Id).
- if ( !resolveModuleDependencies( moduleId, 0, moduleGroup, false, moduleResolvingQueue, moduleReadyQueue ) )
- return false;
- }
- // Check the modules we want to load to ensure that we do not have incompatible modules loaded already.
- for ( typeModuleLoadEntryVector::iterator moduleReadyItr = moduleReadyQueue.begin(); moduleReadyItr != moduleReadyQueue.end(); ++moduleReadyItr )
- {
- // Fetch load ready module definition.
- ModuleDefinition* pLoadReadyModuleDefinition = moduleReadyItr->mpModuleDefinition;;
- // Fetch the module Id loaded entry.
- ModuleLoadEntry* pLoadedModuleEntry = findModuleLoaded( pLoadReadyModuleDefinition->getModuleId() );
- // Did we find a loaded entry?
- if ( pLoadedModuleEntry != NULL )
- {
- // Yes, so is it the one we need to load?
- if ( pLoadedModuleEntry->mpModuleDefinition != pLoadReadyModuleDefinition )
- {
- // Yes, so warn.
- Con::warnf( "Module Manager: Cannot unload module group '%s' as the module Id '%s' at version Id '%d' is required but the module Id is loaded but at version Id '%d'.",
- moduleGroup, pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId(), pLoadedModuleEntry->mpModuleDefinition->getVersionId() );
- return false;
- }
- }
- }
- // Remove module group.
- mGroupsLoaded.erase_fast( groupLoadedItr );
- // Reset modules unloaded count.
- U32 modulesUnloadedCount = 0;
- // Iterate the modules in reverse order calling their destroy function.
- for ( typeModuleLoadEntryVector::iterator moduleReadyItr = moduleReadyQueue.end()-1; moduleReadyItr >= moduleReadyQueue.begin(); --moduleReadyItr )
- {
- // Fetch the ready entry.
- ModuleLoadEntry* pReadyEntry = moduleReadyItr;
- // Fetch load ready module definition.
- ModuleDefinition* pLoadReadyModuleDefinition = pReadyEntry->mpModuleDefinition;;
- // Fetch any loaded entry for the module Id.
- ModuleLoadEntry* pLoadedEntry = findModuleLoaded( pLoadReadyModuleDefinition->getModuleId() );
- // Is the module loaded.
- if ( pLoadedEntry == NULL )
- {
- // No, so warn.
- if ( mEchoInfo )
- {
- Con::printf( "Module Manager: Unloading group '%s' but could not unload module Id '%s' at version Id '%d'.",
- moduleGroup, pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId());
- }
- // Skip.
- continue;
- }
- // Reduce load count.
- pLoadedEntry->mpModuleDefinition->reduceLoadCount();
- // Sanity!
- AssertFatal( pLoadedEntry->mpModuleDefinition->getLoadCount() >= 0, "ModuleManager::unloadModuleGroup() - Encountered an invalid load count." );
- // Do we need to unload?
- if ( pLoadedEntry->mpModuleDefinition->getLoadCount() == 0 )
- {
- // Yes, so info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Module Manager: Unload group '%s' with module Id '%s' at version Id '%d' in group '%s'.",
- moduleGroup, pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId(), pLoadReadyModuleDefinition->getModuleGroup() );
- }
- // Raise notifications.
- raiseModulePreUnloadNotifications( pLoadReadyModuleDefinition );
- // Fetch the module Id loaded entry.
- typeModuleLoadEntryVector::iterator moduleLoadedItr = findModuleLoaded( pLoadReadyModuleDefinition->getModuleId() );
- // Sanity!
- AssertFatal( moduleLoadedItr != NULL, "ModuleManager::unloadModuleGroup() - Cannot find module to unload it." );
- // Dequeue module loaded.
- mModulesLoaded.erase_fast( moduleLoadedItr );
- // Fetch scope set.
- SimSet* pScopeSet = dynamic_cast<SimSet*>(Sim::findObject(pLoadReadyModuleDefinition->mScopeSet));
- // Is the destroy method available?
- if ( pScopeSet->isMethod( pLoadReadyModuleDefinition->getDestroyFunction() ) )
- {
- // Yes, so call the destroy method.
- Con::executef( pScopeSet, pLoadReadyModuleDefinition->getDestroyFunction() );
- }
- // Remove scope set.
- pScopeSet->deleteAllObjects();
- pScopeSet->unregisterObject();
- pLoadReadyModuleDefinition->mScopeSet = 0;
- // Remove path expando for module.
- Con::removePathExpando( pLoadReadyModuleDefinition->getModuleId() );
- // Bump modules unloaded count.
- modulesUnloadedCount++;
- // Raise notifications.
- raiseModulePostUnloadNotifications( pLoadReadyModuleDefinition );
- }
- }
- // Info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Module Manager: Finish unloading '%d' module(s) for group '%s'.", modulesUnloadedCount, moduleGroup );
- Con::printSeparator();
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- bool ModuleManager::loadModuleExplicit( const char* pModuleId, const U32 versionId )
- {
- // Lock database.
- LockDatabase( this );
- // Sanity!
- AssertFatal( pModuleId != NULL, "Cannot load explicit module Id with NULL module Id." );
- typeModuleLoadEntryVector moduleResolvingQueue;
- typeModuleLoadEntryVector moduleReadyQueue;
- // Fetch module Id.
- StringTableEntry moduleId = StringTable->insert( pModuleId );
- // Fetch modules definitions.
- ModuleDefinitionEntry* pDefinitions = findModuleId( moduleId );
- // Did we find the module Id?
- if ( pDefinitions == NULL )
- {
- // No, so warn.
- Con::warnf( "Module Manager: Cannot load explicit module Id '%s' as it does not exist.", moduleId );
- return false;
- }
- // Fetch module group.
- StringTableEntry moduleGroup = pDefinitions->mModuleGroup;
- // Info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Module Manager: Loading explicit module Id '%s' at version Id '%d':", moduleId, versionId );
- }
- // Finish if we could not resolve the dependencies for module Id (of any version Id).
- if ( !resolveModuleDependencies( moduleId, versionId, moduleGroup, false, moduleResolvingQueue, moduleReadyQueue ) )
- return false;
- // Check the modules we want to load to ensure that we do not have incompatible modules loaded already.
- for ( typeModuleLoadEntryVector::iterator moduleReadyItr = moduleReadyQueue.begin(); moduleReadyItr != moduleReadyQueue.end(); ++moduleReadyItr )
- {
- // Fetch load ready module definition.
- ModuleDefinition* pLoadReadyModuleDefinition = moduleReadyItr->mpModuleDefinition;
- // Fetch the module Id loaded entry.
- ModuleLoadEntry* pLoadedModuleEntry = findModuleLoaded( pLoadReadyModuleDefinition->getModuleId() );
- // Did we find a loaded entry?
- if ( pLoadedModuleEntry != NULL )
- {
- // Yes, so is it the one we need to load?
- if ( pLoadedModuleEntry->mpModuleDefinition != pLoadReadyModuleDefinition )
- {
- // Yes, so warn.
- Con::warnf( "Module Manager: Cannot load explicit module Id '%s' at version Id '%d' as the module Id is already loaded but at version Id '%d'.",
- pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId(), pLoadedModuleEntry->mpModuleDefinition->getVersionId() );
- return false;
- }
- }
- }
- // Info.
- if ( mEchoInfo )
- {
- // Info.
- Con::printf( "Module Manager: Explicit load of module Id '%s' at version Id '%d' and its dependencies is comprised of the following '%d' module(s):", moduleId, versionId, moduleReadyQueue.size() );
- // Iterate the modules echoing them.
- for ( typeModuleLoadEntryVector::iterator moduleReadyItr = moduleReadyQueue.begin(); moduleReadyItr != moduleReadyQueue.end(); ++moduleReadyItr )
- {
- // Fetch the ready entry.
- ModuleDefinition* pModuleDefinition = moduleReadyItr->mpModuleDefinition;
- // Info.
- Con::printf( "> module Id '%s' at version Id '%d'", pModuleDefinition->getModuleId(), pModuleDefinition->getVersionId() );
- }
- }
- // Reset modules loaded count.
- U32 modulesLoadedCount = 0;
- // Iterate the modules, executing their script files and call their create function.
- for ( typeModuleLoadEntryVector::iterator moduleReadyItr = moduleReadyQueue.begin(); moduleReadyItr != moduleReadyQueue.end(); ++moduleReadyItr )
- {
- // Fetch the ready entry.
- ModuleLoadEntry* pReadyEntry = moduleReadyItr;
- // Fetch load ready module definition.
- ModuleDefinition* pLoadReadyModuleDefinition = pReadyEntry->mpModuleDefinition;
- // Fetch any loaded entry for the module Id.
- ModuleLoadEntry* pLoadedEntry = findModuleLoaded( pLoadReadyModuleDefinition->getModuleId() );
- // Is the module already loaded.
- if ( pLoadedEntry != NULL )
- {
- // Yes, so increase load count.
- pLoadedEntry->mpModuleDefinition->increaseLoadCount();
- // Skip.
- continue;
- }
- // No, so info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Module Manager: Loading explicit module Id '%s' at version Id '%d' using the script file '%s'.",
- pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId(), pLoadReadyModuleDefinition->getModuleScriptFilePath() );
- }
- // Is the module deprecated?
- if ( pLoadReadyModuleDefinition->getDeprecated() )
- {
- // Yes, so warn.
- Con::warnf( "Module Manager: Caution: module Id '%s' at version Id '%d' is deprecated, You should use a newer version!",
- pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId() );
- }
- // Add the path expando for module.
- Con::addPathExpando( pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getModulePath() );
- // Create a scope set.
- SimSet* pScopeSet = new SimSet;
- pScopeSet->registerObject( pLoadReadyModuleDefinition->getModuleId() );
- pReadyEntry->mpModuleDefinition->mScopeSet = pScopeSet->getId();
- // Increase load count.
- pReadyEntry->mpModuleDefinition->increaseLoadCount();
- // Queue module loaded.
- mModulesLoaded.push_back( *pReadyEntry );
- // Bump modules loaded count.
- modulesLoadedCount++;
- // Raise notifications.
- raiseModulePreLoadNotifications( pLoadReadyModuleDefinition );
- // Do we have a script file-path specified?
- if ( pLoadReadyModuleDefinition->getModuleScriptFilePath() != StringTable->EmptyString() )
- {
- // Yes, so execute the script file.
- ConsoleValue cValue = Con::executef("exec", pLoadReadyModuleDefinition->getModuleScriptFilePath());
- const bool scriptFileExecuted = cValue.getBool();
- // Did we execute the script file?
- if ( !scriptFileExecuted )
- {
- // No, so warn.
- Con::errorf( "Module Manager: Cannot load explicit module Id '%s' at version Id '%d' as it failed to have the script file '%s' loaded.",
- pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId(), pLoadReadyModuleDefinition->getModuleScriptFilePath() );
- }
- }
- // Is the create method available?
- if (pScopeSet->isMethod(pLoadReadyModuleDefinition->getCreateFunction()))
- {
- // Yes, so call the create method.
- Con::executef(pScopeSet, pLoadReadyModuleDefinition->getCreateFunction());
- }
- // Raise notifications.
- raiseModulePostLoadNotifications( pLoadReadyModuleDefinition );
- }
- // Info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Module Manager: Finish loading '%d' explicit module(s).", modulesLoadedCount );
- Con::printSeparator();
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- bool ModuleManager::unloadModuleExplicit( const char* pModuleId )
- {
- // Lock database.
- LockDatabase( this );
- // Sanity!
- AssertFatal( pModuleId != NULL, "Cannot unload explicit module Id with NULL module Id." );
- typeModuleLoadEntryVector moduleResolvingQueue;
- typeModuleLoadEntryVector moduleReadyQueue;
- // Fetch module Id.
- StringTableEntry moduleId = StringTable->insert( pModuleId );
- // Fetch modules definitions.
- ModuleDefinitionEntry* pDefinitions = findModuleId( moduleId );
- // Did we find the module Id?
- if ( pDefinitions == NULL )
- {
- // No, so warn.
- Con::warnf( "Module Manager: Cannot unload explicit module Id '%s' as it does not exist.", moduleId );
- return false;
- }
- // Find if the module is actually loaded.
- ModuleDefinition* pLoadedModule = findLoadedModule( moduleId );
- // Is the module loaded?
- if ( pLoadedModule == NULL )
- {
- // No, so warn.
- Con::warnf( "Module Manager: Cannot unload explicit module Id '%s' as it is not loaded.", moduleId );
- return false;
- }
- // Fetch module group.
- StringTableEntry moduleGroup = pDefinitions->mModuleGroup;
- // Info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Module Manager: Unloading explicit module Id '%s':" , moduleId );
- }
- // Finish if we could not resolve the dependencies for module Id (of any version Id).
- if ( !resolveModuleDependencies( moduleId, pLoadedModule->getVersionId(), moduleGroup, false, moduleResolvingQueue, moduleReadyQueue ) )
- return false;
- // Check the modules we want to unload to ensure that we do not have incompatible modules loaded already.
- for ( typeModuleLoadEntryVector::iterator moduleReadyItr = moduleReadyQueue.begin(); moduleReadyItr != moduleReadyQueue.end(); ++moduleReadyItr )
- {
- // Fetch load ready module definition.
- ModuleDefinition* pLoadReadyModuleDefinition = moduleReadyItr->mpModuleDefinition;;
- // Fetch the module Id loaded entry.
- ModuleLoadEntry* pLoadedModuleEntry = findModuleLoaded( pLoadReadyModuleDefinition->getModuleId() );
- // Did we find a loaded entry?
- if ( pLoadedModuleEntry != NULL )
- {
- // Yes, so is it the one we need to load?
- if ( pLoadedModuleEntry->mpModuleDefinition != pLoadReadyModuleDefinition )
- {
- // Yes, so warn.
- Con::warnf( "Module Manager: Cannot unload explicit module Id '%s' at version Id '%d' as the module Id is loaded but at version Id '%d'.",
- pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId(), pLoadedModuleEntry->mpModuleDefinition->getVersionId() );
- return false;
- }
- }
- }
- // Reset modules unloaded count.
- U32 modulesUnloadedCount = 0;
- // Iterate the modules in reverse order calling their destroy function.
- for ( typeModuleLoadEntryVector::iterator moduleReadyItr = moduleReadyQueue.end()-1; moduleReadyItr >= moduleReadyQueue.begin(); --moduleReadyItr )
- {
- // Fetch the ready entry.
- ModuleLoadEntry* pReadyEntry = moduleReadyItr;
- // Fetch load ready module definition.
- ModuleDefinition* pLoadReadyModuleDefinition = pReadyEntry->mpModuleDefinition;;
- // Fetch any loaded entry for the module Id.
- ModuleLoadEntry* pLoadedEntry = findModuleLoaded( pLoadReadyModuleDefinition->getModuleId() );
- // Is the module loaded.
- if ( pLoadedEntry == NULL )
- {
- // No, so warn.
- if ( mEchoInfo )
- {
- Con::printf( "Module Manager: Unloading explicit module Id '%s' at version Id '%d' but ignoring as it is not loaded.",
- pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId() );
- }
- // Skip.
- continue;
- }
- // Reduce load count.
- pLoadedEntry->mpModuleDefinition->reduceLoadCount();
- // Sanity!
- AssertFatal( pLoadedEntry->mpModuleDefinition->getLoadCount() >= 0, "ModuleManager::unloadModuleGroup() - Encountered an invalid load count." );
- // Do we need to unload?
- if ( pLoadedEntry->mpModuleDefinition->getLoadCount() == 0 )
- {
- // Yes, so info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Module Manager: Unload explicit module Id '%s' at version Id '%d'.",
- pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId() );
- }
- // Raise notifications.
- raiseModulePreUnloadNotifications( pLoadReadyModuleDefinition );
- // Fetch the module Id loaded entry.
- typeModuleLoadEntryVector::iterator moduleLoadedItr = findModuleLoaded( pLoadReadyModuleDefinition->getModuleId() );
- // Sanity!
- AssertFatal( moduleLoadedItr != NULL, "ModuleManager::unloadModuleExplicit() - Cannot find module to unload it." );
- // Dequeue module loaded.
- mModulesLoaded.erase_fast( moduleLoadedItr );
- // Fetch scope set.
- SimSet* pScopeSet = dynamic_cast<SimSet*>(Sim::findObject(pLoadReadyModuleDefinition->mScopeSet));
- // Is the destroy method available?
- if ( pScopeSet->isMethod( pLoadReadyModuleDefinition->getDestroyFunction() ) )
- {
- // Yes, so call the destroy method.
- Con::executef( pScopeSet, pLoadReadyModuleDefinition->getDestroyFunction() );
- }
- // Remove scope set.
- pScopeSet->deleteAllObjects();
- pScopeSet->unregisterObject();
- pLoadReadyModuleDefinition->mScopeSet = 0;
- // Remove path expando for module.
- Con::removePathExpando( pLoadReadyModuleDefinition->getModuleId() );
- // Bump modules unloaded count.
- modulesUnloadedCount++;
- // Raise notifications.
- raiseModulePostUnloadNotifications( pLoadReadyModuleDefinition );
- }
- }
- // Info.
- if ( mEchoInfo )
- {
- Con::printSeparator();
- Con::printf( "Module Manager: Finish unloading '%d' explicit module(s).", modulesUnloadedCount );
- Con::printSeparator();
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- ModuleDefinition* ModuleManager::findModule( const char* pModuleId, const U32 versionId )
- {
- // Sanity!
- AssertFatal( pModuleId != NULL, "Cannot find module with NULL module Id." );
- // Find module definition.
- ModuleDefinitionEntry::iterator moduleItr = findModuleDefinition( StringTable->insert( pModuleId ), versionId );
- // Finish if module was not found.
- if ( moduleItr == NULL )
- return NULL;
- return *moduleItr;
- }
- //-----------------------------------------------------------------------------
- ModuleDefinition* ModuleManager::findModuleByFilePath(StringTableEntry filePath)
- {
- // Sanity!
- AssertFatal(filePath != StringTable->EmptyString(), "Cannot find module with an empty filePath.");
- String desiredPath = filePath;
- StringTableEntry coreModuleId = StringTable->insert("CoreModule");
- for (typeModuleIdDatabaseHash::iterator moduleIdItr = mModuleIdDatabase.begin(); moduleIdItr != mModuleIdDatabase.end(); ++moduleIdItr)
- {
- // Fetch module definition entry.
- ModuleDefinitionEntry* pModuleDefinitionEntry = moduleIdItr->value;
- for (typeModuleDefinitionVector::iterator moduleDefinitionItr = pModuleDefinitionEntry->begin(); moduleDefinitionItr != pModuleDefinitionEntry->end(); ++moduleDefinitionItr)
- {
- // Fetch module definition.
- ModuleDefinition* pModuleDefinition = *moduleDefinitionItr;
- Torque::Path modulePath = pModuleDefinition->getModulePath();
- StringTableEntry asdasd = StringTable->insert(modulePath.getFullPath());
- //We don't deal with CoreModule or ToolsModule having assets for now
- if (desiredPath.startsWith(asdasd) && pModuleDefinition->mModuleId != coreModuleId)
- {
- return pModuleDefinition;
- }
- }
- }
- return nullptr;
- }
- //-----------------------------------------------------------------------------
- ModuleDefinition* ModuleManager::findLoadedModule( const char* pModuleId )
- {
- // Sanity!
- AssertFatal( pModuleId != NULL, "Cannot find module with NULL module Id." );
- // Fetch module Id.
- StringTableEntry moduleId = StringTable->insert( pModuleId );
- // Iterate loaded modules.
- for ( typeModuleLoadEntryVector::iterator loadedModuleItr = mModulesLoaded.begin(); loadedModuleItr != mModulesLoaded.end(); ++loadedModuleItr )
- {
- // Skip if not the module.
- if ( loadedModuleItr->mpModuleDefinition->getModuleId() != moduleId )
- continue;
- return loadedModuleItr->mpModuleDefinition;
- }
- return NULL;
- }
- //-----------------------------------------------------------------------------
- void ModuleManager::findModules( const bool loadedOnly, typeConstModuleDefinitionVector& moduleDefinitions )
- {
- // Iterate module Ids.
- for( typeModuleIdDatabaseHash::iterator moduleIdItr = mModuleIdDatabase.begin(); moduleIdItr != mModuleIdDatabase.end(); ++moduleIdItr )
- {
- // Fetch module definition entry.
- ModuleDefinitionEntry* pModuleDefinitionEntry = moduleIdItr->value;
- // Iterate module definitions.
- for ( typeModuleDefinitionVector::iterator moduleDefinitionItr = pModuleDefinitionEntry->begin(); moduleDefinitionItr != pModuleDefinitionEntry->end(); ++moduleDefinitionItr )
- {
- // Fetch module definition.
- ModuleDefinition* pModuleDefinition = *moduleDefinitionItr;
- // Are we searching for loaded modules only?
- if ( loadedOnly )
- {
- // Yes, so skip if the module is not loaded.
- if ( pModuleDefinition->getLoadCount() == 0 )
- continue;
- // Use module definition.
- moduleDefinitions.push_back( pModuleDefinition );
- // Finish iterating module definitions as only a single module in this entry can be loaded concurrently.
- break;
- }
- // use module definition.
- moduleDefinitions.push_back( pModuleDefinition );
- }
- }
- dQsort(moduleDefinitions.address(), moduleDefinitions.size(), sizeof(ModuleDefinition*), moduleDependencySort);
- }
- //-----------------------------------------------------------------------------
- void ModuleManager::findModuleTypes( const char* pModuleType, const bool loadedOnly, typeConstModuleDefinitionVector& moduleDefinitions )
- {
- // Fetch module type.
- StringTableEntry moduleType = StringTable->insert( pModuleType );
- // Iterate module Ids.
- for( typeModuleIdDatabaseHash::iterator moduleIdItr = mModuleIdDatabase.begin(); moduleIdItr != mModuleIdDatabase.end(); ++moduleIdItr )
- {
- // Fetch module definition entry.
- ModuleDefinitionEntry* pModuleDefinitionEntry = moduleIdItr->value;
- // Skip if note the module type we're searching for.
- if ( pModuleDefinitionEntry->mModuleType != moduleType )
- continue;
- // Iterate module definitions.
- for ( typeModuleDefinitionVector::iterator moduleDefinitionItr = pModuleDefinitionEntry->begin(); moduleDefinitionItr != pModuleDefinitionEntry->end(); ++moduleDefinitionItr )
- {
- // Fetch module definition.
- ModuleDefinition* pModuleDefinition = *moduleDefinitionItr;
- // Are we searching for loaded modules only?
- if ( loadedOnly )
- {
- // Yes, so skip if the module is not loaded.
- if ( pModuleDefinition->getLoadCount() == 0 )
- continue;
- // Use module definition.
- moduleDefinitions.push_back( pModuleDefinition );
- // Finish iterating module definitions as only a single module in this entry can be loaded concurrently.
- break;
- }
- // use module definition.
- moduleDefinitions.push_back( pModuleDefinition );
- }
- }
- }
- //-----------------------------------------------------------------------------
- StringTableEntry ModuleManager::copyModule( ModuleDefinition* pSourceModuleDefinition, const char* pTargetModuleId, const char* pTargetPath, const bool useVersionPathing )
- {
- // Sanity!
- AssertFatal( pSourceModuleDefinition != NULL, "Cannot copy module using a NULL source module definition." );
- AssertFatal( pTargetModuleId != NULL, "Cannot copy module using a NULL target module Id." );
- AssertFatal( pTargetPath != NULL, "Cannot copy module using a NULL target path." );
- // Fetch the source module Id.
- StringTableEntry sourceModuleId = pSourceModuleDefinition->getModuleId();
- // Is the source module definition registered with this module manager?
- if ( pSourceModuleDefinition->getModuleManager() != this )
- {
- // No, so warn.
- Con::warnf("Module Manager: Cannot copy module Id '%s' as it is not registered with this module manager.", sourceModuleId );
- return StringTable->EmptyString();
- }
- // Fetch the target module Id.
- StringTableEntry targetModuleId = StringTable->insert( pTargetModuleId );
- // Extend moduleId/VersionId pathing.
- char versionPathBuffer[1024];
- // Are we using version pathing?
- if ( useVersionPathing )
- {
- // Yes, so format it.
- dSprintf( versionPathBuffer, sizeof(versionPathBuffer), "%s/%s/%d",
- pTargetPath, targetModuleId, pSourceModuleDefinition->getVersionId() );
- }
- else
- {
- // No, so a straight copy.
- dSprintf( versionPathBuffer, sizeof(versionPathBuffer), "%s", pTargetPath );
- }
- // Expand the path.
- char targetPathBuffer[1024];
- Con::expandPath( targetPathBuffer, sizeof(targetPathBuffer), versionPathBuffer );
- pTargetPath = targetPathBuffer;
- // Info.
- if ( mEchoInfo )
- {
- Con::printf( "Module Manager: Started copying module Id '%s' to target directory '%s'.", sourceModuleId, pTargetPath );
- }
- // Is the target folder a directory?
- if ( !Platform::isDirectory( pTargetPath ) )
- {
- // No, so we have to ensure that there is a trailing slash as that indicates a folder (not a file) when creating a path in the platform code.
- char createDirectoryBuffer[1024];
- Con::expandPath( createDirectoryBuffer, sizeof(createDirectoryBuffer), pTargetPath, NULL, true );
- // No, so can we create it?
- if ( !Platform::createPath( createDirectoryBuffer ) )
- {
- // No, so warn.
- Con::warnf("Module Manager: Cannot copy module Id '%s' using target directory '%s' as directory was not found and could not be created.",
- sourceModuleId, pTargetPath );
- return StringTable->EmptyString();
- }
- }
- // Copy the source module to the target folder.
- if ( !dPathCopy( pSourceModuleDefinition->getModulePath(), pTargetPath, false ) )
- {
- // Warn.
- Con::warnf("Module Manager: Cannot copy module Id '%s' using target directory '%s' as directory copy failed.",
- sourceModuleId, pTargetPath );
- return StringTable->EmptyString();
- }
- // Format the new source module definition file-path.
- char newModuleDefinitionSourceFileBuffer[1024];
- dSprintf( newModuleDefinitionSourceFileBuffer, sizeof(newModuleDefinitionSourceFileBuffer), "%s/%s", pTargetPath, pSourceModuleDefinition->getModuleFile() );
- // Finish if source/target module Ids are identical.
- if ( sourceModuleId == targetModuleId )
- return StringTable->insert( newModuleDefinitionSourceFileBuffer );
- // Format the new target module definition file-path.
- char newModuleDefinitionTargetFileBuffer[1024];
- dSprintf( newModuleDefinitionTargetFileBuffer, sizeof(newModuleDefinitionTargetFileBuffer), "%s/%s.%s", pTargetPath, targetModuleId, MODULE_MANAGER_MODULE_DEFINITION_EXTENSION );
- // Rename the module definition.
- if ( !dFileRename( newModuleDefinitionSourceFileBuffer, newModuleDefinitionTargetFileBuffer ) )
- {
- // Warn.
- Con::warnf("Module Manager: Cannot copy module Id '%s' using target directory '%s' as renaming the module from '%s' to '%s' failed.",
- sourceModuleId, pTargetPath, newModuleDefinitionSourceFileBuffer, newModuleDefinitionTargetFileBuffer );
- return StringTable->EmptyString();
- }
- Vector<StringTableEntry> directories;
- // Find directories.
- if ( !Platform::dumpDirectories( pTargetPath, directories, -1 ) )
- {
- // Warn.
- Con::warnf("Module Manager: Cannot copy module Id '%s' using target directory '%s' as sub-folder scanning/renaming failed.",
- sourceModuleId, pTargetPath );
- return StringTable->EmptyString();
- }
- TamlModuleIdUpdateVisitor moduleIdUpdateVisitor;
- moduleIdUpdateVisitor.setModuleIdFrom( sourceModuleId );
- moduleIdUpdateVisitor.setModuleIdTo( targetModuleId );
- Vector<Platform::FileInfo> files;
- const char* pExtension = (const char*)"Taml";
- const U32 extensionLength = dStrlen(pExtension);
- // Iterate directories.
- for( Vector<StringTableEntry>::iterator basePathItr = directories.begin(); basePathItr != directories.end(); ++basePathItr )
- {
- // Fetch base path.
- StringTableEntry basePath = *basePathItr;
- // Find files.
- files.clear();
- if ( !Platform::dumpPath( basePath, files, 0 ) )
- {
- // Warn.
- Con::warnf("Module Manager: Cannot copy module Id '%s' using target directory '%s' as sub-folder scanning/renaming failed.",
- sourceModuleId, pTargetPath );
- return StringTable->EmptyString();
- }
- // Iterate files.
- for ( Vector<Platform::FileInfo>::iterator fileItr = files.begin(); fileItr != files.end(); ++fileItr )
- {
- // Fetch file info.
- Platform::FileInfo* pFileInfo = fileItr;
- // Fetch filename.
- const char* pFilename = pFileInfo->pFileName;
- // Find filename length.
- const U32 filenameLength = dStrlen( pFilename );
- // Skip if extension is longer than filename.
- if ( extensionLength >= filenameLength )
- continue;
- // Skip if extension not found.
- if ( dStricmp( pFilename + filenameLength - extensionLength, pExtension ) != 0 )
- continue;
- char parseFileBuffer[1024];
- dSprintf( parseFileBuffer, sizeof(parseFileBuffer), "%s/%s", pFileInfo->pFullPath, pFilename );
- // Parse file.
- if ( !mTaml.parse( parseFileBuffer, moduleIdUpdateVisitor ) )
- {
- // Warn.
- Con::warnf("Module Manager: Failed to parse file '%s' whilst copying module Id '%s' using target directory '%s'.",
- parseFileBuffer, sourceModuleId, pTargetPath );
- return StringTable->EmptyString();
- }
- }
- }
- // Info.
- if ( mEchoInfo )
- {
- Con::printf( "Module Manager: Finished copying module Id '%s' to target directory '%s'.", sourceModuleId, pTargetPath );
- }
- return StringTable->insert( newModuleDefinitionTargetFileBuffer );
- }
- //-----------------------------------------------------------------------------
- bool ModuleManager::renameModule(ModuleDefinition* pSourceModuleDefinition, const char* pNewModuleName)
- {
- // Sanity!
- AssertFatal(pSourceModuleDefinition != NULL, "Cannot copy module using a NULL source module definition.");
- AssertFatal(pNewModuleName != NULL, "Cannot rename a module using a NULL module name.");
- // Fetch the source module Id.
- StringTableEntry sourceModuleId = pSourceModuleDefinition->getModuleId();
- // Is the source module definition registered with this module manager?
- if (pSourceModuleDefinition->getModuleManager() != this)
- {
- // No, so warn.
- Con::warnf("Module Manager: Cannot rename module Id '%s' as it is not registered with this module manager.", sourceModuleId);
- return StringTable->EmptyString();
- }
- TamlModuleIdUpdateVisitor moduleIdUpdateVisitor;
- moduleIdUpdateVisitor.setModuleIdFrom(sourceModuleId);
- moduleIdUpdateVisitor.setModuleIdTo(pNewModuleName);
- Vector<Platform::FileInfo> files;
- const char* pExtension = (const char*)"Taml";
- const U32 extensionLength = dStrlen(pExtension);
- Vector<StringTableEntry> directories;
- StringTableEntry modulePath = pSourceModuleDefinition->getModulePath();
- // Find directories.
- if (!Platform::dumpDirectories(modulePath, directories, -1))
- {
- // Warn.
- Con::warnf("Module Manager: Cannot rename module Id '%s' in directory '%s' as sub-folder scanning/renaming failed.",
- sourceModuleId, modulePath);
- return false;
- }
- // Iterate directories.
- for (Vector<StringTableEntry>::iterator basePathItr = directories.begin(); basePathItr != directories.end(); ++basePathItr)
- {
- // Fetch base path.
- StringTableEntry basePath = *basePathItr;
- // Find files.
- files.clear();
- if (!Platform::dumpPath(basePath, files, 0))
- {
- // Warn.
- Con::warnf("Module Manager: Cannot rename module Id '%s' in directory '%s' as sub-folder scanning/renaming failed.",
- sourceModuleId, modulePath);
- return false;
- }
- // Iterate files.
- for (Vector<Platform::FileInfo>::iterator fileItr = files.begin(); fileItr != files.end(); ++fileItr)
- {
- // Fetch file info.
- Platform::FileInfo* pFileInfo = fileItr;
- // Fetch filename.
- const char* pFilename = pFileInfo->pFileName;
- // Find filename length.
- const U32 filenameLength = dStrlen(pFilename);
- // Skip if extension is longer than filename.
- if (extensionLength >= filenameLength)
- continue;
- // Skip if extension not found.
- if (dStricmp(pFilename + filenameLength - extensionLength, pExtension) != 0)
- continue;
- char parseFileBuffer[1024];
- dSprintf(parseFileBuffer, sizeof(parseFileBuffer), "%s/%s", pFileInfo->pFullPath, pFilename);
- // Parse file.
- if (!mTaml.parse(parseFileBuffer, moduleIdUpdateVisitor))
- {
- // Warn.
- Con::warnf("Module Manager: Failed to parse file '%s' whilst renaming module Id '%s' in directory '%s'.",
- parseFileBuffer, sourceModuleId, modulePath);
- return false;
- }
- }
- }
- // Info.
- if (mEchoInfo)
- {
- Con::printf("Module Manager: Finished renaming module Id '%s' to '%s'.", sourceModuleId, pNewModuleName);
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- bool ModuleManager::synchronizeDependencies( ModuleDefinition* pRootModuleDefinition, const char* pTargetDependencyPath )
- {
- // Sanity!
- AssertFatal( pRootModuleDefinition != NULL, "Cannot synchronize dependencies with NULL root module definition." );
- AssertFatal( pTargetDependencyPath != NULL, "Cannot synchronize dependencies with NULL target dependency path." );
- // Fetch the root module Id.
- StringTableEntry rootModuleId = pRootModuleDefinition->getModuleId();
- // Is the root module definition registered with this module manager?
- if ( pRootModuleDefinition->getModuleManager() != this )
- {
- // No, so warn.
- Con::warnf("Cannot synchronize dependencies for module Id '%s' as it is not registered with this module manager.", rootModuleId );
- return false;
- }
- // Expand the path.
- char targetPathBuffer[1024];
- Con::expandPath( targetPathBuffer, sizeof(targetPathBuffer), pTargetDependencyPath );
- pTargetDependencyPath = targetPathBuffer;
- // Is the target dependency folder a directory?
- if ( !Platform::isDirectory( pTargetDependencyPath ) )
- {
- // No, so we have to ensure that there is a trailing slash as that indicates a folder (not a file) when creating a path in the platform code.
- char createDirectoryBuffer[1024];
- Con::expandPath( createDirectoryBuffer, sizeof(createDirectoryBuffer), pTargetDependencyPath, NULL, true );
- // No, so can we create it?
- if ( !Platform::createPath( createDirectoryBuffer ) )
- {
- // No, so warn.
- Con::warnf("Cannot synchronize dependencies for module Id '%s' using target directory '%s' as directory was not found and could not be created.",
- rootModuleId, pTargetDependencyPath );
- return false;
- }
- }
- typeModuleLoadEntryVector resolvingQueue;
- typeModuleLoadEntryVector sourceModulesNeeded;
- // Could we resolve source dependencies?
- if ( !resolveModuleDependencies( rootModuleId, pRootModuleDefinition->getVersionId(), pRootModuleDefinition->getModuleGroup(), true, resolvingQueue, sourceModulesNeeded ) )
- {
- // No, so warn.
- Con::warnf("Cannot synchronize dependencies for root module Id '%s' as its dependencies could not be resolved.", rootModuleId );
- return false;
- }
- // Sanity!
- AssertFatal( sourceModulesNeeded.size() > 0, "Cannot synchronize dependencies as no modules were returned." );
- // Remove the root module definition.
- sourceModulesNeeded.pop_back();
- // Initialize the target module manager and scan the target folder for modules.
- ModuleManager targetModuleManager;
- targetModuleManager.mEnforceDependencies = true;
- targetModuleManager.mEchoInfo = false;
- targetModuleManager.scanModules( pTargetDependencyPath );
- char targetFolderGenerateBuffer[1024];
- // Iterate module definitions.
- for ( typeModuleLoadEntryVector::iterator sourceModuleItr = sourceModulesNeeded.begin(); sourceModuleItr != sourceModulesNeeded.end(); ++sourceModuleItr )
- {
- // Fetch module definition.
- ModuleDefinition* pSourceModuleDefinition = sourceModuleItr->mpModuleDefinition;
-
- // Fetch the source module Id,
- StringTableEntry sourceModuleId = pSourceModuleDefinition->getModuleId();
- // Fetch the source module version Id.
- const U32 sourceVersionId = pSourceModuleDefinition->getVersionId();
- // Fetch the source module build Id.
- const U32 sourceBuildId = pSourceModuleDefinition->getBuildId();
- // Fetch module definition entry for this module Id in the target.
- ModuleDefinitionEntry* pDefinitions = targetModuleManager.findModuleId( sourceModuleId );
- // Is the module Id present in the target?
- if ( pDefinitions == NULL )
- {
- // No, so format module Id folder path.
- dSprintf( targetFolderGenerateBuffer, sizeof(targetFolderGenerateBuffer), "%s/%s/", pTargetDependencyPath, sourceModuleId );
- // Create module Id folder.
- if ( !Platform::createPath( targetFolderGenerateBuffer ) )
- {
- // Warn.
- Con::warnf("Cannot synchronize dependencies for module Id '%s' as the target directory '%s' could not be created.", sourceModuleId, targetFolderGenerateBuffer );
- return false;
- }
- }
- else
- {
- // Yes, so fetch the module definition for this module Id and version Id in the target.
- ModuleDefinitionEntry::iterator definitionItr = targetModuleManager.findModuleDefinition( sourceModuleId, sourceVersionId );
- // Is the specific module definition present in the target?
- if ( definitionItr != NULL )
- {
- // Yes, so fetch the module definition.
- ModuleDefinition* pTargetModuleDefinition = *definitionItr;
- // Fetch the target module build Id.
- const U32 targetBuildId = pTargetModuleDefinition->getBuildId();
- // Fetch the target module path.
- StringTableEntry targetModulePath = pTargetModuleDefinition->getModulePath();
- // Remove the target module definition from the database.
- targetModuleManager.removeModuleDefinition( pTargetModuleDefinition );
- // Skip if the target definition is the same build Id.
- if ( targetBuildId == sourceBuildId )
- continue;
- // Delete the target module definition folder.
- if ( !Platform::deleteDirectory( targetModulePath ) )
- {
- // Warn.
- Con::warnf("Cannot synchronize dependencies for module Id '%s' at version Id '%d' as the old module at '%s' could not be deleted.",
- sourceModuleId, sourceVersionId, targetModulePath );
- return false;
- }
- }
- }
- // Format source module path.
- char sourceFolderPath[1024];
- Con::expandPath( sourceFolderPath, sizeof(sourceFolderPath), pSourceModuleDefinition->getModulePath() );
- // Format target module path.
- dSprintf( targetFolderGenerateBuffer, sizeof(targetFolderGenerateBuffer), "%s/%s/%d", pTargetDependencyPath, sourceModuleId, sourceVersionId );
- // Copy the source module to the target folder.
- if (!dPathCopy(sourceFolderPath, targetFolderGenerateBuffer, false))
- {
- // Warn.
- Con::warnf("Cannot synchronize dependencies for module Id '%s' at version Id '%d' as the module could not be copied to '%s'.",
- sourceModuleId, sourceVersionId, targetFolderGenerateBuffer );
- return false;
- }
- }
- // Find any target modules left, These are orphaned modules not depended upon by any other module.
- typeConstModuleDefinitionVector orphanedTargetModules;
- targetModuleManager.findModules( false, orphanedTargetModules );
- // Iterate module definitions.
- for ( typeConstModuleDefinitionVector::const_iterator moduleDefinitionItr = orphanedTargetModules.begin(); moduleDefinitionItr != orphanedTargetModules.end(); ++moduleDefinitionItr )
- {
- // Fetch orphaned module definition.
- const ModuleDefinition* pOrphanedModuleDefinition = *moduleDefinitionItr;
-
- // Delete the target module definition folder.
- if ( !Platform::deleteDirectory( pOrphanedModuleDefinition->getModulePath() ) )
- {
- // Warn.
- Con::warnf("Cannot delete orphaned module Id '%s' at version Id '%d' from '%s'.",
- pOrphanedModuleDefinition->getModuleId(), pOrphanedModuleDefinition->getVersionId(), pOrphanedModuleDefinition->getModulePath() );
- }
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- bool ModuleManager::canMergeModules( const char* pMergeSourcePath )
- {
- // Sanity!
- AssertFatal( pMergeSourcePath != NULL, "Cannot check merge modules with NULL source path." );
- // Expand the source path.
- char sourcePathBuffer[1024];
- Con::expandPath( sourcePathBuffer, sizeof(sourcePathBuffer), pMergeSourcePath );
- pMergeSourcePath = sourcePathBuffer;
- // Is the path a valid directory?
- if ( !Platform::isDirectory( sourcePathBuffer ) )
- {
- // No, so warn.
- Con::warnf( "Cannot check merge modules as path is invalid '%s'.", sourcePathBuffer );
- return false;
- }
- // Initialize the source module manager and scan the source folder for modules.
- ModuleManager mergeModuleManager;
- mergeModuleManager.mEnforceDependencies = false;
- mergeModuleManager.mEchoInfo = false;
- mergeModuleManager.scanModules( pMergeSourcePath );
- // Find all the merge modules.
- typeConstModuleDefinitionVector mergeModules;
- mergeModuleManager.findModules( false, mergeModules );
- // Iterate found merge module definitions.
- for ( typeConstModuleDefinitionVector::const_iterator mergeModuleItr = mergeModules.begin(); mergeModuleItr != mergeModules.end(); ++mergeModuleItr )
- {
- // Fetch module definition.
- const ModuleDefinition* pMergeModuleDefinition = *mergeModuleItr;
- // Fetch module Id.
- StringTableEntry moduleId = pMergeModuleDefinition->getModuleId();
- // Fetch version Id.
- const U32 versionId = pMergeModuleDefinition->getVersionId();
- // Fetch module group.
- StringTableEntry moduleGroup = pMergeModuleDefinition->getModuleGroup();
- // Cannot merge if module already exists.
- if ( findModuleDefinition( moduleId, versionId ) != NULL )
- return false;
- // Cannot merge if module is part of a loaded group.
- if ( findGroupLoaded( moduleGroup ) != NULL )
- return false;
- }
- // Can merge modules.
- return true;
- }
- //-----------------------------------------------------------------------------
- bool ModuleManager::mergeModules( const char* pMergeTargetPath, const bool removeMergeDefinition, const bool registerNewModules )
- {
- // Sanity!
- AssertFatal( pMergeTargetPath != NULL, "Cannot merge modules with a target path of NULL." );
- // Is a module merge available?
- if ( !isModuleMergeAvailable() )
- {
- // No, so warn.
- Con::warnf( "Cannot merge modules as a module merge is not available." );
- return false;
- }
- // Expand the target path.
- char targetPathBuffer[1024];
- Con::expandPath( targetPathBuffer, sizeof(targetPathBuffer), pMergeTargetPath );
- pMergeTargetPath = targetPathBuffer;
- // Fetch module merge file path.
- StringTableEntry moduleMergeFilePath = getModuleMergeFilePath();
- // Read module merge definition.
- Taml taml;
- ModuleMergeDefinition* pModuleMergeDefinition = taml.read<ModuleMergeDefinition>( moduleMergeFilePath );
-
- // Do we have a module merge definition.
- if ( pModuleMergeDefinition == NULL )
- {
- // No, so warn.
- Con::warnf( "Cannot merge modules as the module merge definition file failed to load '%s'.", moduleMergeFilePath );
- return false;
- }
- // Fetch the merge source path.
- StringTableEntry mergeSourcePath = pModuleMergeDefinition->getModuleMergePath();
- // Remove the module merge definition.
- pModuleMergeDefinition->deleteObject();
- pModuleMergeDefinition = NULL;
- // If we cannot merge the modules then we only process modules flagged as critical merge.
- const bool criticalMergeOnly = !canMergeModules( mergeSourcePath );
- // Initialize the target module manager and scan the target folder for modules.
- ModuleManager targetModuleManager;
- targetModuleManager.mEnforceDependencies = false;
- targetModuleManager.mEchoInfo = false;
- targetModuleManager.scanModules( pMergeTargetPath );
- // Initialize the source module manager and scan the source folder for modules.
- ModuleManager sourceModuleManager;
- sourceModuleManager.mEnforceDependencies = false;
- sourceModuleManager.mEchoInfo = false;
- sourceModuleManager.scanModules( mergeSourcePath );
- // Find all the source modules.
- typeConstModuleDefinitionVector sourceModules;
- sourceModuleManager.findModules( false, sourceModules );
- // Iterate found merge module definitions.
- for ( typeConstModuleDefinitionVector::const_iterator sourceModuleItr = sourceModules.begin(); sourceModuleItr != sourceModules.end(); ++sourceModuleItr )
- {
- // Fetch the source module definition.
- const ModuleDefinition* pSourceModuleDefinition = *sourceModuleItr;
- // Skip if we're performing a critical merge only and the module is not flagged as critical merge.
- if ( criticalMergeOnly && pSourceModuleDefinition->getCriticalMerge() )
- continue;
- // Fetch source module Id.
- const StringTableEntry sourceModuleId = pSourceModuleDefinition->getModuleId();
- // Fetch source version Id.
- const U32 sourceVersionId = pSourceModuleDefinition->getVersionId();
- // Fetch source build Id.
- const U32 sourceBuildId = pSourceModuleDefinition->getBuildId();
- // Format module Id folder path.
- char targetModuleIdBuffer[1024];
- dSprintf( targetModuleIdBuffer, sizeof(targetModuleIdBuffer), "%s/%s/", pMergeTargetPath, sourceModuleId );
- // Flag to indicate if the merged module needs registering.
- bool shouldRegisterModule;
- // Does the module Id exist?
- if ( targetModuleManager.findModuleId( sourceModuleId ) == NULL )
- {
- // No, so create module Id folder.
- if ( !Platform::createPath( targetModuleIdBuffer ) )
- {
- // Warn.
- Con::warnf("Cannot merge modules for module '%s' as the path '%s' could not be created.", sourceModuleId, targetModuleIdBuffer );
- return false;
- }
- // Module Should be registered.
- shouldRegisterModule = true;
- }
- else
- {
- // Yes, so find the target module definition that matches the source module definition.
- ModuleDefinitionEntry::iterator targetModuleDefinitionItr = targetModuleManager.findModuleDefinition( sourceModuleId, sourceVersionId );
- // Is there an existing target module definition entry?
- if ( targetModuleDefinitionItr != NULL )
- {
- // Yes, so fetch the target module definition.
- const ModuleDefinition* pTargetModuleDefinition = *targetModuleDefinitionItr;
- // Fetch target module path.
- StringTableEntry targetModulePath = pTargetModuleDefinition->getModulePath();
- // Yes, so we have to remove it first.
- if ( !Platform::deleteDirectory( targetModulePath ) )
- {
- // Module was not deleted so warn.
- Con::warnf( "Failed to remove module folder located at '%s'. Module will be copied over.", targetModulePath );
- }
- // Is the build Id being downgraded?
- if ( sourceBuildId < pTargetModuleDefinition->getBuildId() )
- {
- // Yes, so warn.
- Con::warnf( "Encountered a downgraded build Id for module Id '%s' at version Id '%d'.", sourceModuleId, sourceBuildId );
- }
- // Module should not be registered.
- shouldRegisterModule = false;
- }
- else
- {
- // Module Should be registered.
- shouldRegisterModule = true;
- }
- }
- // Fetch source module path.
- StringTableEntry sourceModulePath = pSourceModuleDefinition->getModulePath();
- // Format target version Id folder path.
- char targetVersionIdBuffer[1024];
- dSprintf( targetVersionIdBuffer, sizeof(targetVersionIdBuffer), "%s%d", targetModuleIdBuffer, sourceVersionId );
- // Copy module (allow overwrites as we may have failed to remove the old folder in which case this is likely to fail as well).
- if (!dPathCopy(sourceModulePath, targetVersionIdBuffer, false))
- {
- // Failed to copy module.
- Con::warnf( "Failed to copy module folder located at '%s' to location '%s'. The modules may now be corrupted.", sourceModulePath, targetVersionIdBuffer );
- }
- // Are we registering new modules and the module needs registering?
- if ( registerNewModules && shouldRegisterModule )
- {
- // Yes, so scan module.
- scanModules( targetVersionIdBuffer, true );
- }
- // Is the module part of a critical merge?
- if ( criticalMergeOnly )
- {
- // Yes, so we need to remove the source module definition.
- if ( !Platform::deleteDirectory( sourceModulePath ) )
- {
- // Module was not deleted so warn.
- Con::warnf( "Failed to remove CRITICAL merge module folder located at '%s'. Module will be copied over.", sourceModulePath );
- }
- }
- }
- // Do we need to remove the module merge definition file?
- if ( removeMergeDefinition )
- {
- // Yes, so remove it.
- dFileDelete( moduleMergeFilePath );
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- void ModuleManager::addListener( SimObject* pListener )
- {
- // Sanity!
- AssertFatal( pListener != NULL, "Cannot add notifications to a NULL object." );
- // Ignore if already added.
- if (mNotificationListeners.find( pListener) != mNotificationListeners.end())
- return;
-
- // Add as a listener.
- mNotificationListeners.addObject( pListener );
- }
- //-----------------------------------------------------------------------------
- void ModuleManager::removeListener( SimObject* pListener )
- {
- // Sanity!
- AssertFatal( pListener != NULL, "Cannot remove notifications from a NULL object." );
- // Remove as a listener.
- mNotificationListeners.removeObject( pListener );
- }
- //-----------------------------------------------------------------------------
- void ModuleManager::clearDatabase( void )
- {
- // Lock database.
- AssertFatal( mDatabaseLocks == 0, "Cannot clear database if database is locked." );
- // Iterate groups loaded.
- while ( mGroupsLoaded.size() > 0 )
- {
- // Unload module group.
- unloadModuleGroup( *mGroupsLoaded.begin() );
- }
- // Iterate any other explicit modules that are loaded.
- while ( mModulesLoaded.size() > 0 )
- {
- // Fetch module definition.
- ModuleDefinition* pModuleDefinition = mModulesLoaded.begin()->mpModuleDefinition;
- // Unload explicit module.
- unloadModuleExplicit( pModuleDefinition->getModuleId() );
- }
- // Iterate modules to delete module definitions.
- for ( typeModuleIdDatabaseHash::iterator moduleItr = mModuleIdDatabase.begin(); moduleItr != mModuleIdDatabase.end(); ++moduleItr )
- {
- // Fetch modules definitions.
- ModuleDefinitionEntry* pDefinitions = moduleItr->value;
- // Iterate module definitions.
- for ( ModuleDefinitionEntry::iterator definitionItr = pDefinitions->begin(); definitionItr != pDefinitions->end(); ++definitionItr )
- {
- // Fetch module definition.
- ModuleDefinition* pModuleDefinition = *definitionItr;
- // Remove notification before we delete it.
- clearNotify( pModuleDefinition );
- // Delete module definition.
- pModuleDefinition->deleteObject();
- }
- // Clear definitions.
- delete pDefinitions;
- }
- // Clear database.
- mModuleIdDatabase.clear();
- // Iterate module groups.
- for ( typeGroupModuleHash::iterator moduleGroupItr = mGroupModules.begin(); moduleGroupItr != mGroupModules.end(); ++moduleGroupItr )
- {
- // Delete module group vector.
- delete moduleGroupItr->value;
- }
- // Clear module groups.
- mGroupModules.clear();
- }
- //-----------------------------------------------------------------------------
- bool ModuleManager::removeModuleDefinition( ModuleDefinition* pModuleDefinition )
- {
- // Sanity!
- AssertFatal( pModuleDefinition != NULL, "Cannot remove module definition if it is NULL." );
-
- // Fetch module Id.
- StringTableEntry moduleId = pModuleDefinition->getModuleId();
- // Is the module definition registered with this module manager?
- if ( pModuleDefinition->getModuleManager() != this )
- {
- // No, so warn.
- Con::warnf("Cannot remove module definition '%s' as it is not registered with this module manager.", moduleId );
- return false;
- }
- // Is the module definition loaded?
- if ( pModuleDefinition->getLoadCount() > 0 )
- {
- // No, so warn.
- Con::warnf("Cannot remove module definition '%s' as it is loaded.", moduleId );
- return false;
- }
- // Find module Id.
- typeModuleIdDatabaseHash::iterator moduleItr = mModuleIdDatabase.find( moduleId );
- // Sanity!
- AssertFatal( moduleItr != mModuleIdDatabase.end(), "Failed to find module definition." );
- // Fetch modules definitions.
- ModuleDefinitionEntry* pDefinitions = moduleItr->value;
- // Fetch version Id.
- const U32 versionId = pModuleDefinition->getVersionId();
- // Iterate module definitions.
- for ( ModuleDefinitionEntry::iterator definitionItr = pDefinitions->begin(); definitionItr != pDefinitions->end(); ++definitionItr )
- {
- // Skip if this isn't the version Id we're searching for.
- if ( versionId != (*definitionItr)->getVersionId() )
- continue;
- // Remove definition entry.
- pDefinitions->erase( definitionItr );
- // Remove notification before we delete it.
- clearNotify( pModuleDefinition );
- // Delete module definition.
- pModuleDefinition->deleteObject();
- // Are there any modules left for this module Id?
- if ( findModuleId( moduleId ) == NULL )
- {
- bool moduleIdFound = false;
- // No, so remove from groups.
- for( typeGroupModuleHash::iterator moduleGroupItr = mGroupModules.begin(); moduleGroupItr != mGroupModules.end(); ++moduleGroupItr )
- {
- // Fetch module Ids.
- typeModuleIdVector* pModuleIds = moduleGroupItr->value;
- // Iterate module Id.
- for( typeModuleIdVector::iterator moduleIdItr = pModuleIds->begin(); moduleIdItr != pModuleIds->end(); ++moduleIdItr )
- {
- // Skip if this isn't the Id.
- if ( *moduleIdItr != moduleId )
- continue;
- // Remove the module Id.
- pModuleIds->erase( moduleIdItr );
- // Flag as found.
- moduleIdFound = true;
- break;
- }
- // Finish if found.
- if ( moduleIdFound )
- break;
- }
- }
- return true;
- }
- // Sanity!
- AssertFatal( false, "Failed to find module definition." );
- return false;
- }
- //-----------------------------------------------------------------------------
- bool ModuleManager::registerModule( const char* pModulePath, const char* pModuleFile )
- {
- // Sanity!
- AssertFatal( pModulePath != NULL, "Cannot scan module with NULL module path." );
- AssertFatal( pModuleFile != NULL, "Cannot scan module with NULL module file." );
- char formatBuffer[1024];
- // Fetch module path trail character.
- char modulePathTrail = pModulePath[dStrlen(pModulePath) - 1];
- // Format module file-path.
- dSprintf( formatBuffer, sizeof(formatBuffer), modulePathTrail == '/' ? "%s%s" : "%s/%s", pModulePath, pModuleFile );
- // Read the module file.
- ModuleDefinition* pModuleDefinition = mTaml.read<ModuleDefinition>( formatBuffer );
- // Did we read a module definition?
- if ( pModuleDefinition == NULL )
- {
- // No, so warn.
- Con::warnf( "Module Manager: Failed to read module definition in file '%s'.", formatBuffer );
- return false;
- }
- // Set the module manager.
- pModuleDefinition->setModuleManager( this );
- // Set module definition path.
- pModuleDefinition->setModulePath( pModulePath );
- // Set module file.
- pModuleDefinition->setModuleFile( pModuleFile );
- // Set module file-path.
- pModuleDefinition->setModuleFilePath( formatBuffer );
- // Fetch module Id.
- StringTableEntry moduleId = pModuleDefinition->getModuleId();
- // Fetch module version Id.
- const U32 versionId = pModuleDefinition->getVersionId();
- // Fetch module group.
- StringTableEntry moduleGroup = pModuleDefinition->getModuleGroup();
- // Fetch module type.
- StringTableEntry moduleType = pModuleDefinition->getModuleType();
- // Is the module enabled?
- if ( !pModuleDefinition->getEnabled() )
- {
- // No, so warn.
- Con::warnf( "Module Manager: Found module: '%s' but it is disabled.", pModuleDefinition->getModuleFilePath() );
- // Destroy module definition and finish.
- pModuleDefinition->deleteObject();
- return false;
- }
- // Is the module Id valid?
- if (moduleId == StringTable->EmptyString())
- {
- // No, so warn.
- Con::warnf( "Module Manager: Found module: '%s' but it has an unspecified module Id.",
- pModuleDefinition->getModuleFilePath() );
- // Destroy module definition and finish.
- pModuleDefinition->deleteObject();
- return false;
- }
- // Is the module version Id valid?
- if ( versionId == 0 )
- {
- // No, so warn.
- Con::warnf( "Module Manager: Found Manager: Registering module: '%s' but it has an invalid Version Id of '0'.",
- pModuleDefinition->getModuleFilePath() );
- // Destroy module definition and finish.
- pModuleDefinition->deleteObject();
- return false;
- }
- // Is the module group already loaded?
- if ( findGroupLoaded( moduleGroup ) != NULL && !mIgnoreLoadedGroups)
- {
- // Yes, so warn.
- Con::warnf( "Module Manager: Found module: '%s' but it is in a module group '%s' which has already been loaded.",
- pModuleDefinition->getModuleFilePath(),
- moduleGroup );
- // Destroy module definition and finish.
- pModuleDefinition->deleteObject();
- return false;
- }
- // Was a script-file specified?
- if ( pModuleDefinition->getScriptFile() != StringTable->EmptyString() )
- {
- // Yes, so format module script file-path.
- dSprintf( formatBuffer, sizeof(formatBuffer), modulePathTrail == '/' ? "%s%s" : "%s/%s", pModulePath, pModuleDefinition->getScriptFile() );
- pModuleDefinition->setModuleScriptFilePath( formatBuffer );
- }
- // Format module signature,
- dSprintf( formatBuffer, sizeof(formatBuffer), "%s_%d_%d", moduleId, versionId, pModuleDefinition->getBuildId() );
- pModuleDefinition->setSignature( formatBuffer );
- // Locked the module definition.
- pModuleDefinition->setModuleLocked( true );
- // Fetch modules definitions.
- ModuleDefinitionEntry* pDefinitions = findModuleId( moduleId );
- // Did we find the module Id?
- if ( pDefinitions != NULL )
- {
- // Yes, so find the module definition.
- ModuleDefinitionEntry::iterator definitionItr = findModuleDefinition( moduleId, versionId );
- // Does this version Id already exist?
- if ( definitionItr != NULL )
- {
- // Yes, so warn.
- Con::warnf( "Module Manager: Found module: '%s' but it is already registered as module Id '%s' at version Id '%d'.",
- pModuleDefinition->getModuleFilePath(), moduleId, versionId );
- // Destroy module definition and finish.
- pModuleDefinition->deleteObject();
- return false;
- }
- // Is the module group the same as the module definitions we already have?
- if ( moduleGroup != pDefinitions->mModuleGroup )
- {
- // No, so warn.
- Con::warnf( "Module Manager: Found module: '%s' but its module group '%s' is not the same as other module definitions of the same module Id.",
- pModuleDefinition->getModuleFilePath(), moduleGroup );
- // Destroy module definition and finish.
- pModuleDefinition->deleteObject();
- return false;
- }
- // Is the module type the same as the module definitions we already have?
- if ( moduleType != pDefinitions->mModuleType )
- {
- // No, so warn.
- Con::warnf( "Module Manager: Found module: '%s' but its module type '%s' is not the same as other module definitions of the same module Id.",
- pModuleDefinition->getModuleFilePath(), moduleGroup );
- // Destroy module definition and finish.
- pModuleDefinition->deleteObject();
- return false;
- }
- }
- else
- {
- // No, so create a vector of definitions.
- pDefinitions = new ModuleDefinitionEntry( moduleId, moduleGroup, moduleType );
- // Insert module Id definitions.
- mModuleIdDatabase.insert( moduleId, pDefinitions );
- }
-
- // Add module definition.
- pDefinitions->push_back( pModuleDefinition );
- // Sort module definitions by version Id so that higher versions appear first.
- dQsort( pDefinitions->address(), pDefinitions->size(), sizeof(ModuleDefinition*), moduleDefinitionVersionIdSort );
- // Find module group.
- typeGroupModuleHash::iterator moduleGroupItr = mGroupModules.find( moduleGroup );
- // Did we find the module group?
- if ( moduleGroupItr != mGroupModules.end() )
- {
- // Yes, so fetch module Ids.
- typeModuleIdVector* pModuleIds = moduleGroupItr->value;
- // Is the module Id already present?
- bool moduleIdFound = false;
- for( typeModuleIdVector::iterator moduleIdItr = pModuleIds->begin(); moduleIdItr != pModuleIds->end(); ++moduleIdItr )
- {
- // Skip if this isn't the Id.
- if ( *moduleIdItr != moduleId )
- continue;
- // Flag as found.
- moduleIdFound = true;
- break;
- }
- // Add if module Id was not found.
- if ( !moduleIdFound )
- pModuleIds->push_back( moduleId );
- }
- else
- {
- // No, so insert a module Id vector.
- moduleGroupItr = mGroupModules.insert( pModuleDefinition->getModuleGroup(), new typeModuleIdVector() );
- // Add module Id.
- moduleGroupItr->value->push_back( moduleId );
- }
- // Notify if the module definition is destroyed.
- deleteNotify( pModuleDefinition );
- // Info.
- if ( mEchoInfo )
- {
- #if 1
- Con::printf( "Module Manager: Registering: '%s' [ ID='%s', VersionId='%d', BuildId='%d', Description='%s' ].",
- pModuleDefinition->getModuleFilePath(),
- pModuleDefinition->getModuleId(),
- pModuleDefinition->getVersionId(),
- pModuleDefinition->getBuildId(),
- pModuleDefinition->getModuleDescription()
- );
- #else
- Con::printf( "Module Manager: Registering: '%s' [ ID='%s', VersionId='%d', BuildId='%d', Description='%s', Group='%s', Dependencies='%s', ScriptFile='%s', CreateFunction='%s', DestroyFunction='%s' ].",
- pModuleDefinition->getModuleFilePath(),
- pModuleDefinition->getModuleId(),
- pModuleDefinition->getVersionId(),
- pModuleDefinition->getBuildId(),
- pModuleDefinition->getModuleDescription(),
- pModuleDefinition->getModuleGroup(),
- pModuleDefinition->getDataField( StringTable->insert("Dependencies"), NULL ),
- pModuleDefinition->getScriptFile(),
- pModuleDefinition->getCreateFunction(),
- pModuleDefinition->getDestroyFunction()
- );
- #endif
- }
- // Emit notifications.
- for( SimSet::iterator notifyItr = mNotificationListeners.begin(); notifyItr != mNotificationListeners.end(); ++notifyItr )
- {
- Con::executef( *notifyItr, "onModuleRegister", pModuleDefinition->getIdString() );
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- bool ModuleManager::unregisterModule( const char* pModuleId, const U32 versionId )
- {
- // Sanity!
- AssertFatal( pModuleId != NULL, "A module Id cannot be NULL." );
- // Fetch module Id.
- StringTableEntry moduleId = StringTable->insert( pModuleId );
- // Find the module definition.
- ModuleDefinition* pModuleDefinition = findModule( pModuleId, versionId );
- // Did we find the module definition?
- if ( pModuleDefinition == NULL )
- {
- // No, so warn.
- Con::warnf( "Module Manager: Cannot unregister module Id '%s' as it is not registered.", moduleId );
- return false;
- }
- // Remove the module definition.
- return removeModuleDefinition( pModuleDefinition );
- }
- //-----------------------------------------------------------------------------
- void ModuleManager::raiseModulePreLoadNotifications( ModuleDefinition* pModuleDefinition )
- {
- // Raise notifications.
- for( SimSet::iterator notifyItr = mNotificationListeners.begin(); notifyItr != mNotificationListeners.end(); ++notifyItr )
- {
- // Fetch listener object.
- SimObject* pListener = *notifyItr;
- // Perform object callback.
- ModuleCallbacks* pCallbacks = dynamic_cast<ModuleCallbacks*>( pListener );
- if ( pCallbacks != NULL )
- pCallbacks->onModulePreLoad( pModuleDefinition );
-
- // Perform script callback.
- if ( pListener->isMethod( "onModulePreLoad" ) )
- Con::executef( pListener, "onModulePreLoad", pModuleDefinition->getIdString() );
- }
- }
- //-----------------------------------------------------------------------------
- void ModuleManager::raiseModulePostLoadNotifications( ModuleDefinition* pModuleDefinition )
- {
- // Raise notifications.
- for( SimSet::iterator notifyItr = mNotificationListeners.begin(); notifyItr != mNotificationListeners.end(); ++notifyItr )
- {
- // Fetch listener object.
- SimObject* pListener = *notifyItr;
- // Perform object callback.
- ModuleCallbacks* pCallbacks = dynamic_cast<ModuleCallbacks*>( pListener );
- if ( pCallbacks != NULL )
- pCallbacks->onModulePostLoad( pModuleDefinition );
-
- // Perform script callback.
- if ( pListener->isMethod( "onModulePostLoad" ) )
- Con::executef( pListener, "onModulePostLoad", pModuleDefinition->getIdString() );
- }
- }
- //-----------------------------------------------------------------------------
- void ModuleManager::raiseModulePreUnloadNotifications( ModuleDefinition* pModuleDefinition )
- {
- // Raise notifications.
- for( SimSet::iterator notifyItr = mNotificationListeners.begin(); notifyItr != mNotificationListeners.end(); ++notifyItr )
- {
- // Fetch listener object.
- SimObject* pListener = *notifyItr;
- // Perform object callback.
- ModuleCallbacks* pCallbacks = dynamic_cast<ModuleCallbacks*>( pListener );
- if ( pCallbacks != NULL )
- pCallbacks->onModulePreUnload( pModuleDefinition );
-
- // Perform script callback.
- if ( pListener->isMethod( "onModulePreUnload" ) )
- Con::executef( pListener, "onModulePreUnload", pModuleDefinition->getIdString() );
- }
- }
- //-----------------------------------------------------------------------------
- void ModuleManager::raiseModulePostUnloadNotifications( ModuleDefinition* pModuleDefinition )
- {
- // Raise notifications.
- for( SimSet::iterator notifyItr = mNotificationListeners.begin(); notifyItr != mNotificationListeners.end(); ++notifyItr )
- {
- // Fetch listener object.
- SimObject* pListener = *notifyItr;
- // Perform object callback.
- ModuleCallbacks* pCallbacks = dynamic_cast<ModuleCallbacks*>( pListener );
- if ( pCallbacks != NULL )
- pCallbacks->onModulePostUnload( pModuleDefinition );
-
- // Perform script callback.
- if ( pListener->isMethod( "onModulePostUnload" ) )
- Con::executef( pListener, "onModulePostUnload", pModuleDefinition->getIdString() );
- }
- }
- //-----------------------------------------------------------------------------
- ModuleManager::ModuleDefinitionEntry* ModuleManager::findModuleId( StringTableEntry moduleId )
- {
- // Sanity!
- AssertFatal( moduleId != NULL, "A module Id cannot be NULL." );
- // Is the module Id valid?
- if ( moduleId == StringTable->EmptyString() )
- {
- // No, so warn.
- Con::warnf( "Module Manager: Invalid Module Id." );
- return NULL;
- }
- // Find module Id.
- typeModuleIdDatabaseHash::iterator moduleItr = mModuleIdDatabase.find( moduleId );
- // Return appropriately.
- return moduleItr != mModuleIdDatabase.end() ? moduleItr->value : NULL;
- }
- //-----------------------------------------------------------------------------
- ModuleManager::ModuleDefinitionEntry::iterator ModuleManager::findModuleDefinition( StringTableEntry moduleId, const U32 versionId )
- {
- // Fetch modules definitions.
- ModuleDefinitionEntry* pDefinitions = findModuleId( moduleId );
- // Finish if no module definitions for the module Id.
- if ( pDefinitions == NULL )
- return NULL;
- // Iterate module definitions.
- for ( ModuleDefinitionEntry::iterator definitionItr = pDefinitions->begin(); definitionItr != pDefinitions->end(); ++definitionItr )
- {
- // Skip if this isn't the version Id we're searching for.
- if ( versionId != (*definitionItr)->getVersionId() )
- continue;
- // Return module definition iterator.
- return definitionItr;
- }
- // Not found.
- return NULL;
- }
- //-----------------------------------------------------------------------------
- bool ModuleManager::resolveModuleDependencies( StringTableEntry moduleId, const U32 versionId, StringTableEntry moduleGroup, bool synchronizedOnly, typeModuleLoadEntryVector& moduleResolvingQueue, typeModuleLoadEntryVector& moduleReadyQueue )
- {
- // Fetch the module Id ready entry.
- ModuleLoadEntry* pLoadReadyEntry = findModuleReady( moduleId, moduleReadyQueue );
- // Is there a load entry?
- if ( pLoadReadyEntry )
- {
- // Yes, so finish if the version Id is not important,
- if ( versionId == 0 )
- return true;
- // Finish if the version Id are compatible.
- if ( versionId == pLoadReadyEntry->mpModuleDefinition->getVersionId() )
- return true;
- // Is it a strict version Id?
- if ( pLoadReadyEntry->mStrictVersionId )
- {
- // Yes, so warn.
- Con::warnf( "Module Manager: A module dependency was detected loading module Id '%s' at version Id '%d' in group '%s' but an version Id '%d' is also required.",
- moduleId, versionId, pLoadReadyEntry->mpModuleDefinition->getVersionId(), moduleGroup );
- return false;
- }
- // No, so find the required module version Id.
- ModuleDefinitionEntry::iterator definitionItr = findModuleDefinition( moduleId, versionId );
- // Did we find the requested module definition.
- if ( definitionItr == NULL )
- {
- // No, so we can safely ignore the missing dependency if we're not enforcing dependencies.
- if ( !mEnforceDependencies )
- return true;
- // Warn!
- Con::warnf( "Module Manager: A missing module dependency was detected loading module Id '%s' at version Id '%d' in group '%s'.",
- moduleId, versionId, moduleGroup );
- return false;
- }
- // Set the new module definition.
- pLoadReadyEntry->mpModuleDefinition = *definitionItr;
- // Set strict version Id.
- pLoadReadyEntry->mStrictVersionId = true;
-
- return true;
- }
- // Is the module Id load resolving?
- if ( findModuleResolving( moduleId, moduleResolvingQueue ) != NULL )
- {
- // Yes, so a cycle has been detected so warn.
- Con::warnf( "Module Manager: A cyclic dependency was detected resolving module Id '%s' at version Id '%d' in group '%s'.",
- moduleId, versionId, moduleGroup );
- return false;
- }
- // Reset selected module definition.
- ModuleDefinition* pSelectedModuleDefinition = NULL;
- // Do we want the latest version Id?
- if ( versionId == 0 )
- {
- // Yes, so find the module Id.
- typeModuleIdDatabaseHash::iterator moduleIdItr = mModuleIdDatabase.find( moduleId );
- // Did we find the module Id?
- if ( moduleIdItr == mModuleIdDatabase.end() )
- {
- // No, so we can safely ignore the missing dependency if we're not enforcing dependencies.
- if ( !mEnforceDependencies )
- return true;
- // Warn!
- Con::warnf( "Module Manager: A missing module dependency was detected loading module Id '%s' at version Id '%d' in group '%s'.",
- moduleId, versionId, moduleGroup );
- return false;
- }
- // Fetch first module definition which should be the highest version Id.
- pSelectedModuleDefinition = (*moduleIdItr->value)[0];
- }
- else
- {
- // No, so find the module Id at the specific version Id.
- ModuleDefinitionEntry::iterator definitionItr = findModuleDefinition( moduleId, versionId );
- // Did we find the module definition?
- if ( definitionItr == NULL )
- {
- // No, so we can safely ignore the missing dependency if we're not enforcing dependencies.
- if ( !mEnforceDependencies )
- return true;
- // Warn!
- Con::warnf( "Module Manager: A missing module dependency was detected loading module Id '%s' at version Id '%d' in group '%s'.",
- moduleId, versionId, moduleGroup );
- return false;
- }
- // Select the module definition.
- pSelectedModuleDefinition = *definitionItr;
- }
- // If we're only resolving synchronized modules and the module is not synchronized then finish.
- if ( synchronizedOnly && !pSelectedModuleDefinition->getSynchronized() )
- return true;
- // Create a load entry.
- ModuleLoadEntry loadEntry( pSelectedModuleDefinition, false );
- // Fetch module dependencies.
- const ModuleDefinition::typeModuleDependencyVector& moduleDependencies = pSelectedModuleDefinition->getDependencies();
- // Do we have any module dependencies?
- if ( moduleDependencies.size() > 0 )
- {
- // Yes, so queue this module as resolving.
- moduleResolvingQueue.push_back( loadEntry );
- // Iterate module dependencies.
- for( ModuleDefinition::typeModuleDependencyVector::const_iterator dependencyItr = moduleDependencies.begin(); dependencyItr != moduleDependencies.end(); ++dependencyItr )
- {
- // Finish if we could not the dependent module Id at the version Id.
- if ( !resolveModuleDependencies( dependencyItr->mModuleId, dependencyItr->mVersionId, moduleGroup, synchronizedOnly, moduleResolvingQueue, moduleReadyQueue ) )
- return false;
- }
- // Remove module as resolving.
- moduleResolvingQueue.pop_back();
- }
- // Queue module as ready.
- moduleReadyQueue.push_back( loadEntry );
- return true;
- }
- //-----------------------------------------------------------------------------
- ModuleManager::ModuleLoadEntry* ModuleManager::findModuleResolving( StringTableEntry moduleId, typeModuleLoadEntryVector& moduleResolvingQueue )
- {
- // Iterate module load resolving queue.
- for( typeModuleLoadEntryVector::iterator loadEntryItr = moduleResolvingQueue.begin(); loadEntryItr != moduleResolvingQueue.end(); ++loadEntryItr )
- {
- // Finish if found.
- if ( moduleId == loadEntryItr->mpModuleDefinition->getModuleId() )
- return loadEntryItr;
- }
- // Not found.
- return NULL;
- }
- //-----------------------------------------------------------------------------
- ModuleManager::ModuleLoadEntry* ModuleManager::findModuleReady( StringTableEntry moduleId, typeModuleLoadEntryVector& moduleReadyQueue )
- {
- // Iterate module load ready queue.
- for( typeModuleLoadEntryVector::iterator loadEntryItr = moduleReadyQueue.begin(); loadEntryItr != moduleReadyQueue.end(); ++loadEntryItr )
- {
- // Finish if found.
- if ( moduleId == loadEntryItr->mpModuleDefinition->getModuleId() )
- return loadEntryItr;
- }
- // Not found.
- return NULL;
- }
- //-----------------------------------------------------------------------------
- ModuleManager::typeModuleLoadEntryVector::iterator ModuleManager::findModuleLoaded( StringTableEntry moduleId, const U32 versionId )
- {
- // Iterate module loaded queue.
- for( typeModuleLoadEntryVector::iterator loadEntryItr = mModulesLoaded.begin(); loadEntryItr != mModulesLoaded.end(); ++loadEntryItr )
- {
- // Skip if not the module Id we're looking for.
- if ( moduleId != loadEntryItr->mpModuleDefinition->getModuleId() )
- continue;
- // Skip if we are searching for a specific version and it does not match.
- if ( versionId != 0 && versionId != loadEntryItr->mpModuleDefinition->getVersionId() )
- continue;
- return loadEntryItr;
- }
- // Not found.
- return NULL;
- }
- //-----------------------------------------------------------------------------
- ModuleManager::typeGroupVector::iterator ModuleManager::findGroupLoaded( StringTableEntry moduleGroup )
- {
- // Iterate groups loaded queue.
- for( typeGroupVector::iterator groupsLoadedItr = mGroupsLoaded.begin(); groupsLoadedItr != mGroupsLoaded.end(); ++groupsLoadedItr )
- {
- // Finish if found.
- if ( moduleGroup == *groupsLoadedItr )
- return groupsLoadedItr;
- }
- // Not found.
- return NULL;
- }
- //-----------------------------------------------------------------------------
- StringTableEntry ModuleManager::getModuleMergeFilePath( void ) const
- {
- // Format merge file path.
- char filePathBuffer[1024];
- dSprintf( filePathBuffer, sizeof(filePathBuffer), "%s/%s", Platform::getExecutablePath(), MODULE_MANAGER_MERGE_FILE );
- return StringTable->insert( filePathBuffer );
- }
|