mirror of
https://github.com/Kelsidavis/WoWee.git
synced 2026-05-11 03:23:51 +00:00
feat(editor): add WMSP JSON round-trip (--export/--import-wmsp-json)
Dual encoding for all 4 WMSP enums (realmType / realmCategory / expansion / population) on import — each accepts int OR token string. New readEnumField helper template factors out the int-or-token coercion logic so the 4 enums share one parser body instead of repeating the same 12-line pattern four times. Helper reports the field label + entry id on parse failure so the operator knows exactly which token broke. Export adds a derived versionString "x.y.z" convenience field alongside the three uint8 components — purely informational, ignored on import (the int components are authoritative). gmOnly accepts bool OR int. All 3 presets (single/cluster/multi-expansion) byte- identical roundtrip OK. Token-form import smoke-tested with rppvp + beta + tbc + high (all 4 enums as strings in the same entry). CLI flag count 1124 -> 1126.
This commit is contained in:
parent
054f44e4aa
commit
4aa7b56e13
3 changed files with 244 additions and 0 deletions
|
|
@ -309,6 +309,7 @@ const char* const kArgRequired[] = {
|
|||
"--export-wcmg-json", "--import-wcmg-json",
|
||||
"--gen-msp", "--gen-msp-cluster", "--gen-msp-multi",
|
||||
"--info-wmsp", "--validate-wmsp",
|
||||
"--export-wmsp-json", "--import-wmsp-json",
|
||||
"--gen-weather-temperate", "--gen-weather-arctic",
|
||||
"--gen-weather-desert", "--gen-weather-stormy",
|
||||
"--gen-zone-atmosphere",
|
||||
|
|
|
|||
|
|
@ -2163,6 +2163,10 @@ void printUsage(const char* argv0) {
|
|||
std::printf(" Print WMSP entries (id / type / category / expansion / population / cap / GM-only / build / address / name)\n");
|
||||
std::printf(" --validate-wmsp <wmsp-base> [--json]\n");
|
||||
std::printf(" Static checks: id+name+address required, realmType in {0,1,4,6,8}, realmCategory 0..3, expansion 0..3, population 0..4, characterCap>0, no duplicate ids OR names; warns on no-port address, build<5000\n");
|
||||
std::printf(" --export-wmsp-json <wmsp-base> [out.json]\n");
|
||||
std::printf(" Export binary .wmsp to a human-editable JSON sidecar (defaults to <base>.wmsp.json; emits all 4 enums as both int AND name string + a versionString \"x.y.z\" convenience field)\n");
|
||||
std::printf(" --import-wmsp-json <json-path> [out-base]\n");
|
||||
std::printf(" Import a .wmsp.json sidecar back into binary .wmsp (realmType int OR \"normal\"/\"pvp\"/\"rp\"/\"rppvp\"/\"test\"; realmCategory \"public\"/\"private\"/\"beta\"/\"dev\"; expansion \"vanilla\"/\"tbc\"/\"wotlk\"/\"cata\"; population \"low\"/\"medium\"/\"high\"/\"full\"/\"locked\")\n");
|
||||
std::printf(" --catalog-pluck <wXXX-file> <id> [--json]\n");
|
||||
std::printf(" Extract one entry by id from any registered catalog format. Auto-detects magic, dispatches to the per-format --info-* handler internally, then prints just the matching entry. Primary-key field is auto-detected (first *Id field, or first numeric)\n");
|
||||
std::printf(" --gen-weather-temperate <wow-base> [zoneName]\n");
|
||||
|
|
|
|||
|
|
@ -190,6 +190,239 @@ int handleInfo(int& i, int argc, char** argv) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
// Token parsers for the four WMSP enums. Each returns -1
|
||||
// if the token doesn't match any known value.
|
||||
int parseRealmTypeToken(const std::string& s) {
|
||||
using R = wowee::pipeline::WoweeRealmList;
|
||||
if (s == "normal") return R::Normal;
|
||||
if (s == "pvp") return R::PvP;
|
||||
if (s == "rp") return R::RP;
|
||||
if (s == "rppvp") return R::RPPvP;
|
||||
if (s == "test") return R::Test;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int parseRealmCategoryToken(const std::string& s) {
|
||||
using R = wowee::pipeline::WoweeRealmList;
|
||||
if (s == "public") return R::Public;
|
||||
if (s == "private") return R::Private;
|
||||
if (s == "beta") return R::Beta;
|
||||
if (s == "dev") return R::Dev;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int parseExpansionToken(const std::string& s) {
|
||||
using R = wowee::pipeline::WoweeRealmList;
|
||||
if (s == "vanilla") return R::Vanilla;
|
||||
if (s == "tbc") return R::TBC;
|
||||
if (s == "wotlk") return R::WotLK;
|
||||
if (s == "cata") return R::Cata;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int parsePopulationToken(const std::string& s) {
|
||||
using R = wowee::pipeline::WoweeRealmList;
|
||||
if (s == "low") return R::Low;
|
||||
if (s == "medium") return R::Medium;
|
||||
if (s == "high") return R::High;
|
||||
if (s == "full") return R::Full;
|
||||
if (s == "locked") return R::Locked;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Generic "int OR token string" coercion helper. Returns
|
||||
// true if a value was successfully extracted; assigns
|
||||
// into outValue. Reports parse failures via stderr with
|
||||
// the supplied label so the operator knows which field of
|
||||
// which entry failed.
|
||||
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-wmsp-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; // field absent — leave outValue at default
|
||||
}
|
||||
|
||||
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 = stripWmspExt(base);
|
||||
if (out.empty()) out = base + ".wmsp.json";
|
||||
if (!wowee::pipeline::WoweeRealmListLoader::exists(base)) {
|
||||
std::fprintf(stderr,
|
||||
"export-wmsp-json: WMSP not found: %s.wmsp\n",
|
||||
base.c_str());
|
||||
return 1;
|
||||
}
|
||||
auto c = wowee::pipeline::WoweeRealmListLoader::load(base);
|
||||
nlohmann::json j;
|
||||
j["magic"] = "WMSP";
|
||||
j["version"] = 1;
|
||||
j["name"] = c.name;
|
||||
nlohmann::json arr = nlohmann::json::array();
|
||||
for (const auto& e : c.entries) {
|
||||
char ver[24];
|
||||
std::snprintf(ver, sizeof(ver), "%u.%u.%u",
|
||||
e.versionMajor, e.versionMinor,
|
||||
e.versionPatch);
|
||||
arr.push_back({
|
||||
{"realmId", e.realmId},
|
||||
{"name", e.name},
|
||||
{"description", e.description},
|
||||
{"address", e.address},
|
||||
{"realmType", e.realmType},
|
||||
{"realmTypeName", realmTypeName(e.realmType)},
|
||||
{"realmCategory", e.realmCategory},
|
||||
{"realmCategoryName",
|
||||
realmCategoryName(e.realmCategory)},
|
||||
{"expansion", e.expansion},
|
||||
{"expansionName", expansionName(e.expansion)},
|
||||
{"population", e.population},
|
||||
{"populationName", populationName(e.population)},
|
||||
{"characterCap", e.characterCap},
|
||||
{"gmOnly", e.gmOnly != 0},
|
||||
{"timezone", e.timezone},
|
||||
{"versionMajor", e.versionMajor},
|
||||
{"versionMinor", e.versionMinor},
|
||||
{"versionPatch", e.versionPatch},
|
||||
{"versionString", ver},
|
||||
{"buildNumber", e.buildNumber},
|
||||
{"iconColorRGBA", e.iconColorRGBA},
|
||||
});
|
||||
}
|
||||
j["entries"] = arr;
|
||||
std::ofstream os(out);
|
||||
if (!os) {
|
||||
std::fprintf(stderr,
|
||||
"export-wmsp-json: failed to open %s for write\n",
|
||||
out.c_str());
|
||||
return 1;
|
||||
}
|
||||
os << j.dump(2) << "\n";
|
||||
std::printf("Wrote %s (%zu realms)\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) == ".wmsp.json") {
|
||||
outBase.resize(outBase.size() - 10);
|
||||
} else {
|
||||
stripExt(outBase, ".json");
|
||||
stripExt(outBase, ".wmsp");
|
||||
}
|
||||
}
|
||||
std::ifstream is(in);
|
||||
if (!is) {
|
||||
std::fprintf(stderr,
|
||||
"import-wmsp-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-wmsp-json: JSON parse error: %s\n", ex.what());
|
||||
return 1;
|
||||
}
|
||||
wowee::pipeline::WoweeRealmList c;
|
||||
c.name = j.value("name", std::string{});
|
||||
if (!j.contains("entries") || !j["entries"].is_array()) {
|
||||
std::fprintf(stderr,
|
||||
"import-wmsp-json: missing or non-array 'entries'\n");
|
||||
return 1;
|
||||
}
|
||||
for (const auto& je : j["entries"]) {
|
||||
wowee::pipeline::WoweeRealmList::Entry e;
|
||||
e.realmId = je.value("realmId", 0u);
|
||||
e.name = je.value("name", std::string{});
|
||||
e.description = je.value("description", std::string{});
|
||||
e.address = je.value("address", std::string{});
|
||||
if (!readEnumField(je, "realmType", "realmTypeName",
|
||||
parseRealmTypeToken, "realmType",
|
||||
e.realmId, e.realmType)) return 1;
|
||||
if (!readEnumField(je, "realmCategory",
|
||||
"realmCategoryName",
|
||||
parseRealmCategoryToken,
|
||||
"realmCategory",
|
||||
e.realmId, e.realmCategory)) return 1;
|
||||
if (!readEnumField(je, "expansion", "expansionName",
|
||||
parseExpansionToken, "expansion",
|
||||
e.realmId, e.expansion)) return 1;
|
||||
if (!readEnumField(je, "population", "populationName",
|
||||
parsePopulationToken, "population",
|
||||
e.realmId, e.population)) return 1;
|
||||
e.characterCap = static_cast<uint8_t>(
|
||||
je.value("characterCap", 10u));
|
||||
if (je.contains("gmOnly")) {
|
||||
const auto& g = je["gmOnly"];
|
||||
if (g.is_boolean())
|
||||
e.gmOnly = g.get<bool>() ? 1 : 0;
|
||||
else if (g.is_number_integer())
|
||||
e.gmOnly = static_cast<uint8_t>(
|
||||
g.get<int>() != 0 ? 1 : 0);
|
||||
}
|
||||
e.timezone = static_cast<uint8_t>(
|
||||
je.value("timezone", 8u));
|
||||
e.versionMajor = static_cast<uint8_t>(
|
||||
je.value("versionMajor", 3u));
|
||||
e.versionMinor = static_cast<uint8_t>(
|
||||
je.value("versionMinor", 3u));
|
||||
e.versionPatch = static_cast<uint8_t>(
|
||||
je.value("versionPatch", 5u));
|
||||
e.buildNumber = je.value("buildNumber", 12340u);
|
||||
e.iconColorRGBA = je.value("iconColorRGBA", 0xFFFFFFFFu);
|
||||
c.entries.push_back(e);
|
||||
}
|
||||
if (!wowee::pipeline::WoweeRealmListLoader::save(c, outBase)) {
|
||||
std::fprintf(stderr,
|
||||
"import-wmsp-json: failed to save %s.wmsp\n",
|
||||
outBase.c_str());
|
||||
return 1;
|
||||
}
|
||||
std::printf("Wrote %s.wmsp (%zu realms)\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);
|
||||
|
|
@ -330,6 +563,12 @@ bool handleRealmListCatalog(int& i, int argc, char** argv,
|
|||
if (std::strcmp(argv[i], "--validate-wmsp") == 0 && i + 1 < argc) {
|
||||
outRc = handleValidate(i, argc, argv); return true;
|
||||
}
|
||||
if (std::strcmp(argv[i], "--export-wmsp-json") == 0 && i + 1 < argc) {
|
||||
outRc = handleExportJson(i, argc, argv); return true;
|
||||
}
|
||||
if (std::strcmp(argv[i], "--import-wmsp-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