From 2fb6047a5911ab47dd3f5befe45bcb0fdfe1b307 Mon Sep 17 00:00:00 2001 From: yamashi Date: Wed, 17 Feb 2021 19:20:16 +0100 Subject: [PATCH] Fix environment again --- src/scripting/ScriptContext.cpp | 41 +++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/src/scripting/ScriptContext.cpp b/src/scripting/ScriptContext.cpp index f5ff9234..f5bd6aef 100644 --- a/src/scripting/ScriptContext.cpp +++ b/src/scripting/ScriptContext.cpp @@ -6,14 +6,14 @@ // TODO: proper exception handling for Lua funcs! template -static sol::protected_function_result TryLuaFunction(std::shared_ptr aLogger, sol::function aFunc, Args... aArgs) +static sol::protected_function_result TryLuaFunction(std::shared_ptr aLogger, sol::environment& aEnv, sol::function aFunc, Args... aArgs) { sol::protected_function_result result{ }; if (aFunc) { try { - result = aFunc(aArgs...); + result = aFunc(aArgs..., aEnv); } catch(std::exception& e) { @@ -74,9 +74,12 @@ ScriptContext::ScriptContext(LuaSandbox& aLuaSandbox, const std::filesystem::pat auto loggerRef = m_logger; std::string vkBindID = m_name + '.' + acID; - VKBind vkBind = { vkBindID, acDescription, [loggerRef, aCallback]() + VKBind vkBind = { vkBindID, acDescription, [loggerRef, aCallback, this]() { - TryLuaFunction(loggerRef, aCallback); + auto& sb = m_sandbox[m_sandboxID]; + auto& env = sb.GetEnvironment(); + + TryLuaFunction(loggerRef, env, aCallback); }}; m_vkBindInfos.emplace_back(VKBindInfo{vkBind}); @@ -130,37 +133,52 @@ const std::vector& ScriptContext::GetBinds() const void ScriptContext::TriggerOnInit() const { + auto& sb = m_sandbox[m_sandboxID]; + auto& env = sb.GetEnvironment(); + auto state = m_sandbox.GetState(); - TryLuaFunction(m_logger, m_onInit); + TryLuaFunction(m_logger, env, m_onInit); } void ScriptContext::TriggerOnUpdate(float aDeltaTime) const { + auto& sb = m_sandbox[m_sandboxID]; + auto& env = sb.GetEnvironment(); + auto state = m_sandbox.GetState(); - TryLuaFunction(m_logger, m_onUpdate, aDeltaTime); + TryLuaFunction(m_logger, env, m_onUpdate, aDeltaTime); } void ScriptContext::TriggerOnDraw() const { + auto& sb = m_sandbox[m_sandboxID]; + auto& env = sb.GetEnvironment(); + auto state = m_sandbox.GetState(); - TryLuaFunction(m_logger, m_onDraw); + TryLuaFunction(m_logger, env, m_onDraw); } void ScriptContext::TriggerOnOverlayOpen() const { + auto& sb = m_sandbox[m_sandboxID]; + auto& env = sb.GetEnvironment(); + auto state = m_sandbox.GetState(); - TryLuaFunction(m_logger, m_onOverlayOpen); + TryLuaFunction(m_logger, env, m_onOverlayOpen); } void ScriptContext::TriggerOnOverlayClose() const { + auto& sb = m_sandbox[m_sandboxID]; + auto& env = sb.GetEnvironment(); + auto state = m_sandbox.GetState(); - TryLuaFunction(m_logger, m_onOverlayClose); + TryLuaFunction(m_logger, env, m_onOverlayClose); } sol::object ScriptContext::GetRootObject() const @@ -170,7 +188,10 @@ sol::object ScriptContext::GetRootObject() const void ScriptContext::TriggerOnShutdown() const { + auto& sb = m_sandbox[m_sandboxID]; + auto& env = sb.GetEnvironment(); + auto state = m_sandbox.GetState(); - TryLuaFunction(m_logger, m_onShutdown); + TryLuaFunction(m_logger, env, m_onShutdown); }