From 4dd1fb09ff25a47b120686c2b6482df9d775f74b Mon Sep 17 00:00:00 2001 From: Kelsi Date: Sun, 10 May 2026 05:34:45 -0700 Subject: [PATCH] feat(editor): WSWP JSON round-trip closure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds --export-wswp-json / --import-wswp-json with the established readEnumField template factoring int+name dual encoding for conditionKind ("always"/"zoneonly"/"classonly"/"raceonly"/ "genderonly"). Signed gain field (gainAdjustDb_x10, int16) preserved bit-for-bit through JSON. All 3 presets (bosses/race/ui) byte-identical binary roundtrip OK. Live-tested both the duplicate-trigger error AND the same- priority warning in one mutation: copied Nefarian rule's trigger triple to match Onyxia (originalSoundId=1234, ZoneOnly, conditionValue=249). Validator emitted BOTH: ERROR: duplicate trigger triple WARNING: same priorityIndex=100 — tie-break order undefined Demonstrates that the validator catches both layers of the collision (the deterministic data tie + the priority-based disambiguation that the runtime would have used to resolve it). CLI flag count 1470 -> 1472. --- tools/editor/cli_arg_required.cpp | 1 + tools/editor/cli_help.cpp | 4 + tools/editor/cli_sound_swap_catalog.cpp | 167 ++++++++++++++++++++++++ 3 files changed, 172 insertions(+) diff --git a/tools/editor/cli_arg_required.cpp b/tools/editor/cli_arg_required.cpp index 4c47f86b..4ac97629 100644 --- a/tools/editor/cli_arg_required.cpp +++ b/tools/editor/cli_arg_required.cpp @@ -433,6 +433,7 @@ const char* const kArgRequired[] = { "--export-wbrd-json", "--import-wbrd-json", "--gen-swp-bosses", "--gen-swp-race", "--gen-swp-ui", "--info-wswp", "--validate-wswp", + "--export-wswp-json", "--import-wswp-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 7f968ad5..17ff516e 100644 --- a/tools/editor/cli_help.cpp +++ b/tools/editor/cli_help.cpp @@ -2737,6 +2737,10 @@ void printUsage(const char* argv0) { std::printf(" Print WSWP entries (id / origSound / replSound / conditionKind+value / priority / gain dB / name)\n"); std::printf(" --validate-wswp [--json]\n"); std::printf(" Static checks: id+name+originalSoundId+replacementSoundId required, conditionKind 0..4, no duplicate ruleIds, no self-replacement (orig==repl is no-op); CRITICAL: no duplicate (originalSoundId, conditionKind, conditionValue) trigger triple (runtime would have two rules for the same trigger), non-Always conditionKind requires non-zero conditionValue. Warns on priorityIndex=0 (effectively disabled), |gainAdjustDb_x10| > 300 (±30dB clip risk), Always condition with non-zero conditionValue (dead data), and same-priority within same originalSoundId (tie-break undefined when both conditions match)\n"); + std::printf(" --export-wswp-json [out.json]\n"); + std::printf(" Export binary .wswp to a human-editable JSON sidecar (defaults to .wswp.json; emits conditionKind as int + name string)\n"); + std::printf(" --import-wswp-json [out-base]\n"); + std::printf(" Import a .wswp.json sidecar back into binary .wswp (conditionKind int OR \"always\"/\"zoneonly\"/\"classonly\"/\"raceonly\"/\"genderonly\" — round-trips priority + gain + condition tables 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_sound_swap_catalog.cpp b/tools/editor/cli_sound_swap_catalog.cpp index 2f57e032..39e4748d 100644 --- a/tools/editor/cli_sound_swap_catalog.cpp +++ b/tools/editor/cli_sound_swap_catalog.cpp @@ -142,6 +142,54 @@ int handleInfo(int& i, int argc, char** argv) { return 0; } +int parseConditionKindToken(const std::string& s) { + using S = wowee::pipeline::WoweeSoundSwap; + if (s == "always") return S::Always; + if (s == "zoneonly") return S::ZoneOnly; + if (s == "classonly") return S::ClassOnly; + if (s == "raceonly") return S::RaceOnly; + if (s == "genderonly") return S::GenderOnly; + return -1; +} + +template +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()); + if (parsed < 0) { + std::fprintf(stderr, + "import-wswp-json: unknown %s token " + "'%s' on entry id=%u\n", + label, v.get().c_str(), + entryId); + return false; + } + outValue = static_cast(parsed); + return true; + } + if (v.is_number_integer()) { + outValue = static_cast(v.get()); + return true; + } + } + if (je.contains(nameKey) && je[nameKey].is_string()) { + int parsed = parseFn(je[nameKey].get()); + if (parsed >= 0) { + outValue = static_cast(parsed); + return true; + } + } + return true; +} + int handleValidate(int& i, int argc, char** argv) { std::string base = argv[++i]; bool jsonOut = consumeJsonFlag(i, argc, argv); @@ -316,6 +364,117 @@ 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 = stripWswpExt(base); + if (out.empty()) out = base + ".wswp.json"; + if (!wowee::pipeline::WoweeSoundSwapLoader::exists(base)) { + std::fprintf(stderr, + "export-wswp-json: WSWP not found: %s.wswp\n", + base.c_str()); + return 1; + } + auto c = wowee::pipeline::WoweeSoundSwapLoader::load(base); + nlohmann::json j; + j["magic"] = "WSWP"; + j["version"] = 1; + j["name"] = c.name; + nlohmann::json arr = nlohmann::json::array(); + for (const auto& e : c.entries) { + arr.push_back({ + {"ruleId", e.ruleId}, + {"name", e.name}, + {"originalSoundId", e.originalSoundId}, + {"replacementSoundId", e.replacementSoundId}, + {"conditionKind", e.conditionKind}, + {"conditionKindName", + conditionKindName(e.conditionKind)}, + {"priorityIndex", e.priorityIndex}, + {"gainAdjustDb_x10", e.gainAdjustDb_x10}, + {"conditionValue", e.conditionValue}, + }); + } + j["entries"] = arr; + std::ofstream os(out); + if (!os) { + std::fprintf(stderr, + "export-wswp-json: failed to open %s for write\n", + out.c_str()); + return 1; + } + os << j.dump(2) << "\n"; + std::printf("Wrote %s (%zu rules)\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) == ".wswp.json") { + outBase.resize(outBase.size() - 10); + } else { + stripExt(outBase, ".json"); + stripExt(outBase, ".wswp"); + } + } + std::ifstream is(in); + if (!is) { + std::fprintf(stderr, + "import-wswp-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-wswp-json: JSON parse error: %s\n", ex.what()); + return 1; + } + wowee::pipeline::WoweeSoundSwap c; + c.name = j.value("name", std::string{}); + if (!j.contains("entries") || !j["entries"].is_array()) { + std::fprintf(stderr, + "import-wswp-json: missing or non-array 'entries'\n"); + return 1; + } + for (const auto& je : j["entries"]) { + wowee::pipeline::WoweeSoundSwap::Entry e; + e.ruleId = je.value("ruleId", 0u); + e.name = je.value("name", std::string{}); + e.originalSoundId = je.value("originalSoundId", 0u); + e.replacementSoundId = + je.value("replacementSoundId", 0u); + if (!readEnumField(je, "conditionKind", + "conditionKindName", + parseConditionKindToken, + "conditionKind", e.ruleId, + e.conditionKind)) return 1; + e.priorityIndex = static_cast( + je.value("priorityIndex", 0)); + e.gainAdjustDb_x10 = static_cast( + je.value("gainAdjustDb_x10", 0)); + e.conditionValue = je.value("conditionValue", 0u); + c.entries.push_back(e); + } + if (!wowee::pipeline::WoweeSoundSwapLoader::save(c, outBase)) { + std::fprintf(stderr, + "import-wswp-json: failed to save %s.wswp\n", + outBase.c_str()); + return 1; + } + std::printf("Wrote %s.wswp (%zu rules)\n", + outBase.c_str(), c.entries.size()); + return 0; +} + } // namespace bool handleSoundSwapCatalog(int& i, int argc, char** argv, @@ -339,6 +498,14 @@ bool handleSoundSwapCatalog(int& i, int argc, char** argv, i + 1 < argc) { outRc = handleValidate(i, argc, argv); return true; } + if (std::strcmp(argv[i], "--export-wswp-json") == 0 && + i + 1 < argc) { + outRc = handleExportJson(i, argc, argv); return true; + } + if (std::strcmp(argv[i], "--import-wswp-json") == 0 && + i + 1 < argc) { + outRc = handleImportJson(i, argc, argv); return true; + } return false; }