diff --git a/apps/openmw-mp/Script/LangLua/LangLua.cpp b/apps/openmw-mp/Script/LangLua/LangLua.cpp index e7c99c737..1d12d153e 100644 --- a/apps/openmw-mp/Script/LangLua/LangLua.cpp +++ b/apps/openmw-mp/Script/LangLua/LangLua.cpp @@ -53,53 +53,67 @@ LangLua::~LangLua() } -template -struct Lua_dispatch_ { - template - inline static R Lua_dispatch(lua_State*&& lua, Args&&... args) noexcept { - constexpr ScriptFunctionData const& F_ = ScriptFunctions::functions[F]; - auto arg = luabridge::Stack::type>::get(lua, I); - return Lua_dispatch_::template Lua_dispatch( - std::forward(lua), - arg, - std::forward(args)...); +// LuaFunctionDispatcher template struct for Lua function dispatch +template +struct LuaFunctionDispatcher { + // Dispatch Lua function with the given arguments + template + inline static ReturnType Dispatch(lua_State*&& lua, Args&&... args) noexcept { + // Retrieve function data + constexpr ScriptFunctionData const& functionData = ScriptFunctions::functions[FunctionIndex]; + // Retrieve argument from the Lua stack + auto argument = luabridge::Stack::type>::get(lua, ArgIndex); + // Recursively dispatch the Lua function + return LuaFunctionDispatcher::template Dispatch( + std::forward(lua), argument, std::forward(args)...); } }; -template -struct Lua_dispatch_<0, F> { - template - inline static R Lua_dispatch(lua_State*&&, Args&&... args) noexcept { - constexpr ScriptFunctionData const& F_ = ScriptFunctions::functions[F]; - return reinterpret_cast>(F_.func.addr)(std::forward(args)...); +// Specialization for LuaFunctionDispatcher when ArgIndex is 0 +template +struct LuaFunctionDispatcher<0, FunctionIndex> { + // Dispatch Lua function with the given arguments + template + inline static ReturnType Dispatch(lua_State*&&, Args&&... args) noexcept { + // Retrieve function data + constexpr ScriptFunctionData const& functionData = ScriptFunctions::functions[FunctionIndex]; + // Call the C++ function using reinterpret_cast + return reinterpret_cast>(functionData.func.addr)(std::forward(args)...); } }; -template -static typename std::enable_if::type wrapper(lua_State* lua) noexcept { - Lua_dispatch_::template Lua_dispatch(std::forward(lua)); +// Lua function wrapper for functions returning 'void' +template +static typename std::enable_if::type LuaFunctionWrapper(lua_State* lua) noexcept { + // Dispatch the Lua function + LuaFunctionDispatcher::template Dispatch(std::forward(lua)); return 0; } -template -static typename std::enable_if::type wrapper(lua_State* lua) noexcept { - auto ret = Lua_dispatch_::template Lua_dispatch< - typename CharType::type>(std::forward(lua)); - luabridge::Stack ::type>::push (lua, ret); +// Lua function wrapper for functions with non-void return types +template +static typename std::enable_if::type LuaFunctionWrapper(lua_State* lua) noexcept { + // Dispatch the Lua function + auto result = LuaFunctionDispatcher::template Dispatch< + typename CharType::type>(std::forward(lua)); + // Push the result onto the Lua stack + luabridge::Stack::type>::push(lua, result); return 1; } -template -struct F_ -{ - static constexpr LuaFuctionData F{ScriptFunctions::functions[I].name, wrapper}; +// Struct for defining Lua functions with names and wrappers +template +struct LuaFunctionDefinition { + static constexpr LuaFunctionData FunctionInfo{ + ScriptFunctions::functions[FunctionIndex].name, LuaFunctionWrapper + }; }; +template<> struct LuaFunctionDefinition<0> { static constexpr LuaFunctionData FunctionInfo{"CreateTimer", LangLua::CreateTimer}; }; +template<> struct LuaFunctionDefinition<1> { static constexpr LuaFunctionData FunctionInfo{"CreateTimerEx", LangLua::CreateTimerEx}; }; +template<> struct LuaFunctionDefinition<2> { static constexpr LuaFunctionData FunctionInfo{"MakePublic", LangLua::MakePublic}; }; +template<> struct LuaFunctionDefinition<3> { static constexpr LuaFunctionData FunctionInfo{"CallPublic", LangLua::CallPublic}; }; -template<> struct F_<0> { static constexpr LuaFuctionData F{"CreateTimer", LangLua::CreateTimer}; }; -template<> struct F_<1> { static constexpr LuaFuctionData F{"CreateTimerEx", LangLua::CreateTimerEx}; }; -template<> struct F_<2> { static constexpr LuaFuctionData F{"MakePublic", LangLua::MakePublic}; }; -template<> struct F_<3> { static constexpr LuaFuctionData F{"CallPublic", LangLua::CallPublic}; }; #ifdef __arm__ template @@ -128,30 +142,29 @@ LuaFuctionData *functions(indices) } #else template -struct C +struct LuaFunctionInitializer { - constexpr static void Fn(LuaFuctionData *functions_) + constexpr static void Initialize(LuaFunctionData *functions_) { - functions_[I] = F_::F; - C::Fn(functions_); + functions_[I] = LuaFunctionDefinition::FunctionInfo; + LuaFunctionInitializer::Initialize(functions_); } }; template<> -struct C<0> +struct LuaFunctionInitializer<0> { - constexpr static void Fn(LuaFuctionData *functions_) + constexpr static void Initialize(LuaFunctionData *functions_) { - functions_[0] = F_<0>::F; + functions_[0] = LuaFunctionDefinition<0>::FunctionInfo; } }; template -LuaFuctionData *functions() +LuaFunctionData *GetLuaFunctions() { - - static LuaFuctionData functions_[LastI]; - C::Fn(functions_); + static LuaFunctionData functions_[LastI]; + LuaFunctionInitializer::Initialize(functions_); static_assert( sizeof(functions_) / sizeof(functions_[0]) == @@ -173,20 +186,21 @@ void LangLua::LoadProgram(const char *filename) constexpr auto functions_n = sizeof(ScriptFunctions::functions) / sizeof(ScriptFunctions::functions[0]); #ifdef __arm__ - LuaFuctionData *functions_ = functions(IndicesFor{}); + LuaFunctionData *functions_ = GetLuaFunctions(IndicesFor{}); #else - LuaFuctionData *functions_ = functions(); + LuaFunctionData *functions_ = GetLuaFunctions(); #endif - luabridge::Namespace tes3mp = luabridge::getGlobalNamespace(lua).beginNamespace("tes3mp"); +luabridge::Namespace tes3mp = luabridge::getGlobalNamespace(lua).beginNamespace("tes3mp"); - for (unsigned i = 0; i < functions_n; i++) - tes3mp.addCFunction(functions_[i].name, functions_[i].func); +for (unsigned i = 0; i < functions_n; i++) + tes3mp.addCFunction(functions_[i].name, functions_[i].func); - tes3mp.endNamespace(); +tes3mp.endNamespace(); + +if ((err = lua_pcall(lua, 0, 0, 0)) != 0) // Run once script for load in memory. + throw std::runtime_error("Lua script " + std::string(filename) + " error (" + std::to_string(err) + "): \"" + + std::string(lua_tostring(lua, -1)) + "\""); - if ((err = lua_pcall(lua, 0, 0, 0)) != 0) // Run once script for load in memory. - throw std::runtime_error("Lua script " + std::string(filename) + " error (" + std::to_string(err) + "): \"" + - std::string(lua_tostring(lua, -1)) + "\""); } int LangLua::FreeProgram() diff --git a/apps/openmw-mp/Script/LangLua/LangLua.hpp b/apps/openmw-mp/Script/LangLua/LangLua.hpp index e75fe223b..9f0ae3fdf 100644 --- a/apps/openmw-mp/Script/LangLua/LangLua.hpp +++ b/apps/openmw-mp/Script/LangLua/LangLua.hpp @@ -11,7 +11,7 @@ #include "../ScriptFunction.hpp" #include "../Language.hpp" -struct LuaFuctionData +struct LuaFunctionData { const char* name; lua_CFunction func;