From a4dd71fc90b2666f3047b5134ee5f254ae9dcad0 Mon Sep 17 00:00:00 2001 From: Kelsi Date: Sun, 10 May 2026 05:57:35 -0700 Subject: [PATCH] feat(editor): WCAM JSON round-trip closure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- tools/editor/cli_arg_required.cpp | 1 + tools/editor/cli_camera_presets_catalog.cpp | 174 ++++++++++++++++++++ tools/editor/cli_help.cpp | 4 + 3 files changed, 179 insertions(+) diff --git a/tools/editor/cli_arg_required.cpp b/tools/editor/cli_arg_required.cpp index 36851a5f..3a0d757e 100644 --- a/tools/editor/cli_arg_required.cpp +++ b/tools/editor/cli_arg_required.cpp @@ -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", diff --git a/tools/editor/cli_camera_presets_catalog.cpp b/tools/editor/cli_camera_presets_catalog.cpp index 75a3d1a0..bc807886 100644 --- a/tools/editor/cli_camera_presets_catalog.cpp +++ b/tools/editor/cli_camera_presets_catalog.cpp @@ -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 +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-wcam-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); @@ -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( + 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; } diff --git a/tools/editor/cli_help.cpp b/tools/editor/cli_help.cpp index b1c91c10..fb6e6941 100644 --- a/tools/editor/cli_help.cpp +++ b/tools/editor/cli_help.cpp @@ -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 [--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 [out.json]\n"); + std::printf(" Export binary .wcam to a human-editable JSON sidecar (defaults to .wcam.json; emits purposeKind as int + name string; floats preserved bit-for-bit)\n"); + std::printf(" --import-wcam-json [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 [--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");