feat(editor): WGCH JSON round-trip closure

Adds --export-wgch-json / --import-wgch-json with the established
readEnumField template factoring int+name dual encoding for both
channelKind ("global"/"realmzone"/"faction"/"custom") and accessKind
("publicjoin"/"inviteonly"/"autojoinonzone"/"moderated").

All 3 presets (std-channels / roleplay / admin) byte-identical
binary roundtrip OK including the zoneDefaultMapId=1519 Stormwind
auto-join binding on TradeStormwind. Validators pass on round-
tripped binaries.

CLI flag count 1317 -> 1319.
This commit is contained in:
Kelsi 2026-05-10 03:26:29 -07:00
parent c7d85fc598
commit 09ad03ca34
3 changed files with 181 additions and 0 deletions

View file

@ -46,6 +46,62 @@ const char* accessKindName(uint8_t k) {
}
}
int parseChannelKindToken(const std::string& s) {
using G = wowee::pipeline::WoweeGlobalChannels;
if (s == "global") return G::Global;
if (s == "realmzone") return G::RealmZone;
if (s == "faction") return G::Faction;
if (s == "custom") return G::Custom;
return -1;
}
int parseAccessKindToken(const std::string& s) {
using G = wowee::pipeline::WoweeGlobalChannels;
if (s == "publicjoin") return G::PublicJoin;
if (s == "inviteonly") return G::InviteOnly;
if (s == "autojoinonzone") return G::AutoJoinOnZone;
if (s == "moderated") return G::Moderated;
return -1;
}
template <typename ParseFn>
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<std::string>());
if (parsed < 0) {
std::fprintf(stderr,
"import-wgch-json: unknown %s token "
"'%s' on entry id=%u\n",
label, v.get<std::string>().c_str(),
entryId);
return false;
}
outValue = static_cast<uint8_t>(parsed);
return true;
}
if (v.is_number_integer()) {
outValue = static_cast<uint8_t>(v.get<int>());
return true;
}
}
if (je.contains(nameKey) && je[nameKey].is_string()) {
int parsed = parseFn(je[nameKey].get<std::string>());
if (parsed >= 0) {
outValue = static_cast<uint8_t>(parsed);
return true;
}
}
return true;
}
bool saveOrError(const wowee::pipeline::WoweeGlobalChannels& c,
const std::string& base, const char* cmd) {
if (!wowee::pipeline::WoweeGlobalChannelsLoader::save(c, base)) {
@ -255,6 +311,120 @@ 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 = stripWgchExt(base);
if (out.empty()) out = base + ".wgch.json";
if (!wowee::pipeline::WoweeGlobalChannelsLoader::exists(base)) {
std::fprintf(stderr,
"export-wgch-json: WGCH not found: %s.wgch\n",
base.c_str());
return 1;
}
auto c = wowee::pipeline::WoweeGlobalChannelsLoader::load(base);
nlohmann::json j;
j["magic"] = "WGCH";
j["version"] = 1;
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},
{"channelKind", e.channelKind},
{"channelKindName", channelKindName(e.channelKind)},
{"accessKind", e.accessKind},
{"accessKindName", accessKindName(e.accessKind)},
{"passwordRequired", e.passwordRequired != 0},
{"levelMin", e.levelMin},
{"maxMembers", e.maxMembers},
{"topicSetByMods", e.topicSetByMods != 0},
{"zoneDefaultMapId", e.zoneDefaultMapId},
{"iconColorRGBA", e.iconColorRGBA},
});
}
j["entries"] = arr;
std::ofstream os(out);
if (!os) {
std::fprintf(stderr,
"export-wgch-json: failed to open %s for write\n",
out.c_str());
return 1;
}
os << j.dump(2) << "\n";
std::printf("Wrote %s (%zu channels)\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) == ".wgch.json") {
outBase.resize(outBase.size() - 10);
} else {
stripExt(outBase, ".json");
stripExt(outBase, ".wgch");
}
}
std::ifstream is(in);
if (!is) {
std::fprintf(stderr,
"import-wgch-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-wgch-json: JSON parse error: %s\n", ex.what());
return 1;
}
wowee::pipeline::WoweeGlobalChannels c;
c.name = j.value("name", std::string{});
if (!j.contains("entries") || !j["entries"].is_array()) {
std::fprintf(stderr,
"import-wgch-json: missing or non-array 'entries'\n");
return 1;
}
for (const auto& je : j["entries"]) {
wowee::pipeline::WoweeGlobalChannels::Entry e;
e.channelId = je.value("channelId", 0u);
e.name = je.value("name", std::string{});
e.description = je.value("description", std::string{});
if (!readEnumField(je, "channelKind", "channelKindName",
parseChannelKindToken, "channelKind",
e.channelId, e.channelKind)) return 1;
if (!readEnumField(je, "accessKind", "accessKindName",
parseAccessKindToken, "accessKind",
e.channelId, e.accessKind)) return 1;
e.passwordRequired = je.value("passwordRequired", false) ? 1 : 0;
e.levelMin = static_cast<uint8_t>(je.value("levelMin", 0));
e.maxMembers = static_cast<uint16_t>(je.value("maxMembers", 0));
e.topicSetByMods = je.value("topicSetByMods", true) ? 1 : 0;
e.zoneDefaultMapId = je.value("zoneDefaultMapId", 0u);
e.iconColorRGBA = je.value("iconColorRGBA", 0xFFFFFFFFu);
c.entries.push_back(e);
}
if (!wowee::pipeline::WoweeGlobalChannelsLoader::save(c, outBase)) {
std::fprintf(stderr,
"import-wgch-json: failed to save %s.wgch\n",
outBase.c_str());
return 1;
}
std::printf("Wrote %s.wgch (%zu channels)\n",
outBase.c_str(), c.entries.size());
return 0;
}
} // namespace
bool handleGlobalChannelsCatalog(int& i, int argc, char** argv,
@ -275,6 +445,12 @@ bool handleGlobalChannelsCatalog(int& i, int argc, char** argv,
if (std::strcmp(argv[i], "--validate-wgch") == 0 && i + 1 < argc) {
outRc = handleValidate(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--export-wgch-json") == 0 && i + 1 < argc) {
outRc = handleExportJson(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--import-wgch-json") == 0 && i + 1 < argc) {
outRc = handleImportJson(i, argc, argv); return true;
}
return false;
}