mirror of
https://github.com/Kelsidavis/WoWee.git
synced 2026-05-11 03:23:51 +00:00
feat(editor): add WWFL JSON round-trip (--export/--import-wwfl-json)
Dual encoding for both WWFL enums via the readEnumField template: filterKind (int 0..5 OR 255 OR token "spam"/ "goldseller"/"allcaps"/"repeatchar"/"url"/"advertreward"/ "misc") and severity (int 0..3 OR token "warn"/"replace"/ "drop"/"mute"). caseSensitive accepts bool or int. pattern and replacement serialize as plain JSON strings — operators editing the JSON sidecar can hand-craft new moderation patterns without binary tooling. Strings are nlohmann::json-escaped on export and unescaped on import, preserving literal special characters byte-identically. All 3 presets (spam/caps/url) byte-identical roundtrip OK. CLI flag count 1239 -> 1241.
This commit is contained in:
parent
7d201cd6f3
commit
4be543a2ed
3 changed files with 187 additions and 0 deletions
|
|
@ -358,6 +358,7 @@ const char* const kArgRequired[] = {
|
|||
"--export-wtrd-json", "--import-wtrd-json",
|
||||
"--gen-wfl", "--gen-wfl-caps", "--gen-wfl-url",
|
||||
"--info-wwfl", "--validate-wwfl",
|
||||
"--export-wwfl-json", "--import-wwfl-json",
|
||||
"--gen-weather-temperate", "--gen-weather-arctic",
|
||||
"--gen-weather-desert", "--gen-weather-stormy",
|
||||
"--gen-zone-atmosphere",
|
||||
|
|
|
|||
|
|
@ -2387,6 +2387,10 @@ void printUsage(const char* argv0) {
|
|||
std::printf(" Print WWFL entries (id / kind / severity / case-sensitive / pattern -> replacement / name)\n");
|
||||
std::printf(" --validate-wwfl <wwfl-base> [--json]\n");
|
||||
std::printf(" Static checks: id+name+pattern required, filterKind 0..5 OR 255 Misc, severity 0..3, no duplicate filterIds, no two filters with same pattern (preprocessor dispatch ambiguity); warns on Replace severity with empty replacement (would silently lose match — use Drop explicitly if intended)\n");
|
||||
std::printf(" --export-wwfl-json <wwfl-base> [out.json]\n");
|
||||
std::printf(" Export binary .wwfl to a human-editable JSON sidecar (defaults to <base>.wwfl.json; emits both filterKind and severity as int + name string; pattern and replacement as plain strings)\n");
|
||||
std::printf(" --import-wwfl-json <json-path> [out-base]\n");
|
||||
std::printf(" Import a .wwfl.json sidecar back into binary .wwfl (filterKind int OR \"spam\"/\"goldseller\"/\"allcaps\"/\"repeatchar\"/\"url\"/\"advertreward\"/\"misc\"; severity int OR \"warn\"/\"replace\"/\"drop\"/\"mute\"; caseSensitive accepts bool OR int)\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(" --catalog-find <directory> <id> [--magic <WXXX>] [--json]\n");
|
||||
|
|
|
|||
|
|
@ -149,6 +149,182 @@ int handleInfo(int& i, int argc, char** argv) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int parseFilterKindToken(const std::string& s) {
|
||||
using F = wowee::pipeline::WoweeWordFilters;
|
||||
if (s == "spam") return F::Spam;
|
||||
if (s == "goldseller") return F::GoldSeller;
|
||||
if (s == "allcaps") return F::AllCaps;
|
||||
if (s == "repeatchar") return F::RepeatChar;
|
||||
if (s == "url") return F::URL;
|
||||
if (s == "advertreward") return F::AdvertReward;
|
||||
if (s == "misc") return F::Misc;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int parseSeverityToken(const std::string& s) {
|
||||
using F = wowee::pipeline::WoweeWordFilters;
|
||||
if (s == "warn") return F::Warn;
|
||||
if (s == "replace") return F::Replace;
|
||||
if (s == "drop") return F::Drop;
|
||||
if (s == "mute") return F::Mute;
|
||||
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-wwfl-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 = stripWwflExt(base);
|
||||
if (out.empty()) out = base + ".wwfl.json";
|
||||
if (!wowee::pipeline::WoweeWordFiltersLoader::exists(base)) {
|
||||
std::fprintf(stderr,
|
||||
"export-wwfl-json: WWFL not found: %s.wwfl\n",
|
||||
base.c_str());
|
||||
return 1;
|
||||
}
|
||||
auto c = wowee::pipeline::WoweeWordFiltersLoader::load(base);
|
||||
nlohmann::json j;
|
||||
j["magic"] = "WWFL";
|
||||
j["version"] = 1;
|
||||
j["name"] = c.name;
|
||||
nlohmann::json arr = nlohmann::json::array();
|
||||
for (const auto& e : c.entries) {
|
||||
arr.push_back({
|
||||
{"filterId", e.filterId},
|
||||
{"name", e.name},
|
||||
{"description", e.description},
|
||||
{"pattern", e.pattern},
|
||||
{"replacement", e.replacement},
|
||||
{"filterKind", e.filterKind},
|
||||
{"filterKindName", filterKindName(e.filterKind)},
|
||||
{"severity", e.severity},
|
||||
{"severityName", severityName(e.severity)},
|
||||
{"caseSensitive", e.caseSensitive != 0},
|
||||
{"iconColorRGBA", e.iconColorRGBA},
|
||||
});
|
||||
}
|
||||
j["entries"] = arr;
|
||||
std::ofstream os(out);
|
||||
if (!os) {
|
||||
std::fprintf(stderr,
|
||||
"export-wwfl-json: failed to open %s for write\n",
|
||||
out.c_str());
|
||||
return 1;
|
||||
}
|
||||
os << j.dump(2) << "\n";
|
||||
std::printf("Wrote %s (%zu filters)\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) == ".wwfl.json") {
|
||||
outBase.resize(outBase.size() - 10);
|
||||
} else {
|
||||
stripExt(outBase, ".json");
|
||||
stripExt(outBase, ".wwfl");
|
||||
}
|
||||
}
|
||||
std::ifstream is(in);
|
||||
if (!is) {
|
||||
std::fprintf(stderr,
|
||||
"import-wwfl-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-wwfl-json: JSON parse error: %s\n", ex.what());
|
||||
return 1;
|
||||
}
|
||||
wowee::pipeline::WoweeWordFilters c;
|
||||
c.name = j.value("name", std::string{});
|
||||
if (!j.contains("entries") || !j["entries"].is_array()) {
|
||||
std::fprintf(stderr,
|
||||
"import-wwfl-json: missing or non-array 'entries'\n");
|
||||
return 1;
|
||||
}
|
||||
for (const auto& je : j["entries"]) {
|
||||
wowee::pipeline::WoweeWordFilters::Entry e;
|
||||
e.filterId = je.value("filterId", 0u);
|
||||
e.name = je.value("name", std::string{});
|
||||
e.description = je.value("description", std::string{});
|
||||
e.pattern = je.value("pattern", std::string{});
|
||||
e.replacement = je.value("replacement", std::string{});
|
||||
if (!readEnumField(je, "filterKind", "filterKindName",
|
||||
parseFilterKindToken, "filterKind",
|
||||
e.filterId, e.filterKind)) return 1;
|
||||
if (!readEnumField(je, "severity", "severityName",
|
||||
parseSeverityToken, "severity",
|
||||
e.filterId, e.severity)) return 1;
|
||||
if (je.contains("caseSensitive")) {
|
||||
const auto& v = je["caseSensitive"];
|
||||
if (v.is_boolean())
|
||||
e.caseSensitive = v.get<bool>() ? 1 : 0;
|
||||
else if (v.is_number_integer())
|
||||
e.caseSensitive = static_cast<uint8_t>(
|
||||
v.get<int>() != 0 ? 1 : 0);
|
||||
}
|
||||
e.iconColorRGBA = je.value("iconColorRGBA", 0xFFFFFFFFu);
|
||||
c.entries.push_back(e);
|
||||
}
|
||||
if (!wowee::pipeline::WoweeWordFiltersLoader::save(c, outBase)) {
|
||||
std::fprintf(stderr,
|
||||
"import-wwfl-json: failed to save %s.wwfl\n",
|
||||
outBase.c_str());
|
||||
return 1;
|
||||
}
|
||||
std::printf("Wrote %s.wwfl (%zu filters)\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);
|
||||
|
|
@ -268,6 +444,12 @@ bool handleWordFiltersCatalog(int& i, int argc, char** argv,
|
|||
if (std::strcmp(argv[i], "--validate-wwfl") == 0 && i + 1 < argc) {
|
||||
outRc = handleValidate(i, argc, argv); return true;
|
||||
}
|
||||
if (std::strcmp(argv[i], "--export-wwfl-json") == 0 && i + 1 < argc) {
|
||||
outRc = handleExportJson(i, argc, argv); return true;
|
||||
}
|
||||
if (std::strcmp(argv[i], "--import-wwfl-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