* as初步
This commit is contained in:
@@ -31,6 +31,7 @@
|
||||
#include <dl_render.h>
|
||||
|
||||
#include <misc/dl_code_writer.h>
|
||||
#include <misc/dl_mod.h>
|
||||
|
||||
#include <script/dl_script.h>
|
||||
#include <script/dl_script_test.h>
|
||||
|
||||
+15
-5
@@ -1,16 +1,21 @@
|
||||
#include "dl_mod.h"
|
||||
|
||||
#include "base/dl_string.h"
|
||||
|
||||
namespace dl
|
||||
{
|
||||
ModManager* g_mod;
|
||||
|
||||
constexpr std::string_view PATH_NAME_CONFIG = "config.json";
|
||||
|
||||
Mod::Mod() :
|
||||
_uid{ 0 },
|
||||
_name{ "no name" },
|
||||
_author{ "unknown" },
|
||||
_versionType{"test"},
|
||||
_versionCode{}
|
||||
_versionType{ "test" },
|
||||
_versionCode{},
|
||||
_id{ (size_t)-1},
|
||||
_state{ State::ERR }
|
||||
{
|
||||
|
||||
}
|
||||
@@ -18,6 +23,7 @@ Mod::Mod() :
|
||||
|
||||
bool Mod::_load_config(json& j)
|
||||
{
|
||||
if (!Json::Get(j, "uid", _uid)) return false;
|
||||
if (!Json::Get(j, "name", _name)) return false;
|
||||
Json::Get(j, "author", _author);
|
||||
Json::Get(j, "version_type", _versionType);
|
||||
@@ -27,20 +33,24 @@ bool Mod::_load_config(json& j)
|
||||
|
||||
void ModManager::LoadMod(std::string_view path)
|
||||
{
|
||||
_allMod.push_back({});
|
||||
Mod& mod = _allMod.back();
|
||||
mod._pathRoot = path;
|
||||
|
||||
// 1.读取根配置
|
||||
json j = Json::Parse(path);
|
||||
if (j.is_null())
|
||||
{
|
||||
log_err("配置文件加载失败:{}", path + PATH_NAME_CONFIG);
|
||||
log_warn("配置文件加载失败:{}", path + PATH_NAME_CONFIG);
|
||||
return;
|
||||
}
|
||||
|
||||
Mod mod;
|
||||
if (!mod._load_config(j))
|
||||
{
|
||||
log_err("配置文件解析失败:{}", path + PATH_NAME_CONFIG);
|
||||
log_warn("配置文件解析失败:{}", path + PATH_NAME_CONFIG);
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,13 +20,25 @@ class Mod
|
||||
{
|
||||
friend class ModManager;
|
||||
public:
|
||||
enum class State
|
||||
{
|
||||
ERR, // 错误
|
||||
DISABLE, // 未启用
|
||||
NORMAL, // 正常
|
||||
};
|
||||
Mod();
|
||||
private:
|
||||
//-------------------------config-------------------------
|
||||
uint64_t _uid;
|
||||
std::string _name;
|
||||
std::string _author;
|
||||
std::string _versionType;
|
||||
std::array<size_t, 3> _versionCode;
|
||||
|
||||
size_t _id;
|
||||
State _state;
|
||||
std::string _pathRoot;// 根目录
|
||||
|
||||
bool _load_config(json& j);
|
||||
};
|
||||
|
||||
@@ -36,9 +48,12 @@ public:
|
||||
/**
|
||||
* @brief 加载模组
|
||||
* @param[in] path 路径
|
||||
* @note 无效会创建空Mod
|
||||
*/
|
||||
void LoadMod(std::string_view path);
|
||||
private:
|
||||
std::vector<Mod> _allMod;
|
||||
};
|
||||
|
||||
extern ModManager* g_mod;
|
||||
}
|
||||
|
||||
+33
-14
@@ -10,6 +10,8 @@
|
||||
|
||||
namespace dl
|
||||
{
|
||||
AngelScript* g_as;
|
||||
|
||||
void as_print(const std::string& msg)
|
||||
{
|
||||
log_msg0(msg);
|
||||
@@ -24,7 +26,8 @@ void as_message_callback(const asSMessageInfo* msg, void* param)
|
||||
log_err("{}({},{}): {}", msg->section, msg->row, msg->col, msg->message);
|
||||
}
|
||||
|
||||
AngelScript::AngelScript()
|
||||
AngelScript::AngelScript() :
|
||||
_fileExt{ "as" }
|
||||
{
|
||||
// 初始化引擎
|
||||
_engine = asCreateScriptEngine();
|
||||
@@ -45,37 +48,54 @@ AngelScript::AngelScript()
|
||||
r = _engine->RegisterGlobalFunction("void print(const string &in)", asFUNCTION(as_print), asCALL_CDECL); assert(r >= 0);
|
||||
}
|
||||
|
||||
void AngelScript::LoadFile(std::string_view path_name, std::string_view name_module)
|
||||
void AngelScript::Init(std::string_view path, std::string_view file_ext, std::string_view name_prefix)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void AngelScript::SetPathRoot(std::string_view path)
|
||||
{
|
||||
_pathRoot = path;
|
||||
}
|
||||
|
||||
void AngelScript::LoadModule(std::string_view module_name)
|
||||
{
|
||||
std::string real_path = fmt::format("{}{}.{}", _pathRoot, module_name, _fileExt);
|
||||
|
||||
CScriptBuilder builder;
|
||||
int r = builder.StartNewModule(_engine, name_module.data());
|
||||
int r = builder.StartNewModule(_engine, module_name.data());
|
||||
if (r < 0)
|
||||
{
|
||||
log_err("StartNewModule失败!");
|
||||
return;
|
||||
}
|
||||
r = builder.AddSectionFromFile(path_name.data());
|
||||
r = builder.AddSectionFromFile(real_path.data());
|
||||
if (r < 0)
|
||||
{
|
||||
// The builder wasn't able to load the file. Maybe the file
|
||||
// has been removed, or the wrong name was given, or some
|
||||
// preprocessing commands are incorrectly written.
|
||||
log_err("脚本错误!");
|
||||
log_err("文件或预处理错误!");
|
||||
return;
|
||||
}
|
||||
r = builder.BuildModule();
|
||||
if (r < 0)
|
||||
{
|
||||
// An error occurred. Instruct the script writer to fix the
|
||||
// compilation errors that were listed in the output stream.
|
||||
printf("Please correct the errors in the script and try again.\n");
|
||||
printf("编译错误!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void AngelScript::Excute(std::string_view name_module)
|
||||
void AngelScript::UnloadModule(std::string_view module_name)
|
||||
{
|
||||
asIScriptModule* mod = _engine->GetModule(name_module.data());
|
||||
|
||||
}
|
||||
|
||||
void AngelScript::ExcuteModule(std::string_view module_name)
|
||||
{
|
||||
asIScriptModule* mod = _engine->GetModule(module_name.data());
|
||||
if (!mod)
|
||||
{
|
||||
log_err("module不存在:", module_name);
|
||||
return;
|
||||
}
|
||||
asIScriptFunction* func = mod->GetFunctionByDecl("void main()");
|
||||
if (func == 0)
|
||||
{
|
||||
@@ -88,7 +108,6 @@ void AngelScript::Excute(std::string_view name_module)
|
||||
int r = ctx->Execute();
|
||||
if (r != asEXECUTION_FINISHED)
|
||||
{
|
||||
// The execution didn't complete as expected. Determine what happened.
|
||||
if (r == asEXECUTION_EXCEPTION)
|
||||
{
|
||||
log_err("脚本出现异常:{}", ctx->GetExceptionString());
|
||||
|
||||
+42
-11
@@ -17,17 +17,48 @@
|
||||
|
||||
namespace dl
|
||||
{
|
||||
class AngelScript
|
||||
class AngelScript
|
||||
{
|
||||
public:
|
||||
struct ModuleInfo
|
||||
{
|
||||
public:
|
||||
AngelScript();
|
||||
|
||||
void LoadFile(std::string_view path_name, std::string_view name_module);
|
||||
|
||||
void Excute(std::string_view name_module);
|
||||
|
||||
~AngelScript();
|
||||
private:
|
||||
asIScriptEngine* _engine;
|
||||
std::string _pathAbsolute;
|
||||
};
|
||||
|
||||
AngelScript();
|
||||
/**
|
||||
* @brief 初始化设置加载根路径、模块名前缀
|
||||
* @param[in] path_root 加载根路径
|
||||
* @param[in] file_ext 文件后缀
|
||||
* @param[in] name_prefix 模块名前缀
|
||||
* @note 例如 res/script/、as、game
|
||||
* 在 res/script/ui/main.as的脚本,模块名为game:main
|
||||
*/
|
||||
void Init(std::string_view path, std::string_view file_ext, std::string_view name_prefix);
|
||||
|
||||
/**
|
||||
* @brief 加载模块
|
||||
* @param[in] name 模块名
|
||||
*/
|
||||
void LoadModule(std::string_view name);
|
||||
/**
|
||||
* @brief 卸载模块
|
||||
* @param[in] name 模块名
|
||||
*/
|
||||
void UnloadModule(std::string_view name);
|
||||
/**
|
||||
* @brief 执行模块
|
||||
* @param[in] name 模块名
|
||||
*/
|
||||
void ExcuteModule(std::string_view name);
|
||||
|
||||
~AngelScript();
|
||||
private:
|
||||
asIScriptEngine* _engine;
|
||||
std::string _pathRoot; // 根路径(可修改)
|
||||
std::string _fileExt; // 脚本文件后缀(可修改)
|
||||
std::unordered_map<std::string, ModuleInfo> _allModule;
|
||||
};
|
||||
|
||||
extern AngelScript* g_as;
|
||||
}
|
||||
@@ -13,6 +13,7 @@
|
||||
#include "dl_system.h"
|
||||
#include "ui/dl_ui_manager.h"
|
||||
#include "script/dl_script.h"
|
||||
#include "script/dl_as.h"
|
||||
#include "render/dl_graphics.h"
|
||||
#include "render/dl_action.h"
|
||||
#include "io/dl_net.h"
|
||||
@@ -22,6 +23,7 @@
|
||||
#include "ui/dl_ui_scene.h"
|
||||
#include "io/dl_http.h"
|
||||
#include "misc/dl_language.h"
|
||||
#include "misc/dl_mod.h"
|
||||
|
||||
|
||||
namespace dl
|
||||
@@ -90,6 +92,8 @@ void Engine::Init()
|
||||
//g_lang = new Language;
|
||||
g_physics = _param._useFreetype ? new Physics : nullptr;
|
||||
g_script = new ScriptSystem;
|
||||
g_as = new AngelScript;
|
||||
g_mod = new ModManager;
|
||||
#ifndef DL_DISABLE_AUDIO
|
||||
g_audio = new Audio(_param._useAudio);
|
||||
#endif
|
||||
@@ -113,7 +117,9 @@ void Engine::Release()
|
||||
delete g_lang;
|
||||
delete g_http;
|
||||
delete g_action;
|
||||
delete g_net;
|
||||
delete g_net;
|
||||
delete g_mod;
|
||||
delete g_as;
|
||||
delete g_script;
|
||||
#ifndef DL_DISABLE_RENDER
|
||||
delete g_scene;
|
||||
|
||||
Reference in New Issue
Block a user