#include "dl_mod.h" #ifndef DL_DISABLE_RENDER #include "base/dl_string.h" #include "io/dl_file.h" #include "render/dl_factory.h" #include "script/dl_lua.h" #include "misc/dl_language.h" namespace dl { ModManager* g_mod; constexpr std::string_view PATH_NAME_CONFIG = "config.json"; constexpr std::string_view PATH_IMAGE = "image/"; constexpr std::string_view PATH_LANGUAGE = "language/"; constexpr std::string_view PATH_SCRIPT = "script/"; Mod::Mod() : _uid{ 0 }, _name{ "no name" }, _author{ "unknown" }, _versionType{ "test" }, _versionCode{}, _id{ (size_t)-1}, _state{ State::ERR } { } bool Mod::_load_config(json& j) { if (!Json::Get(j, "uid", _uid)) return false; if (!Json::Get(j, "name", _name)) return false; Json::DisableLog l; Json::Get(j, "author", _author); Json::Get(j, "version_type", _versionType); Json::Get(j, "version_code", _versionCode); return true; } void ModManager::LoadMod(std::string_view path) { _allMod.push_back({}); Mod& mod = _allMod.back(); mod._pathRoot = path; // 1.读取根配置 json j = Json::ParseFile(path + PATH_NAME_CONFIG); if (j.is_null()) { log_warn("配置文件加载失败:{}", path + PATH_NAME_CONFIG); return; } if (!mod._load_config(j)) { log_warn("配置文件解析失败:{}", path + PATH_NAME_CONFIG); return; } log_msg("识别到一个mod:{}\n" "name:{}\n" "version:{}\n", path + PATH_NAME_CONFIG, mod._name, dl::to_string(mod._versionCode)); g_factory->RegisterImagePath(mod._pathRoot + PATH_IMAGE, mod._name); { std::string path_lang = mod._pathRoot + PATH_LANGUAGE; std::vector vec_path; File::SearchRule rule; rule._vecFmt.push_back("xlsx"); rule._vecFilter.push_back("."); File::GetPathFileAll(path_lang, vec_path, rule, true); for (auto& iter : vec_path) { g_lang->LoadXlsx(iter); } } } void ModManager::LoadModAll(std::string_view path) { std::vector vec; File::GetPathContent(path, vec, "", true, false); for (auto& iter : vec) { LoadMod(iter); } } void ModManager::CallLuaScript(std::string_view path_name) { for (auto& iter : _allMod) { _pathCurrentMod = iter._pathRoot; std::string path_lua = fmt::format("{}script/{}", iter._pathRoot, path_name); if(File::IsExist(path_lua)) g_lua->DoFile(path_lua); } } } #endif