refactor: migrate all remaining JSON to nlohmann/json

- npc_spawner: save/load with proper JSON (25+ fields + patrol paths)
- zone_manifest: save/load with nlohmann (was naive string concat/parse)
  - load now parses all fields: mapId, baseHeight, tiles, hasCreatures
- custom_zone_discovery: parse zone.json with nlohmann, extract mapId
  and tile coordinates (was only reading name/author/description)
- object_placer: save/load with nlohmann (was substring parsing)
- editor_app: stats.json export uses nlohmann, score display now /6

Zero naive JSON string concatenation remains in the editor codebase.
This commit is contained in:
Kelsi 2026-05-05 13:10:07 -07:00
parent 815787933b
commit 08500384e2
5 changed files with 223 additions and 268 deletions

View file

@ -1,5 +1,6 @@
#include "zone_manifest.hpp"
#include "core/logger.hpp"
#include <nlohmann/json.hpp>
#include <fstream>
#include <filesystem>
#include <chrono>
@ -12,44 +13,40 @@ bool ZoneManifest::save(const std::string& path) const {
auto dir = std::filesystem::path(path).parent_path();
if (!dir.empty()) std::filesystem::create_directories(dir);
std::ofstream f(path);
if (!f) { LOG_ERROR("Failed to write zone manifest: ", path); return false; }
nlohmann::json j;
j["mapName"] = mapName;
j["displayName"] = displayName;
j["mapId"] = mapId;
j["biome"] = biome;
j["baseHeight"] = baseHeight;
j["hasCreatures"] = hasCreatures;
j["description"] = description;
j["editorVersion"] = "1.0.0";
f << "{\n";
f << " \"mapName\": \"" << mapName << "\",\n";
f << " \"displayName\": \"" << displayName << "\",\n";
f << " \"mapId\": " << mapId << ",\n";
f << " \"biome\": \"" << biome << "\",\n";
f << " \"baseHeight\": " << baseHeight << ",\n";
f << " \"hasCreatures\": " << (hasCreatures ? "true" : "false") << ",\n";
f << " \"description\": \"" << description << "\",\n";
f << " \"editorVersion\": \"0.9.0\",\n";
// Add export timestamp
{
auto now = std::chrono::system_clock::now();
auto time = std::chrono::system_clock::to_time_t(now);
char timeBuf[32];
std::strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%dT%H:%M:%S", std::localtime(&time));
f << " \"exportTime\": \"" << timeBuf << "\",\n";
j["exportTime"] = timeBuf;
}
f << " \"tiles\": [";
for (size_t i = 0; i < tiles.size(); i++) {
f << "[" << tiles[i].first << "," << tiles[i].second << "]";
if (i + 1 < tiles.size()) f << ",";
nlohmann::json tilesArr = nlohmann::json::array();
for (const auto& t : tiles) tilesArr.push_back({t.first, t.second});
j["tiles"] = tilesArr;
nlohmann::json files;
files["wdt"] = mapName + ".wdt";
for (const auto& t : tiles) {
std::string key = "adt_" + std::to_string(t.first) + "_" + std::to_string(t.second);
files[key] = mapName + "_" + std::to_string(t.first) + "_" + std::to_string(t.second) + ".adt";
}
f << "],\n";
f << " \"files\": {\n";
f << " \"wdt\": \"" << mapName << ".wdt\",\n";
for (size_t i = 0; i < tiles.size(); i++) {
f << " \"adt_" << tiles[i].first << "_" << tiles[i].second << "\": \""
<< mapName << "_" << tiles[i].first << "_" << tiles[i].second << ".adt\"";
if (i + 1 < tiles.size() || hasCreatures) f << ",";
f << "\n";
}
if (hasCreatures)
f << " \"creatures\": \"creatures.json\"\n";
f << " }\n";
f << "}\n";
if (hasCreatures) files["creatures"] = "creatures.json";
j["files"] = files;
std::ofstream f(path);
if (!f) { LOG_ERROR("Failed to write zone manifest: ", path); return false; }
f << j.dump(2) << "\n";
LOG_INFO("Zone manifest saved: ", path);
return true;
@ -58,30 +55,32 @@ bool ZoneManifest::save(const std::string& path) const {
bool ZoneManifest::load(const std::string& path) {
std::ifstream f(path);
if (!f) return false;
std::string content((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
auto findStr = [&](const std::string& key) -> std::string {
auto pos = content.find("\"" + key + "\"");
if (pos == std::string::npos) return "";
pos = content.find('"', content.find(':', pos) + 1);
if (pos == std::string::npos) return "";
auto end = content.find('"', pos + 1);
return content.substr(pos + 1, end - pos - 1);
};
try {
auto j = nlohmann::json::parse(f);
mapName = findStr("mapName");
displayName = findStr("displayName");
biome = findStr("biome");
description = findStr("description");
mapName = j.value("mapName", "");
if (mapName.empty()) mapName = j.value("name", "");
displayName = j.value("displayName", mapName);
biome = j.value("biome", "");
description = j.value("description", "");
mapId = j.value("mapId", 9000u);
baseHeight = j.value("baseHeight", 100.0f);
hasCreatures = j.value("hasCreatures", false);
auto numPos = content.find("\"mapId\"");
if (numPos != std::string::npos) {
numPos = content.find(':', numPos);
mapId = static_cast<uint32_t>(std::stoi(content.substr(numPos + 1)));
tiles.clear();
if (j.contains("tiles") && j["tiles"].is_array()) {
for (const auto& t : j["tiles"]) {
if (t.is_array() && t.size() >= 2)
tiles.push_back({t[0].get<int>(), t[1].get<int>()});
}
}
return !mapName.empty();
} catch (const std::exception& e) {
LOG_ERROR("Failed to parse zone manifest: ", e.what());
return false;
}
return !mapName.empty();
}
} // namespace editor