* lua绑定库替换为sol2

This commit is contained in:
2026-04-05 16:12:20 +08:00
parent 556ad75f61
commit ee346c8f28
19 changed files with 362 additions and 503 deletions
+22 -18
View File
@@ -13,27 +13,22 @@ namespace dl
LuaScript::LuaScript()
{
L = luaL_newstate();
luaL_openlibs(L);
_l.open_libraries(sol::lib::base, sol::lib::string, sol::lib::math);
}
LuaScript::~LuaScript()
{
lua_close(L);
}
bool LuaScript::DoString(std::string_view str)
{
int ret = luaL_loadstring(L, str.data());
if (ret)
try
{
log_err("lua 解析失败[{}]:\n{}", lua_tostring(L, -1), str);
return false;
auto ret = _l.script(str);
}
ret = lua_pcall(L, 0, 0, 0);
if (ret)
catch (sol::error& e)
{
log_err("lua 执行失败[{}]:\n{}", lua_tostring(L, -1), str);
log_warn("{}\n{}", e.what(), str);
return false;
}
return true;
@@ -41,17 +36,26 @@ namespace dl
bool LuaScript::DoFile(std::string_view path_name)
{
Buffer buf = File::GetBuffer(path_name);
int ret = luaL_loadbuffer(L, (const char*)buf.data(), buf.size(), path_name.data());
if (ret)
try
{
log_err("lua 解析(加载)失败[{}]:{}", path_name, lua_tostring(L, -1));
std::string str = File::GetString(path_name);
auto ret = _l.script(str, std::string{ path_name });
}
catch (sol::error& e)
{
log_warn("{}", e.what());
return false;
}
ret = lua_pcall(L, 0, 0, 0);
if (ret)
return true;
}
bool LuaScript::ExcuteFunction(sol::function func)
{
sol::function_result ret = func();
if (!ret.valid())
{
log_err("lua 执行失败[{}]:{}", path_name, lua_tostring(L, -1));
sol::error e = ret;
log_warn("{}", e.what());
return false;
}
return true;
@@ -59,7 +63,7 @@ namespace dl
void LuaScript::RegisterAll()
{
lua_register_all(L);
lua_register_all(_l);
}
}