module.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "platform/platform.h"
  23. #include "core/module.h"
  24. #include "core/util/tVector.h"
  25. #include "core/strings/stringFunctions.h"
  26. //#define DEBUG_SPEW
  27. #define DEBUG_SPEW_LEVEL 2
  28. Module* Module::smFirst;
  29. //-----------------------------------------------------------------------------
  30. bool Module::_constrainedToComeBefore( Module* module, Mode mode )
  31. {
  32. if( module == this )
  33. return false;
  34. for( Dependency* dependency = _getDependencies( mode );
  35. dependency != NULL; dependency = dependency->mNext )
  36. {
  37. Module* depModule = dependency->mModule;
  38. if( !depModule )
  39. {
  40. depModule = EngineModuleManager::findModule( dependency->mModuleName );
  41. if( !depModule )
  42. {
  43. // Module does not exist. Only emit a warning here so that modules
  44. // can be omitted from a link without requiring the module definitions
  45. // to be adapted.
  46. Platform::outputDebugString( "[EngineModuleManager] Module %s of '%s' depends on module '%s' which does not exist",
  47. mode == Module::ModeInitialize ? "init" : "shutdown",
  48. module->getName(), dependency->mModuleName );
  49. continue;
  50. }
  51. dependency->mModule = depModule;
  52. }
  53. if( dependency->mType == DependencyBefore )
  54. {
  55. if( depModule == module
  56. || depModule->_constrainedToComeBefore( module, mode ) )
  57. return true;
  58. }
  59. }
  60. return false;
  61. }
  62. //-----------------------------------------------------------------------------
  63. bool Module::_constrainedToComeAfter( Module* module, Mode mode )
  64. {
  65. if( module == this )
  66. return false;
  67. for( Dependency* dependency = _getDependencies( mode );
  68. dependency != NULL; dependency = dependency->mNext )
  69. {
  70. Module* depModule = dependency->mModule;
  71. if( !depModule )
  72. {
  73. depModule = EngineModuleManager::findModule( dependency->mModuleName );
  74. if( !depModule )
  75. {
  76. // Module does not exist. Only emit a warning here so that modules
  77. // can be omitted from a link without requiring the module definitions
  78. // to be adapted.
  79. Platform::outputDebugString( "[EngineModuleManager] Module %s of '%s' depends on module '%s' which does not exist",
  80. mode == Module::ModeInitialize ? "init" : "shutdown",
  81. module->getName(), dependency->mModuleName );
  82. continue;
  83. }
  84. dependency->mModule = depModule;
  85. }
  86. if( dependency->mType == DependencyAfter )
  87. {
  88. if( depModule == module
  89. || depModule->_constrainedToComeAfter( module, mode ) )
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. //-----------------------------------------------------------------------------
  96. String EngineModuleManager::_moduleListToString( Vector< Module* >& moduleList )
  97. {
  98. StringBuilder str;
  99. const U32 numModules = moduleList.size();
  100. bool isFirst = true;
  101. for( U32 i = 0; i < numModules; ++ i )
  102. {
  103. if( !isFirst )
  104. str.append( " -> " );
  105. str.append( moduleList[ i ]->getName() );
  106. isFirst = false;
  107. }
  108. return str.end();
  109. }
  110. //-----------------------------------------------------------------------------
  111. void EngineModuleManager::_printModuleList( Vector< Module* >& moduleList )
  112. {
  113. Platform::outputDebugString( _moduleListToString( moduleList ) );
  114. }
  115. //-----------------------------------------------------------------------------
  116. void EngineModuleManager::_insertIntoModuleList( Module::Mode mode, Vector< Module* >& moduleList, Module* module )
  117. {
  118. // If this module is being overridden, switch over to
  119. // the module overriding it.
  120. Module* override;
  121. do
  122. {
  123. override = _findOverrideFor( module );
  124. if( override )
  125. module = override;
  126. }
  127. while( override != NULL );
  128. // If we are already on the list, return.
  129. if( _getIndexOfModuleInList( moduleList, module ) != -1 )
  130. return;
  131. // If we don't have dependencies, just push the module
  132. // to the back of the list.
  133. if( !module->_getDependencies( mode ) )
  134. {
  135. #if defined( DEBUG_SPEW ) && DEBUG_SPEW_LEVEL > 1
  136. Platform::outputDebugString( "[EngineModuleManager] Appending '%s' to '%s'",
  137. module->getName(), _moduleListToString( moduleList ).c_str() );
  138. #endif
  139. moduleList.push_back( module );
  140. return;
  141. }
  142. // First make sure that all 'after' dependencies are in the list.
  143. #if defined( DEBUG_SPEW ) && DEBUG_SPEW_LEVEL > 1
  144. Platform::outputDebugString( "[EngineModuleManager] Resolving %s dependencies of '%s'",
  145. mode == Module::ModeInitialize ? "init" : "shutdown",
  146. module->getName() );
  147. #endif
  148. for( Module::Dependency* dependency = module->_getDependencies( mode );
  149. dependency != NULL; dependency = dependency->mNext )
  150. {
  151. if( dependency->mType != Module::DependencyAfter )
  152. continue;
  153. dependency->mModule = findModule( dependency->mModuleName );
  154. if( !dependency->mModule )
  155. continue; // Allow modules to not exist.
  156. if( _getIndexOfModuleInList( moduleList, dependency->mModule ) == -1 )
  157. _insertIntoModuleList( mode, moduleList, dependency->mModule );
  158. }
  159. AssertFatal( _getIndexOfModuleInList( moduleList, module ) == -1,
  160. avar( "EngineModuleManager::_insertModuleIntoList - Cycle in 'after' %s dependency chain of '%s'",
  161. mode == Module::ModeInitialize ? "init" : "shutdown",
  162. module->getName() ) );
  163. // Now add the module itself.
  164. const U32 numModules = moduleList.size();
  165. for( U32 i = 0; i < numModules; ++ i )
  166. {
  167. const bool thisBeforeCurrent = module->_constrainedToComeBefore( moduleList[ i ], mode );
  168. const bool currentAfterThis = moduleList[ i ]->_constrainedToComeAfter( module, mode );
  169. AssertFatal( !( thisBeforeCurrent && currentAfterThis ),
  170. avar( "EngineModuleManager::_insertModuleIntoList - Ambiguous %s placement of module '%s' relative to '%s'",
  171. mode == Module::ModeInitialize ? "init" : "shutdown",
  172. module->getName(), moduleList[ i ]->getName() ) );
  173. // If no contraints relate us to this module,
  174. // push us one more position back in the line.
  175. if( !thisBeforeCurrent && !currentAfterThis )
  176. continue;
  177. // If this module is contrained to come before the
  178. // module at our current position but that module does
  179. // not actually have dependencies of its own, make sure
  180. // that module is at the back of the module list so that
  181. // if we have more dependencies, it will not prevent us
  182. // from correctly positioning us in relation to them.
  183. if( thisBeforeCurrent && !moduleList[ i ]->_getDependencies( mode ) && i != numModules - 1 )
  184. {
  185. #if defined( DEBUG_SPEW ) && DEBUG_SPEW_LEVEL > 1
  186. Platform::outputDebugString( "[EngineModuleManager] Pushing '%s' to back end of chain for resolving '%s'",
  187. moduleList[ i ]->getName(), module->getName() );
  188. #endif
  189. Module* depModule = moduleList[ i ];
  190. moduleList.erase( i );
  191. -- i;
  192. moduleList.push_back( depModule );
  193. continue;
  194. }
  195. // Try the reverse constraint with all remaining modules in the list.
  196. // If there is one for which we have one, then the placement of this
  197. // module is ambiguous.
  198. for( U32 n = i + 1; n < numModules; ++ n )
  199. AssertFatal( !( moduleList[ n ]->_constrainedToComeBefore( module, mode )
  200. || module->_constrainedToComeAfter( moduleList[ n ], mode ) ),
  201. avar( "EngineModuleManager::_insertModuleIntoList - Ambiguous %s constraint on module '%s' to come before '%s' yet after '%s'",
  202. mode == Module::ModeInitialize ? "init" : "shutdown",
  203. module->getName(),
  204. moduleList[ i ]->getName(),
  205. moduleList[ n ]->getName() ) );
  206. // Add the module at this position.
  207. #if defined( DEBUG_SPEW ) && DEBUG_SPEW_LEVEL > 1
  208. Platform::outputDebugString( "[EngineModuleManager] Inserting '%s' at index %i into '%s'",
  209. module->getName(), i, _moduleListToString( moduleList ).c_str() );
  210. #endif
  211. moduleList.insert( i, module );
  212. return;
  213. }
  214. // No constraint-based position. Just append.
  215. #if defined( DEBUG_SPEW ) && DEBUG_SPEW_LEVEL > 1
  216. Platform::outputDebugString( "[EngineModuleManager] Appending '%s' to '%s'",
  217. module->getName(), _moduleListToString( moduleList ).c_str() );
  218. #endif
  219. moduleList.push_back( module );
  220. }
  221. //-----------------------------------------------------------------------------
  222. Module* EngineModuleManager::_findOverrideFor( Module* module )
  223. {
  224. const char* name = module->getName();
  225. for( Module* ptr = Module::smFirst; ptr != NULL; ptr = ptr->mNext )
  226. for( Module::Override* override = ptr->mOverrides; override != NULL; override = override->mNext )
  227. if( dStricmp( override->mModuleName, name ) == 0 )
  228. return ptr;
  229. return NULL;
  230. }
  231. //-----------------------------------------------------------------------------
  232. S32 EngineModuleManager::_getIndexOfModuleInList( Vector< Module* >& moduleList, Module* module )
  233. {
  234. const U32 numModules = moduleList.size();
  235. for( U32 i = 0; i < numModules; ++ i )
  236. if( moduleList[ i ] == module )
  237. return i;
  238. return -1;
  239. }
  240. //-----------------------------------------------------------------------------
  241. S32 EngineModuleManager::_getIndexOfModuleInList( Vector< Module* >& moduleList, const char* moduleName )
  242. {
  243. const U32 numModules = moduleList.size();
  244. for( U32 i = 0; i < numModules; ++ i )
  245. if( dStricmp( moduleList[ i ]->getName(), moduleName ) == 0 )
  246. return i;
  247. return -1;
  248. }
  249. //-----------------------------------------------------------------------------
  250. void EngineModuleManager::_createModuleList( Module::Mode mode, Vector< Module* >& moduleList )
  251. {
  252. for( Module* module = Module::smFirst; module != NULL; module = module->mNext )
  253. _insertIntoModuleList( mode, moduleList, module );
  254. }
  255. //-----------------------------------------------------------------------------
  256. void EngineModuleManager::initializeSystem()
  257. {
  258. Vector< Module* > modules;
  259. _createModuleList( Module::ModeInitialize, modules );
  260. const U32 numModules = modules.size();
  261. for( U32 i = 0; i < numModules; ++ i )
  262. {
  263. Module* module = modules[ i ];
  264. if( !module->mIsInitialized )
  265. {
  266. #ifdef DEBUG_SPEW
  267. Platform::outputDebugString( "[EngineModuleManager] Initializing %s",
  268. module->getName() );
  269. #endif
  270. module->initialize();
  271. module->mIsInitialized = true;
  272. }
  273. }
  274. }
  275. //-----------------------------------------------------------------------------
  276. void EngineModuleManager::shutdownSystem()
  277. {
  278. Vector< Module* > modules;
  279. _createModuleList( Module::ModeShutdown, modules );
  280. const U32 numModules = modules.size();
  281. for( U32 i = 0; i < numModules; ++ i )
  282. {
  283. if( modules[ i ]->mIsInitialized )
  284. {
  285. #ifdef DEBUG_SPEW
  286. Platform::outputDebugString( "[EngineModuleManager] Shutting down %s",
  287. modules[ i ]->getName() );
  288. #endif
  289. modules[ i ]->shutdown();
  290. modules[ i ]->mIsInitialized = false;
  291. }
  292. }
  293. }
  294. //-----------------------------------------------------------------------------
  295. Module* EngineModuleManager::findModule( const char* name )
  296. {
  297. for( Module* module = Module::smFirst; module != NULL; module = module->mNext )
  298. if( dStricmp( module->getName(), name ) == 0 )
  299. return module;
  300. return NULL;
  301. }