Browse Source

- Added the ability to unregister a module. This isn't used very often but has been used in the past for game-project templated modules.

MelvMay-GG 12 years ago
parent
commit
21255aa

+ 64 - 5
engine/source/module/moduleManager.cc

@@ -1836,6 +1836,39 @@ bool ModuleManager::removeModuleDefinition( ModuleDefinition* pModuleDefinition
         // Delete module definition.
         pModuleDefinition->deleteObject();
 
+        // Are there any modules left for this module Id?
+        if ( findModuleId( moduleId ) == NULL )
+        {
+            bool moduleIdFound = false;
+
+            // No, so remove from groups.
+            for( typeGroupModuleHash::iterator moduleGroupItr = mGroupModules.begin(); moduleGroupItr != mGroupModules.end(); ++moduleGroupItr )
+            {
+                // Fetch module Ids.
+                typeModuleIdVector* pModuleIds = moduleGroupItr->value;
+
+                // Iterate module Id.
+                for( typeModuleIdVector::iterator moduleIdItr = pModuleIds->begin(); moduleIdItr != pModuleIds->end(); ++moduleIdItr )
+                {
+                    // Skip if this isn't the Id.
+                    if ( *moduleIdItr != moduleId )
+                        continue;
+
+                    // Remove the module Id.
+                    pModuleIds->erase( moduleIdItr );
+
+                    // Flag as found.
+                    moduleIdFound = true;
+
+                    break;
+                }
+
+                // Finish if found.
+                if ( moduleIdFound )
+                    break;
+            }
+        }
+
         return true;
     }
 
@@ -2039,11 +2072,12 @@ bool ModuleManager::registerModule( const char* pModulePath, const char* pModule
         for( typeModuleIdVector::iterator moduleIdItr = pModuleIds->begin(); moduleIdItr != pModuleIds->end(); ++moduleIdItr )
         {
             // Skip if this isn't the Id.
-            if ( *moduleIdItr == moduleId )
-            {
-                moduleIdFound = true;
-                break;
-            }
+            if ( *moduleIdItr != moduleId )
+                continue;
+
+            // Flag as found.
+            moduleIdFound = true;
+            break;
         }
 
         // Add if module Id was not found.
@@ -2100,6 +2134,31 @@ bool ModuleManager::registerModule( const char* pModulePath, const char* pModule
 
 //-----------------------------------------------------------------------------
 
+bool ModuleManager::unregisterModule( const char* pModuleId, const U32 versionId )
+{
+    // Sanity!
+    AssertFatal( pModuleId != NULL, "A module Id cannot be NULL." );
+
+    // Fetch module Id.
+    StringTableEntry moduleId = StringTable->insert( pModuleId );
+
+    // Find the module definition.
+    ModuleDefinition* pModuleDefinition = findModule( pModuleId, versionId );
+
+    // Did we find the module definition?
+    if ( pModuleDefinition == NULL )
+    {
+        // No, so warn.
+        Con::warnf( "Module Manager: Cannot unregister module Id '%s' as it is not registered.", moduleId );
+        return false;
+    }
+
+    // Remove the module definition.
+    return removeModuleDefinition( pModuleDefinition );
+}
+
+//-----------------------------------------------------------------------------
+
 void ModuleManager::raiseModulePreLoadNotifications( ModuleDefinition* pModuleDefinition )
 {
     // Raise notifications.

+ 3 - 0
engine/source/module/moduleManager.h

@@ -152,6 +152,9 @@ public:
     /// Module discovery.
     bool scanModules( const char* pPath, const bool rootOnly = false );
 
+    /// Module unregister.
+    bool unregisterModule( const char* pModuleId, const U32 versionId );
+
     /// Module (un)loading.
     bool loadModuleGroup( const char* pModuleGroup );
     bool unloadModuleGroup( const char* pModuleGroup );

+ 17 - 0
engine/source/module/moduleManager_ScriptBinding.h

@@ -54,6 +54,23 @@ ConsoleMethod(ModuleManager, scanModules, bool, 3, 4,   "(moduleRootPath, [rootO
 
 //-----------------------------------------------------------------------------
 
+ConsoleMethod(ModuleManager, unregisterModule, bool, 4, 4,  "(moduleId, versionId) - Unregister the specified module.\n"
+                                                            "@param moduleId The module Id to unregister.\n"
+                                                            "@param versionId The version Id to unregister.\n"
+                                                            "@return Whether the module was unregister or not.")
+{
+    // Fetch the module Id.
+    const char* pModuleId = argv[2];
+
+    // Fetch the version Id.
+    const U32 versionId = (U32)dAtoi(argv[3]);
+
+    // Unregister the module.
+    return object->unregisterModule( pModuleId, versionId );
+}
+
+//-----------------------------------------------------------------------------
+
 ConsoleMethod(ModuleManager, loadGroup, bool, 3, 3,     "(moduleGroup) - Load the specified module group.\n"
                                                         "@param moduleGroup The module group to load.\n"
                                                         "@return Whether the module group was loaded or not.")