feat(editor): WCAM JSON round-trip closure

Adds --export-wcam-json / --import-wcam-json with the established
readEnumField template factoring int+name dual encoding for
purposeKind ("cinematic"/"combat"/"mounted"/"vehicle"/
"cutscene"/"photomode"). Float fields (FOV, distance, pitch,
yaw, shoulder offset) preserved bit-for-bit through JSON.

All 3 presets (combat/mounted/cinematic) byte-identical binary
roundtrip OK including the Cinematic Portrait preset's offbeat
yaw=15deg + 35deg telephoto + head-bone tracking combination.

Live-tested gimbal-lock validator: hand-mutated Cinematic
Establishing preset pitch from -30 to -95 (beyond the -89
gimbal-lock limit). Validator correctly errored:
"pitchDegrees=-95.000000 gimbal-locks the camera (must be
within (-89, +89))". Catches the class of cinematic-camera bugs
where a pitch of ±90 mathematically aligns with the world up
vector and causes the camera basis to collapse.

CLI flag count 1497 -> 1499.
This commit is contained in:
Kelsi 2026-05-10 05:57:35 -07:00
parent ab69171ad9
commit a4dd71fc90
3 changed files with 179 additions and 0 deletions

View file

@ -442,6 +442,7 @@ const char* const kArgRequired[] = {
"--export-wcmd-json", "--import-wcmd-json",
"--gen-cam-combat", "--gen-cam-mounted", "--gen-cam-cinematic",
"--info-wcam", "--validate-wcam",
"--export-wcam-json", "--import-wcam-json",
"--gen-weather-temperate", "--gen-weather-arctic",
"--gen-weather-desert", "--gen-weather-stormy",
"--gen-zone-atmosphere",

View file

@ -147,6 +147,55 @@ int handleInfo(int& i, int argc, char** argv) {
return 0;
}
int parsePurposeKindToken(const std::string& s) {
using C = wowee::pipeline::WoweeCameraPresets;
if (s == "cinematic") return C::Cinematic;
if (s == "combat") return C::Combat;
if (s == "mounted") return C::Mounted;
if (s == "vehicle") return C::Vehicle;
if (s == "cutscene") return C::Cutscene;
if (s == "photomode") return C::PhotoMode;
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-wcam-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);
@ -270,6 +319,123 @@ 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 = stripWcamExt(base);
if (out.empty()) out = base + ".wcam.json";
if (!wowee::pipeline::WoweeCameraPresetsLoader::exists(base)) {
std::fprintf(stderr,
"export-wcam-json: WCAM not found: %s.wcam\n",
base.c_str());
return 1;
}
auto c = wowee::pipeline::WoweeCameraPresetsLoader::load(base);
nlohmann::json j;
j["magic"] = "WCAM";
j["version"] = 1;
j["name"] = c.name;
nlohmann::json arr = nlohmann::json::array();
for (const auto& e : c.entries) {
arr.push_back({
{"presetId", e.presetId},
{"name", e.name},
{"purposeKind", e.purposeKind},
{"purposeKindName",
purposeKindName(e.purposeKind)},
{"motionDamping", e.motionDamping},
{"fovDegrees", e.fovDegrees},
{"distanceFromTarget", e.distanceFromTarget},
{"pitchDegrees", e.pitchDegrees},
{"yawOffsetDegrees", e.yawOffsetDegrees},
{"shoulderOffsetMeters",
e.shoulderOffsetMeters},
{"focusBoneId", e.focusBoneId},
});
}
j["entries"] = arr;
std::ofstream os(out);
if (!os) {
std::fprintf(stderr,
"export-wcam-json: failed to open %s for write\n",
out.c_str());
return 1;
}
os << j.dump(2) << "\n";
std::printf("Wrote %s (%zu presets)\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) == ".wcam.json") {
outBase.resize(outBase.size() - 10);
} else {
stripExt(outBase, ".json");
stripExt(outBase, ".wcam");
}
}
std::ifstream is(in);
if (!is) {
std::fprintf(stderr,
"import-wcam-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-wcam-json: JSON parse error: %s\n", ex.what());
return 1;
}
wowee::pipeline::WoweeCameraPresets c;
c.name = j.value("name", std::string{});
if (!j.contains("entries") || !j["entries"].is_array()) {
std::fprintf(stderr,
"import-wcam-json: missing or non-array 'entries'\n");
return 1;
}
for (const auto& je : j["entries"]) {
wowee::pipeline::WoweeCameraPresets::Entry e;
e.presetId = je.value("presetId", 0u);
e.name = je.value("name", std::string{});
if (!readEnumField(je, "purposeKind",
"purposeKindName",
parsePurposeKindToken,
"purposeKind", e.presetId,
e.purposeKind)) return 1;
e.motionDamping = static_cast<uint8_t>(
je.value("motionDamping", 0));
e.fovDegrees = je.value("fovDegrees", 0.f);
e.distanceFromTarget =
je.value("distanceFromTarget", 0.f);
e.pitchDegrees = je.value("pitchDegrees", 0.f);
e.yawOffsetDegrees =
je.value("yawOffsetDegrees", 0.f);
e.shoulderOffsetMeters =
je.value("shoulderOffsetMeters", 0.f);
e.focusBoneId = je.value("focusBoneId", 0u);
c.entries.push_back(e);
}
if (!wowee::pipeline::WoweeCameraPresetsLoader::save(c, outBase)) {
std::fprintf(stderr,
"import-wcam-json: failed to save %s.wcam\n",
outBase.c_str());
return 1;
}
std::printf("Wrote %s.wcam (%zu presets)\n",
outBase.c_str(), c.entries.size());
return 0;
}
} // namespace
bool handleCameraPresetsCatalog(int& i, int argc, char** argv,
@ -293,6 +459,14 @@ bool handleCameraPresetsCatalog(int& i, int argc, char** argv,
i + 1 < argc) {
outRc = handleValidate(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--export-wcam-json") == 0 &&
i + 1 < argc) {
outRc = handleExportJson(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--import-wcam-json") == 0 &&
i + 1 < argc) {
outRc = handleImportJson(i, argc, argv); return true;
}
return false;
}

View file

@ -2779,6 +2779,10 @@ void printUsage(const char* argv0) {
std::printf(" Print WCAM entries (id / purpose / damping / FOV / distance / pitch / yaw / shoulder / focusBone / name)\n");
std::printf(" --validate-wcam <wcam-base> [--json]\n");
std::printf(" Static checks: id+name required, purposeKind 0..5, no duplicate presetIds, FOV in (0,180) (zero/negative makes no sense, >=180 inverts the view frustum), distanceFromTarget >= 0 (negative places camera in front of target); CRITICAL: pitch within (-89,+89) — beyond gimbal-locks the camera. Warns on FOV outside 30..120 player-comfort range (motion-sickness risk), distanceFromTarget < 0.5m (clips into model), and yawOffsetDegrees beyond ±180 (wraps to smaller equivalent — simplify)\n");
std::printf(" --export-wcam-json <wcam-base> [out.json]\n");
std::printf(" Export binary .wcam to a human-editable JSON sidecar (defaults to <base>.wcam.json; emits purposeKind as int + name string; floats preserved bit-for-bit)\n");
std::printf(" --import-wcam-json <json-path> [out-base]\n");
std::printf(" Import a .wcam.json sidecar back into binary .wcam (purposeKind int OR \"cinematic\"/\"combat\"/\"mounted\"/\"vehicle\"/\"cutscene\"/\"photomode\" — round-trips FOV+distance+pitch+yaw+shoulder floats byte-identical)\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");