feat(editor): add WSRG JSON round-trip (--export/--import-wsrg-json)

Closes the editing loop on the spell-range bucket catalog: dump a
.wsrg to a JSON sidecar, hand-edit the buckets (rename, retune
yards, recolor HUD indicator), re-import to a byte-identical
binary. The exporter emits both the int rangeKind (0..6) and the
human-readable rangeKindName ("self", "melee", "short", "ranged",
"long", "very-long", "unlimited"); the importer accepts either,
so JSON sidecars stay readable without losing the canonical binary
encoding.

Verified byte-identical round-trip on all three presets (starter,
ranged, friendly). CLI flag count 890 -> 892.
This commit is contained in:
Kelsi 2026-05-09 21:34:43 -07:00
parent ede2d9918a
commit 53611be09d
3 changed files with 157 additions and 0 deletions

View file

@ -208,6 +208,7 @@ const char* const kArgRequired[] = {
"--export-wqso-json", "--import-wqso-json",
"--gen-srg", "--gen-srg-ranged", "--gen-srg-friendly",
"--info-wsrg", "--validate-wsrg",
"--export-wsrg-json", "--import-wsrg-json",
"--gen-weather-temperate", "--gen-weather-arctic",
"--gen-weather-desert", "--gen-weather-stormy",
"--gen-zone-atmosphere",

View file

@ -1697,6 +1697,10 @@ void printUsage(const char* argv0) {
std::printf(" Print WSRG entries (id / kind / hostile + friendly min-max yards / icon color / name)\n");
std::printf(" --validate-wsrg <wsrg-base> [--json]\n");
std::printf(" Static checks: id+name required, rangeKind 0..6, min<=max, no negatives, no duplicate ids; warns on Self+nonzero range and Melee>8y\n");
std::printf(" --export-wsrg-json <wsrg-base> [out.json]\n");
std::printf(" Export binary .wsrg to a human-editable JSON sidecar (defaults to <base>.wsrg.json)\n");
std::printf(" --import-wsrg-json <json-path> [out-base]\n");
std::printf(" Import a .wsrg.json sidecar back into binary .wsrg (accepts rangeKind int OR rangeKindName string)\n");
std::printf(" --gen-weather-temperate <wow-base> [zoneName]\n");
std::printf(" Emit .wow weather schedule: clear-dominant + occasional rain + fog (forest / grassland)\n");
std::printf(" --gen-weather-arctic <wow-base> [zoneName]\n");

View file

@ -5,6 +5,7 @@
#include "pipeline/wowee_spell_ranges.hpp"
#include <nlohmann/json.hpp>
#include <cctype>
#include <cstdint>
#include <cstdio>
#include <cstring>
@ -122,6 +123,151 @@ int handleInfo(int& i, int argc, char** argv) {
return 0;
}
int handleExportJson(int& i, int argc, char** argv) {
std::string base = argv[++i];
std::string outPath;
if (parseOptArg(i, argc, argv)) outPath = argv[++i];
base = stripWsrgExt(base);
if (!wowee::pipeline::WoweeSpellRangeLoader::exists(base)) {
std::fprintf(stderr,
"export-wsrg-json: WSRG not found: %s.wsrg\n",
base.c_str());
return 1;
}
auto c = wowee::pipeline::WoweeSpellRangeLoader::load(base);
if (outPath.empty()) outPath = base + ".wsrg.json";
nlohmann::json j;
j["catalog"] = c.name;
nlohmann::json arr = nlohmann::json::array();
for (const auto& e : c.entries) {
nlohmann::json je;
je["rangeId"] = e.rangeId;
je["name"] = e.name;
je["description"] = e.description;
je["rangeKind"] = e.rangeKind;
je["rangeKindName"] =
wowee::pipeline::WoweeSpellRange::rangeKindName(e.rangeKind);
je["minRange"] = e.minRange;
je["maxRange"] = e.maxRange;
je["minRangeFriendly"] = e.minRangeFriendly;
je["maxRangeFriendly"] = e.maxRangeFriendly;
je["iconColorRGBA"] = e.iconColorRGBA;
arr.push_back(je);
}
j["entries"] = arr;
std::ofstream os(outPath);
if (!os) {
std::fprintf(stderr,
"export-wsrg-json: failed to open %s for write\n",
outPath.c_str());
return 1;
}
os << j.dump(2) << "\n";
std::printf("Wrote %s\n", outPath.c_str());
std::printf(" catalog : %s\n", c.name.c_str());
std::printf(" ranges : %zu\n", c.entries.size());
return 0;
}
uint8_t parseRangeKindToken(const nlohmann::json& jv,
uint8_t fallback) {
if (jv.is_number_integer() || jv.is_number_unsigned()) {
int v = jv.get<int>();
if (v < 0 || v > wowee::pipeline::WoweeSpellRange::Unlimited)
return fallback;
return static_cast<uint8_t>(v);
}
if (jv.is_string()) {
std::string s = jv.get<std::string>();
for (auto& ch : s) ch = static_cast<char>(std::tolower(ch));
if (s == "self") return wowee::pipeline::WoweeSpellRange::Self;
if (s == "melee") return wowee::pipeline::WoweeSpellRange::Melee;
if (s == "short" ||
s == "shortranged") return wowee::pipeline::WoweeSpellRange::ShortRanged;
if (s == "ranged") return wowee::pipeline::WoweeSpellRange::Ranged;
if (s == "long" ||
s == "longranged") return wowee::pipeline::WoweeSpellRange::LongRanged;
if (s == "very-long" ||
s == "verylong") return wowee::pipeline::WoweeSpellRange::VeryLong;
if (s == "unlimited") return wowee::pipeline::WoweeSpellRange::Unlimited;
}
return fallback;
}
int handleImportJson(int& i, int argc, char** argv) {
std::string jsonPath = argv[++i];
std::string outBase;
if (parseOptArg(i, argc, argv)) outBase = argv[++i];
std::ifstream is(jsonPath);
if (!is) {
std::fprintf(stderr,
"import-wsrg-json: failed to open %s\n", jsonPath.c_str());
return 1;
}
nlohmann::json j;
try {
is >> j;
} catch (const std::exception& ex) {
std::fprintf(stderr,
"import-wsrg-json: parse error in %s: %s\n",
jsonPath.c_str(), ex.what());
return 1;
}
wowee::pipeline::WoweeSpellRange c;
if (j.contains("catalog") && j["catalog"].is_string())
c.name = j["catalog"].get<std::string>();
if (j.contains("entries") && j["entries"].is_array()) {
for (const auto& je : j["entries"]) {
wowee::pipeline::WoweeSpellRange::Entry e;
if (je.contains("rangeId")) e.rangeId = je["rangeId"].get<uint32_t>();
if (je.contains("name")) e.name = je["name"].get<std::string>();
if (je.contains("description")) e.description = je["description"].get<std::string>();
// Accept both rangeKind (int) and rangeKindName
// (string) — falling back to the other when only
// one form is present, mirroring the dual int+name
// shape the export emits.
uint8_t kind = wowee::pipeline::WoweeSpellRange::Ranged;
if (je.contains("rangeKind"))
kind = parseRangeKindToken(je["rangeKind"], kind);
else if (je.contains("rangeKindName"))
kind = parseRangeKindToken(je["rangeKindName"], kind);
e.rangeKind = kind;
if (je.contains("minRange")) e.minRange = je["minRange"].get<float>();
if (je.contains("maxRange")) e.maxRange = je["maxRange"].get<float>();
if (je.contains("minRangeFriendly")) e.minRangeFriendly = je["minRangeFriendly"].get<float>();
if (je.contains("maxRangeFriendly")) e.maxRangeFriendly = je["maxRangeFriendly"].get<float>();
if (je.contains("iconColorRGBA")) e.iconColorRGBA = je["iconColorRGBA"].get<uint32_t>();
c.entries.push_back(e);
}
}
if (outBase.empty()) {
outBase = jsonPath;
// strip trailing ".json" or ".wsrg.json"
const std::string suffix1 = ".wsrg.json";
const std::string suffix2 = ".json";
if (outBase.size() >= suffix1.size() &&
outBase.compare(outBase.size() - suffix1.size(),
suffix1.size(), suffix1) == 0) {
outBase.resize(outBase.size() - suffix1.size());
} else if (outBase.size() >= suffix2.size() &&
outBase.compare(outBase.size() - suffix2.size(),
suffix2.size(), suffix2) == 0) {
outBase.resize(outBase.size() - suffix2.size());
}
}
outBase = stripWsrgExt(outBase);
if (!wowee::pipeline::WoweeSpellRangeLoader::save(c, outBase)) {
std::fprintf(stderr,
"import-wsrg-json: failed to save %s.wsrg\n",
outBase.c_str());
return 1;
}
std::printf("Wrote %s.wsrg\n", outBase.c_str());
std::printf(" catalog : %s\n", c.name.c_str());
std::printf(" ranges : %zu\n", c.entries.size());
return 0;
}
int handleValidate(int& i, int argc, char** argv) {
std::string base = argv[++i];
bool jsonOut = consumeJsonFlag(i, argc, argv);
@ -241,6 +387,12 @@ bool handleSpellRangesCatalog(int& i, int argc, char** argv,
if (std::strcmp(argv[i], "--validate-wsrg") == 0 && i + 1 < argc) {
outRc = handleValidate(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--export-wsrg-json") == 0 && i + 1 < argc) {
outRc = handleExportJson(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--import-wsrg-json") == 0 && i + 1 < argc) {
outRc = handleImportJson(i, argc, argv); return true;
}
return false;
}