From 9318b6c006e3e872fc3507fade038fb20c1a74e2 Mon Sep 17 00:00:00 2001 From: Kelsi Date: Sat, 9 May 2026 22:16:13 -0700 Subject: [PATCH] feat(editor): add WMAT JSON round-trip (--export/--import-wmat-json) Closes the editing loop on the item-material catalog: dump a .wmat to JSON, hand-edit materialKind / weightCategory / foley / impact sound bindings / material flags (e.g. swap a Hide entry from Light to Medium weight, add IsBreakable to a wooden bow, re-bind Plate's foley to a different WSND entry, mark a new HolyForged variant), re-import to a byte-identical binary. Three different field types each take dual int+name forms: - materialKind: int 0..11 OR "cloth"/"leather"/"mail"/"plate"/ "wood"/"stone"/"metal"/"liquid"/"organic"/"crystal"/ "ethereal"/"hide" - weightCategory: int 0..2 OR "light"/"medium"/"heavy" - materialFlags: int bitfield OR pipe-separated label string ("IsMagical|IsBreakable|IsHolyCharged"). Importer prefers int form when both present so unknown bits round-trip losslessly. Verified byte-identical round-trip on all three presets (armor / weapon / magical). CLI flag count 948 -> 950. --- tools/editor/cli_arg_required.cpp | 1 + tools/editor/cli_help.cpp | 4 + tools/editor/cli_item_materials_catalog.cpp | 207 ++++++++++++++++++++ 3 files changed, 212 insertions(+) diff --git a/tools/editor/cli_arg_required.cpp b/tools/editor/cli_arg_required.cpp index d81df0b5..0817dd76 100644 --- a/tools/editor/cli_arg_required.cpp +++ b/tools/editor/cli_arg_required.cpp @@ -233,6 +233,7 @@ const char* const kArgRequired[] = { "--export-wcdf-json", "--import-wcdf-json", "--gen-mat", "--gen-mat-weapon", "--gen-mat-magical", "--info-wmat", "--validate-wmat", + "--export-wmat-json", "--import-wmat-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 f0362ee1..d86292f4 100644 --- a/tools/editor/cli_help.cpp +++ b/tools/editor/cli_help.cpp @@ -1813,6 +1813,10 @@ void printUsage(const char* argv0) { std::printf(" Print WMAT entries (id / kind / weight / foley/impact sound ids / material flags / name) — flags decoded as label list\n"); std::printf(" --validate-wmat [--json]\n"); std::printf(" Static checks: id+name required, materialKind 0..11, weightCategory 0..2, no duplicate ids; warns on Holy+Cursed combo, Plate non-heavy, Cloth non-light\n"); + std::printf(" --export-wmat-json [out.json]\n"); + std::printf(" Export binary .wmat to a human-editable JSON sidecar (defaults to .wmat.json)\n"); + std::printf(" --import-wmat-json [out-base]\n"); + std::printf(" Import a .wmat.json sidecar back into binary .wmat (accepts materialKind/weightCategory int OR name; materialFlags int OR pipe-separated label 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"); diff --git a/tools/editor/cli_item_materials_catalog.cpp b/tools/editor/cli_item_materials_catalog.cpp index 9c8849c3..ddf7adac 100644 --- a/tools/editor/cli_item_materials_catalog.cpp +++ b/tools/editor/cli_item_materials_catalog.cpp @@ -5,6 +5,7 @@ #include "pipeline/wowee_item_materials.hpp" #include +#include #include #include #include @@ -144,6 +145,206 @@ 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 = stripWmatExt(base); + if (!wowee::pipeline::WoweeItemMaterialLoader::exists(base)) { + std::fprintf(stderr, + "export-wmat-json: WMAT not found: %s.wmat\n", + base.c_str()); + return 1; + } + auto c = wowee::pipeline::WoweeItemMaterialLoader::load(base); + if (outPath.empty()) outPath = base + ".wmat.json"; + nlohmann::json j; + j["catalog"] = c.name; + nlohmann::json arr = nlohmann::json::array(); + for (const auto& e : c.entries) { + std::string flagNames; + appendMaterialFlagNames(e.materialFlags, flagNames); + nlohmann::json je; + je["materialId"] = e.materialId; + je["name"] = e.name; + je["description"] = e.description; + je["materialKind"] = e.materialKind; + je["materialKindName"] = + wowee::pipeline::WoweeItemMaterial::materialKindName(e.materialKind); + je["weightCategory"] = e.weightCategory; + je["weightCategoryName"] = + wowee::pipeline::WoweeItemMaterial::weightCategoryName(e.weightCategory); + je["foleySoundId"] = e.foleySoundId; + je["impactSoundId"] = e.impactSoundId; + je["materialFlags"] = e.materialFlags; + je["materialFlagsLabels"] = flagNames; + je["iconColorRGBA"] = e.iconColorRGBA; + arr.push_back(je); + } + j["entries"] = arr; + std::ofstream os(outPath); + if (!os) { + std::fprintf(stderr, + "export-wmat-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(" materials : %zu\n", c.entries.size()); + return 0; +} + +uint8_t parseMaterialKindToken(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::WoweeItemMaterial::Hide) + 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 == "cloth") return wowee::pipeline::WoweeItemMaterial::Cloth; + if (s == "leather") return wowee::pipeline::WoweeItemMaterial::Leather; + if (s == "mail") return wowee::pipeline::WoweeItemMaterial::Mail; + if (s == "plate") return wowee::pipeline::WoweeItemMaterial::Plate; + if (s == "wood") return wowee::pipeline::WoweeItemMaterial::Wood; + if (s == "stone") return wowee::pipeline::WoweeItemMaterial::Stone; + if (s == "metal") return wowee::pipeline::WoweeItemMaterial::Metal; + if (s == "liquid") return wowee::pipeline::WoweeItemMaterial::Liquid; + if (s == "organic") return wowee::pipeline::WoweeItemMaterial::Organic; + if (s == "crystal") return wowee::pipeline::WoweeItemMaterial::Crystal; + if (s == "ethereal") return wowee::pipeline::WoweeItemMaterial::Ethereal; + if (s == "hide") return wowee::pipeline::WoweeItemMaterial::Hide; + } + return fallback; +} + +uint8_t parseWeightCategoryToken(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::WoweeItemMaterial::Heavy) + 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 == "light") return wowee::pipeline::WoweeItemMaterial::Light; + if (s == "medium") return wowee::pipeline::WoweeItemMaterial::Medium; + if (s == "heavy") return wowee::pipeline::WoweeItemMaterial::Heavy; + } + return fallback; +} + +uint32_t parseMaterialFlagsField(const nlohmann::json& jv) { + using F = wowee::pipeline::WoweeItemMaterial; + if (jv.is_number_integer() || jv.is_number_unsigned()) + return jv.get(); + if (jv.is_string()) { + std::string s = jv.get(); + uint32_t out = 0; + size_t pos = 0; + while (pos < s.size()) { + size_t end = s.find('|', pos); + if (end == std::string::npos) end = s.size(); + std::string tok = s.substr(pos, end - pos); + for (auto& ch : tok) ch = static_cast(std::tolower(ch)); + if (tok == "isbreakable") out |= F::IsBreakable; + else if (tok == "ismagical") out |= F::IsMagical; + else if (tok == "isflammable") out |= F::IsFlammable; + else if (tok == "isconductive") out |= F::IsConductive; + else if (tok == "isholycharged") out |= F::IsHolyCharged; + else if (tok == "iscursed") out |= F::IsCursed; + pos = end + 1; + } + return out; + } + 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]; + std::ifstream is(jsonPath); + if (!is) { + std::fprintf(stderr, + "import-wmat-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-wmat-json: parse error in %s: %s\n", + jsonPath.c_str(), ex.what()); + return 1; + } + wowee::pipeline::WoweeItemMaterial 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::WoweeItemMaterial::Entry e; + if (je.contains("materialId")) e.materialId = je["materialId"].get(); + if (je.contains("name")) e.name = je["name"].get(); + if (je.contains("description")) e.description = je["description"].get(); + uint8_t kind = wowee::pipeline::WoweeItemMaterial::Cloth; + if (je.contains("materialKind")) + kind = parseMaterialKindToken(je["materialKind"], kind); + else if (je.contains("materialKindName")) + kind = parseMaterialKindToken(je["materialKindName"], kind); + e.materialKind = kind; + uint8_t weight = wowee::pipeline::WoweeItemMaterial::Light; + if (je.contains("weightCategory")) + weight = parseWeightCategoryToken(je["weightCategory"], weight); + else if (je.contains("weightCategoryName")) + weight = parseWeightCategoryToken(je["weightCategoryName"], weight); + e.weightCategory = weight; + if (je.contains("foleySoundId")) e.foleySoundId = je["foleySoundId"].get(); + if (je.contains("impactSoundId")) e.impactSoundId = je["impactSoundId"].get(); + if (je.contains("materialFlags")) + e.materialFlags = parseMaterialFlagsField(je["materialFlags"]); + else if (je.contains("materialFlagsLabels")) + e.materialFlags = parseMaterialFlagsField(je["materialFlagsLabels"]); + if (je.contains("iconColorRGBA")) + e.iconColorRGBA = je["iconColorRGBA"].get(); + c.entries.push_back(e); + } + } + if (outBase.empty()) { + outBase = jsonPath; + const std::string suffix1 = ".wmat.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 = stripWmatExt(outBase); + if (!wowee::pipeline::WoweeItemMaterialLoader::save(c, outBase)) { + std::fprintf(stderr, + "import-wmat-json: failed to save %s.wmat\n", + outBase.c_str()); + return 1; + } + std::printf("Wrote %s.wmat\n", outBase.c_str()); + std::printf(" catalog : %s\n", c.name.c_str()); + std::printf(" materials : %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); @@ -269,6 +470,12 @@ bool handleItemMaterialsCatalog(int& i, int argc, char** argv, if (std::strcmp(argv[i], "--validate-wmat") == 0 && i + 1 < argc) { outRc = handleValidate(i, argc, argv); return true; } + if (std::strcmp(argv[i], "--export-wmat-json") == 0 && i + 1 < argc) { + outRc = handleExportJson(i, argc, argv); return true; + } + if (std::strcmp(argv[i], "--import-wmat-json") == 0 && i + 1 < argc) { + outRc = handleImportJson(i, argc, argv); return true; + } return false; }