Add some callbacks back and forth

This commit is contained in:
Mm2PL 2023-01-29 22:08:59 +01:00
parent d1c9dfc865
commit c5688dd033
No known key found for this signature in database
GPG key ID: 94AC9B80EFA15ED9
3 changed files with 106 additions and 3 deletions

View file

@ -10,6 +10,7 @@
#include "controllers/commands/Command.hpp"
#include "controllers/commands/CommandContext.hpp"
#include "controllers/commands/CommandModel.hpp"
#include "controllers/plugins/PluginController.hpp"
#include "controllers/userdata/UserDataController.hpp"
#include "messages/Message.hpp"
#include "messages/MessageBuilder.hpp"
@ -3125,6 +3126,10 @@ void CommandController::initialize(Settings &, Paths &paths)
return "";
});
this->registerCommand("lualol", [](const auto &ctx) {
getApp()->plugins->callEvery("test");
return "";
});
}
void CommandController::save()

View file

@ -1,8 +1,12 @@
#include "PluginController.hpp"
#include "Application.hpp"
#include "common/QLogging.hpp"
#include "messages/MessageBuilder.hpp"
#include "providers/twitch/TwitchIrcServer.hpp"
#include <QApplication>
#include <memory>
#include <utility>
extern "C" {
#include <lauxlib.h>
@ -47,9 +51,63 @@ void PluginController::load(QFileInfo index, QDir pluginDir)
qCDebug(chatterinoLua) << "Running lua file" << index;
lua_State *l = luaL_newstate();
luaL_openlibs(l);
this->loadChatterinoLib(l);
luaL_dofile(l, index.absoluteFilePath().toStdString().c_str());
lua_close(l);
auto pluginName = pluginDir.dirName();
auto plugin = std::make_unique<Plugin>(pluginName, l);
this->plugins.insert({pluginName, std::move(plugin)});
qCInfo(chatterinoLua) << "Loaded" << pluginName << "plugin from" << index;
}
void PluginController::callEvery(const QString &functionName)
{
for (const auto &[name, plugin] : this->plugins)
{
lua_getglobal(plugin->state_, functionName.toStdString().c_str());
lua_pcall(plugin->state_, 0, 0, 0);
}
}
constexpr int C_FALSE = 0;
constexpr int C_TRUE = 1;
extern "C" {
int luaC2SystemMsg(lua_State *L)
{
if (lua_gettop(L) != 2)
{
luaL_error(L, "need exactly 2 arguments"); // NOLINT
lua_pushboolean(L, C_FALSE);
return 1;
}
const char *channel = luaL_optstring(L, 1, NULL);
const char *text = luaL_optstring(L, 2, NULL);
lua_pop(L, 2);
const auto chn = getApp()->twitch->getChannelOrEmpty(channel);
if (chn->isEmpty())
{
lua_pushboolean(L, C_FALSE);
return 1;
}
chn->addMessage(makeSystemMessage(text));
lua_pushboolean(L, C_TRUE);
return 0;
}
// NOLINTNEXTLINE
static const luaL_Reg C2LIB[] = {
{"system_msg", luaC2SystemMsg},
{nullptr, nullptr},
};
}
void PluginController::loadChatterinoLib(lua_State *L)
{
lua_pushglobaltable(L);
luaL_setfuncs(L, C2LIB, 0);
}
}; // namespace chatterino

View file

@ -3,19 +3,59 @@
#include "common/Singleton.hpp"
#include "singletons/Paths.hpp"
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QString>
#include <map>
#include <memory>
#include <utility>
#include <vector>
class lua_State;
namespace chatterino {
//class Registration
//{
//public:
// enum Type {
// COMMAND,
// };
//
// Type type;
// QString name;
// const char *receiverFunctionName;
//};
class Plugin
{
public:
QString name;
Plugin(QString name, lua_State *state)
: name(std::move(name))
, state_(state)
{
}
private:
lua_State *state_;
friend class PluginController;
};
class PluginController : public Singleton
{
public:
void initialize(Settings &settings, Paths &paths) override;
void save() override{};
void callEvery(const QString &functionName);
private:
void load(QFileInfo index, QDir pluginDir);
void loadChatterinoLib(lua_State *l);
std::map<QString, std::unique_ptr<Plugin>> plugins;
};
}; // namespace chatterino