-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModuleManager.cpp
173 lines (143 loc) · 5.83 KB
/
ModuleManager.cpp
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
#ifndef _SCRIPT_PAD_MODULE_HANDLER_CPP
#define _SCRIPT_PAD_MODULE_HANDLER_CPP
#include "ModuleManager.h"
//#include "Node.h"
namespace querier {
Module::Module() {};
Module::Module(std::string _name, path _path, std::string _lib, LanguageType _type) : Name(_name), Path(_path), Library(_lib), Type(_type) {
}
ModuleManager::ModuleManager(AppWindow* mainWin) : m_MainWindow(mainWin) {
}
ModuleManager::~ModuleManager() {
CleanupLanguageModules();
CleanupLoadedModules();
m_MainWindow = nullptr;
}
void ModuleManager::Initialize(path workingDirectory, path modulesDirectory) {
WorkingDirectory = workingDirectory;
ModulesDirectory = modulesDirectory;
load_Modules();
}
bool ModuleManager::load_Modules() {
std::vector<path> mods = Directory::GetDirectories(ModulesDirectory);
for (auto it = mods.begin(); it < mods.end(); it++) {
path moduleDirectory = ModulesDirectory / path(*it);
path moduleData = moduleDirectory / path("data");
path moduleFile = moduleDirectory / path(it->wstring() + L".dll");
path moduleConfig = moduleDirectory / path("module_config.json");
if (File::Exists(moduleFile)) {
if (File::Exists(moduleConfig)) {
json sd = json::parse(File::ReadAllText(moduleConfig));
Module* mod = new Module(sd.template get<Module>());
mod->Path = moduleFile;
mod->Data.Path = moduleData;
mod->Data.Library = mod->Data.Path / mod->Library;
mod->Data.SourceFile = std::string(s_DefaultSourceFileName + mod->SourceFileExtension);
LoadModule(mod->Name, mod->Path, mod->Data.SourceFile, mod->Data.Library, &mod->LanguageModule);
Modules.insert({ it->string(), mod});
}
}
}
return Modules.size() > 0;
}
bool ModuleManager::LoadModule(std::string moduleName, path modulePath, path querySourceFile, path libFilePath, LanguageModule** langModule) {
HINSTANCE hLib;
DLL ProcAddr{ 0 };
BOOL fRunTimeLinkSuccess = FALSE;
hLib = LoadLibrary(modulePath.c_str());
if (hLib != NULL) {
ProcAddr = (DLL)GetProcAddress(hLib, "CreateModule");
if (NULL != ProcAddr) {
fRunTimeLinkSuccess = TRUE;
m_LoadedModules.insert({ moduleName, new LoadedModule{moduleName, modulePath, hLib, ProcAddr } });
*langModule = (LanguageModule*)ProcAddr(libFilePath);
(*langModule)->OnOutputReceivedQ = std::bind(&ModuleManager::HandleOutputReceived, this, std::placeholders::_1, std::placeholders::_2);
(*langModule)->OnErrorReceivedQ = std::bind(&ModuleManager::HandleErrorReceived, this, std::placeholders::_1, std::placeholders::_2);
}
}
return fRunTimeLinkSuccess;
}
void ModuleManager::HandleOutputReceived(std::string ret, std::string queryName) {
ApplicationMessage* response = new ApplicationMessage{
MODULE,
{ ret, RESULT_MODULE, SUCCESS_RESULT, queryName }
};
PostApplicationMessage(response);
delete response;
}
void ModuleManager::HandleErrorReceived(std::string ret, std::string queryName) {
ApplicationMessage* response = new ApplicationMessage{
MODULE,
{ ret, RESULT_MODULE, ERROR_RESULT, queryName }
};
PostApplicationMessage(response);
delete response;
}
void ModuleManager::PostApplicationMessage(ApplicationMessage *msg) {
json* pjson = new json(*msg);
PostMessage(m_MainWindow->get_MainWindow(), WM_WEBVIEW, reinterpret_cast<WPARAM>(pjson), NULL);
}
json ModuleManager::get_ModulesAsJson() {
return json(get_Modules());
}
std::vector<Module> ModuleManager::get_Modules() {
std::vector<Module> vec;
for (auto iterator : Modules) {
vec.push_back(*iterator.second);
}
return vec;
}
void ModuleManager::HandleCommand(ModuleMessage* msg) {
switch (msg->cmd) {
break;
case CONFIG_MODULE:
break;
case CODESYNC_MODULE:
break;
case GET_MODULES:
msg->content = get_ModulesAsJson().dump();
break;
}
}
void ModuleManager::CleanupLoadedModules() {
if (m_LoadedModules.size() > 0) {
for (auto it : m_LoadedModules) {
if (it.second != nullptr) {
LoadedModule* lm = it.second;
if (FreeLibrary(lm->hLibrary)) {
delete lm;
}
it.second = nullptr;
}
}
}
}
void ModuleManager::CleanupLanguageModules() {
if (Modules.size() > 0) {
for (auto it : Modules) {
if (it.second->LanguageModule != nullptr) {
delete it.second->LanguageModule;
it.second->LanguageModule = nullptr;
}
}
}
}
std::map<std::wstring, path> ModuleManager::m_wModules;
std::map<std::string, path> ModuleManager::m_Modules;
std::map<std::string, Module*> ModuleManager::m_mModules;
void to_json(json& j, const Module& m) {
j = json{
{ "name", m.Name },
{ "sourceFileExtension", m.SourceFileExtension },
{ "library", m.Library },
{ "type", m.Type }
};
}
void from_json(const json& j, Module& m) {
j.at("name").get_to(m.Name);
j.at("sourceFileExtension").get_to(m.SourceFileExtension);
j.at("library").get_to(m.Library);
j.at("type").get_to(m.Type);
}
}
#endif