mirror of
https://github.com/Kelsidavis/WoWee.git
synced 2026-05-11 11:33:52 +00:00
feat(editor): add WANV JSON round-trip (--export/--import-wanv-json)
Dual encoding for both WANV enums via the readEnumField template: eventKind (int 0..6 OR 255 OR token "holiday" / "anniversary" / "doublexp" / "doublehonor" / "petbattle" / "bgbonus" / "seasonalquest" / "misc") and recurrenceKind (int 0..3 OR token "yearly" / "monthly" / "weekly" / "oneoff"). The polymorphic startDay field (1..31 day-of-month for Yearly/Monthly/OneOff vs 0..6 weekday for Weekly) is serialized as a plain int — operators editing JSON need to know the recurrenceKind context to interpret the value, which the validator already enforces on import. All 3 presets (holidays / weekly bonus / anniversary) byte-identical roundtrip OK. CLI flag count 1274 -> 1276.
This commit is contained in:
parent
0df50f9f72
commit
3e14b7b5b1
3 changed files with 195 additions and 0 deletions
|
|
@ -179,6 +179,190 @@ int handleInfo(int& i, int argc, char** argv) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int parseEventKindToken(const std::string& s) {
|
||||
using A = wowee::pipeline::WoweeAnniversaryEvents;
|
||||
if (s == "holiday") return A::Holiday;
|
||||
if (s == "anniversary") return A::Anniversary;
|
||||
if (s == "doublexp") return A::DoubleXP;
|
||||
if (s == "doublehonor") return A::DoubleHonor;
|
||||
if (s == "petbattle") return A::PetBattleWeekend;
|
||||
if (s == "bgbonus") return A::BattlegroundBonus;
|
||||
if (s == "seasonalquest") return A::SeasonalQuest;
|
||||
if (s == "misc") return A::Misc;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int parseRecurrenceKindToken(const std::string& s) {
|
||||
using A = wowee::pipeline::WoweeAnniversaryEvents;
|
||||
if (s == "yearly") return A::Yearly;
|
||||
if (s == "monthly") return A::Monthly;
|
||||
if (s == "weekly") return A::Weekly;
|
||||
if (s == "oneoff") return A::OneOff;
|
||||
return -1;
|
||||
}
|
||||
|
||||
template <typename ParseFn>
|
||||
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<std::string>());
|
||||
if (parsed < 0) {
|
||||
std::fprintf(stderr,
|
||||
"import-wanv-json: unknown %s token "
|
||||
"'%s' on entry id=%u\n",
|
||||
label, v.get<std::string>().c_str(),
|
||||
entryId);
|
||||
return false;
|
||||
}
|
||||
outValue = static_cast<uint8_t>(parsed);
|
||||
return true;
|
||||
}
|
||||
if (v.is_number_integer()) {
|
||||
outValue = static_cast<uint8_t>(v.get<int>());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (je.contains(nameKey) && je[nameKey].is_string()) {
|
||||
int parsed = parseFn(je[nameKey].get<std::string>());
|
||||
if (parsed >= 0) {
|
||||
outValue = static_cast<uint8_t>(parsed);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
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 = stripWanvExt(base);
|
||||
if (out.empty()) out = base + ".wanv.json";
|
||||
if (!wowee::pipeline::WoweeAnniversaryEventsLoader::exists(
|
||||
base)) {
|
||||
std::fprintf(stderr,
|
||||
"export-wanv-json: WANV not found: %s.wanv\n",
|
||||
base.c_str());
|
||||
return 1;
|
||||
}
|
||||
auto c = wowee::pipeline::WoweeAnniversaryEventsLoader::load(
|
||||
base);
|
||||
nlohmann::json j;
|
||||
j["magic"] = "WANV";
|
||||
j["version"] = 1;
|
||||
j["name"] = c.name;
|
||||
nlohmann::json arr = nlohmann::json::array();
|
||||
for (const auto& e : c.entries) {
|
||||
arr.push_back({
|
||||
{"eventId", e.eventId},
|
||||
{"name", e.name},
|
||||
{"description", e.description},
|
||||
{"eventKind", e.eventKind},
|
||||
{"eventKindName", eventKindName(e.eventKind)},
|
||||
{"recurrenceKind", e.recurrenceKind},
|
||||
{"recurrenceKindName",
|
||||
recurrenceKindName(e.recurrenceKind)},
|
||||
{"startMonth", e.startMonth},
|
||||
{"startDay", e.startDay},
|
||||
{"durationDays", e.durationDays},
|
||||
{"payloadSpellId", e.payloadSpellId},
|
||||
{"payloadItemId", e.payloadItemId},
|
||||
{"iconColorRGBA", e.iconColorRGBA},
|
||||
});
|
||||
}
|
||||
j["entries"] = arr;
|
||||
std::ofstream os(out);
|
||||
if (!os) {
|
||||
std::fprintf(stderr,
|
||||
"export-wanv-json: failed to open %s for write\n",
|
||||
out.c_str());
|
||||
return 1;
|
||||
}
|
||||
os << j.dump(2) << "\n";
|
||||
std::printf("Wrote %s (%zu events)\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) == ".wanv.json") {
|
||||
outBase.resize(outBase.size() - 10);
|
||||
} else {
|
||||
stripExt(outBase, ".json");
|
||||
stripExt(outBase, ".wanv");
|
||||
}
|
||||
}
|
||||
std::ifstream is(in);
|
||||
if (!is) {
|
||||
std::fprintf(stderr,
|
||||
"import-wanv-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-wanv-json: JSON parse error: %s\n", ex.what());
|
||||
return 1;
|
||||
}
|
||||
wowee::pipeline::WoweeAnniversaryEvents c;
|
||||
c.name = j.value("name", std::string{});
|
||||
if (!j.contains("entries") || !j["entries"].is_array()) {
|
||||
std::fprintf(stderr,
|
||||
"import-wanv-json: missing or non-array 'entries'\n");
|
||||
return 1;
|
||||
}
|
||||
for (const auto& je : j["entries"]) {
|
||||
wowee::pipeline::WoweeAnniversaryEvents::Entry e;
|
||||
e.eventId = je.value("eventId", 0u);
|
||||
e.name = je.value("name", std::string{});
|
||||
e.description = je.value("description", std::string{});
|
||||
if (!readEnumField(je, "eventKind", "eventKindName",
|
||||
parseEventKindToken, "eventKind",
|
||||
e.eventId, e.eventKind)) return 1;
|
||||
if (!readEnumField(je, "recurrenceKind",
|
||||
"recurrenceKindName",
|
||||
parseRecurrenceKindToken,
|
||||
"recurrenceKind",
|
||||
e.eventId,
|
||||
e.recurrenceKind)) return 1;
|
||||
e.startMonth = static_cast<uint8_t>(
|
||||
je.value("startMonth", 1u));
|
||||
e.startDay = static_cast<uint8_t>(
|
||||
je.value("startDay", 1u));
|
||||
e.durationDays = static_cast<uint16_t>(
|
||||
je.value("durationDays", 7u));
|
||||
e.payloadSpellId = je.value("payloadSpellId", 0u);
|
||||
e.payloadItemId = je.value("payloadItemId", 0u);
|
||||
e.iconColorRGBA = je.value("iconColorRGBA", 0xFFFFFFFFu);
|
||||
c.entries.push_back(e);
|
||||
}
|
||||
if (!wowee::pipeline::WoweeAnniversaryEventsLoader::save(
|
||||
c, outBase)) {
|
||||
std::fprintf(stderr,
|
||||
"import-wanv-json: failed to save %s.wanv\n",
|
||||
outBase.c_str());
|
||||
return 1;
|
||||
}
|
||||
std::printf("Wrote %s.wanv (%zu events)\n",
|
||||
outBase.c_str(), c.entries.size());
|
||||
return 0;
|
||||
}
|
||||
|
||||
int handleValidate(int& i, int argc, char** argv) {
|
||||
std::string base = argv[++i];
|
||||
bool jsonOut = consumeJsonFlag(i, argc, argv);
|
||||
|
|
@ -329,6 +513,12 @@ bool handleAnniversaryEventsCatalog(int& i, int argc, char** argv,
|
|||
if (std::strcmp(argv[i], "--validate-wanv") == 0 && i + 1 < argc) {
|
||||
outRc = handleValidate(i, argc, argv); return true;
|
||||
}
|
||||
if (std::strcmp(argv[i], "--export-wanv-json") == 0 && i + 1 < argc) {
|
||||
outRc = handleExportJson(i, argc, argv); return true;
|
||||
}
|
||||
if (std::strcmp(argv[i], "--import-wanv-json") == 0 && i + 1 < argc) {
|
||||
outRc = handleImportJson(i, argc, argv); return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue