From 7fbf35e98c0d0952e46b70917a2ae58cfa4b32a8 Mon Sep 17 00:00:00 2001 From: Kelsi Date: Sun, 10 May 2026 05:49:45 -0700 Subject: [PATCH] feat(editor): WCMD JSON round-trip closure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds --export-wcmd-json / --import-wcmd-json with the established readEnumField template factoring int+name dual encoding for both minSecurityLevel ("player"/"helper"/"moderator"/"gamemaster"/ "admin") and category ("info"/"movement"/"communication"/ "admincmd"/"debug"). Aliases array serialized as JSON string array. All 3 presets (basic/movement/admin) byte-identical binary roundtrip OK including the movement preset's multi-alias commands (/stand has aliases ["standup", "su"]). Live-tested flat-namespace collision validator: hand-mutated /stand (cmdId=11) to add 'sit' as an alias, colliding with the canonical /sit command. Validator correctly errored: "alias 'sit' collides with another command name or alias — chat parser would dispatch ambiguously". Catches the class of typo bugs where a new alias would shadow an existing command, with the failure surface being silent dispatch ambiguity rather than an error. CLI flag count 1488 -> 1490. --- tools/editor/cli_arg_required.cpp | 1 + tools/editor/cli_chat_commands_catalog.cpp | 186 +++++++++++++++++++++ tools/editor/cli_help.cpp | 4 + 3 files changed, 191 insertions(+) diff --git a/tools/editor/cli_arg_required.cpp b/tools/editor/cli_arg_required.cpp index bbba237d..9168286d 100644 --- a/tools/editor/cli_arg_required.cpp +++ b/tools/editor/cli_arg_required.cpp @@ -439,6 +439,7 @@ const char* const kArgRequired[] = { "--export-wtur-json", "--import-wtur-json", "--gen-cmd-basic", "--gen-cmd-movement", "--gen-cmd-admin", "--info-wcmd", "--validate-wcmd", + "--export-wcmd-json", "--import-wcmd-json", "--gen-weather-temperate", "--gen-weather-arctic", "--gen-weather-desert", "--gen-weather-stormy", "--gen-zone-atmosphere", diff --git a/tools/editor/cli_chat_commands_catalog.cpp b/tools/editor/cli_chat_commands_catalog.cpp index 968c85e4..a377a64c 100644 --- a/tools/editor/cli_chat_commands_catalog.cpp +++ b/tools/editor/cli_chat_commands_catalog.cpp @@ -157,6 +157,64 @@ int handleInfo(int& i, int argc, char** argv) { return 0; } +int parseSecurityLevelToken(const std::string& s) { + using W = wowee::pipeline::WoweeChatCommands; + if (s == "player") return W::Player; + if (s == "helper") return W::Helper; + if (s == "moderator") return W::Moderator; + if (s == "gamemaster") return W::GameMaster; + if (s == "admin") return W::Admin; + return -1; +} + +int parseCategoryToken(const std::string& s) { + using W = wowee::pipeline::WoweeChatCommands; + if (s == "info") return W::Info; + if (s == "movement") return W::Movement; + if (s == "communication") return W::Communication; + if (s == "admincmd") return W::AdminCmd; + if (s == "debug") return W::Debug; + 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-wcmd-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); @@ -306,6 +364,126 @@ 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 = stripWcmdExt(base); + if (out.empty()) out = base + ".wcmd.json"; + if (!wowee::pipeline::WoweeChatCommandsLoader::exists(base)) { + std::fprintf(stderr, + "export-wcmd-json: WCMD not found: %s.wcmd\n", + base.c_str()); + return 1; + } + auto c = wowee::pipeline::WoweeChatCommandsLoader::load(base); + nlohmann::json j; + j["magic"] = "WCMD"; + j["version"] = 1; + j["name"] = c.name; + nlohmann::json arr = nlohmann::json::array(); + for (const auto& e : c.entries) { + arr.push_back({ + {"cmdId", e.cmdId}, + {"command", e.command}, + {"minSecurityLevel", e.minSecurityLevel}, + {"minSecurityLevelName", + securityLevelName(e.minSecurityLevel)}, + {"category", e.category}, + {"categoryName", categoryName(e.category)}, + {"isHidden", e.isHidden != 0}, + {"throttleMs", e.throttleMs}, + {"argSchema", e.argSchema}, + {"helpText", e.helpText}, + {"aliases", e.aliases}, + }); + } + j["entries"] = arr; + std::ofstream os(out); + if (!os) { + std::fprintf(stderr, + "export-wcmd-json: failed to open %s for write\n", + out.c_str()); + return 1; + } + os << j.dump(2) << "\n"; + std::printf("Wrote %s (%zu commands)\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) == ".wcmd.json") { + outBase.resize(outBase.size() - 10); + } else { + stripExt(outBase, ".json"); + stripExt(outBase, ".wcmd"); + } + } + std::ifstream is(in); + if (!is) { + std::fprintf(stderr, + "import-wcmd-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-wcmd-json: JSON parse error: %s\n", ex.what()); + return 1; + } + wowee::pipeline::WoweeChatCommands c; + c.name = j.value("name", std::string{}); + if (!j.contains("entries") || !j["entries"].is_array()) { + std::fprintf(stderr, + "import-wcmd-json: missing or non-array 'entries'\n"); + return 1; + } + for (const auto& je : j["entries"]) { + wowee::pipeline::WoweeChatCommands::Entry e; + e.cmdId = je.value("cmdId", 0u); + e.command = je.value("command", std::string{}); + if (!readEnumField(je, "minSecurityLevel", + "minSecurityLevelName", + parseSecurityLevelToken, + "minSecurityLevel", e.cmdId, + e.minSecurityLevel)) return 1; + if (!readEnumField(je, "category", "categoryName", + parseCategoryToken, "category", + e.cmdId, e.category)) return 1; + e.isHidden = je.value("isHidden", false) ? 1 : 0; + e.throttleMs = je.value("throttleMs", 0u); + e.argSchema = je.value("argSchema", std::string{}); + e.helpText = je.value("helpText", std::string{}); + if (je.contains("aliases") && + je["aliases"].is_array()) { + for (const auto& a : je["aliases"]) { + if (a.is_string()) { + e.aliases.push_back(a.get()); + } + } + } + c.entries.push_back(e); + } + if (!wowee::pipeline::WoweeChatCommandsLoader::save(c, outBase)) { + std::fprintf(stderr, + "import-wcmd-json: failed to save %s.wcmd\n", + outBase.c_str()); + return 1; + } + std::printf("Wrote %s.wcmd (%zu commands)\n", + outBase.c_str(), c.entries.size()); + return 0; +} + } // namespace bool handleChatCommandsCatalog(int& i, int argc, char** argv, @@ -329,6 +507,14 @@ bool handleChatCommandsCatalog(int& i, int argc, char** argv, i + 1 < argc) { outRc = handleValidate(i, argc, argv); return true; } + if (std::strcmp(argv[i], "--export-wcmd-json") == 0 && + i + 1 < argc) { + outRc = handleExportJson(i, argc, argv); return true; + } + if (std::strcmp(argv[i], "--import-wcmd-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 f50e6b54..4e4a59bc 100644 --- a/tools/editor/cli_help.cpp +++ b/tools/editor/cli_help.cpp @@ -2765,6 +2765,10 @@ void printUsage(const char* argv0) { std::printf(" Print WCMD entries (id / /command / minSecurityLevel / category / hidden / throttleMs / aliases count / argSchema)\n"); std::printf(" --validate-wcmd [--json]\n"); std::printf(" Static checks: id+command required, minSecurityLevel 0..4, category 0..4, no duplicate cmdIds; CRITICAL: command names + aliases share one flat namespace (chat parser dispatches uniformly) — duplicate name across canonical+aliases errors. Warns on uppercase chars (parser is case-insensitive but convention is canonical lowercase), Admin-category command at Player/Helper security (likely security misconfiguration), throttleMs > 60000 (likely ms-vs-s units typo), self-alias (canonical already matches), and empty helpText (/help would show no description)\n"); + std::printf(" --export-wcmd-json [out.json]\n"); + std::printf(" Export binary .wcmd to a human-editable JSON sidecar (defaults to .wcmd.json; emits both minSecurityLevel and category as int + name string; aliases as JSON string array)\n"); + std::printf(" --import-wcmd-json [out-base]\n"); + std::printf(" Import a .wcmd.json sidecar back into binary .wcmd (minSecurityLevel int OR \"player\"/\"helper\"/\"moderator\"/\"gamemaster\"/\"admin\"; category int OR \"info\"/\"movement\"/\"communication\"/\"admincmd\"/\"debug\"; aliases array preserved)\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");