mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
Move pushing stuff to lua stack to LuaUtilities
This commit is contained in:
parent
7312af1bec
commit
b4a20e15c0
4 changed files with 93 additions and 19 deletions
|
@ -138,6 +138,8 @@ set(SOURCE_FILES
|
|||
|
||||
controllers/plugins/PluginController.hpp
|
||||
controllers/plugins/PluginController.cpp
|
||||
controllers/plugins/LuaUtilities.cpp
|
||||
controllers/plugins/LuaUtilities.hpp
|
||||
|
||||
controllers/userdata/UserDataController.cpp
|
||||
controllers/userdata/UserDataController.hpp
|
||||
|
|
38
src/controllers/plugins/LuaUtilities.cpp
Normal file
38
src/controllers/plugins/LuaUtilities.cpp
Normal file
|
@ -0,0 +1,38 @@
|
|||
#include "LuaUtilities.hpp"
|
||||
|
||||
#include "common/Channel.hpp"
|
||||
#include "controllers/commands/CommandContext.hpp"
|
||||
#include "lua.h"
|
||||
|
||||
namespace chatterino::lua {
|
||||
int pushEmptyArray(lua_State *L, int countArray)
|
||||
{
|
||||
lua_createtable(L, countArray, 0);
|
||||
return lua_gettop(L);
|
||||
}
|
||||
|
||||
int pushEmptyTable(lua_State *L, int countProperties)
|
||||
{
|
||||
lua_createtable(L, 0, countProperties);
|
||||
return lua_gettop(L);
|
||||
}
|
||||
|
||||
int push(lua_State *L, const QString &str)
|
||||
{
|
||||
lua_pushstring(L, str.toStdString().c_str());
|
||||
return lua_gettop(L);
|
||||
}
|
||||
|
||||
int push(lua_State *L, const CommandContext &ctx)
|
||||
{
|
||||
auto outIdx = pushEmptyTable(L, 2);
|
||||
|
||||
push(L, ctx.words);
|
||||
lua_setfield(L, outIdx, "words");
|
||||
push(L, ctx.channel->getName());
|
||||
lua_setfield(L, outIdx, "channelName");
|
||||
|
||||
return outIdx;
|
||||
}
|
||||
|
||||
} // namespace chatterino::lua
|
51
src/controllers/plugins/LuaUtilities.hpp
Normal file
51
src/controllers/plugins/LuaUtilities.hpp
Normal file
|
@ -0,0 +1,51 @@
|
|||
#pragma once
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include <qlist.h>
|
||||
|
||||
#include <vector>
|
||||
struct lua_State;
|
||||
namespace chatterino {
|
||||
class CommandContext;
|
||||
} // namespace chatterino
|
||||
|
||||
namespace chatterino::lua {
|
||||
|
||||
int pushEmptyArray(lua_State *L, int countArray);
|
||||
int pushEmptyTable(lua_State *L, int countProperties);
|
||||
|
||||
int push(lua_State *L, const CommandContext &ctx);
|
||||
int push(lua_State *L, const QString &str);
|
||||
|
||||
/// TEMPLATES
|
||||
|
||||
template <typename T>
|
||||
int push(lua_State *L, std::vector<T> vec)
|
||||
{
|
||||
auto out = pushEmptyArray(L, vec.size());
|
||||
int i = 1;
|
||||
for (const auto &el : vec)
|
||||
{
|
||||
push(L, el);
|
||||
lua_seti(L, out, i);
|
||||
i += 1;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
int push(lua_State *L, QList<T> vec)
|
||||
{
|
||||
auto out = pushEmptyArray(L, vec.size());
|
||||
int i = 1;
|
||||
for (const auto &el : vec)
|
||||
{
|
||||
push(L, el);
|
||||
lua_seti(L, out, i);
|
||||
i += 1;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace chatterino::lua
|
|
@ -3,6 +3,7 @@
|
|||
#include "Application.hpp"
|
||||
#include "common/QLogging.hpp"
|
||||
#include "controllers/commands/CommandContext.hpp"
|
||||
#include "controllers/plugins/LuaUtilities.hpp"
|
||||
#include "messages/MessageBuilder.hpp"
|
||||
#include "providers/twitch/TwitchIrcServer.hpp"
|
||||
#include "singletons/WindowManager.hpp"
|
||||
|
@ -98,25 +99,7 @@ QString PluginController::tryExecPluginCommand(const QString &commandName,
|
|||
|
||||
auto *L = plugin->state_; // NOLINT
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, funcName.toStdString().c_str());
|
||||
// put args on stack
|
||||
lua_createtable(L, 0, 2);
|
||||
auto outIdx = lua_gettop(L);
|
||||
|
||||
lua_createtable(L, ctx.words.count(), 0);
|
||||
auto wordsIdx = lua_gettop(L);
|
||||
|
||||
int i = 1;
|
||||
for (const auto &w : ctx.words)
|
||||
{
|
||||
lua_pushstring(L, w.toStdString().c_str());
|
||||
lua_seti(L, wordsIdx, i);
|
||||
i += 1;
|
||||
}
|
||||
|
||||
lua_setfield(L, outIdx, "words");
|
||||
|
||||
lua_pushstring(L, ctx.channel->getName().toStdString().c_str());
|
||||
lua_setfield(L, outIdx, "channelName");
|
||||
lua::push(L, ctx);
|
||||
|
||||
auto res = lua_pcall(L, 1, 0, 0);
|
||||
if (res != LUA_OK)
|
||||
|
|
Loading…
Reference in a new issue