moduleManager.cpp 100 KB

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