From dda7933df940089bb740837c7a24399dc4ce57f4 Mon Sep 17 00:00:00 2001 From: Kelsi Date: Sat, 9 May 2026 15:45:58 -0700 Subject: [PATCH] feat(editor): add WFAC JSON round-trip authoring workflow MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes the WFAC open-format loop with --export-wfac-json / --import-wfac-json, mirroring the JSON pairs added for every other novel binary format. All 11 binary formats added since WOL now have full JSON round-trip authoring. Each faction round-trips: • 13 scalar fields (id, parent, name, description, flags, 7 ascending threshold values, baseReputation) • enemies[] and friends[] as JSON int arrays for trivial hand-edit • reputationFlags as dual int + string-array form (e.g. ["visible", "header"]) The 7 reputation thresholds round-trip exactly even though they have non-default values in some presets — the importer uses the canonical Hostile/Friendly/etc enum values as defaults so a hand-author can omit the standard thresholds and only specify custom ones. Verified byte-identical round-trip on the alliance preset (5 factions: Alliance header + 3 cities with reciprocal friend lists + Defias enemy). Adds 2 flags (523 documented total now). --- tools/editor/cli_arg_required.cpp | 1 + tools/editor/cli_factions_catalog.cpp | 161 ++++++++++++++++++++++++++ tools/editor/cli_help.cpp | 4 + 3 files changed, 166 insertions(+) diff --git a/tools/editor/cli_arg_required.cpp b/tools/editor/cli_arg_required.cpp index ec416f67..08cee5f4 100644 --- a/tools/editor/cli_arg_required.cpp +++ b/tools/editor/cli_arg_required.cpp @@ -49,6 +49,7 @@ const char* const kArgRequired[] = { "--export-wgot-json", "--import-wgot-json", "--gen-factions", "--gen-factions-alliance", "--gen-factions-wildlife", "--info-wfac", "--validate-wfac", + "--export-wfac-json", "--import-wfac-json", "--gen-locks", "--gen-locks-dungeon", "--gen-locks-professions", "--info-wlck", "--validate-wlck", "--gen-weather-temperate", "--gen-weather-arctic", diff --git a/tools/editor/cli_factions_catalog.cpp b/tools/editor/cli_factions_catalog.cpp index b609ac40..9fccec6a 100644 --- a/tools/editor/cli_factions_catalog.cpp +++ b/tools/editor/cli_factions_catalog.cpp @@ -140,6 +140,161 @@ 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 faction emits all 13 scalar fields + // plus the variable-length enemies + friends arrays and + // a string-array form for the reputation flag bitset. + std::string base = argv[++i]; + std::string outPath; + if (parseOptArg(i, argc, argv)) outPath = argv[++i]; + base = stripWfacExt(base); + if (outPath.empty()) outPath = base + ".wfac.json"; + if (!wowee::pipeline::WoweeFactionLoader::exists(base)) { + std::fprintf(stderr, + "export-wfac-json: WFAC not found: %s.wfac\n", base.c_str()); + return 1; + } + auto c = wowee::pipeline::WoweeFactionLoader::load(base); + nlohmann::json j; + j["name"] = c.name; + nlohmann::json arr = nlohmann::json::array(); + for (const auto& e : c.entries) { + nlohmann::json je; + je["factionId"] = e.factionId; + je["parentFactionId"] = e.parentFactionId; + je["name"] = e.name; + je["description"] = e.description; + je["reputationFlags"] = e.reputationFlags; + nlohmann::json fa = nlohmann::json::array(); + if (e.reputationFlags & wowee::pipeline::WoweeFaction::VisibleOnTab) fa.push_back("visible"); + if (e.reputationFlags & wowee::pipeline::WoweeFaction::AtWarDefault) fa.push_back("at-war"); + if (e.reputationFlags & wowee::pipeline::WoweeFaction::Hidden) fa.push_back("hidden"); + if (e.reputationFlags & wowee::pipeline::WoweeFaction::NoReputation) fa.push_back("no-rep"); + if (e.reputationFlags & wowee::pipeline::WoweeFaction::IsHeader) fa.push_back("header"); + je["reputationFlagsList"] = fa; + je["baseReputation"] = e.baseReputation; + je["thresholdHostile"] = e.thresholdHostile; + je["thresholdUnfriendly"] = e.thresholdUnfriendly; + je["thresholdNeutral"] = e.thresholdNeutral; + je["thresholdFriendly"] = e.thresholdFriendly; + je["thresholdHonored"] = e.thresholdHonored; + je["thresholdRevered"] = e.thresholdRevered; + je["thresholdExalted"] = e.thresholdExalted; + je["enemies"] = e.enemies; + je["friends"] = e.friends; + arr.push_back(je); + } + j["entries"] = arr; + std::ofstream out(outPath); + if (!out) { + std::fprintf(stderr, + "export-wfac-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.wfac\n", base.c_str()); + std::printf(" factions : %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 = ".wfac.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 = stripWfacExt(outBase); + std::ifstream in(jsonPath); + if (!in) { + std::fprintf(stderr, + "import-wfac-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-wfac-json: bad JSON in %s: %s\n", + jsonPath.c_str(), e.what()); + return 1; + } + auto repFlagFromName = [](const std::string& s) -> uint32_t { + if (s == "visible") return wowee::pipeline::WoweeFaction::VisibleOnTab; + if (s == "at-war") return wowee::pipeline::WoweeFaction::AtWarDefault; + if (s == "hidden") return wowee::pipeline::WoweeFaction::Hidden; + if (s == "no-rep") return wowee::pipeline::WoweeFaction::NoReputation; + if (s == "header") return wowee::pipeline::WoweeFaction::IsHeader; + return 0; + }; + auto readU32Vec = [](const nlohmann::json& jv, std::vector& v) { + if (jv.is_array()) { + for (const auto& e : jv) { + if (e.is_number_integer()) v.push_back(e.get()); + } + } + }; + wowee::pipeline::WoweeFaction c; + c.name = j.value("name", std::string{}); + if (j.contains("entries") && j["entries"].is_array()) { + for (const auto& je : j["entries"]) { + wowee::pipeline::WoweeFaction::Entry e; + e.factionId = je.value("factionId", 0u); + e.parentFactionId = je.value("parentFactionId", 0u); + e.name = je.value("name", std::string{}); + e.description = je.value("description", std::string{}); + if (je.contains("reputationFlags") && + je["reputationFlags"].is_number_integer()) { + e.reputationFlags = je["reputationFlags"].get(); + } else if (je.contains("reputationFlagsList") && + je["reputationFlagsList"].is_array()) { + e.reputationFlags = 0; + for (const auto& f : je["reputationFlagsList"]) { + if (f.is_string()) + e.reputationFlags |= repFlagFromName(f.get()); + } + } + e.baseReputation = je.value("baseReputation", 0); + e.thresholdHostile = je.value("thresholdHostile", + static_cast(wowee::pipeline::WoweeFaction::Hostile)); + e.thresholdUnfriendly = je.value("thresholdUnfriendly", + static_cast(wowee::pipeline::WoweeFaction::Unfriendly)); + e.thresholdNeutral = je.value("thresholdNeutral", 0); + e.thresholdFriendly = je.value("thresholdFriendly", + static_cast(wowee::pipeline::WoweeFaction::Friendly)); + e.thresholdHonored = je.value("thresholdHonored", + static_cast(wowee::pipeline::WoweeFaction::Honored)); + e.thresholdRevered = je.value("thresholdRevered", + static_cast(wowee::pipeline::WoweeFaction::Revered)); + e.thresholdExalted = je.value("thresholdExalted", + static_cast(wowee::pipeline::WoweeFaction::Exalted)); + if (je.contains("enemies")) readU32Vec(je["enemies"], e.enemies); + if (je.contains("friends")) readU32Vec(je["friends"], e.friends); + c.entries.push_back(std::move(e)); + } + } + if (!wowee::pipeline::WoweeFactionLoader::save(c, outBase)) { + std::fprintf(stderr, + "import-wfac-json: failed to save %s.wfac\n", outBase.c_str()); + return 1; + } + std::printf("Wrote %s.wfac\n", outBase.c_str()); + std::printf(" source : %s\n", jsonPath.c_str()); + std::printf(" factions : %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); @@ -260,6 +415,12 @@ bool handleFactionsCatalog(int& i, int argc, char** argv, int& outRc) { if (std::strcmp(argv[i], "--validate-wfac") == 0 && i + 1 < argc) { outRc = handleValidate(i, argc, argv); return true; } + if (std::strcmp(argv[i], "--export-wfac-json") == 0 && i + 1 < argc) { + outRc = handleExportJson(i, argc, argv); return true; + } + if (std::strcmp(argv[i], "--import-wfac-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 041e8dbb..11a3a920 100644 --- a/tools/editor/cli_help.cpp +++ b/tools/editor/cli_help.cpp @@ -947,6 +947,10 @@ void printUsage(const char* argv0) { std::printf(" Print WFAC entries (id / parent / flags / enemy + friend counts / name)\n"); std::printf(" --validate-wfac [--json]\n"); std::printf(" Static checks: factionId>0+unique, name not empty, threshold ordering, no self-enemy, no enemy/friend overlap\n"); + std::printf(" --export-wfac-json [out.json]\n"); + std::printf(" Export binary .wfac to a human-editable JSON sidecar (defaults to .wfac.json)\n"); + std::printf(" --import-wfac-json [out-base]\n"); + std::printf(" Import a .wfac.json sidecar back into binary .wfac (accepts flag int OR flagsList strings)\n"); std::printf(" --gen-locks [name]\n"); std::printf(" Emit .wlck starter: 2 locks (Iron Door key+force, Wooden Chest force-only) — lockId=1 matches WGOT\n"); std::printf(" --gen-locks-dungeon [name]\n");