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
|
/* Copyright (c) 2015 Stanislaw Halik
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*/
#pragma once
#include "plugin-api.hpp"
#include <memory>
#include <algorithm>
#include <QDebug>
#include <QString>
#include <QLibrary>
#include <QList>
#include <QDir>
#include <QList>
#include <QIcon>
#if defined(__APPLE__)
# define OPENTRACK_SOLIB_EXT "dylib"
#elif defined(_WIN32)
# define OPENTRACK_SOLIB_EXT "dll"
#else
# define OPENTRACK_SOLIB_EXT "so"
#endif
#ifdef _MSC_VER
# define OPENTRACK_SOLIB_PREFIX ""
#else
# define OPENTRACK_SOLIB_PREFIX "lib"
#endif
extern "C" typedef void* (*OPENTRACK_CTOR_FUNPTR)(void);
extern "C" typedef Metadata* (*OPENTRACK_METADATA_FUNPTR)(void);
struct dylib final
{
enum Type { Filter = 0xdeadbabe, Tracker = 0xcafebeef, Protocol = 0xdeadf00d, Invalid = 0xcafebabe };
dylib(const QString& filename, Type t) :
type(Invalid),
filename(filename),
Dialog(nullptr),
Constructor(nullptr),
Meta(nullptr)
{
// otherwise dlopen opens the calling executable
if (filename.size() == 0)
return;
handle.setFileName(filename);
handle.setLoadHints(QLibrary::DeepBindHint | QLibrary::ResolveAllSymbolsHint);
if (check(!handle.load()))
return;
if (check((Dialog = (OPENTRACK_CTOR_FUNPTR) handle.resolve("GetDialog"), !Dialog)))
return;
if (check((Constructor = (OPENTRACK_CTOR_FUNPTR) handle.resolve("GetConstructor"), !Constructor)))
return;
if (check((Meta = (OPENTRACK_METADATA_FUNPTR) handle.resolve("GetMetadata"), !Meta)))
return;
auto m = std::unique_ptr<Metadata>(Meta());
icon = m->icon();
name = m->name();
type = t;
}
~dylib()
{
// QLibrary refcounts the .dll's so don't forcefully unload
}
static QList<std::shared_ptr<dylib>> enum_libraries(const QString& library_path)
{
QDir module_directory(library_path);
QList<std::shared_ptr<dylib>> ret;
static const struct filter_ {
Type type;
QString glob;
} filters[] = {
{ Filter, OPENTRACK_SOLIB_PREFIX "opentrack-filter-*." OPENTRACK_SOLIB_EXT, },
{ Tracker, OPENTRACK_SOLIB_PREFIX "opentrack-tracker-*." OPENTRACK_SOLIB_EXT, },
{ Protocol, OPENTRACK_SOLIB_PREFIX "opentrack-proto-*." OPENTRACK_SOLIB_EXT, },
};
for (const filter_& filter : filters)
{
for (const QString& filename : module_directory.entryList({ filter.glob }, QDir::Files, QDir::Name))
{
std::shared_ptr<dylib> lib = std::make_shared<dylib>(QStringLiteral("%1/%2").arg(library_path).arg(filename), filter.type);
if (lib->type == Invalid)
{
qDebug() << "can't load dylib" << filename;
continue;
}
if (std::any_of(ret.cbegin(),
ret.cend(),
[&lib](const std::shared_ptr<dylib>& a) {
return a->type == lib->type && a->name == lib->name;
}))
{
qDebug() << "duplicate lib" << filename << "ident" << lib->name;
continue;
}
ret.push_back(lib);
}
}
return ret;
}
Type type;
QString filename;
QIcon icon;
QString name;
OPENTRACK_CTOR_FUNPTR Dialog;
OPENTRACK_CTOR_FUNPTR Constructor;
OPENTRACK_METADATA_FUNPTR Meta;
private:
QLibrary handle;
bool check(bool fail)
{
if (fail)
{
if (handle.isLoaded())
(void) handle.unload();
Constructor = nullptr;
Dialog = nullptr;
Meta = nullptr;
type = Invalid;
}
return fail;
}
};
struct Modules final
{
using dylib_ptr = std::shared_ptr<dylib>;
using dylib_list = QList<dylib_ptr>;
Modules(const QString& library_path) :
module_list(dylib::enum_libraries(library_path)),
filter_modules(filter(dylib::Filter)),
tracker_modules(filter(dylib::Tracker)),
protocol_modules(filter(dylib::Protocol))
{}
dylib_list& filters() { return filter_modules; }
dylib_list& trackers() { return tracker_modules; }
dylib_list& protocols() { return protocol_modules; }
private:
dylib_list module_list;
dylib_list filter_modules;
dylib_list tracker_modules;
dylib_list protocol_modules;
static dylib_list& sorted(dylib_list& xs)
{
std::sort(xs.begin(), xs.end(), [&](const dylib_ptr& a, const dylib_ptr& b) { return a->name.toLower() < b->name.toLower(); });
return xs;
}
dylib_list filter(dylib::Type t)
{
QList<std::shared_ptr<dylib>> ret;
for (auto x : module_list)
if (x->type == t)
ret.push_back(x);
return sorted(ret);
}
};
template<typename t>
static inline std::shared_ptr<t> make_dylib_instance(const std::shared_ptr<dylib>& lib)
{
std::shared_ptr<t> ret;
if (lib != nullptr && lib->Constructor)
ret = std::shared_ptr<t>(reinterpret_cast<t*>(reinterpret_cast<OPENTRACK_CTOR_FUNPTR>(lib->Constructor)()));
return ret;
}
|