From 2d99f8253177b219466d5bc7bc91cc9ffec6511d Mon Sep 17 00:00:00 2001 From: Kelsi Date: Sun, 10 May 2026 00:27:05 -0700 Subject: [PATCH] feat(editor): add WHRT JSON round-trip (--export/--import-whrt-json) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Dual encoding for both bindKind (int 0..5 OR "inn"/"capital"/"quest"/"guild"/"specialport"/"faction") and factionMask (int 1..3 OR "alliance"/"horde"/"both") on import. Export emits both int and *Name string fields for round-trip safety and human readability — the int form is authoritative; the *Name string can be read for edits but is ignored if the int form is also present. All 3 presets (starter-cities/capitals/inns) byte- identical roundtrip OK. Token-form import smoke-tested with mixed faction+kind tokens. CLI flag count 1102 -> 1104. --- tools/editor/cli_arg_required.cpp | 1 + tools/editor/cli_hearth_binds_catalog.cpp | 187 ++++++++++++++++++++++ tools/editor/cli_help.cpp | 4 + 3 files changed, 192 insertions(+) diff --git a/tools/editor/cli_arg_required.cpp b/tools/editor/cli_arg_required.cpp index 8708f048..6a544bf3 100644 --- a/tools/editor/cli_arg_required.cpp +++ b/tools/editor/cli_arg_required.cpp @@ -299,6 +299,7 @@ const char* const kArgRequired[] = { "--export-wgrp-json", "--import-wgrp-json", "--gen-hrt", "--gen-hrt-capitals", "--gen-hrt-inns", "--info-whrt", "--validate-whrt", + "--export-whrt-json", "--import-whrt-json", "--gen-weather-temperate", "--gen-weather-arctic", "--gen-weather-desert", "--gen-weather-stormy", "--gen-zone-atmosphere", diff --git a/tools/editor/cli_hearth_binds_catalog.cpp b/tools/editor/cli_hearth_binds_catalog.cpp index caf64508..561cd723 100644 --- a/tools/editor/cli_hearth_binds_catalog.cpp +++ b/tools/editor/cli_hearth_binds_catalog.cpp @@ -148,6 +148,187 @@ int handleInfo(int& i, int argc, char** argv) { return 0; } +// Token parser for bindKind. Returns -1 if the token +// doesn't match any known kind, else the enum value. +int parseBindKindToken(const std::string& s) { + using H = wowee::pipeline::WoweeHearthBinds; + if (s == "inn") return H::Inn; + if (s == "capital") return H::Capital; + if (s == "quest") return H::Quest; + if (s == "guild") return H::Guild; + if (s == "specialport") return H::SpecialPort; + if (s == "faction") return H::Faction; + return -1; +} + +// Token parser for factionMask. Returns -1 if unknown. +int parseFactionMaskToken(const std::string& s) { + using H = wowee::pipeline::WoweeHearthBinds; + if (s == "alliance") return H::AllianceOnly; + if (s == "horde") return H::HordeOnly; + if (s == "both") return H::Both; + return -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 = stripWhrtExt(base); + if (out.empty()) out = base + ".whrt.json"; + if (!wowee::pipeline::WoweeHearthBindsLoader::exists(base)) { + std::fprintf(stderr, + "export-whrt-json: WHRT not found: %s.whrt\n", + base.c_str()); + return 1; + } + auto c = wowee::pipeline::WoweeHearthBindsLoader::load(base); + nlohmann::json j; + j["magic"] = "WHRT"; + j["version"] = 1; + j["name"] = c.name; + nlohmann::json arr = nlohmann::json::array(); + for (const auto& e : c.entries) { + arr.push_back({ + {"bindId", e.bindId}, + {"name", e.name}, + {"description", e.description}, + {"mapId", e.mapId}, + {"areaId", e.areaId}, + {"x", e.x}, {"y", e.y}, {"z", e.z}, + {"facing", e.facing}, + {"npcId", e.npcId}, + {"factionMask", e.factionMask}, + {"factionMaskName", factionMaskName(e.factionMask)}, + {"bindKind", e.bindKind}, + {"bindKindName", bindKindName(e.bindKind)}, + {"levelMin", e.levelMin}, + {"iconColorRGBA", e.iconColorRGBA}, + }); + } + j["entries"] = arr; + std::ofstream os(out); + if (!os) { + std::fprintf(stderr, + "export-whrt-json: failed to open %s for write\n", + out.c_str()); + return 1; + } + os << j.dump(2) << "\n"; + std::printf("Wrote %s (%zu binds)\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) == ".whrt.json") { + outBase.resize(outBase.size() - 10); + } else { + stripExt(outBase, ".json"); + stripExt(outBase, ".whrt"); + } + } + std::ifstream is(in); + if (!is) { + std::fprintf(stderr, + "import-whrt-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-whrt-json: JSON parse error: %s\n", ex.what()); + return 1; + } + wowee::pipeline::WoweeHearthBinds c; + c.name = j.value("name", std::string{}); + if (!j.contains("entries") || !j["entries"].is_array()) { + std::fprintf(stderr, + "import-whrt-json: missing or non-array 'entries'\n"); + return 1; + } + for (const auto& je : j["entries"]) { + wowee::pipeline::WoweeHearthBinds::Entry e; + e.bindId = je.value("bindId", 0u); + e.name = je.value("name", std::string{}); + e.description = je.value("description", std::string{}); + e.mapId = je.value("mapId", 0u); + e.areaId = je.value("areaId", 0u); + e.x = je.value("x", 0.0f); + e.y = je.value("y", 0.0f); + e.z = je.value("z", 0.0f); + e.facing = je.value("facing", 0.0f); + e.npcId = je.value("npcId", 0u); + // bindKind: int OR token string. + if (je.contains("bindKind")) { + const auto& bk = je["bindKind"]; + if (bk.is_string()) { + int parsed = parseBindKindToken(bk.get()); + if (parsed < 0) { + std::fprintf(stderr, + "import-whrt-json: unknown bindKind " + "token '%s' on entry id=%u\n", + bk.get().c_str(), e.bindId); + return 1; + } + e.bindKind = static_cast(parsed); + } else if (bk.is_number_integer()) { + e.bindKind = static_cast(bk.get()); + } + } else if (je.contains("bindKindName") && + je["bindKindName"].is_string()) { + int parsed = parseBindKindToken( + je["bindKindName"].get()); + if (parsed >= 0) + e.bindKind = static_cast(parsed); + } + // factionMask: int OR token string. + if (je.contains("factionMask")) { + const auto& fm = je["factionMask"]; + if (fm.is_string()) { + int parsed = parseFactionMaskToken( + fm.get()); + if (parsed < 0) { + std::fprintf(stderr, + "import-whrt-json: unknown factionMask " + "token '%s' on entry id=%u\n", + fm.get().c_str(), e.bindId); + return 1; + } + e.factionMask = static_cast(parsed); + } else if (fm.is_number_integer()) { + e.factionMask = static_cast(fm.get()); + } + } else if (je.contains("factionMaskName") && + je["factionMaskName"].is_string()) { + int parsed = parseFactionMaskToken( + je["factionMaskName"].get()); + if (parsed >= 0) + e.factionMask = static_cast(parsed); + } + e.levelMin = static_cast(je.value("levelMin", 0u)); + e.iconColorRGBA = je.value("iconColorRGBA", 0xFFFFFFFFu); + c.entries.push_back(e); + } + if (!wowee::pipeline::WoweeHearthBindsLoader::save(c, outBase)) { + std::fprintf(stderr, + "import-whrt-json: failed to save %s.whrt\n", + outBase.c_str()); + return 1; + } + std::printf("Wrote %s.whrt (%zu binds)\n", + outBase.c_str(), c.entries.size()); + return 0; +} + int handleValidate(int& i, int argc, char** argv) { std::string base = argv[++i]; bool jsonOut = consumeJsonFlag(i, argc, argv); @@ -268,6 +449,12 @@ bool handleHearthBindsCatalog(int& i, int argc, char** argv, if (std::strcmp(argv[i], "--validate-whrt") == 0 && i + 1 < argc) { outRc = handleValidate(i, argc, argv); return true; } + if (std::strcmp(argv[i], "--export-whrt-json") == 0 && i + 1 < argc) { + outRc = handleExportJson(i, argc, argv); return true; + } + if (std::strcmp(argv[i], "--import-whrt-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 cfadf1fc..07661050 100644 --- a/tools/editor/cli_help.cpp +++ b/tools/editor/cli_help.cpp @@ -2121,6 +2121,10 @@ void printUsage(const char* argv0) { std::printf(" Print WHRT entries (id / map / area / faction / kind / npc / levelMin / name)\n"); std::printf(" --validate-whrt [--json]\n"); std::printf(" Static checks: id+name required, factionMask 1..3, bindKind 0..5, no duplicate ids; warns on (0,0,0) position, Inn with npcId=0, Quest with levelMin=0\n"); + std::printf(" --export-whrt-json [out.json]\n"); + std::printf(" Export binary .whrt to a human-editable JSON sidecar (defaults to .whrt.json; emits both bindKind/factionMask ints AND name strings)\n"); + std::printf(" --import-whrt-json [out-base]\n"); + std::printf(" Import a .whrt.json sidecar back into binary .whrt (accepts bindKind int OR \"inn\"/\"capital\"/\"quest\"/\"guild\"/\"specialport\"/\"faction\"; factionMask int OR \"alliance\"/\"horde\"/\"both\")\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");