moduleManager.cpp 94 KB

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