Skip to content

Commit

Permalink
Fix environment again
Browse files Browse the repository at this point in the history
  • Loading branch information
maximegmd committed Feb 17, 2021
1 parent 00f3f5c commit 2fb6047
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions src/scripting/ScriptContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

// TODO: proper exception handling for Lua funcs!
template <typename ...Args>
static sol::protected_function_result TryLuaFunction(std::shared_ptr<spdlog::logger> aLogger, sol::function aFunc, Args... aArgs)
static sol::protected_function_result TryLuaFunction(std::shared_ptr<spdlog::logger> 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)
{
Expand Down Expand Up @@ -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]()

This comment has been minimized.

Copy link
@WSSDude

WSSDude Feb 17, 2021

Collaborator

this may be toxic, i would rather pass reference to sandbox or even better, just environment

This comment has been minimized.

Copy link
@WSSDude

WSSDude Feb 17, 2021

Collaborator

actually, m_sandbox reference + m_sandboxID, as reference directly to specific sandbox/enviroment of it can itself become also invalid with multiple mods...

so i would pass reference to m_sandbox and m_sandboxID instead of this here

{
TryLuaFunction(loggerRef, aCallback);
auto& sb = m_sandbox[m_sandboxID];
auto& env = sb.GetEnvironment();

TryLuaFunction(loggerRef, env, aCallback);
}};

m_vkBindInfos.emplace_back(VKBindInfo{vkBind});
Expand Down Expand Up @@ -130,37 +133,52 @@ const std::vector<VKBindInfo>& 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
Expand All @@ -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);
}

0 comments on commit 2fb6047

Please sign in to comment.