refactor(editor): extract audit family into cli_audits.cpp

Continues the modularization. Moves the four audit handlers
(--validate-zone-pack, --validate-project-packs, --info-zone-deps,
--info-project-deps) into their own file using the same
handle<Family>(int& i, int argc, char** argv, int& outRc) pattern.

Side-cleanup: the two project-scope audits had identical
subprocess-fanout structure (enumerate zones → run per-zone
command → tally PASS/FAIL → print summary). Consolidated that
into a shared runPerZoneAudit helper. Saves ~80 lines of
duplicated dispatch code.

main.cpp drops 28,070 → 27,736 lines (-334). Audit family is
fully self-contained (~330 lines), behavior unchanged
(verified all 4 commands against existing test zones).
This commit is contained in:
Kelsi 2026-05-08 17:12:10 -07:00
parent 774dab9330
commit ac17d04f8c
4 changed files with 376 additions and 338 deletions

View file

@ -1,6 +1,7 @@
#include "editor_app.hpp"
#include "cli_gen_audio.hpp"
#include "cli_zone_packs.hpp"
#include "cli_audits.hpp"
#include "content_pack.hpp"
#include "npc_spawner.hpp"
#include "object_placer.hpp"
@ -1369,6 +1370,9 @@ int main(int argc, char* argv[]) {
if (wowee::editor::cli::handleZonePacks(i, argc, argv, outRc)) {
return outRc;
}
if (wowee::editor::cli::handleAudits(i, argc, argv, outRc)) {
return outRc;
}
}
if (std::strcmp(argv[i], "--data") == 0 && i + 1 < argc) {
dataPath = argv[++i];
@ -14482,154 +14486,6 @@ int main(int argc, char* argv[]) {
totalAssets,
static_cast<unsigned long long>(totalBytes));
return 0;
} else if (std::strcmp(argv[i], "--info-zone-deps") == 0 && i + 1 < argc) {
// Broken-reference audit: walk every WOM in the zone,
// collect its texturePaths, normalize them, and check
// whether each path exists relative to the zone dir.
// Reports any reference that does NOT resolve to a real
// file. Catches "WOM was added but its texture wasn't
// copied into textures/" mistakes before runtime.
std::string zoneDir = argv[++i];
bool jsonOut = (i + 1 < argc &&
std::strcmp(argv[i + 1], "--json") == 0);
if (jsonOut) i++;
namespace fs = std::filesystem;
if (!fs::exists(zoneDir + "/zone.json")) {
std::fprintf(stderr,
"info-zone-deps: %s has no zone.json\n", zoneDir.c_str());
return 1;
}
// For each WOM, list (wom path, texture path, exists?).
// A texture path resolves if any of the candidate paths
// resolves: as-is, relative to zone dir, relative to
// zone/textures/, or with the basename matched in
// textures/.
struct DepRef {
std::string womPath;
std::string texPath;
bool exists;
};
std::vector<DepRef> refs;
std::error_code ec;
for (const auto& e : fs::recursive_directory_iterator(zoneDir, ec)) {
if (!e.is_regular_file()) continue;
if (e.path().extension() != ".wom") continue;
std::string womRel = fs::relative(e.path(), zoneDir).string();
std::string base = e.path().string();
base = base.substr(0, base.size() - 4);
auto wom = wowee::pipeline::WoweeModelLoader::load(base);
for (const auto& tp : wom.texturePaths) {
if (tp.empty()) continue;
bool found = false;
fs::path candidates[4] = {
fs::path(tp),
fs::path(zoneDir) / tp,
fs::path(zoneDir) / "textures" / fs::path(tp).filename(),
e.path().parent_path() / fs::path(tp).filename(),
};
for (const auto& c : candidates) {
if (fs::exists(c, ec) && fs::is_regular_file(c, ec)) {
found = true;
break;
}
}
refs.push_back({womRel, tp, found});
}
}
std::sort(refs.begin(), refs.end(),
[](const DepRef& a, const DepRef& b) {
if (a.exists != b.exists) return !a.exists;
if (a.womPath != b.womPath) return a.womPath < b.womPath;
return a.texPath < b.texPath;
});
int total = static_cast<int>(refs.size());
int missing = 0;
for (const auto& r : refs) if (!r.exists) ++missing;
if (jsonOut) {
nlohmann::json j;
j["zone"] = zoneDir;
j["totalRefs"] = total;
j["missingRefs"] = missing;
nlohmann::json arr = nlohmann::json::array();
for (const auto& r : refs) {
arr.push_back({
{"wom", r.womPath},
{"texture", r.texPath},
{"exists", r.exists},
});
}
j["refs"] = arr;
std::printf("%s\n", j.dump(2).c_str());
return missing == 0 ? 0 : 1;
}
std::printf("Zone deps: %s\n", zoneDir.c_str());
std::printf(" total refs : %d\n", total);
std::printf(" missing refs : %d\n", missing);
if (refs.empty()) {
std::printf(" *no texture references in any WOM*\n");
return 0;
}
std::printf("\n exists WOM texture\n");
for (const auto& r : refs) {
std::printf(" %-6s %-35s %s\n",
r.exists ? "yes" : "NO",
r.womPath.c_str(),
r.texPath.c_str());
}
std::printf("\n %s\n", missing == 0
? "PASS — all texture references resolve"
: "FAIL — missing references above");
return missing == 0 ? 0 : 1;
} else if (std::strcmp(argv[i], "--info-project-deps") == 0 && i + 1 < argc) {
// Run --info-zone-deps across every zone in a project
// and roll up per-zone PASS/FAIL plus a project total.
// Designed as a CI gate: exits non-zero if any zone has
// a broken texture reference. Lighter than re-implementing
// the dep walk here — defers to the per-zone command via
// subprocess so the resolution logic stays consistent.
std::string projectDir = argv[++i];
namespace fs = std::filesystem;
if (!fs::exists(projectDir) || !fs::is_directory(projectDir)) {
std::fprintf(stderr,
"info-project-deps: %s is not a directory\n",
projectDir.c_str());
return 1;
}
std::vector<std::string> zones;
for (const auto& entry : fs::directory_iterator(projectDir)) {
if (!entry.is_directory()) continue;
if (!fs::exists(entry.path() / "zone.json")) continue;
zones.push_back(entry.path().string());
}
std::sort(zones.begin(), zones.end());
if (zones.empty()) {
std::fprintf(stderr,
"info-project-deps: %s contains no zones\n",
projectDir.c_str());
return 1;
}
std::string self = (argc > 0) ? argv[0] : "wowee_editor";
int passed = 0, failed = 0;
std::printf("Project deps: %s\n", projectDir.c_str());
std::printf(" zones: %zu\n\n", zones.size());
for (const auto& z : zones) {
std::string cmd = "\"" + self + "\" --info-zone-deps \"" +
z + "\" > /dev/null 2>&1";
int rc = std::system(cmd.c_str());
std::string name = fs::path(z).filename().string();
if (rc == 0) {
++passed;
std::printf(" PASS %s\n", name.c_str());
} else {
++failed;
std::printf(" FAIL %s\n", name.c_str());
}
}
std::printf("\n Total: %d passed, %d failed\n", passed, failed);
std::printf(" %s\n",
failed == 0 ? "PROJECT PASS"
: "PROJECT FAIL — re-run --info-zone-deps on FAILing zones for detail");
return failed == 0 ? 0 : 1;
} else if (std::strcmp(argv[i], "--info-project-summary") == 0 && i + 1 < argc) {
// Project-wide companion to --info-zone-summary. Walks
// every zone in <projectDir> and reports a per-zone
@ -15045,196 +14901,6 @@ int main(int argc, char* argv[]) {
std::printf(" total bytes : %llu\n",
static_cast<unsigned long long>(totalBytes));
return 0;
} else if (std::strcmp(argv[i], "--validate-zone-pack") == 0 && i + 1 < argc) {
// Audit a zone's open-format asset pack. Reports counts
// and total bytes per category (textures/, meshes/,
// audio/) plus any malformed WOMs or invalid WAVs.
// Exit code 1 if any check fails — useful in CI to
// gate that gen-zone-starter-pack output is healthy.
std::string zoneDir = argv[++i];
bool jsonOut = (i + 1 < argc &&
std::strcmp(argv[i + 1], "--json") == 0);
if (jsonOut) i++;
namespace fs = std::filesystem;
if (!fs::exists(zoneDir + "/zone.json")) {
std::fprintf(stderr,
"validate-zone-pack: %s has no zone.json\n", zoneDir.c_str());
return 1;
}
struct CatStats {
int count = 0;
uint64_t bytes = 0;
int invalid = 0;
std::vector<std::string> invalidPaths;
};
CatStats tex, mesh, audio;
std::error_code ec;
// Textures: PNGs under textures/
fs::path texDir = fs::path(zoneDir) / "textures";
if (fs::exists(texDir)) {
for (const auto& e : fs::recursive_directory_iterator(texDir, ec)) {
if (!e.is_regular_file()) continue;
if (e.path().extension() != ".png") continue;
tex.count++;
tex.bytes += e.file_size();
// Quick PNG signature check (8 bytes)
FILE* f = std::fopen(e.path().c_str(), "rb");
if (f) {
unsigned char sig[8];
bool ok = (std::fread(sig, 1, 8, f) == 8 &&
sig[0] == 0x89 && sig[1] == 'P' &&
sig[2] == 'N' && sig[3] == 'G');
std::fclose(f);
if (!ok) {
tex.invalid++;
tex.invalidPaths.push_back(
fs::relative(e.path(), zoneDir).string());
}
}
}
}
// Meshes: WOMs under meshes/ — load & sanity check
fs::path meshDir = fs::path(zoneDir) / "meshes";
if (fs::exists(meshDir)) {
for (const auto& e : fs::recursive_directory_iterator(meshDir, ec)) {
if (!e.is_regular_file()) continue;
if (e.path().extension() != ".wom") continue;
mesh.count++;
mesh.bytes += e.file_size();
std::string base = e.path().string();
base = base.substr(0, base.size() - 4);
auto wom = wowee::pipeline::WoweeModelLoader::load(base);
if (wom.vertices.empty() || wom.indices.empty() ||
wom.batches.empty()) {
mesh.invalid++;
mesh.invalidPaths.push_back(
fs::relative(e.path(), zoneDir).string());
}
}
}
// Audio: WAVs under audio/ — RIFF header check
fs::path audDir = fs::path(zoneDir) / "audio";
if (fs::exists(audDir)) {
for (const auto& e : fs::recursive_directory_iterator(audDir, ec)) {
if (!e.is_regular_file()) continue;
if (e.path().extension() != ".wav") continue;
audio.count++;
audio.bytes += e.file_size();
FILE* f = std::fopen(e.path().c_str(), "rb");
if (f) {
char hdr[12];
bool ok = (std::fread(hdr, 1, 12, f) == 12 &&
std::memcmp(hdr, "RIFF", 4) == 0 &&
std::memcmp(hdr + 8, "WAVE", 4) == 0);
std::fclose(f);
if (!ok) {
audio.invalid++;
audio.invalidPaths.push_back(
fs::relative(e.path(), zoneDir).string());
}
}
}
}
int totalCount = tex.count + mesh.count + audio.count;
int totalInvalid = tex.invalid + mesh.invalid + audio.invalid;
uint64_t totalBytes = tex.bytes + mesh.bytes + audio.bytes;
bool pass = (totalInvalid == 0 && totalCount > 0);
if (jsonOut) {
auto catJ = [](const CatStats& c) {
return nlohmann::json{
{"count", c.count},
{"bytes", c.bytes},
{"invalid", c.invalid},
{"invalidPaths", c.invalidPaths},
};
};
nlohmann::json j;
j["zone"] = zoneDir;
j["pass"] = pass;
j["totalCount"] = totalCount;
j["totalBytes"] = totalBytes;
j["totalInvalid"] = totalInvalid;
j["textures"] = catJ(tex);
j["meshes"] = catJ(mesh);
j["audio"] = catJ(audio);
std::printf("%s\n", j.dump(2).c_str());
return pass ? 0 : 1;
}
std::printf("Zone pack audit: %s\n", zoneDir.c_str());
std::printf("\n category count bytes invalid\n");
std::printf(" textures %5d %7llu %7d\n",
tex.count,
static_cast<unsigned long long>(tex.bytes),
tex.invalid);
std::printf(" meshes %5d %7llu %7d\n",
mesh.count,
static_cast<unsigned long long>(mesh.bytes),
mesh.invalid);
std::printf(" audio %5d %7llu %7d\n",
audio.count,
static_cast<unsigned long long>(audio.bytes),
audio.invalid);
std::printf(" ----------------------------------\n");
std::printf(" TOTAL %5d %7llu %7d\n",
totalCount,
static_cast<unsigned long long>(totalBytes),
totalInvalid);
for (const auto* cat : { &tex, &mesh, &audio }) {
for (const auto& p : cat->invalidPaths) {
std::printf(" INVALID %s\n", p.c_str());
}
}
std::printf("\n %s\n", pass ? "PASS — pack is healthy"
: "FAIL — see invalid paths above");
return pass ? 0 : 1;
} else if (std::strcmp(argv[i], "--validate-project-packs") == 0 && i + 1 < argc) {
// Run --validate-zone-pack across every zone in a project
// and aggregate the result. Reports a single PASS/FAIL
// line per zone plus a summary; exits non-zero if any
// zone fails. Designed for CI use as a gate before
// shipping a project.
std::string projectDir = argv[++i];
namespace fs = std::filesystem;
if (!fs::exists(projectDir) || !fs::is_directory(projectDir)) {
std::fprintf(stderr,
"validate-project-packs: %s is not a directory\n",
projectDir.c_str());
return 1;
}
std::vector<std::string> zones;
for (const auto& entry : fs::directory_iterator(projectDir)) {
if (!entry.is_directory()) continue;
if (!fs::exists(entry.path() / "zone.json")) continue;
zones.push_back(entry.path().string());
}
std::sort(zones.begin(), zones.end());
if (zones.empty()) {
std::fprintf(stderr,
"validate-project-packs: %s contains no zones\n",
projectDir.c_str());
return 1;
}
std::string self = (argc > 0) ? argv[0] : "wowee_editor";
int passed = 0, failed = 0;
std::printf("Project pack audit: %s\n", projectDir.c_str());
std::printf(" zones: %zu\n\n", zones.size());
for (const auto& z : zones) {
std::string cmd = "\"" + self + "\" --validate-zone-pack \"" +
z + "\" > /dev/null 2>&1";
int rc = std::system(cmd.c_str());
std::string name = fs::path(z).filename().string();
if (rc == 0) {
++passed;
std::printf(" PASS %s\n", name.c_str());
} else {
++failed;
std::printf(" FAIL %s\n", name.c_str());
}
}
std::printf("\n Total: %d passed, %d failed\n", passed, failed);
std::printf(" %s\n",
failed == 0 ? "PROJECT PASS" : "PROJECT FAIL");
return failed == 0 ? 0 : 1;
} else if (std::strcmp(argv[i], "--gen-random-project") == 0 && i + 1 < argc) {
// Project-wide companion: spawn N random zones in one
// pass. Names default to "Zone1, Zone2..."; tile