From b8dc28d7045504b321502f9cb3348bea4a0d816f Mon Sep 17 00:00:00 2001 From: Kelsi Date: Sat, 9 May 2026 21:43:16 -0700 Subject: [PATCH] feat(editor): add WSDR JSON round-trip (--export/--import-wsdr-json) Closes the editing loop on the spell-duration bucket catalog: dump a .wsdr to JSON, hand-edit baseDurationMs / perLevelMs / clamp / durationKind (e.g. retune Renew from 9s to 10s base across every spell that references the HoT5Tick bucket), re-import to a byte-identical binary. The exporter emits both durationKind (int 0..4) and the human-readable durationKindName ("instant", "timed", "tick", "until-cancelled", "until-death"); 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 / buffs / dot). CLI flag count 904 -> 906. --- tools/editor/cli_arg_required.cpp | 1 + tools/editor/cli_help.cpp | 4 + tools/editor/cli_spell_durations_catalog.cpp | 143 +++++++++++++++++++ 3 files changed, 148 insertions(+) diff --git a/tools/editor/cli_arg_required.cpp b/tools/editor/cli_arg_required.cpp index 23b77a69..45c0f964 100644 --- a/tools/editor/cli_arg_required.cpp +++ b/tools/editor/cli_arg_required.cpp @@ -214,6 +214,7 @@ const char* const kArgRequired[] = { "--export-wsct-json", "--import-wsct-json", "--gen-sdr", "--gen-sdr-buffs", "--gen-sdr-dot", "--info-wsdr", "--validate-wsdr", + "--export-wsdr-json", "--import-wsdr-json", "--gen-weather-temperate", "--gen-weather-arctic", "--gen-weather-desert", "--gen-weather-stormy", "--gen-zone-atmosphere", diff --git a/tools/editor/cli_help.cpp b/tools/editor/cli_help.cpp index 5a5c3f03..7f86351a 100644 --- a/tools/editor/cli_help.cpp +++ b/tools/editor/cli_help.cpp @@ -1725,6 +1725,10 @@ void printUsage(const char* argv0) { std::printf(" Print WSDR entries (id / kind / baseMs / perLevelMs / maxMs / iconColor / name)\n"); std::printf(" --validate-wsdr [--json]\n"); std::printf(" Static checks: id+name required, durationKind 0..4, base>0 for Timed/TickBased, base<0 for permanent kinds, max>=base, no duplicate ids\n"); + std::printf(" --export-wsdr-json [out.json]\n"); + std::printf(" Export binary .wsdr to a human-editable JSON sidecar (defaults to .wsdr.json)\n"); + std::printf(" --import-wsdr-json [out-base]\n"); + std::printf(" Import a .wsdr.json sidecar back into binary .wsdr (accepts durationKind int OR durationKindName string)\n"); std::printf(" --gen-weather-temperate [zoneName]\n"); std::printf(" Emit .wow weather schedule: clear-dominant + occasional rain + fog (forest / grassland)\n"); std::printf(" --gen-weather-arctic [zoneName]\n"); diff --git a/tools/editor/cli_spell_durations_catalog.cpp b/tools/editor/cli_spell_durations_catalog.cpp index 07935c45..bf898bad 100644 --- a/tools/editor/cli_spell_durations_catalog.cpp +++ b/tools/editor/cli_spell_durations_catalog.cpp @@ -5,6 +5,7 @@ #include "pipeline/wowee_spell_durations.hpp" #include +#include #include #include #include @@ -121,6 +122,142 @@ 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 = stripWsdrExt(base); + if (!wowee::pipeline::WoweeSpellDurationLoader::exists(base)) { + std::fprintf(stderr, + "export-wsdr-json: WSDR not found: %s.wsdr\n", + base.c_str()); + return 1; + } + auto c = wowee::pipeline::WoweeSpellDurationLoader::load(base); + if (outPath.empty()) outPath = base + ".wsdr.json"; + nlohmann::json j; + j["catalog"] = c.name; + nlohmann::json arr = nlohmann::json::array(); + for (const auto& e : c.entries) { + nlohmann::json je; + je["durationId"] = e.durationId; + je["name"] = e.name; + je["description"] = e.description; + je["durationKind"] = e.durationKind; + je["durationKindName"] = + wowee::pipeline::WoweeSpellDuration::durationKindName(e.durationKind); + je["baseDurationMs"] = e.baseDurationMs; + je["perLevelMs"] = e.perLevelMs; + je["maxDurationMs"] = e.maxDurationMs; + je["iconColorRGBA"] = e.iconColorRGBA; + arr.push_back(je); + } + j["entries"] = arr; + std::ofstream os(outPath); + if (!os) { + std::fprintf(stderr, + "export-wsdr-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(" buckets : %zu\n", c.entries.size()); + return 0; +} + +uint8_t parseDurationKindToken(const nlohmann::json& jv, + uint8_t fallback) { + if (jv.is_number_integer() || jv.is_number_unsigned()) { + int v = jv.get(); + if (v < 0 || v > wowee::pipeline::WoweeSpellDuration::UntilDeath) + return fallback; + return static_cast(v); + } + if (jv.is_string()) { + std::string s = jv.get(); + for (auto& ch : s) ch = static_cast(std::tolower(ch)); + if (s == "instant") return wowee::pipeline::WoweeSpellDuration::Instant; + if (s == "timed") return wowee::pipeline::WoweeSpellDuration::Timed; + if (s == "tick" || + s == "tickbased") return wowee::pipeline::WoweeSpellDuration::TickBased; + if (s == "until-cancelled" || + s == "untilcancelled") return wowee::pipeline::WoweeSpellDuration::UntilCancelled; + if (s == "until-death" || + s == "untildeath") return wowee::pipeline::WoweeSpellDuration::UntilDeath; + } + 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-wsdr-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-wsdr-json: parse error in %s: %s\n", + jsonPath.c_str(), ex.what()); + return 1; + } + wowee::pipeline::WoweeSpellDuration c; + if (j.contains("catalog") && j["catalog"].is_string()) + c.name = j["catalog"].get(); + if (j.contains("entries") && j["entries"].is_array()) { + for (const auto& je : j["entries"]) { + wowee::pipeline::WoweeSpellDuration::Entry e; + if (je.contains("durationId")) e.durationId = je["durationId"].get(); + if (je.contains("name")) e.name = je["name"].get(); + if (je.contains("description")) e.description = je["description"].get(); + uint8_t kind = wowee::pipeline::WoweeSpellDuration::Timed; + if (je.contains("durationKind")) + kind = parseDurationKindToken(je["durationKind"], kind); + else if (je.contains("durationKindName")) + kind = parseDurationKindToken(je["durationKindName"], kind); + e.durationKind = kind; + if (je.contains("baseDurationMs")) e.baseDurationMs = je["baseDurationMs"].get(); + if (je.contains("perLevelMs")) e.perLevelMs = je["perLevelMs"].get(); + if (je.contains("maxDurationMs")) e.maxDurationMs = je["maxDurationMs"].get(); + if (je.contains("iconColorRGBA")) e.iconColorRGBA = je["iconColorRGBA"].get(); + c.entries.push_back(e); + } + } + if (outBase.empty()) { + outBase = jsonPath; + const std::string suffix1 = ".wsdr.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 = stripWsdrExt(outBase); + if (!wowee::pipeline::WoweeSpellDurationLoader::save(c, outBase)) { + std::fprintf(stderr, + "import-wsdr-json: failed to save %s.wsdr\n", + outBase.c_str()); + return 1; + } + std::printf("Wrote %s.wsdr\n", outBase.c_str()); + std::printf(" catalog : %s\n", c.name.c_str()); + std::printf(" buckets : %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); @@ -247,6 +384,12 @@ bool handleSpellDurationsCatalog(int& i, int argc, char** argv, if (std::strcmp(argv[i], "--validate-wsdr") == 0 && i + 1 < argc) { outRc = handleValidate(i, argc, argv); return true; } + if (std::strcmp(argv[i], "--export-wsdr-json") == 0 && i + 1 < argc) { + outRc = handleExportJson(i, argc, argv); return true; + } + if (std::strcmp(argv[i], "--import-wsdr-json") == 0 && i + 1 < argc) { + outRc = handleImportJson(i, argc, argv); return true; + } return false; }