| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513 | //-----------------------------------------------------------------------------// 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// 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;}//-----------------------------------------------------------------------------ModuleManager::ModuleManager() :    mEnforceDependencies(true),    mEchoInfo(true),    mDatabaseLocks( 0 ){    // Set module extension.    dStrcpy( mModuleExtension, MODULE_MANAGER_MODULE_DEFINITION_EXTENSION );}//-----------------------------------------------------------------------------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 );    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." );    // Expand module location.    char pathBuffer[1024];    Con::expandPath( pathBuffer, sizeof(pathBuffer), pPath );    // Info.    if ( mEchoInfo )    {        Con::printSeparator();        Con::printf( "Module Manager: Started scanning '%s'...", pathBuffer );    }    Vector<StringTableEntry> directories;    // Find directories.    if ( !Platform::dumpDirectories( pathBuffer, directories, rootOnly ? 1 : -1 ) )    {        // Failed so warn.        Con::warnf( "Module Manager: Failed to scan module directories in path '%s'.", pathBuffer );        return false;    }    // Fetch extension length.    const U32 extensionLength = dStrlen( mModuleExtension );    Vector<Platform::FileInfo> files;    // Iterate directories.    for( Vector<StringTableEntry>::iterator basePathItr = directories.begin(); basePathItr != directories.end(); ++basePathItr )    {        // Fetch base path.        StringTableEntry basePath = *basePathItr;        // Skip if we're only processing the root and this is not the root.        if ( rootOnly && basePathItr != directories.begin() )            continue;        // Find files.        files.clear();        if ( !Platform::dumpPath( basePath, files, 0 ) )        {            // Failed so warn.            Con::warnf( "Module Manager: Failed to scan modules files in directory '%s'.", basePath );            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, mModuleExtension ) != 0 )                continue;            // Register module.            registerModule( basePath, pFileInfo->pFileName );        }        // Stop processing if we're only processing the root.        if ( rootOnly )            break;    }    // Info.    if ( mEchoInfo )    {        Con::printf( "Module Manager: Finished scanning '%s'.", pathBuffer );    }    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.            const bool scriptFileExecuted = dAtob( Con::executef("exec", pLoadReadyModuleDefinition->getModuleScriptFilePath() ) );            // 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.                    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() );            }        }        // 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, pLoadedEntry->mpModuleDefinition->getModuleId(), pLoadedEntry->mpModuleDefinition->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.            const bool scriptFileExecuted = dAtob( Con::executef("exec", pLoadReadyModuleDefinition->getModuleScriptFilePath() ) );            // 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.                    Con::executef( pScopeSet, pLoadReadyModuleDefinition->getCreateFunction() );                }            }            else            {                // 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() );            }        }        // 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::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 );        }    }}//-----------------------------------------------------------------------------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::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." );    // Make the module path a full-path.    char fullPathBuffer[1024];    Platform::makeFullPathName( pModulePath, fullPathBuffer, sizeof(fullPathBuffer) );    pModulePath = fullPathBuffer;    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 )    {        // 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->setLocked( 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 );}
 |