From 2fa2eb9cf0fc5a0dedad3644b43d8c1b3966c873 Mon Sep 17 00:00:00 2001 From: Kelsi Date: Sun, 10 May 2026 05:42:40 -0700 Subject: [PATCH] feat(editor): WTUR JSON round-trip closure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds --export-wtur-json / --import-wtur-json with the established readEnumField template factoring int+name dual encoding for triggerEvent ("login"/"zoneenter"/"levelup"/"itempickup"/ "skilltrain"). Title/body/targetUIElementName multibyte text preserved through JSON. All 3 presets (newbie/levelup/bg) byte-identical binary roundtrip OK including the BG preset's per-mapId trigger gating (AV=30/WSG=489/AB=529). Live-tested readability validator: hand-mutated Welcome step (tutId 1) hideAfterSec from 30 to 3 in JSON. Validator correctly errored: "hideAfterSec=3 is below 5s — popup vanishes before the player can read it". Catches the class of UX bugs where overly-aggressive auto-dismiss timers make tutorials unhelpful (player sees a flash they can't process). CLI flag count 1479 -> 1481. --- tools/editor/cli_arg_required.cpp | 1 + tools/editor/cli_help.cpp | 4 + tools/editor/cli_tutorial_steps_catalog.cpp | 171 ++++++++++++++++++++ 3 files changed, 176 insertions(+) diff --git a/tools/editor/cli_arg_required.cpp b/tools/editor/cli_arg_required.cpp index adf25ecc..78dd6032 100644 --- a/tools/editor/cli_arg_required.cpp +++ b/tools/editor/cli_arg_required.cpp @@ -436,6 +436,7 @@ const char* const kArgRequired[] = { "--export-wswp-json", "--import-wswp-json", "--gen-tut-newbie", "--gen-tut-levelup", "--gen-tut-bg", "--info-wtur", "--validate-wtur", + "--export-wtur-json", "--import-wtur-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 4f8ab3cf..01157f11 100644 --- a/tools/editor/cli_help.cpp +++ b/tools/editor/cli_help.cpp @@ -2751,6 +2751,10 @@ void printUsage(const char* argv0) { std::printf(" Print WTUR entries (id / step / triggerEvent+value / hide-after sec / body length / title)\n"); std::printf(" --validate-wtur [--json]\n"); std::printf(" Static checks: id+name+title+body required, triggerEvent 0..4, stepIndex > 0 (sequence starts at 1), no duplicate tutIds, no duplicate (event,value,step) triples (sequence ordering tie); hideAfterSec MUST be 0 (no auto-dismiss) OR >= 5s (else popup vanishes before player can read it). Warns on Login event with non-zero triggerValue (dead data, Login is unconditional), non-Login event with triggerValue=0 (would fire for ALL events of that kind), and body length < 10 chars (likely placeholder)\n"); + std::printf(" --export-wtur-json [out.json]\n"); + std::printf(" Export binary .wtur to a human-editable JSON sidecar (defaults to .wtur.json; emits triggerEvent as int + name string; multibyte strings preserved)\n"); + std::printf(" --import-wtur-json [out-base]\n"); + std::printf(" Import a .wtur.json sidecar back into binary .wtur (triggerEvent int OR \"login\"/\"zoneenter\"/\"levelup\"/\"itempickup\"/\"skilltrain\" — round-trips title/body/target text 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"); diff --git a/tools/editor/cli_tutorial_steps_catalog.cpp b/tools/editor/cli_tutorial_steps_catalog.cpp index 94d37410..5c808b91 100644 --- a/tools/editor/cli_tutorial_steps_catalog.cpp +++ b/tools/editor/cli_tutorial_steps_catalog.cpp @@ -142,6 +142,54 @@ int handleInfo(int& i, int argc, char** argv) { return 0; } +int parseTriggerEventToken(const std::string& s) { + using T = wowee::pipeline::WoweeTutorialSteps; + if (s == "login") return T::Login; + if (s == "zoneenter") return T::ZoneEnter; + if (s == "levelup") return T::LevelUp; + if (s == "itempickup") return T::ItemPickup; + if (s == "skilltrain") return T::SkillTrain; + 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-wtur-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); @@ -287,6 +335,121 @@ 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 = stripWturExt(base); + if (out.empty()) out = base + ".wtur.json"; + if (!wowee::pipeline::WoweeTutorialStepsLoader::exists(base)) { + std::fprintf(stderr, + "export-wtur-json: WTUR not found: %s.wtur\n", + base.c_str()); + return 1; + } + auto c = wowee::pipeline::WoweeTutorialStepsLoader::load(base); + nlohmann::json j; + j["magic"] = "WTUR"; + j["version"] = 1; + j["name"] = c.name; + nlohmann::json arr = nlohmann::json::array(); + for (const auto& e : c.entries) { + arr.push_back({ + {"tutId", e.tutId}, + {"name", e.name}, + {"stepIndex", e.stepIndex}, + {"triggerEvent", e.triggerEvent}, + {"triggerEventName", + triggerEventName(e.triggerEvent)}, + {"triggerValue", e.triggerValue}, + {"iconIndex", e.iconIndex}, + {"hideAfterSec", e.hideAfterSec}, + {"title", e.title}, + {"body", e.body}, + {"targetUIElementName", + e.targetUIElementName}, + }); + } + j["entries"] = arr; + std::ofstream os(out); + if (!os) { + std::fprintf(stderr, + "export-wtur-json: failed to open %s for write\n", + out.c_str()); + return 1; + } + os << j.dump(2) << "\n"; + std::printf("Wrote %s (%zu steps)\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) == ".wtur.json") { + outBase.resize(outBase.size() - 10); + } else { + stripExt(outBase, ".json"); + stripExt(outBase, ".wtur"); + } + } + std::ifstream is(in); + if (!is) { + std::fprintf(stderr, + "import-wtur-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-wtur-json: JSON parse error: %s\n", ex.what()); + return 1; + } + wowee::pipeline::WoweeTutorialSteps c; + c.name = j.value("name", std::string{}); + if (!j.contains("entries") || !j["entries"].is_array()) { + std::fprintf(stderr, + "import-wtur-json: missing or non-array 'entries'\n"); + return 1; + } + for (const auto& je : j["entries"]) { + wowee::pipeline::WoweeTutorialSteps::Entry e; + e.tutId = je.value("tutId", 0u); + e.name = je.value("name", std::string{}); + e.stepIndex = static_cast( + je.value("stepIndex", 0)); + if (!readEnumField(je, "triggerEvent", + "triggerEventName", + parseTriggerEventToken, + "triggerEvent", e.tutId, + e.triggerEvent)) return 1; + e.triggerValue = je.value("triggerValue", 0u); + e.iconIndex = je.value("iconIndex", 0u); + e.hideAfterSec = je.value("hideAfterSec", 0u); + e.title = je.value("title", std::string{}); + e.body = je.value("body", std::string{}); + e.targetUIElementName = + je.value("targetUIElementName", std::string{}); + c.entries.push_back(e); + } + if (!wowee::pipeline::WoweeTutorialStepsLoader::save(c, outBase)) { + std::fprintf(stderr, + "import-wtur-json: failed to save %s.wtur\n", + outBase.c_str()); + return 1; + } + std::printf("Wrote %s.wtur (%zu steps)\n", + outBase.c_str(), c.entries.size()); + return 0; +} + } // namespace bool handleTutorialStepsCatalog(int& i, int argc, char** argv, @@ -310,6 +473,14 @@ bool handleTutorialStepsCatalog(int& i, int argc, char** argv, i + 1 < argc) { outRc = handleValidate(i, argc, argv); return true; } + if (std::strcmp(argv[i], "--export-wtur-json") == 0 && + i + 1 < argc) { + outRc = handleExportJson(i, argc, argv); return true; + } + if (std::strcmp(argv[i], "--import-wtur-json") == 0 && + i + 1 < argc) { + outRc = handleImportJson(i, argc, argv); return true; + } return false; }