moduleManager.cpp 99 KB

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