feat(editor): add WEMO JSON round-trip (--export/--import-wemo-json)

Dual encoding for all 3 WEMO enums on import: emoteKind
(int OR "social"/"combat"/"roleplay"/"system"), sex
("both"/"male"/"female"), ttsHint ("talk"/"whisper"/"yell"/
"silent"). Reuses the readEnumField template pattern from
WMSP so the 3 enums share one parser body.

Also escapes a stray %s in the validate-wemo help text
that gcc -Wformat had been flagging as a missing-argument
warning. The literal "%s" appears inside the warning
description string so it must be encoded as %%s for
printf to render the helptext glyph correctly.

All 3 presets (basic/combat/rp) byte-identical roundtrip
OK. Token-form import smoke-tested with mixed roleplay +
female + whisper. CLI flag count 1131 -> 1133.
This commit is contained in:
Kelsi 2026-05-10 01:06:59 -07:00
parent c9b822002f
commit dc0c71fdd7
3 changed files with 204 additions and 1 deletions

View file

@ -312,6 +312,7 @@ const char* const kArgRequired[] = {
"--export-wmsp-json", "--import-wmsp-json",
"--gen-emo", "--gen-emo-combat", "--gen-emo-rp",
"--info-wemo", "--validate-wemo",
"--export-wemo-json", "--import-wemo-json",
"--gen-weather-temperate", "--gen-weather-arctic",
"--gen-weather-desert", "--gen-weather-stormy",
"--gen-zone-atmosphere",

View file

@ -161,6 +161,198 @@ int handleInfo(int& i, int argc, char** argv) {
return 0;
}
// Token parsers for the three WEMO enums.
int parseEmoteKindToken(const std::string& s) {
using E = wowee::pipeline::WoweeEmotes;
if (s == "social") return E::Social;
if (s == "combat") return E::Combat;
if (s == "roleplay") return E::RolePlay;
if (s == "system") return E::System;
return -1;
}
int parseSexFilterToken(const std::string& s) {
using E = wowee::pipeline::WoweeEmotes;
if (s == "both") return E::SexBoth;
if (s == "male") return E::MaleOnly;
if (s == "female") return E::FemaleOnly;
return -1;
}
int parseTtsHintToken(const std::string& s) {
using E = wowee::pipeline::WoweeEmotes;
if (s == "talk") return E::TtsTalk;
if (s == "whisper") return E::TtsWhisper;
if (s == "yell") return E::TtsYell;
if (s == "silent") return E::TtsSilent;
return -1;
}
// Generic int-or-token coercion — same shape as the
// WMSP helper. Returns false on parse error (hard error)
// and reports via stderr; returns true on success OR
// when the field is absent (leave outValue at default).
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-wemo-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;
}
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 = stripWemoExt(base);
if (out.empty()) out = base + ".wemo.json";
if (!wowee::pipeline::WoweeEmotesLoader::exists(base)) {
std::fprintf(stderr,
"export-wemo-json: WEMO not found: %s.wemo\n",
base.c_str());
return 1;
}
auto c = wowee::pipeline::WoweeEmotesLoader::load(base);
nlohmann::json j;
j["magic"] = "WEMO";
j["version"] = 1;
j["name"] = c.name;
nlohmann::json arr = nlohmann::json::array();
for (const auto& e : c.entries) {
arr.push_back({
{"emoteId", e.emoteId},
{"name", e.name},
{"description", e.description},
{"slashCommand", e.slashCommand},
{"animationId", e.animationId},
{"soundId", e.soundId},
{"targetMessage", e.targetMessage},
{"noTargetMessage", e.noTargetMessage},
{"emoteKind", e.emoteKind},
{"emoteKindName", emoteKindName(e.emoteKind)},
{"sex", e.sex},
{"sexName", sexFilterName(e.sex)},
{"requiredRace", e.requiredRace},
{"ttsHint", e.ttsHint},
{"ttsHintName", ttsHintName(e.ttsHint)},
{"iconColorRGBA", e.iconColorRGBA},
});
}
j["entries"] = arr;
std::ofstream os(out);
if (!os) {
std::fprintf(stderr,
"export-wemo-json: failed to open %s for write\n",
out.c_str());
return 1;
}
os << j.dump(2) << "\n";
std::printf("Wrote %s (%zu emotes)\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) == ".wemo.json") {
outBase.resize(outBase.size() - 10);
} else {
stripExt(outBase, ".json");
stripExt(outBase, ".wemo");
}
}
std::ifstream is(in);
if (!is) {
std::fprintf(stderr,
"import-wemo-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-wemo-json: JSON parse error: %s\n", ex.what());
return 1;
}
wowee::pipeline::WoweeEmotes c;
c.name = j.value("name", std::string{});
if (!j.contains("entries") || !j["entries"].is_array()) {
std::fprintf(stderr,
"import-wemo-json: missing or non-array 'entries'\n");
return 1;
}
for (const auto& je : j["entries"]) {
wowee::pipeline::WoweeEmotes::Entry e;
e.emoteId = je.value("emoteId", 0u);
e.name = je.value("name", std::string{});
e.description = je.value("description", std::string{});
e.slashCommand = je.value("slashCommand", std::string{});
e.animationId = je.value("animationId", 0u);
e.soundId = je.value("soundId", 0u);
e.targetMessage = je.value("targetMessage", std::string{});
e.noTargetMessage = je.value("noTargetMessage",
std::string{});
if (!readEnumField(je, "emoteKind", "emoteKindName",
parseEmoteKindToken, "emoteKind",
e.emoteId, e.emoteKind)) return 1;
if (!readEnumField(je, "sex", "sexName",
parseSexFilterToken, "sex",
e.emoteId, e.sex)) return 1;
if (!readEnumField(je, "ttsHint", "ttsHintName",
parseTtsHintToken, "ttsHint",
e.emoteId, e.ttsHint)) return 1;
e.requiredRace = static_cast<uint8_t>(
je.value("requiredRace", 0u));
e.iconColorRGBA = je.value("iconColorRGBA", 0xFFFFFFFFu);
c.entries.push_back(e);
}
if (!wowee::pipeline::WoweeEmotesLoader::save(c, outBase)) {
std::fprintf(stderr,
"import-wemo-json: failed to save %s.wemo\n",
outBase.c_str());
return 1;
}
std::printf("Wrote %s.wemo (%zu emotes)\n",
outBase.c_str(), c.entries.size());
return 0;
}
int handleValidate(int& i, int argc, char** argv) {
std::string base = argv[++i];
bool jsonOut = consumeJsonFlag(i, argc, argv);
@ -319,6 +511,12 @@ bool handleEmotesCatalog(int& i, int argc, char** argv, int& outRc) {
if (std::strcmp(argv[i], "--validate-wemo") == 0 && i + 1 < argc) {
outRc = handleValidate(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--export-wemo-json") == 0 && i + 1 < argc) {
outRc = handleExportJson(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--import-wemo-json") == 0 && i + 1 < argc) {
outRc = handleImportJson(i, argc, argv); return true;
}
return false;
}

View file

@ -2176,7 +2176,11 @@ void printUsage(const char* argv0) {
std::printf(" --info-wemo <wemo-base> [--json]\n");
std::printf(" Print WEMO entries (id / slash command / kind / animation / sound / sex filter / TTS hint)\n");
std::printf(" --validate-wemo <wemo-base> [--json]\n");
std::printf(" Static checks: id+name+slashCommand required, slash must be lowercase + no leading '/' (chat parser case-folds before lookup), emoteKind 0..3, sex 0..2, ttsHint 0..3, no duplicate ids OR commands; warns on targetMessage with !=2 %s tokens, noTargetMessage with !=1\n");
std::printf(" Static checks: id+name+slashCommand required, slash must be lowercase + no leading '/' (chat parser case-folds before lookup), emoteKind 0..3, sex 0..2, ttsHint 0..3, no duplicate ids OR commands; warns on targetMessage with !=2 %%s tokens, noTargetMessage with !=1\n");
std::printf(" --export-wemo-json <wemo-base> [out.json]\n");
std::printf(" Export binary .wemo to a human-editable JSON sidecar (defaults to <base>.wemo.json; emits all 3 enums as both int AND name string)\n");
std::printf(" --import-wemo-json <json-path> [out-base]\n");
std::printf(" Import a .wemo.json sidecar back into binary .wemo (emoteKind int OR \"social\"/\"combat\"/\"roleplay\"/\"system\"; sex int OR \"both\"/\"male\"/\"female\"; ttsHint int OR \"talk\"/\"whisper\"/\"yell\"/\"silent\")\n");
std::printf(" --catalog-pluck <wXXX-file> <id> [--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(" --gen-weather-temperate <wow-base> [zoneName]\n");