-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathPluginFactory.cpp
207 lines (172 loc) · 5.46 KB
/
PluginFactory.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
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
/*
* PluginFactory.cpp
*
* Copyright (c) 2015 Lukas W <lukaswhl/at/gmail.com>
*
* This file is part of LMMS - https://lmms.io
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#include "PluginFactory.h"
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QLibrary>
#include "ConfigManager.h"
#ifdef LMMS_BUILD_WIN32
QStringList nameFilters("*.dll");
#else
QStringList nameFilters("lib*.so");
#endif
qint64 qHash(const QFileInfo& fi)
{
return qHash(fi.absoluteFilePath());
}
PluginFactory* PluginFactory::s_instance = nullptr;
PluginFactory::PluginFactory()
{
// Adds a search path relative to the main executable if the path exists.
auto addRelativeIfExists = [this] (const QString& path) {
QDir dir(qApp->applicationDirPath());
if (!path.isEmpty() && dir.cd(path)) {
QDir::addSearchPath("plugins", dir.absolutePath());
}
};
// We're either running LMMS installed on an Unixoid or we're running a
// portable version like we do on Windows.
// We want to find our plugins in both cases:
// (a) Installed (Unix):
// e.g. binary at /usr/bin/lmms - plugin dir at /usr/lib/lmms/
// (b) Portable:
// e.g. binary at "C:/Program Files/LMMS/lmms.exe"
// plugins at "C:/Program Files/LMMS/plugins/"
#ifndef LMMS_BUILD_WIN32
addRelativeIfExists("../lib/lmms"); // Installed
#endif
addRelativeIfExists("plugins"); // Portable
#ifdef PLUGIN_DIR // We may also have received a relative directory via a define
addRelativeIfExists(PLUGIN_DIR);
#endif
// Or via an environment variable:
QString env_path;
if (!(env_path = qgetenv("LMMS_PLUGIN_DIR")).isEmpty())
QDir::addSearchPath("plugins", env_path);
QDir::addSearchPath("plugins", ConfigManager::inst()->workingDir() + "plugins");
discoverPlugins();
}
PluginFactory::~PluginFactory()
{
}
PluginFactory* PluginFactory::instance()
{
if (s_instance == nullptr)
s_instance = new PluginFactory();
return s_instance;
}
const Plugin::DescriptorList PluginFactory::descriptors() const
{
return m_descriptors.values();
}
const Plugin::DescriptorList PluginFactory::descriptors(Plugin::PluginTypes type) const
{
return m_descriptors.values(type);
}
const PluginFactory::PluginInfoList& PluginFactory::pluginInfos() const
{
return m_pluginInfos;
}
const PluginFactory::PluginInfo PluginFactory::pluginSupportingExtension(const QString& ext)
{
PluginInfo* info = m_pluginByExt.value(ext, nullptr);
return info == nullptr ? PluginInfo() : *info;
}
const PluginFactory::PluginInfo PluginFactory::pluginInfo(const char* name) const
{
for (const PluginInfo* info : m_pluginInfos)
{
if (qstrcmp(info->descriptor->name, name) == 0)
return *info;
}
return PluginInfo();
}
QString PluginFactory::errorString(QString pluginName) const
{
static QString notfound = qApp->translate("PluginFactory", "Plugin not found.");
return m_errors.value(pluginName, notfound);
}
void PluginFactory::discoverPlugins()
{
DescriptorMap descriptors;
PluginInfoList pluginInfos;
m_pluginByExt.clear();
QSet<QFileInfo> files;
for (const QString& searchPath : QDir::searchPaths("plugins"))
{
files.unite(QDir(searchPath).entryInfoList(nameFilters).toSet());
}
// Cheap dependency handling: zynaddsubfx needs ZynAddSubFxCore. By loading
// all libraries twice we ensure that libZynAddSubFxCore is found.
for (const QFileInfo& file : files)
{
QLibrary(file.absoluteFilePath()).load();
}
for (const QFileInfo& file : files)
{
QLibrary* library = new QLibrary(file.absoluteFilePath());
if (! library->load()) {
m_errors[file.baseName()] = library->errorString();
qWarning("%s", library->errorString().toLocal8Bit().data());
continue;
}
if (library->resolve("lmms_plugin_main") == nullptr) {
continue;
}
QString descriptorName = file.baseName() + "_plugin_descriptor";
if( descriptorName.left(3) == "lib" )
{
descriptorName = descriptorName.mid(3);
}
Plugin::Descriptor* pluginDescriptor = (Plugin::Descriptor*) library->resolve(descriptorName.toUtf8().constData());
if(pluginDescriptor == nullptr)
{
qWarning() << qApp->translate("PluginFactory", "LMMS plugin %1 does not have a plugin descriptor named %2!").
arg(file.absoluteFilePath()).arg(descriptorName);
continue;
}
PluginInfo* info = new PluginInfo;
info->file = file;
info->library = library;
info->descriptor = pluginDescriptor;
pluginInfos << info;
for (const QString& ext : QString(info->descriptor->supportedFileTypes).split(','))
{
m_pluginByExt.insert(ext, info);
}
descriptors.insert(info->descriptor->type, info->descriptor);
}
for (PluginInfo* info : m_pluginInfos)
{
delete info->library;
delete info;
}
m_pluginInfos = pluginInfos;
m_descriptors = descriptors;
}
const QString PluginFactory::PluginInfo::name() const
{
return descriptor ? descriptor->name : QString();
}