2024-04-12 14:36:37 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
|
|
|
|
2024-05-02 14:33:21 +02:00
|
|
|
#include "luaengine.h"
|
2024-05-06 11:54:53 +02:00
|
|
|
#include "luapluginspec.h"
|
2024-04-12 14:36:37 +02:00
|
|
|
|
2024-05-16 12:56:07 +02:00
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
#include <coreplugin/jsexpander.h>
|
2024-05-06 11:54:53 +02:00
|
|
|
#include <coreplugin/messagemanager.h>
|
2024-04-12 14:36:37 +02:00
|
|
|
|
|
|
|
#include <extensionsystem/iplugin.h>
|
|
|
|
#include <extensionsystem/pluginmanager.h>
|
|
|
|
|
|
|
|
#include <utils/algorithm.h>
|
|
|
|
|
|
|
|
#include <QDebug>
|
2024-05-06 11:54:53 +02:00
|
|
|
|
|
|
|
using namespace Core;
|
|
|
|
using namespace Utils;
|
|
|
|
using namespace ExtensionSystem;
|
2024-04-12 14:36:37 +02:00
|
|
|
|
|
|
|
namespace Lua::Internal {
|
|
|
|
|
|
|
|
void addAsyncModule();
|
|
|
|
void addFetchModule();
|
|
|
|
void addActionModule();
|
|
|
|
void addUtilsModule();
|
|
|
|
void addMessageManagerModule();
|
|
|
|
void addProcessModule();
|
|
|
|
void addSettingsModule();
|
2024-05-30 16:35:45 +02:00
|
|
|
void addGuiModule();
|
2024-04-12 14:36:37 +02:00
|
|
|
void addQtModule();
|
|
|
|
void addCoreModule();
|
|
|
|
void addHookModule();
|
2024-05-14 13:47:50 +02:00
|
|
|
void addInstallModule();
|
2024-04-12 14:36:37 +02:00
|
|
|
|
2024-05-16 12:56:07 +02:00
|
|
|
class LuaJsExtension : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit LuaJsExtension(QObject *parent = nullptr)
|
|
|
|
: QObject(parent)
|
|
|
|
{}
|
|
|
|
|
|
|
|
Q_INVOKABLE QString metaFolder() const
|
|
|
|
{
|
|
|
|
return Core::ICore::resourcePath("lua/meta").toFSPathString();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-05-30 14:32:34 +02:00
|
|
|
struct Arguments
|
|
|
|
{
|
|
|
|
std::optional<FilePath> loadPlugin;
|
|
|
|
};
|
|
|
|
|
|
|
|
Arguments parseArguments(const QStringList &arguments)
|
|
|
|
{
|
|
|
|
Arguments args;
|
|
|
|
for (int i = 0; i < arguments.size() - 1; ++i) {
|
|
|
|
if (arguments.at(i) == QLatin1String("-loadluaplugin")) {
|
|
|
|
const QString path(arguments.at(i + 1));
|
|
|
|
args.loadPlugin = FilePath::fromUserInput(path);
|
|
|
|
i++; // skip the argument
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2024-05-06 11:54:53 +02:00
|
|
|
class LuaPlugin : public IPlugin
|
2024-04-12 14:36:37 +02:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "Lua.json")
|
|
|
|
|
2024-05-02 14:33:21 +02:00
|
|
|
private:
|
|
|
|
std::unique_ptr<LuaEngine> m_luaEngine;
|
2024-05-30 14:32:34 +02:00
|
|
|
Arguments m_arguments;
|
2024-05-02 14:33:21 +02:00
|
|
|
|
2024-04-12 14:36:37 +02:00
|
|
|
public:
|
2024-05-06 11:54:53 +02:00
|
|
|
LuaPlugin() {}
|
2024-04-12 14:36:37 +02:00
|
|
|
~LuaPlugin() override = default;
|
|
|
|
|
2024-05-30 14:32:34 +02:00
|
|
|
bool initialize(const QStringList &arguments, QString *) final
|
2024-04-12 14:36:37 +02:00
|
|
|
{
|
2024-05-06 11:54:53 +02:00
|
|
|
m_luaEngine.reset(new LuaEngine());
|
|
|
|
|
2024-05-30 14:32:34 +02:00
|
|
|
m_arguments = parseArguments(arguments);
|
|
|
|
|
2024-04-12 14:36:37 +02:00
|
|
|
addAsyncModule();
|
|
|
|
addFetchModule();
|
|
|
|
addActionModule();
|
|
|
|
addUtilsModule();
|
|
|
|
addMessageManagerModule();
|
|
|
|
addProcessModule();
|
|
|
|
addSettingsModule();
|
2024-05-30 16:35:45 +02:00
|
|
|
addGuiModule();
|
2024-04-12 14:36:37 +02:00
|
|
|
addQtModule();
|
|
|
|
addCoreModule();
|
|
|
|
addHookModule();
|
2024-05-14 13:47:50 +02:00
|
|
|
addInstallModule();
|
2024-05-16 12:56:07 +02:00
|
|
|
|
|
|
|
Core::JsExpander::registerGlobalObject("Lua", [] { return new LuaJsExtension(); });
|
2024-05-30 14:32:34 +02:00
|
|
|
|
|
|
|
return true;
|
2024-04-12 14:36:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool delayedInitialize() final
|
|
|
|
{
|
2024-06-13 10:52:43 +02:00
|
|
|
scanForPlugins(PluginManager::pluginPaths());
|
2024-04-12 14:36:37 +02:00
|
|
|
return true;
|
|
|
|
}
|
2024-05-06 11:54:53 +02:00
|
|
|
|
|
|
|
void scanForPlugins(const FilePaths &paths)
|
|
|
|
{
|
|
|
|
QSet<PluginSpec *> plugins;
|
|
|
|
for (const FilePath &path : paths) {
|
2024-05-30 14:32:34 +02:00
|
|
|
FilePaths folders = path.dirEntries(FileFilter({}, QDir::Dirs | QDir::NoDotAndDotDot));
|
2024-05-06 11:54:53 +02:00
|
|
|
|
|
|
|
for (const FilePath &folder : folders) {
|
|
|
|
const FilePath script = folder / (folder.baseName() + ".lua");
|
|
|
|
const expected_str<LuaPluginSpec *> result = m_luaEngine->loadPlugin(script);
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
qWarning() << "Failed to load plugin" << script << ":" << result.error();
|
|
|
|
MessageManager::writeFlashing(tr("Failed to load plugin %1: %2")
|
|
|
|
.arg(script.toUserOutput())
|
|
|
|
.arg(result.error()));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
plugins.insert(*result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-30 14:32:34 +02:00
|
|
|
if (m_arguments.loadPlugin) {
|
|
|
|
const FilePath folder = *m_arguments.loadPlugin;
|
|
|
|
const FilePath script = folder / (folder.baseName() + ".lua");
|
|
|
|
const expected_str<LuaPluginSpec *> result = m_luaEngine->loadPlugin(script);
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
qWarning() << "Failed to load plugin" << script << ":" << result.error();
|
|
|
|
MessageManager::writeFlashing(tr("Failed to load plugin %1: %2")
|
|
|
|
.arg(script.toUserOutput())
|
|
|
|
.arg(result.error()));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
(*result)->setEnabledBySettings(true);
|
|
|
|
plugins.insert(*result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-06 11:54:53 +02:00
|
|
|
PluginManager::addPlugins({plugins.begin(), plugins.end()});
|
|
|
|
PluginManager::loadPluginsAtRuntime(plugins);
|
|
|
|
}
|
2024-04-12 14:36:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Lua::Internal
|
|
|
|
|
|
|
|
#include "luaplugin.moc"
|