118 lines
2.3 KiB
C++
118 lines
2.3 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
|
|
//#define LUABRIDGE_HAS_EXCEPTIONS 0
|
|
|
|
// std::span会转为lua的table,故而无法转回来,所以关闭默认实现
|
|
#define LUABRIDGE_DISABLE_CXX20_SPAN
|
|
#include <lua.hpp>
|
|
#include <LuaBridge/LuaBridge.h>
|
|
|
|
#include "script/dl_lua_register_container.h"
|
|
#include "dl_io.h"
|
|
|
|
struct lua_State;
|
|
namespace dl
|
|
{
|
|
|
|
using luabridge::LuaRef;
|
|
|
|
//! lua脚本语言
|
|
class LuaScript
|
|
{
|
|
public:
|
|
LuaScript();
|
|
~LuaScript();
|
|
|
|
struct Env
|
|
{
|
|
//std::string _funcUpdate;
|
|
};
|
|
/**
|
|
* @brief 设置加载根路径
|
|
*/
|
|
void SetPathRoot(std::string_view str) { _pathRoot = std::string{ str }; }
|
|
|
|
/**
|
|
* @brief 执行lua字符串
|
|
*/
|
|
bool DoString(std::string_view str);
|
|
/**
|
|
* @brief 执行lua文件
|
|
*/
|
|
bool DoFile(std::string_view path_name);
|
|
/**
|
|
* @brief 执行lua文件,并获取返回值
|
|
*/
|
|
bool DoFileEx(std::string_view path_name, LuaRef& ret);
|
|
|
|
//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;
|
|
//}
|
|
|
|
/**
|
|
* @brief 调用函数
|
|
* @param[in] func lua对象(函数)
|
|
* @note 失败会打印日志
|
|
*/
|
|
bool CallFunction(LuaRef func);
|
|
|
|
/**
|
|
* @brief 返回lua对象字符串
|
|
* @param[in] func lua对象(函数、字符串)
|
|
* @retval 失败返回空(不会打印)
|
|
*/
|
|
std::string GetRefString(LuaRef ref);
|
|
|
|
Env& GetEnv() { return _env; }
|
|
lua_State* GetL() { return L; }
|
|
|
|
// 由引擎调用
|
|
void RegisterAll();
|
|
private:
|
|
lua_State* L;
|
|
Env _env;
|
|
std::string _pathRoot;
|
|
};
|
|
|
|
extern LuaScript* g_lua;
|
|
} |