moduleManager.cpp 101 KB

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