From 9a734acb871625551c8cdcf3c02227a13829c250 Mon Sep 17 00:00:00 2001 From: Kelsi Date: Sun, 10 May 2026 02:53:18 -0700 Subject: [PATCH] feat(editor): add WSKP JSON round-trip (--export/--import-wskp-json) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit No enum coercion (all-numeric payload). Pct/raw dual encoding for cloudSpeedX10: stored on disk as raw uint8 (0..255 = 0..25.5 mph) for byte-precision round-trip, exported with both forms — cloudSpeedX10 (raw int, authoritative) and cloudSpeedMph (float, derived). Import accepts either form; pct conversion clamps to 0..255 with rounding. This is the same dual-encoding pattern WHRD uses for bonusQualityChance — get byte-identical round-trip from binary AND human-friendly editing in JSON. cloudOpacity stays as raw 0..255 since percent conversion would lose precision (cloudOpacity=78 doesn't round-trip cleanly through %.2f display). All 3 presets (stormwind/arctic/hellfire) byte- identical roundtrip OK. CLI flag count 1260 -> 1262. --- tools/editor/cli_arg_required.cpp | 1 + tools/editor/cli_help.cpp | 4 + tools/editor/cli_sky_params_catalog.cpp | 139 ++++++++++++++++++++++++ 3 files changed, 144 insertions(+) diff --git a/tools/editor/cli_arg_required.cpp b/tools/editor/cli_arg_required.cpp index 7eba701c..6580195f 100644 --- a/tools/editor/cli_arg_required.cpp +++ b/tools/editor/cli_arg_required.cpp @@ -367,6 +367,7 @@ const char* const kArgRequired[] = { "--export-wlma-json", "--import-wlma-json", "--gen-skp", "--gen-skp-arctic", "--gen-skp-hellfire", "--info-wskp", "--validate-wskp", + "--export-wskp-json", "--import-wskp-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 ec5a85bb..2c2be34c 100644 --- a/tools/editor/cli_help.cpp +++ b/tools/editor/cli_help.cpp @@ -2429,6 +2429,10 @@ void printUsage(const char* argv0) { std::printf(" Print WSKP entries (id / map / area / hour / sun angle / fog start..end yards / cloud opacity %% / wind mph / name)\n"); std::printf(" --validate-wskp [--json]\n"); std::printf(" Static checks: id+name required, timeOfDayHour 0..23, fogStartYards < fogEndYards (else inverted/zero falloff), no negative fog distances, no duplicate skyIds, no two keyframes at same (mapId, areaId, timeOfDayHour) triple (diurnal interpolation tie); warns on sunAngleDeg outside [0,360]\n"); + std::printf(" --export-wskp-json [out.json]\n"); + std::printf(" Export binary .wskp to a human-editable JSON sidecar (defaults to .wskp.json; emits cloudSpeedX10 as both raw int AND derived cloudSpeedMph float convenience field)\n"); + std::printf(" --import-wskp-json [out-base]\n"); + std::printf(" Import a .wskp.json sidecar back into binary .wskp (no enum coercion — all-numeric payload; cloudSpeedX10 raw int OR cloudSpeedMph float — converts mph * 10 -> raw with rounding and clamp to 0..255)\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_sky_params_catalog.cpp b/tools/editor/cli_sky_params_catalog.cpp index 4122b0ac..8824620a 100644 --- a/tools/editor/cli_sky_params_catalog.cpp +++ b/tools/editor/cli_sky_params_catalog.cpp @@ -132,6 +132,139 @@ 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 out; + if (parseOptArg(i, argc, argv)) out = argv[++i]; + base = stripWskpExt(base); + if (out.empty()) out = base + ".wskp.json"; + if (!wowee::pipeline::WoweeSkyParamsLoader::exists(base)) { + std::fprintf(stderr, + "export-wskp-json: WSKP not found: %s.wskp\n", + base.c_str()); + return 1; + } + auto c = wowee::pipeline::WoweeSkyParamsLoader::load(base); + nlohmann::json j; + j["magic"] = "WSKP"; + j["version"] = 1; + j["name"] = c.name; + nlohmann::json arr = nlohmann::json::array(); + for (const auto& e : c.entries) { + arr.push_back({ + {"skyId", e.skyId}, + {"name", e.name}, + {"description", e.description}, + {"mapId", e.mapId}, + {"areaId", e.areaId}, + {"timeOfDayHour", e.timeOfDayHour}, + {"zenithColor", e.zenithColor}, + {"horizonColor", e.horizonColor}, + {"sunColor", e.sunColor}, + {"sunAngleDeg", e.sunAngleDeg}, + {"fogStartYards", e.fogStartYards}, + {"fogEndYards", e.fogEndYards}, + {"cloudOpacity", e.cloudOpacity}, + {"cloudSpeedX10", e.cloudSpeedX10}, + {"cloudSpeedMph", e.cloudSpeedX10 / 10.0}, + {"iconColorRGBA", e.iconColorRGBA}, + }); + } + j["entries"] = arr; + std::ofstream os(out); + if (!os) { + std::fprintf(stderr, + "export-wskp-json: failed to open %s for write\n", + out.c_str()); + return 1; + } + os << j.dump(2) << "\n"; + std::printf("Wrote %s (%zu keyframes)\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) == ".wskp.json") { + outBase.resize(outBase.size() - 10); + } else { + stripExt(outBase, ".json"); + stripExt(outBase, ".wskp"); + } + } + std::ifstream is(in); + if (!is) { + std::fprintf(stderr, + "import-wskp-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-wskp-json: JSON parse error: %s\n", ex.what()); + return 1; + } + wowee::pipeline::WoweeSkyParams c; + c.name = j.value("name", std::string{}); + if (!j.contains("entries") || !j["entries"].is_array()) { + std::fprintf(stderr, + "import-wskp-json: missing or non-array 'entries'\n"); + return 1; + } + for (const auto& je : j["entries"]) { + wowee::pipeline::WoweeSkyParams::Entry e; + e.skyId = je.value("skyId", 0u); + e.name = je.value("name", std::string{}); + e.description = je.value("description", std::string{}); + e.mapId = je.value("mapId", 0u); + e.areaId = je.value("areaId", 0u); + e.timeOfDayHour = static_cast( + je.value("timeOfDayHour", 12u)); + e.zenithColor = je.value("zenithColor", 0xFF000000u); + e.horizonColor = je.value("horizonColor", 0xFF000000u); + e.sunColor = je.value("sunColor", 0xFFFFFFFFu); + e.sunAngleDeg = je.value("sunAngleDeg", 0.0f); + e.fogStartYards = je.value("fogStartYards", 100.0f); + e.fogEndYards = je.value("fogEndYards", 500.0f); + e.cloudOpacity = static_cast( + je.value("cloudOpacity", 128u)); + // cloudSpeedX10 accepts raw int form (preferred, + // 0..255 = 0..25.5 mph) OR cloudSpeedMph float + // form (convenience for hand-edited JSON). + if (je.contains("cloudSpeedX10") && + je["cloudSpeedX10"].is_number_integer()) { + e.cloudSpeedX10 = static_cast( + je["cloudSpeedX10"].get()); + } else if (je.contains("cloudSpeedMph") && + je["cloudSpeedMph"].is_number()) { + double mph = je["cloudSpeedMph"].get(); + int x10 = static_cast(mph * 10.0 + 0.5); + if (x10 < 0) x10 = 0; + if (x10 > 255) x10 = 255; + e.cloudSpeedX10 = static_cast(x10); + } + e.iconColorRGBA = je.value("iconColorRGBA", 0xFFFFFFFFu); + c.entries.push_back(e); + } + if (!wowee::pipeline::WoweeSkyParamsLoader::save(c, outBase)) { + std::fprintf(stderr, + "import-wskp-json: failed to save %s.wskp\n", + outBase.c_str()); + return 1; + } + std::printf("Wrote %s.wskp (%zu keyframes)\n", + outBase.c_str(), c.entries.size()); + return 0; +} + int handleValidate(int& i, int argc, char** argv) { std::string base = argv[++i]; bool jsonOut = consumeJsonFlag(i, argc, argv); @@ -264,6 +397,12 @@ bool handleSkyParamsCatalog(int& i, int argc, char** argv, if (std::strcmp(argv[i], "--validate-wskp") == 0 && i + 1 < argc) { outRc = handleValidate(i, argc, argv); return true; } + if (std::strcmp(argv[i], "--export-wskp-json") == 0 && i + 1 < argc) { + outRc = handleExportJson(i, argc, argv); return true; + } + if (std::strcmp(argv[i], "--import-wskp-json") == 0 && i + 1 < argc) { + outRc = handleImportJson(i, argc, argv); return true; + } return false; }