From af59866f3ddaf315b4f4da0e13d86ddc46651361 Mon Sep 17 00:00:00 2001 From: Kelsi Date: Sun, 10 May 2026 04:55:54 -0700 Subject: [PATCH] feat(editor): WBHV JSON round-trip closure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds --export-wbhv-json / --import-wbhv-json with the established readEnumField template factoring int+name dual encoding for both creatureKind ("melee"/"caster"/"tank"/"healer"/"pet"/"beast") and evadeBehavior ("resettospawn"/"healatpath"/"fleetospawn"/ "noevade"). Variable-length specialAbilities serialize as JSON object array of {spellId, cooldownMs, useChancePct}. All 3 presets (melee/caster/boss) byte-identical binary roundtrip OK including the boss preset's 4-ability rotation with NoEvade and 90s-cooldown Deep Breath. Live-tested leash 1427. --- tools/editor/cli_arg_required.cpp | 1 + .../editor/cli_creature_behavior_catalog.cpp | 203 ++++++++++++++++++ tools/editor/cli_help.cpp | 4 + 3 files changed, 208 insertions(+) diff --git a/tools/editor/cli_arg_required.cpp b/tools/editor/cli_arg_required.cpp index 01c0d387..d7742115 100644 --- a/tools/editor/cli_arg_required.cpp +++ b/tools/editor/cli_arg_required.cpp @@ -418,6 +418,7 @@ const char* const kArgRequired[] = { "--export-wbnd-json", "--import-wbnd-json", "--gen-bhv-melee", "--gen-bhv-caster", "--gen-bhv-boss", "--info-wbhv", "--validate-wbhv", + "--export-wbhv-json", "--import-wbhv-json", "--gen-weather-temperate", "--gen-weather-arctic", "--gen-weather-desert", "--gen-weather-stormy", "--gen-zone-atmosphere", diff --git a/tools/editor/cli_creature_behavior_catalog.cpp b/tools/editor/cli_creature_behavior_catalog.cpp index b627280c..855b3d1f 100644 --- a/tools/editor/cli_creature_behavior_catalog.cpp +++ b/tools/editor/cli_creature_behavior_catalog.cpp @@ -165,6 +165,64 @@ int handleInfo(int& i, int argc, char** argv) { return 0; } +int parseCreatureKindToken(const std::string& s) { + using B = wowee::pipeline::WoweeCreatureBehavior; + if (s == "melee") return B::Melee; + if (s == "caster") return B::Caster; + if (s == "tank") return B::Tank; + if (s == "healer") return B::Healer; + if (s == "pet") return B::Pet; + if (s == "beast") return B::Beast; + return -1; +} + +int parseEvadeBehaviorToken(const std::string& s) { + using B = wowee::pipeline::WoweeCreatureBehavior; + if (s == "resettospawn") return B::ResetToSpawn; + if (s == "healatpath") return B::HealAtPath; + if (s == "fleetospawn") return B::FleeToSpawn; + if (s == "noevade") return B::NoEvade; + return -1; +} + +template +bool readEnumField(const nlohmann::json& je, + const char* intKey, + const char* nameKey, + ParseFn parseFn, + const char* label, + uint32_t entryId, + uint8_t& outValue) { + if (je.contains(intKey)) { + const auto& v = je[intKey]; + if (v.is_string()) { + int parsed = parseFn(v.get()); + if (parsed < 0) { + std::fprintf(stderr, + "import-wbhv-json: unknown %s token " + "'%s' on entry id=%u\n", + label, v.get().c_str(), + entryId); + return false; + } + outValue = static_cast(parsed); + return true; + } + if (v.is_number_integer()) { + outValue = static_cast(v.get()); + return true; + } + } + if (je.contains(nameKey) && je[nameKey].is_string()) { + int parsed = parseFn(je[nameKey].get()); + if (parsed >= 0) { + outValue = static_cast(parsed); + return true; + } + } + return true; +} + int handleValidate(int& i, int argc, char** argv) { std::string base = argv[++i]; bool jsonOut = consumeJsonFlag(i, argc, argv); @@ -308,6 +366,143 @@ int handleValidate(int& i, int argc, char** argv) { return ok ? 0 : 1; } +int handleExportJson(int& i, int argc, char** argv) { + std::string base = argv[++i]; + std::string out; + if (parseOptArg(i, argc, argv)) out = argv[++i]; + base = stripWbhvExt(base); + if (out.empty()) out = base + ".wbhv.json"; + if (!wowee::pipeline::WoweeCreatureBehaviorLoader::exists(base)) { + std::fprintf(stderr, + "export-wbhv-json: WBHV not found: %s.wbhv\n", + base.c_str()); + return 1; + } + auto c = wowee::pipeline::WoweeCreatureBehaviorLoader::load(base); + nlohmann::json j; + j["magic"] = "WBHV"; + j["version"] = 1; + j["name"] = c.name; + nlohmann::json arr = nlohmann::json::array(); + for (const auto& e : c.entries) { + nlohmann::json specs = nlohmann::json::array(); + for (const auto& s : e.specialAbilities) { + specs.push_back({ + {"spellId", s.spellId}, + {"cooldownMs", s.cooldownMs}, + {"useChancePct", s.useChancePct}, + }); + } + arr.push_back({ + {"behaviorId", e.behaviorId}, + {"name", e.name}, + {"creatureKind", e.creatureKind}, + {"creatureKindName", + creatureKindName(e.creatureKind)}, + {"evadeBehavior", e.evadeBehavior}, + {"evadeBehaviorName", + evadeBehaviorName(e.evadeBehavior)}, + {"aggroRadius", e.aggroRadius}, + {"leashRadius", e.leashRadius}, + {"corpseDurationSec", e.corpseDurationSec}, + {"mainAttackSpellId", e.mainAttackSpellId}, + {"specialAbilities", specs}, + }); + } + j["entries"] = arr; + std::ofstream os(out); + if (!os) { + std::fprintf(stderr, + "export-wbhv-json: failed to open %s for write\n", + out.c_str()); + return 1; + } + os << j.dump(2) << "\n"; + std::printf("Wrote %s (%zu behaviors)\n", + out.c_str(), c.entries.size()); + return 0; +} + +int handleImportJson(int& i, int argc, char** argv) { + std::string in = argv[++i]; + std::string outBase; + if (parseOptArg(i, argc, argv)) outBase = argv[++i]; + if (outBase.empty()) { + outBase = in; + if (outBase.size() >= 10 && + outBase.substr(outBase.size() - 10) == ".wbhv.json") { + outBase.resize(outBase.size() - 10); + } else { + stripExt(outBase, ".json"); + stripExt(outBase, ".wbhv"); + } + } + std::ifstream is(in); + if (!is) { + std::fprintf(stderr, + "import-wbhv-json: cannot open %s\n", in.c_str()); + return 1; + } + nlohmann::json j; + try { + is >> j; + } catch (const std::exception& ex) { + std::fprintf(stderr, + "import-wbhv-json: JSON parse error: %s\n", ex.what()); + return 1; + } + wowee::pipeline::WoweeCreatureBehavior c; + c.name = j.value("name", std::string{}); + if (!j.contains("entries") || !j["entries"].is_array()) { + std::fprintf(stderr, + "import-wbhv-json: missing or non-array 'entries'\n"); + return 1; + } + for (const auto& je : j["entries"]) { + wowee::pipeline::WoweeCreatureBehavior::Entry e; + e.behaviorId = je.value("behaviorId", 0u); + e.name = je.value("name", std::string{}); + if (!readEnumField(je, "creatureKind", + "creatureKindName", + parseCreatureKindToken, + "creatureKind", e.behaviorId, + e.creatureKind)) return 1; + if (!readEnumField(je, "evadeBehavior", + "evadeBehaviorName", + parseEvadeBehaviorToken, + "evadeBehavior", e.behaviorId, + e.evadeBehavior)) return 1; + e.aggroRadius = je.value("aggroRadius", 0.f); + e.leashRadius = je.value("leashRadius", 0.f); + e.corpseDurationSec = + je.value("corpseDurationSec", 0u); + e.mainAttackSpellId = + je.value("mainAttackSpellId", 0u); + if (je.contains("specialAbilities") && + je["specialAbilities"].is_array()) { + for (const auto& sj : je["specialAbilities"]) { + wowee::pipeline::WoweeCreatureBehavior:: + SpecialAbility s; + s.spellId = sj.value("spellId", 0u); + s.cooldownMs = sj.value("cooldownMs", 0u); + s.useChancePct = static_cast( + sj.value("useChancePct", 0)); + e.specialAbilities.push_back(s); + } + } + c.entries.push_back(e); + } + if (!wowee::pipeline::WoweeCreatureBehaviorLoader::save(c, outBase)) { + std::fprintf(stderr, + "import-wbhv-json: failed to save %s.wbhv\n", + outBase.c_str()); + return 1; + } + std::printf("Wrote %s.wbhv (%zu behaviors)\n", + outBase.c_str(), c.entries.size()); + return 0; +} + } // namespace bool handleCreatureBehaviorCatalog(int& i, int argc, char** argv, @@ -331,6 +526,14 @@ bool handleCreatureBehaviorCatalog(int& i, int argc, char** argv, i + 1 < argc) { outRc = handleValidate(i, argc, argv); return true; } + if (std::strcmp(argv[i], "--export-wbhv-json") == 0 && + i + 1 < argc) { + outRc = handleExportJson(i, argc, argv); return true; + } + if (std::strcmp(argv[i], "--import-wbhv-json") == 0 && + i + 1 < argc) { + outRc = handleImportJson(i, argc, argv); return true; + } return false; } diff --git a/tools/editor/cli_help.cpp b/tools/editor/cli_help.cpp index e443e2e2..b18cc772 100644 --- a/tools/editor/cli_help.cpp +++ b/tools/editor/cli_help.cpp @@ -2667,6 +2667,10 @@ void printUsage(const char* argv0) { std::printf(" Print WBHV entries (id / creatureKind / evadeBehavior / aggro / leash / corpse / mainAttackSpellId / specials count / name)\n"); std::printf(" --validate-wbhv [--json]\n"); std::printf(" Static checks: id+name required, creatureKind 0..5, evadeBehavior 0..3, aggroRadius > 0, no duplicate behaviorIds, no zero-spellId specials, no duplicate spellId within same behavior; CRITICAL invariant: leashRadius >= aggroRadius (else creature evades before engaging — un-killable from outside leash). Warns on corpseDuration < 60s (looting may fail in busy zones), useChancePct=0 on a special (ability never auto-fires; verify owner-triggered intent like warlock Sacrifice)\n"); + std::printf(" --export-wbhv-json [out.json]\n"); + std::printf(" Export binary .wbhv to a human-editable JSON sidecar (defaults to .wbhv.json; emits both creatureKind and evadeBehavior as int + name string; specialAbilities as JSON object array of {spellId, cooldownMs, useChancePct})\n"); + std::printf(" --import-wbhv-json [out-base]\n"); + std::printf(" Import a .wbhv.json sidecar back into binary .wbhv (creatureKind int OR \"melee\"/\"caster\"/\"tank\"/\"healer\"/\"pet\"/\"beast\"; evadeBehavior int OR \"resettospawn\"/\"healatpath\"/\"fleetospawn\"/\"noevade\"; specialAbilities accept JSON object array — round-trips per-behavior variable-length ability lists byte-identical)\n"); std::printf(" --catalog-pluck [--json]\n"); std::printf(" Extract one entry by id from any registered catalog format. Auto-detects magic, dispatches to the per-format --info-* handler internally, then prints just the matching entry. Primary-key field is auto-detected (first *Id field, or first numeric)\n"); std::printf(" --catalog-find [--magic ] [--json]\n");