diff --git a/tools/editor/cli_arg_required.cpp b/tools/editor/cli_arg_required.cpp index 79fd2021..3ed5ab5b 100644 --- a/tools/editor/cli_arg_required.cpp +++ b/tools/editor/cli_arg_required.cpp @@ -64,6 +64,7 @@ const char* const kArgRequired[] = { "--export-wach-json", "--import-wach-json", "--gen-trainers", "--gen-trainers-mage", "--gen-trainers-weapons", "--info-wtrn", "--validate-wtrn", + "--export-wtrn-json", "--import-wtrn-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 011b269c..77cbe708 100644 --- a/tools/editor/cli_help.cpp +++ b/tools/editor/cli_help.cpp @@ -1017,6 +1017,10 @@ void printUsage(const char* argv0) { std::printf(" Print WTRN entries (npc / kind / spells with skill+level reqs / items with stock + restock)\n"); std::printf(" --validate-wtrn [--json]\n"); std::printf(" Static checks: npcId>0+unique, kindMask>0, Trainer needs spells, Vendor needs items, no orphan offers\n"); + std::printf(" --export-wtrn-json [out.json]\n"); + std::printf(" Export binary .wtrn to a human-editable JSON sidecar (defaults to .wtrn.json)\n"); + std::printf(" --import-wtrn-json [out-base]\n"); + std::printf(" Import a .wtrn.json sidecar back into binary .wtrn (accepts kindMask int OR kindList string array)\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"); diff --git a/tools/editor/cli_trainers_catalog.cpp b/tools/editor/cli_trainers_catalog.cpp index 5a46d2d5..466a67a7 100644 --- a/tools/editor/cli_trainers_catalog.cpp +++ b/tools/editor/cli_trainers_catalog.cpp @@ -179,6 +179,183 @@ int handleInfo(int& i, int argc, char** argv) { return 0; } +int handleExportJson(int& i, int argc, char** argv) { + // Mirrors the JSON pairs added for every other novel + // open format. Each NPC emits scalar + greeting fields + // plus the spell-offer and item-offer arrays. The + // kindMask emits dual int + name forms. + std::string base = argv[++i]; + std::string outPath; + if (parseOptArg(i, argc, argv)) outPath = argv[++i]; + base = stripWtrnExt(base); + if (outPath.empty()) outPath = base + ".wtrn.json"; + if (!wowee::pipeline::WoweeTrainerLoader::exists(base)) { + std::fprintf(stderr, + "export-wtrn-json: WTRN not found: %s.wtrn\n", base.c_str()); + return 1; + } + auto c = wowee::pipeline::WoweeTrainerLoader::load(base); + nlohmann::json j; + j["name"] = c.name; + nlohmann::json arr = nlohmann::json::array(); + for (const auto& e : c.entries) { + nlohmann::json je; + je["npcId"] = e.npcId; + je["kindMask"] = e.kindMask; + je["kindMaskName"] = wowee::pipeline::WoweeTrainer::kindMaskName(e.kindMask); + nlohmann::json km = nlohmann::json::array(); + if (e.kindMask & wowee::pipeline::WoweeTrainer::Trainer) km.push_back("trainer"); + if (e.kindMask & wowee::pipeline::WoweeTrainer::Vendor) km.push_back("vendor"); + je["kindList"] = km; + je["greeting"] = e.greeting; + nlohmann::json sa = nlohmann::json::array(); + for (const auto& s : e.spells) { + sa.push_back({ + {"spellId", s.spellId}, + {"moneyCostCopper", s.moneyCostCopper}, + {"requiredSkillId", s.requiredSkillId}, + {"requiredSkillRank", s.requiredSkillRank}, + {"requiredLevel", s.requiredLevel}, + }); + } + je["spells"] = sa; + nlohmann::json ia = nlohmann::json::array(); + for (const auto& it : e.items) { + nlohmann::json ji; + ji["itemId"] = it.itemId; + // Emit "unlimited" string when stock is the sentinel + // value so JSON is friendlier to hand-edit. Importer + // accepts either form. + if (it.stockCount == wowee::pipeline::WoweeTrainer::kUnlimitedStock) { + ji["stockCount"] = "unlimited"; + } else { + ji["stockCount"] = it.stockCount; + } + ji["restockSec"] = it.restockSec; + ji["extendedCost"] = it.extendedCost; + ji["moneyCostCopper"] = it.moneyCostCopper; + ia.push_back(ji); + } + je["items"] = ia; + arr.push_back(je); + } + j["entries"] = arr; + std::ofstream out(outPath); + if (!out) { + std::fprintf(stderr, + "export-wtrn-json: cannot write %s\n", outPath.c_str()); + return 1; + } + out << j.dump(2) << "\n"; + out.close(); + std::printf("Wrote %s\n", outPath.c_str()); + std::printf(" source : %s.wtrn\n", base.c_str()); + std::printf(" npcs : %zu\n", c.entries.size()); + return 0; +} + +int handleImportJson(int& i, int argc, char** argv) { + std::string jsonPath = argv[++i]; + std::string outBase; + if (parseOptArg(i, argc, argv)) outBase = argv[++i]; + if (outBase.empty()) { + outBase = jsonPath; + std::string suffix = ".wtrn.json"; + if (outBase.size() > suffix.size() && + outBase.substr(outBase.size() - suffix.size()) == suffix) { + outBase = outBase.substr(0, outBase.size() - suffix.size()); + } else if (outBase.size() > 5 && + outBase.substr(outBase.size() - 5) == ".json") { + outBase = outBase.substr(0, outBase.size() - 5); + } + } + outBase = stripWtrnExt(outBase); + std::ifstream in(jsonPath); + if (!in) { + std::fprintf(stderr, + "import-wtrn-json: cannot read %s\n", jsonPath.c_str()); + return 1; + } + nlohmann::json j; + try { in >> j; } + catch (const std::exception& e) { + std::fprintf(stderr, + "import-wtrn-json: bad JSON in %s: %s\n", + jsonPath.c_str(), e.what()); + return 1; + } + auto kindFromName = [](const std::string& s) -> uint8_t { + if (s == "trainer") return wowee::pipeline::WoweeTrainer::Trainer; + if (s == "vendor") return wowee::pipeline::WoweeTrainer::Vendor; + return 0; + }; + wowee::pipeline::WoweeTrainer c; + c.name = j.value("name", std::string{}); + if (j.contains("entries") && j["entries"].is_array()) { + for (const auto& je : j["entries"]) { + wowee::pipeline::WoweeTrainer::Entry e; + e.npcId = je.value("npcId", 0u); + if (je.contains("kindMask") && je["kindMask"].is_number_integer()) { + e.kindMask = static_cast(je["kindMask"].get()); + } else if (je.contains("kindList") && je["kindList"].is_array()) { + for (const auto& f : je["kindList"]) { + if (f.is_string()) e.kindMask |= kindFromName(f.get()); + } + } + e.greeting = je.value("greeting", std::string{}); + if (je.contains("spells") && je["spells"].is_array()) { + for (const auto& js : je["spells"]) { + wowee::pipeline::WoweeTrainer::SpellOffer s; + s.spellId = js.value("spellId", 0u); + s.moneyCostCopper = js.value("moneyCostCopper", 0u); + s.requiredSkillId = js.value("requiredSkillId", 0u); + s.requiredSkillRank = static_cast( + js.value("requiredSkillRank", 0)); + s.requiredLevel = static_cast( + js.value("requiredLevel", 1)); + e.spells.push_back(s); + } + } + if (je.contains("items") && je["items"].is_array()) { + for (const auto& ji : je["items"]) { + wowee::pipeline::WoweeTrainer::ItemOffer it; + it.itemId = ji.value("itemId", 0u); + if (ji.contains("stockCount")) { + const auto& sc = ji["stockCount"]; + if (sc.is_string() && + sc.get() == "unlimited") { + it.stockCount = + wowee::pipeline::WoweeTrainer::kUnlimitedStock; + } else if (sc.is_number_integer()) { + it.stockCount = sc.get(); + } else { + it.stockCount = + wowee::pipeline::WoweeTrainer::kUnlimitedStock; + } + } else { + it.stockCount = + wowee::pipeline::WoweeTrainer::kUnlimitedStock; + } + it.restockSec = ji.value("restockSec", 0u); + it.extendedCost = ji.value("extendedCost", 0u); + it.moneyCostCopper = ji.value("moneyCostCopper", 0u); + e.items.push_back(it); + } + } + c.entries.push_back(std::move(e)); + } + } + if (!wowee::pipeline::WoweeTrainerLoader::save(c, outBase)) { + std::fprintf(stderr, + "import-wtrn-json: failed to save %s.wtrn\n", outBase.c_str()); + return 1; + } + std::printf("Wrote %s.wtrn\n", outBase.c_str()); + std::printf(" source : %s\n", jsonPath.c_str()); + std::printf(" npcs : %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); @@ -308,6 +485,12 @@ bool handleTrainersCatalog(int& i, int argc, char** argv, int& outRc) { if (std::strcmp(argv[i], "--validate-wtrn") == 0 && i + 1 < argc) { outRc = handleValidate(i, argc, argv); return true; } + if (std::strcmp(argv[i], "--export-wtrn-json") == 0 && i + 1 < argc) { + outRc = handleExportJson(i, argc, argv); return true; + } + if (std::strcmp(argv[i], "--import-wtrn-json") == 0 && i + 1 < argc) { + outRc = handleImportJson(i, argc, argv); return true; + } return false; }