moduleManager.cpp 95 KB

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