]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modmanager_static.cpp
Change more modules to VF_OPTCOMMON and assert identical charsets in m_nationalchars
[user/henk/code/inspircd.git] / src / modmanager_static.cpp
1 #include "inspircd.h"
2
3 #ifdef PURE_STATIC
4
5 static std::vector<AllCommandList::fn>* cmdlist = NULL;
6 static std::vector<AllModuleList*>* modlist = NULL;
7
8 AllCommandList::AllCommandList(fn cmd)
9 {
10         if (!cmdlist)
11                 cmdlist = new std::vector<AllCommandList::fn>();
12         cmdlist->push_back(cmd);
13 }
14
15 AllModuleList::AllModuleList(AllModuleList::fn mod, const std::string& Name) : init(mod), name(Name)
16 {
17         if (!modlist)
18                 modlist = new std::vector<AllModuleList*>();
19         modlist->push_back(this);
20 }
21
22 class AllModule : public Module
23 {
24         std::vector<Command*> cmds;
25  public:
26         AllModule()
27         {
28                 if (!cmdlist)
29                         return;
30                 try
31                 {
32                         cmds.reserve(cmdlist->size());
33                         for(std::vector<AllCommandList::fn>::iterator i = cmdlist->begin(); i != cmdlist->end(); ++i)
34                         {
35                                 Command* c = (*i)(this);
36                                 cmds.push_back(c);
37                                 ServerInstance->AddCommand(c);
38                         }
39                 }
40                 catch (...)
41                 {
42                         this->AllModule::~AllModule();
43                         throw;
44                 }
45         }
46
47         ~AllModule()
48         {
49                 for(std::vector<Command*>::iterator i = cmds.begin(); i != cmds.end(); ++i)
50                         delete *i;
51         }
52
53         Version GetVersion()
54         {
55                 return Version("All commands", VF_VENDOR|VF_CORE);
56         }
57 };
58
59 MODULE_INIT(AllModule)
60
61 bool ModuleManager::Load(const char* name)
62 {
63         for(std::vector<AllModuleList*>::iterator i = modlist->begin(); i != modlist->end(); ++i)
64         {
65                 if ((**i).name == name)
66                 {
67                         try
68                         {
69                                 Module* c = (*(**i).init)();
70                                 Modules[name] = c;
71                                 FOREACH_MOD(I_OnLoadModule,OnLoadModule(c));
72                                 return true;
73                         }
74                         catch (CoreException& modexcept)
75                         {
76                                 ServerInstance->Logs->Log("MODULE", DEFAULT, "Unable to load " + (**i).name + ": " + modexcept.GetReason());
77                         }
78                 }
79         }
80         return false;
81 }
82
83 bool ModuleManager::Unload(Module* mod)
84 {
85         return false;
86 }
87
88 namespace {
89         struct ReloadAction : public HandlerBase0<void>
90         {
91                 Module* const mod;
92                 HandlerBase1<void, bool>* const callback;
93                 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
94                         : mod(m), callback(c) {}
95                 void Call()
96                 {
97                         std::string name = mod->ModuleSourceFile;
98                         ServerInstance->Modules->DoSafeUnload(mod);
99                         ServerInstance->GlobalCulls.Apply();
100                         bool rv = ServerInstance->Modules->Load(name.c_str());
101                         callback->Call(rv);
102                         ServerInstance->GlobalCulls.AddItem(this);
103                 }
104         };
105 }
106
107 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
108 {
109         if (CanUnload(mod))
110                 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
111         else
112                 callback->Call(false);
113 }
114
115 void ModuleManager::LoadAll()
116 {
117         ModCount = 0;
118         for(std::vector<AllModuleList*>::iterator i = modlist->begin(); i != modlist->end(); ++i)
119         {
120                 try
121                 {
122                         Module* c = (*(**i).init)();
123                         c->ModuleSourceFile = (**i).name;
124                         Modules[(**i).name] = c;
125                         FOREACH_MOD(I_OnLoadModule,OnLoadModule(c));
126                 }
127                 catch (CoreException& modexcept)
128                 {
129                         ServerInstance->Logs->Log("MODULE", DEFAULT, "Unable to load " + (**i).name + ": " + modexcept.GetReason());
130                 }
131         }
132
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
135          * of others
136          */
137         for(int tries = 0; tries < 20; tries++)
138         {
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();
142
143                 if (prioritizationState == PRIO_STATE_LAST)
144                         break;
145                 if (tries == 19)
146                         ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected");
147         }
148
149         ServerInstance->BuildISupport();
150 }
151
152 void ModuleManager::UnloadAll()
153 {
154         // TODO don't really need this, who cares if we leak on exit?
155 }
156
157 #endif