moduleManager.cpp 99 KB

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