5 static std::vector<AllCommandList::fn>* cmdlist = NULL;
6 static std::vector<AllModuleList*>* modlist = NULL;
8 AllCommandList::AllCommandList(fn cmd)
11 cmdlist = new std::vector<AllCommandList::fn>();
12 cmdlist->push_back(cmd);
15 AllModuleList::AllModuleList(AllModuleList::fn mod, const std::string& Name) : init(mod), name(Name)
18 modlist = new std::vector<AllModuleList*>();
19 modlist->push_back(this);
22 class AllModule : public Module
24 std::vector<Command*> cmds;
32 cmds.reserve(cmdlist->size());
33 for(std::vector<AllCommandList::fn>::iterator i = cmdlist->begin(); i != cmdlist->end(); ++i)
35 Command* c = (*i)(this);
37 ServerInstance->AddCommand(c);
42 this->AllModule::~AllModule();
49 for(std::vector<Command*>::iterator i = cmds.begin(); i != cmds.end(); ++i)
55 return Version("All commands", VF_VENDOR|VF_CORE);
59 MODULE_INIT(AllModule)
61 bool ModuleManager::Load(const char* name)
63 for(std::vector<AllModuleList*>::iterator i = modlist->begin(); i != modlist->end(); ++i)
65 if ((**i).name == name)
69 Module* c = (*(**i).init)();
71 FOREACH_MOD(I_OnLoadModule,OnLoadModule(c));
74 catch (CoreException& modexcept)
76 ServerInstance->Logs->Log("MODULE", DEFAULT, "Unable to load " + (**i).name + ": " + modexcept.GetReason());
83 bool ModuleManager::Unload(Module* mod)
89 struct ReloadAction : public HandlerBase0<void>
92 HandlerBase1<void, bool>* const callback;
93 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
94 : mod(m), callback(c) {}
97 std::string name = mod->ModuleSourceFile;
98 ServerInstance->Modules->DoSafeUnload(mod);
99 ServerInstance->GlobalCulls.Apply();
100 bool rv = ServerInstance->Modules->Load(name.c_str());
102 ServerInstance->GlobalCulls.AddItem(this);
107 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
110 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
112 callback->Call(false);
115 void ModuleManager::LoadAll()
118 for(std::vector<AllModuleList*>::iterator i = modlist->begin(); i != modlist->end(); ++i)
122 Module* c = (*(**i).init)();
123 c->ModuleSourceFile = (**i).name;
124 Modules[(**i).name] = c;
125 FOREACH_MOD(I_OnLoadModule,OnLoadModule(c));
127 catch (CoreException& modexcept)
129 ServerInstance->Logs->Log("MODULE", DEFAULT, "Unable to load " + (**i).name + ": " + modexcept.GetReason());
133 /* We give every module a chance to re-prioritize when we introduce a new one,
134 * not just the one thats loading, as the new module could affect the preference
137 for(int tries = 0; tries < 20; tries++)
139 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
140 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
141 n->second->Prioritize();
143 if (prioritizationState == PRIO_STATE_LAST)
146 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected");
149 ServerInstance->BuildISupport();
152 void ModuleManager::UnloadAll()
154 // TODO don't really need this, who cares if we leak on exit?