+namespace
+{
+ struct UnloadAction : public ActionBase
+ {
+ Module* const mod;
+ UnloadAction(Module* m) : mod(m) {}
+ void Call() CXX11_OVERRIDE
+ {
+ DLLManager* dll = mod->ModuleDLLManager;
+ ServerInstance->Modules->DoSafeUnload(mod);
+ ServerInstance->GlobalCulls.Apply();
+ // In pure static mode this is always NULL
+ delete dll;
+ ServerInstance->GlobalCulls.AddItem(this);
+ }
+ };
+}
+
+bool ModuleManager::Unload(Module* mod)
+{
+ if (!CanUnload(mod))
+ return false;
+ ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
+ return true;
+}
+
+void ModuleManager::LoadAll()
+{
+ std::map<std::string, ServiceList> servicemap;
+ LoadCoreModules(servicemap);
+
+ ConfigTagList tags = ServerInstance->Config->ConfTags("module");
+ for (ConfigIter i = tags.first; i != tags.second; ++i)
+ {
+ ConfigTag* tag = i->second;
+ std::string name = ExpandModName(tag->getString("name"));
+ this->NewServices = &servicemap[name];
+
+ // Skip modules which are already loaded.
+ if (Modules.find(name) != Modules.end())
+ continue;
+
+ std::cout << "[" << con_green << "*" << con_reset << "] Loading module:\t" << con_green << name << con_reset << std::endl;
+ if (!this->Load(name, true))
+ {
+ ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, this->LastError());
+ std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << this->LastError() << std::endl << std::endl;
+ ServerInstance->Exit(EXIT_STATUS_MODULE);
+ }
+ }
+
+ ConfigStatus confstatus;
+
+ for (ModuleMap::const_iterator i = Modules.begin(); i != Modules.end(); ++i)
+ {
+ Module* mod = i->second;
+ try
+ {
+ ServerInstance->Logs->Log("MODULE", LOG_DEBUG, "Initializing %s", i->first.c_str());
+ AttachAll(mod);
+ AddServices(servicemap[i->first]);
+ mod->init();
+ mod->ReadConfig(confstatus);
+ }
+ catch (CoreException& modexcept)
+ {
+ LastModuleError = "Unable to initialize " + mod->ModuleSourceFile + ": " + modexcept.GetReason();
+ ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
+ std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << LastModuleError << std::endl << std::endl;
+ ServerInstance->Exit(EXIT_STATUS_MODULE);
+ }
+ }
+
+ this->NewServices = NULL;
+
+ if (!PrioritizeHooks())
+ ServerInstance->Exit(EXIT_STATUS_MODULE);
+}
+