moduleManager.cpp 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "moduleManager.h"
  23. #ifndef _MODULE_MERGE_DEFINITION_H
  24. #include "moduleMergeDefinition.h"
  25. #endif
  26. #ifndef _TAML_MODULE_ID_UPDATE_VISITOR_H_
  27. #include "tamlModuleIdUpdateVisitor.h"
  28. #endif
  29. #ifndef _MODULE_CALLBACKS_H_
  30. #include "moduleCallbacks.h"
  31. #endif
  32. #ifndef _CONSOLETYPES_H_
  33. #include "console/consoleTypes.h"
  34. #endif
  35. // Script bindings.
  36. #include "moduleManager_ScriptBinding.h"
  37. //-----------------------------------------------------------------------------
  38. IMPLEMENT_CONOBJECT( ModuleManager );
  39. //-----------------------------------------------------------------------------
  40. ModuleManager ModuleDatabase;
  41. //-----------------------------------------------------------------------------
  42. S32 QSORT_CALLBACK moduleDefinitionVersionIdSort( const void* a, const void* b )
  43. {
  44. // Fetch module definitions.
  45. ModuleDefinition* pDefinition1 = *(ModuleDefinition**)a;
  46. ModuleDefinition* pDefinition2 = *(ModuleDefinition**)b;
  47. // Fetch version Ids.
  48. const U32 versionId1 = pDefinition1->getVersionId();
  49. const U32 versionId2 = pDefinition2->getVersionId();
  50. // We sort higher version Id first.
  51. return versionId1 > versionId2 ? -1 : versionId1 < versionId2 ? 1 : 0;
  52. }
  53. //-----------------------------------------------------------------------------
  54. ModuleManager::ModuleManager() :
  55. mEnforceDependencies(true),
  56. mEchoInfo(false),
  57. mDatabaseLocks( 0 ),
  58. mIgnoreLoadedGroups(false)
  59. {
  60. // Set module extension.
  61. dStrcpy( mModuleExtension, MODULE_MANAGER_MODULE_DEFINITION_EXTENSION, 256 );
  62. }
  63. //-----------------------------------------------------------------------------
  64. bool ModuleManager::onAdd()
  65. {
  66. if( !Parent::onAdd() )
  67. return false;
  68. // Register listeners.
  69. mNotificationListeners.registerObject();
  70. return true;
  71. }
  72. //-----------------------------------------------------------------------------
  73. void ModuleManager::onRemove()
  74. {
  75. // Clear database.
  76. clearDatabase();
  77. // Unregister object.
  78. mNotificationListeners.unregisterObject();
  79. // Call parent.
  80. Parent::onRemove();
  81. }
  82. //-----------------------------------------------------------------------------
  83. void ModuleManager::initPersistFields()
  84. {
  85. // Call parent.
  86. Parent::initPersistFields();
  87. addField( "EnforceDependencies", TypeBool, Offset(mEnforceDependencies, ModuleManager), "Whether the module manager enforces any dependencies on module definitions it discovers or not." );
  88. addField( "EchoInfo", TypeBool, Offset(mEchoInfo, ModuleManager), "Whether the module manager echos extra information to the console or not." );
  89. }
  90. //-----------------------------------------------------------------------------
  91. void ModuleManager::onDeleteNotify( SimObject *object )
  92. {
  93. // Cast to a module definition.
  94. ModuleDefinition* pModuleDefinition = dynamic_cast<ModuleDefinition*>( object );
  95. // Ignore if not appropriate.
  96. if ( pModuleDefinition == NULL )
  97. return;
  98. // Warn.
  99. 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.",
  100. pModuleDefinition->getModuleId(), pModuleDefinition->getVersionId() );
  101. }
  102. //-----------------------------------------------------------------------------
  103. bool ModuleManager::setModuleExtension( const char* pExtension )
  104. {
  105. // Sanity!
  106. AssertFatal( pExtension != NULL, "Cannot set module extension with NULL extension." );
  107. // Did we find an extension period?
  108. if ( *pExtension == '.' )
  109. {
  110. // Yes, so warn.
  111. Con::warnf("Module Manager: Failed to set extension as supplied extension contains an initial period: '%s'.", pExtension );
  112. return false;
  113. }
  114. // Is the extension too large?
  115. if ( dStrlen( pExtension ) > sizeof( mModuleExtension ) )
  116. {
  117. // Yes, so warn.
  118. Con::warnf("Module Manager: Failed to set extension as supplied extension is too large: '%s'.", pExtension );
  119. return false;
  120. }
  121. // Set module extension.
  122. dStrcpy( mModuleExtension, pExtension, 256 );
  123. return true;
  124. }
  125. //-----------------------------------------------------------------------------
  126. bool ModuleManager::scanModules( const char* pPath, const bool rootOnly )
  127. {
  128. // Lock database.
  129. LockDatabase( this );
  130. // Sanity!
  131. AssertFatal( pPath != NULL, "Cannot scan module with NULL path." );
  132. // Expand module location.
  133. char pathBuffer[1024];
  134. Con::expandPath( pathBuffer, sizeof(pathBuffer), pPath );
  135. // Info.
  136. if ( mEchoInfo )
  137. {
  138. Con::printSeparator();
  139. Con::printf( "Module Manager: Started scanning '%s'...", pathBuffer );
  140. }
  141. Vector<StringTableEntry> directories;
  142. // Find directories.
  143. if ( !Platform::dumpDirectories( pathBuffer, directories, rootOnly ? 1 : -1 ) )
  144. {
  145. // Failed so warn.
  146. Con::warnf( "Module Manager: Failed to scan module directories in path '%s'.", pathBuffer );
  147. return false;
  148. }
  149. Torque::FS::sgMountSystem;
  150. // Fetch extension length.
  151. const U32 extensionLength = dStrlen( mModuleExtension );
  152. Vector<Platform::FileInfo> files;
  153. // Iterate directories.
  154. for( Vector<StringTableEntry>::iterator basePathItr = directories.begin(); basePathItr != directories.end(); ++basePathItr )
  155. {
  156. // Fetch base path.
  157. StringTableEntry basePath = *basePathItr;
  158. // Skip if we're only processing the root and this is not the root.
  159. if ( rootOnly && basePathItr != directories.begin() )
  160. continue;
  161. // Find files.
  162. files.clear();
  163. if ( !Platform::dumpPath( basePath, files, 0 ) )
  164. {
  165. // Failed so warn.
  166. Con::warnf( "Module Manager: Failed to scan modules files in directory '%s'.", basePath );
  167. return false;
  168. }
  169. // Iterate files.
  170. for ( Vector<Platform::FileInfo>::iterator fileItr = files.begin(); fileItr != files.end(); ++fileItr )
  171. {
  172. // Fetch file info.
  173. Platform::FileInfo* pFileInfo = fileItr;
  174. // Fetch filename.
  175. const char* pFilename = pFileInfo->pFileName;
  176. // Find filename length.
  177. const U32 filenameLength = dStrlen( pFilename );
  178. // Skip if extension is longer than filename.
  179. if ( extensionLength > filenameLength )
  180. continue;
  181. // Skip if extension not found.
  182. if ( dStricmp( pFilename + filenameLength - extensionLength, mModuleExtension ) != 0 )
  183. continue;
  184. // Register module.
  185. registerModule( basePath, pFileInfo->pFileName );
  186. }
  187. // Stop processing if we're only processing the root.
  188. if ( rootOnly )
  189. break;
  190. }
  191. // Info.
  192. if ( mEchoInfo )
  193. {
  194. Con::printf( "Module Manager: Finished scanning '%s'.", pathBuffer );
  195. }
  196. return true;
  197. }
  198. //-----------------------------------------------------------------------------
  199. bool ModuleManager::loadModuleGroup( const char* pModuleGroup )
  200. {
  201. // Lock database.
  202. LockDatabase( this );
  203. // Sanity!
  204. AssertFatal( pModuleGroup != NULL, "Cannot load module group with NULL group name." );
  205. typeModuleLoadEntryVector moduleResolvingQueue;
  206. typeModuleLoadEntryVector moduleReadyQueue;
  207. // Fetch module group.
  208. StringTableEntry moduleGroup = StringTable->insert( pModuleGroup );
  209. // Info.
  210. if ( mEchoInfo )
  211. {
  212. Con::printSeparator();
  213. Con::printf( "Module Manager: Loading group '%s':" ,moduleGroup );
  214. }
  215. // Is the module group already loaded?
  216. if ( findGroupLoaded( moduleGroup ) != NULL )
  217. {
  218. // Yes, so warn.
  219. Con::warnf( "Module Manager: Cannot load group '%s' as it is already loaded.", moduleGroup );
  220. return false;
  221. }
  222. // Find module group.
  223. typeGroupModuleHash::iterator moduleGroupItr = mGroupModules.find( moduleGroup );
  224. // Did we find the module group?
  225. if ( moduleGroupItr == mGroupModules.end() )
  226. {
  227. // No, so info.
  228. if ( mEchoInfo )
  229. {
  230. Con::printf( "Module Manager: No modules found for module group '%s'.", moduleGroup );
  231. }
  232. return true;
  233. }
  234. // Yes, so fetch the module Ids.
  235. typeModuleIdVector* pModuleIds = moduleGroupItr->value;
  236. // Iterate module groups.
  237. for( typeModuleIdVector::iterator moduleIdItr = pModuleIds->begin(); moduleIdItr != pModuleIds->end(); ++moduleIdItr )
  238. {
  239. // Fetch module Id.
  240. StringTableEntry moduleId = *moduleIdItr;
  241. // Finish if we could not resolve the dependencies for module Id (of any version Id).
  242. if ( !resolveModuleDependencies( moduleId, 0, moduleGroup, false, moduleResolvingQueue, moduleReadyQueue ) )
  243. return false;
  244. }
  245. // Check the modules we want to load to ensure that we do not have incompatible modules loaded already.
  246. for ( typeModuleLoadEntryVector::iterator moduleReadyItr = moduleReadyQueue.begin(); moduleReadyItr != moduleReadyQueue.end(); ++moduleReadyItr )
  247. {
  248. // Fetch load ready module definition.
  249. ModuleDefinition* pLoadReadyModuleDefinition = moduleReadyItr->mpModuleDefinition;;
  250. // Fetch the module Id loaded entry.
  251. ModuleLoadEntry* pLoadedModuleEntry = findModuleLoaded( pLoadReadyModuleDefinition->getModuleId() );
  252. // Did we find a loaded entry?
  253. if ( pLoadedModuleEntry != NULL )
  254. {
  255. // Yes, so is it the one we need to load?
  256. if ( pLoadedModuleEntry->mpModuleDefinition != pLoadReadyModuleDefinition )
  257. {
  258. // Yes, so warn.
  259. 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'.",
  260. moduleGroup, pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId(), pLoadedModuleEntry->mpModuleDefinition->getVersionId() );
  261. return false;
  262. }
  263. }
  264. }
  265. // Info.
  266. if ( mEchoInfo )
  267. {
  268. // Info.
  269. Con::printf( "Module Manager: Group '%s' and its dependencies is comprised of the following '%d' module(s):", moduleGroup, moduleReadyQueue.size() );
  270. // Iterate the modules echoing them.
  271. for ( typeModuleLoadEntryVector::iterator moduleReadyItr = moduleReadyQueue.begin(); moduleReadyItr != moduleReadyQueue.end(); ++moduleReadyItr )
  272. {
  273. // Fetch the ready entry.
  274. ModuleDefinition* pModuleDefinition = moduleReadyItr->mpModuleDefinition;
  275. // Info.
  276. Con::printf( "> module Id '%s' at version Id '%d':", pModuleDefinition->getModuleId(), pModuleDefinition->getVersionId() );
  277. }
  278. }
  279. // Add module group.
  280. mGroupsLoaded.push_back( moduleGroup );
  281. // Reset modules loaded count.
  282. U32 modulesLoadedCount = 0;
  283. // Iterate the modules, executing their script files and call their create function.
  284. for ( typeModuleLoadEntryVector::iterator moduleReadyItr = moduleReadyQueue.begin(); moduleReadyItr != moduleReadyQueue.end(); ++moduleReadyItr )
  285. {
  286. // Fetch the ready entry.
  287. ModuleLoadEntry* pReadyEntry = moduleReadyItr;
  288. // Fetch load ready module definition.
  289. ModuleDefinition* pLoadReadyModuleDefinition = pReadyEntry->mpModuleDefinition;
  290. // Fetch any loaded entry for the module Id.
  291. ModuleLoadEntry* pLoadedEntry = findModuleLoaded( pLoadReadyModuleDefinition->getModuleId() );
  292. // Is the module already loaded.
  293. if ( pLoadedEntry != NULL )
  294. {
  295. // Yes, so increase load count.
  296. pLoadedEntry->mpModuleDefinition->increaseLoadCount();
  297. // Skip.
  298. continue;
  299. }
  300. // No, so info.
  301. if ( mEchoInfo )
  302. {
  303. Con::printSeparator();
  304. Con::printf( "Module Manager: Loading group '%s' : module Id '%s' at version Id '%d' in group '%s' using the script file '%s'.",
  305. moduleGroup, pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId(), pLoadReadyModuleDefinition->getModuleGroup(), pLoadReadyModuleDefinition->getModuleScriptFilePath() );
  306. }
  307. // Is the module deprecated?
  308. if ( pLoadReadyModuleDefinition->getDeprecated() )
  309. {
  310. // Yes, so warn.
  311. Con::warnf( "Module Manager: Caution: module Id '%s' at version Id '%d' in group '%s' is deprecated. You should use a newer version!",
  312. pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId(), pLoadReadyModuleDefinition->getModuleGroup() );
  313. }
  314. // Add the path expando for module.
  315. Con::addPathExpando( pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getModulePath() );
  316. // Create a scope set.
  317. SimSet* pScopeSet = new SimSet;
  318. pScopeSet->registerObject( pLoadReadyModuleDefinition->getModuleId() );
  319. pReadyEntry->mpModuleDefinition->mScopeSet = pScopeSet->getId();
  320. // Increase load count.
  321. pReadyEntry->mpModuleDefinition->increaseLoadCount();
  322. // Queue module loaded.
  323. mModulesLoaded.push_back( *pReadyEntry );
  324. // Bump modules loaded count.
  325. modulesLoadedCount++;
  326. // Raise notifications.
  327. raiseModulePreLoadNotifications( pLoadReadyModuleDefinition );
  328. // Do we have a script file-path specified?
  329. if ( pLoadReadyModuleDefinition->getModuleScriptFilePath() != StringTable->EmptyString() )
  330. {
  331. // Yes, so execute the script file.
  332. const bool scriptFileExecuted = dAtob( Con::executef("exec", pLoadReadyModuleDefinition->getModuleScriptFilePath() ) );
  333. // Did we execute the script file?
  334. if ( scriptFileExecuted )
  335. {
  336. // Yes, so is the create method available?
  337. if ( pScopeSet->isMethod( pLoadReadyModuleDefinition->getCreateFunction() ) )
  338. {
  339. // Yes, so call the create method.
  340. //But first, check if we're overriding objects, and if so, set our console var to make that happen while we exec our create function
  341. if (pLoadReadyModuleDefinition->getOverrideExistingObjects())
  342. {
  343. String redefineBehaviorPrev = Con::getVariable("$Con::redefineBehavior");
  344. Con::setVariable("$Con::redefineBehavior", "replaceExisting");
  345. Con::executef(pScopeSet, pLoadReadyModuleDefinition->getCreateFunction());
  346. //And now that we've executed, switch back to the prior behavior
  347. Con::setVariable("$Con::redefineBehavior", redefineBehaviorPrev.c_str());
  348. }
  349. else
  350. {
  351. //Nothing to do, just run the create function
  352. Con::executef(pScopeSet, pLoadReadyModuleDefinition->getCreateFunction());
  353. }
  354. }
  355. }
  356. else
  357. {
  358. // No, so warn.
  359. 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.",
  360. moduleGroup, pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId(), pLoadReadyModuleDefinition->getModuleScriptFilePath() );
  361. }
  362. }
  363. else
  364. {
  365. // Is the create method available?
  366. if (pScopeSet->isMethod(pLoadReadyModuleDefinition->getCreateFunction()))
  367. {
  368. // Yes, so call the create method.
  369. Con::executef(pScopeSet, pLoadReadyModuleDefinition->getCreateFunction());
  370. }
  371. }
  372. // Raise notifications.
  373. raiseModulePostLoadNotifications( pLoadReadyModuleDefinition );
  374. }
  375. // Info.
  376. if ( mEchoInfo )
  377. {
  378. Con::printSeparator();
  379. Con::printf( "Module Manager: Finish loading '%d' module(s) for group '%s'.", modulesLoadedCount, moduleGroup );
  380. Con::printSeparator();
  381. }
  382. return true;
  383. }
  384. //-----------------------------------------------------------------------------
  385. bool ModuleManager::unloadModuleGroup( const char* pModuleGroup )
  386. {
  387. // Lock database.
  388. LockDatabase( this );
  389. // Sanity!
  390. AssertFatal( pModuleGroup != NULL, "Cannot unload module group with NULL group name." );
  391. typeModuleLoadEntryVector moduleResolvingQueue;
  392. typeModuleLoadEntryVector moduleReadyQueue;
  393. // Fetch module group.
  394. StringTableEntry moduleGroup = StringTable->insert( pModuleGroup );
  395. // Info.
  396. if ( mEchoInfo )
  397. {
  398. Con::printSeparator();
  399. Con::printf( "Module Manager: Unloading group '%s':" , moduleGroup );
  400. }
  401. // Find the group loaded iterator.
  402. typeGroupVector::iterator groupLoadedItr = findGroupLoaded( moduleGroup );
  403. // Is the module group already unloaded?
  404. if ( groupLoadedItr == NULL )
  405. {
  406. // No, so warn.
  407. Con::warnf( "Module Manager: Cannot unload group '%s' as it is not loaded.", moduleGroup );
  408. return false;
  409. }
  410. // Find module group.
  411. typeGroupModuleHash::iterator moduleGroupItr = mGroupModules.find( moduleGroup );
  412. // Did we find the module group?
  413. if ( moduleGroupItr == mGroupModules.end() )
  414. {
  415. // No, so info.
  416. if ( mEchoInfo )
  417. {
  418. Con::printf( "Module Manager: No modules found for module group '%s'.", moduleGroup );
  419. return true;
  420. }
  421. }
  422. // Yes, so fetch the module Ids.
  423. typeModuleIdVector* pModuleIds = moduleGroupItr->value;
  424. // Iterate module groups.
  425. for( typeModuleIdVector::iterator moduleIdItr = pModuleIds->begin(); moduleIdItr != pModuleIds->end(); ++moduleIdItr )
  426. {
  427. // Fetch module Id.
  428. StringTableEntry moduleId = *moduleIdItr;
  429. // Finish if we could not resolve the dependencies for module Id (of any version Id).
  430. if ( !resolveModuleDependencies( moduleId, 0, moduleGroup, false, moduleResolvingQueue, moduleReadyQueue ) )
  431. return false;
  432. }
  433. // Check the modules we want to load to ensure that we do not have incompatible modules loaded already.
  434. for ( typeModuleLoadEntryVector::iterator moduleReadyItr = moduleReadyQueue.begin(); moduleReadyItr != moduleReadyQueue.end(); ++moduleReadyItr )
  435. {
  436. // Fetch load ready module definition.
  437. ModuleDefinition* pLoadReadyModuleDefinition = moduleReadyItr->mpModuleDefinition;;
  438. // Fetch the module Id loaded entry.
  439. ModuleLoadEntry* pLoadedModuleEntry = findModuleLoaded( pLoadReadyModuleDefinition->getModuleId() );
  440. // Did we find a loaded entry?
  441. if ( pLoadedModuleEntry != NULL )
  442. {
  443. // Yes, so is it the one we need to load?
  444. if ( pLoadedModuleEntry->mpModuleDefinition != pLoadReadyModuleDefinition )
  445. {
  446. // Yes, so warn.
  447. 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'.",
  448. moduleGroup, pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId(), pLoadedModuleEntry->mpModuleDefinition->getVersionId() );
  449. return false;
  450. }
  451. }
  452. }
  453. // Remove module group.
  454. mGroupsLoaded.erase_fast( groupLoadedItr );
  455. // Reset modules unloaded count.
  456. U32 modulesUnloadedCount = 0;
  457. // Iterate the modules in reverse order calling their destroy function.
  458. for ( typeModuleLoadEntryVector::iterator moduleReadyItr = moduleReadyQueue.end()-1; moduleReadyItr >= moduleReadyQueue.begin(); --moduleReadyItr )
  459. {
  460. // Fetch the ready entry.
  461. ModuleLoadEntry* pReadyEntry = moduleReadyItr;
  462. // Fetch load ready module definition.
  463. ModuleDefinition* pLoadReadyModuleDefinition = pReadyEntry->mpModuleDefinition;;
  464. // Fetch any loaded entry for the module Id.
  465. ModuleLoadEntry* pLoadedEntry = findModuleLoaded( pLoadReadyModuleDefinition->getModuleId() );
  466. // Is the module loaded.
  467. if ( pLoadedEntry == NULL )
  468. {
  469. // No, so warn.
  470. if ( mEchoInfo )
  471. {
  472. Con::printf( "Module Manager: Unloading group '%s' but could not unload module Id '%s' at version Id '%d'.",
  473. moduleGroup, pLoadedEntry->mpModuleDefinition->getModuleId(), pLoadedEntry->mpModuleDefinition->getVersionId() );
  474. }
  475. // Skip.
  476. continue;
  477. }
  478. // Reduce load count.
  479. pLoadedEntry->mpModuleDefinition->reduceLoadCount();
  480. // Sanity!
  481. AssertFatal( pLoadedEntry->mpModuleDefinition->getLoadCount() >= 0, "ModuleManager::unloadModuleGroup() - Encountered an invalid load count." );
  482. // Do we need to unload?
  483. if ( pLoadedEntry->mpModuleDefinition->getLoadCount() == 0 )
  484. {
  485. // Yes, so info.
  486. if ( mEchoInfo )
  487. {
  488. Con::printSeparator();
  489. Con::printf( "Module Manager: Unload group '%s' with module Id '%s' at version Id '%d' in group '%s'.",
  490. moduleGroup, pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId(), pLoadReadyModuleDefinition->getModuleGroup() );
  491. }
  492. // Raise notifications.
  493. raiseModulePreUnloadNotifications( pLoadReadyModuleDefinition );
  494. // Fetch the module Id loaded entry.
  495. typeModuleLoadEntryVector::iterator moduleLoadedItr = findModuleLoaded( pLoadReadyModuleDefinition->getModuleId() );
  496. // Sanity!
  497. AssertFatal( moduleLoadedItr != NULL, "ModuleManager::unloadModuleGroup() - Cannot find module to unload it." );
  498. // Dequeue module loaded.
  499. mModulesLoaded.erase_fast( moduleLoadedItr );
  500. // Fetch scope set.
  501. SimSet* pScopeSet = dynamic_cast<SimSet*>(Sim::findObject(pLoadReadyModuleDefinition->mScopeSet));
  502. // Is the destroy method available?
  503. if ( pScopeSet->isMethod( pLoadReadyModuleDefinition->getDestroyFunction() ) )
  504. {
  505. // Yes, so call the destroy method.
  506. Con::executef( pScopeSet, pLoadReadyModuleDefinition->getDestroyFunction() );
  507. }
  508. // Remove scope set.
  509. pScopeSet->deleteAllObjects();
  510. pScopeSet->unregisterObject();
  511. pLoadReadyModuleDefinition->mScopeSet = 0;
  512. // Remove path expando for module.
  513. Con::removePathExpando( pLoadReadyModuleDefinition->getModuleId() );
  514. // Bump modules unloaded count.
  515. modulesUnloadedCount++;
  516. // Raise notifications.
  517. raiseModulePostUnloadNotifications( pLoadReadyModuleDefinition );
  518. }
  519. }
  520. // Info.
  521. if ( mEchoInfo )
  522. {
  523. Con::printSeparator();
  524. Con::printf( "Module Manager: Finish unloading '%d' module(s) for group '%s'.", modulesUnloadedCount, moduleGroup );
  525. Con::printSeparator();
  526. }
  527. return true;
  528. }
  529. //-----------------------------------------------------------------------------
  530. bool ModuleManager::loadModuleExplicit( const char* pModuleId, const U32 versionId )
  531. {
  532. // Lock database.
  533. LockDatabase( this );
  534. // Sanity!
  535. AssertFatal( pModuleId != NULL, "Cannot load explicit module Id with NULL module Id." );
  536. typeModuleLoadEntryVector moduleResolvingQueue;
  537. typeModuleLoadEntryVector moduleReadyQueue;
  538. // Fetch module Id.
  539. StringTableEntry moduleId = StringTable->insert( pModuleId );
  540. // Fetch modules definitions.
  541. ModuleDefinitionEntry* pDefinitions = findModuleId( moduleId );
  542. // Did we find the module Id?
  543. if ( pDefinitions == NULL )
  544. {
  545. // No, so warn.
  546. Con::warnf( "Module Manager: Cannot load explicit module Id '%s' as it does not exist.", moduleId );
  547. return false;
  548. }
  549. // Fetch module group.
  550. StringTableEntry moduleGroup = pDefinitions->mModuleGroup;
  551. // Info.
  552. if ( mEchoInfo )
  553. {
  554. Con::printSeparator();
  555. Con::printf( "Module Manager: Loading explicit module Id '%s' at version Id '%d':", moduleId, versionId );
  556. }
  557. // Finish if we could not resolve the dependencies for module Id (of any version Id).
  558. if ( !resolveModuleDependencies( moduleId, versionId, moduleGroup, false, moduleResolvingQueue, moduleReadyQueue ) )
  559. return false;
  560. // Check the modules we want to load to ensure that we do not have incompatible modules loaded already.
  561. for ( typeModuleLoadEntryVector::iterator moduleReadyItr = moduleReadyQueue.begin(); moduleReadyItr != moduleReadyQueue.end(); ++moduleReadyItr )
  562. {
  563. // Fetch load ready module definition.
  564. ModuleDefinition* pLoadReadyModuleDefinition = moduleReadyItr->mpModuleDefinition;
  565. // Fetch the module Id loaded entry.
  566. ModuleLoadEntry* pLoadedModuleEntry = findModuleLoaded( pLoadReadyModuleDefinition->getModuleId() );
  567. // Did we find a loaded entry?
  568. if ( pLoadedModuleEntry != NULL )
  569. {
  570. // Yes, so is it the one we need to load?
  571. if ( pLoadedModuleEntry->mpModuleDefinition != pLoadReadyModuleDefinition )
  572. {
  573. // Yes, so warn.
  574. 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'.",
  575. pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId(), pLoadedModuleEntry->mpModuleDefinition->getVersionId() );
  576. return false;
  577. }
  578. }
  579. }
  580. // Info.
  581. if ( mEchoInfo )
  582. {
  583. // Info.
  584. 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() );
  585. // Iterate the modules echoing them.
  586. for ( typeModuleLoadEntryVector::iterator moduleReadyItr = moduleReadyQueue.begin(); moduleReadyItr != moduleReadyQueue.end(); ++moduleReadyItr )
  587. {
  588. // Fetch the ready entry.
  589. ModuleDefinition* pModuleDefinition = moduleReadyItr->mpModuleDefinition;
  590. // Info.
  591. Con::printf( "> module Id '%s' at version Id '%d'", pModuleDefinition->getModuleId(), pModuleDefinition->getVersionId() );
  592. }
  593. }
  594. // Reset modules loaded count.
  595. U32 modulesLoadedCount = 0;
  596. // Iterate the modules, executing their script files and call their create function.
  597. for ( typeModuleLoadEntryVector::iterator moduleReadyItr = moduleReadyQueue.begin(); moduleReadyItr != moduleReadyQueue.end(); ++moduleReadyItr )
  598. {
  599. // Fetch the ready entry.
  600. ModuleLoadEntry* pReadyEntry = moduleReadyItr;
  601. // Fetch load ready module definition.
  602. ModuleDefinition* pLoadReadyModuleDefinition = pReadyEntry->mpModuleDefinition;
  603. // Fetch any loaded entry for the module Id.
  604. ModuleLoadEntry* pLoadedEntry = findModuleLoaded( pLoadReadyModuleDefinition->getModuleId() );
  605. // Is the module already loaded.
  606. if ( pLoadedEntry != NULL )
  607. {
  608. // Yes, so increase load count.
  609. pLoadedEntry->mpModuleDefinition->increaseLoadCount();
  610. // Skip.
  611. continue;
  612. }
  613. // No, so info.
  614. if ( mEchoInfo )
  615. {
  616. Con::printSeparator();
  617. Con::printf( "Module Manager: Loading explicit module Id '%s' at version Id '%d' using the script file '%s'.",
  618. pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId(), pLoadReadyModuleDefinition->getModuleScriptFilePath() );
  619. }
  620. // Is the module deprecated?
  621. if ( pLoadReadyModuleDefinition->getDeprecated() )
  622. {
  623. // Yes, so warn.
  624. Con::warnf( "Module Manager: Caution: module Id '%s' at version Id '%d' is deprecated, You should use a newer version!",
  625. pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId() );
  626. }
  627. // Add the path expando for module.
  628. Con::addPathExpando( pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getModulePath() );
  629. // Create a scope set.
  630. SimSet* pScopeSet = new SimSet;
  631. pScopeSet->registerObject( pLoadReadyModuleDefinition->getModuleId() );
  632. pReadyEntry->mpModuleDefinition->mScopeSet = pScopeSet->getId();
  633. // Increase load count.
  634. pReadyEntry->mpModuleDefinition->increaseLoadCount();
  635. // Queue module loaded.
  636. mModulesLoaded.push_back( *pReadyEntry );
  637. // Bump modules loaded count.
  638. modulesLoadedCount++;
  639. // Raise notifications.
  640. raiseModulePreLoadNotifications( pLoadReadyModuleDefinition );
  641. // Do we have a script file-path specified?
  642. if ( pLoadReadyModuleDefinition->getModuleScriptFilePath() != StringTable->EmptyString() )
  643. {
  644. // Yes, so execute the script file.
  645. const bool scriptFileExecuted = dAtob( Con::executef("exec", pLoadReadyModuleDefinition->getModuleScriptFilePath() ) );
  646. // Did we execute the script file?
  647. if ( scriptFileExecuted )
  648. {
  649. // Yes, so is the create method available?
  650. if ( pScopeSet->isMethod( pLoadReadyModuleDefinition->getCreateFunction() ) )
  651. {
  652. // Yes, so call the create method.
  653. Con::executef( pScopeSet, pLoadReadyModuleDefinition->getCreateFunction() );
  654. }
  655. }
  656. else
  657. {
  658. // No, so warn.
  659. Con::errorf( "Module Manager: Cannot load explicit module Id '%s' at version Id '%d' as it failed to have the script file '%s' loaded.",
  660. pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId(), pLoadReadyModuleDefinition->getModuleScriptFilePath() );
  661. }
  662. }
  663. // Raise notifications.
  664. raiseModulePostLoadNotifications( pLoadReadyModuleDefinition );
  665. }
  666. // Info.
  667. if ( mEchoInfo )
  668. {
  669. Con::printSeparator();
  670. Con::printf( "Module Manager: Finish loading '%d' explicit module(s).", modulesLoadedCount );
  671. Con::printSeparator();
  672. }
  673. return true;
  674. }
  675. //-----------------------------------------------------------------------------
  676. bool ModuleManager::unloadModuleExplicit( const char* pModuleId )
  677. {
  678. // Lock database.
  679. LockDatabase( this );
  680. // Sanity!
  681. AssertFatal( pModuleId != NULL, "Cannot unload explicit module Id with NULL module Id." );
  682. typeModuleLoadEntryVector moduleResolvingQueue;
  683. typeModuleLoadEntryVector moduleReadyQueue;
  684. // Fetch module Id.
  685. StringTableEntry moduleId = StringTable->insert( pModuleId );
  686. // Fetch modules definitions.
  687. ModuleDefinitionEntry* pDefinitions = findModuleId( moduleId );
  688. // Did we find the module Id?
  689. if ( pDefinitions == NULL )
  690. {
  691. // No, so warn.
  692. Con::warnf( "Module Manager: Cannot unload explicit module Id '%s' as it does not exist.", moduleId );
  693. return false;
  694. }
  695. // Find if the module is actually loaded.
  696. ModuleDefinition* pLoadedModule = findLoadedModule( moduleId );
  697. // Is the module loaded?
  698. if ( pLoadedModule == NULL )
  699. {
  700. // No, so warn.
  701. Con::warnf( "Module Manager: Cannot unload explicit module Id '%s' as it is not loaded.", moduleId );
  702. return false;
  703. }
  704. // Fetch module group.
  705. StringTableEntry moduleGroup = pDefinitions->mModuleGroup;
  706. // Info.
  707. if ( mEchoInfo )
  708. {
  709. Con::printSeparator();
  710. Con::printf( "Module Manager: Unloading explicit module Id '%s':" , moduleId );
  711. }
  712. // Finish if we could not resolve the dependencies for module Id (of any version Id).
  713. if ( !resolveModuleDependencies( moduleId, pLoadedModule->getVersionId(), moduleGroup, false, moduleResolvingQueue, moduleReadyQueue ) )
  714. return false;
  715. // Check the modules we want to unload to ensure that we do not have incompatible modules loaded already.
  716. for ( typeModuleLoadEntryVector::iterator moduleReadyItr = moduleReadyQueue.begin(); moduleReadyItr != moduleReadyQueue.end(); ++moduleReadyItr )
  717. {
  718. // Fetch load ready module definition.
  719. ModuleDefinition* pLoadReadyModuleDefinition = moduleReadyItr->mpModuleDefinition;;
  720. // Fetch the module Id loaded entry.
  721. ModuleLoadEntry* pLoadedModuleEntry = findModuleLoaded( pLoadReadyModuleDefinition->getModuleId() );
  722. // Did we find a loaded entry?
  723. if ( pLoadedModuleEntry != NULL )
  724. {
  725. // Yes, so is it the one we need to load?
  726. if ( pLoadedModuleEntry->mpModuleDefinition != pLoadReadyModuleDefinition )
  727. {
  728. // Yes, so warn.
  729. 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'.",
  730. pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId(), pLoadedModuleEntry->mpModuleDefinition->getVersionId() );
  731. return false;
  732. }
  733. }
  734. }
  735. // Reset modules unloaded count.
  736. U32 modulesUnloadedCount = 0;
  737. // Iterate the modules in reverse order calling their destroy function.
  738. for ( typeModuleLoadEntryVector::iterator moduleReadyItr = moduleReadyQueue.end()-1; moduleReadyItr >= moduleReadyQueue.begin(); --moduleReadyItr )
  739. {
  740. // Fetch the ready entry.
  741. ModuleLoadEntry* pReadyEntry = moduleReadyItr;
  742. // Fetch load ready module definition.
  743. ModuleDefinition* pLoadReadyModuleDefinition = pReadyEntry->mpModuleDefinition;;
  744. // Fetch any loaded entry for the module Id.
  745. ModuleLoadEntry* pLoadedEntry = findModuleLoaded( pLoadReadyModuleDefinition->getModuleId() );
  746. // Is the module loaded.
  747. if ( pLoadedEntry == NULL )
  748. {
  749. // No, so warn.
  750. if ( mEchoInfo )
  751. {
  752. Con::printf( "Module Manager: Unloading explicit module Id '%s' at version Id '%d' but ignoring as it is not loaded.",
  753. pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId() );
  754. }
  755. // Skip.
  756. continue;
  757. }
  758. // Reduce load count.
  759. pLoadedEntry->mpModuleDefinition->reduceLoadCount();
  760. // Sanity!
  761. AssertFatal( pLoadedEntry->mpModuleDefinition->getLoadCount() >= 0, "ModuleManager::unloadModuleGroup() - Encountered an invalid load count." );
  762. // Do we need to unload?
  763. if ( pLoadedEntry->mpModuleDefinition->getLoadCount() == 0 )
  764. {
  765. // Yes, so info.
  766. if ( mEchoInfo )
  767. {
  768. Con::printSeparator();
  769. Con::printf( "Module Manager: Unload explicit module Id '%s' at version Id '%d'.",
  770. pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId() );
  771. }
  772. // Raise notifications.
  773. raiseModulePreUnloadNotifications( pLoadReadyModuleDefinition );
  774. // Fetch the module Id loaded entry.
  775. typeModuleLoadEntryVector::iterator moduleLoadedItr = findModuleLoaded( pLoadReadyModuleDefinition->getModuleId() );
  776. // Sanity!
  777. AssertFatal( moduleLoadedItr != NULL, "ModuleManager::unloadModuleExplicit() - Cannot find module to unload it." );
  778. // Dequeue module loaded.
  779. mModulesLoaded.erase_fast( moduleLoadedItr );
  780. // Fetch scope set.
  781. SimSet* pScopeSet = dynamic_cast<SimSet*>(Sim::findObject(pLoadReadyModuleDefinition->mScopeSet));
  782. // Is the destroy method available?
  783. if ( pScopeSet->isMethod( pLoadReadyModuleDefinition->getDestroyFunction() ) )
  784. {
  785. // Yes, so call the destroy method.
  786. Con::executef( pScopeSet, pLoadReadyModuleDefinition->getDestroyFunction() );
  787. }
  788. // Remove scope set.
  789. pScopeSet->deleteAllObjects();
  790. pScopeSet->unregisterObject();
  791. pLoadReadyModuleDefinition->mScopeSet = 0;
  792. // Remove path expando for module.
  793. Con::removePathExpando( pLoadReadyModuleDefinition->getModuleId() );
  794. // Bump modules unloaded count.
  795. modulesUnloadedCount++;
  796. // Raise notifications.
  797. raiseModulePostUnloadNotifications( pLoadReadyModuleDefinition );
  798. }
  799. }
  800. // Info.
  801. if ( mEchoInfo )
  802. {
  803. Con::printSeparator();
  804. Con::printf( "Module Manager: Finish unloading '%d' explicit module(s).", modulesUnloadedCount );
  805. Con::printSeparator();
  806. }
  807. return true;
  808. }
  809. //-----------------------------------------------------------------------------
  810. ModuleDefinition* ModuleManager::findModule( const char* pModuleId, const U32 versionId )
  811. {
  812. // Sanity!
  813. AssertFatal( pModuleId != NULL, "Cannot find module with NULL module Id." );
  814. // Find module definition.
  815. ModuleDefinitionEntry::iterator moduleItr = findModuleDefinition( StringTable->insert( pModuleId ), versionId );
  816. // Finish if module was not found.
  817. if ( moduleItr == NULL )
  818. return NULL;
  819. return *moduleItr;
  820. }
  821. //-----------------------------------------------------------------------------
  822. ModuleDefinition* ModuleManager::findLoadedModule( const char* pModuleId )
  823. {
  824. // Sanity!
  825. AssertFatal( pModuleId != NULL, "Cannot find module with NULL module Id." );
  826. // Fetch module Id.
  827. StringTableEntry moduleId = StringTable->insert( pModuleId );
  828. // Iterate loaded modules.
  829. for ( typeModuleLoadEntryVector::iterator loadedModuleItr = mModulesLoaded.begin(); loadedModuleItr != mModulesLoaded.end(); ++loadedModuleItr )
  830. {
  831. // Skip if not the module.
  832. if ( loadedModuleItr->mpModuleDefinition->getModuleId() != moduleId )
  833. continue;
  834. return loadedModuleItr->mpModuleDefinition;
  835. }
  836. return NULL;
  837. }
  838. //-----------------------------------------------------------------------------
  839. void ModuleManager::findModules( const bool loadedOnly, typeConstModuleDefinitionVector& moduleDefinitions )
  840. {
  841. // Iterate module Ids.
  842. for( typeModuleIdDatabaseHash::iterator moduleIdItr = mModuleIdDatabase.begin(); moduleIdItr != mModuleIdDatabase.end(); ++moduleIdItr )
  843. {
  844. // Fetch module definition entry.
  845. ModuleDefinitionEntry* pModuleDefinitionEntry = moduleIdItr->value;
  846. // Iterate module definitions.
  847. for ( typeModuleDefinitionVector::iterator moduleDefinitionItr = pModuleDefinitionEntry->begin(); moduleDefinitionItr != pModuleDefinitionEntry->end(); ++moduleDefinitionItr )
  848. {
  849. // Fetch module definition.
  850. ModuleDefinition* pModuleDefinition = *moduleDefinitionItr;
  851. // Are we searching for loaded modules only?
  852. if ( loadedOnly )
  853. {
  854. // Yes, so skip if the module is not loaded.
  855. if ( pModuleDefinition->getLoadCount() == 0 )
  856. continue;
  857. // Use module definition.
  858. moduleDefinitions.push_back( pModuleDefinition );
  859. // Finish iterating module definitions as only a single module in this entry can be loaded concurrently.
  860. break;
  861. }
  862. // use module definition.
  863. moduleDefinitions.push_back( pModuleDefinition );
  864. }
  865. }
  866. }
  867. //-----------------------------------------------------------------------------
  868. void ModuleManager::findModuleTypes( const char* pModuleType, const bool loadedOnly, typeConstModuleDefinitionVector& moduleDefinitions )
  869. {
  870. // Fetch module type.
  871. StringTableEntry moduleType = StringTable->insert( pModuleType );
  872. // Iterate module Ids.
  873. for( typeModuleIdDatabaseHash::iterator moduleIdItr = mModuleIdDatabase.begin(); moduleIdItr != mModuleIdDatabase.end(); ++moduleIdItr )
  874. {
  875. // Fetch module definition entry.
  876. ModuleDefinitionEntry* pModuleDefinitionEntry = moduleIdItr->value;
  877. // Skip if note the module type we're searching for.
  878. if ( pModuleDefinitionEntry->mModuleType != moduleType )
  879. continue;
  880. // Iterate module definitions.
  881. for ( typeModuleDefinitionVector::iterator moduleDefinitionItr = pModuleDefinitionEntry->begin(); moduleDefinitionItr != pModuleDefinitionEntry->end(); ++moduleDefinitionItr )
  882. {
  883. // Fetch module definition.
  884. ModuleDefinition* pModuleDefinition = *moduleDefinitionItr;
  885. // Are we searching for loaded modules only?
  886. if ( loadedOnly )
  887. {
  888. // Yes, so skip if the module is not loaded.
  889. if ( pModuleDefinition->getLoadCount() == 0 )
  890. continue;
  891. // Use module definition.
  892. moduleDefinitions.push_back( pModuleDefinition );
  893. // Finish iterating module definitions as only a single module in this entry can be loaded concurrently.
  894. break;
  895. }
  896. // use module definition.
  897. moduleDefinitions.push_back( pModuleDefinition );
  898. }
  899. }
  900. }
  901. //-----------------------------------------------------------------------------
  902. StringTableEntry ModuleManager::copyModule( ModuleDefinition* pSourceModuleDefinition, const char* pTargetModuleId, const char* pTargetPath, const bool useVersionPathing )
  903. {
  904. // Sanity!
  905. AssertFatal( pSourceModuleDefinition != NULL, "Cannot copy module using a NULL source module definition." );
  906. AssertFatal( pTargetModuleId != NULL, "Cannot copy module using a NULL target module Id." );
  907. AssertFatal( pTargetPath != NULL, "Cannot copy module using a NULL target path." );
  908. // Fetch the source module Id.
  909. StringTableEntry sourceModuleId = pSourceModuleDefinition->getModuleId();
  910. // Is the source module definition registered with this module manager?
  911. if ( pSourceModuleDefinition->getModuleManager() != this )
  912. {
  913. // No, so warn.
  914. Con::warnf("Module Manager: Cannot copy module Id '%s' as it is not registered with this module manager.", sourceModuleId );
  915. return StringTable->EmptyString();
  916. }
  917. // Fetch the target module Id.
  918. StringTableEntry targetModuleId = StringTable->insert( pTargetModuleId );
  919. // Extend moduleId/VersionId pathing.
  920. char versionPathBuffer[1024];
  921. // Are we using version pathing?
  922. if ( useVersionPathing )
  923. {
  924. // Yes, so format it.
  925. dSprintf( versionPathBuffer, sizeof(versionPathBuffer), "%s/%s/%d",
  926. pTargetPath, targetModuleId, pSourceModuleDefinition->getVersionId() );
  927. }
  928. else
  929. {
  930. // No, so a straight copy.
  931. dSprintf( versionPathBuffer, sizeof(versionPathBuffer), "%s", pTargetPath );
  932. }
  933. // Expand the path.
  934. char targetPathBuffer[1024];
  935. Con::expandPath( targetPathBuffer, sizeof(targetPathBuffer), versionPathBuffer );
  936. pTargetPath = targetPathBuffer;
  937. // Info.
  938. if ( mEchoInfo )
  939. {
  940. Con::printf( "Module Manager: Started copying module Id '%s' to target directory '%s'.", sourceModuleId, pTargetPath );
  941. }
  942. // Is the target folder a directory?
  943. if ( !Platform::isDirectory( pTargetPath ) )
  944. {
  945. // 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.
  946. char createDirectoryBuffer[1024];
  947. Con::expandPath( createDirectoryBuffer, sizeof(createDirectoryBuffer), pTargetPath, NULL, true );
  948. // No, so can we create it?
  949. if ( !Platform::createPath( createDirectoryBuffer ) )
  950. {
  951. // No, so warn.
  952. Con::warnf("Module Manager: Cannot copy module Id '%s' using target directory '%s' as directory was not found and could not be created.",
  953. sourceModuleId, pTargetPath );
  954. return StringTable->EmptyString();
  955. }
  956. }
  957. // Copy the source module to the target folder.
  958. if ( !dPathCopy( pSourceModuleDefinition->getModulePath(), pTargetPath, false ) )
  959. {
  960. // Warn.
  961. Con::warnf("Module Manager: Cannot copy module Id '%s' using target directory '%s' as directory copy failed.",
  962. sourceModuleId, pTargetPath );
  963. return StringTable->EmptyString();
  964. }
  965. // Format the new source module definition file-path.
  966. char newModuleDefinitionSourceFileBuffer[1024];
  967. dSprintf( newModuleDefinitionSourceFileBuffer, sizeof(newModuleDefinitionSourceFileBuffer), "%s/%s", pTargetPath, pSourceModuleDefinition->getModuleFile() );
  968. // Finish if source/target module Ids are identical.
  969. if ( sourceModuleId == targetModuleId )
  970. return StringTable->insert( newModuleDefinitionSourceFileBuffer );
  971. // Format the new target module definition file-path.
  972. char newModuleDefinitionTargetFileBuffer[1024];
  973. dSprintf( newModuleDefinitionTargetFileBuffer, sizeof(newModuleDefinitionTargetFileBuffer), "%s/%s.%s", pTargetPath, targetModuleId, MODULE_MANAGER_MODULE_DEFINITION_EXTENSION );
  974. // Rename the module definition.
  975. if ( !dFileRename( newModuleDefinitionSourceFileBuffer, newModuleDefinitionTargetFileBuffer ) )
  976. {
  977. // Warn.
  978. Con::warnf("Module Manager: Cannot copy module Id '%s' using target directory '%s' as renaming the module from '%s' to '%s' failed.",
  979. sourceModuleId, pTargetPath, newModuleDefinitionSourceFileBuffer, newModuleDefinitionTargetFileBuffer );
  980. return StringTable->EmptyString();
  981. }
  982. Vector<StringTableEntry> directories;
  983. // Find directories.
  984. if ( !Platform::dumpDirectories( pTargetPath, directories, -1 ) )
  985. {
  986. // Warn.
  987. Con::warnf("Module Manager: Cannot copy module Id '%s' using target directory '%s' as sub-folder scanning/renaming failed.",
  988. sourceModuleId, pTargetPath );
  989. return StringTable->EmptyString();
  990. }
  991. TamlModuleIdUpdateVisitor moduleIdUpdateVisitor;
  992. moduleIdUpdateVisitor.setModuleIdFrom( sourceModuleId );
  993. moduleIdUpdateVisitor.setModuleIdTo( targetModuleId );
  994. Vector<Platform::FileInfo> files;
  995. const char* pExtension = (const char*)"Taml";
  996. const U32 extensionLength = dStrlen(pExtension);
  997. // Iterate directories.
  998. for( Vector<StringTableEntry>::iterator basePathItr = directories.begin(); basePathItr != directories.end(); ++basePathItr )
  999. {
  1000. // Fetch base path.
  1001. StringTableEntry basePath = *basePathItr;
  1002. // Find files.
  1003. files.clear();
  1004. if ( !Platform::dumpPath( basePath, files, 0 ) )
  1005. {
  1006. // Warn.
  1007. Con::warnf("Module Manager: Cannot copy module Id '%s' using target directory '%s' as sub-folder scanning/renaming failed.",
  1008. sourceModuleId, pTargetPath );
  1009. return StringTable->EmptyString();
  1010. }
  1011. // Iterate files.
  1012. for ( Vector<Platform::FileInfo>::iterator fileItr = files.begin(); fileItr != files.end(); ++fileItr )
  1013. {
  1014. // Fetch file info.
  1015. Platform::FileInfo* pFileInfo = fileItr;
  1016. // Fetch filename.
  1017. const char* pFilename = pFileInfo->pFileName;
  1018. // Find filename length.
  1019. const U32 filenameLength = dStrlen( pFilename );
  1020. // Skip if extension is longer than filename.
  1021. if ( extensionLength >= filenameLength )
  1022. continue;
  1023. // Skip if extension not found.
  1024. if ( dStricmp( pFilename + filenameLength - extensionLength, pExtension ) != 0 )
  1025. continue;
  1026. char parseFileBuffer[1024];
  1027. dSprintf( parseFileBuffer, sizeof(parseFileBuffer), "%s/%s", pFileInfo->pFullPath, pFilename );
  1028. // Parse file.
  1029. if ( !mTaml.parse( parseFileBuffer, moduleIdUpdateVisitor ) )
  1030. {
  1031. // Warn.
  1032. Con::warnf("Module Manager: Failed to parse file '%s' whilst copying module Id '%s' using target directory '%s'.",
  1033. parseFileBuffer, sourceModuleId, pTargetPath );
  1034. return StringTable->EmptyString();
  1035. }
  1036. }
  1037. }
  1038. // Info.
  1039. if ( mEchoInfo )
  1040. {
  1041. Con::printf( "Module Manager: Finished copying module Id '%s' to target directory '%s'.", sourceModuleId, pTargetPath );
  1042. }
  1043. return StringTable->insert( newModuleDefinitionTargetFileBuffer );
  1044. }
  1045. //-----------------------------------------------------------------------------
  1046. bool ModuleManager::renameModule(ModuleDefinition* pSourceModuleDefinition, const char* pNewModuleName)
  1047. {
  1048. // Sanity!
  1049. AssertFatal(pSourceModuleDefinition != NULL, "Cannot copy module using a NULL source module definition.");
  1050. AssertFatal(pNewModuleName != NULL, "Cannot rename a module using a NULL module name.");
  1051. // Fetch the source module Id.
  1052. StringTableEntry sourceModuleId = pSourceModuleDefinition->getModuleId();
  1053. // Is the source module definition registered with this module manager?
  1054. if (pSourceModuleDefinition->getModuleManager() != this)
  1055. {
  1056. // No, so warn.
  1057. Con::warnf("Module Manager: Cannot rename module Id '%s' as it is not registered with this module manager.", sourceModuleId);
  1058. return StringTable->EmptyString();
  1059. }
  1060. TamlModuleIdUpdateVisitor moduleIdUpdateVisitor;
  1061. moduleIdUpdateVisitor.setModuleIdFrom(sourceModuleId);
  1062. moduleIdUpdateVisitor.setModuleIdTo(pNewModuleName);
  1063. Vector<Platform::FileInfo> files;
  1064. const char* pExtension = (const char*)"Taml";
  1065. const U32 extensionLength = dStrlen(pExtension);
  1066. Vector<StringTableEntry> directories;
  1067. StringTableEntry modulePath = pSourceModuleDefinition->getModulePath();
  1068. // Find directories.
  1069. if (!Platform::dumpDirectories(modulePath, directories, -1))
  1070. {
  1071. // Warn.
  1072. Con::warnf("Module Manager: Cannot rename module Id '%s' in directory '%s' as sub-folder scanning/renaming failed.",
  1073. sourceModuleId, modulePath);
  1074. return false;
  1075. }
  1076. // Iterate directories.
  1077. for (Vector<StringTableEntry>::iterator basePathItr = directories.begin(); basePathItr != directories.end(); ++basePathItr)
  1078. {
  1079. // Fetch base path.
  1080. StringTableEntry basePath = *basePathItr;
  1081. // Find files.
  1082. files.clear();
  1083. if (!Platform::dumpPath(basePath, files, 0))
  1084. {
  1085. // Warn.
  1086. Con::warnf("Module Manager: Cannot rename module Id '%s' in directory '%s' as sub-folder scanning/renaming failed.",
  1087. sourceModuleId, modulePath);
  1088. return false;
  1089. }
  1090. // Iterate files.
  1091. for (Vector<Platform::FileInfo>::iterator fileItr = files.begin(); fileItr != files.end(); ++fileItr)
  1092. {
  1093. // Fetch file info.
  1094. Platform::FileInfo* pFileInfo = fileItr;
  1095. // Fetch filename.
  1096. const char* pFilename = pFileInfo->pFileName;
  1097. // Find filename length.
  1098. const U32 filenameLength = dStrlen(pFilename);
  1099. // Skip if extension is longer than filename.
  1100. if (extensionLength >= filenameLength)
  1101. continue;
  1102. // Skip if extension not found.
  1103. if (dStricmp(pFilename + filenameLength - extensionLength, pExtension) != 0)
  1104. continue;
  1105. char parseFileBuffer[1024];
  1106. dSprintf(parseFileBuffer, sizeof(parseFileBuffer), "%s/%s", pFileInfo->pFullPath, pFilename);
  1107. // Parse file.
  1108. if (!mTaml.parse(parseFileBuffer, moduleIdUpdateVisitor))
  1109. {
  1110. // Warn.
  1111. Con::warnf("Module Manager: Failed to parse file '%s' whilst renaming module Id '%s' in directory '%s'.",
  1112. parseFileBuffer, sourceModuleId, modulePath);
  1113. return false;
  1114. }
  1115. }
  1116. }
  1117. // Info.
  1118. if (mEchoInfo)
  1119. {
  1120. Con::printf("Module Manager: Finished renaming module Id '%s' to '%s'.", sourceModuleId, pNewModuleName);
  1121. }
  1122. return true;
  1123. }
  1124. //-----------------------------------------------------------------------------
  1125. bool ModuleManager::synchronizeDependencies( ModuleDefinition* pRootModuleDefinition, const char* pTargetDependencyPath )
  1126. {
  1127. // Sanity!
  1128. AssertFatal( pRootModuleDefinition != NULL, "Cannot synchronize dependencies with NULL root module definition." );
  1129. AssertFatal( pTargetDependencyPath != NULL, "Cannot synchronize dependencies with NULL target dependency path." );
  1130. // Fetch the root module Id.
  1131. StringTableEntry rootModuleId = pRootModuleDefinition->getModuleId();
  1132. // Is the root module definition registered with this module manager?
  1133. if ( pRootModuleDefinition->getModuleManager() != this )
  1134. {
  1135. // No, so warn.
  1136. Con::warnf("Cannot synchronize dependencies for module Id '%s' as it is not registered with this module manager.", rootModuleId );
  1137. return false;
  1138. }
  1139. // Expand the path.
  1140. char targetPathBuffer[1024];
  1141. Con::expandPath( targetPathBuffer, sizeof(targetPathBuffer), pTargetDependencyPath );
  1142. pTargetDependencyPath = targetPathBuffer;
  1143. // Is the target dependency folder a directory?
  1144. if ( !Platform::isDirectory( pTargetDependencyPath ) )
  1145. {
  1146. // 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.
  1147. char createDirectoryBuffer[1024];
  1148. Con::expandPath( createDirectoryBuffer, sizeof(createDirectoryBuffer), pTargetDependencyPath, NULL, true );
  1149. // No, so can we create it?
  1150. if ( !Platform::createPath( createDirectoryBuffer ) )
  1151. {
  1152. // No, so warn.
  1153. Con::warnf("Cannot synchronize dependencies for module Id '%s' using target directory '%s' as directory was not found and could not be created.",
  1154. rootModuleId, pTargetDependencyPath );
  1155. return false;
  1156. }
  1157. }
  1158. typeModuleLoadEntryVector resolvingQueue;
  1159. typeModuleLoadEntryVector sourceModulesNeeded;
  1160. // Could we resolve source dependencies?
  1161. if ( !resolveModuleDependencies( rootModuleId, pRootModuleDefinition->getVersionId(), pRootModuleDefinition->getModuleGroup(), true, resolvingQueue, sourceModulesNeeded ) )
  1162. {
  1163. // No, so warn.
  1164. Con::warnf("Cannot synchronize dependencies for root module Id '%s' as its dependencies could not be resolved.", rootModuleId );
  1165. return false;
  1166. }
  1167. // Sanity!
  1168. AssertFatal( sourceModulesNeeded.size() > 0, "Cannot synchronize dependencies as no modules were returned." );
  1169. // Remove the root module definition.
  1170. sourceModulesNeeded.pop_back();
  1171. // Initialize the target module manager and scan the target folder for modules.
  1172. ModuleManager targetModuleManager;
  1173. targetModuleManager.mEnforceDependencies = true;
  1174. targetModuleManager.mEchoInfo = false;
  1175. targetModuleManager.scanModules( pTargetDependencyPath );
  1176. char targetFolderGenerateBuffer[1024];
  1177. // Iterate module definitions.
  1178. for ( typeModuleLoadEntryVector::iterator sourceModuleItr = sourceModulesNeeded.begin(); sourceModuleItr != sourceModulesNeeded.end(); ++sourceModuleItr )
  1179. {
  1180. // Fetch module definition.
  1181. ModuleDefinition* pSourceModuleDefinition = sourceModuleItr->mpModuleDefinition;
  1182. // Fetch the source module Id,
  1183. StringTableEntry sourceModuleId = pSourceModuleDefinition->getModuleId();
  1184. // Fetch the source module version Id.
  1185. const U32 sourceVersionId = pSourceModuleDefinition->getVersionId();
  1186. // Fetch the source module build Id.
  1187. const U32 sourceBuildId = pSourceModuleDefinition->getBuildId();
  1188. // Fetch module definition entry for this module Id in the target.
  1189. ModuleDefinitionEntry* pDefinitions = targetModuleManager.findModuleId( sourceModuleId );
  1190. // Is the module Id present in the target?
  1191. if ( pDefinitions == NULL )
  1192. {
  1193. // No, so format module Id folder path.
  1194. dSprintf( targetFolderGenerateBuffer, sizeof(targetFolderGenerateBuffer), "%s/%s/", pTargetDependencyPath, sourceModuleId );
  1195. // Create module Id folder.
  1196. if ( !Platform::createPath( targetFolderGenerateBuffer ) )
  1197. {
  1198. // Warn.
  1199. Con::warnf("Cannot synchronize dependencies for module Id '%s' as the target directory '%s' could not be created.", sourceModuleId, targetFolderGenerateBuffer );
  1200. return false;
  1201. }
  1202. }
  1203. else
  1204. {
  1205. // Yes, so fetch the module definition for this module Id and version Id in the target.
  1206. ModuleDefinitionEntry::iterator definitionItr = targetModuleManager.findModuleDefinition( sourceModuleId, sourceVersionId );
  1207. // Is the specific module definition present in the target?
  1208. if ( definitionItr != NULL )
  1209. {
  1210. // Yes, so fetch the module definition.
  1211. ModuleDefinition* pTargetModuleDefinition = *definitionItr;
  1212. // Fetch the target module build Id.
  1213. const U32 targetBuildId = pTargetModuleDefinition->getBuildId();
  1214. // Fetch the target module path.
  1215. StringTableEntry targetModulePath = pTargetModuleDefinition->getModulePath();
  1216. // Remove the target module definition from the database.
  1217. targetModuleManager.removeModuleDefinition( pTargetModuleDefinition );
  1218. // Skip if the target definition is the same build Id.
  1219. if ( targetBuildId == sourceBuildId )
  1220. continue;
  1221. // Delete the target module definition folder.
  1222. if ( !Platform::deleteDirectory( targetModulePath ) )
  1223. {
  1224. // Warn.
  1225. Con::warnf("Cannot synchronize dependencies for module Id '%s' at version Id '%d' as the old module at '%s' could not be deleted.",
  1226. sourceModuleId, sourceVersionId, targetModulePath );
  1227. return false;
  1228. }
  1229. }
  1230. }
  1231. // Format source module path.
  1232. char sourceFolderPath[1024];
  1233. Con::expandPath( sourceFolderPath, sizeof(sourceFolderPath), pSourceModuleDefinition->getModulePath() );
  1234. // Format target module path.
  1235. dSprintf( targetFolderGenerateBuffer, sizeof(targetFolderGenerateBuffer), "%s/%s/%d", pTargetDependencyPath, sourceModuleId, sourceVersionId );
  1236. // Copy the source module to the target folder.
  1237. if (!dPathCopy(sourceFolderPath, targetFolderGenerateBuffer, false))
  1238. {
  1239. // Warn.
  1240. Con::warnf("Cannot synchronize dependencies for module Id '%s' at version Id '%d' as the module could not be copied to '%s'.",
  1241. sourceModuleId, sourceVersionId, targetFolderGenerateBuffer );
  1242. return false;
  1243. }
  1244. }
  1245. // Find any target modules left, These are orphaned modules not depended upon by any other module.
  1246. typeConstModuleDefinitionVector orphanedTargetModules;
  1247. targetModuleManager.findModules( false, orphanedTargetModules );
  1248. // Iterate module definitions.
  1249. for ( typeConstModuleDefinitionVector::const_iterator moduleDefinitionItr = orphanedTargetModules.begin(); moduleDefinitionItr != orphanedTargetModules.end(); ++moduleDefinitionItr )
  1250. {
  1251. // Fetch orphaned module definition.
  1252. const ModuleDefinition* pOrphanedModuleDefinition = *moduleDefinitionItr;
  1253. // Delete the target module definition folder.
  1254. if ( !Platform::deleteDirectory( pOrphanedModuleDefinition->getModulePath() ) )
  1255. {
  1256. // Warn.
  1257. Con::warnf("Cannot delete orphaned module Id '%s' at version Id '%d' from '%s'.",
  1258. pOrphanedModuleDefinition->getModuleId(), pOrphanedModuleDefinition->getVersionId(), pOrphanedModuleDefinition->getModulePath() );
  1259. }
  1260. }
  1261. return true;
  1262. }
  1263. //-----------------------------------------------------------------------------
  1264. bool ModuleManager::canMergeModules( const char* pMergeSourcePath )
  1265. {
  1266. // Sanity!
  1267. AssertFatal( pMergeSourcePath != NULL, "Cannot check merge modules with NULL source path." );
  1268. // Expand the source path.
  1269. char sourcePathBuffer[1024];
  1270. Con::expandPath( sourcePathBuffer, sizeof(sourcePathBuffer), pMergeSourcePath );
  1271. pMergeSourcePath = sourcePathBuffer;
  1272. // Is the path a valid directory?
  1273. if ( !Platform::isDirectory( sourcePathBuffer ) )
  1274. {
  1275. // No, so warn.
  1276. Con::warnf( "Cannot check merge modules as path is invalid '%s'.", sourcePathBuffer );
  1277. return false;
  1278. }
  1279. // Initialize the source module manager and scan the source folder for modules.
  1280. ModuleManager mergeModuleManager;
  1281. mergeModuleManager.mEnforceDependencies = false;
  1282. mergeModuleManager.mEchoInfo = false;
  1283. mergeModuleManager.scanModules( pMergeSourcePath );
  1284. // Find all the merge modules.
  1285. typeConstModuleDefinitionVector mergeModules;
  1286. mergeModuleManager.findModules( false, mergeModules );
  1287. // Iterate found merge module definitions.
  1288. for ( typeConstModuleDefinitionVector::const_iterator mergeModuleItr = mergeModules.begin(); mergeModuleItr != mergeModules.end(); ++mergeModuleItr )
  1289. {
  1290. // Fetch module definition.
  1291. const ModuleDefinition* pMergeModuleDefinition = *mergeModuleItr;
  1292. // Fetch module Id.
  1293. StringTableEntry moduleId = pMergeModuleDefinition->getModuleId();
  1294. // Fetch version Id.
  1295. const U32 versionId = pMergeModuleDefinition->getVersionId();
  1296. // Fetch module group.
  1297. StringTableEntry moduleGroup = pMergeModuleDefinition->getModuleGroup();
  1298. // Cannot merge if module already exists.
  1299. if ( findModuleDefinition( moduleId, versionId ) != NULL )
  1300. return false;
  1301. // Cannot merge if module is part of a loaded group.
  1302. if ( findGroupLoaded( moduleGroup ) != NULL )
  1303. return false;
  1304. }
  1305. // Can merge modules.
  1306. return true;
  1307. }
  1308. //-----------------------------------------------------------------------------
  1309. bool ModuleManager::mergeModules( const char* pMergeTargetPath, const bool removeMergeDefinition, const bool registerNewModules )
  1310. {
  1311. // Sanity!
  1312. AssertFatal( pMergeTargetPath != NULL, "Cannot merge modules with a target path of NULL." );
  1313. // Is a module merge available?
  1314. if ( !isModuleMergeAvailable() )
  1315. {
  1316. // No, so warn.
  1317. Con::warnf( "Cannot merge modules as a module merge is not available." );
  1318. return false;
  1319. }
  1320. // Expand the target path.
  1321. char targetPathBuffer[1024];
  1322. Con::expandPath( targetPathBuffer, sizeof(targetPathBuffer), pMergeTargetPath );
  1323. pMergeTargetPath = targetPathBuffer;
  1324. // Fetch module merge file path.
  1325. StringTableEntry moduleMergeFilePath = getModuleMergeFilePath();
  1326. // Read module merge definition.
  1327. Taml taml;
  1328. ModuleMergeDefinition* pModuleMergeDefinition = taml.read<ModuleMergeDefinition>( moduleMergeFilePath );
  1329. // Do we have a module merge definition.
  1330. if ( pModuleMergeDefinition == NULL )
  1331. {
  1332. // No, so warn.
  1333. Con::warnf( "Cannot merge modules as the module merge definition file failed to load '%s'.", moduleMergeFilePath );
  1334. return false;
  1335. }
  1336. // Fetch the merge source path.
  1337. StringTableEntry mergeSourcePath = pModuleMergeDefinition->getModuleMergePath();
  1338. // Remove the module merge definition.
  1339. pModuleMergeDefinition->deleteObject();
  1340. pModuleMergeDefinition = NULL;
  1341. // If we cannot merge the modules then we only process modules flagged as critical merge.
  1342. const bool criticalMergeOnly = !canMergeModules( mergeSourcePath );
  1343. // Initialize the target module manager and scan the target folder for modules.
  1344. ModuleManager targetModuleManager;
  1345. targetModuleManager.mEnforceDependencies = false;
  1346. targetModuleManager.mEchoInfo = false;
  1347. targetModuleManager.scanModules( pMergeTargetPath );
  1348. // Initialize the source module manager and scan the source folder for modules.
  1349. ModuleManager sourceModuleManager;
  1350. sourceModuleManager.mEnforceDependencies = false;
  1351. sourceModuleManager.mEchoInfo = false;
  1352. sourceModuleManager.scanModules( mergeSourcePath );
  1353. // Find all the source modules.
  1354. typeConstModuleDefinitionVector sourceModules;
  1355. sourceModuleManager.findModules( false, sourceModules );
  1356. // Iterate found merge module definitions.
  1357. for ( typeConstModuleDefinitionVector::const_iterator sourceModuleItr = sourceModules.begin(); sourceModuleItr != sourceModules.end(); ++sourceModuleItr )
  1358. {
  1359. // Fetch the source module definition.
  1360. const ModuleDefinition* pSourceModuleDefinition = *sourceModuleItr;
  1361. // Skip if we're performing a critical merge only and the module is not flagged as critical merge.
  1362. if ( criticalMergeOnly && pSourceModuleDefinition->getCriticalMerge() )
  1363. continue;
  1364. // Fetch source module Id.
  1365. const StringTableEntry sourceModuleId = pSourceModuleDefinition->getModuleId();
  1366. // Fetch source version Id.
  1367. const U32 sourceVersionId = pSourceModuleDefinition->getVersionId();
  1368. // Fetch source build Id.
  1369. const U32 sourceBuildId = pSourceModuleDefinition->getBuildId();
  1370. // Format module Id folder path.
  1371. char targetModuleIdBuffer[1024];
  1372. dSprintf( targetModuleIdBuffer, sizeof(targetModuleIdBuffer), "%s/%s/", pMergeTargetPath, sourceModuleId );
  1373. // Flag to indicate if the merged module needs registering.
  1374. bool shouldRegisterModule;
  1375. // Does the module Id exist?
  1376. if ( targetModuleManager.findModuleId( sourceModuleId ) == NULL )
  1377. {
  1378. // No, so create module Id folder.
  1379. if ( !Platform::createPath( targetModuleIdBuffer ) )
  1380. {
  1381. // Warn.
  1382. Con::warnf("Cannot merge modules for module '%s' as the path '%s' could not be created.", sourceModuleId, targetModuleIdBuffer );
  1383. return false;
  1384. }
  1385. // Module Should be registered.
  1386. shouldRegisterModule = true;
  1387. }
  1388. else
  1389. {
  1390. // Yes, so find the target module definition that matches the source module definition.
  1391. ModuleDefinitionEntry::iterator targetModuleDefinitionItr = targetModuleManager.findModuleDefinition( sourceModuleId, sourceVersionId );
  1392. // Is there an existing target module definition entry?
  1393. if ( targetModuleDefinitionItr != NULL )
  1394. {
  1395. // Yes, so fetch the target module definition.
  1396. const ModuleDefinition* pTargetModuleDefinition = *targetModuleDefinitionItr;
  1397. // Fetch target module path.
  1398. StringTableEntry targetModulePath = pTargetModuleDefinition->getModulePath();
  1399. // Yes, so we have to remove it first.
  1400. if ( !Platform::deleteDirectory( targetModulePath ) )
  1401. {
  1402. // Module was not deleted so warn.
  1403. Con::warnf( "Failed to remove module folder located at '%s'. Module will be copied over.", targetModulePath );
  1404. }
  1405. // Is the build Id being downgraded?
  1406. if ( sourceBuildId < pTargetModuleDefinition->getBuildId() )
  1407. {
  1408. // Yes, so warn.
  1409. Con::warnf( "Encountered a downgraded build Id for module Id '%s' at version Id '%d'.", sourceModuleId, sourceBuildId );
  1410. }
  1411. // Module should not be registered.
  1412. shouldRegisterModule = false;
  1413. }
  1414. else
  1415. {
  1416. // Module Should be registered.
  1417. shouldRegisterModule = true;
  1418. }
  1419. }
  1420. // Fetch source module path.
  1421. StringTableEntry sourceModulePath = pSourceModuleDefinition->getModulePath();
  1422. // Format target version Id folder path.
  1423. char targetVersionIdBuffer[1024];
  1424. dSprintf( targetVersionIdBuffer, sizeof(targetVersionIdBuffer), "%s%d", targetModuleIdBuffer, sourceVersionId );
  1425. // Copy module (allow overwrites as we may have failed to remove the old folder in which case this is likely to fail as well).
  1426. if (!dPathCopy(sourceModulePath, targetVersionIdBuffer, false))
  1427. {
  1428. // Failed to copy module.
  1429. Con::warnf( "Failed to copy module folder located at '%s' to location '%s'. The modules may now be corrupted.", sourceModulePath, targetVersionIdBuffer );
  1430. }
  1431. // Are we registering new modules and the module needs registering?
  1432. if ( registerNewModules && shouldRegisterModule )
  1433. {
  1434. // Yes, so scan module.
  1435. scanModules( targetVersionIdBuffer, true );
  1436. }
  1437. // Is the module part of a critical merge?
  1438. if ( criticalMergeOnly )
  1439. {
  1440. // Yes, so we need to remove the source module definition.
  1441. if ( !Platform::deleteDirectory( sourceModulePath ) )
  1442. {
  1443. // Module was not deleted so warn.
  1444. Con::warnf( "Failed to remove CRITICAL merge module folder located at '%s'. Module will be copied over.", sourceModulePath );
  1445. }
  1446. }
  1447. }
  1448. // Do we need to remove the module merge definition file?
  1449. if ( removeMergeDefinition )
  1450. {
  1451. // Yes, so remove it.
  1452. dFileDelete( moduleMergeFilePath );
  1453. }
  1454. return true;
  1455. }
  1456. //-----------------------------------------------------------------------------
  1457. void ModuleManager::addListener( SimObject* pListener )
  1458. {
  1459. // Sanity!
  1460. AssertFatal( pListener != NULL, "Cannot add notifications to a NULL object." );
  1461. // Ignore if already added.
  1462. if (mNotificationListeners.find( pListener) != mNotificationListeners.end())
  1463. return;
  1464. // Add as a listener.
  1465. mNotificationListeners.addObject( pListener );
  1466. }
  1467. //-----------------------------------------------------------------------------
  1468. void ModuleManager::removeListener( SimObject* pListener )
  1469. {
  1470. // Sanity!
  1471. AssertFatal( pListener != NULL, "Cannot remove notifications from a NULL object." );
  1472. // Remove as a listener.
  1473. mNotificationListeners.removeObject( pListener );
  1474. }
  1475. //-----------------------------------------------------------------------------
  1476. void ModuleManager::clearDatabase( void )
  1477. {
  1478. // Lock database.
  1479. AssertFatal( mDatabaseLocks == 0, "Cannot clear database if database is locked." );
  1480. // Iterate groups loaded.
  1481. while ( mGroupsLoaded.size() > 0 )
  1482. {
  1483. // Unload module group.
  1484. unloadModuleGroup( *mGroupsLoaded.begin() );
  1485. }
  1486. // Iterate any other explicit modules that are loaded.
  1487. while ( mModulesLoaded.size() > 0 )
  1488. {
  1489. // Fetch module definition.
  1490. ModuleDefinition* pModuleDefinition = mModulesLoaded.begin()->mpModuleDefinition;
  1491. // Unload explicit module.
  1492. unloadModuleExplicit( pModuleDefinition->getModuleId() );
  1493. }
  1494. // Iterate modules to delete module definitions.
  1495. for ( typeModuleIdDatabaseHash::iterator moduleItr = mModuleIdDatabase.begin(); moduleItr != mModuleIdDatabase.end(); ++moduleItr )
  1496. {
  1497. // Fetch modules definitions.
  1498. ModuleDefinitionEntry* pDefinitions = moduleItr->value;
  1499. // Iterate module definitions.
  1500. for ( ModuleDefinitionEntry::iterator definitionItr = pDefinitions->begin(); definitionItr != pDefinitions->end(); ++definitionItr )
  1501. {
  1502. // Fetch module definition.
  1503. ModuleDefinition* pModuleDefinition = *definitionItr;
  1504. // Remove notification before we delete it.
  1505. clearNotify( pModuleDefinition );
  1506. // Delete module definition.
  1507. pModuleDefinition->deleteObject();
  1508. }
  1509. // Clear definitions.
  1510. delete pDefinitions;
  1511. }
  1512. // Clear database.
  1513. mModuleIdDatabase.clear();
  1514. // Iterate module groups.
  1515. for ( typeGroupModuleHash::iterator moduleGroupItr = mGroupModules.begin(); moduleGroupItr != mGroupModules.end(); ++moduleGroupItr )
  1516. {
  1517. // Delete module group vector.
  1518. delete moduleGroupItr->value;
  1519. }
  1520. // Clear module groups.
  1521. mGroupModules.clear();
  1522. }
  1523. //-----------------------------------------------------------------------------
  1524. bool ModuleManager::removeModuleDefinition( ModuleDefinition* pModuleDefinition )
  1525. {
  1526. // Sanity!
  1527. AssertFatal( pModuleDefinition != NULL, "Cannot remove module definition if it is NULL." );
  1528. // Fetch module Id.
  1529. StringTableEntry moduleId = pModuleDefinition->getModuleId();
  1530. // Is the module definition registered with this module manager?
  1531. if ( pModuleDefinition->getModuleManager() != this )
  1532. {
  1533. // No, so warn.
  1534. Con::warnf("Cannot remove module definition '%s' as it is not registered with this module manager.", moduleId );
  1535. return false;
  1536. }
  1537. // Is the module definition loaded?
  1538. if ( pModuleDefinition->getLoadCount() > 0 )
  1539. {
  1540. // No, so warn.
  1541. Con::warnf("Cannot remove module definition '%s' as it is loaded.", moduleId );
  1542. return false;
  1543. }
  1544. // Find module Id.
  1545. typeModuleIdDatabaseHash::iterator moduleItr = mModuleIdDatabase.find( moduleId );
  1546. // Sanity!
  1547. AssertFatal( moduleItr != mModuleIdDatabase.end(), "Failed to find module definition." );
  1548. // Fetch modules definitions.
  1549. ModuleDefinitionEntry* pDefinitions = moduleItr->value;
  1550. // Fetch version Id.
  1551. const U32 versionId = pModuleDefinition->getVersionId();
  1552. // Iterate module definitions.
  1553. for ( ModuleDefinitionEntry::iterator definitionItr = pDefinitions->begin(); definitionItr != pDefinitions->end(); ++definitionItr )
  1554. {
  1555. // Skip if this isn't the version Id we're searching for.
  1556. if ( versionId != (*definitionItr)->getVersionId() )
  1557. continue;
  1558. // Remove definition entry.
  1559. pDefinitions->erase( definitionItr );
  1560. // Remove notification before we delete it.
  1561. clearNotify( pModuleDefinition );
  1562. // Delete module definition.
  1563. pModuleDefinition->deleteObject();
  1564. // Are there any modules left for this module Id?
  1565. if ( findModuleId( moduleId ) == NULL )
  1566. {
  1567. bool moduleIdFound = false;
  1568. // No, so remove from groups.
  1569. for( typeGroupModuleHash::iterator moduleGroupItr = mGroupModules.begin(); moduleGroupItr != mGroupModules.end(); ++moduleGroupItr )
  1570. {
  1571. // Fetch module Ids.
  1572. typeModuleIdVector* pModuleIds = moduleGroupItr->value;
  1573. // Iterate module Id.
  1574. for( typeModuleIdVector::iterator moduleIdItr = pModuleIds->begin(); moduleIdItr != pModuleIds->end(); ++moduleIdItr )
  1575. {
  1576. // Skip if this isn't the Id.
  1577. if ( *moduleIdItr != moduleId )
  1578. continue;
  1579. // Remove the module Id.
  1580. pModuleIds->erase( moduleIdItr );
  1581. // Flag as found.
  1582. moduleIdFound = true;
  1583. break;
  1584. }
  1585. // Finish if found.
  1586. if ( moduleIdFound )
  1587. break;
  1588. }
  1589. }
  1590. return true;
  1591. }
  1592. // Sanity!
  1593. AssertFatal( false, "Failed to find module definition." );
  1594. return false;
  1595. }
  1596. //-----------------------------------------------------------------------------
  1597. bool ModuleManager::registerModule( const char* pModulePath, const char* pModuleFile )
  1598. {
  1599. // Sanity!
  1600. AssertFatal( pModulePath != NULL, "Cannot scan module with NULL module path." );
  1601. AssertFatal( pModuleFile != NULL, "Cannot scan module with NULL module file." );
  1602. // Make the module path a full-path.
  1603. char fullPathBuffer[1024];
  1604. Platform::makeFullPathName( pModulePath, fullPathBuffer, sizeof(fullPathBuffer) );
  1605. pModulePath = fullPathBuffer;
  1606. char formatBuffer[1024];
  1607. // Fetch module path trail character.
  1608. char modulePathTrail = pModulePath[dStrlen(pModulePath) - 1];
  1609. // Format module file-path.
  1610. dSprintf( formatBuffer, sizeof(formatBuffer), modulePathTrail == '/' ? "%s%s" : "%s/%s", pModulePath, pModuleFile );
  1611. // Read the module file.
  1612. ModuleDefinition* pModuleDefinition = mTaml.read<ModuleDefinition>( formatBuffer );
  1613. // Did we read a module definition?
  1614. if ( pModuleDefinition == NULL )
  1615. {
  1616. // No, so warn.
  1617. Con::warnf( "Module Manager: Failed to read module definition in file '%s'.", formatBuffer );
  1618. return false;
  1619. }
  1620. // Set the module manager.
  1621. pModuleDefinition->setModuleManager( this );
  1622. // Set module definition path.
  1623. pModuleDefinition->setModulePath( pModulePath );
  1624. // Set module file.
  1625. pModuleDefinition->setModuleFile( pModuleFile );
  1626. // Set module file-path.
  1627. pModuleDefinition->setModuleFilePath( formatBuffer );
  1628. // Fetch module Id.
  1629. StringTableEntry moduleId = pModuleDefinition->getModuleId();
  1630. // Fetch module version Id.
  1631. const U32 versionId = pModuleDefinition->getVersionId();
  1632. // Fetch module group.
  1633. StringTableEntry moduleGroup = pModuleDefinition->getModuleGroup();
  1634. // Fetch module type.
  1635. StringTableEntry moduleType = pModuleDefinition->getModuleType();
  1636. // Is the module enabled?
  1637. if ( !pModuleDefinition->getEnabled() )
  1638. {
  1639. // No, so warn.
  1640. Con::warnf( "Module Manager: Found module: '%s' but it is disabled.", pModuleDefinition->getModuleFilePath() );
  1641. // Destroy module definition and finish.
  1642. pModuleDefinition->deleteObject();
  1643. return false;
  1644. }
  1645. // Is the module Id valid?
  1646. if (moduleId == StringTable->EmptyString())
  1647. {
  1648. // No, so warn.
  1649. Con::warnf( "Module Manager: Found module: '%s' but it has an unspecified module Id.",
  1650. pModuleDefinition->getModuleFilePath() );
  1651. // Destroy module definition and finish.
  1652. pModuleDefinition->deleteObject();
  1653. return false;
  1654. }
  1655. // Is the module version Id valid?
  1656. if ( versionId == 0 )
  1657. {
  1658. // No, so warn.
  1659. Con::warnf( "Module Manager: Found Manager: Registering module: '%s' but it has an invalid Version Id of '0'.",
  1660. pModuleDefinition->getModuleFilePath() );
  1661. // Destroy module definition and finish.
  1662. pModuleDefinition->deleteObject();
  1663. return false;
  1664. }
  1665. // Is the module group already loaded?
  1666. if ( findGroupLoaded( moduleGroup ) != NULL && !mIgnoreLoadedGroups)
  1667. {
  1668. // Yes, so warn.
  1669. Con::warnf( "Module Manager: Found module: '%s' but it is in a module group '%s' which has already been loaded.",
  1670. pModuleDefinition->getModuleFilePath(),
  1671. moduleGroup );
  1672. // Destroy module definition and finish.
  1673. pModuleDefinition->deleteObject();
  1674. return false;
  1675. }
  1676. // Was a script-file specified?
  1677. if ( pModuleDefinition->getScriptFile() != StringTable->EmptyString() )
  1678. {
  1679. // Yes, so format module script file-path.
  1680. dSprintf( formatBuffer, sizeof(formatBuffer), modulePathTrail == '/' ? "%s%s" : "%s/%s", pModulePath, pModuleDefinition->getScriptFile() );
  1681. pModuleDefinition->setModuleScriptFilePath( formatBuffer );
  1682. }
  1683. // Format module signature,
  1684. dSprintf( formatBuffer, sizeof(formatBuffer), "%s_%d_%d", moduleId, versionId, pModuleDefinition->getBuildId() );
  1685. pModuleDefinition->setSignature( formatBuffer );
  1686. // Locked the module definition.
  1687. pModuleDefinition->setModuleLocked( true );
  1688. // Fetch modules definitions.
  1689. ModuleDefinitionEntry* pDefinitions = findModuleId( moduleId );
  1690. // Did we find the module Id?
  1691. if ( pDefinitions != NULL )
  1692. {
  1693. // Yes, so find the module definition.
  1694. ModuleDefinitionEntry::iterator definitionItr = findModuleDefinition( moduleId, versionId );
  1695. // Does this version Id already exist?
  1696. if ( definitionItr != NULL )
  1697. {
  1698. // Yes, so warn.
  1699. Con::warnf( "Module Manager: Found module: '%s' but it is already registered as module Id '%s' at version Id '%d'.",
  1700. pModuleDefinition->getModuleFilePath(), moduleId, versionId );
  1701. // Destroy module definition and finish.
  1702. pModuleDefinition->deleteObject();
  1703. return false;
  1704. }
  1705. // Is the module group the same as the module definitions we already have?
  1706. if ( moduleGroup != pDefinitions->mModuleGroup )
  1707. {
  1708. // No, so warn.
  1709. 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.",
  1710. pModuleDefinition->getModuleFilePath(), moduleGroup );
  1711. // Destroy module definition and finish.
  1712. pModuleDefinition->deleteObject();
  1713. return false;
  1714. }
  1715. // Is the module type the same as the module definitions we already have?
  1716. if ( moduleType != pDefinitions->mModuleType )
  1717. {
  1718. // No, so warn.
  1719. 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.",
  1720. pModuleDefinition->getModuleFilePath(), moduleGroup );
  1721. // Destroy module definition and finish.
  1722. pModuleDefinition->deleteObject();
  1723. return false;
  1724. }
  1725. }
  1726. else
  1727. {
  1728. // No, so create a vector of definitions.
  1729. pDefinitions = new ModuleDefinitionEntry( moduleId, moduleGroup, moduleType );
  1730. // Insert module Id definitions.
  1731. mModuleIdDatabase.insert( moduleId, pDefinitions );
  1732. }
  1733. // Add module definition.
  1734. pDefinitions->push_back( pModuleDefinition );
  1735. // Sort module definitions by version Id so that higher versions appear first.
  1736. dQsort( pDefinitions->address(), pDefinitions->size(), sizeof(ModuleDefinition*), moduleDefinitionVersionIdSort );
  1737. // Find module group.
  1738. typeGroupModuleHash::iterator moduleGroupItr = mGroupModules.find( moduleGroup );
  1739. // Did we find the module group?
  1740. if ( moduleGroupItr != mGroupModules.end() )
  1741. {
  1742. // Yes, so fetch module Ids.
  1743. typeModuleIdVector* pModuleIds = moduleGroupItr->value;
  1744. // Is the module Id already present?
  1745. bool moduleIdFound = false;
  1746. for( typeModuleIdVector::iterator moduleIdItr = pModuleIds->begin(); moduleIdItr != pModuleIds->end(); ++moduleIdItr )
  1747. {
  1748. // Skip if this isn't the Id.
  1749. if ( *moduleIdItr != moduleId )
  1750. continue;
  1751. // Flag as found.
  1752. moduleIdFound = true;
  1753. break;
  1754. }
  1755. // Add if module Id was not found.
  1756. if ( !moduleIdFound )
  1757. pModuleIds->push_back( moduleId );
  1758. }
  1759. else
  1760. {
  1761. // No, so insert a module Id vector.
  1762. moduleGroupItr = mGroupModules.insert( pModuleDefinition->getModuleGroup(), new typeModuleIdVector() );
  1763. // Add module Id.
  1764. moduleGroupItr->value->push_back( moduleId );
  1765. }
  1766. // Notify if the module definition is destroyed.
  1767. deleteNotify( pModuleDefinition );
  1768. // Info.
  1769. if ( mEchoInfo )
  1770. {
  1771. #if 1
  1772. Con::printf( "Module Manager: Registering: '%s' [ ID='%s', VersionId='%d', BuildId='%d', Description='%s' ].",
  1773. pModuleDefinition->getModuleFilePath(),
  1774. pModuleDefinition->getModuleId(),
  1775. pModuleDefinition->getVersionId(),
  1776. pModuleDefinition->getBuildId(),
  1777. pModuleDefinition->getModuleDescription()
  1778. );
  1779. #else
  1780. Con::printf( "Module Manager: Registering: '%s' [ ID='%s', VersionId='%d', BuildId='%d', Description='%s', Group='%s', Dependencies='%s', ScriptFile='%s', CreateFunction='%s', DestroyFunction='%s' ].",
  1781. pModuleDefinition->getModuleFilePath(),
  1782. pModuleDefinition->getModuleId(),
  1783. pModuleDefinition->getVersionId(),
  1784. pModuleDefinition->getBuildId(),
  1785. pModuleDefinition->getModuleDescription(),
  1786. pModuleDefinition->getModuleGroup(),
  1787. pModuleDefinition->getDataField( StringTable->insert("Dependencies"), NULL ),
  1788. pModuleDefinition->getScriptFile(),
  1789. pModuleDefinition->getCreateFunction(),
  1790. pModuleDefinition->getDestroyFunction()
  1791. );
  1792. #endif
  1793. }
  1794. // Emit notifications.
  1795. for( SimSet::iterator notifyItr = mNotificationListeners.begin(); notifyItr != mNotificationListeners.end(); ++notifyItr )
  1796. {
  1797. Con::executef( *notifyItr, "onModuleRegister", pModuleDefinition->getIdString() );
  1798. }
  1799. return true;
  1800. }
  1801. //-----------------------------------------------------------------------------
  1802. bool ModuleManager::unregisterModule( const char* pModuleId, const U32 versionId )
  1803. {
  1804. // Sanity!
  1805. AssertFatal( pModuleId != NULL, "A module Id cannot be NULL." );
  1806. // Fetch module Id.
  1807. StringTableEntry moduleId = StringTable->insert( pModuleId );
  1808. // Find the module definition.
  1809. ModuleDefinition* pModuleDefinition = findModule( pModuleId, versionId );
  1810. // Did we find the module definition?
  1811. if ( pModuleDefinition == NULL )
  1812. {
  1813. // No, so warn.
  1814. Con::warnf( "Module Manager: Cannot unregister module Id '%s' as it is not registered.", moduleId );
  1815. return false;
  1816. }
  1817. // Remove the module definition.
  1818. return removeModuleDefinition( pModuleDefinition );
  1819. }
  1820. //-----------------------------------------------------------------------------
  1821. void ModuleManager::raiseModulePreLoadNotifications( ModuleDefinition* pModuleDefinition )
  1822. {
  1823. // Raise notifications.
  1824. for( SimSet::iterator notifyItr = mNotificationListeners.begin(); notifyItr != mNotificationListeners.end(); ++notifyItr )
  1825. {
  1826. // Fetch listener object.
  1827. SimObject* pListener = *notifyItr;
  1828. // Perform object callback.
  1829. ModuleCallbacks* pCallbacks = dynamic_cast<ModuleCallbacks*>( pListener );
  1830. if ( pCallbacks != NULL )
  1831. pCallbacks->onModulePreLoad( pModuleDefinition );
  1832. // Perform script callback.
  1833. if ( pListener->isMethod( "onModulePreLoad" ) )
  1834. Con::executef( pListener, "onModulePreLoad", pModuleDefinition->getIdString() );
  1835. }
  1836. }
  1837. //-----------------------------------------------------------------------------
  1838. void ModuleManager::raiseModulePostLoadNotifications( ModuleDefinition* pModuleDefinition )
  1839. {
  1840. // Raise notifications.
  1841. for( SimSet::iterator notifyItr = mNotificationListeners.begin(); notifyItr != mNotificationListeners.end(); ++notifyItr )
  1842. {
  1843. // Fetch listener object.
  1844. SimObject* pListener = *notifyItr;
  1845. // Perform object callback.
  1846. ModuleCallbacks* pCallbacks = dynamic_cast<ModuleCallbacks*>( pListener );
  1847. if ( pCallbacks != NULL )
  1848. pCallbacks->onModulePostLoad( pModuleDefinition );
  1849. // Perform script callback.
  1850. if ( pListener->isMethod( "onModulePostLoad" ) )
  1851. Con::executef( pListener, "onModulePostLoad", pModuleDefinition->getIdString() );
  1852. }
  1853. }
  1854. //-----------------------------------------------------------------------------
  1855. void ModuleManager::raiseModulePreUnloadNotifications( ModuleDefinition* pModuleDefinition )
  1856. {
  1857. // Raise notifications.
  1858. for( SimSet::iterator notifyItr = mNotificationListeners.begin(); notifyItr != mNotificationListeners.end(); ++notifyItr )
  1859. {
  1860. // Fetch listener object.
  1861. SimObject* pListener = *notifyItr;
  1862. // Perform object callback.
  1863. ModuleCallbacks* pCallbacks = dynamic_cast<ModuleCallbacks*>( pListener );
  1864. if ( pCallbacks != NULL )
  1865. pCallbacks->onModulePreUnload( pModuleDefinition );
  1866. // Perform script callback.
  1867. if ( pListener->isMethod( "onModulePreUnload" ) )
  1868. Con::executef( pListener, "onModulePreUnload", pModuleDefinition->getIdString() );
  1869. }
  1870. }
  1871. //-----------------------------------------------------------------------------
  1872. void ModuleManager::raiseModulePostUnloadNotifications( ModuleDefinition* pModuleDefinition )
  1873. {
  1874. // Raise notifications.
  1875. for( SimSet::iterator notifyItr = mNotificationListeners.begin(); notifyItr != mNotificationListeners.end(); ++notifyItr )
  1876. {
  1877. // Fetch listener object.
  1878. SimObject* pListener = *notifyItr;
  1879. // Perform object callback.
  1880. ModuleCallbacks* pCallbacks = dynamic_cast<ModuleCallbacks*>( pListener );
  1881. if ( pCallbacks != NULL )
  1882. pCallbacks->onModulePostUnload( pModuleDefinition );
  1883. // Perform script callback.
  1884. if ( pListener->isMethod( "onModulePostUnload" ) )
  1885. Con::executef( pListener, "onModulePostUnload", pModuleDefinition->getIdString() );
  1886. }
  1887. }
  1888. //-----------------------------------------------------------------------------
  1889. ModuleManager::ModuleDefinitionEntry* ModuleManager::findModuleId( StringTableEntry moduleId )
  1890. {
  1891. // Sanity!
  1892. AssertFatal( moduleId != NULL, "A module Id cannot be NULL." );
  1893. // Is the module Id valid?
  1894. if ( moduleId == StringTable->EmptyString() )
  1895. {
  1896. // No, so warn.
  1897. Con::warnf( "Module Manager: Invalid Module Id." );
  1898. return NULL;
  1899. }
  1900. // Find module Id.
  1901. typeModuleIdDatabaseHash::iterator moduleItr = mModuleIdDatabase.find( moduleId );
  1902. // Return appropriately.
  1903. return moduleItr != mModuleIdDatabase.end() ? moduleItr->value : NULL;
  1904. }
  1905. //-----------------------------------------------------------------------------
  1906. ModuleManager::ModuleDefinitionEntry::iterator ModuleManager::findModuleDefinition( StringTableEntry moduleId, const U32 versionId )
  1907. {
  1908. // Fetch modules definitions.
  1909. ModuleDefinitionEntry* pDefinitions = findModuleId( moduleId );
  1910. // Finish if no module definitions for the module Id.
  1911. if ( pDefinitions == NULL )
  1912. return NULL;
  1913. // Iterate module definitions.
  1914. for ( ModuleDefinitionEntry::iterator definitionItr = pDefinitions->begin(); definitionItr != pDefinitions->end(); ++definitionItr )
  1915. {
  1916. // Skip if this isn't the version Id we're searching for.
  1917. if ( versionId != (*definitionItr)->getVersionId() )
  1918. continue;
  1919. // Return module definition iterator.
  1920. return definitionItr;
  1921. }
  1922. // Not found.
  1923. return NULL;
  1924. }
  1925. //-----------------------------------------------------------------------------
  1926. bool ModuleManager::resolveModuleDependencies( StringTableEntry moduleId, const U32 versionId, StringTableEntry moduleGroup, bool synchronizedOnly, typeModuleLoadEntryVector& moduleResolvingQueue, typeModuleLoadEntryVector& moduleReadyQueue )
  1927. {
  1928. // Fetch the module Id ready entry.
  1929. ModuleLoadEntry* pLoadReadyEntry = findModuleReady( moduleId, moduleReadyQueue );
  1930. // Is there a load entry?
  1931. if ( pLoadReadyEntry )
  1932. {
  1933. // Yes, so finish if the version Id is not important,
  1934. if ( versionId == 0 )
  1935. return true;
  1936. // Finish if the version Id are compatible.
  1937. if ( versionId == pLoadReadyEntry->mpModuleDefinition->getVersionId() )
  1938. return true;
  1939. // Is it a strict version Id?
  1940. if ( pLoadReadyEntry->mStrictVersionId )
  1941. {
  1942. // Yes, so warn.
  1943. 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.",
  1944. moduleId, versionId, pLoadReadyEntry->mpModuleDefinition->getVersionId(), moduleGroup );
  1945. return false;
  1946. }
  1947. // No, so find the required module version Id.
  1948. ModuleDefinitionEntry::iterator definitionItr = findModuleDefinition( moduleId, versionId );
  1949. // Did we find the requested module definition.
  1950. if ( definitionItr == NULL )
  1951. {
  1952. // No, so we can safely ignore the missing dependency if we're not enforcing dependencies.
  1953. if ( !mEnforceDependencies )
  1954. return true;
  1955. // Warn!
  1956. Con::warnf( "Module Manager: A missing module dependency was detected loading module Id '%s' at version Id '%d' in group '%s'.",
  1957. moduleId, versionId, moduleGroup );
  1958. return false;
  1959. }
  1960. // Set the new module definition.
  1961. pLoadReadyEntry->mpModuleDefinition = *definitionItr;
  1962. // Set strict version Id.
  1963. pLoadReadyEntry->mStrictVersionId = true;
  1964. return true;
  1965. }
  1966. // Is the module Id load resolving?
  1967. if ( findModuleResolving( moduleId, moduleResolvingQueue ) != NULL )
  1968. {
  1969. // Yes, so a cycle has been detected so warn.
  1970. Con::warnf( "Module Manager: A cyclic dependency was detected resolving module Id '%s' at version Id '%d' in group '%s'.",
  1971. moduleId, versionId, moduleGroup );
  1972. return false;
  1973. }
  1974. // Reset selected module definition.
  1975. ModuleDefinition* pSelectedModuleDefinition = NULL;
  1976. // Do we want the latest version Id?
  1977. if ( versionId == 0 )
  1978. {
  1979. // Yes, so find the module Id.
  1980. typeModuleIdDatabaseHash::iterator moduleIdItr = mModuleIdDatabase.find( moduleId );
  1981. // Did we find the module Id?
  1982. if ( moduleIdItr == mModuleIdDatabase.end() )
  1983. {
  1984. // No, so we can safely ignore the missing dependency if we're not enforcing dependencies.
  1985. if ( !mEnforceDependencies )
  1986. return true;
  1987. // Warn!
  1988. Con::warnf( "Module Manager: A missing module dependency was detected loading module Id '%s' at version Id '%d' in group '%s'.",
  1989. moduleId, versionId, moduleGroup );
  1990. return false;
  1991. }
  1992. // Fetch first module definition which should be the highest version Id.
  1993. pSelectedModuleDefinition = (*moduleIdItr->value)[0];
  1994. }
  1995. else
  1996. {
  1997. // No, so find the module Id at the specific version Id.
  1998. ModuleDefinitionEntry::iterator definitionItr = findModuleDefinition( moduleId, versionId );
  1999. // Did we find the module definition?
  2000. if ( definitionItr == NULL )
  2001. {
  2002. // No, so we can safely ignore the missing dependency if we're not enforcing dependencies.
  2003. if ( !mEnforceDependencies )
  2004. return true;
  2005. // Warn!
  2006. Con::warnf( "Module Manager: A missing module dependency was detected loading module Id '%s' at version Id '%d' in group '%s'.",
  2007. moduleId, versionId, moduleGroup );
  2008. return false;
  2009. }
  2010. // Select the module definition.
  2011. pSelectedModuleDefinition = *definitionItr;
  2012. }
  2013. // If we're only resolving synchronized modules and the module is not synchronized then finish.
  2014. if ( synchronizedOnly && !pSelectedModuleDefinition->getSynchronized() )
  2015. return true;
  2016. // Create a load entry.
  2017. ModuleLoadEntry loadEntry( pSelectedModuleDefinition, false );
  2018. // Fetch module dependencies.
  2019. const ModuleDefinition::typeModuleDependencyVector& moduleDependencies = pSelectedModuleDefinition->getDependencies();
  2020. // Do we have any module dependencies?
  2021. if ( moduleDependencies.size() > 0 )
  2022. {
  2023. // Yes, so queue this module as resolving.
  2024. moduleResolvingQueue.push_back( loadEntry );
  2025. // Iterate module dependencies.
  2026. for( ModuleDefinition::typeModuleDependencyVector::const_iterator dependencyItr = moduleDependencies.begin(); dependencyItr != moduleDependencies.end(); ++dependencyItr )
  2027. {
  2028. // Finish if we could not the dependent module Id at the version Id.
  2029. if ( !resolveModuleDependencies( dependencyItr->mModuleId, dependencyItr->mVersionId, moduleGroup, synchronizedOnly, moduleResolvingQueue, moduleReadyQueue ) )
  2030. return false;
  2031. }
  2032. // Remove module as resolving.
  2033. moduleResolvingQueue.pop_back();
  2034. }
  2035. // Queue module as ready.
  2036. moduleReadyQueue.push_back( loadEntry );
  2037. return true;
  2038. }
  2039. //-----------------------------------------------------------------------------
  2040. ModuleManager::ModuleLoadEntry* ModuleManager::findModuleResolving( StringTableEntry moduleId, typeModuleLoadEntryVector& moduleResolvingQueue )
  2041. {
  2042. // Iterate module load resolving queue.
  2043. for( typeModuleLoadEntryVector::iterator loadEntryItr = moduleResolvingQueue.begin(); loadEntryItr != moduleResolvingQueue.end(); ++loadEntryItr )
  2044. {
  2045. // Finish if found.
  2046. if ( moduleId == loadEntryItr->mpModuleDefinition->getModuleId() )
  2047. return loadEntryItr;
  2048. }
  2049. // Not found.
  2050. return NULL;
  2051. }
  2052. //-----------------------------------------------------------------------------
  2053. ModuleManager::ModuleLoadEntry* ModuleManager::findModuleReady( StringTableEntry moduleId, typeModuleLoadEntryVector& moduleReadyQueue )
  2054. {
  2055. // Iterate module load ready queue.
  2056. for( typeModuleLoadEntryVector::iterator loadEntryItr = moduleReadyQueue.begin(); loadEntryItr != moduleReadyQueue.end(); ++loadEntryItr )
  2057. {
  2058. // Finish if found.
  2059. if ( moduleId == loadEntryItr->mpModuleDefinition->getModuleId() )
  2060. return loadEntryItr;
  2061. }
  2062. // Not found.
  2063. return NULL;
  2064. }
  2065. //-----------------------------------------------------------------------------
  2066. ModuleManager::typeModuleLoadEntryVector::iterator ModuleManager::findModuleLoaded( StringTableEntry moduleId, const U32 versionId )
  2067. {
  2068. // Iterate module loaded queue.
  2069. for( typeModuleLoadEntryVector::iterator loadEntryItr = mModulesLoaded.begin(); loadEntryItr != mModulesLoaded.end(); ++loadEntryItr )
  2070. {
  2071. // Skip if not the module Id we're looking for.
  2072. if ( moduleId != loadEntryItr->mpModuleDefinition->getModuleId() )
  2073. continue;
  2074. // Skip if we are searching for a specific version and it does not match.
  2075. if ( versionId != 0 && versionId != loadEntryItr->mpModuleDefinition->getVersionId() )
  2076. continue;
  2077. return loadEntryItr;
  2078. }
  2079. // Not found.
  2080. return NULL;
  2081. }
  2082. //-----------------------------------------------------------------------------
  2083. ModuleManager::typeGroupVector::iterator ModuleManager::findGroupLoaded( StringTableEntry moduleGroup )
  2084. {
  2085. // Iterate groups loaded queue.
  2086. for( typeGroupVector::iterator groupsLoadedItr = mGroupsLoaded.begin(); groupsLoadedItr != mGroupsLoaded.end(); ++groupsLoadedItr )
  2087. {
  2088. // Finish if found.
  2089. if ( moduleGroup == *groupsLoadedItr )
  2090. return groupsLoadedItr;
  2091. }
  2092. // Not found.
  2093. return NULL;
  2094. }
  2095. //-----------------------------------------------------------------------------
  2096. StringTableEntry ModuleManager::getModuleMergeFilePath( void ) const
  2097. {
  2098. // Format merge file path.
  2099. char filePathBuffer[1024];
  2100. dSprintf( filePathBuffer, sizeof(filePathBuffer), "%s/%s", Platform::getExecutablePath(), MODULE_MANAGER_MERGE_FILE );
  2101. return StringTable->insert( filePathBuffer );
  2102. }