From e46b74f2af60ff1994dce618eae77d863a4962de Mon Sep 17 00:00:00 2001 From: Lveyou <1339484752@qq.com> Date: Wed, 25 Dec 2024 19:20:44 +0800 Subject: [PATCH] =?UTF-8?q?*=20lua=E5=AF=BC=E5=87=BA=E5=9F=BA=E7=A1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dl/CMakeLists.txt | 2 ++ dl/script/dl_lua.cpp | 7 ++++++- dl/script/dl_lua_register.cpp | 27 +++++++++++++++++++++++++++ dl/script/dl_lua_register.h | 20 ++++++++++++++++++++ project/dl_console/CMakeLists.txt | 2 ++ project/dl_console/res/main.lua | 3 ++- third/luaaa/luaaa.hpp | 2 +- 7 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 dl/script/dl_lua_register.cpp create mode 100644 dl/script/dl_lua_register.h diff --git a/dl/CMakeLists.txt b/dl/CMakeLists.txt index fa45cabf..fcc613e4 100644 --- a/dl/CMakeLists.txt +++ b/dl/CMakeLists.txt @@ -227,6 +227,8 @@ include_directories(${CMAKE_SOURCE_DIR}/third/sqlite) include_directories(${CMAKE_SOURCE_DIR}/third/tinyobjloader) # tinygltf include_directories(${CMAKE_SOURCE_DIR}/third/tinygltf) +# luaaa +include_directories(${CMAKE_SOURCE_DIR}/third/luaaa) if (CMAKE_SYSTEM_NAME MATCHES "Android") find_library(log-lib log) diff --git a/dl/script/dl_lua.cpp b/dl/script/dl_lua.cpp index ad34faac..9ef86f4d 100644 --- a/dl/script/dl_lua.cpp +++ b/dl/script/dl_lua.cpp @@ -2,6 +2,9 @@ #define LUA_USE_APICHECK #include + +#include "dl_lua_register.h" + namespace dl { LuaScript* g_lua; @@ -10,6 +13,8 @@ namespace dl { L = luaL_newstate(); luaL_openlibs(L); + + lua_register_base(L); } LuaScript::~LuaScript() @@ -22,7 +27,7 @@ namespace dl int ret = luaL_dofile(L, path_name.data()); if (ret) { - log_err("lua do file失败!{}", path_name); + log_err("lua 执行失败:[{}]:{}", path_name, lua_tostring(L, -1)); return false; } return true; diff --git a/dl/script/dl_lua_register.cpp b/dl/script/dl_lua_register.cpp new file mode 100644 index 00000000..df289c03 --- /dev/null +++ b/dl/script/dl_lua_register.cpp @@ -0,0 +1,27 @@ +#include "dl_lua_register.h" + +#include + +#include "dl_io.h" + +namespace dl +{ + void lua_log(const std::string& str) + { + log(str); + } + + void lua_register_base(lua_State* L) + { + /*luaaa::LuaClass class_string_view(L, "string_view"); + class_string_view.ctor();*/ + /*luaaa::LuaClass< Lua_dl> lua_dl(L, "dl"); + lua_dl.ctor(); + lua_dl.def("gfx", g_gfx); + lua_dl.fun("log", log);*/ + + luaaa::LuaModule m{ L, "dl" }; + m.fun("log", lua_log); + } + +} \ No newline at end of file diff --git a/dl/script/dl_lua_register.h b/dl/script/dl_lua_register.h new file mode 100644 index 00000000..0df20ea0 --- /dev/null +++ b/dl/script/dl_lua_register.h @@ -0,0 +1,20 @@ +/** +* @file dl_lua_register.h +* @brief lua脚本接口注册 +* +* +* @version 1.0 +* @author lveyou +* @date 24-12-25 +* +* @note +*/ +#pragma once + +struct lua_State; +namespace dl +{ + + void lua_register_base(lua_State* L); + +} \ No newline at end of file diff --git a/project/dl_console/CMakeLists.txt b/project/dl_console/CMakeLists.txt index 2995d72d..757a4682 100644 --- a/project/dl_console/CMakeLists.txt +++ b/project/dl_console/CMakeLists.txt @@ -42,6 +42,8 @@ include_directories(${CMAKE_SOURCE_DIR}/third/tinyxml2) include_directories(${CMAKE_SOURCE_DIR}/third/asio/include) # angelscript include_directories(${CMAKE_SOURCE_DIR}/third/angelscript/angelscript/include) +# luaaa +include_directories(${CMAKE_SOURCE_DIR}/third/luaaa) # vld资源文件夹 if(CMAKE_CL_64) diff --git a/project/dl_console/res/main.lua b/project/dl_console/res/main.lua index 6ca79a06..4fb474d3 100644 --- a/project/dl_console/res/main.lua +++ b/project/dl_console/res/main.lua @@ -1 +1,2 @@ -print "xxxx" \ No newline at end of file +print "xxxx" +dl.log("hello dl!") \ No newline at end of file diff --git a/third/luaaa/luaaa.hpp b/third/luaaa/luaaa.hpp index 6ea435ef..5df8781d 100644 --- a/third/luaaa/luaaa.hpp +++ b/third/luaaa/luaaa.hpp @@ -797,7 +797,7 @@ namespace LUAAA_NS template TRET LuaInvokeImpl(lua_State* state, void* calleePtr, size_t skip, indices) { - return (*(FTYPE*)(calleePtr))(LuaStack::get(state, Ns + 1 + skip)...); + return (*(FTYPE*)(calleePtr))(LuaStack::get(state, (int)(Ns + 1 + skip))...); } template