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).
This commit is contained in:
Kelsi 2026-05-09 18:52:32 -07:00
parent 0be9bf86db
commit d2dbf1959d
3 changed files with 157 additions and 0 deletions

View file

@ -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<uint8_t>(
je["channelType"].get<int>());
} else if (je.contains("channelTypeName") &&
je["channelTypeName"].is_string()) {
e.channelType = channelTypeFromName(
je["channelTypeName"].get<std::string>());
}
if (je.contains("factionAccess") &&
je["factionAccess"].is_number_integer()) {
e.factionAccess = static_cast<uint8_t>(
je["factionAccess"].get<int>());
} else if (je.contains("factionAccessName") &&
je["factionAccessName"].is_string()) {
e.factionAccess = factionFromName(
je["factionAccessName"].get<std::string>());
}
e.autoJoin = static_cast<uint8_t>(je.value("autoJoin", 0));
e.announce = static_cast<uint8_t>(je.value("announce", 1));
e.moderated = static_cast<uint8_t>(je.value("moderated", 0));
e.minLevel = static_cast<uint16_t>(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;
}