Kelsidavis-WoWee/tools/editor/cli_crafting_recipes_catalog.cpp
Kelsi 6c77419633 feat(editor): WCRA JSON round-trip closure
Adds --export-wcra-json / --import-wcra-json. Variable-length
reagent arrays serialize as JSON object array of {itemId, count}
allowing hand-edits of recipe formulas. tradeSkillName field on
export is informational (tradeSkillId int is authoritative).
All 3 presets (alchemy/engineering/blacksmithing) byte-identical
binary roundtrip OK including the 5-reagent Target Dummy
recipe.

Live-tested perpetual-motion validator: hand-mutated Minor
Healing Potion (recipeId 1, produces itemId 118) to add itemId
118 to its reagents. Validator correctly errored: "reagent
itemId equals producedItemId=118 — recipe consumes what it
makes (perpetual-motion bug)" — catches a class of crafting
bugs that would silently let players duplicate items.

CLI flag count 1398 -> 1400.
2026-05-10 04:32:02 -07:00

468 lines
16 KiB
C++

#include "cli_crafting_recipes_catalog.hpp"
#include "cli_arg_parse.hpp"
#include "cli_box_emitter.hpp"
#include "pipeline/wowee_crafting_recipes.hpp"
#include <nlohmann/json.hpp>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <set>
#include <string>
#include <vector>
namespace wowee {
namespace editor {
namespace cli {
namespace {
std::string stripWcraExt(std::string base) {
stripExt(base, ".wcra");
return base;
}
const char* tradeSkillName(uint16_t s) {
switch (s) {
case 164: return "Blacksmithing";
case 165: return "LeatherWorking";
case 171: return "Alchemy";
case 197: return "Tailoring";
case 202: return "Engineering";
case 333: return "Enchanting";
case 185: return "Cooking";
case 129: return "FirstAid";
default: return "?";
}
}
bool saveOrError(const wowee::pipeline::WoweeCraftingRecipes& c,
const std::string& base, const char* cmd) {
if (!wowee::pipeline::WoweeCraftingRecipesLoader::save(c, base)) {
std::fprintf(stderr, "%s: failed to save %s.wcra\n",
cmd, base.c_str());
return false;
}
return true;
}
void printGenSummary(const wowee::pipeline::WoweeCraftingRecipes& c,
const std::string& base) {
std::printf("Wrote %s.wcra\n", base.c_str());
std::printf(" catalog : %s\n", c.name.c_str());
std::printf(" recipes : %zu\n", c.entries.size());
}
int handleGenAlchemy(int& i, int argc, char** argv) {
std::string base = argv[++i];
std::string name = "AlchemyPotions";
if (parseOptArg(i, argc, argv)) name = argv[++i];
base = stripWcraExt(base);
auto c = wowee::pipeline::WoweeCraftingRecipesLoader::
makeAlchemyPotions(name);
if (!saveOrError(c, base, "gen-cra-alchemy")) return 1;
printGenSummary(c, base);
return 0;
}
int handleGenEngineering(int& i, int argc, char** argv) {
std::string base = argv[++i];
std::string name = "EngineeringRecipes";
if (parseOptArg(i, argc, argv)) name = argv[++i];
base = stripWcraExt(base);
auto c = wowee::pipeline::WoweeCraftingRecipesLoader::
makeEngineering(name);
if (!saveOrError(c, base, "gen-cra-engineering")) return 1;
printGenSummary(c, base);
return 0;
}
int handleGenBlacksmithing(int& i, int argc, char** argv) {
std::string base = argv[++i];
std::string name = "BlacksmithingRecipes";
if (parseOptArg(i, argc, argv)) name = argv[++i];
base = stripWcraExt(base);
auto c = wowee::pipeline::WoweeCraftingRecipesLoader::
makeBlacksmithing(name);
if (!saveOrError(c, base, "gen-cra-blacksmithing")) return 1;
printGenSummary(c, base);
return 0;
}
int handleInfo(int& i, int argc, char** argv) {
std::string base = argv[++i];
bool jsonOut = consumeJsonFlag(i, argc, argv);
base = stripWcraExt(base);
if (!wowee::pipeline::WoweeCraftingRecipesLoader::exists(base)) {
std::fprintf(stderr, "WCRA not found: %s.wcra\n",
base.c_str());
return 1;
}
auto c = wowee::pipeline::WoweeCraftingRecipesLoader::load(base);
if (jsonOut) {
nlohmann::json j;
j["wcra"] = base + ".wcra";
j["name"] = c.name;
j["count"] = c.entries.size();
nlohmann::json arr = nlohmann::json::array();
for (const auto& e : c.entries) {
nlohmann::json reagents = nlohmann::json::array();
for (const auto& r : e.reagents) {
reagents.push_back({
{"itemId", r.itemId},
{"count", r.count},
});
}
arr.push_back({
{"recipeId", e.recipeId},
{"spellId", e.spellId},
{"name", e.name},
{"tradeSkillId", e.tradeSkillId},
{"tradeSkillName",
tradeSkillName(e.tradeSkillId)},
{"requiredSkillLevel", e.requiredSkillLevel},
{"producedItemId", e.producedItemId},
{"producedCount", e.producedCount},
{"categoryId", e.categoryId},
{"learnedFromItemId", e.learnedFromItemId},
{"reagents", reagents},
});
}
j["entries"] = arr;
std::printf("%s\n", j.dump(2).c_str());
return 0;
}
std::printf("WCRA: %s.wcra\n", base.c_str());
std::printf(" catalog : %s\n", c.name.c_str());
std::printf(" recipes : %zu\n", c.entries.size());
if (c.entries.empty()) return 0;
std::printf(" id spell trade-skill req produced count reag name\n");
for (const auto& e : c.entries) {
std::printf(" %4u %7u %-15s %3u %8u %5u %4zu %s\n",
e.recipeId, e.spellId,
tradeSkillName(e.tradeSkillId),
e.requiredSkillLevel,
e.producedItemId, e.producedCount,
e.reagents.size(), e.name.c_str());
}
return 0;
}
int handleValidate(int& i, int argc, char** argv) {
std::string base = argv[++i];
bool jsonOut = consumeJsonFlag(i, argc, argv);
base = stripWcraExt(base);
if (!wowee::pipeline::WoweeCraftingRecipesLoader::exists(base)) {
std::fprintf(stderr,
"validate-wcra: WCRA not found: %s.wcra\n",
base.c_str());
return 1;
}
auto c = wowee::pipeline::WoweeCraftingRecipesLoader::load(base);
std::vector<std::string> errors;
std::vector<std::string> warnings;
if (c.entries.empty()) {
warnings.push_back("catalog has zero entries");
}
std::set<uint32_t> idsSeen;
std::set<uint32_t> spellIdsSeen;
for (size_t k = 0; k < c.entries.size(); ++k) {
const auto& e = c.entries[k];
std::string ctx = "entry " + std::to_string(k) +
" (id=" + std::to_string(e.recipeId);
if (!e.name.empty()) ctx += " " + e.name;
ctx += ")";
if (e.recipeId == 0)
errors.push_back(ctx + ": recipeId is 0");
if (e.spellId == 0)
errors.push_back(ctx +
": spellId is 0 (no cast spell)");
if (e.name.empty())
errors.push_back(ctx + ": name is empty");
if (e.tradeSkillId == 0) {
errors.push_back(ctx +
": tradeSkillId is 0 (recipe must "
"belong to a trade skill)");
}
if (e.producedItemId == 0)
errors.push_back(ctx +
": producedItemId is 0 (recipe produces "
"nothing)");
if (e.producedCount == 0) {
errors.push_back(ctx +
": producedCount is 0 (recipe yields 0 "
"items)");
}
// Vanilla skill cap is 300. Skill > 300 is
// valid for TBC (375) and WotLK (450) — only
// warn if absurdly above all expansions.
if (e.requiredSkillLevel > 450) {
warnings.push_back(ctx +
": requiredSkillLevel=" +
std::to_string(e.requiredSkillLevel) +
" exceeds WotLK cap of 450 (likely typo)");
}
// Empty reagent list: technically valid (some
// alchemy transmutes have only catalysts) but
// unusual. Warn.
if (e.reagents.empty()) {
warnings.push_back(ctx +
": no reagents — recipe is "
"free-to-craft (unusual; verify "
"intentional)");
}
// Per-reagent checks: zero itemId or zero
// count is a typo.
std::set<uint32_t> reagentItems;
for (size_t r = 0; r < e.reagents.size(); ++r) {
const auto& reagent = e.reagents[r];
if (reagent.itemId == 0) {
errors.push_back(ctx +
": reagent[" + std::to_string(r) +
"].itemId is 0");
}
if (reagent.count == 0) {
errors.push_back(ctx +
": reagent[" + std::to_string(r) +
"].count is 0");
}
// Same itemId listed twice in the reagent
// array — should be merged into one
// reagent with summed count.
if (reagent.itemId != 0 &&
!reagentItems.insert(reagent.itemId).second) {
warnings.push_back(ctx +
": reagent itemId " +
std::to_string(reagent.itemId) +
" appears twice — should be merged "
"into single entry with summed count");
}
}
// Self-reagent: a recipe consuming its own
// produced item is a perpetual-motion bug.
for (const auto& reagent : e.reagents) {
if (reagent.itemId == e.producedItemId &&
reagent.itemId != 0) {
errors.push_back(ctx +
": reagent itemId equals "
"producedItemId=" +
std::to_string(e.producedItemId) +
" — recipe consumes what it makes "
"(perpetual-motion bug)");
}
}
// Duplicate spellId — recipe-cast handler
// would resolve ambiguously.
if (e.spellId != 0 &&
!spellIdsSeen.insert(e.spellId).second) {
errors.push_back(ctx +
": duplicate spellId " +
std::to_string(e.spellId) +
" — two recipes would respond to the "
"same cast");
}
if (!idsSeen.insert(e.recipeId).second) {
errors.push_back(ctx + ": duplicate recipeId");
}
}
bool ok = errors.empty();
if (jsonOut) {
nlohmann::json j;
j["wcra"] = base + ".wcra";
j["ok"] = ok;
j["errors"] = errors;
j["warnings"] = warnings;
std::printf("%s\n", j.dump(2).c_str());
return ok ? 0 : 1;
}
std::printf("validate-wcra: %s.wcra\n", base.c_str());
if (ok && warnings.empty()) {
std::printf(" OK — %zu recipes, all recipeIds + "
"spellIds unique, non-zero spellId/"
"tradeSkillId/producedItemId/"
"producedCount, no zero-item/zero-count "
"reagents, no duplicate reagent itemIds "
"in same recipe, no self-reagent\n",
c.entries.size());
return 0;
}
if (!warnings.empty()) {
std::printf(" warnings (%zu):\n", warnings.size());
for (const auto& w : warnings)
std::printf(" - %s\n", w.c_str());
}
if (!errors.empty()) {
std::printf(" ERRORS (%zu):\n", errors.size());
for (const auto& e : errors)
std::printf(" - %s\n", e.c_str());
}
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 = stripWcraExt(base);
if (out.empty()) out = base + ".wcra.json";
if (!wowee::pipeline::WoweeCraftingRecipesLoader::exists(base)) {
std::fprintf(stderr,
"export-wcra-json: WCRA not found: %s.wcra\n",
base.c_str());
return 1;
}
auto c = wowee::pipeline::WoweeCraftingRecipesLoader::load(base);
nlohmann::json j;
j["magic"] = "WCRA";
j["version"] = 1;
j["name"] = c.name;
nlohmann::json arr = nlohmann::json::array();
for (const auto& e : c.entries) {
nlohmann::json reagents = nlohmann::json::array();
for (const auto& r : e.reagents) {
reagents.push_back({
{"itemId", r.itemId},
{"count", r.count},
});
}
arr.push_back({
{"recipeId", e.recipeId},
{"spellId", e.spellId},
{"name", e.name},
{"tradeSkillId", e.tradeSkillId},
{"tradeSkillName",
tradeSkillName(e.tradeSkillId)},
{"requiredSkillLevel", e.requiredSkillLevel},
{"producedItemId", e.producedItemId},
{"producedCount", e.producedCount},
{"categoryId", e.categoryId},
{"learnedFromItemId", e.learnedFromItemId},
{"reagents", reagents},
});
}
j["entries"] = arr;
std::ofstream os(out);
if (!os) {
std::fprintf(stderr,
"export-wcra-json: failed to open %s for write\n",
out.c_str());
return 1;
}
os << j.dump(2) << "\n";
std::printf("Wrote %s (%zu recipes)\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) == ".wcra.json") {
outBase.resize(outBase.size() - 10);
} else {
stripExt(outBase, ".json");
stripExt(outBase, ".wcra");
}
}
std::ifstream is(in);
if (!is) {
std::fprintf(stderr,
"import-wcra-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-wcra-json: JSON parse error: %s\n", ex.what());
return 1;
}
wowee::pipeline::WoweeCraftingRecipes c;
c.name = j.value("name", std::string{});
if (!j.contains("entries") || !j["entries"].is_array()) {
std::fprintf(stderr,
"import-wcra-json: missing or non-array 'entries'\n");
return 1;
}
for (const auto& je : j["entries"]) {
wowee::pipeline::WoweeCraftingRecipes::Entry e;
e.recipeId = je.value("recipeId", 0u);
e.spellId = je.value("spellId", 0u);
e.name = je.value("name", std::string{});
e.tradeSkillId = static_cast<uint16_t>(
je.value("tradeSkillId", 0));
e.requiredSkillLevel = static_cast<uint16_t>(
je.value("requiredSkillLevel", 0));
e.producedItemId = je.value("producedItemId", 0u);
e.producedCount = static_cast<uint16_t>(
je.value("producedCount", 1));
e.categoryId = static_cast<uint16_t>(
je.value("categoryId", 0));
e.learnedFromItemId = je.value("learnedFromItemId", 0u);
if (je.contains("reagents") &&
je["reagents"].is_array()) {
for (const auto& r : je["reagents"]) {
wowee::pipeline::WoweeCraftingRecipes::
Reagent reagent;
reagent.itemId = r.value("itemId", 0u);
reagent.count = r.value("count", 0u);
e.reagents.push_back(reagent);
}
}
c.entries.push_back(e);
}
if (!wowee::pipeline::WoweeCraftingRecipesLoader::save(c, outBase)) {
std::fprintf(stderr,
"import-wcra-json: failed to save %s.wcra\n",
outBase.c_str());
return 1;
}
std::printf("Wrote %s.wcra (%zu recipes)\n",
outBase.c_str(), c.entries.size());
return 0;
}
} // namespace
bool handleCraftingRecipesCatalog(int& i, int argc, char** argv,
int& outRc) {
if (std::strcmp(argv[i], "--gen-cra-alchemy") == 0 &&
i + 1 < argc) {
outRc = handleGenAlchemy(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--gen-cra-engineering") == 0 &&
i + 1 < argc) {
outRc = handleGenEngineering(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--gen-cra-blacksmithing") == 0 &&
i + 1 < argc) {
outRc = handleGenBlacksmithing(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--info-wcra") == 0 && i + 1 < argc) {
outRc = handleInfo(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--validate-wcra") == 0 &&
i + 1 < argc) {
outRc = handleValidate(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--export-wcra-json") == 0 &&
i + 1 < argc) {
outRc = handleExportJson(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--import-wcra-json") == 0 &&
i + 1 < argc) {
outRc = handleImportJson(i, argc, argv); return true;
}
return false;
}
} // namespace cli
} // namespace editor
} // namespace wowee