Move Plugin and PluginMeta into plugin.{c,h}pp

This commit is contained in:
Mm2PL 2023-02-01 22:42:17 +01:00
parent 3ee84ff444
commit a61bc4d6d1
No known key found for this signature in database
GPG key ID: 94AC9B80EFA15ED9
4 changed files with 129 additions and 108 deletions

View file

@ -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

View file

@ -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<QString> Plugin::listRegisteredCommands()
{
std::set<QString> out;
for (const auto &[name, _] : this->ownedCommands)
{
out.insert(name);
}
return out;
}
} // namespace chatterino

View file

@ -0,0 +1,97 @@
#pragma once
#include "Application.hpp"
#include "controllers/commands/CommandController.hpp"
#include <QDir>
#include <QJsonArray>
#include <QJsonObject>
#include <QString>
#include <semver/semver.hpp>
#include <set>
#include <vector>
struct lua_State;
namespace chatterino {
struct PluginMeta {
QString name;
QString description;
QString authors;
QString homepage;
QString license;
semver::version version;
std::vector<QString> tags;
std::set<QString> 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<QString> listRegisteredCommands();
private:
QDir loadDirectory_;
lua_State *state_;
// maps command name -> function name
std::map<QString, QString> ownedCommands;
friend class PluginController;
};
} // namespace chatterino

View file

@ -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 <QDir>
@ -12,7 +9,6 @@
#include <QJsonArray>
#include <QJsonObject>
#include <QString>
#include <semver/semver.hpp>
#include <algorithm>
#include <map>
@ -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<QString> tags;
std::set<QString> 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<QString> listRegisteredCommands()
{
std::set<QString> 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<QString, QString> ownedCommands;
friend class PluginController;
};
class PluginController : public Singleton
{
public: