From d2dbf1959d2735f73874ad0c2322f65710465507 Mon Sep 17 00:00:00 2001 From: Kelsi Date: Sat, 9 May 2026 18:52:32 -0700 Subject: [PATCH] feat(editor): add WCHN JSON round-trip (export/import sidecar) Closes the JSON round-trip gap on the channels catalog format shipped last batch. --export-wchn-json emits all 10 scalar fields plus dual int + name forms for channelType and factionAccess so hand-edits can use either representation. --import-wchn-json accepts either form, falling back to the int when both are present. Verified byte-identical round-trip on all three preset emitters (starter / city / moderated). --- tools/editor/cli_arg_required.cpp | 1 + tools/editor/cli_channels_catalog.cpp | 152 ++++++++++++++++++++++++++ tools/editor/cli_help.cpp | 4 + 3 files changed, 157 insertions(+) diff --git a/tools/editor/cli_arg_required.cpp b/tools/editor/cli_arg_required.cpp index add2863d..1842a336 100644 --- a/tools/editor/cli_arg_required.cpp +++ b/tools/editor/cli_arg_required.cpp @@ -118,6 +118,7 @@ const char* const kArgRequired[] = { "--export-wauc-json", "--import-wauc-json", "--gen-channels", "--gen-channels-city", "--gen-channels-moderated", "--info-wchn", "--validate-wchn", + "--export-wchn-json", "--import-wchn-json", "--gen-cinematics", "--gen-cinematics-intros", "--gen-cinematics-quests", "--info-wcms", "--validate-wcms", "--gen-weather-temperate", "--gen-weather-arctic", diff --git a/tools/editor/cli_channels_catalog.cpp b/tools/editor/cli_channels_catalog.cpp index eb26717c..281bad12 100644 --- a/tools/editor/cli_channels_catalog.cpp +++ b/tools/editor/cli_channels_catalog.cpp @@ -126,6 +126,152 @@ int handleInfo(int& i, int argc, char** argv) { return 0; } +int handleExportJson(int& i, int argc, char** argv) { + // Mirrors the JSON pairs added for every other novel + // open format. Each channel emits all 10 scalar fields + // plus dual int + name forms for channelType and + // factionAccess (so hand-edits can use either form). + std::string base = argv[++i]; + std::string outPath; + if (parseOptArg(i, argc, argv)) outPath = argv[++i]; + base = stripWchnExt(base); + if (outPath.empty()) outPath = base + ".wchn.json"; + if (!wowee::pipeline::WoweeChannelLoader::exists(base)) { + std::fprintf(stderr, + "export-wchn-json: WCHN not found: %s.wchn\n", base.c_str()); + return 1; + } + auto c = wowee::pipeline::WoweeChannelLoader::load(base); + nlohmann::json j; + j["name"] = c.name; + nlohmann::json arr = nlohmann::json::array(); + for (const auto& e : c.entries) { + arr.push_back({ + {"channelId", e.channelId}, + {"name", e.name}, + {"description", e.description}, + {"channelType", e.channelType}, + {"channelTypeName", wowee::pipeline::WoweeChannel::channelTypeName(e.channelType)}, + {"factionAccess", e.factionAccess}, + {"factionAccessName", wowee::pipeline::WoweeChannel::factionAccessName(e.factionAccess)}, + {"autoJoin", e.autoJoin}, + {"announce", e.announce}, + {"moderated", e.moderated}, + {"minLevel", e.minLevel}, + {"areaIdGate", e.areaIdGate}, + {"mapIdGate", e.mapIdGate}, + }); + } + j["entries"] = arr; + std::ofstream out(outPath); + if (!out) { + std::fprintf(stderr, + "export-wchn-json: cannot write %s\n", outPath.c_str()); + return 1; + } + out << j.dump(2) << "\n"; + out.close(); + std::printf("Wrote %s\n", outPath.c_str()); + std::printf(" source : %s.wchn\n", base.c_str()); + std::printf(" channels : %zu\n", c.entries.size()); + return 0; +} + +int handleImportJson(int& i, int argc, char** argv) { + std::string jsonPath = argv[++i]; + std::string outBase; + if (parseOptArg(i, argc, argv)) outBase = argv[++i]; + if (outBase.empty()) { + outBase = jsonPath; + std::string suffix = ".wchn.json"; + if (outBase.size() > suffix.size() && + outBase.substr(outBase.size() - suffix.size()) == suffix) { + outBase = outBase.substr(0, outBase.size() - suffix.size()); + } else if (outBase.size() > 5 && + outBase.substr(outBase.size() - 5) == ".json") { + outBase = outBase.substr(0, outBase.size() - 5); + } + } + outBase = stripWchnExt(outBase); + std::ifstream in(jsonPath); + if (!in) { + std::fprintf(stderr, + "import-wchn-json: cannot read %s\n", jsonPath.c_str()); + return 1; + } + nlohmann::json j; + try { in >> j; } + catch (const std::exception& e) { + std::fprintf(stderr, + "import-wchn-json: bad JSON in %s: %s\n", + jsonPath.c_str(), e.what()); + return 1; + } + auto channelTypeFromName = [](const std::string& s) -> uint8_t { + if (s == "area-local") return wowee::pipeline::WoweeChannel::AreaLocal; + if (s == "zone") return wowee::pipeline::WoweeChannel::Zone; + if (s == "continent") return wowee::pipeline::WoweeChannel::Continent; + if (s == "world") return wowee::pipeline::WoweeChannel::World; + if (s == "trade") return wowee::pipeline::WoweeChannel::Trade; + if (s == "lfg") return wowee::pipeline::WoweeChannel::LookingForGroup; + if (s == "guild-recruit") return wowee::pipeline::WoweeChannel::GuildRecruit; + if (s == "local-defense") return wowee::pipeline::WoweeChannel::LocalDefense; + if (s == "custom") return wowee::pipeline::WoweeChannel::Custom; + if (s == "pvp") return wowee::pipeline::WoweeChannel::Pvp; + return wowee::pipeline::WoweeChannel::AreaLocal; + }; + auto factionFromName = [](const std::string& s) -> uint8_t { + if (s == "alliance") return wowee::pipeline::WoweeChannel::Alliance; + if (s == "horde") return wowee::pipeline::WoweeChannel::Horde; + if (s == "both") return wowee::pipeline::WoweeChannel::Both; + return wowee::pipeline::WoweeChannel::Both; + }; + wowee::pipeline::WoweeChannel c; + c.name = j.value("name", std::string{}); + if (j.contains("entries") && j["entries"].is_array()) { + for (const auto& je : j["entries"]) { + wowee::pipeline::WoweeChannel::Entry e; + e.channelId = je.value("channelId", 0u); + e.name = je.value("name", std::string{}); + e.description = je.value("description", std::string{}); + if (je.contains("channelType") && + je["channelType"].is_number_integer()) { + e.channelType = static_cast( + je["channelType"].get()); + } else if (je.contains("channelTypeName") && + je["channelTypeName"].is_string()) { + e.channelType = channelTypeFromName( + je["channelTypeName"].get()); + } + if (je.contains("factionAccess") && + je["factionAccess"].is_number_integer()) { + e.factionAccess = static_cast( + je["factionAccess"].get()); + } else if (je.contains("factionAccessName") && + je["factionAccessName"].is_string()) { + e.factionAccess = factionFromName( + je["factionAccessName"].get()); + } + e.autoJoin = static_cast(je.value("autoJoin", 0)); + e.announce = static_cast(je.value("announce", 1)); + e.moderated = static_cast(je.value("moderated", 0)); + e.minLevel = static_cast(je.value("minLevel", 1)); + e.areaIdGate = je.value("areaIdGate", 0u); + e.mapIdGate = je.value("mapIdGate", 0u); + c.entries.push_back(e); + } + } + if (!wowee::pipeline::WoweeChannelLoader::save(c, outBase)) { + std::fprintf(stderr, + "import-wchn-json: failed to save %s.wchn\n", outBase.c_str()); + return 1; + } + std::printf("Wrote %s.wchn\n", outBase.c_str()); + std::printf(" source : %s\n", jsonPath.c_str()); + std::printf(" channels : %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); @@ -226,6 +372,12 @@ bool handleChannelsCatalog(int& i, int argc, char** argv, int& outRc) { if (std::strcmp(argv[i], "--validate-wchn") == 0 && i + 1 < argc) { outRc = handleValidate(i, argc, argv); return true; } + if (std::strcmp(argv[i], "--export-wchn-json") == 0 && i + 1 < argc) { + outRc = handleExportJson(i, argc, argv); return true; + } + if (std::strcmp(argv[i], "--import-wchn-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 aa32f14d..0bf9828a 100644 --- a/tools/editor/cli_help.cpp +++ b/tools/editor/cli_help.cpp @@ -1273,6 +1273,10 @@ void printUsage(const char* argv0) { std::printf(" Print WCHN entries (id / type / faction / autoJoin/announce/moderated / level / map+area gates / name)\n"); std::printf(" --validate-wchn [--json]\n"); std::printf(" Static checks: id>0+unique, name not empty, type 0..9, faction 0..2, world+area-gate combo warning\n"); + std::printf(" --export-wchn-json [out.json]\n"); + std::printf(" Export binary .wchn to a human-editable JSON sidecar (defaults to .wchn.json)\n"); + std::printf(" --import-wchn-json [out-base]\n"); + std::printf(" Import a .wchn.json sidecar back into binary .wchn (accepts channelType/factionAccess int OR name string)\n"); std::printf(" --gen-cinematics [name]\n"); std::printf(" Emit .wcms starter: 3 cinematics (pre-rendered intro / quest cutscene / login splash)\n"); std::printf(" --gen-cinematics-intros [name]\n");