]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modmanager_static.cpp
Allow opermotd to specify its file in <files> without also requiring an <opermotd...
[user/henk/code/inspircd.git] / src / modmanager_static.cpp
index e09310a13bf66fde0823082094565e38540baa39..d9ee07a257a7190754c299a76e4986b9da42da6e 100644 (file)
@@ -58,28 +58,78 @@ class AllModule : public Module
 
 MODULE_INIT(AllModule)
 
-bool ModuleManager::Load(const char* filename)
+bool ModuleManager::Load(const char* name)
 {
+       for(std::vector<AllModuleList*>::iterator i = modlist->begin(); i != modlist->end(); ++i)
+       {
+               if ((**i).name == name)
+               {
+                       Module* c = NULL;
+                       try
+                       {
+                               c = (*(**i).init)();
+                               Modules[name] = c;
+                               c->init();
+                               FOREACH_MOD(I_OnLoadModule,OnLoadModule(c));
+                               return true;
+                       }
+                       catch (CoreException& modexcept)
+                       {
+                               if (c)
+                                       DoSafeUnload(c);
+                               delete c;
+                               ServerInstance->Logs->Log("MODULE", DEFAULT, "Unable to load " + (**i).name + ": " + modexcept.GetReason());
+                       }
+               }
+       }
        return false;
 }
 
-bool ModuleManager::CanUnload(Module* mod)
-{
-       return false;
-}
+namespace {
+       struct UnloadAction : public HandlerBase0<void>
+       {
+               Module* const mod;
+               UnloadAction(Module* m) : mod(m) {}
+               void Call()
+               {
+                       ServerInstance->Modules->DoSafeUnload(mod);
+                       ServerInstance->GlobalCulls.Apply();
+                       ServerInstance->GlobalCulls.AddItem(this);
+               }
+       };
 
-void ModuleManager::DoSafeUnload(Module* mod)
-{
+       struct ReloadAction : public HandlerBase0<void>
+       {
+               Module* const mod;
+               HandlerBase1<void, bool>* const callback;
+               ReloadAction(Module* m, HandlerBase1<void, bool>* c)
+                       : mod(m), callback(c) {}
+               void Call()
+               {
+                       std::string name = mod->ModuleSourceFile;
+                       ServerInstance->Modules->DoSafeUnload(mod);
+                       ServerInstance->GlobalCulls.Apply();
+                       bool rv = ServerInstance->Modules->Load(name.c_str());
+                       callback->Call(rv);
+                       ServerInstance->GlobalCulls.AddItem(this);
+               }
+       };
 }
 
 bool ModuleManager::Unload(Module* mod)
 {
-       return false;
+       if (!CanUnload(mod))
+               return false;
+       ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
+       return true;
 }
 
 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
 {
-       callback->Call(false);
+       if (CanUnload(mod))
+               ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
+       else
+               callback->Call(false);
 }
 
 void ModuleManager::LoadAll()
@@ -87,15 +137,21 @@ void ModuleManager::LoadAll()
        ModCount = 0;
        for(std::vector<AllModuleList*>::iterator i = modlist->begin(); i != modlist->end(); ++i)
        {
+               Module* c = NULL;
                try
                {
-                       Module* c = (*(**i).init)();
+                       c = (*(**i).init)();
                        c->ModuleSourceFile = (**i).name;
+                       c->ModuleDLLManager = NULL;
                        Modules[(**i).name] = c;
+                       c->init();
                        FOREACH_MOD(I_OnLoadModule,OnLoadModule(c));
                }
                catch (CoreException& modexcept)
                {
+                       if (c)
+                               DoSafeUnload(c);
+                       delete c;
                        ServerInstance->Logs->Log("MODULE", DEFAULT, "Unable to load " + (**i).name + ": " + modexcept.GetReason());
                }
        }