From a7ba6bf71142a1c2c28c1ddefd3492ee341c93c6 Mon Sep 17 00:00:00 2001 From: Kelsi Date: Thu, 7 May 2026 01:52:43 -0700 Subject: [PATCH] feat(editor): add --remove-item completing the items CRUD set MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removes the entry at given 0-based index from /items.json. Mirrors --remove-creature/--remove-object/--remove-quest semantics: bounds-checked, file rewrites on success, exit 1 on out-of-range or malformed JSON. Reports the removed item's name + id in the success line so the user has feedback on what they just dropped. Items CRUD set is now complete: --add-item / --list-items / --remove-item. Together they form the items.json half of the new content-creation direction (textures/meshes/items) the user asked for in the prior batch. Verified: 3-item zone → remove idx 1 ('Bread', id=2) leaves 2 items intact and renumbered; out-of-range index 99 fails with exit 1 and clear error. Brings command count to 198. --- tools/editor/main.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/tools/editor/main.cpp b/tools/editor/main.cpp index b5e78473..164d43cb 100644 --- a/tools/editor/main.cpp +++ b/tools/editor/main.cpp @@ -532,6 +532,8 @@ static void printUsage(const char* argv0) { std::printf(" Append one item entry to /items.json (auto-creates the file)\n"); std::printf(" --list-items [--json]\n"); std::printf(" Print every item in /items.json with quality colors and key fields\n"); + std::printf(" --remove-item \n"); + std::printf(" Remove item at given 0-based index from /items.json\n"); std::printf(" --convert-dbc-json [out.json]\n"); std::printf(" Convert one DBC file to wowee JSON sidecar format\n"); std::printf(" --convert-json-dbc [out.dbc]\n"); @@ -939,7 +941,7 @@ int main(int argc, char* argv[]) { "--add-quest-objective", "--add-quest-reward-item", "--set-quest-reward", "--remove-quest-objective", "--clone-quest", "--clone-creature", "--clone-object", - "--remove-creature", "--remove-object", "--remove-quest", + "--remove-creature", "--remove-object", "--remove-quest", "--remove-item", "--copy-zone", "--rename-zone", "--remove-zone", "--clear-zone-content", "--strip-zone", "--strip-project", "--repair-zone", "--repair-project", @@ -12646,6 +12648,62 @@ int main(int argc, char* argv[]) { qualityNames[quality], displayId, name.c_str()); } return 0; + } else if (std::strcmp(argv[i], "--remove-item") == 0 && i + 2 < argc) { + // Remove the item at given 0-based index from / + // items.json. Mirrors --remove-creature/--remove-object/ + // --remove-quest semantics — bounds-checked, file rewrites + // on success, exit 1 on out-of-range. + std::string zoneDir = argv[++i]; + int idx = -1; + try { idx = std::stoi(argv[++i]); } + catch (...) { + std::fprintf(stderr, + "remove-item: index must be an integer\n"); + return 1; + } + namespace fs = std::filesystem; + std::string path = zoneDir + "/items.json"; + if (!fs::exists(path)) { + std::fprintf(stderr, + "remove-item: %s has no items.json\n", zoneDir.c_str()); + return 1; + } + nlohmann::json doc; + try { + std::ifstream in(path); + in >> doc; + } catch (...) { + std::fprintf(stderr, + "remove-item: %s is not valid JSON\n", path.c_str()); + return 1; + } + if (!doc.contains("items") || !doc["items"].is_array()) { + std::fprintf(stderr, + "remove-item: %s has no 'items' array\n", path.c_str()); + return 1; + } + auto& items = doc["items"]; + if (idx < 0 || static_cast(idx) >= items.size()) { + std::fprintf(stderr, + "remove-item: index %d out of range (have %zu)\n", + idx, items.size()); + return 1; + } + std::string removedName = items[idx].value("name", std::string("(unnamed)")); + uint32_t removedId = items[idx].value("id", 0u); + items.erase(items.begin() + idx); + std::ofstream out(path); + if (!out) { + std::fprintf(stderr, + "remove-item: failed to write %s\n", path.c_str()); + return 1; + } + out << doc.dump(2); + out.close(); + std::printf("Removed item '%s' (id=%u) from %s (now %zu total)\n", + removedName.c_str(), removedId, + path.c_str(), items.size()); + return 0; } else if (std::strcmp(argv[i], "--scaffold-zone") == 0 && i + 1 < argc) { // Generate a minimal valid empty zone — useful for kickstarting // a new authoring session without needing to launch the GUI.