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