From 2a28d3c1cd42a374a8272fe270fc6a5cd5fc987d Mon Sep 17 00:00:00 2001 From: Kelsi Date: Sun, 10 May 2026 03:47:03 -0700 Subject: [PATCH] feat(editor): WPHM JSON round-trip closure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- tools/editor/cli_arg_required.cpp | 1 + tools/editor/cli_help.cpp | 4 + .../cli_player_movement_anim_catalog.cpp | 168 ++++++++++++++++++ 3 files changed, 173 insertions(+) diff --git a/tools/editor/cli_arg_required.cpp b/tools/editor/cli_arg_required.cpp index 31c28a8b..6b0d1b7c 100644 --- a/tools/editor/cli_arg_required.cpp +++ b/tools/editor/cli_arg_required.cpp @@ -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", diff --git a/tools/editor/cli_help.cpp b/tools/editor/cli_help.cpp index ac4dab20..d93b75d1 100644 --- a/tools/editor/cli_help.cpp +++ b/tools/editor/cli_help.cpp @@ -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 [--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 [out.json]\n"); + std::printf(" Export binary .wphm to a human-editable JSON sidecar (defaults to .wphm.json; emits movementState as int + name string)\n"); + std::printf(" --import-wphm-json [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 [--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 [--magic ] [--json]\n"); diff --git a/tools/editor/cli_player_movement_anim_catalog.cpp b/tools/editor/cli_player_movement_anim_catalog.cpp index fc6f7ea4..9318ea61 100644 --- a/tools/editor/cli_player_movement_anim_catalog.cpp +++ b/tools/editor/cli_player_movement_anim_catalog.cpp @@ -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 +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()); + if (parsed < 0) { + std::fprintf(stderr, + "import-wphm-json: unknown %s token " + "'%s' on entry id=%u\n", + label, v.get().c_str(), + entryId); + return false; + } + outValue = static_cast(parsed); + return true; + } + if (v.is_number_integer()) { + outValue = static_cast(v.get()); + return true; + } + } + if (je.contains(nameKey) && je[nameKey].is_string()) { + int parsed = parseFn(je[nameKey].get()); + if (parsed >= 0) { + outValue = static_cast(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(je.value("raceId", 0)); + e.genderId = static_cast(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( + 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; }