From a61bc4d6d14016e1524558206bc004e8f061f6c7 Mon Sep 17 00:00:00 2001 From: Mm2PL Date: Wed, 1 Feb 2023 22:42:17 +0100 Subject: [PATCH] Move Plugin and PluginMeta into plugin.{c,h}pp --- src/CMakeLists.txt | 2 + src/controllers/plugins/Plugin.cpp | 29 +++++ src/controllers/plugins/Plugin.hpp | 97 +++++++++++++++++ src/controllers/plugins/PluginController.hpp | 109 +------------------ 4 files changed, 129 insertions(+), 108 deletions(-) create mode 100644 src/controllers/plugins/Plugin.cpp create mode 100644 src/controllers/plugins/Plugin.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 679d1bc6d..3b9230fcf 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -136,6 +136,8 @@ set(SOURCE_FILES controllers/pings/MutedChannelModel.cpp controllers/pings/MutedChannelModel.hpp + controllers/plugins/Plugin.cpp + controllers/plugins/Plugin.hpp controllers/plugins/PluginController.hpp controllers/plugins/PluginController.cpp controllers/plugins/LuaUtilities.cpp diff --git a/src/controllers/plugins/Plugin.cpp b/src/controllers/plugins/Plugin.cpp new file mode 100644 index 000000000..d75c3f19d --- /dev/null +++ b/src/controllers/plugins/Plugin.cpp @@ -0,0 +1,29 @@ +#include "Plugin.hpp" + +namespace chatterino { +bool Plugin::registerCommand(const QString &name, const QString &functionName) +{ + if (this->ownedCommands.find(name) != this->ownedCommands.end()) + { + return false; + } + + auto ok = getApp()->commands->registerPluginCommand(name); + if (!ok) + { + return false; + } + this->ownedCommands.insert({name, functionName}); + return true; +} + +std::set Plugin::listRegisteredCommands() +{ + std::set out; + for (const auto &[name, _] : this->ownedCommands) + { + out.insert(name); + } + return out; +} +} // namespace chatterino diff --git a/src/controllers/plugins/Plugin.hpp b/src/controllers/plugins/Plugin.hpp new file mode 100644 index 000000000..27ad69a97 --- /dev/null +++ b/src/controllers/plugins/Plugin.hpp @@ -0,0 +1,97 @@ +#pragma once +#include "Application.hpp" +#include "controllers/commands/CommandController.hpp" + +#include +#include +#include +#include +#include + +#include +#include + +struct lua_State; + +namespace chatterino { + +struct PluginMeta { + QString name; + QString description; + QString authors; + QString homepage; + + QString license; + semver::version version; + + std::vector tags; + + std::set libraryPermissions; + + explicit PluginMeta(const QJsonObject &obj) + : name(obj.value("name").toString("A Plugin with no name")) + , description(obj.value("description").toString("Nothing here")) + , authors( + obj.value("authors").toString("[please tell me who made this]")) + , homepage(obj.value("homepage").toString("[https://example.com]")) + , license(obj.value("license").toString("[unknown]")) + { + auto v = semver::from_string_noexcept( + obj.value("version").toString().toStdString()); + if (v.has_value()) + { + this->version = v.value(); + } + else + { + this->version = semver::version(0, 0, 0); + description.append("\nWarning: invalid version. Use semver."); + } + for (const auto &t : obj.value("tags").toArray()) + { + this->tags.push_back(t.toString()); + } + for (const auto &t : obj.value("library_permissions").toArray()) + { + this->libraryPermissions.insert(t.toString()); + } + } + + bool hasDangerousLibraries() + { + const auto *perms = &this->libraryPermissions; + return perms->contains("io") || perms->contains("package") || + perms->contains("os"); + } +}; + +class Plugin +{ +public: + QString codename; + PluginMeta meta; + bool isDupeName{}; + + Plugin(QString codename, lua_State *state, PluginMeta meta, + const QDir &loadDirectory) + : codename(std::move(codename)) + , meta(std::move(meta)) + , loadDirectory_(loadDirectory) + , state_(state) + { + } + + bool registerCommand(const QString &name, const QString &functionName); + + std::set listRegisteredCommands(); + +private: + QDir loadDirectory_; + lua_State *state_; + + // maps command name -> function name + std::map ownedCommands; + + friend class PluginController; +}; +} // namespace chatterino diff --git a/src/controllers/plugins/PluginController.hpp b/src/controllers/plugins/PluginController.hpp index 0ca7f4b7f..d024dd801 100644 --- a/src/controllers/plugins/PluginController.hpp +++ b/src/controllers/plugins/PluginController.hpp @@ -1,10 +1,7 @@ #pragma once -#include "Application.hpp" -#include "common/QLogging.hpp" #include "common/Singleton.hpp" -#include "controllers/commands/CommandContext.hpp" -#include "controllers/commands/CommandController.hpp" +#include "controllers/plugins/Plugin.hpp" #include "singletons/Paths.hpp" #include @@ -12,7 +9,6 @@ #include #include #include -#include #include #include @@ -24,109 +20,6 @@ struct lua_State; namespace chatterino { -struct PluginMeta { - QString name; - QString description; - QString authors; - QString homepage; - - QString license; - semver::version version; - - std::vector tags; - - std::set libraryPermissions; - - explicit PluginMeta(const QJsonObject &obj) - : name(obj.value("name").toString("A Plugin with no name")) - , description(obj.value("description").toString("Nothing here")) - , authors( - obj.value("authors").toString("[please tell me who made this]")) - , homepage(obj.value("homepage").toString("[https://example.com]")) - , license(obj.value("license").toString("[unknown]")) - - { - auto v = semver::from_string_noexcept( - obj.value("version").toString().toStdString()); - if (v.has_value()) - { - this->version = v.value(); - } - else - { - this->version = semver::version(0, 0, 0); - description.append("\nWarning: invalid version. Use semver."); - } - for (const auto &t : obj.value("tags").toArray()) - { - this->tags.push_back(t.toString()); - } - for (const auto &t : obj.value("library_permissions").toArray()) - { - this->libraryPermissions.insert(t.toString()); - } - } - - bool hasDangerousLibraries() - { - const auto *perms = &this->libraryPermissions; - return perms->contains("io") || perms->contains("package") || - perms->contains("os"); - } -}; - -class Plugin -{ -public: - QString codename; - PluginMeta meta; - bool isDupeName{}; - - Plugin(QString codename, lua_State *state, PluginMeta meta, - const QDir &loadDirectory) - : codename(std::move(codename)) - , meta(std::move(meta)) - , loadDirectory_(loadDirectory) - , state_(state) - { - } - - bool registerCommand(const QString &name, const QString &functionName) - { - if (this->ownedCommands.find(name) != this->ownedCommands.end()) - { - return false; - } - - auto ok = getApp()->commands->registerPluginCommand(name); - if (!ok) - { - return false; - } - this->ownedCommands.insert({name, functionName}); - return true; - } - - std::set listRegisteredCommands() - { - std::set out; - for (const auto &[name, _] : this->ownedCommands) - { - out.insert(name); - } - return out; - } - -private: - QDir loadDirectory_; - lua_State *state_; - - // maps command name -> function name - std::map ownedCommands; - - friend class PluginController; -}; - class PluginController : public Singleton { public: