60 lines
1.6 KiB
C++
60 lines
1.6 KiB
C++
#include "ItemRegistry.h"
|
|
#include "utils/Utils.h"
|
|
#include "Localization.h"
|
|
#include "external/nlohmann/json.hpp"
|
|
#include <iostream>
|
|
|
|
namespace ZL {
|
|
|
|
ItemRegistry& ItemRegistry::instance() {
|
|
static ItemRegistry reg;
|
|
return reg;
|
|
}
|
|
|
|
void ItemRegistry::loadFromJson(const std::string& jsonPath, const std::string& zipPath) {
|
|
const std::string content = readLocalizedConfigFile(jsonPath, zipPath);
|
|
if (content.empty()) {
|
|
std::cerr << "[ItemRegistry] Failed to read " << jsonPath << "\n";
|
|
return;
|
|
}
|
|
|
|
using json = nlohmann::json;
|
|
json j;
|
|
try {
|
|
j = json::parse(content);
|
|
} catch (const std::exception& e) {
|
|
std::cerr << "[ItemRegistry] JSON parse error in " << jsonPath << ": " << e.what() << "\n";
|
|
return;
|
|
}
|
|
|
|
if (!j.contains("items") || !j["items"].is_array()) {
|
|
std::cerr << "[ItemRegistry] No 'items' array in " << jsonPath << "\n";
|
|
return;
|
|
}
|
|
|
|
items_.clear();
|
|
|
|
for (const auto& entry : j["items"]) {
|
|
std::string id = entry.value("id", "");
|
|
if (id.empty()) continue;
|
|
|
|
Item item(
|
|
id,
|
|
entry.value("name", "Unknown"),
|
|
entry.value("description", ""),
|
|
entry.value("icon", "")
|
|
);
|
|
item.selectedIcon = entry.value("selectedIcon", "");
|
|
items_[id] = std::move(item);
|
|
}
|
|
|
|
logger() << "[ItemRegistry] Loaded " << items_.size() << " items from " << jsonPath << "\n";
|
|
}
|
|
|
|
const Item* ItemRegistry::findById(const std::string& id) const {
|
|
auto it = items_.find(id);
|
|
return it != items_.end() ? &it->second : nullptr;
|
|
}
|
|
|
|
} // namespace ZL
|