moduleManager.cc 92 KB

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