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{
25{
26public:
28 {
29 std::string _pathAbsolute;
30 };
31
36 void SetPathRoot(std::string_view str);
37 void SetFileExt(std::string_view str);
38 void SetNamePrefix(std::string_view str);
39
45 void LoadModule(std::string_view path_name, std::string_view name = {});
50 void UnloadModule(std::string_view name);
51
56 int RegisterPointer(std::string_view name);
57
58
64 asIScriptFunction* GetModuleFunction(std::string_view name, std::string_view func_decl);
65
70 void ExcuteModule(std::string_view name);
78 template <typename T, typename ...Args>
79 bool ExcuteFunction(std::string_view name, std::string_view func_decl, T& ret, Args&&... args)
80 {
81 asIScriptFunction* func = GetModuleFunction(name, func_decl);
82 if (func == 0)
83 return false;
84
85 asIScriptContext* ctx = _engine->CreateContext();
86 ctx->Prepare(func);
87
88 if (!_set_arg_imp(ctx, sizeof...(Args), std::forward<Args>(args)...))
89 {
90 log_err("参数设置失败:{} -> {}", name, func_decl);
91 return false;
92 }
93
94 int r = ctx->Execute();
95 if (r != asEXECUTION_FINISHED)
96 {
97 if (r == asEXECUTION_EXCEPTION)
98 {
99 log_err("脚本出现异常:{}", ctx->GetExceptionString());
100 }
101 ctx->Release();
102 return false;
103 }
104 ret = _get_return<T>(ctx);
105 ctx->Release();
106 return true;
107 }
108
109 asIScriptEngine* GetEngine() { return _engine; }
110
112private:
113 asIScriptEngine* _engine;
114 std::string _pathRoot; // 根路径
115 std::string _fileExt; // 脚本文件后缀
116 std::string _namePrefix;// 模块前缀
117 std::unordered_map<std::string, ModuleInfo> _allModule;
118
119
120 // 设置参数
121 bool _set_arg_imp(asIScriptContext* ctx, size_t num)
122 {
123 return true;
124 }
125
126 template <typename T, typename ...Args>
127 bool _set_arg_imp(asIScriptContext* ctx, unsigned num, T&& param, Args&&... args)
128 {
129 asUINT i = num - sizeof...(Args) - 1;
130 auto p = std::forward<T>(param);
131 int r = -1;
132
133 if constexpr (std::is_integral<T>::value)
134 {
135 if constexpr (sizeof(T) == 1)
136 r = ctx->SetArgByte(i, p);
137 else if constexpr (sizeof(T) == 2)
138 r = ctx->SetArgWord(i, p);
139 else if constexpr (sizeof(T) == 4)
140 r = ctx->SetArgDWord(i, p);
141 else if constexpr (sizeof(T) == 8)
142 r = ctx->SetArgQWord(i, p);
143 else
144 static_assert(0);
145 }
146 else if constexpr (std::is_pointer<T>::value)
147 {
148 if constexpr (sizeof(T) == 4)
149 r = ctx->SetArgDWord(i, p);
150 else if constexpr (sizeof(T) == 8)
151 r = ctx->SetArgQWord(i, p);
152 else
153 static_assert(0);
154 }
155 else if constexpr (std::is_floating_point<T>::value)
156 {
157 if constexpr (sizeof(T) == 4)
158 r = ctx->SetArgFloat(i, p);
159 else if constexpr (sizeof(T) == 8)
160 r = ctx->SetArgDouble(i, p);
161 else
162 static_assert(0);
163 }
164 else
165 {
166 r = ctx->SetArgObject(i, &p);
167 }
168 //
169 if (r == -1)
170 return false;
171 return _set_arg_imp(ctx, num, std::forward<Args>(args)...);
172 }
173
174 // 返回值
175 template <typename T>
176 T _get_return(asIScriptContext* ctx)
177 {
178 if constexpr (std::is_integral<T>::value)
179 {
180 if constexpr (sizeof(T) == 1)
181 return ctx->GetReturnByte();
182 else if constexpr (sizeof(T) == 2)
183 return ctx->GetReturnWord();
184 else if constexpr (sizeof(T) == 4)
185 return ctx->GetReturnDWord();
186 else if constexpr (sizeof(T) == 8)
187 return ctx->GetReturnQWord();
188 else
189 static_assert(0);
190 }
191 else if constexpr (std::is_pointer<T>::value)
192 {
193 return (T)ctx->GetReturnObject();
194 }
195 else if constexpr (std::is_floating_point<T>::value)
196 {
197 if constexpr (sizeof(T) == 4)
198 return ctx->GetReturnFloat();
199 else if constexpr (sizeof(T) == 8)
200 return ctx->GetReturnDouble();
201 else
202 static_assert(0);
203 }
204 else
205 static_assert(0);
206 }
207
208};
209
210extern AngelScript* g_as;
211}
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:79
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