osb/source/game/StarCodexDatabase.cpp

42 lines
1.2 KiB
C++
Raw Normal View History

2023-06-20 04:33:09 +00:00
#include "StarCodexDatabase.hpp"
#include "StarJsonExtra.hpp"
#include "StarRoot.hpp"
#include "StarAssets.hpp"
namespace Star {
CodexDatabase::CodexDatabase() {
auto assets = Root::singleton().assets();
2024-03-15 10:28:11 +00:00
auto& files = assets->scanExtension("codex");
2023-06-20 04:33:09 +00:00
auto codexConfig = assets->json("/codex.config");
assets->queueJsons(files);
2024-03-15 10:28:11 +00:00
for (auto& file : files) {
2023-06-20 04:33:09 +00:00
try {
auto codexJson = assets->json(file);
codexJson = codexJson.set("icon",
AssetPath::relativeTo(AssetPath::directory(file), codexJson.getString("icon", codexConfig.getString("defaultIcon"))));
auto codex = make_shared<Codex>(codexJson, AssetPath::directory(file));
if (m_codexes.contains(codex->id()))
2023-06-27 10:23:44 +00:00
throw CodexDatabaseException::format("Duplicate codex named '{}', config file '{}'", codex->id(), file);
2023-06-20 04:33:09 +00:00
m_codexes[codex->id()] = codex;
} catch (std::exception const& e) {
2023-06-27 10:23:44 +00:00
throw CodexDatabaseException(strf("Error reading codex config {}", file), e);
2023-06-20 04:33:09 +00:00
}
}
}
StringMap<CodexConstPtr> CodexDatabase::codexes() const {
return m_codexes;
}
CodexConstPtr CodexDatabase::codex(String const& codexId) const {
if (auto codex = m_codexes.maybe(codexId))
return codex.take();
return {};
}
}