moduleManager.cpp 98 KB

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