#include "script.h" #include "common.h" #include "logger.h" #include "objects/base/instance.h" #include "objects/base/member.h" #include "objects/script/scriptcontext.h" #include "objects/workspace.h" #include "objects/datamodel.h" #include "datatypes/ref.h" #include "lua.h" #include #include int script_wait(lua_State*); int script_delay(lua_State*); Script::Script(): Instance(&TYPE) { source = "workspace.Part.Touched:Connect(function(otherPart)\n" " print(\"Touched by: \", otherPart.Name)\n" "end)\n" "\n" "workspace.Part.TouchEnded:Connect(function(otherPart)\n" " print(\"Touched ended with: \", otherPart.Name)\n" "end)\n" "\n" "error(\"Test\")"; } Script::~Script() { } void Script::Run() { std::shared_ptr scriptContext = dataModel().value()->GetService(); lua_State* L = scriptContext->state; // Create thread this->thread = lua_newthread(L); lua_State* Lt = thread; // Initialize script globals lua_getglobal(Lt, "_G"); Data::InstanceRef(dataModel().value()).PushLuaValue(Lt); lua_setfield(Lt, -2, "game"); Data::InstanceRef(dataModel().value()->GetService()).PushLuaValue(Lt); lua_setfield(Lt, -2, "workspace"); lua_pushlightuserdata(Lt, scriptContext.get()); lua_pushcclosure(Lt, script_wait, 1); lua_setfield(Lt, -2, "wait"); lua_pushlightuserdata(Lt, scriptContext.get()); lua_pushcclosure(Lt, script_delay, 1); lua_setfield(Lt, -2, "delay"); lua_pop(Lt, 1); // _G // Load source and push onto thread stack as function ptr // luaL_loadstring(Lt, source.c_str()); luaL_loadbuffer(Lt, source.c_str(), source.size(), scriptContext->RegisterScriptSource(shared