Files
dl_old/dl/misc/dl_mod.cpp
T
2026-03-10 11:39:00 +08:00

79 lines
1.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "dl_mod.h"
#ifndef DL_DISABLE_RENDER
#include "base/dl_string.h"
#include "io/dl_file.h"
#include "render/dl_factory.h"
namespace dl
{
ModManager* g_mod;
constexpr std::string_view PATH_NAME_CONFIG = "config.json";
constexpr std::string_view PATH_IMAGE = "image/";
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::Parse(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);
}
void ModManager::LoadModAll(std::string_view path)
{
std::vector<std::string> vec;
File::GetPathContent(path, vec, "", true, false);
for (auto& iter : vec)
{
LoadMod(iter);
}
}
}
#endif