From 7fa24af3b2ab5fe72f82a5bdbd3ca79838235d2d Mon Sep 17 00:00:00 2001 From: Kelsi Date: Sun, 10 May 2026 01:26:02 -0700 Subject: [PATCH] feat(editor): add WTBD JSON round-trip (--export/--import-wtbd-json) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Dual encoding for both WTBD enums on import: backgroundPattern (int 0..4 OR token "solid" / "gradient" / "chevron" / "quartered" / "starburst") and borderPattern (int 0..3 OR token "none" / "thin" / "thick" / "decorative"). isApproved accepts bool or int. The 3 RGBA color fields serialize as 0xAARRGGBB uint32 values directly — operators picking colors in JSON can use any familiar hex form via Python int("0x...", 16) or similar pre-processing. All 3 presets (alliance/horde/faction) byte-identical roundtrip OK. Token-form import smoke-tested with starburst + decorative pattern combination. CLI flag count 1146 -> 1148. --- tools/editor/cli_arg_required.cpp | 1 + tools/editor/cli_help.cpp | 4 + tools/editor/cli_tabards_catalog.cpp | 198 +++++++++++++++++++++++++++ 3 files changed, 203 insertions(+) diff --git a/tools/editor/cli_arg_required.cpp b/tools/editor/cli_arg_required.cpp index bb1d1e7c..a3fcf86a 100644 --- a/tools/editor/cli_arg_required.cpp +++ b/tools/editor/cli_arg_required.cpp @@ -318,6 +318,7 @@ const char* const kArgRequired[] = { "--export-wbab-json", "--import-wbab-json", "--gen-tbd", "--gen-tbd-horde", "--gen-tbd-faction", "--info-wtbd", "--validate-wtbd", + "--export-wtbd-json", "--import-wtbd-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 ac871cae..0c676d7b 100644 --- a/tools/editor/cli_help.cpp +++ b/tools/editor/cli_help.cpp @@ -2205,6 +2205,10 @@ void printUsage(const char* argv0) { std::printf(" Print WTBD entries (id / bg-pattern / border / emblem / guild / approval state / name)\n"); std::printf(" --validate-wtbd [--json]\n"); std::printf(" Static checks: id+name required, backgroundPattern 0..4, borderPattern 0..3, no duplicate ids; warns on emblemId>1023, alpha=0 on any color layer (transparent), and emblem-vs-background color similarity (squared RGB distance < 1500 — emblem unreadable)\n"); + std::printf(" --export-wtbd-json [out.json]\n"); + std::printf(" Export binary .wtbd to a human-editable JSON sidecar (defaults to .wtbd.json; emits both backgroundPattern/borderPattern ints AND name strings; all 3 colors as 0xAARRGGBB uint32)\n"); + std::printf(" --import-wtbd-json [out-base]\n"); + std::printf(" Import a .wtbd.json sidecar back into binary .wtbd (backgroundPattern int OR \"solid\"/\"gradient\"/\"chevron\"/\"quartered\"/\"starburst\"; borderPattern int OR \"none\"/\"thin\"/\"thick\"/\"decorative\"; isApproved bool OR int)\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_tabards_catalog.cpp b/tools/editor/cli_tabards_catalog.cpp index a59a19b7..2a6e0256 100644 --- a/tools/editor/cli_tabards_catalog.cpp +++ b/tools/editor/cli_tabards_catalog.cpp @@ -154,6 +154,198 @@ int handleInfo(int& i, int argc, char** argv) { return 0; } +int parseBackgroundPatternToken(const std::string& s) { + using T = wowee::pipeline::WoweeTabards; + if (s == "solid") return T::Solid; + if (s == "gradient") return T::Gradient; + if (s == "chevron") return T::Chevron; + if (s == "quartered") return T::Quartered; + if (s == "starburst") return T::Starburst; + return -1; +} + +int parseBorderPatternToken(const std::string& s) { + using T = wowee::pipeline::WoweeTabards; + if (s == "none") return T::BorderNone; + if (s == "thin") return T::BorderThin; + if (s == "thick") return T::BorderThick; + if (s == "decorative") return T::BorderDecorative; + 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-wtbd-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 handleExportJson(int& i, int argc, char** argv) { + std::string base = argv[++i]; + std::string out; + if (parseOptArg(i, argc, argv)) out = argv[++i]; + base = stripWtbdExt(base); + if (out.empty()) out = base + ".wtbd.json"; + if (!wowee::pipeline::WoweeTabardsLoader::exists(base)) { + std::fprintf(stderr, + "export-wtbd-json: WTBD not found: %s.wtbd\n", + base.c_str()); + return 1; + } + auto c = wowee::pipeline::WoweeTabardsLoader::load(base); + nlohmann::json j; + j["magic"] = "WTBD"; + j["version"] = 1; + j["name"] = c.name; + nlohmann::json arr = nlohmann::json::array(); + for (const auto& e : c.entries) { + arr.push_back({ + {"tabardId", e.tabardId}, + {"name", e.name}, + {"description", e.description}, + {"backgroundPattern", e.backgroundPattern}, + {"backgroundPatternName", + backgroundPatternName(e.backgroundPattern)}, + {"backgroundColor", e.backgroundColor}, + {"borderPattern", e.borderPattern}, + {"borderPatternName", + borderPatternName(e.borderPattern)}, + {"borderColor", e.borderColor}, + {"emblemId", e.emblemId}, + {"emblemColor", e.emblemColor}, + {"guildId", e.guildId}, + {"creatorPlayerId", e.creatorPlayerId}, + {"isApproved", e.isApproved != 0}, + {"iconColorRGBA", e.iconColorRGBA}, + }); + } + j["entries"] = arr; + std::ofstream os(out); + if (!os) { + std::fprintf(stderr, + "export-wtbd-json: failed to open %s for write\n", + out.c_str()); + return 1; + } + os << j.dump(2) << "\n"; + std::printf("Wrote %s (%zu tabards)\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) == ".wtbd.json") { + outBase.resize(outBase.size() - 10); + } else { + stripExt(outBase, ".json"); + stripExt(outBase, ".wtbd"); + } + } + std::ifstream is(in); + if (!is) { + std::fprintf(stderr, + "import-wtbd-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-wtbd-json: JSON parse error: %s\n", ex.what()); + return 1; + } + wowee::pipeline::WoweeTabards c; + c.name = j.value("name", std::string{}); + if (!j.contains("entries") || !j["entries"].is_array()) { + std::fprintf(stderr, + "import-wtbd-json: missing or non-array 'entries'\n"); + return 1; + } + for (const auto& je : j["entries"]) { + wowee::pipeline::WoweeTabards::Entry e; + e.tabardId = je.value("tabardId", 0u); + e.name = je.value("name", std::string{}); + e.description = je.value("description", std::string{}); + if (!readEnumField(je, "backgroundPattern", + "backgroundPatternName", + parseBackgroundPatternToken, + "backgroundPattern", + e.tabardId, + e.backgroundPattern)) return 1; + if (!readEnumField(je, "borderPattern", + "borderPatternName", + parseBorderPatternToken, + "borderPattern", + e.tabardId, + e.borderPattern)) return 1; + e.emblemId = static_cast( + je.value("emblemId", 0u)); + e.backgroundColor = je.value("backgroundColor", + 0xFF000000u); + e.borderColor = je.value("borderColor", 0xFFFFFFFFu); + e.emblemColor = je.value("emblemColor", 0xFFFFFFFFu); + e.guildId = je.value("guildId", 0u); + e.creatorPlayerId = je.value("creatorPlayerId", 0u); + if (je.contains("isApproved")) { + const auto& a = je["isApproved"]; + if (a.is_boolean()) + e.isApproved = a.get() ? 1 : 0; + else if (a.is_number_integer()) + e.isApproved = static_cast( + a.get() != 0 ? 1 : 0); + } + e.iconColorRGBA = je.value("iconColorRGBA", 0xFFFFFFFFu); + c.entries.push_back(e); + } + if (!wowee::pipeline::WoweeTabardsLoader::save(c, outBase)) { + std::fprintf(stderr, + "import-wtbd-json: failed to save %s.wtbd\n", + outBase.c_str()); + return 1; + } + std::printf("Wrote %s.wtbd (%zu tabards)\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); @@ -281,6 +473,12 @@ bool handleTabardsCatalog(int& i, int argc, char** argv, if (std::strcmp(argv[i], "--validate-wtbd") == 0 && i + 1 < argc) { outRc = handleValidate(i, argc, argv); return true; } + if (std::strcmp(argv[i], "--export-wtbd-json") == 0 && i + 1 < argc) { + outRc = handleExportJson(i, argc, argv); return true; + } + if (std::strcmp(argv[i], "--import-wtbd-json") == 0 && i + 1 < argc) { + outRc = handleImportJson(i, argc, argv); return true; + } return false; }