mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
Move Plugin and PluginMeta into plugin.{c,h}pp
This commit is contained in:
parent
3ee84ff444
commit
a61bc4d6d1
4 changed files with 129 additions and 108 deletions
|
@ -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
|
||||
|
|
29
src/controllers/plugins/Plugin.cpp
Normal file
29
src/controllers/plugins/Plugin.cpp
Normal 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
|
97
src/controllers/plugins/Plugin.hpp
Normal file
97
src/controllers/plugins/Plugin.hpp
Normal 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
|
|
@ -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:
|
||||
|
|
Loading…
Reference in a new issue