213 lines
4.5 KiB
C++
213 lines
4.5 KiB
C++
/**
|
|
* @file dl_as.h
|
|
* @brief angelscript封装
|
|
*
|
|
*
|
|
* @version 1.0
|
|
* @author lveyou
|
|
* @date 24-09-06
|
|
*
|
|
* @note 例如 res/script/、as、mod
|
|
* 在 res/script/ui/main.as的脚本,模块名为 mod:ui/main
|
|
*/
|
|
#pragma once
|
|
|
|
#include <string>
|
|
#include <unordered_map>
|
|
|
|
#include <angelscript.h>
|
|
|
|
#include "dl_io.h"
|
|
|
|
namespace dl
|
|
{
|
|
//! AngelScirpt脚本语言(低优先级支持,或废弃)
|
|
class AngelScript
|
|
{
|
|
public:
|
|
struct ModuleInfo
|
|
{
|
|
std::string _pathAbsolute;
|
|
};
|
|
|
|
AngelScript();
|
|
/**
|
|
* @brief 设置加载根路径
|
|
*/
|
|
void SetPathRoot(std::string_view str);
|
|
void SetFileExt(std::string_view str);
|
|
void SetNamePrefix(std::string_view str);
|
|
|
|
/**
|
|
* @brief 加载模块
|
|
* @param[in] path_name 路径
|
|
* @param[in] name 模块名(为空自动生成)
|
|
*/
|
|
void LoadModule(std::string_view path_name, std::string_view name = {});
|
|
/**
|
|
* @brief 卸载模块
|
|
* @param[in] name 模块名
|
|
*/
|
|
void UnloadModule(std::string_view name);
|
|
|
|
/**
|
|
* @brief 注册指针类型
|
|
* @param[in] name 模块名
|
|
*/
|
|
int RegisterPointer(std::string_view name);
|
|
|
|
|
|
/**
|
|
* @brief 获取模块函数
|
|
* @param[in] name 模块名
|
|
* @param[in] func_decl 函数声明
|
|
*/
|
|
asIScriptFunction* GetModuleFunction(std::string_view name, std::string_view func_decl);
|
|
|
|
/**
|
|
* @brief 执行模块
|
|
* @param[in] name 模块名
|
|
*/
|
|
void ExcuteModule(std::string_view name);
|
|
/**
|
|
* @brief 执行模块
|
|
* @param[in] name 模块名
|
|
* @param[in] func_decl 函数声明
|
|
* @param[out] ret 返回值
|
|
* @param[in] ... 参数
|
|
*/
|
|
template <typename T, typename ...Args>
|
|
bool ExcuteFunction(std::string_view name, std::string_view func_decl, T& ret, Args&&... args)
|
|
{
|
|
asIScriptFunction* func = GetModuleFunction(name, func_decl);
|
|
if (func == 0)
|
|
return false;
|
|
|
|
asIScriptContext* ctx = _engine->CreateContext();
|
|
ctx->Prepare(func);
|
|
|
|
if (!_set_arg_imp(ctx, sizeof...(Args), std::forward<Args>(args)...))
|
|
{
|
|
log_err("参数设置失败:{} -> {}", name, func_decl);
|
|
return false;
|
|
}
|
|
|
|
int r = ctx->Execute();
|
|
if (r != asEXECUTION_FINISHED)
|
|
{
|
|
if (r == asEXECUTION_EXCEPTION)
|
|
{
|
|
log_err("脚本出现异常:{}", ctx->GetExceptionString());
|
|
}
|
|
ctx->Release();
|
|
return false;
|
|
}
|
|
ret = _get_return<T>(ctx);
|
|
ctx->Release();
|
|
return true;
|
|
}
|
|
|
|
asIScriptEngine* GetEngine() { return _engine; }
|
|
|
|
~AngelScript();
|
|
private:
|
|
asIScriptEngine* _engine;
|
|
std::string _pathRoot; // 根路径
|
|
std::string _fileExt; // 脚本文件后缀
|
|
std::string _namePrefix;// 模块前缀
|
|
std::unordered_map<std::string, ModuleInfo> _allModule;
|
|
|
|
|
|
// 设置参数
|
|
bool _set_arg_imp(asIScriptContext* ctx, size_t num)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
template <typename T, typename ...Args>
|
|
bool _set_arg_imp(asIScriptContext* ctx, unsigned num, T&& param, Args&&... args)
|
|
{
|
|
asUINT i = num - sizeof...(Args) - 1;
|
|
auto p = std::forward<T>(param);
|
|
int r = -1;
|
|
|
|
if constexpr (std::is_integral<T>::value)
|
|
{
|
|
if constexpr (sizeof(T) == 1)
|
|
r = ctx->SetArgByte(i, p);
|
|
else if constexpr (sizeof(T) == 2)
|
|
r = ctx->SetArgWord(i, p);
|
|
else if constexpr (sizeof(T) == 4)
|
|
r = ctx->SetArgDWord(i, p);
|
|
else if constexpr (sizeof(T) == 8)
|
|
r = ctx->SetArgQWord(i, p);
|
|
else
|
|
assert(0);
|
|
}
|
|
else if constexpr (std::is_pointer<T>::value)
|
|
{
|
|
if constexpr (sizeof(T) == 4)
|
|
r = ctx->SetArgDWord(i, p);
|
|
else if constexpr (sizeof(T) == 8)
|
|
r = ctx->SetArgQWord(i, p);
|
|
else
|
|
assert(0);
|
|
}
|
|
else if constexpr (std::is_floating_point<T>::value)
|
|
{
|
|
if constexpr (sizeof(T) == 4)
|
|
r = ctx->SetArgFloat(i, p);
|
|
else if constexpr (sizeof(T) == 8)
|
|
r = ctx->SetArgDouble(i, p);
|
|
else
|
|
assert(0);
|
|
}
|
|
else
|
|
{
|
|
r = ctx->SetArgObject(i, &p);
|
|
}
|
|
//
|
|
if (r == -1)
|
|
return false;
|
|
return _set_arg_imp(ctx, num, std::forward<Args>(args)...);
|
|
}
|
|
|
|
// 返回值
|
|
template <typename T>
|
|
T _get_return(asIScriptContext* ctx)
|
|
{
|
|
if constexpr (std::is_integral<T>::value)
|
|
{
|
|
if constexpr (sizeof(T) == 1)
|
|
return ctx->GetReturnByte();
|
|
else if constexpr (sizeof(T) == 2)
|
|
return ctx->GetReturnWord();
|
|
else if constexpr (sizeof(T) == 4)
|
|
return ctx->GetReturnDWord();
|
|
else if constexpr (sizeof(T) == 8)
|
|
return ctx->GetReturnQWord();
|
|
else
|
|
assert(0);
|
|
}
|
|
else if constexpr (std::is_pointer<T>::value)
|
|
{
|
|
return (T)ctx->GetReturnObject();
|
|
}
|
|
else if constexpr (std::is_floating_point<T>::value)
|
|
{
|
|
if constexpr (sizeof(T) == 4)
|
|
return ctx->GetReturnFloat();
|
|
else if constexpr (sizeof(T) == 8)
|
|
return ctx->GetReturnDouble();
|
|
else
|
|
assert(0);
|
|
}
|
|
else
|
|
assert(0);
|
|
return {};
|
|
}
|
|
|
|
};
|
|
|
|
extern AngelScript* g_as;
|
|
} |