feat(editor): add WPET JSON round-trip authoring workflow

Closes the WPET open-format loop with --export-wpet-json /
--import-wpet-json, mirroring the JSON pairs added for
every other novel binary format. All 32 binary formats
added since WOL now have full JSON round-trip authoring.

Two top-level arrays mirror the binary layout:
  • families[] — familyId / name / description / icon /
                  petType (dual int + name) /
                  baseAttackSpeed / damageMultiplier /
                  armorMultiplier / dietMask + name /
                  abilities[]
  • minions[]  — minionId / name / summonSpellId /
                  creatureId / abilities[]

Verified byte-identical round-trip on the starter preset
(2 hunter families with 3 abilities each + 1 warlock Imp
with 2 abilities including autocast preferences preserved
through the JSON layer).

Adds 2 flags (672 documented total now).
This commit is contained in:
Kelsi 2026-05-09 18:37:12 -07:00
parent ab72380ee0
commit 13f09b8cb7
3 changed files with 188 additions and 0 deletions

View file

@ -166,6 +166,183 @@ int handleInfo(int& i, int argc, char** argv) {
return 0;
}
int handleExportJson(int& i, int argc, char** argv) {
// Mirrors the JSON pairs added for every other novel
// open format. Two top-level arrays (families / minions)
// mirror the binary layout. petType + dietMask emit dual
// int + name forms.
std::string base = argv[++i];
std::string outPath;
if (parseOptArg(i, argc, argv)) outPath = argv[++i];
base = stripWpetExt(base);
if (outPath.empty()) outPath = base + ".wpet.json";
if (!wowee::pipeline::WoweePetLoader::exists(base)) {
std::fprintf(stderr,
"export-wpet-json: WPET not found: %s.wpet\n", base.c_str());
return 1;
}
auto c = wowee::pipeline::WoweePetLoader::load(base);
nlohmann::json j;
j["name"] = c.name;
nlohmann::json fa = nlohmann::json::array();
for (const auto& f : c.families) {
nlohmann::json jf;
jf["familyId"] = f.familyId;
jf["name"] = f.name;
jf["description"] = f.description;
jf["iconPath"] = f.iconPath;
jf["petType"] = f.petType;
jf["petTypeName"] = wowee::pipeline::WoweePet::petTypeName(f.petType);
jf["baseAttackSpeed"] = f.baseAttackSpeed;
jf["damageMultiplier"] = f.damageMultiplier;
jf["armorMultiplier"] = f.armorMultiplier;
jf["dietMask"] = f.dietMask;
jf["dietMaskName"] = wowee::pipeline::WoweePet::dietMaskName(f.dietMask);
nlohmann::json abs = nlohmann::json::array();
for (const auto& a : f.abilities) {
abs.push_back({
{"spellId", a.spellId},
{"learnedAtLevel", a.learnedAtLevel},
{"rank", a.rank},
});
}
jf["abilities"] = abs;
fa.push_back(jf);
}
j["families"] = fa;
nlohmann::json ma = nlohmann::json::array();
for (const auto& m : c.minions) {
nlohmann::json jm;
jm["minionId"] = m.minionId;
jm["name"] = m.name;
jm["summonSpellId"] = m.summonSpellId;
jm["creatureId"] = m.creatureId;
nlohmann::json abs = nlohmann::json::array();
for (const auto& a : m.abilities) {
abs.push_back({
{"spellId", a.spellId},
{"rank", a.rank},
{"autocastDefault", a.autocastDefault},
});
}
jm["abilities"] = abs;
ma.push_back(jm);
}
j["minions"] = ma;
std::ofstream out(outPath);
if (!out) {
std::fprintf(stderr,
"export-wpet-json: cannot write %s\n", outPath.c_str());
return 1;
}
out << j.dump(2) << "\n";
out.close();
std::printf("Wrote %s\n", outPath.c_str());
std::printf(" source : %s.wpet\n", base.c_str());
std::printf(" families / minions: %zu / %zu\n",
c.families.size(), c.minions.size());
return 0;
}
int handleImportJson(int& i, int argc, char** argv) {
std::string jsonPath = argv[++i];
std::string outBase;
if (parseOptArg(i, argc, argv)) outBase = argv[++i];
if (outBase.empty()) {
outBase = jsonPath;
std::string suffix = ".wpet.json";
if (outBase.size() > suffix.size() &&
outBase.substr(outBase.size() - suffix.size()) == suffix) {
outBase = outBase.substr(0, outBase.size() - suffix.size());
} else if (outBase.size() > 5 &&
outBase.substr(outBase.size() - 5) == ".json") {
outBase = outBase.substr(0, outBase.size() - 5);
}
}
outBase = stripWpetExt(outBase);
std::ifstream in(jsonPath);
if (!in) {
std::fprintf(stderr,
"import-wpet-json: cannot read %s\n", jsonPath.c_str());
return 1;
}
nlohmann::json j;
try { in >> j; }
catch (const std::exception& e) {
std::fprintf(stderr,
"import-wpet-json: bad JSON in %s: %s\n",
jsonPath.c_str(), e.what());
return 1;
}
auto petTypeFromName = [](const std::string& s) -> uint8_t {
if (s == "cunning") return wowee::pipeline::WoweePet::Cunning;
if (s == "ferocity") return wowee::pipeline::WoweePet::Ferocity;
if (s == "tenacity") return wowee::pipeline::WoweePet::Tenacity;
return wowee::pipeline::WoweePet::Cunning;
};
wowee::pipeline::WoweePet c;
c.name = j.value("name", std::string{});
if (j.contains("families") && j["families"].is_array()) {
for (const auto& jf : j["families"]) {
wowee::pipeline::WoweePet::Family f;
f.familyId = jf.value("familyId", 0u);
f.name = jf.value("name", std::string{});
f.description = jf.value("description", std::string{});
f.iconPath = jf.value("iconPath", std::string{});
if (jf.contains("petType") && jf["petType"].is_number_integer()) {
f.petType = static_cast<uint8_t>(jf["petType"].get<int>());
} else if (jf.contains("petTypeName") && jf["petTypeName"].is_string()) {
f.petType = petTypeFromName(jf["petTypeName"].get<std::string>());
}
f.baseAttackSpeed = jf.value("baseAttackSpeed", 2.0f);
f.damageMultiplier = jf.value("damageMultiplier", 1.0f);
f.armorMultiplier = jf.value("armorMultiplier", 1.0f);
f.dietMask = jf.value("dietMask", 0u);
if (jf.contains("abilities") && jf["abilities"].is_array()) {
for (const auto& ja : jf["abilities"]) {
wowee::pipeline::WoweePet::FamilyAbility a;
a.spellId = ja.value("spellId", 0u);
a.learnedAtLevel = static_cast<uint16_t>(
ja.value("learnedAtLevel", 1));
a.rank = static_cast<uint8_t>(ja.value("rank", 1));
f.abilities.push_back(a);
}
}
c.families.push_back(std::move(f));
}
}
if (j.contains("minions") && j["minions"].is_array()) {
for (const auto& jm : j["minions"]) {
wowee::pipeline::WoweePet::Minion m;
m.minionId = jm.value("minionId", 0u);
m.name = jm.value("name", std::string{});
m.summonSpellId = jm.value("summonSpellId", 0u);
m.creatureId = jm.value("creatureId", 0u);
if (jm.contains("abilities") && jm["abilities"].is_array()) {
for (const auto& ja : jm["abilities"]) {
wowee::pipeline::WoweePet::MinionAbility a;
a.spellId = ja.value("spellId", 0u);
a.rank = static_cast<uint8_t>(ja.value("rank", 1));
a.autocastDefault = static_cast<uint8_t>(
ja.value("autocastDefault", 0));
m.abilities.push_back(a);
}
}
c.minions.push_back(std::move(m));
}
}
if (!wowee::pipeline::WoweePetLoader::save(c, outBase)) {
std::fprintf(stderr,
"import-wpet-json: failed to save %s.wpet\n", outBase.c_str());
return 1;
}
std::printf("Wrote %s.wpet\n", outBase.c_str());
std::printf(" source : %s\n", jsonPath.c_str());
std::printf(" families / minions: %zu / %zu\n",
c.families.size(), c.minions.size());
return 0;
}
int handleValidate(int& i, int argc, char** argv) {
std::string base = argv[++i];
bool jsonOut = consumeJsonFlag(i, argc, argv);
@ -280,6 +457,12 @@ bool handlePetsCatalog(int& i, int argc, char** argv, int& outRc) {
if (std::strcmp(argv[i], "--validate-wpet") == 0 && i + 1 < argc) {
outRc = handleValidate(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--export-wpet-json") == 0 && i + 1 < argc) {
outRc = handleExportJson(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--import-wpet-json") == 0 && i + 1 < argc) {
outRc = handleImportJson(i, argc, argv); return true;
}
return false;
}