add resource-aliases.json

This commit is contained in:
MihailRis 2024-10-14 07:18:24 +03:00
parent e154bd6ac1
commit 4c3ce8c174
4 changed files with 41 additions and 1 deletions

View File

@ -0,0 +1,8 @@
{
"camera": {
"base:first-person": "core:first-person",
"base:third-person-front": "core:third-person-front",
"base:third-person-back": "core:third-person-back",
"base:cinematic": "core:cinematic"
}
}

View File

@ -137,12 +137,21 @@ public:
static constexpr size_t MISSING = SIZE_MAX;
void add(std::string name, dv::value map) {
void add(const std::string& name, dv::value map) {
indices[name] = names.size();
names.push_back(name);
savedData->push_back(std::move(map));
}
void addAlias(const std::string& name, const std::string& alias) {
size_t index = indexOf(name);
if (index == MISSING) {
throw std::runtime_error(
"resource does not exists: "+name);
}
indices[alias] = index;
}
const std::string& getName(size_t index) const {
return names.at(index);
}

View File

@ -790,6 +790,20 @@ void ContentLoader::load() {
}
}
// Load pack resources aliases
fs::path aliasesFile = folder / fs::u8path("resource-aliases.json");
if (fs::exists(aliasesFile)) {
auto resRoot = files::read_json(aliasesFile);
for (const auto& [key, arr] : resRoot.asObject()) {
if (auto resType = ResourceType_from(key)) {
loadResourceAliases(*resType, arr);
} else {
// Ignore unknown resources
logger.warning() << "unknown resource type: " << key;
}
}
}
// Load block materials
fs::path materialsDir = folder / fs::u8path("block_materials");
if (fs::is_directory(materialsDir)) {
@ -835,3 +849,11 @@ void ContentLoader::loadResources(ResourceType type, const dv::value& list) {
);
}
}
void ContentLoader::loadResourceAliases(ResourceType type, const dv::value& aliases) {
for (const auto& [alias, name] : aliases.asObject()) {
builder.resourceIndices[static_cast<size_t>(type)].addAlias(
name.asString(), alias
);
}
}

View File

@ -54,6 +54,7 @@ class ContentLoader {
EntityDef& def, const std::string& name, const fs::path& file
);
void loadResources(ResourceType type, const dv::value& list);
void loadResourceAliases(ResourceType type, const dv::value& aliases);
void loadContent(const dv::value& map);
public: