From 6b2bfb0f5a95d9db989a13a55df872bca08e2b94 Mon Sep 17 00:00:00 2001 From: Kelsi Date: Sat, 9 May 2026 22:45:02 -0700 Subject: [PATCH] feat(editor): add WACR JSON round-trip (--export/--import-wacr-json) Closes the editing loop on the achievement-criteria catalog: dump a .wacr to JSON, hand-edit criteriaType / targetId / requiredCount / timeLimitMs / progressOrder (e.g. retune a kill-count from 50 boars to 25, swap a quest progression target, add a time limit to turn a normal achievement into a speedrun, reorder progressOrder so the easiest sub-objective shows first in the UI), re-import to a byte-identical binary. The exporter emits both criteriaType (int 0..12) and the human- readable criteriaTypeName ("kill-creature" / "reach-level" / "complete-quest" / "earn-gold" / "gain-honor" / "earn-reputation" / "explore-zone" / "loot-item" / "use-item" / "cast-spell" / "pvp-kill" / "dungeon-run" / "misc"); the importer accepts either form. The 13-way enum is the largest single-field dual-encoding implemented so far in any catalog round-trip. Verified byte-identical round-trip on all three presets (kill / quest / mixed). CLI flag count 986 -> 988. --- .../cli_achievement_criteria_catalog.cpp | 164 ++++++++++++++++++ tools/editor/cli_arg_required.cpp | 1 + tools/editor/cli_help.cpp | 4 + 3 files changed, 169 insertions(+) diff --git a/tools/editor/cli_achievement_criteria_catalog.cpp b/tools/editor/cli_achievement_criteria_catalog.cpp index b2e52354..facccf19 100644 --- a/tools/editor/cli_achievement_criteria_catalog.cpp +++ b/tools/editor/cli_achievement_criteria_catalog.cpp @@ -5,6 +5,7 @@ #include "pipeline/wowee_achievement_criteria.hpp" #include +#include #include #include #include @@ -122,6 +123,163 @@ 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 outPath; + if (parseOptArg(i, argc, argv)) outPath = argv[++i]; + base = stripWacrExt(base); + if (!wowee::pipeline::WoweeAchievementCriteriaLoader::exists(base)) { + std::fprintf(stderr, + "export-wacr-json: WACR not found: %s.wacr\n", + base.c_str()); + return 1; + } + auto c = wowee::pipeline::WoweeAchievementCriteriaLoader::load(base); + if (outPath.empty()) outPath = base + ".wacr.json"; + nlohmann::json j; + j["catalog"] = c.name; + nlohmann::json arr = nlohmann::json::array(); + for (const auto& e : c.entries) { + nlohmann::json je; + je["criteriaId"] = e.criteriaId; + je["name"] = e.name; + je["description"] = e.description; + je["achievementId"] = e.achievementId; + je["targetId"] = e.targetId; + je["requiredCount"] = e.requiredCount; + je["timeLimitMs"] = e.timeLimitMs; + je["criteriaType"] = e.criteriaType; + je["criteriaTypeName"] = + wowee::pipeline::WoweeAchievementCriteria::criteriaTypeName(e.criteriaType); + je["progressOrder"] = e.progressOrder; + je["iconColorRGBA"] = e.iconColorRGBA; + arr.push_back(je); + } + j["entries"] = arr; + std::ofstream os(outPath); + if (!os) { + std::fprintf(stderr, + "export-wacr-json: failed to open %s for write\n", + outPath.c_str()); + return 1; + } + os << j.dump(2) << "\n"; + std::printf("Wrote %s\n", outPath.c_str()); + std::printf(" catalog : %s\n", c.name.c_str()); + std::printf(" criteria : %zu\n", c.entries.size()); + return 0; +} + +uint8_t parseCriteriaTypeToken(const nlohmann::json& jv, + uint8_t fallback) { + if (jv.is_number_integer() || jv.is_number_unsigned()) { + int v = jv.get(); + if (v < 0 || v > wowee::pipeline::WoweeAchievementCriteria::Misc) + return fallback; + return static_cast(v); + } + if (jv.is_string()) { + std::string s = jv.get(); + for (auto& ch : s) ch = static_cast(std::tolower(ch)); + if (s == "kill-creature" || + s == "killcreature") return wowee::pipeline::WoweeAchievementCriteria::KillCreature; + if (s == "reach-level" || + s == "reachlevel") return wowee::pipeline::WoweeAchievementCriteria::ReachLevel; + if (s == "complete-quest" || + s == "completequest") return wowee::pipeline::WoweeAchievementCriteria::CompleteQuest; + if (s == "earn-gold" || + s == "earngold") return wowee::pipeline::WoweeAchievementCriteria::EarnGold; + if (s == "gain-honor" || + s == "gainhonor") return wowee::pipeline::WoweeAchievementCriteria::GainHonor; + if (s == "earn-reputation" || + s == "earnreputation") return wowee::pipeline::WoweeAchievementCriteria::EarnReputation; + if (s == "explore-zone" || + s == "explorezone") return wowee::pipeline::WoweeAchievementCriteria::ExploreZone; + if (s == "loot-item" || + s == "lootitem") return wowee::pipeline::WoweeAchievementCriteria::LootItem; + if (s == "use-item" || + s == "useitem") return wowee::pipeline::WoweeAchievementCriteria::UseItem; + if (s == "cast-spell" || + s == "castspell") return wowee::pipeline::WoweeAchievementCriteria::CastSpell; + if (s == "pvp-kill" || + s == "pvpkill") return wowee::pipeline::WoweeAchievementCriteria::PvPKill; + if (s == "dungeon-run" || + s == "dungeonrun") return wowee::pipeline::WoweeAchievementCriteria::DungeonRun; + if (s == "misc") return wowee::pipeline::WoweeAchievementCriteria::Misc; + } + return fallback; +} + +int handleImportJson(int& i, int argc, char** argv) { + std::string jsonPath = argv[++i]; + std::string outBase; + if (parseOptArg(i, argc, argv)) outBase = argv[++i]; + std::ifstream is(jsonPath); + if (!is) { + std::fprintf(stderr, + "import-wacr-json: failed to open %s\n", jsonPath.c_str()); + return 1; + } + nlohmann::json j; + try { + is >> j; + } catch (const std::exception& ex) { + std::fprintf(stderr, + "import-wacr-json: parse error in %s: %s\n", + jsonPath.c_str(), ex.what()); + return 1; + } + wowee::pipeline::WoweeAchievementCriteria c; + if (j.contains("catalog") && j["catalog"].is_string()) + c.name = j["catalog"].get(); + if (j.contains("entries") && j["entries"].is_array()) { + for (const auto& je : j["entries"]) { + wowee::pipeline::WoweeAchievementCriteria::Entry e; + if (je.contains("criteriaId")) e.criteriaId = je["criteriaId"].get(); + if (je.contains("name")) e.name = je["name"].get(); + if (je.contains("description")) e.description = je["description"].get(); + if (je.contains("achievementId")) e.achievementId = je["achievementId"].get(); + if (je.contains("targetId")) e.targetId = je["targetId"].get(); + if (je.contains("requiredCount")) e.requiredCount = je["requiredCount"].get(); + if (je.contains("timeLimitMs")) e.timeLimitMs = je["timeLimitMs"].get(); + uint8_t type = wowee::pipeline::WoweeAchievementCriteria::KillCreature; + if (je.contains("criteriaType")) + type = parseCriteriaTypeToken(je["criteriaType"], type); + else if (je.contains("criteriaTypeName")) + type = parseCriteriaTypeToken(je["criteriaTypeName"], type); + e.criteriaType = type; + if (je.contains("progressOrder")) e.progressOrder = je["progressOrder"].get(); + if (je.contains("iconColorRGBA")) e.iconColorRGBA = je["iconColorRGBA"].get(); + c.entries.push_back(e); + } + } + if (outBase.empty()) { + outBase = jsonPath; + const std::string suffix1 = ".wacr.json"; + const std::string suffix2 = ".json"; + if (outBase.size() >= suffix1.size() && + outBase.compare(outBase.size() - suffix1.size(), + suffix1.size(), suffix1) == 0) { + outBase.resize(outBase.size() - suffix1.size()); + } else if (outBase.size() >= suffix2.size() && + outBase.compare(outBase.size() - suffix2.size(), + suffix2.size(), suffix2) == 0) { + outBase.resize(outBase.size() - suffix2.size()); + } + } + outBase = stripWacrExt(outBase); + if (!wowee::pipeline::WoweeAchievementCriteriaLoader::save(c, outBase)) { + std::fprintf(stderr, + "import-wacr-json: failed to save %s.wacr\n", + outBase.c_str()); + return 1; + } + std::printf("Wrote %s.wacr\n", outBase.c_str()); + std::printf(" catalog : %s\n", c.name.c_str()); + std::printf(" criteria : %zu\n", c.entries.size()); + return 0; +} + int handleValidate(int& i, int argc, char** argv) { std::string base = argv[++i]; bool jsonOut = consumeJsonFlag(i, argc, argv); @@ -258,6 +416,12 @@ bool handleAchievementCriteriaCatalog(int& i, int argc, char** argv, if (std::strcmp(argv[i], "--validate-wacr") == 0 && i + 1 < argc) { outRc = handleValidate(i, argc, argv); return true; } + if (std::strcmp(argv[i], "--export-wacr-json") == 0 && i + 1 < argc) { + outRc = handleExportJson(i, argc, argv); return true; + } + if (std::strcmp(argv[i], "--import-wacr-json") == 0 && i + 1 < argc) { + outRc = handleImportJson(i, argc, argv); return true; + } return false; } diff --git a/tools/editor/cli_arg_required.cpp b/tools/editor/cli_arg_required.cpp index ddf97980..9ef58d9a 100644 --- a/tools/editor/cli_arg_required.cpp +++ b/tools/editor/cli_arg_required.cpp @@ -249,6 +249,7 @@ const char* const kArgRequired[] = { "--export-wspr-json", "--import-wspr-json", "--gen-acr", "--gen-acr-quest", "--gen-acr-mixed", "--info-wacr", "--validate-wacr", + "--export-wacr-json", "--import-wacr-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 010cb694..5c9f2128 100644 --- a/tools/editor/cli_help.cpp +++ b/tools/editor/cli_help.cpp @@ -1889,6 +1889,10 @@ void printUsage(const char* argv0) { std::printf(" Print WACR entries (id / achievementId / type / targetId / requiredCount / timeLimitMs / progressOrder / name)\n"); std::printf(" --validate-wacr [--json]\n"); std::printf(" Static checks: id+name+achievementId required, criteriaType 0..12, no duplicate ids; warns on missing targetId for type-specific kinds, ReachLevel>80, timeLimit on non-timed types, requiredCount=0\n"); + std::printf(" --export-wacr-json [out.json]\n"); + std::printf(" Export binary .wacr to a human-editable JSON sidecar (defaults to .wacr.json)\n"); + std::printf(" --import-wacr-json [out-base]\n"); + std::printf(" Import a .wacr.json sidecar back into binary .wacr (accepts criteriaType int 0..12 OR criteriaTypeName string)\n"); std::printf(" --gen-weather-temperate [zoneName]\n"); std::printf(" Emit .wow weather schedule: clear-dominant + occasional rain + fog (forest / grassland)\n"); std::printf(" --gen-weather-arctic [zoneName]\n");