2
0

moduleManager.cpp 100 KB

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