From 02564171b57b229fba45956acae02f2ae9af4c7d Mon Sep 17 00:00:00 2001 From: Kelsi Date: Sat, 9 May 2026 09:23:23 -0700 Subject: [PATCH] refactor(editor): extract for-each batch runners into cli_for_each.cpp Moves the two batch-runner handlers (--for-each-zone, --for-each-tile) out of main.cpp into a new cli_for_each.{hpp,cpp} module. Both substitute `{}` with the iterated path (find -exec convention) and shell-escape every token before passing to std::system. Exit code is the failure count, capped at 255 so the shell can still see it. main.cpp shrinks by 157 lines (2,964 to 2,807). --- CMakeLists.txt | 1 + tools/editor/cli_for_each.cpp | 201 ++++++++++++++++++++++++++++++++++ tools/editor/cli_for_each.hpp | 22 ++++ tools/editor/main.cpp | 165 +--------------------------- 4 files changed, 228 insertions(+), 161 deletions(-) create mode 100644 tools/editor/cli_for_each.cpp create mode 100644 tools/editor/cli_for_each.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c5515ac..96aa5b98 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1353,6 +1353,7 @@ add_executable(wowee_editor tools/editor/cli_zone_list.cpp tools/editor/cli_tilemap.cpp tools/editor/cli_deps.cpp + tools/editor/cli_for_each.cpp tools/editor/editor_app.cpp tools/editor/editor_camera.cpp tools/editor/editor_viewport.cpp diff --git a/tools/editor/cli_for_each.cpp b/tools/editor/cli_for_each.cpp new file mode 100644 index 00000000..09bdf65a --- /dev/null +++ b/tools/editor/cli_for_each.cpp @@ -0,0 +1,201 @@ +#include "cli_for_each.hpp" + +#include "zone_manifest.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace wowee { +namespace editor { +namespace cli { + +namespace { + +int handleForEachZone(int& i, int argc, char** argv) { + // Batch runner: enumerates zones in and runs the + // command after '--' for each one. '{}' in the command is + // substituted with the zone path (find -exec convention). + // + // wowee_editor --for-each-zone custom_zones -- \\ + // wowee_editor --validate-all {} + // + // Returns the count of failed runs as the exit code (capped + // at 255 so the shell can still see it). + std::string projectDir = argv[++i]; + // The literal '--' separates the projectDir from the command. + // Skip it; everything after is the command template. + if (i + 1 < argc && std::strcmp(argv[i + 1], "--") == 0) ++i; + if (i + 1 >= argc) { + std::fprintf(stderr, + "for-each-zone: need command after '--'\n"); + return 1; + } + // Collect command tokens until end of argv. Don't try to be + // clever about quoting — just escape each token for shell + // safety using single quotes (' inside is escaped as '\\''). + std::vector cmdTokens; + for (int k = i + 1; k < argc; ++k) cmdTokens.push_back(argv[k]); + i = argc - 1; // consume rest of argv + namespace fs = std::filesystem; + if (!fs::exists(projectDir) || !fs::is_directory(projectDir)) { + std::fprintf(stderr, "for-each-zone: %s is not a directory\n", + projectDir.c_str()); + return 1; + } + // Find every child dir that contains a zone.json — that's the + // canonical 'is this a zone?' test the rest of the editor uses. + std::vector zones; + for (const auto& entry : fs::directory_iterator(projectDir)) { + if (!entry.is_directory()) continue; + if (fs::exists(entry.path() / "zone.json")) { + zones.push_back(entry.path().string()); + } + } + std::sort(zones.begin(), zones.end()); + if (zones.empty()) { + std::fprintf(stderr, "for-each-zone: no zones found in %s\n", + projectDir.c_str()); + return 1; + } + auto shellEscape = [](const std::string& s) { + std::string out = "'"; + for (char c : s) { + if (c == '\'') out += "'\\''"; + else out += c; + } + out += "'"; + return out; + }; + int failed = 0; + for (const auto& zone : zones) { + std::string cmd; + for (size_t k = 0; k < cmdTokens.size(); ++k) { + if (k > 0) cmd += " "; + std::string token = cmdTokens[k]; + // Replace {} with zone path (every occurrence). + size_t pos; + while ((pos = token.find("{}")) != std::string::npos) { + token.replace(pos, 2, zone); + } + cmd += shellEscape(token); + } + std::printf("[%s]\n", zone.c_str()); + // Flush before std::system so the header lands above the + // child's output rather than after (parent stdout is line- + // buffered, child writes go straight to the terminal). + std::fflush(stdout); + int rc = std::system(cmd.c_str()); + if (rc != 0) { + failed++; + std::fprintf(stderr, + "for-each-zone: command exited %d for %s\n", + rc, zone.c_str()); + } + } + std::printf("\nfor-each-zone: %zu zones, %d failed\n", + zones.size(), failed); + return failed > 255 ? 255 : failed; +} + +int handleForEachTile(int& i, int argc, char** argv) { + // Per-tile batch runner. --for-each-zone iterates zones in + // a project; this iterates tiles within a zone. The '{}' in + // the command template is replaced with the tile-base path + // (zoneDir/mapName_TX_TY) — the form most tile-level + // editor commands take. + // + // wowee_editor --for-each-tile MyZone -- \\ + // wowee_editor --build-woc {} + // wowee_editor --for-each-tile MyZone -- \\ + // wowee_editor --validate-whm {} + std::string zoneDir = argv[++i]; + if (i + 1 < argc && std::strcmp(argv[i + 1], "--") == 0) ++i; + if (i + 1 >= argc) { + std::fprintf(stderr, + "for-each-tile: need command after '--'\n"); + return 1; + } + std::vector cmdTokens; + for (int k = i + 1; k < argc; ++k) cmdTokens.push_back(argv[k]); + i = argc - 1; + namespace fs = std::filesystem; + std::string manifestPath = zoneDir + "/zone.json"; + if (!fs::exists(manifestPath)) { + std::fprintf(stderr, + "for-each-tile: %s has no zone.json\n", zoneDir.c_str()); + return 1; + } + wowee::editor::ZoneManifest zm; + if (!zm.load(manifestPath)) { + std::fprintf(stderr, "for-each-tile: parse failed\n"); + return 1; + } + if (zm.tiles.empty()) { + std::fprintf(stderr, "for-each-tile: zone has no tiles\n"); + return 1; + } + // Same shell-escape + cmd-substitution as --for-each-zone. + auto shellEscape = [](const std::string& s) { + std::string out = "'"; + for (char c : s) { + if (c == '\'') out += "'\\''"; + else out += c; + } + out += "'"; + return out; + }; + int failed = 0; + // Sort tiles so order is deterministic across runs. + auto tiles = zm.tiles; + std::sort(tiles.begin(), tiles.end()); + for (const auto& [tx, ty] : tiles) { + std::string tileBase = zoneDir + "/" + zm.mapName + "_" + + std::to_string(tx) + "_" + std::to_string(ty); + std::string cmd; + for (size_t k = 0; k < cmdTokens.size(); ++k) { + if (k > 0) cmd += " "; + std::string token = cmdTokens[k]; + size_t pos; + while ((pos = token.find("{}")) != std::string::npos) { + token.replace(pos, 2, tileBase); + } + cmd += shellEscape(token); + } + std::printf("[%s (%d, %d)]\n", tileBase.c_str(), tx, ty); + std::fflush(stdout); + int rc = std::system(cmd.c_str()); + if (rc != 0) { + failed++; + std::fprintf(stderr, + "for-each-tile: command exited %d for (%d, %d)\n", + rc, tx, ty); + } + } + std::printf("\nfor-each-tile: %zu tiles, %d failed\n", + tiles.size(), failed); + return failed > 255 ? 255 : failed; +} + + +} // namespace + +bool handleForEach(int& i, int argc, char** argv, int& outRc) { + if (std::strcmp(argv[i], "--for-each-zone") == 0 && i + 1 < argc) { + outRc = handleForEachZone(i, argc, argv); return true; + } + if (std::strcmp(argv[i], "--for-each-tile") == 0 && i + 1 < argc) { + outRc = handleForEachTile(i, argc, argv); return true; + } + return false; +} + +} // namespace cli +} // namespace editor +} // namespace wowee diff --git a/tools/editor/cli_for_each.hpp b/tools/editor/cli_for_each.hpp new file mode 100644 index 00000000..d9fcf43f --- /dev/null +++ b/tools/editor/cli_for_each.hpp @@ -0,0 +1,22 @@ +#pragma once + +namespace wowee { +namespace editor { +namespace cli { + +// Dispatch the batch-runner handlers — iterate over zones (or +// tiles within a zone) and execute a shell command for each +// one, with `{}` substitution like find -exec. +// --for-each-zone -- +// --for-each-tile -- +// +// Useful for batch-validating, rebuilding, or processing every +// zone / tile without hand-typing the loop. Exit code is the +// failure count, capped at 255 so the shell can still see it. +// +// Returns true if matched; outRc holds the exit code. +bool handleForEach(int& i, int argc, char** argv, int& outRc); + +} // namespace cli +} // namespace editor +} // namespace wowee diff --git a/tools/editor/main.cpp b/tools/editor/main.cpp index 7f8ce60d..54d2105d 100644 --- a/tools/editor/main.cpp +++ b/tools/editor/main.cpp @@ -54,6 +54,7 @@ #include "cli_zone_list.hpp" #include "cli_tilemap.hpp" #include "cli_deps.hpp" +#include "cli_for_each.hpp" #include "content_pack.hpp" #include "npc_spawner.hpp" #include "object_placer.hpp" @@ -556,6 +557,9 @@ int main(int argc, char* argv[]) { if (wowee::editor::cli::handleDeps(i, argc, argv, outRc)) { return outRc; } + if (wowee::editor::cli::handleForEach(i, argc, argv, outRc)) { + return outRc; + } } if (std::strcmp(argv[i], "--data") == 0 && i + 1 < argc) { dataPath = argv[++i]; @@ -2443,167 +2447,6 @@ int main(int argc, char* argv[]) { } std::printf("\n %d zone(s) have dangling refs\n", projectFailedZones); return 1; - } else if (std::strcmp(argv[i], "--for-each-zone") == 0 && i + 1 < argc) { - // Batch runner: enumerates zones in and runs the - // command after '--' for each one. '{}' in the command is - // substituted with the zone path (find -exec convention). - // - // wowee_editor --for-each-zone custom_zones -- \\ - // wowee_editor --validate-all {} - // - // Returns the count of failed runs as the exit code (capped - // at 255 so the shell can still see it). - std::string projectDir = argv[++i]; - // The literal '--' separates the projectDir from the command. - // Skip it; everything after is the command template. - if (i + 1 < argc && std::strcmp(argv[i + 1], "--") == 0) ++i; - if (i + 1 >= argc) { - std::fprintf(stderr, - "for-each-zone: need command after '--'\n"); - return 1; - } - // Collect command tokens until end of argv. Don't try to be - // clever about quoting — just escape each token for shell - // safety using single quotes (' inside is escaped as '\\''). - std::vector cmdTokens; - for (int k = i + 1; k < argc; ++k) cmdTokens.push_back(argv[k]); - i = argc - 1; // consume rest of argv - namespace fs = std::filesystem; - if (!fs::exists(projectDir) || !fs::is_directory(projectDir)) { - std::fprintf(stderr, "for-each-zone: %s is not a directory\n", - projectDir.c_str()); - return 1; - } - // Find every child dir that contains a zone.json — that's the - // canonical 'is this a zone?' test the rest of the editor uses. - std::vector zones; - for (const auto& entry : fs::directory_iterator(projectDir)) { - if (!entry.is_directory()) continue; - if (fs::exists(entry.path() / "zone.json")) { - zones.push_back(entry.path().string()); - } - } - std::sort(zones.begin(), zones.end()); - if (zones.empty()) { - std::fprintf(stderr, "for-each-zone: no zones found in %s\n", - projectDir.c_str()); - return 1; - } - auto shellEscape = [](const std::string& s) { - std::string out = "'"; - for (char c : s) { - if (c == '\'') out += "'\\''"; - else out += c; - } - out += "'"; - return out; - }; - int failed = 0; - for (const auto& zone : zones) { - std::string cmd; - for (size_t k = 0; k < cmdTokens.size(); ++k) { - if (k > 0) cmd += " "; - std::string token = cmdTokens[k]; - // Replace {} with zone path (every occurrence). - size_t pos; - while ((pos = token.find("{}")) != std::string::npos) { - token.replace(pos, 2, zone); - } - cmd += shellEscape(token); - } - std::printf("[%s]\n", zone.c_str()); - // Flush before std::system so the header lands above the - // child's output rather than after (parent stdout is line- - // buffered, child writes go straight to the terminal). - std::fflush(stdout); - int rc = std::system(cmd.c_str()); - if (rc != 0) { - failed++; - std::fprintf(stderr, - "for-each-zone: command exited %d for %s\n", - rc, zone.c_str()); - } - } - std::printf("\nfor-each-zone: %zu zones, %d failed\n", - zones.size(), failed); - return failed > 255 ? 255 : failed; - } else if (std::strcmp(argv[i], "--for-each-tile") == 0 && i + 1 < argc) { - // Per-tile batch runner. --for-each-zone iterates zones in - // a project; this iterates tiles within a zone. The '{}' in - // the command template is replaced with the tile-base path - // (zoneDir/mapName_TX_TY) — the form most tile-level - // editor commands take. - // - // wowee_editor --for-each-tile MyZone -- \\ - // wowee_editor --build-woc {} - // wowee_editor --for-each-tile MyZone -- \\ - // wowee_editor --validate-whm {} - std::string zoneDir = argv[++i]; - if (i + 1 < argc && std::strcmp(argv[i + 1], "--") == 0) ++i; - if (i + 1 >= argc) { - std::fprintf(stderr, - "for-each-tile: need command after '--'\n"); - return 1; - } - std::vector cmdTokens; - for (int k = i + 1; k < argc; ++k) cmdTokens.push_back(argv[k]); - i = argc - 1; - namespace fs = std::filesystem; - std::string manifestPath = zoneDir + "/zone.json"; - if (!fs::exists(manifestPath)) { - std::fprintf(stderr, - "for-each-tile: %s has no zone.json\n", zoneDir.c_str()); - return 1; - } - wowee::editor::ZoneManifest zm; - if (!zm.load(manifestPath)) { - std::fprintf(stderr, "for-each-tile: parse failed\n"); - return 1; - } - if (zm.tiles.empty()) { - std::fprintf(stderr, "for-each-tile: zone has no tiles\n"); - return 1; - } - // Same shell-escape + cmd-substitution as --for-each-zone. - auto shellEscape = [](const std::string& s) { - std::string out = "'"; - for (char c : s) { - if (c == '\'') out += "'\\''"; - else out += c; - } - out += "'"; - return out; - }; - int failed = 0; - // Sort tiles so order is deterministic across runs. - auto tiles = zm.tiles; - std::sort(tiles.begin(), tiles.end()); - for (const auto& [tx, ty] : tiles) { - std::string tileBase = zoneDir + "/" + zm.mapName + "_" + - std::to_string(tx) + "_" + std::to_string(ty); - std::string cmd; - for (size_t k = 0; k < cmdTokens.size(); ++k) { - if (k > 0) cmd += " "; - std::string token = cmdTokens[k]; - size_t pos; - while ((pos = token.find("{}")) != std::string::npos) { - token.replace(pos, 2, tileBase); - } - cmd += shellEscape(token); - } - std::printf("[%s (%d, %d)]\n", tileBase.c_str(), tx, ty); - std::fflush(stdout); - int rc = std::system(cmd.c_str()); - if (rc != 0) { - failed++; - std::fprintf(stderr, - "for-each-tile: command exited %d for (%d, %d)\n", - rc, tx, ty); - } - } - std::printf("\nfor-each-tile: %zu tiles, %d failed\n", - tiles.size(), failed); - return failed > 255 ? 255 : failed; } else if (std::strcmp(argv[i], "--version") == 0 || std::strcmp(argv[i], "-v") == 0) { std::printf("Wowee World Editor v1.0.0\n"); std::printf("Open formats: WOT/WHM/WOM/WOB/WOC/WCP + PNG/JSON (all novel)\n");