-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChainLoader.cpp
More file actions
129 lines (113 loc) · 3.42 KB
/
ChainLoader.cpp
File metadata and controls
129 lines (113 loc) · 3.42 KB
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
#include "ChainLoader.h"
#include <filesystem>
#include <queue>
#include <fstream>
#include <string_view>
using std::string;
using std::vector;
using std::string_view_literals::operator""sv;
// look for libraries with these prefixes and extensions.
// .ini files are expected to contain a single line with the path to the library to load, which allows for loading libraries with non-standard names or from non-standard locations
// note that if both a .ini and a .dll/.so are present _both_ will be loaded, which may have unintended consequences if they point to the same library, so use with caution
constexpr auto search_prefix = {"dfhooks_"sv, "libdfhooks_"sv};
constexpr auto search_ext = {".dll"sv, ".so"sv};
constexpr auto search_ext_ini = ".ini"sv;
ChainLoader::ChainLoader() {
auto cmp = [](const LibWrapper * lhs, const LibWrapper * rhs) { return lhs->priority < rhs->priority; };
std::priority_queue<LibWrapper*, vector<LibWrapper*>, decltype(cmp)> priq(cmp);
for (auto const& dir_entry : std::filesystem::directory_iterator{"."})
{
auto fname = dir_entry.path().filename();
bool fnd = false;
for (auto& prefix : search_prefix) {
if (fname.stem().string().find(prefix) == 0) {
fnd = true;
break;
}
}
if (!fnd) continue;
if (fname.extension() == search_ext_ini)
{
std::string indirection;
std::ifstream ini_file{fname};
std::getline(ini_file, indirection);
if (!indirection.empty())
{
std::filesystem::path lib{indirection};
auto wrapper = new LibWrapper(lib);
if (wrapper->handle)
priq.emplace(wrapper);
else
delete wrapper;
}
}
else for (auto& ext : search_ext)
{
if (fname.extension() == ext)
{
auto wrapper = new LibWrapper(fname);
if (wrapper->handle)
priq.emplace(wrapper);
else
delete wrapper;
}
}
}
while (!priq.empty()) {
auto lib = priq.top();
libs.emplace_back(lib);
priq.pop();
}
}
ChainLoader::~ChainLoader() {
for (auto & lib : libs)
delete lib;
libs.clear();
}
void ChainLoader::init() {
for (auto & lib : libs) {
if (lib->init)
lib->init();
}
}
void ChainLoader::shutdown() {
// shut down in reverse order
for (auto libit = libs.rbegin(); libit != libs.rend(); ++libit) {
if ((*libit)->shutdown)
(*libit)->shutdown();
}
}
void ChainLoader::update() {
for (auto & lib : libs) {
if (lib->update)
lib->update();
}
}
void ChainLoader::prerender() {
for (auto & lib : libs) {
if (lib->prerender)
lib->prerender();
}
}
bool ChainLoader::sdl_event(SDL_Event* event) {
for (auto & lib : libs) {
if (lib->sdl_event)
if (lib->sdl_event(event))
return true;
}
return false;
}
void ChainLoader::sdl_loop() {
for (auto & lib : libs) {
if (lib->sdl_loop)
lib->sdl_loop();
}
}
bool ChainLoader::ncurses_key(int key) {
for (auto & lib : libs) {
if (lib->ncurses_key)
if (lib->ncurses_key(key))
return true;
}
return false;
}