moduleManager_ScriptBinding.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 "console/engineAPI.h"
  23. #include "moduleDefinition.h"
  24. #include "moduleManager.h"
  25. DefineEngineMethod(ModuleManager, setModuleExtension, bool, (const char* moduleExtension), (""),
  26. "Set the module extension used to scan for modules. The default is 'module'.\n"
  27. "@param moduleExtension The module extension used to scan for modules.Do not use a period character.\n"
  28. "@return Whether setting the module extension was successful or not.\n")
  29. {
  30. // Set module extension.
  31. return object->setModuleExtension(moduleExtension);
  32. }
  33. //-----------------------------------------------------------------------------
  34. DefineEngineMethod(ModuleManager, scanModules, bool, (const char* pRootPath, bool rootOnly), ("", false),
  35. "Scans for modules which are sub-directories of the specified path.\n"
  36. "@param moduleRootPath The root directory to scan for sub - directories containing modules.\n"
  37. "@param rootOnly[Optional] - Specifies whether to only scan the root path or not when searching for modules.\n"
  38. "@return Whether the scan was successful or not.A successful scan can still find zero modules.\n")
  39. {
  40. // Scan modules.
  41. return object->scanModules( pRootPath, rootOnly );
  42. }
  43. //-----------------------------------------------------------------------------
  44. DefineEngineMethod(ModuleManager, registerModule, bool, (const char* pModulePath, const char* pModuleFile), ("", ""),
  45. "Register the specified module.\n"
  46. "@param moduleId The module Id to register.\n"
  47. "@param versionId The version Id to register.\n"
  48. "@return Whether the module was registered or not.\n")
  49. {
  50. // Unregister the module.
  51. return object->registerModule(pModulePath, pModuleFile);
  52. }
  53. DefineEngineMethod(ModuleManager, unregisterModule, bool, (const char* pModuleId, bool versionId), ("", false),
  54. "Unregister the specified module.\n"
  55. "@param moduleId The module Id to unregister.\n"
  56. "@param versionId The version Id to unregister.\n"
  57. "@return Whether the module was unregister or not.\n")
  58. {
  59. // Unregister the module.
  60. return object->unregisterModule( pModuleId, versionId );
  61. }
  62. //-----------------------------------------------------------------------------
  63. DefineEngineMethod(ModuleManager, loadGroup, bool, (const char* pModuleGroup), (""),
  64. "Load the specified module group.\n"
  65. "@param moduleGroup The module group to load.\n"
  66. "@return Whether the module group was loaded or not.\n")
  67. {
  68. // Load module group.
  69. return object->loadModuleGroup(pModuleGroup);
  70. }
  71. //-----------------------------------------------------------------------------
  72. DefineEngineMethod(ModuleManager, unloadGroup, bool, (const char* pModuleGroup), (""),
  73. "Unload the specified module group.\n"
  74. "@param moduleGroup The module group to unload.\n"
  75. "@return Whether the module group was unloaded or not.\n")
  76. {
  77. // Unload module group.
  78. return object->unloadModuleGroup(pModuleGroup);
  79. }
  80. //-----------------------------------------------------------------------------
  81. DefineEngineMethod(ModuleManager, loadExplicit, bool, (const char* pModuleId, S32 pVersionId), ("", -1),
  82. "Load the specified module explicitly.\n"
  83. "@param moduleId The module Id to load.\n"
  84. "@param versionId The version Id to load.Optional: Will load the latest version.\n"
  85. "@return Whether the module Id was loaded or not.\n")
  86. {
  87. if (pVersionId == -1)
  88. return object->loadModuleExplicit(pModuleId);
  89. else
  90. return object->loadModuleExplicit(pModuleId, pVersionId);
  91. }
  92. //-----------------------------------------------------------------------------
  93. DefineEngineMethod(ModuleManager, unloadExplicit, bool, (const char* pModuleId), (""),
  94. "Unload the specified module explicitly.\n"
  95. "@param moduleId The module Id to unload.\n"
  96. "@return Whether the module Id was unloaded or not.\n")
  97. {
  98. // Unload module Id explicitly.
  99. return object->unloadModuleExplicit(pModuleId);
  100. }
  101. //-----------------------------------------------------------------------------
  102. DefineEngineMethod(ModuleManager, findModule, String, (const char* pModuleId, U32 pVersionId), ("", 1),
  103. "Find the specific module Id optionally at the specified version Id.\n"
  104. "@param moduleId The module Id to find.\n"
  105. "@param versionId The version Id to find.\n"
  106. "@return The module definition object or NULL if not found.\n")
  107. {
  108. // Find module definition.
  109. ModuleDefinition* pModuleDefinition = object->findModule(pModuleId, pVersionId);
  110. // Return nothing if not found.
  111. if ( pModuleDefinition == NULL )
  112. return StringTable->EmptyString();
  113. return pModuleDefinition->getIdString();
  114. }
  115. //-----------------------------------------------------------------------------
  116. DefineEngineMethod(ModuleManager, findModuleByFilePath, String, (const char* filePath), (""),
  117. "Find the specific module Id optionally at the specified version Id.\n"
  118. "@param moduleId The module Id to find.\n"
  119. "@param versionId The version Id to find.\n"
  120. "@return The module definition object or NULL if not found.\n")
  121. {
  122. // Find module definition.
  123. ModuleDefinition* pModuleDefinition = object->findModuleByFilePath(StringTable->insert(filePath));
  124. // Return nothing if not found.
  125. if (pModuleDefinition == NULL)
  126. return StringTable->EmptyString();
  127. return pModuleDefinition->getIdString();
  128. }
  129. //-----------------------------------------------------------------------------
  130. DefineEngineMethod(ModuleManager, findModules, String, (bool loadedOnly), (false),
  131. "Find all the modules registered with the specified loaded state.\n"
  132. "@param loadedOnly Whether to return only modules that are loaded or not.\n"
  133. "@return A list of space - separated module definition object Ids.\n")
  134. {
  135. // Find module type definitions.
  136. Vector<const ModuleDefinition*> moduleDefinitions;
  137. // Find modules.
  138. object->findModules( loadedOnly, moduleDefinitions );
  139. // Fetch module definition count.
  140. const U32 moduleDefinitionCount = (U32)moduleDefinitions.size();
  141. // Finish if no module definition were found.
  142. if ( moduleDefinitionCount == 0 )
  143. return StringTable->EmptyString();
  144. // Create a return buffer.
  145. S32 bufferSize = 4096;
  146. char* pReturnBuffer = Con::getReturnBuffer( bufferSize );
  147. char* pBufferWrite = pReturnBuffer;
  148. // Iterate module definitions.
  149. for ( ModuleManager::typeConstModuleDefinitionVector::const_iterator moduleDefinitionItr = moduleDefinitions.begin(); moduleDefinitionItr != moduleDefinitions.end(); ++moduleDefinitionItr )
  150. {
  151. // Fetch module definition.
  152. const ModuleDefinition* pModuleDefinition = *moduleDefinitionItr;
  153. // Format module definition.
  154. const U32 offset = dSprintf( pBufferWrite, bufferSize, "%d ", pModuleDefinition->getId() );
  155. pBufferWrite += offset;
  156. bufferSize -= offset;
  157. // Are we out of buffer space?
  158. if ( bufferSize <= 0 )
  159. {
  160. // Yes, so warn.
  161. Con::warnf( "ModuleManager::findModules() - Ran out of buffer space." );
  162. break;
  163. }
  164. }
  165. return pReturnBuffer;
  166. }
  167. //-----------------------------------------------------------------------------
  168. DefineEngineMethod(ModuleManager, findModuleTypes, String, (const char* pModuleType, bool loadedOnly), ("", false),
  169. "Find the modules registered with the specified module type.\n"
  170. "@param moduleType The module type to search for.\n"
  171. "@param loadedOnly Whether to return only modules that are loaded or not.\n"
  172. "@return A list of space - separated module definition object Ids.\n")
  173. {
  174. // Find module type definitions.
  175. Vector<const ModuleDefinition*> moduleDefinitions;
  176. // Find module types.
  177. object->findModuleTypes( pModuleType, loadedOnly, moduleDefinitions );
  178. // Fetch module definition count.
  179. const U32 moduleDefinitionCount = (U32)moduleDefinitions.size();
  180. // Finish if no module definition were found.
  181. if ( moduleDefinitionCount == 0 )
  182. return StringTable->EmptyString();
  183. // Create a return buffer.
  184. S32 bufferSize = 4096;
  185. char* pReturnBuffer = Con::getReturnBuffer( bufferSize );
  186. char* pBufferWrite = pReturnBuffer;
  187. // Iterate module definitions.
  188. for ( ModuleManager::typeConstModuleDefinitionVector::const_iterator moduleDefinitionItr = moduleDefinitions.begin(); moduleDefinitionItr != moduleDefinitions.end(); ++moduleDefinitionItr )
  189. {
  190. // Fetch module definition.
  191. const ModuleDefinition* pModuleDefinition = *moduleDefinitionItr;
  192. // Format module definition.
  193. const U32 offset = dSprintf( pBufferWrite, bufferSize, "%d ", pModuleDefinition->getId() );
  194. pBufferWrite += offset;
  195. bufferSize -= offset;
  196. // Are we out of buffer space?
  197. if ( bufferSize <= 0 )
  198. {
  199. // Yes, so warn.
  200. Con::warnf( "ModuleManager::findTypes() - Ran out of buffer space." );
  201. break;
  202. }
  203. }
  204. return pReturnBuffer;
  205. }
  206. //-----------------------------------------------------------------------------
  207. DefineEngineMethod(ModuleManager, copyModule, String, (const char* sourceModuleDefinition, const char* pTargetModuleId, const char* pTargetPath, const bool useVersionPathing),
  208. ("", "", "", false),
  209. "Copy the module to a new location with a new module Id.\n"
  210. "@param sourceModuleDefinition The module definition to copy.\n"
  211. "@param targetModuleId The module Id to rename the copied module to including all references to the source module Id.It is valid to specifiy the source module Id to produce an identical copy.\n"
  212. "@param targetPath The target path to copy the module to.Addition folders will be created depending on whether 'useVersionPathing' is used or not.\n"
  213. "@param useVersionPathing Whether to add a '/targetModuleId/versionId' folder to the target path or not.This allows copying multiple versions of the same module Id.\n"
  214. "@return The new module definition file if copy was successful or NULL if not.\n")
  215. {
  216. // Find the source module definition.
  217. ModuleDefinition* pSourceModuleDefinition = dynamic_cast<ModuleDefinition*>(Sim::findObject(sourceModuleDefinition));
  218. // Was the module definition found?
  219. if ( pSourceModuleDefinition == NULL )
  220. {
  221. // No, so warn.
  222. Con::warnf("ModuleManager::copyModule() - Could not find source module definition '%s'.", sourceModuleDefinition);
  223. return "";
  224. }
  225. // Copy module.
  226. return object->copyModule( pSourceModuleDefinition, pTargetModuleId, pTargetPath, useVersionPathing );
  227. }
  228. //-----------------------------------------------------------------------------
  229. DefineEngineMethod(ModuleManager, renameModule, bool, (const char* sourceModuleDefinition, const char* pNewModuleName),
  230. ("", ""),
  231. "Rename a module.\n"
  232. "@param sourceModuleDefinition The module definition to rename.\n"
  233. "@param pNewModuleName The new name the module should have.\n"
  234. "@return Weither the rename was successful or not.\n")
  235. {
  236. // Find the source module definition.
  237. ModuleDefinition* pSourceModuleDefinition = dynamic_cast<ModuleDefinition*>(Sim::findObject(sourceModuleDefinition));
  238. // Was the module definition found?
  239. if (pSourceModuleDefinition == NULL)
  240. {
  241. // No, so warn.
  242. Con::warnf("ModuleManager::renameModule() - Could not find source module definition '%s'.", sourceModuleDefinition);
  243. return "";
  244. }
  245. // Copy module.
  246. return object->renameModule(pSourceModuleDefinition, pNewModuleName);
  247. }
  248. //-----------------------------------------------------------------------------
  249. DefineEngineMethod(ModuleManager, synchronizeDependencies, bool, (const char* rootModuleDefinition, const char* pTargetDependencyFolder), ("", ""),
  250. "Synchronize the module dependencies of a module definition to a target dependency folder.\n"
  251. "@param rootModuleDefinition The module definition used to determine dependencies.\n"
  252. "@param targetDependencyPath The target dependency folder to copy dependencies to.\n"
  253. "@return Whether the module dependencies were synchronized correctly or not.\n")
  254. {
  255. // Find the root module definition.
  256. ModuleDefinition* pRootModuleDefinition = dynamic_cast<ModuleDefinition*>(Sim::findObject(rootModuleDefinition));
  257. // Was the module definition found?
  258. if ( pRootModuleDefinition == NULL )
  259. {
  260. // No, so warn.
  261. Con::warnf("ModuleManager::synchronizeModules() - Could not find root module definition '%s'.", rootModuleDefinition);
  262. return false;
  263. }
  264. // Synchronize dependencies.
  265. return object->synchronizeDependencies( pRootModuleDefinition, pTargetDependencyFolder );
  266. }
  267. //-----------------------------------------------------------------------------
  268. DefineEngineMethod(ModuleManager, isModuleMergeAvailable, bool, (),,
  269. "Checks whether a module merge definition file is available or not.\n"
  270. "@return Whether a module merge definition file is available or not.\n")
  271. {
  272. // Check if module merge is available or not.
  273. return object->isModuleMergeAvailable();
  274. }
  275. //-----------------------------------------------------------------------------
  276. DefineEngineMethod(ModuleManager, canMergeModules, bool, (const char* mergeSourcePath), (""),
  277. "Checks whether a module merge using the modules in the source path can current happen or not.\n"
  278. "@param mergeSourcePath The path where modules to be merged are located.\n"
  279. "@return Whether a module merge using the modules in the source path can current happen or not.\n")
  280. {
  281. // Check whether the merge modules can current happen or not.
  282. return object->canMergeModules(mergeSourcePath);
  283. }
  284. //-----------------------------------------------------------------------------
  285. DefineEngineMethod(ModuleManager, mergeModules, bool, (const char* pMergeTargetPath, bool removeMergeDefinition, bool registerNewModules), ("", false, false),
  286. "Performs a module merge into the selected target path.\n"
  287. "@param mergeTargetPath The path where modules will be merged into.\n"
  288. "@param removeMergeDefinition Whether to remove any merge definition found or not if merge is successful.\n"
  289. "@param registerNewModules Whether new (not replaced or updated) modules should be registered or not.\n"
  290. "@return Whether the module merge was successful or not.Failure here could result in a corrupt module state.Reinstall is recommended or at least advised to the user is recommended.\n")
  291. {
  292. // Merge modules.
  293. return object->mergeModules( pMergeTargetPath, removeMergeDefinition, registerNewModules );
  294. }
  295. //-----------------------------------------------------------------------------
  296. DefineEngineMethod(ModuleManager, addListener, void, (const char* listenerObject), (""),
  297. "Registers the specified object as a listener for module notifications.\n"
  298. "@param listenerObject The object to start receiving module notifications.\n"
  299. "@return No return value.\n")
  300. {
  301. // Find object.
  302. SimObject* pListener = Sim::findObject(listenerObject);
  303. // Did we find the listener object?
  304. if ( pListener == NULL )
  305. {
  306. // No, so warn.
  307. Con::warnf("ModuleManager::addNotifications() - Could not find the listener object '%s'.", listenerObject);
  308. return;
  309. }
  310. object->addListener( pListener );
  311. }
  312. //-----------------------------------------------------------------------------
  313. DefineEngineMethod(ModuleManager, removeListener, void, (const char* listenerObject), (""),
  314. "Unregisters the specified object as a listener for module notifications.\n"
  315. "@param listenerObject The object to stop receiving module notifications.\n"
  316. "@return No return value.\n")
  317. {
  318. // Find object.
  319. SimObject* pListener = Sim::findObject(listenerObject);
  320. // Did we find the listener object?
  321. if ( pListener == NULL )
  322. {
  323. // No, so warn.
  324. Con::warnf("ModuleManager::removeNotifications() - Could not find the listener object '%s'.", listenerObject);
  325. return;
  326. }
  327. object->removeListener( pListener );
  328. }
  329. //-----------------------------------------------------------------------------
  330. DefineEngineMethod(ModuleManager, ignoreLoadedGroups, void, (bool doIgnore), (false),
  331. "Sets if the Module Manager should ingore loaded groups.\n"
  332. "@param doIgnore Whether we should or should not ignore loaded groups.\n"
  333. "@return No return value.\n")
  334. {
  335. // Check whether the merge modules can current happen or not.
  336. return object->setIgnoreLoadedGroups(doIgnore);
  337. }