feat(editor): add WCMR JSON round-trip (--export/--import-wcmr-json)

Closes the editing loop on the creature-patrol catalog: dump a
.wcmr to JSON, hand-edit pathKind / moveType / waypoint coords /
delays (e.g. add a new mid-route waypoint to a city guard's
patrol, change Stormwind cathedral guards from Walk to Run kind,
extend an ICC patrol from 16 to 24 waypoints to cover a wider
area), re-import to a byte-identical binary.

First round-trip with truly variable-length payloads. The
waypoint arrays serialize as JSON arrays of {x, y, z, delayMs}
objects — adding or removing waypoints in the JSON sidecar
preserves the length-prefixed binary layout on import. The
generic --bulk-export-json / --bulk-import-json utilities work
on these too without modification, since the sidecar follows
the same magic-pattern naming convention.

Two dual-encoded fields:
  - pathKind: int 0..3 OR "loop" / "one-shot" / "reverse" /
    "random"
  - moveType: int 0..3 OR "walk" / "run" / "fly" / "swim"

Verified byte-identical round-trip on all three presets
(patrol / city / boss). The boss preset's 12-pt + 8-pt + 16-pt
patrols (36 waypoints total = 576 bytes of waypoint payload)
round-trip exactly, confirming the variable-length encoding
preserves byte-for-byte order in both directions. CLI flag count
1053 -> 1055.
This commit is contained in:
Kelsi 2026-05-09 23:41:17 -07:00
parent 7d3b80e1f7
commit 5a12f5d183
3 changed files with 186 additions and 0 deletions

View file

@ -278,6 +278,7 @@ const char* const kArgRequired[] = {
"--export-wsps-json", "--import-wsps-json",
"--gen-cmr", "--gen-cmr-city", "--gen-cmr-boss",
"--info-wcmr", "--validate-wcmr",
"--export-wcmr-json", "--import-wcmr-json",
"--gen-weather-temperate", "--gen-weather-arctic",
"--gen-weather-desert", "--gen-weather-stormy",
"--gen-zone-atmosphere",

View file

@ -5,6 +5,7 @@
#include "pipeline/wowee_creature_patrols.hpp"
#include <nlohmann/json.hpp>
#include <cctype>
#include <cstdint>
#include <cstdio>
#include <cstring>
@ -135,6 +136,180 @@ 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 = stripWcmrExt(base);
if (!wowee::pipeline::WoweeCreaturePatrolLoader::exists(base)) {
std::fprintf(stderr,
"export-wcmr-json: WCMR not found: %s.wcmr\n",
base.c_str());
return 1;
}
auto c = wowee::pipeline::WoweeCreaturePatrolLoader::load(base);
if (outPath.empty()) outPath = base + ".wcmr.json";
nlohmann::json j;
j["catalog"] = c.name;
nlohmann::json arr = nlohmann::json::array();
for (const auto& e : c.entries) {
nlohmann::json wpArr = nlohmann::json::array();
for (const auto& w : e.waypoints) {
wpArr.push_back({
{"x", w.x}, {"y", w.y}, {"z", w.z},
{"delayMs", w.delayMs},
});
}
nlohmann::json je;
je["pathId"] = e.pathId;
je["name"] = e.name;
je["description"] = e.description;
je["creatureGuid"] = e.creatureGuid;
je["pathKind"] = e.pathKind;
je["pathKindName"] =
wowee::pipeline::WoweeCreaturePatrol::pathKindName(e.pathKind);
je["moveType"] = e.moveType;
je["moveTypeName"] =
wowee::pipeline::WoweeCreaturePatrol::moveTypeName(e.moveType);
je["waypoints"] = wpArr;
je["iconColorRGBA"] = e.iconColorRGBA;
arr.push_back(je);
}
j["entries"] = arr;
std::ofstream os(outPath);
if (!os) {
std::fprintf(stderr,
"export-wcmr-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(" paths : %zu\n", c.entries.size());
return 0;
}
uint8_t parsePathKindToken(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::WoweeCreaturePatrol::Random)
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 == "loop") return wowee::pipeline::WoweeCreaturePatrol::Loop;
if (s == "one-shot" ||
s == "oneshot") return wowee::pipeline::WoweeCreaturePatrol::OneShot;
if (s == "reverse") return wowee::pipeline::WoweeCreaturePatrol::Reverse;
if (s == "random") return wowee::pipeline::WoweeCreaturePatrol::Random;
}
return fallback;
}
uint8_t parseMoveTypeToken(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::WoweeCreaturePatrol::Swim)
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 == "walk") return wowee::pipeline::WoweeCreaturePatrol::Walk;
if (s == "run") return wowee::pipeline::WoweeCreaturePatrol::Run;
if (s == "fly") return wowee::pipeline::WoweeCreaturePatrol::Fly;
if (s == "swim") return wowee::pipeline::WoweeCreaturePatrol::Swim;
}
return fallback;
}
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-wcmr-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-wcmr-json: parse error in %s: %s\n",
jsonPath.c_str(), ex.what());
return 1;
}
wowee::pipeline::WoweeCreaturePatrol 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::WoweeCreaturePatrol::Entry e;
if (je.contains("pathId")) e.pathId = je["pathId"].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>();
if (je.contains("creatureGuid")) e.creatureGuid = je["creatureGuid"].get<uint32_t>();
uint8_t kind = wowee::pipeline::WoweeCreaturePatrol::Loop;
if (je.contains("pathKind"))
kind = parsePathKindToken(je["pathKind"], kind);
else if (je.contains("pathKindName"))
kind = parsePathKindToken(je["pathKindName"], kind);
e.pathKind = kind;
uint8_t move = wowee::pipeline::WoweeCreaturePatrol::Walk;
if (je.contains("moveType"))
move = parseMoveTypeToken(je["moveType"], move);
else if (je.contains("moveTypeName"))
move = parseMoveTypeToken(je["moveTypeName"], move);
e.moveType = move;
if (je.contains("waypoints") && je["waypoints"].is_array()) {
for (const auto& wj : je["waypoints"]) {
wowee::pipeline::WoweeCreaturePatrol::Waypoint w;
if (wj.contains("x")) w.x = wj["x"].get<float>();
if (wj.contains("y")) w.y = wj["y"].get<float>();
if (wj.contains("z")) w.z = wj["z"].get<float>();
if (wj.contains("delayMs")) w.delayMs = wj["delayMs"].get<uint32_t>();
e.waypoints.push_back(w);
}
}
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 = ".wcmr.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 = stripWcmrExt(outBase);
if (!wowee::pipeline::WoweeCreaturePatrolLoader::save(c, outBase)) {
std::fprintf(stderr,
"import-wcmr-json: failed to save %s.wcmr\n",
outBase.c_str());
return 1;
}
std::printf("Wrote %s.wcmr\n", outBase.c_str());
std::printf(" catalog : %s\n", c.name.c_str());
std::printf(" paths : %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);
@ -243,6 +418,12 @@ bool handleCreaturePatrolsCatalog(int& i, int argc, char** argv,
if (std::strcmp(argv[i], "--validate-wcmr") == 0 && i + 1 < argc) {
outRc = handleValidate(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--export-wcmr-json") == 0 && i + 1 < argc) {
outRc = handleExportJson(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--import-wcmr-json") == 0 && i + 1 < argc) {
outRc = handleImportJson(i, argc, argv); return true;
}
return false;
}

View file

@ -2023,6 +2023,10 @@ void printUsage(const char* argv0) {
std::printf(" Print WCMR entries (id / creatureGuid / pathKind / moveType / waypoint count / total path length yards / name)\n");
std::printf(" --validate-wcmr <wcmr-base> [--json]\n");
std::printf(" Static checks: id+name+creatureGuid+waypoints required, pathKind 0..3, moveType 0..3, no duplicate ids; warns on 1-waypoint paths (idle), Loop with <3 waypoints (degenerate)\n");
std::printf(" --export-wcmr-json <wcmr-base> [out.json]\n");
std::printf(" Export binary .wcmr to a human-editable JSON sidecar (defaults to <base>.wcmr.json). Waypoints exported as JSON arrays for variable-length editing\n");
std::printf(" --import-wcmr-json <json-path> [out-base]\n");
std::printf(" Import a .wcmr.json sidecar back into binary .wcmr (accepts pathKind/moveType int OR name; waypoint arrays length-preserving)\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");