dl
dl_as.h
浏览该文件的文档.
1
13#pragma once
14
15#include <string>
16#include <unordered_map>
17
18#include <angelscript.h>
19
20#include "dl_io.h"
21
22namespace dl
23{
26{
27public:
29 {
30 std::string _pathAbsolute;
31 };
32
37 void SetPathRoot(std::string_view str);
38 void SetFileExt(std::string_view str);
39 void SetNamePrefix(std::string_view str);
40
46 void LoadModule(std::string_view path_name, std::string_view name = {});
51 void UnloadModule(std::string_view name);
52
57 int RegisterPointer(std::string_view name);
58
59
65 asIScriptFunction* GetModuleFunction(std::string_view name, std::string_view func_decl);
66
71 void ExcuteModule(std::string_view name);
79 template <typename T, typename ...Args>
80 bool ExcuteFunction(std::string_view name, std::string_view func_decl, T& ret, Args&&... args)
81 {
82 asIScriptFunction* func = GetModuleFunction(name, func_decl);
83 if (func == 0)
84 return false;
85
86 asIScriptContext* ctx = _engine->CreateContext();
87 ctx->Prepare(func);
88
89 if (!_set_arg_imp(ctx, sizeof...(Args), std::forward<Args>(args)...))
90 {
91 log_err("参数设置失败:{} -> {}", name, func_decl);
92 return false;
93 }
94
95 int r = ctx->Execute();
96 if (r != asEXECUTION_FINISHED)
97 {
98 if (r == asEXECUTION_EXCEPTION)
99 {
100 log_err("脚本出现异常:{}", ctx->GetExceptionString());
101 }
102 ctx->Release();
103 return false;
104 }
105 ret = _get_return<T>(ctx);
106 ctx->Release();
107 return true;
108 }
109
110 asIScriptEngine* GetEngine() { return _engine; }
111
113private:
114 asIScriptEngine* _engine;
115 std::string _pathRoot; // 根路径
116 std::string _fileExt; // 脚本文件后缀
117 std::string _namePrefix;// 模块前缀
118 std::unordered_map<std::string, ModuleInfo> _allModule;
119
120
121 // 设置参数
122 bool _set_arg_imp(asIScriptContext* ctx, size_t num)
123 {
124 return true;
125 }
126
127 template <typename T, typename ...Args>
128 bool _set_arg_imp(asIScriptContext* ctx, unsigned num, T&& param, Args&&... args)
129 {
130 asUINT i = num - sizeof...(Args) - 1;
131 auto p = std::forward<T>(param);
132 int r = -1;
133
134 if constexpr (std::is_integral<T>::value)
135 {
136 if constexpr (sizeof(T) == 1)
137 r = ctx->SetArgByte(i, p);
138 else if constexpr (sizeof(T) == 2)
139 r = ctx->SetArgWord(i, p);
140 else if constexpr (sizeof(T) == 4)
141 r = ctx->SetArgDWord(i, p);
142 else if constexpr (sizeof(T) == 8)
143 r = ctx->SetArgQWord(i, p);
144 else
145 assert(0);
146 }
147 else if constexpr (std::is_pointer<T>::value)
148 {
149 if constexpr (sizeof(T) == 4)
150 r = ctx->SetArgDWord(i, p);
151 else if constexpr (sizeof(T) == 8)
152 r = ctx->SetArgQWord(i, p);
153 else
154 assert(0);
155 }
156 else if constexpr (std::is_floating_point<T>::value)
157 {
158 if constexpr (sizeof(T) == 4)
159 r = ctx->SetArgFloat(i, p);
160 else if constexpr (sizeof(T) == 8)
161 r = ctx->SetArgDouble(i, p);
162 else
163 assert(0);
164 }
165 else
166 {
167 r = ctx->SetArgObject(i, &p);
168 }
169 //
170 if (r == -1)
171 return false;
172 return _set_arg_imp(ctx, num, std::forward<Args>(args)...);
173 }
174
175 // 返回值
176 template <typename T>
177 T _get_return(asIScriptContext* ctx)
178 {
179 if constexpr (std::is_integral<T>::value)
180 {
181 if constexpr (sizeof(T) == 1)
182 return ctx->GetReturnByte();
183 else if constexpr (sizeof(T) == 2)
184 return ctx->GetReturnWord();
185 else if constexpr (sizeof(T) == 4)
186 return ctx->GetReturnDWord();
187 else if constexpr (sizeof(T) == 8)
188 return ctx->GetReturnQWord();
189 else
190 assert(0);
191 }
192 else if constexpr (std::is_pointer<T>::value)
193 {
194 return (T)ctx->GetReturnObject();
195 }
196 else if constexpr (std::is_floating_point<T>::value)
197 {
198 if constexpr (sizeof(T) == 4)
199 return ctx->GetReturnFloat();
200 else if constexpr (sizeof(T) == 8)
201 return ctx->GetReturnDouble();
202 else
203 assert(0);
204 }
205 else
206 assert(0);
207 return {};
208 }
209
210};
211
212extern AngelScript* g_as;
213}
AngelScirpt脚本语言(低优先级支持,或废弃)
定义 dl_as.h:26
int RegisterPointer(std::string_view name)
注册指针类型
void ExcuteModule(std::string_view name)
执行模块
void SetFileExt(std::string_view str)
void UnloadModule(std::string_view name)
卸载模块
bool ExcuteFunction(std::string_view name, std::string_view func_decl, T &ret, Args &&... args)
执行模块
定义 dl_as.h:80
void SetPathRoot(std::string_view str)
设置加载根路径
asIScriptEngine * GetEngine()
asIScriptFunction * GetModuleFunction(std::string_view name, std::string_view func_decl)
获取模块函数
void SetNamePrefix(std::string_view str)
void LoadModule(std::string_view path_name, std::string_view name={})
加载模块
io相关
#define log_err(...)
AngelScript * g_as