diff --git a/core/src/objects/datamodel.cpp b/core/src/objects/datamodel.cpp index ef09d1b..1469b6e 100644 --- a/core/src/objects/datamodel.cpp +++ b/core/src/objects/datamodel.cpp @@ -138,6 +138,7 @@ result, NoSuchService> DataModel::GetService(std::strin services[className] = std::dynamic_pointer_cast(INSTANCE_MAP[className]->constructor()); AddChild(std::dynamic_pointer_cast(services[className])); + services[className]->InitService(); return std::dynamic_pointer_cast(services[className]); } diff --git a/core/src/objects/jointsservice.cpp b/core/src/objects/jointsservice.cpp index 1ba1734..226ca45 100644 --- a/core/src/objects/jointsservice.cpp +++ b/core/src/objects/jointsservice.cpp @@ -24,7 +24,7 @@ void JointsService::InitService() { initialized = true; // Clear children before any new joints are added - for (std::shared_ptr inst : dataModel().value()->GetService()->GetChildren()) { + for (std::shared_ptr inst : GetChildren()) { inst->Destroy(); } } diff --git a/core/src/objects/script.cpp b/core/src/objects/script.cpp index c062949..3141e3a 100644 --- a/core/src/objects/script.cpp +++ b/core/src/objects/script.cpp @@ -1,6 +1,10 @@ #include "script.h" +#include "logger.h" #include "objects/base/instance.h" #include "objects/base/member.h" +#include "objects/script/scriptcontext.h" +#include +#include const InstanceType Script::TYPE = { .super = &Instance::TYPE, @@ -25,13 +29,22 @@ Script::Script(): Instance(&TYPE) { }}, } }); + + source = "print \"Hello, world!\""; } Script::~Script() { } void Script::Run() { - + lua_State* L = dataModel().value()->GetService()->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() { diff --git a/core/src/objects/script/scriptcontext.cpp b/core/src/objects/script/scriptcontext.cpp index b45cc04..fee1199 100644 --- a/core/src/objects/script/scriptcontext.cpp +++ b/core/src/objects/script/scriptcontext.cpp @@ -1,6 +1,14 @@ #include "scriptcontext.h" +#include "logger.h" #include #include +#include + +static int redirectPrint(lua_State*); +static const struct luaL_Reg luaglobals [] = { + {"print", redirectPrint}, + {NULL, NULL} /* end of array */ +}; const InstanceType ScriptContext::TYPE = { .super = &Instance::TYPE, @@ -26,4 +34,28 @@ void ScriptContext::InitService() { initialized = true; 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; } \ No newline at end of file diff --git a/core/src/objects/script/serverscriptservice.cpp b/core/src/objects/script/serverscriptservice.cpp index bdf3107..fcea055 100644 --- a/core/src/objects/script/serverscriptservice.cpp +++ b/core/src/objects/script/serverscriptservice.cpp @@ -23,7 +23,7 @@ void ServerScriptService::InitService() { initialized = true; } -void Service::OnRun() { +void ServerScriptService::OnRun() { auto workspace = dataModel().value()->GetService(); for (auto it = workspace->GetDescendantsStart(); it != workspace->GetDescendantsEnd(); it++) { if (!it->IsA