Restructure plugin system to be more portable

This commit is contained in:
Gary Talent 2018-02-16 00:41:00 -06:00
parent 2b8b5661eb
commit c2c87e0363
9 changed files with 57 additions and 35 deletions

View File

@ -57,7 +57,7 @@ else()
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(NOSTALGIA_DIST_BIN bin)
set(NOSTALGIA_DIST_LIB lib)
set(NOSTALGIA_DIST_PLUGIN lib)
set(NOSTALGIA_DIST_PLUGIN lib/nostalgia/plugins)
set(NOSTALGIA_DIST_RESOURCES share)
endif()

View File

@ -20,7 +20,7 @@ install(
TARGETS
NostalgiaCore-Studio
LIBRARY DESTINATION
${NOSTALGIA_DIST_PLUGIN}/nostalgia
${NOSTALGIA_DIST_PLUGIN}
)
install(

View File

@ -0,0 +1,3 @@
{
"plugin_name": "Nostalgia Core"
}

View File

@ -17,7 +17,7 @@ namespace core {
class Plugin: public QObject, studio::Plugin {
Q_OBJECT
Q_PLUGIN_METADATA(IID "net.drinkingtea.nostalgia.studio.Plugin")
Q_PLUGIN_METADATA(IID "net.drinkingtea.nostalgia.studio.Plugin" FILE "core-studio.json")
Q_INTERFACES(nostalgia::studio::Plugin)
public:

View File

@ -73,30 +73,48 @@ MainWindow::~MainWindow() {
}
void MainWindow::loadPlugins() {
for (auto p : m_profile.plugins) {
#if defined(Q_OS_WIN)
auto libName = p.libName + ".dll";
#elif defined(Q_OS_MAC)
auto libName = "lib" + p.libName + ".dylib";
#else
auto libName = "lib" + p.libName + ".so";
#endif
auto pluginPath = QFileInfo(m_profilePath).absolutePath() + "/" + p.dir + "/" + libName;
auto loader = new QPluginLoader(pluginPath);
auto plugin = qobject_cast<Plugin*>(loader->instance());
if (plugin) {
m_cleanupTasks.push_back([loader]() {
loader->unload();
delete loader;
});
m_plugins.push_back(plugin);
} else {
qInfo() << loader->errorString();
for (auto dir : m_profile.pluginsPath) {
QFileInfo dirInfo(m_profilePath);
dir = dirInfo.absolutePath() + "/" + dir;
if (dirInfo.exists()) {
qDebug() << "Checking plugin directory:" << dir;
loadPluginDir(dir);
}
}
}
void MainWindow::loadPluginDir(QString dir) {
#if defined(Q_OS_WIN)
constexpr auto libSuffix = ".dll";
#elif defined(Q_OS_MAC)
constexpr auto libSuffix = ".dylib";
#else
constexpr auto libSuffix = ".so";
#endif
for (auto pluginPath : QDir(dir).entryList()) {
pluginPath = dir + '/' + pluginPath;
if (pluginPath.endsWith(libSuffix)) {
loadPlugin(pluginPath);
}
}
}
void MainWindow::loadPlugin(QString pluginPath) {
qDebug() << "Loading plugin:" << pluginPath;
auto loader = new QPluginLoader(pluginPath);
auto plugin = qobject_cast<Plugin*>(loader->instance());
if (plugin) {
m_cleanupTasks.push_back([loader]() {
loader->unload();
delete loader;
});
m_plugins.push_back(plugin);
} else {
qInfo() << loader->errorString();
}
}
void MainWindow::setupMenu() {
auto menu = menuBar();
auto fileMenu = menu->addMenu(tr("&File"));

View File

@ -61,7 +61,7 @@ ox::Error ioOp(T *io, NostalgiaStudioPluginDef *obj) {
struct NostalgiaStudioProfile {
QString appName;
QString orgName;
QVector<NostalgiaStudioPluginDef> plugins;
QVector<QString> pluginsPath;
};
template<typename T>
@ -70,7 +70,7 @@ ox::Error ioOp(T *io, NostalgiaStudioProfile *obj) {
io->setFields(3);
err |= io->op("app_name", &obj->appName);
err |= io->op("org_name", &obj->orgName);
err |= io->op("plugins", &obj->plugins);
err |= io->op("plugins_path", &obj->pluginsPath);
return err;
}
@ -104,6 +104,10 @@ class MainWindow: public QMainWindow {
private:
void loadPlugins();
void loadPluginDir(QString path);
void loadPlugin(QString path);
void setupDockWidgets();
void setupMenu();

View File

@ -1,14 +1,8 @@
{
"app_name": "Nostalgia Studio",
"org_name": "Drinking Tea",
"plugins": [
{
"dir": "../lib/nostalgia",
"lib_name": "NostalgiaCore-Studio"
},
{
"dir": "../lib/nostalgia",
"lib_name": "NostalgiaWorld-Studio"
}
"plugins_path": [
"../lib/nostalgia/plugins",
"../Plugins"
]
}

View File

@ -18,5 +18,5 @@ install(
TARGETS
NostalgiaWorld-Studio
LIBRARY DESTINATION
${NOSTALGIA_DIST_PLUGIN}/nostalgia
${NOSTALGIA_DIST_PLUGIN}
)

View File

@ -0,0 +1,3 @@
{
"plugin_name": "Nostalgia World"
}