Kelsidavis-WoWee/tools/editor/cli_sound_swap_catalog.cpp
Kelsi 8c36fc5274 feat(pipeline): WSWP sound swap rules catalog (141st open format)
Novel format covering a need vanilla WoW lacked entirely:
priority-based sound substitution. Blizzard had no formal
mechanism for swapping a stock SoundEntry for a custom
replacement conditionally on zone/class/race/gender; the
closest equivalents were patch-level SoundEntries.dbc edits
with no condition support. Each WSWP entry binds one
(originalSoundId, condition) trigger to a replacementSoundId,
a priority index for tie-breaking (higher wins), and an
optional gain adjustment in 0.1 dB units (range ±30 dB
practical mixer limit).

Three presets covering common substitution scenarios:
  --gen-swp-bosses  3 raid-boss zone-only swaps (Onyxia roar
                    in Onyxia's Lair / Ragnaros emerge in
                    Molten Core +2dB / Nefarian shout in BWL).
                    Priority 100 — beats global rules
  --gen-swp-race    3 race-conditional voice swaps (BloodElf
                    priest / Tauren shaman / Undead warlock
                    cast voices). Priority 50
  --gen-swp-ui      3 always-on UI sound swaps (level-up /
                    quest-complete / mount-up) at priority
                    10 with +3dB gain (boss/race overrides
                    win the priority fight)

Validator catches: id+name+original+replacement required,
conditionKind 0..4, no duplicate ruleIds, no self-replacement
(orig==repl is a no-op slot), non-Always kinds require non-
zero conditionValue (kind without target = matches everything,
duplicating Always semantics). CRITICAL: no duplicate
(originalSoundId, conditionKind, conditionValue) trigger
triple — runtime would have two rules for the same trigger
without a tie-breaker. Warns on priorityIndex=0 (rule never
wins), |gainAdjustDb_x10| > 300 (clip risk), Always condition
with non-zero conditionValue (dead data ignored at runtime),
and same-priority within same originalSoundId (tie-break
undefined when both rules' conditions match simultaneously).

Format count 140 -> 141. CLI flag count 1463 -> 1470.
2026-05-10 05:32:23 -07:00

347 lines
12 KiB
C++

#include "cli_sound_swap_catalog.hpp"
#include "cli_arg_parse.hpp"
#include "cli_box_emitter.hpp"
#include "pipeline/wowee_sound_swap.hpp"
#include <nlohmann/json.hpp>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <set>
#include <string>
#include <tuple>
#include <vector>
namespace wowee {
namespace editor {
namespace cli {
namespace {
std::string stripWswpExt(std::string base) {
stripExt(base, ".wswp");
return base;
}
const char* conditionKindName(uint8_t k) {
using S = wowee::pipeline::WoweeSoundSwap;
switch (k) {
case S::Always: return "always";
case S::ZoneOnly: return "zoneonly";
case S::ClassOnly: return "classonly";
case S::RaceOnly: return "raceonly";
case S::GenderOnly: return "genderonly";
default: return "?";
}
}
bool saveOrError(const wowee::pipeline::WoweeSoundSwap& c,
const std::string& base, const char* cmd) {
if (!wowee::pipeline::WoweeSoundSwapLoader::save(c, base)) {
std::fprintf(stderr, "%s: failed to save %s.wswp\n",
cmd, base.c_str());
return false;
}
return true;
}
void printGenSummary(const wowee::pipeline::WoweeSoundSwap& c,
const std::string& base) {
std::printf("Wrote %s.wswp\n", base.c_str());
std::printf(" catalog : %s\n", c.name.c_str());
std::printf(" rules : %zu\n", c.entries.size());
}
int handleGenBosses(int& i, int argc, char** argv) {
std::string base = argv[++i];
std::string name = "BossSoundOverrides";
if (parseOptArg(i, argc, argv)) name = argv[++i];
base = stripWswpExt(base);
auto c = wowee::pipeline::WoweeSoundSwapLoader::
makeBossOverrides(name);
if (!saveOrError(c, base, "gen-swp-bosses")) return 1;
printGenSummary(c, base);
return 0;
}
int handleGenRace(int& i, int argc, char** argv) {
std::string base = argv[++i];
std::string name = "RaceVoiceOverrides";
if (parseOptArg(i, argc, argv)) name = argv[++i];
base = stripWswpExt(base);
auto c = wowee::pipeline::WoweeSoundSwapLoader::
makeRaceVoices(name);
if (!saveOrError(c, base, "gen-swp-race")) return 1;
printGenSummary(c, base);
return 0;
}
int handleGenUI(int& i, int argc, char** argv) {
std::string base = argv[++i];
std::string name = "GlobalUISoundOverrides";
if (parseOptArg(i, argc, argv)) name = argv[++i];
base = stripWswpExt(base);
auto c = wowee::pipeline::WoweeSoundSwapLoader::
makeGlobalUI(name);
if (!saveOrError(c, base, "gen-swp-ui")) return 1;
printGenSummary(c, base);
return 0;
}
int handleInfo(int& i, int argc, char** argv) {
std::string base = argv[++i];
bool jsonOut = consumeJsonFlag(i, argc, argv);
base = stripWswpExt(base);
if (!wowee::pipeline::WoweeSoundSwapLoader::exists(base)) {
std::fprintf(stderr, "WSWP not found: %s.wswp\n",
base.c_str());
return 1;
}
auto c = wowee::pipeline::WoweeSoundSwapLoader::load(base);
if (jsonOut) {
nlohmann::json j;
j["wswp"] = base + ".wswp";
j["name"] = c.name;
j["count"] = c.entries.size();
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::printf("%s\n", j.dump(2).c_str());
return 0;
}
std::printf("WSWP: %s.wswp\n", base.c_str());
std::printf(" catalog : %s\n", c.name.c_str());
std::printf(" rules : %zu\n", c.entries.size());
if (c.entries.empty()) return 0;
std::printf(" id origSound replSound condition value prio gain name\n");
for (const auto& e : c.entries) {
std::printf(" %4u %9u %9u %-12s %5u %3u %+4d %s\n",
e.ruleId, e.originalSoundId,
e.replacementSoundId,
conditionKindName(e.conditionKind),
e.conditionValue,
e.priorityIndex,
e.gainAdjustDb_x10,
e.name.c_str());
}
return 0;
}
int handleValidate(int& i, int argc, char** argv) {
std::string base = argv[++i];
bool jsonOut = consumeJsonFlag(i, argc, argv);
base = stripWswpExt(base);
if (!wowee::pipeline::WoweeSoundSwapLoader::exists(base)) {
std::fprintf(stderr,
"validate-wswp: WSWP not found: %s.wswp\n",
base.c_str());
return 1;
}
auto c = wowee::pipeline::WoweeSoundSwapLoader::load(base);
std::vector<std::string> errors;
std::vector<std::string> warnings;
if (c.entries.empty()) {
warnings.push_back("catalog has zero entries");
}
std::set<uint32_t> idsSeen;
using Triple = std::tuple<uint32_t, uint8_t, uint32_t>;
std::set<Triple> tripleSeen;
using PrioPair = std::pair<uint32_t, uint8_t>;
std::set<PrioPair> prioSeen;
for (size_t k = 0; k < c.entries.size(); ++k) {
const auto& e = c.entries[k];
std::string ctx = "entry " + std::to_string(k) +
" (id=" + std::to_string(e.ruleId);
if (!e.name.empty()) ctx += " " + e.name;
ctx += ")";
if (e.ruleId == 0)
errors.push_back(ctx + ": ruleId is 0");
if (e.name.empty())
errors.push_back(ctx + ": name is empty");
if (e.originalSoundId == 0)
errors.push_back(ctx +
": originalSoundId is 0 — no source "
"sound to swap");
if (e.replacementSoundId == 0)
errors.push_back(ctx +
": replacementSoundId is 0 — no "
"replacement to play");
if (e.conditionKind > 4) {
errors.push_back(ctx + ": conditionKind " +
std::to_string(e.conditionKind) +
" out of range (0..4)");
}
// Self-replacement is always a bug — replacing
// a sound with itself is a no-op that wastes
// a dispatch slot.
if (e.originalSoundId != 0 &&
e.originalSoundId == e.replacementSoundId) {
errors.push_back(ctx +
": originalSoundId == replacementSoundId="
+ std::to_string(e.originalSoundId) +
" — no-op self-replacement");
}
// priorityIndex == 0 means the rule is never
// picked when any other rule for the same
// sound matches. Could be intentional
// (disable rule) but warn.
if (e.priorityIndex == 0) {
warnings.push_back(ctx +
": priorityIndex=0 — rule never wins "
"tie-break (effectively disabled); "
"remove or set priority > 0");
}
// Gain range clamp: ±30 dB is the practical
// limit for client mixers; beyond risks
// clipping or inaudibility.
if (e.gainAdjustDb_x10 > 300 ||
e.gainAdjustDb_x10 < -300) {
warnings.push_back(ctx +
": gainAdjustDb_x10=" +
std::to_string(e.gainAdjustDb_x10) +
" (=" +
std::to_string(e.gainAdjustDb_x10 / 10) +
" dB) outside ±30 dB practical range "
"— mixer may clip or sound becomes "
"inaudible");
}
// Condition-value sanity: Always condition
// should have value=0 (any other value is
// dead data). Other kinds need value != 0
// (kind without target = matches everything,
// duplicating Always semantics).
using S = wowee::pipeline::WoweeSoundSwap;
if (e.conditionKind == S::Always &&
e.conditionValue != 0) {
warnings.push_back(ctx +
": Always condition with non-zero "
"conditionValue=" +
std::to_string(e.conditionValue) +
" — value is ignored at runtime "
"(dead data)");
}
if (e.conditionKind != S::Always &&
e.conditionValue == 0) {
errors.push_back(ctx +
": non-Always conditionKind=" +
std::string(conditionKindName(e.conditionKind))
+ " requires non-zero conditionValue");
}
// (originalSoundId, conditionKind,
// conditionValue) MUST be unique — two rules
// with the same trigger triple at different
// priorities are still ordered, but two with
// the SAME priority would tie.
Triple t{e.originalSoundId, e.conditionKind,
e.conditionValue};
if (e.originalSoundId != 0 &&
!tripleSeen.insert(t).second) {
errors.push_back(ctx +
": duplicate trigger triple "
"(originalSoundId=" +
std::to_string(e.originalSoundId) +
", conditionKind=" +
std::string(conditionKindName(e.conditionKind))
+ ", conditionValue=" +
std::to_string(e.conditionValue) +
") — runtime would have two rules "
"for the same trigger");
}
// Same priority within same originalSoundId
// is a tie-break ambiguity even if conditions
// differ.
PrioPair pp{e.originalSoundId, e.priorityIndex};
if (e.originalSoundId != 0 &&
e.priorityIndex != 0 &&
!prioSeen.insert(pp).second) {
warnings.push_back(ctx +
": originalSoundId=" +
std::to_string(e.originalSoundId) +
" has another rule at same "
"priorityIndex=" +
std::to_string(e.priorityIndex) +
" — tie-break order undefined when "
"both rules' conditions match");
}
if (!idsSeen.insert(e.ruleId).second) {
errors.push_back(ctx + ": duplicate ruleId");
}
}
bool ok = errors.empty();
if (jsonOut) {
nlohmann::json j;
j["wswp"] = base + ".wswp";
j["ok"] = ok;
j["errors"] = errors;
j["warnings"] = warnings;
std::printf("%s\n", j.dump(2).c_str());
return ok ? 0 : 1;
}
std::printf("validate-wswp: %s.wswp\n", base.c_str());
if (ok && warnings.empty()) {
std::printf(" OK — %zu rules, all ruleIds unique, "
"non-zero original+replacement sound, "
"no self-replacement, conditionKind "
"0..4, no duplicate trigger triples, "
"non-Always kinds have non-zero "
"conditionValue\n",
c.entries.size());
return 0;
}
if (!warnings.empty()) {
std::printf(" warnings (%zu):\n", warnings.size());
for (const auto& w : warnings)
std::printf(" - %s\n", w.c_str());
}
if (!errors.empty()) {
std::printf(" ERRORS (%zu):\n", errors.size());
for (const auto& e : errors)
std::printf(" - %s\n", e.c_str());
}
return ok ? 0 : 1;
}
} // namespace
bool handleSoundSwapCatalog(int& i, int argc, char** argv,
int& outRc) {
if (std::strcmp(argv[i], "--gen-swp-bosses") == 0 &&
i + 1 < argc) {
outRc = handleGenBosses(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--gen-swp-race") == 0 &&
i + 1 < argc) {
outRc = handleGenRace(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--gen-swp-ui") == 0 &&
i + 1 < argc) {
outRc = handleGenUI(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--info-wswp") == 0 && i + 1 < argc) {
outRc = handleInfo(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--validate-wswp") == 0 &&
i + 1 < argc) {
outRc = handleValidate(i, argc, argv); return true;
}
return false;
}
} // namespace cli
} // namespace editor
} // namespace wowee