moduleManager.cpp 100 KB

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