feat(editor): add WPCR JSON round-trip (--export/--import-wpcr-json)

Dual encoding for actionKind (int 0..10 OR token form
covering all 11 kinds: revive/mend/feed/dismiss/tame/
beastlore/stable/untrain/rename/abandon/summon).
requiresPet and requiresStableNPC accept bool or int.
happinessRestore serializes as signed int8 (-25..+25
typical, +/-127 hard limit) — matters because Abandon's
happiness penalty (canonically -10) is negative.

All 3 presets (hunter/stable/warlock) byte-identical
roundtrip OK. CLI flag count 1204 -> 1206.
This commit is contained in:
Kelsi 2026-05-10 02:12:01 -07:00
parent cebf821205
commit 9a0309818e
3 changed files with 181 additions and 0 deletions

View file

@ -343,6 +343,7 @@ const char* const kArgRequired[] = {
"--export-wmnl-json", "--import-wmnl-json",
"--gen-pcr", "--gen-pcr-stable", "--gen-pcr-warlock",
"--info-wpcr", "--validate-wpcr",
"--export-wpcr-json", "--import-wpcr-json",
"--gen-weather-temperate", "--gen-weather-arctic",
"--gen-weather-desert", "--gen-weather-stormy",
"--gen-zone-atmosphere",

View file

@ -2317,6 +2317,10 @@ void printUsage(const char* argv0) {
std::printf(" Print WPCR entries (id / spellId / classFilter / actionKind / happiness / pet-required / stable-NPC required / cost copper / reagent / cast ms / name)\n");
std::printf(" --validate-wpcr <wpcr-base> [--json]\n");
std::printf(" Static checks: id+name+classFilter required, actionKind 0..10, no duplicate actionIds, per-kind constraints (Tame and Summon REQUIRE no active pet, requiresPet must be 0); warns on happinessRestore outside +/-25, Stable kind without requiresStableNPC, Tame kind without cooldown (canonically 15 sec)\n");
std::printf(" --export-wpcr-json <wpcr-base> [out.json]\n");
std::printf(" Export binary .wpcr to a human-editable JSON sidecar (defaults to <base>.wpcr.json; emits actionKind as both int AND name string; requiresPet/requiresStableNPC as bool)\n");
std::printf(" --import-wpcr-json <json-path> [out-base]\n");
std::printf(" Import a .wpcr.json sidecar back into binary .wpcr (actionKind int OR \"revive\"/\"mend\"/\"feed\"/\"dismiss\"/\"tame\"/\"beastlore\"/\"stable\"/\"untrain\"/\"rename\"/\"abandon\"/\"summon\"; bool fields accept bool OR int)\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(" --catalog-find <directory> <id> [--magic <WXXX>] [--json]\n");

View file

@ -148,6 +148,176 @@ int handleInfo(int& i, int argc, char** argv) {
return 0;
}
int parseActionKindToken(const std::string& s) {
using P = wowee::pipeline::WoweePetCare;
if (s == "revive") return P::Revive;
if (s == "mend") return P::Mend;
if (s == "feed") return P::Feed;
if (s == "dismiss") return P::Dismiss;
if (s == "tame") return P::Tame;
if (s == "beastlore") return P::BeastLore;
if (s == "stable") return P::Stable;
if (s == "untrain") return P::Untrain;
if (s == "rename") return P::Rename;
if (s == "abandon") return P::Abandon;
if (s == "summon") return P::Summon;
return -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 = stripWpcrExt(base);
if (out.empty()) out = base + ".wpcr.json";
if (!wowee::pipeline::WoweePetCareLoader::exists(base)) {
std::fprintf(stderr,
"export-wpcr-json: WPCR not found: %s.wpcr\n",
base.c_str());
return 1;
}
auto c = wowee::pipeline::WoweePetCareLoader::load(base);
nlohmann::json j;
j["magic"] = "WPCR";
j["version"] = 1;
j["name"] = c.name;
nlohmann::json arr = nlohmann::json::array();
for (const auto& e : c.entries) {
arr.push_back({
{"actionId", e.actionId},
{"name", e.name},
{"description", e.description},
{"spellId", e.spellId},
{"classFilter", e.classFilter},
{"actionKind", e.actionKind},
{"actionKindName", actionKindName(e.actionKind)},
{"happinessRestore", e.happinessRestore},
{"requiresPet", e.requiresPet != 0},
{"requiresStableNPC", e.requiresStableNPC != 0},
{"costCopper", e.costCopper},
{"reagentItemId", e.reagentItemId},
{"castTimeMs", e.castTimeMs},
{"cooldownSec", e.cooldownSec},
{"iconColorRGBA", e.iconColorRGBA},
});
}
j["entries"] = arr;
std::ofstream os(out);
if (!os) {
std::fprintf(stderr,
"export-wpcr-json: failed to open %s for write\n",
out.c_str());
return 1;
}
os << j.dump(2) << "\n";
std::printf("Wrote %s (%zu actions)\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) == ".wpcr.json") {
outBase.resize(outBase.size() - 10);
} else {
stripExt(outBase, ".json");
stripExt(outBase, ".wpcr");
}
}
std::ifstream is(in);
if (!is) {
std::fprintf(stderr,
"import-wpcr-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-wpcr-json: JSON parse error: %s\n", ex.what());
return 1;
}
wowee::pipeline::WoweePetCare c;
c.name = j.value("name", std::string{});
if (!j.contains("entries") || !j["entries"].is_array()) {
std::fprintf(stderr,
"import-wpcr-json: missing or non-array 'entries'\n");
return 1;
}
for (const auto& je : j["entries"]) {
wowee::pipeline::WoweePetCare::Entry e;
e.actionId = je.value("actionId", 0u);
e.name = je.value("name", std::string{});
e.description = je.value("description", std::string{});
e.spellId = je.value("spellId", 0u);
e.classFilter = je.value("classFilter", 0u);
if (je.contains("actionKind")) {
const auto& v = je["actionKind"];
if (v.is_string()) {
int parsed = parseActionKindToken(
v.get<std::string>());
if (parsed < 0) {
std::fprintf(stderr,
"import-wpcr-json: unknown "
"actionKind token '%s' on entry "
"id=%u\n",
v.get<std::string>().c_str(),
e.actionId);
return 1;
}
e.actionKind = static_cast<uint8_t>(parsed);
} else if (v.is_number_integer()) {
e.actionKind = static_cast<uint8_t>(v.get<int>());
}
} else if (je.contains("actionKindName") &&
je["actionKindName"].is_string()) {
int parsed = parseActionKindToken(
je["actionKindName"].get<std::string>());
if (parsed >= 0)
e.actionKind = static_cast<uint8_t>(parsed);
}
e.happinessRestore = static_cast<int8_t>(
je.value("happinessRestore", 0));
if (je.contains("requiresPet")) {
const auto& v = je["requiresPet"];
if (v.is_boolean())
e.requiresPet = v.get<bool>() ? 1 : 0;
else if (v.is_number_integer())
e.requiresPet = static_cast<uint8_t>(
v.get<int>() != 0 ? 1 : 0);
}
if (je.contains("requiresStableNPC")) {
const auto& v = je["requiresStableNPC"];
if (v.is_boolean())
e.requiresStableNPC = v.get<bool>() ? 1 : 0;
else if (v.is_number_integer())
e.requiresStableNPC = static_cast<uint8_t>(
v.get<int>() != 0 ? 1 : 0);
}
e.costCopper = je.value("costCopper", 0u);
e.reagentItemId = je.value("reagentItemId", 0u);
e.castTimeMs = je.value("castTimeMs", 0u);
e.cooldownSec = je.value("cooldownSec", 0u);
e.iconColorRGBA = je.value("iconColorRGBA", 0xFFFFFFFFu);
c.entries.push_back(e);
}
if (!wowee::pipeline::WoweePetCareLoader::save(c, outBase)) {
std::fprintf(stderr,
"import-wpcr-json: failed to save %s.wpcr\n",
outBase.c_str());
return 1;
}
std::printf("Wrote %s.wpcr (%zu actions)\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);
@ -281,6 +451,12 @@ bool handlePetCareCatalog(int& i, int argc, char** argv, int& outRc) {
if (std::strcmp(argv[i], "--validate-wpcr") == 0 && i + 1 < argc) {
outRc = handleValidate(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--export-wpcr-json") == 0 && i + 1 < argc) {
outRc = handleExportJson(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--import-wpcr-json") == 0 && i + 1 < argc) {
outRc = handleImportJson(i, argc, argv); return true;
}
return false;
}