feat(script): added code to actually run lua code

This commit is contained in:
maelstrom 2025-04-24 17:36:47 +02:00
parent c081469a49
commit 99440cc3ee
7 changed files with 61 additions and 13 deletions

View file

@ -138,6 +138,7 @@ result<std::shared_ptr<Service>, NoSuchService> DataModel::GetService(std::strin
services[className] = std::dynamic_pointer_cast<Service>(INSTANCE_MAP[className]->constructor()); services[className] = std::dynamic_pointer_cast<Service>(INSTANCE_MAP[className]->constructor());
AddChild(std::dynamic_pointer_cast<Instance>(services[className])); AddChild(std::dynamic_pointer_cast<Instance>(services[className]));
services[className]->InitService();
return std::dynamic_pointer_cast<Service>(services[className]); return std::dynamic_pointer_cast<Service>(services[className]);
} }

View file

@ -24,7 +24,7 @@ void JointsService::InitService() {
initialized = true; initialized = true;
// Clear children before any new joints are added // Clear children before any new joints are added
for (std::shared_ptr<Instance> inst : dataModel().value()->GetService<JointsService>()->GetChildren()) { for (std::shared_ptr<Instance> inst : GetChildren()) {
inst->Destroy(); inst->Destroy();
} }
} }

View file

@ -1,6 +1,10 @@
#include "script.h" #include "script.h"
#include "logger.h"
#include "objects/base/instance.h" #include "objects/base/instance.h"
#include "objects/base/member.h" #include "objects/base/member.h"
#include "objects/script/scriptcontext.h"
#include <luajit-2.1/lauxlib.h>
#include <luajit-2.1/lua.h>
const InstanceType Script::TYPE = { const InstanceType Script::TYPE = {
.super = &Instance::TYPE, .super = &Instance::TYPE,
@ -25,13 +29,22 @@ Script::Script(): Instance(&TYPE) {
}}, }},
} }
}); });
source = "print \"Hello, world!\"";
} }
Script::~Script() { Script::~Script() {
} }
void Script::Run() { void Script::Run() {
lua_State* L = dataModel().value()->GetService<ScriptContext>()->state;
luaL_loadstring(L, source.c_str());
int status = lua_pcall(L, 0, LUA_MULTRET, 0);
if (status != 0) {
Logger::error(lua_tostring(L, -1));
lua_pop(L, 1);
}
} }
void Script::Stop() { void Script::Stop() {

View file

@ -1,6 +1,14 @@
#include "scriptcontext.h" #include "scriptcontext.h"
#include "logger.h"
#include <luajit-2.1/lauxlib.h> #include <luajit-2.1/lauxlib.h>
#include <luajit-2.1/lua.h> #include <luajit-2.1/lua.h>
#include <luajit-2.1/lualib.h>
static int redirectPrint(lua_State*);
static const struct luaL_Reg luaglobals [] = {
{"print", redirectPrint},
{NULL, NULL} /* end of array */
};
const InstanceType ScriptContext::TYPE = { const InstanceType ScriptContext::TYPE = {
.super = &Instance::TYPE, .super = &Instance::TYPE,
@ -26,4 +34,28 @@ void ScriptContext::InitService() {
initialized = true; initialized = true;
state = luaL_newstate(); state = luaL_newstate();
luaL_openlibs(state);
// Override print
// https://stackoverflow.com/a/4514193/16255372
lua_getglobal(state, "_G");
luaL_register(state, NULL, luaglobals);
lua_pop(state, 1);
}
static int redirectPrint(lua_State* L) {
std::string buf;
int nargs = lua_gettop(L);
for (int i=1; i <= nargs; i++) {
std::string arg(lua_tostring(L, -1));
lua_pop(L, 1);
buf += arg;
}
Logger::infof(buf);
return 0;
} }

View file

@ -23,7 +23,7 @@ void ServerScriptService::InitService() {
initialized = true; initialized = true;
} }
void Service::OnRun() { void ServerScriptService::OnRun() {
auto workspace = dataModel().value()->GetService<Workspace>(); auto workspace = dataModel().value()->GetService<Workspace>();
for (auto it = workspace->GetDescendantsStart(); it != workspace->GetDescendantsEnd(); it++) { for (auto it = workspace->GetDescendantsStart(); it != workspace->GetDescendantsEnd(); it++) {
if (!it->IsA<Script>()) continue; if (!it->IsA<Script>()) continue;

View file

@ -39,17 +39,19 @@ inline bool isDarkMode() {
QtMessageHandler defaultMessageHandler = nullptr; QtMessageHandler defaultMessageHandler = nullptr;
std::map<QtMsgType, Logger::LogLevel> QT_MESSAGE_TYPE_TO_LOG_LEVEL = { // std::map<QtMsgType, Logger::LogLevel> QT_MESSAGE_TYPE_TO_LOG_LEVEL = {
{ QtMsgType::QtInfoMsg, Logger::LogLevel::INFO }, // { QtMsgType::QtInfoMsg, Logger::LogLevel::INFO },
{ QtMsgType::QtSystemMsg, Logger::LogLevel::INFO }, // { QtMsgType::QtSystemMsg, Logger::LogLevel::INFO },
{ QtMsgType::QtDebugMsg, Logger::LogLevel::DEBUG }, // { QtMsgType::QtDebugMsg, Logger::LogLevel::DEBUG },
{ QtMsgType::QtWarningMsg, Logger::LogLevel::WARNING }, // { QtMsgType::QtWarningMsg, Logger::LogLevel::WARNING },
{ QtMsgType::QtCriticalMsg, Logger::LogLevel::ERROR }, // { QtMsgType::QtCriticalMsg, Logger::LogLevel::ERROR },
{ QtMsgType::QtFatalMsg, Logger::LogLevel::FATAL_ERROR }, // { QtMsgType::QtFatalMsg, Logger::LogLevel::FATAL_ERROR },
}; // };
void logQtMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg) { void logQtMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
Logger::log("[Qt] " + msg.toStdString(), QT_MESSAGE_TYPE_TO_LOG_LEVEL[type]); // Logger::log("[Qt] " + msg.toStdString(), QT_MESSAGE_TYPE_TO_LOG_LEVEL[type]);
Logger::LogLevel logLevel = type == QtMsgType::QtFatalMsg ? Logger::LogLevel::FATAL_ERROR : Logger::LogLevel::DEBUG;
Logger::log("[Qt] " + msg.toStdString(), logLevel);
// if (defaultMessageHandler) defaultMessageHandler(type, context, msg); // if (defaultMessageHandler) defaultMessageHandler(type, context, msg);
} }

View file

@ -36,7 +36,7 @@ ScriptDocument::ScriptDocument(std::shared_ptr<Script> script, QWidget* parent):
scintilla->setCaretForegroundColor(palette().text().color()); scintilla->setCaretForegroundColor(palette().text().color());
scintilla->setFont(font); scintilla->setFont(font);
scintilla->setLexer(); scintilla->setText(QString::fromStdString(script->source));
connect(scintilla, &QsciScintilla::textChanged, [this]() { connect(scintilla, &QsciScintilla::textChanged, [this]() {
// this-> is important here, as otherwise it will refer to the // this-> is important here, as otherwise it will refer to the