osb/source/game/StarTechDatabase.cpp

63 lines
1.9 KiB
C++
Raw Permalink Normal View History

2023-06-20 04:33:09 +00:00
#include "StarTechDatabase.hpp"
#include "StarJsonExtra.hpp"
#include "StarRoot.hpp"
#include "StarAssets.hpp"
namespace Star {
EnumMap<TechType> const TechTypeNames{
{TechType::Head, "Head"},
{TechType::Body, "Body"},
{TechType::Legs, "Legs"}
};
TechDatabase::TechDatabase() {
auto assets = Root::singleton().assets();
2024-03-15 10:28:11 +00:00
auto& files = assets->scanExtension("tech");
2023-06-20 04:33:09 +00:00
assets->queueJsons(files);
2024-03-15 10:28:11 +00:00
for (auto& file : files) {
2023-06-20 04:33:09 +00:00
auto tech = parseTech(assets->json(file), file);
if (m_tech.contains(tech.name))
2023-06-27 10:23:44 +00:00
throw TechDatabaseException::format("Duplicate tech named '{}', config file '{}'", tech.name, file);
2023-06-20 04:33:09 +00:00
m_tech[tech.name] = tech;
}
}
bool TechDatabase::contains(String const& techName) const {
return m_tech.contains(techName);
}
TechConfig TechDatabase::tech(String const& techName) const {
if (auto p = m_tech.ptr(techName))
return *p;
2023-06-27 10:23:44 +00:00
throw TechDatabaseException::format("No such tech '{}'", techName);
2023-06-20 04:33:09 +00:00
}
TechConfig TechDatabase::parseTech(Json const& config, String const& path) const {
try {
auto assets = Root::singleton().assets();
TechConfig tech;
tech.name = config.getString("name");
tech.path = path;
tech.parameters = config;
tech.type = TechTypeNames.getLeft(config.getString("type"));
tech.scripts = jsonToStringList(config.get("scripts")).transformed(bind(AssetPath::relativeTo, path, _1));
tech.animationConfig = config.optString("animator").apply(bind(&AssetPath::relativeTo, path, _1));
tech.description = config.getString("description");
tech.shortDescription = config.getString("shortDescription");
tech.rarity = RarityNames.getLeft(config.getString("rarity"));
tech.icon = AssetPath::relativeTo(path, config.getString("icon"));
return tech;
} catch (std::exception const& e) {
2023-06-27 10:23:44 +00:00
throw TechDatabaseException(strf("Error reading tech config {}", path), e);
2023-06-20 04:33:09 +00:00
}
}
}