feat(editor): add WSPC JSON round-trip (--export/--import-wspc-json)

Closes the editing loop on the spell-power-cost bucket catalog:
dump a .wspc to JSON, hand-edit baseCost / perLevelCost /
percentOfBase / powerType / costFlags (e.g. retune LowMana from
5% to 4%, add ScalesWithMastery to a class bucket, switch a
Whirlwind cost from 25 rage to 20), re-import to a byte-identical
binary.

The exporter emits both powerType (int 0..11) and the human-
readable powerTypeName ("mana" / "rage" / "focus" / "energy" /
"happiness" / "runic-power" / "runes" / "soul-shards" /
"holy-power" / "eclipse" / "health" / "no-cost"). costFlags is
emitted as both int bitfield AND pipe-separated label string.
The importer prefers the int form for costFlags when both are
present so unknown flag bits round-trip losslessly.

Verified byte-identical round-trip on all three presets
(starter / rage / mixed). CLI flag count 927 -> 929.
This commit is contained in:
Kelsi 2026-05-09 22:02:08 -07:00
parent 88effe39cd
commit 47d8892f74
3 changed files with 189 additions and 0 deletions

View file

@ -224,6 +224,7 @@ const char* const kArgRequired[] = {
"--export-wcef-json", "--import-wcef-json",
"--gen-spc", "--gen-spc-rage", "--gen-spc-mixed",
"--info-wspc", "--validate-wspc",
"--export-wspc-json", "--import-wspc-json",
"--gen-weather-temperate", "--gen-weather-arctic",
"--gen-weather-desert", "--gen-weather-stormy",
"--gen-zone-atmosphere",

View file

@ -1771,6 +1771,10 @@ void printUsage(const char* argv0) {
std::printf(" Print WSPC entries (id / powerType / baseCost / perLevelCost / percentOfBase / cost flags / name) — flags decoded as label list\n");
std::printf(" --validate-wspc <wspc-base> [--json]\n");
std::printf(" Static checks: id+name required, powerType 0..11, no duplicate ids; warns on percent outside [0,1], NoCost+nonzero, and non-NoCost types with no cost set (would cast for free)\n");
std::printf(" --export-wspc-json <wspc-base> [out.json]\n");
std::printf(" Export binary .wspc to a human-editable JSON sidecar (defaults to <base>.wspc.json)\n");
std::printf(" --import-wspc-json <json-path> [out-base]\n");
std::printf(" Import a .wspc.json sidecar back into binary .wspc (accepts powerType int OR name; costFlags int OR pipe-separated label string)\n");
std::printf(" --gen-weather-temperate <wow-base> [zoneName]\n");
std::printf(" Emit .wow weather schedule: clear-dominant + occasional rain + fog (forest / grassland)\n");
std::printf(" --gen-weather-arctic <wow-base> [zoneName]\n");

View file

@ -5,6 +5,7 @@
#include "pipeline/wowee_spell_power_costs.hpp"
#include <nlohmann/json.hpp>
#include <cctype>
#include <cstdint>
#include <cstdio>
#include <cstring>
@ -141,6 +142,183 @@ int handleInfo(int& i, int argc, char** argv) {
return 0;
}
int handleExportJson(int& i, int argc, char** argv) {
std::string base = argv[++i];
std::string outPath;
if (parseOptArg(i, argc, argv)) outPath = argv[++i];
base = stripWspcExt(base);
if (!wowee::pipeline::WoweeSpellPowerCostLoader::exists(base)) {
std::fprintf(stderr,
"export-wspc-json: WSPC not found: %s.wspc\n",
base.c_str());
return 1;
}
auto c = wowee::pipeline::WoweeSpellPowerCostLoader::load(base);
if (outPath.empty()) outPath = base + ".wspc.json";
nlohmann::json j;
j["catalog"] = c.name;
nlohmann::json arr = nlohmann::json::array();
for (const auto& e : c.entries) {
std::string flagNames;
appendCostFlagNames(e.costFlags, flagNames);
nlohmann::json je;
je["powerCostId"] = e.powerCostId;
je["name"] = e.name;
je["description"] = e.description;
je["powerType"] = e.powerType;
je["powerTypeName"] =
wowee::pipeline::WoweeSpellPowerCost::powerTypeName(e.powerType);
je["baseCost"] = e.baseCost;
je["perLevelCost"] = e.perLevelCost;
je["percentOfBase"] = e.percentOfBase;
je["costFlags"] = e.costFlags;
je["costFlagsLabels"] = flagNames;
je["iconColorRGBA"] = e.iconColorRGBA;
arr.push_back(je);
}
j["entries"] = arr;
std::ofstream os(outPath);
if (!os) {
std::fprintf(stderr,
"export-wspc-json: failed to open %s for write\n",
outPath.c_str());
return 1;
}
os << j.dump(2) << "\n";
std::printf("Wrote %s\n", outPath.c_str());
std::printf(" catalog : %s\n", c.name.c_str());
std::printf(" buckets : %zu\n", c.entries.size());
return 0;
}
uint8_t parsePowerTypeToken(const nlohmann::json& jv,
uint8_t fallback) {
if (jv.is_number_integer() || jv.is_number_unsigned()) {
int v = jv.get<int>();
if (v < 0 || v > wowee::pipeline::WoweeSpellPowerCost::NoCost)
return fallback;
return static_cast<uint8_t>(v);
}
if (jv.is_string()) {
std::string s = jv.get<std::string>();
for (auto& ch : s) ch = static_cast<char>(std::tolower(ch));
if (s == "mana") return wowee::pipeline::WoweeSpellPowerCost::Mana;
if (s == "rage") return wowee::pipeline::WoweeSpellPowerCost::Rage;
if (s == "focus") return wowee::pipeline::WoweeSpellPowerCost::Focus;
if (s == "energy") return wowee::pipeline::WoweeSpellPowerCost::Energy;
if (s == "happiness") return wowee::pipeline::WoweeSpellPowerCost::Happiness;
if (s == "runic-power" ||
s == "runicpower") return wowee::pipeline::WoweeSpellPowerCost::RunicPower;
if (s == "runes") return wowee::pipeline::WoweeSpellPowerCost::Runes;
if (s == "soul-shards" ||
s == "soulshards") return wowee::pipeline::WoweeSpellPowerCost::SoulShards;
if (s == "holy-power" ||
s == "holypower") return wowee::pipeline::WoweeSpellPowerCost::HolyPower;
if (s == "eclipse") return wowee::pipeline::WoweeSpellPowerCost::Eclipse;
if (s == "health") return wowee::pipeline::WoweeSpellPowerCost::Health;
if (s == "no-cost" ||
s == "nocost") return wowee::pipeline::WoweeSpellPowerCost::NoCost;
}
return fallback;
}
uint32_t parseCostFlagsField(const nlohmann::json& jv) {
using F = wowee::pipeline::WoweeSpellPowerCost;
if (jv.is_number_integer() || jv.is_number_unsigned())
return jv.get<uint32_t>();
if (jv.is_string()) {
std::string s = jv.get<std::string>();
uint32_t out = 0;
size_t pos = 0;
while (pos < s.size()) {
size_t end = s.find('|', pos);
if (end == std::string::npos) end = s.size();
std::string tok = s.substr(pos, end - pos);
for (auto& ch : tok) ch = static_cast<char>(std::tolower(ch));
if (tok == "requirescombatstance") out |= F::RequiresCombatStance;
else if (tok == "refundonmiss") out |= F::RefundOnMiss;
else if (tok == "doublesinform") out |= F::DoublesInForm;
else if (tok == "scaleswithmastery") out |= F::ScalesWithMastery;
pos = end + 1;
}
return out;
}
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];
std::ifstream is(jsonPath);
if (!is) {
std::fprintf(stderr,
"import-wspc-json: failed to open %s\n", jsonPath.c_str());
return 1;
}
nlohmann::json j;
try {
is >> j;
} catch (const std::exception& ex) {
std::fprintf(stderr,
"import-wspc-json: parse error in %s: %s\n",
jsonPath.c_str(), ex.what());
return 1;
}
wowee::pipeline::WoweeSpellPowerCost c;
if (j.contains("catalog") && j["catalog"].is_string())
c.name = j["catalog"].get<std::string>();
if (j.contains("entries") && j["entries"].is_array()) {
for (const auto& je : j["entries"]) {
wowee::pipeline::WoweeSpellPowerCost::Entry e;
if (je.contains("powerCostId")) e.powerCostId = je["powerCostId"].get<uint32_t>();
if (je.contains("name")) e.name = je["name"].get<std::string>();
if (je.contains("description")) e.description = je["description"].get<std::string>();
uint8_t type = wowee::pipeline::WoweeSpellPowerCost::Mana;
if (je.contains("powerType"))
type = parsePowerTypeToken(je["powerType"], type);
else if (je.contains("powerTypeName"))
type = parsePowerTypeToken(je["powerTypeName"], type);
e.powerType = type;
if (je.contains("baseCost")) e.baseCost = je["baseCost"].get<int32_t>();
if (je.contains("perLevelCost")) e.perLevelCost = je["perLevelCost"].get<int32_t>();
if (je.contains("percentOfBase")) e.percentOfBase = je["percentOfBase"].get<float>();
if (je.contains("costFlags"))
e.costFlags = parseCostFlagsField(je["costFlags"]);
else if (je.contains("costFlagsLabels"))
e.costFlags = parseCostFlagsField(je["costFlagsLabels"]);
if (je.contains("iconColorRGBA"))
e.iconColorRGBA = je["iconColorRGBA"].get<uint32_t>();
c.entries.push_back(e);
}
}
if (outBase.empty()) {
outBase = jsonPath;
const std::string suffix1 = ".wspc.json";
const std::string suffix2 = ".json";
if (outBase.size() >= suffix1.size() &&
outBase.compare(outBase.size() - suffix1.size(),
suffix1.size(), suffix1) == 0) {
outBase.resize(outBase.size() - suffix1.size());
} else if (outBase.size() >= suffix2.size() &&
outBase.compare(outBase.size() - suffix2.size(),
suffix2.size(), suffix2) == 0) {
outBase.resize(outBase.size() - suffix2.size());
}
}
outBase = stripWspcExt(outBase);
if (!wowee::pipeline::WoweeSpellPowerCostLoader::save(c, outBase)) {
std::fprintf(stderr,
"import-wspc-json: failed to save %s.wspc\n",
outBase.c_str());
return 1;
}
std::printf("Wrote %s.wspc\n", outBase.c_str());
std::printf(" catalog : %s\n", c.name.c_str());
std::printf(" buckets : %zu\n", c.entries.size());
return 0;
}
int handleValidate(int& i, int argc, char** argv) {
std::string base = argv[++i];
bool jsonOut = consumeJsonFlag(i, argc, argv);
@ -262,6 +440,12 @@ bool handleSpellPowerCostsCatalog(int& i, int argc, char** argv,
if (std::strcmp(argv[i], "--validate-wspc") == 0 && i + 1 < argc) {
outRc = handleValidate(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--export-wspc-json") == 0 && i + 1 < argc) {
outRc = handleExportJson(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--import-wspc-json") == 0 && i + 1 < argc) {
outRc = handleImportJson(i, argc, argv); return true;
}
return false;
}