moduleManager.cpp 100 KB

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