From c60e779e67636fe789b91b8db01cc997e562a1e1 Mon Sep 17 00:00:00 2001 From: Kelsi Date: Sun, 10 May 2026 05:03:30 -0700 Subject: [PATCH] feat(editor): WIRC JSON round-trip closure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds --export-wirc-json / --import-wirc-json. allowedSlotsMask emitted as both int + readable string (e.g. "Helm|Chest|Leg| Boot") for tooling readability. Variable-length enchants serialize as JSON object array of {enchantId, weight}. All 3 presets (bear/eagle/tiger) byte-identical binary roundtrip OK including the bear pool's totalWeight=100 (30+50+15+5 enchant weight distribution). Live-tested totalWeight mismatch validator: hand-mutated bear pool enchant[1].weight from 50 to 60 (sum=110) while leaving totalWeight=100 in JSON. Validator correctly errored: "totalWeight=100 does not match sum of enchant weights=110 — loot generator would mis-pick". Catches the class of denormal- ized-cache-staleness bugs where the loot generator's hot-path roll uses a precomputed total that no longer matches the enchant table — players would see wrong rates of each enchant tier without any obvious symptom. CLI flag count 1434 -> 1436. --- tools/editor/cli_arg_required.cpp | 1 + tools/editor/cli_help.cpp | 4 + tools/editor/cli_random_property_catalog.cpp | 129 +++++++++++++++++++ 3 files changed, 134 insertions(+) diff --git a/tools/editor/cli_arg_required.cpp b/tools/editor/cli_arg_required.cpp index f8d3c6d1..338a62a6 100644 --- a/tools/editor/cli_arg_required.cpp +++ b/tools/editor/cli_arg_required.cpp @@ -421,6 +421,7 @@ const char* const kArgRequired[] = { "--export-wbhv-json", "--import-wbhv-json", "--gen-irc-bear", "--gen-irc-eagle", "--gen-irc-tiger", "--info-wirc", "--validate-wirc", + "--export-wirc-json", "--import-wirc-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 0aeb5d71..f957a991 100644 --- a/tools/editor/cli_help.cpp +++ b/tools/editor/cli_help.cpp @@ -2681,6 +2681,10 @@ void printUsage(const char* argv0) { std::printf(" Print WIRC entries (poolId / scaleLevel / allowedSlots-mask-as-string / classes-bitmask / totalWeight / enchant count / name)\n"); std::printf(" --validate-wirc [--json]\n"); std::printf(" Static checks: id+name required, allowedSlotsMask != 0 (else pool is unreachable — no slot would ever roll it), non-empty enchant array (loot generator needs something to pick), no zero-id enchants, no duplicate enchantIds within same pool (should be merged with summed weight), no duplicate poolIds; CRITICAL: totalWeight MUST equal sum of enchant weights (else loot generator's denormalized roll mis-picks the distribution). Warns on enchant weight=0 (never picked, dead entry)\n"); + std::printf(" --export-wirc-json [out.json]\n"); + std::printf(" Export binary .wirc to a human-editable JSON sidecar (defaults to .wirc.json; allowedSlotsMask emitted as int + readable string; enchants as JSON object array of {enchantId, weight})\n"); + std::printf(" --import-wirc-json [out-base]\n"); + std::printf(" Import a .wirc.json sidecar back into binary .wirc (allowedSlotsMask uint8 bitmask of Helm=0x01..Belt=0x80; enchants accept JSON object array — round-trips weighted pools byte-identical)\n"); std::printf(" --catalog-pluck [--json]\n"); std::printf(" Extract one entry by id from any registered catalog format. Auto-detects magic, dispatches to the per-format --info-* handler internally, then prints just the matching entry. Primary-key field is auto-detected (first *Id field, or first numeric)\n"); std::printf(" --catalog-find [--magic ] [--json]\n"); diff --git a/tools/editor/cli_random_property_catalog.cpp b/tools/editor/cli_random_property_catalog.cpp index 7ca907cd..09e6ed94 100644 --- a/tools/editor/cli_random_property_catalog.cpp +++ b/tools/editor/cli_random_property_catalog.cpp @@ -277,6 +277,127 @@ 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 = stripWircExt(base); + if (out.empty()) out = base + ".wirc.json"; + if (!wowee::pipeline::WoweeRandomPropertyLoader::exists(base)) { + std::fprintf(stderr, + "export-wirc-json: WIRC not found: %s.wirc\n", + base.c_str()); + return 1; + } + auto c = wowee::pipeline::WoweeRandomPropertyLoader::load(base); + nlohmann::json j; + j["magic"] = "WIRC"; + j["version"] = 1; + j["name"] = c.name; + nlohmann::json arr = nlohmann::json::array(); + for (const auto& e : c.entries) { + nlohmann::json enchants = nlohmann::json::array(); + for (const auto& en : e.enchants) { + enchants.push_back({ + {"enchantId", en.enchantId}, + {"weight", en.weight}, + }); + } + arr.push_back({ + {"poolId", e.poolId}, + {"name", e.name}, + {"scaleLevel", e.scaleLevel}, + {"allowedSlotsMask", e.allowedSlotsMask}, + {"allowedSlotsString", + slotsMaskString(e.allowedSlotsMask)}, + {"allowedClassesMask", e.allowedClassesMask}, + {"totalWeight", e.totalWeight}, + {"enchants", enchants}, + }); + } + j["entries"] = arr; + std::ofstream os(out); + if (!os) { + std::fprintf(stderr, + "export-wirc-json: failed to open %s for write\n", + out.c_str()); + return 1; + } + os << j.dump(2) << "\n"; + std::printf("Wrote %s (%zu pools)\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) == ".wirc.json") { + outBase.resize(outBase.size() - 10); + } else { + stripExt(outBase, ".json"); + stripExt(outBase, ".wirc"); + } + } + std::ifstream is(in); + if (!is) { + std::fprintf(stderr, + "import-wirc-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-wirc-json: JSON parse error: %s\n", ex.what()); + return 1; + } + wowee::pipeline::WoweeRandomProperty c; + c.name = j.value("name", std::string{}); + if (!j.contains("entries") || !j["entries"].is_array()) { + std::fprintf(stderr, + "import-wirc-json: missing or non-array 'entries'\n"); + return 1; + } + for (const auto& je : j["entries"]) { + wowee::pipeline::WoweeRandomProperty::Entry e; + e.poolId = je.value("poolId", 0u); + e.name = je.value("name", std::string{}); + e.scaleLevel = static_cast( + je.value("scaleLevel", 0)); + e.allowedSlotsMask = static_cast( + je.value("allowedSlotsMask", 0)); + e.allowedClassesMask = static_cast( + je.value("allowedClassesMask", 0)); + e.totalWeight = je.value("totalWeight", 0u); + if (je.contains("enchants") && + je["enchants"].is_array()) { + for (const auto& enj : je["enchants"]) { + wowee::pipeline::WoweeRandomProperty:: + EnchantEntry en; + en.enchantId = enj.value("enchantId", 0u); + en.weight = enj.value("weight", 0u); + e.enchants.push_back(en); + } + } + c.entries.push_back(e); + } + if (!wowee::pipeline::WoweeRandomPropertyLoader::save(c, outBase)) { + std::fprintf(stderr, + "import-wirc-json: failed to save %s.wirc\n", + outBase.c_str()); + return 1; + } + std::printf("Wrote %s.wirc (%zu pools)\n", + outBase.c_str(), c.entries.size()); + return 0; +} + } // namespace bool handleRandomPropertyCatalog(int& i, int argc, char** argv, @@ -300,6 +421,14 @@ bool handleRandomPropertyCatalog(int& i, int argc, char** argv, i + 1 < argc) { outRc = handleValidate(i, argc, argv); return true; } + if (std::strcmp(argv[i], "--export-wirc-json") == 0 && + i + 1 < argc) { + outRc = handleExportJson(i, argc, argv); return true; + } + if (std::strcmp(argv[i], "--import-wirc-json") == 0 && + i + 1 < argc) { + outRc = handleImportJson(i, argc, argv); return true; + } return false; }