mirror of
https://github.com/scp-fs2open/fs2open.github.com.git
synced 2026-11-08 07:28:22 -10:00
This will ensure that Lua functions will always be executed in the correct Lua state. Since we now have multiple threads (which are different lua_State objects) it is possible that the current code could execute a Lua function in the main state instead of the thread that is currently executing. This can lead to weird behavior and probably also to issues inside Lua.
49 lines
1.1 KiB
C++
49 lines
1.1 KiB
C++
#pragma once
|
|
|
|
#include "object/object.h"
|
|
#include "scripting/lua/LuaFunction.h"
|
|
|
|
namespace scripting {
|
|
namespace api {
|
|
namespace util {
|
|
|
|
void convert_arg(lua_State* L, luacpp::LuaValueList& out, object* objp);
|
|
}
|
|
|
|
template <typename Ret, typename... Args>
|
|
class LuaEventCallback {
|
|
luacpp::LuaFunction _func;
|
|
|
|
void convert_args(lua_State* /*L*/, luacpp::LuaValueList& /*out*/) {}
|
|
|
|
template <typename T, typename... A>
|
|
void convert_args(lua_State* L, luacpp::LuaValueList& out, T t, A... args)
|
|
{
|
|
util::convert_arg(L, out, t);
|
|
|
|
convert_args(L, out, args...);
|
|
}
|
|
|
|
public:
|
|
explicit LuaEventCallback(const luacpp::LuaFunction& func) : _func(func) {}
|
|
|
|
void operator()(Args... args)
|
|
{
|
|
using namespace luacpp;
|
|
|
|
LuaValueList lua_args;
|
|
convert_args(_func.getLuaState(), lua_args, args...);
|
|
|
|
_func(_func.getLuaState(), lua_args);
|
|
}
|
|
};
|
|
|
|
template <typename Ret, typename... Args>
|
|
std::function<Ret(Args...)> make_lua_callback(const luacpp::LuaFunction& func)
|
|
{
|
|
return std::function<Ret(Args...)>(LuaEventCallback<Ret, Args...>(func));
|
|
}
|
|
|
|
} // namespace api
|
|
} // namespace scripting
|