82 lines
1.4 KiB
C++
82 lines
1.4 KiB
C++
/**
|
|
* @file dl_lua.h
|
|
* @brief lua封装
|
|
*
|
|
*
|
|
* @version 1.0
|
|
* @author lveyou
|
|
* @date 24-12-23
|
|
*
|
|
* @note 例如 res/script/、as、mod
|
|
* 在 res/script/ui/main.as的脚本,模块名为 mod:ui/main
|
|
*
|
|
* 直接暴露lua与luabridge
|
|
*/
|
|
#pragma once
|
|
|
|
#include <string>
|
|
#include <unordered_map>
|
|
|
|
#define LUA_USE_APICHECK
|
|
#include <lua.hpp>
|
|
#include <LuaBridge/LuaBridge.h>
|
|
|
|
#include "dl_io.h"
|
|
|
|
struct lua_State;
|
|
namespace dl
|
|
{
|
|
class LuaScript
|
|
{
|
|
public:
|
|
LuaScript();
|
|
~LuaScript();
|
|
|
|
struct Env
|
|
{
|
|
//std::string _funcUpdate;
|
|
};
|
|
/**
|
|
* @brief 设置加载根路径
|
|
*/
|
|
void SetPathRoot(std::string_view str) { _pathRoot = std::string{ str }; }
|
|
|
|
|
|
bool DoFile(std::string_view path_name);
|
|
|
|
bool ExcuteFunction(std::string_view name)
|
|
{
|
|
luabridge::LuaRef v = luabridge::getGlobal(L, name.data());
|
|
luabridge::LuaResult ret = v();
|
|
if (ret.hasFailed())
|
|
{
|
|
log_warn("{}", ret.errorMessage());
|
|
return false;
|
|
}
|
|
// todo ret
|
|
return true;
|
|
}
|
|
template <typename T, typename ...Args>
|
|
bool ExcuteFunction(std::string_view name, Args&&... args)
|
|
{
|
|
luabridge::LuaRef v = luabridge::getGlobal(L, name.data());
|
|
luabridge::LuaResult ret = v(args...);
|
|
if (ret.hasFailed())
|
|
{
|
|
log_warn("{}", ret.errorMessage());
|
|
return false;
|
|
}
|
|
// todo ret
|
|
return true;
|
|
}
|
|
|
|
Env& GetEnv() { return _env; }
|
|
lua_State* GetL() { return L; }
|
|
private:
|
|
lua_State* L;
|
|
Env _env;
|
|
std::string _pathRoot;
|
|
};
|
|
|
|
extern LuaScript* g_lua;
|
|
} |