Kelsidavis-WoWee/src/pipeline/custom_zone_discovery.cpp

69 lines
2.3 KiB
C++
Raw Normal View History

#include "pipeline/custom_zone_discovery.hpp"
#include "core/logger.hpp"
#include <nlohmann/json.hpp>
#include <fstream>
#include <filesystem>
namespace wowee {
namespace pipeline {
std::vector<CustomZoneInfo> CustomZoneDiscovery::scan(const std::vector<std::string>& searchPaths) {
std::vector<CustomZoneInfo> results;
for (const auto& path : searchPaths) {
auto found = scanDirectory(path);
results.insert(results.end(), found.begin(), found.end());
}
return results;
}
std::vector<CustomZoneInfo> CustomZoneDiscovery::scanDirectory(const std::string& path) {
namespace fs = std::filesystem;
std::vector<CustomZoneInfo> results;
if (!fs::exists(path)) return results;
for (auto& entry : fs::directory_iterator(path)) {
if (!entry.is_directory()) continue;
std::string zoneJson = entry.path().string() + "/zone.json";
if (!fs::exists(zoneJson)) continue;
std::ifstream f(zoneJson);
if (!f) continue;
try {
auto j = nlohmann::json::parse(f);
CustomZoneInfo info;
info.name = j.value("mapName", "");
if (info.name.empty()) info.name = j.value("name", "");
info.author = j.value("author", "");
info.description = j.value("description", "");
info.mapId = j.value("mapId", 9000u);
info.directory = entry.path().string();
info.hasCreatures = j.value("hasCreatures", false) ||
fs::exists(entry.path().string() + "/creatures.json");
info.hasQuests = fs::exists(entry.path().string() + "/quests.json");
if (j.contains("tiles") && j["tiles"].is_array()) {
for (const auto& t : j["tiles"]) {
if (t.is_array() && t.size() >= 2)
info.tiles.push_back({t[0].get<int>(), t[1].get<int>()});
}
}
if (!info.name.empty()) {
results.push_back(info);
LOG_INFO("Discovered custom zone: ", info.name, " in ", info.directory);
}
} catch (const std::exception& e) {
LOG_WARNING("Failed to parse zone.json in ", entry.path().string(), ": ", e.what());
}
}
return results;
}
} // namespace pipeline
} // namespace wowee