2
0

moduleManager.cpp 99 KB

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