mirror of
https://github.com/Kelsidavis/WoWee.git
synced 2026-05-11 03:23:51 +00:00
feat(editor): WPHM JSON round-trip closure
Adds --export-wphm-json / --import-wphm-json with the established
readEnumField template factoring int+name dual encoding for the
movementState enum ("idle"/"walk"/"run"/"swim"/"fly"/"sit"/
"mount"/"death").
Both encoding paths verified live: stripped the int field from
exported JSON and re-imported using only the string token —
result byte-identical to the int-keyed form. All 3 presets
(human / orc / undead) byte-identical binary roundtrip OK
preserving Undead shambling variant 38 on Run state and slower
swim transition (400ms vs 350ms for living races).
CLI flag count 1344 -> 1346.
This commit is contained in:
parent
949a6e0182
commit
2a28d3c1cd
3 changed files with 173 additions and 0 deletions
|
|
@ -391,6 +391,7 @@ const char* const kArgRequired[] = {
|
|||
"--export-wspk-json", "--import-wspk-json",
|
||||
"--gen-phm-human", "--gen-phm-orc", "--gen-phm-undead",
|
||||
"--info-wphm", "--validate-wphm",
|
||||
"--export-wphm-json", "--import-wphm-json",
|
||||
"--gen-weather-temperate", "--gen-weather-arctic",
|
||||
"--gen-weather-desert", "--gen-weather-stormy",
|
||||
"--gen-zone-atmosphere",
|
||||
|
|
|
|||
|
|
@ -2541,6 +2541,10 @@ void printUsage(const char* argv0) {
|
|||
std::printf(" Print WPHM bindings (id / race / gender / movementState / baseAnimId / variantAnimId / transitionMs)\n");
|
||||
std::printf(" --validate-wphm <wphm-base> [--json]\n");
|
||||
std::printf(" Static checks: id required, raceId 1..10, genderId 0..1, movementState 0..7, no duplicate mapIds, no duplicate (race,gender,state) triples (renderer dispatch ambiguity), baseAnimId=0 forbidden on non-Idle states (would freeze model); warns on variantAnimId==baseAnimId no-op and transitionMs > 2000 animation hang\n");
|
||||
std::printf(" --export-wphm-json <wphm-base> [out.json]\n");
|
||||
std::printf(" Export binary .wphm to a human-editable JSON sidecar (defaults to <base>.wphm.json; emits movementState as int + name string)\n");
|
||||
std::printf(" --import-wphm-json <json-path> [out-base]\n");
|
||||
std::printf(" Import a .wphm.json sidecar back into binary .wphm (movementState int OR \"idle\"/\"walk\"/\"run\"/\"swim\"/\"fly\"/\"sit\"/\"mount\"/\"death\")\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");
|
||||
|
|
|
|||
|
|
@ -165,6 +165,57 @@ int handleInfo(int& i, int argc, char** argv) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int parseMovementStateToken(const std::string& s) {
|
||||
using P = wowee::pipeline::WoweePlayerMovementAnim;
|
||||
if (s == "idle") return P::StateIdle;
|
||||
if (s == "walk") return P::StateWalk;
|
||||
if (s == "run") return P::StateRun;
|
||||
if (s == "swim") return P::StateSwim;
|
||||
if (s == "fly") return P::StateFly;
|
||||
if (s == "sit") return P::StateSit;
|
||||
if (s == "mount") return P::StateMount;
|
||||
if (s == "death") return P::StateDeath;
|
||||
return -1;
|
||||
}
|
||||
|
||||
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-wphm-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 handleValidate(int& i, int argc, char** argv) {
|
||||
std::string base = argv[++i];
|
||||
bool jsonOut = consumeJsonFlag(i, argc, argv);
|
||||
|
|
@ -288,6 +339,115 @@ int handleValidate(int& i, int argc, char** argv) {
|
|||
return ok ? 0 : 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 = stripWphmExt(base);
|
||||
if (out.empty()) out = base + ".wphm.json";
|
||||
if (!wowee::pipeline::WoweePlayerMovementAnimLoader::exists(base)) {
|
||||
std::fprintf(stderr,
|
||||
"export-wphm-json: WPHM not found: %s.wphm\n",
|
||||
base.c_str());
|
||||
return 1;
|
||||
}
|
||||
auto c = wowee::pipeline::WoweePlayerMovementAnimLoader::load(base);
|
||||
nlohmann::json j;
|
||||
j["magic"] = "WPHM";
|
||||
j["version"] = 1;
|
||||
j["name"] = c.name;
|
||||
nlohmann::json arr = nlohmann::json::array();
|
||||
for (const auto& e : c.entries) {
|
||||
arr.push_back({
|
||||
{"mapId", e.mapId},
|
||||
{"raceId", e.raceId},
|
||||
{"raceName", raceIdName(e.raceId)},
|
||||
{"genderId", e.genderId},
|
||||
{"genderName", genderName(e.genderId)},
|
||||
{"movementState", e.movementState},
|
||||
{"movementStateName",
|
||||
movementStateName(e.movementState)},
|
||||
{"baseAnimId", e.baseAnimId},
|
||||
{"variantAnimId", e.variantAnimId},
|
||||
{"transitionMs", e.transitionMs},
|
||||
});
|
||||
}
|
||||
j["entries"] = arr;
|
||||
std::ofstream os(out);
|
||||
if (!os) {
|
||||
std::fprintf(stderr,
|
||||
"export-wphm-json: failed to open %s for write\n",
|
||||
out.c_str());
|
||||
return 1;
|
||||
}
|
||||
os << j.dump(2) << "\n";
|
||||
std::printf("Wrote %s (%zu bindings)\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) == ".wphm.json") {
|
||||
outBase.resize(outBase.size() - 10);
|
||||
} else {
|
||||
stripExt(outBase, ".json");
|
||||
stripExt(outBase, ".wphm");
|
||||
}
|
||||
}
|
||||
std::ifstream is(in);
|
||||
if (!is) {
|
||||
std::fprintf(stderr,
|
||||
"import-wphm-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-wphm-json: JSON parse error: %s\n", ex.what());
|
||||
return 1;
|
||||
}
|
||||
wowee::pipeline::WoweePlayerMovementAnim c;
|
||||
c.name = j.value("name", std::string{});
|
||||
if (!j.contains("entries") || !j["entries"].is_array()) {
|
||||
std::fprintf(stderr,
|
||||
"import-wphm-json: missing or non-array 'entries'\n");
|
||||
return 1;
|
||||
}
|
||||
for (const auto& je : j["entries"]) {
|
||||
wowee::pipeline::WoweePlayerMovementAnim::Entry e;
|
||||
e.mapId = je.value("mapId", 0u);
|
||||
e.raceId = static_cast<uint8_t>(je.value("raceId", 0));
|
||||
e.genderId = static_cast<uint8_t>(je.value("genderId", 0));
|
||||
if (!readEnumField(je, "movementState",
|
||||
"movementStateName",
|
||||
parseMovementStateToken,
|
||||
"movementState",
|
||||
e.mapId, e.movementState)) return 1;
|
||||
e.baseAnimId = je.value("baseAnimId", 0u);
|
||||
e.variantAnimId = je.value("variantAnimId", 0u);
|
||||
e.transitionMs = static_cast<uint16_t>(
|
||||
je.value("transitionMs", 0));
|
||||
c.entries.push_back(e);
|
||||
}
|
||||
if (!wowee::pipeline::WoweePlayerMovementAnimLoader::save(c, outBase)) {
|
||||
std::fprintf(stderr,
|
||||
"import-wphm-json: failed to save %s.wphm\n",
|
||||
outBase.c_str());
|
||||
return 1;
|
||||
}
|
||||
std::printf("Wrote %s.wphm (%zu bindings)\n",
|
||||
outBase.c_str(), c.entries.size());
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool handlePlayerMovementAnimCatalog(int& i, int argc, char** argv,
|
||||
|
|
@ -311,6 +471,14 @@ bool handlePlayerMovementAnimCatalog(int& i, int argc, char** argv,
|
|||
i + 1 < argc) {
|
||||
outRc = handleValidate(i, argc, argv); return true;
|
||||
}
|
||||
if (std::strcmp(argv[i], "--export-wphm-json") == 0 &&
|
||||
i + 1 < argc) {
|
||||
outRc = handleExportJson(i, argc, argv); return true;
|
||||
}
|
||||
if (std::strcmp(argv[i], "--import-wphm-json") == 0 &&
|
||||
i + 1 < argc) {
|
||||
outRc = handleImportJson(i, argc, argv); return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue