-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNMF_Modding.h
242 lines (187 loc) · 6.02 KB
/
NMF_Modding.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#pragma once
#ifndef NMF_VERSION
#include "NMF.h"
#endif
#ifdef NMF_USE_MODDING
#include <map>
#include <stdint.h>
#include <string>
namespace NMF
{
class ModBase;
class NMF_EXPORT ModuleBase
{
public:
ModuleBase(ModBase* owner, const char* moduleName)
: Owner(owner), ModuleName(moduleName)
{
}
virtual ~ModuleBase() {}
virtual void OnAttach() {}
virtual void OnBeforeStart(const char* commandLine) {}
virtual WndProcHandleResult WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { return WndProcHandleResult::Continue; }
#ifdef NMF_USE_IMGUI
virtual void OnImGuiDraw(ImGuiDrawTarget target) {}
#endif
const char* GetName() const { return ModuleName; }
ModBase* GetOwner() const { return Owner; }
private:
ModBase* Owner;
const char* ModuleName;
};
class NMF_EXPORT ModBase
{
public:
ModBase(const char* modName)
: ModName(modName)
{
}
virtual ~ModBase() {}
virtual void OnAttach()
{
for (const auto& module : Modules)
module.second->OnAttach();
}
virtual void OnBeforeStart(const char* commandLine)
{
for (const auto& module : Modules)
module.second->OnBeforeStart(commandLine);
}
virtual WndProcHandleResult WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
WndProcHandleResult result;
for (const auto& module : Modules)
{
result = module.second->WndProc(hWnd, uMsg, wParam, lParam);
if (result != WndProcHandleResult::Continue)
return result;
}
return WndProcHandleResult::Continue;
}
#ifdef NMF_USE_IMGUI
virtual void OnImGuiDraw(ImGuiDrawTarget target)
{
for (const auto& module : Modules)
module.second->OnImGuiDraw(target);
}
#endif
const char* GetName() const { return ModName; }
virtual bool RegisterModule(ModuleBase* module)
{
#ifdef NMF_USE_LOGGING
BaseLogger.Log(LogSeverity::Debug, "Registering module: %s in mod: %s!", module->GetName(), GetName());
#endif
std::string moduleName(module->GetName());
auto itr = Modules.find(moduleName);
if (itr != Modules.end())
{
#ifdef NMF_USE_LOGGING
BaseLogger.Log(LogSeverity::Error, "A module under the name %s has already been registered! Skipping...", module->GetName());
#endif
return false;
}
Modules[moduleName] = module;
#ifdef NMF_USE_LOGGING
BaseLogger.Log(LogSeverity::Debug, "Module: %s in mod: %s has been successfully registered!", module->GetName(), GetName());
#endif
return true;
}
virtual ModuleBase* GetModule(const char* moduleNameRaw) const
{
std::string moduleName{ moduleNameRaw };
auto itr = Modules.find(moduleName);
if (itr != Modules.end())
return itr->second;
return nullptr;
}
void ForEach(std::function<void(ModuleBase*)> func)
{
for (const auto& module : Modules)
func(module.second);
}
private:
const char* ModName;
#ifdef NMF_USE_LOGGING
static Logger BaseLogger;
#endif
#pragma warning(disable: 4251)
std::map<std::string, ModuleBase*> Modules;
#pragma warning(default: 4251)
};
class NMF_EXPORT ModManagerBase
{
public:
static ModManagerBase* Instance;
ModManagerBase()
{
if (Instance == nullptr)
Instance = this;
}
virtual ~ModManagerBase() {}
virtual bool RegisterMod(ModBase* mod)
{
#ifdef NMF_USE_LOGGING
BaseLogger.Log(LogSeverity::Debug, "Registering mod: %s...", mod->GetName());
#endif
std::string modName(mod->GetName());
auto itr = Mods.find(modName);
if (itr != Mods.end())
{
#ifdef NMF_USE_LOGGING
BaseLogger.Log(LogSeverity::Error, "A mod under the name %s has already been registered! Skipping...", mod->GetName());
#endif
return false;
}
Mods[modName] = mod;
#ifdef NMF_USE_LOGGING
BaseLogger.Log(LogSeverity::Debug, "Mod: %s has been succesfully registered!", mod->GetName());
#endif
mod->OnAttach();
return true;
}
virtual ModBase* GetMod(const char* modNameRaw)
{
std::string modName{ modNameRaw };
auto itr = Mods.find(modName);
if (itr != Mods.end())
return itr->second;
return nullptr;
}
virtual void BeforeStart(HINSTANCE hInstance, HINSTANCE hPrevInstance, const char* lpCmdLine, int nShowCmd)
{
for (const auto& mod : Mods)
mod.second->OnBeforeStart(lpCmdLine);
}
virtual WndProcHandleResult WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
WndProcHandleResult result;
for (const auto& mod : Mods)
{
result = mod.second->WndProc(hWnd, uMsg, wParam, lParam);
if (result != WndProcHandleResult::Continue)
return result;
}
return WndProcHandleResult::Continue;
}
void ForEach(std::function<void(ModBase*)> func)
{
for (const auto& mod : Mods)
func(mod.second);
}
#ifdef NMF_USE_IMGUI
virtual void OnImGuiDraw(ImGuiDrawTarget target)
{
for (const auto& mod : Mods)
mod.second->OnImGuiDraw(target);
}
#endif
private:
#ifdef NMF_USE_LOGGING
static Logger BaseLogger;
#endif
#pragma warning(disable: 4251)
std::map<std::string, ModBase*> Mods;
#pragma warning(default: 4251)
};
}
#endif