145 lines
2.7 KiB
C++
145 lines
2.7 KiB
C++
#include "dl_as.h"
|
|
|
|
#include <scriptstdstring/scriptstdstring.h>
|
|
#include <scriptbuilder/scriptbuilder.h>
|
|
|
|
#include "script/dl_as_register.h"
|
|
|
|
|
|
namespace dl
|
|
{
|
|
AngelScript* g_as;
|
|
|
|
|
|
AngelScript::AngelScript() :
|
|
_fileExt{ "as" }
|
|
{
|
|
// 初始化引擎
|
|
_engine = asCreateScriptEngine();
|
|
if (!_engine)
|
|
{
|
|
log_err("asCreateScriptEngine 失败!");
|
|
return;
|
|
}
|
|
|
|
|
|
//
|
|
as_register_base(_engine);
|
|
as_register_ui(_engine);
|
|
}
|
|
|
|
void AngelScript::SetPathRoot(std::string_view str)
|
|
{
|
|
_pathRoot = File::GetAbsolute(str);
|
|
}
|
|
|
|
void AngelScript::SetFileExt(std::string_view str)
|
|
{
|
|
_fileExt = str;
|
|
}
|
|
|
|
void AngelScript::SetNamePrefix(std::string_view str)
|
|
{
|
|
_namePrefix = str;
|
|
}
|
|
|
|
|
|
void AngelScript::LoadModule(std::string_view path_name, std::string_view name)
|
|
{
|
|
std::string path_relative = File::GetRelative(path_name, _pathRoot);
|
|
std::string path_real;
|
|
std::string res_name;
|
|
if (!String::GetPathAndRes(_pathRoot, path_relative, path_real, res_name, name ))
|
|
{
|
|
log_err("获取资源名错误:{} -> {}", res_name, path_real);
|
|
return;
|
|
}
|
|
|
|
CScriptBuilder builder;
|
|
int r = builder.StartNewModule(_engine, res_name.data());
|
|
if (r < 0)
|
|
{
|
|
log_err("StartNewModule失败!");
|
|
return;
|
|
}
|
|
r = builder.AddSectionFromFile(path_real.data());
|
|
if (r < 0)
|
|
{
|
|
log_err("文件或预处理错误!");
|
|
return;
|
|
}
|
|
r = builder.BuildModule();
|
|
if (r < 0)
|
|
{
|
|
printf("编译错误!");
|
|
return;
|
|
}
|
|
|
|
std::string module_name{ name };
|
|
|
|
auto iter = _allModule.find(module_name);
|
|
if (iter == _allModule.end())
|
|
{
|
|
ModuleInfo info;
|
|
info._pathAbsolute = File::GetAbsolute(path_real);
|
|
_allModule.insert({ module_name, info });
|
|
}
|
|
else
|
|
{
|
|
ModuleInfo& info = iter->second;
|
|
if (info._pathAbsolute != path_real)
|
|
{
|
|
log_err("重载入路径不匹配:{} -> {}", path_real, info._pathAbsolute);
|
|
info._pathAbsolute = path_real;
|
|
}
|
|
}
|
|
}
|
|
|
|
void AngelScript::UnloadModule(std::string_view name)
|
|
{
|
|
|
|
}
|
|
|
|
asIScriptFunction* AngelScript::GetModuleFunction(std::string_view name, std::string_view func_decl)
|
|
{
|
|
asIScriptModule* mod = _engine->GetModule(name.data());
|
|
if (!mod)
|
|
{
|
|
log_err("module不存在:", name);
|
|
return nullptr;
|
|
}
|
|
asIScriptFunction* func = mod->GetFunctionByDecl(func_decl.data());
|
|
if (func == 0)
|
|
{
|
|
log_err("函数不存在:", func_decl);
|
|
return nullptr;
|
|
}
|
|
return func;
|
|
}
|
|
|
|
void AngelScript::ExcuteModule(std::string_view name)
|
|
{
|
|
asIScriptFunction* func = GetModuleFunction(name, "void main()");
|
|
if (func == 0)
|
|
return;
|
|
|
|
asIScriptContext* ctx = _engine->CreateContext();
|
|
ctx->Prepare(func);
|
|
|
|
int r = ctx->Execute();
|
|
if (r != asEXECUTION_FINISHED)
|
|
{
|
|
if (r == asEXECUTION_EXCEPTION)
|
|
{
|
|
log_err("脚本出现异常:{}", ctx->GetExceptionString());
|
|
}
|
|
}
|
|
ctx->Release();
|
|
}
|
|
|
|
AngelScript::~AngelScript()
|
|
{
|
|
_engine->ShutDownAndRelease();
|
|
}
|
|
|
|
} |