From 8c0cab27beaea5b6b9c91a611daae02ae7b47ba3 Mon Sep 17 00:00:00 2001 From: Kelsi Date: Sun, 10 May 2026 00:33:38 -0700 Subject: [PATCH] feat(editor): add WSCB JSON round-trip (--export/--import-wscb-json) Dual encoding for both channelKind (int 0..4 OR "login"/"system"/"raidwarning"/"motd"/"helptip") and factionFilter (int 1..3 OR "alliance"/"horde"/"both") on import. Export emits both int and *Name string fields for round-trip safety; the int form is authoritative, the *Name string is for human edit ergonomics and is ignored if the int form is also present. All 3 presets (motd/maintenance/helptips) byte-identical roundtrip OK. Token-form import smoke-tested with mixed channel+faction tokens. CLI flag count 1109 -> 1111. --- tools/editor/cli_arg_required.cpp | 1 + tools/editor/cli_help.cpp | 4 + .../editor/cli_server_broadcasts_catalog.cpp | 181 ++++++++++++++++++ 3 files changed, 186 insertions(+) diff --git a/tools/editor/cli_arg_required.cpp b/tools/editor/cli_arg_required.cpp index 50cba01c..3b9aba92 100644 --- a/tools/editor/cli_arg_required.cpp +++ b/tools/editor/cli_arg_required.cpp @@ -302,6 +302,7 @@ const char* const kArgRequired[] = { "--export-whrt-json", "--import-whrt-json", "--gen-scb", "--gen-scb-maintenance", "--gen-scb-helptips", "--info-wscb", "--validate-wscb", + "--export-wscb-json", "--import-wscb-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 1de095d4..a8d8b406 100644 --- a/tools/editor/cli_help.cpp +++ b/tools/editor/cli_help.cpp @@ -2135,6 +2135,10 @@ void printUsage(const char* argv0) { std::printf(" Print WSCB entries (id / channel / faction / interval / level range / name)\n"); std::printf(" --validate-wscb [--json]\n"); std::printf(" Static checks: id+name+messageText required, factionFilter 1..3, channelKind 0..4, min<=max level, no duplicate ids, intervalSeconds>=10 (errors below); warns on interval>0 with login/MOTD (timer ignored), interval<60 (spammy), text>255 chars (truncation)\n"); + std::printf(" --export-wscb-json [out.json]\n"); + std::printf(" Export binary .wscb to a human-editable JSON sidecar (defaults to .wscb.json; emits both channelKind/factionFilter ints AND name strings)\n"); + std::printf(" --import-wscb-json [out-base]\n"); + std::printf(" Import a .wscb.json sidecar back into binary .wscb (channelKind int OR \"login\"/\"system\"/\"raidwarning\"/\"motd\"/\"helptip\"; factionFilter 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"); diff --git a/tools/editor/cli_server_broadcasts_catalog.cpp b/tools/editor/cli_server_broadcasts_catalog.cpp index 4249b7b3..7b79f75b 100644 --- a/tools/editor/cli_server_broadcasts_catalog.cpp +++ b/tools/editor/cli_server_broadcasts_catalog.cpp @@ -149,6 +149,181 @@ int handleInfo(int& i, int argc, char** argv) { return 0; } +// Token parser for channelKind. Returns -1 if unknown. +int parseChannelKindToken(const std::string& s) { + using S = wowee::pipeline::WoweeServerBroadcasts; + if (s == "login") return S::Login; + if (s == "system") return S::SystemChannel; + if (s == "raidwarning") return S::RaidWarning; + if (s == "motd") return S::MOTD; + if (s == "helptip") return S::HelpTip; + return -1; +} + +// Token parser for factionFilter. Returns -1 if unknown. +int parseFactionFilterToken(const std::string& s) { + using S = wowee::pipeline::WoweeServerBroadcasts; + if (s == "alliance") return S::AllianceOnly; + if (s == "horde") return S::HordeOnly; + if (s == "both") return S::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 = stripWscbExt(base); + if (out.empty()) out = base + ".wscb.json"; + if (!wowee::pipeline::WoweeServerBroadcastsLoader::exists(base)) { + std::fprintf(stderr, + "export-wscb-json: WSCB not found: %s.wscb\n", + base.c_str()); + return 1; + } + auto c = wowee::pipeline::WoweeServerBroadcastsLoader::load(base); + nlohmann::json j; + j["magic"] = "WSCB"; + j["version"] = 1; + j["name"] = c.name; + nlohmann::json arr = nlohmann::json::array(); + for (const auto& e : c.entries) { + arr.push_back({ + {"broadcastId", e.broadcastId}, + {"name", e.name}, + {"description", e.description}, + {"messageText", e.messageText}, + {"intervalSeconds", e.intervalSeconds}, + {"channelKind", e.channelKind}, + {"channelKindName", channelKindName(e.channelKind)}, + {"factionFilter", e.factionFilter}, + {"factionFilterName", + factionFilterName(e.factionFilter)}, + {"minLevel", e.minLevel}, + {"maxLevel", e.maxLevel}, + {"iconColorRGBA", e.iconColorRGBA}, + }); + } + j["entries"] = arr; + std::ofstream os(out); + if (!os) { + std::fprintf(stderr, + "export-wscb-json: failed to open %s for write\n", + out.c_str()); + return 1; + } + os << j.dump(2) << "\n"; + std::printf("Wrote %s (%zu broadcasts)\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) == ".wscb.json") { + outBase.resize(outBase.size() - 10); + } else { + stripExt(outBase, ".json"); + stripExt(outBase, ".wscb"); + } + } + std::ifstream is(in); + if (!is) { + std::fprintf(stderr, + "import-wscb-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-wscb-json: JSON parse error: %s\n", ex.what()); + return 1; + } + wowee::pipeline::WoweeServerBroadcasts c; + c.name = j.value("name", std::string{}); + if (!j.contains("entries") || !j["entries"].is_array()) { + std::fprintf(stderr, + "import-wscb-json: missing or non-array 'entries'\n"); + return 1; + } + for (const auto& je : j["entries"]) { + wowee::pipeline::WoweeServerBroadcasts::Entry e; + e.broadcastId = je.value("broadcastId", 0u); + e.name = je.value("name", std::string{}); + e.description = je.value("description", std::string{}); + e.messageText = je.value("messageText", std::string{}); + e.intervalSeconds = je.value("intervalSeconds", 0u); + // channelKind: int OR token string. + if (je.contains("channelKind")) { + const auto& ck = je["channelKind"]; + if (ck.is_string()) { + int parsed = parseChannelKindToken(ck.get()); + if (parsed < 0) { + std::fprintf(stderr, + "import-wscb-json: unknown channelKind " + "token '%s' on entry id=%u\n", + ck.get().c_str(), e.broadcastId); + return 1; + } + e.channelKind = static_cast(parsed); + } else if (ck.is_number_integer()) { + e.channelKind = static_cast(ck.get()); + } + } else if (je.contains("channelKindName") && + je["channelKindName"].is_string()) { + int parsed = parseChannelKindToken( + je["channelKindName"].get()); + if (parsed >= 0) + e.channelKind = static_cast(parsed); + } + // factionFilter: int OR token string. + if (je.contains("factionFilter")) { + const auto& ff = je["factionFilter"]; + if (ff.is_string()) { + int parsed = parseFactionFilterToken( + ff.get()); + if (parsed < 0) { + std::fprintf(stderr, + "import-wscb-json: unknown factionFilter " + "token '%s' on entry id=%u\n", + ff.get().c_str(), + e.broadcastId); + return 1; + } + e.factionFilter = static_cast(parsed); + } else if (ff.is_number_integer()) { + e.factionFilter = static_cast(ff.get()); + } + } else if (je.contains("factionFilterName") && + je["factionFilterName"].is_string()) { + int parsed = parseFactionFilterToken( + je["factionFilterName"].get()); + if (parsed >= 0) + e.factionFilter = static_cast(parsed); + } + e.minLevel = static_cast(je.value("minLevel", 0u)); + e.maxLevel = static_cast(je.value("maxLevel", 0u)); + e.iconColorRGBA = je.value("iconColorRGBA", 0xFFFFFFFFu); + c.entries.push_back(e); + } + if (!wowee::pipeline::WoweeServerBroadcastsLoader::save(c, outBase)) { + std::fprintf(stderr, + "import-wscb-json: failed to save %s.wscb\n", + outBase.c_str()); + return 1; + } + std::printf("Wrote %s.wscb (%zu broadcasts)\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); @@ -290,6 +465,12 @@ bool handleServerBroadcastsCatalog(int& i, int argc, char** argv, if (std::strcmp(argv[i], "--validate-wscb") == 0 && i + 1 < argc) { outRc = handleValidate(i, argc, argv); return true; } + if (std::strcmp(argv[i], "--export-wscb-json") == 0 && i + 1 < argc) { + outRc = handleExportJson(i, argc, argv); return true; + } + if (std::strcmp(argv[i], "--import-wscb-json") == 0 && i + 1 < argc) { + outRc = handleImportJson(i, argc, argv); return true; + } return false; }