diff --git a/plugins/orders.cpp b/plugins/orders.cpp index fca55713e..c984f5060 100644 --- a/plugins/orders.cpp +++ b/plugins/orders.cpp @@ -40,6 +40,8 @@ DFHACK_PLUGIN("orders"); REQUIRE_GLOBAL(world); +static const std::string ORDERS_DIR = "dfhack-config/orders"; + static command_result orders_command(color_ostream & out, std::vector & parameters); DFhackCExport command_result plugin_init(color_ostream & out, std::vector & commands) @@ -50,6 +52,8 @@ DFhackCExport command_result plugin_init(color_ostream & out, std::vector files; + if (0 < Filesystem::listdir_recursive(ORDERS_DIR, files, 0, false)) + { + out << COLOR_LIGHTRED << "Unable to list files in directory: " << ORDERS_DIR << std::endl; + return CR_FAILURE; + } + + if (files.empty()) + { + out << COLOR_YELLOW << "No exported orders yet. Create manager orders and export them with 'orders export ', or copy pre-made orders .json files into " << ORDERS_DIR << "." << std::endl; + return CR_OK; + } + + for (auto it : files) + { + if (it.second) + continue; // skip directories + std::string name = it.first; + if (name.length() <= 5 || name.rfind(".json") != name.length() - 5) + continue; // skip non-.json files + name.resize(name.length() - 5); + out << name << std::endl; + } + + return CR_OK; +} + static bool is_safe_filename(color_ostream & out, const std::string & name) { if (name.empty()) @@ -439,9 +481,9 @@ static command_result orders_export_command(color_ostream & out, const std::stri } } - Filesystem::mkdir("dfhack-config/orders"); + Filesystem::mkdir(ORDERS_DIR); - std::ofstream file("dfhack-config/orders/" + name + ".json"); + std::ofstream file(ORDERS_DIR + "/" + name + ".json"); file << orders << std::endl; @@ -852,7 +894,7 @@ static command_result orders_import_command(color_ostream & out, const std::stri return CR_WRONG_USAGE; } - const std::string filename("dfhack-config/orders/" + name + ".json"); + const std::string filename(ORDERS_DIR + "/" + name + ".json"); Json::Value orders; { diff --git a/test/plugins/orders.lua b/test/plugins/orders.lua index 95fe186d2..a0959ae94 100644 --- a/test/plugins/orders.lua +++ b/test/plugins/orders.lua @@ -258,3 +258,9 @@ function test.import_invalid_reaction_conditions() ]], 'condition ignored for bad reagent name', 1) expect.eq(0, #get_last_order().item_conditions) end + +function test.list() + local output, status = dfhack.run_command_silent('orders', 'list') + expect.eq(CR_OK, status) + expect.str_find(BACKUP_FILE_NAME:gsub('%-', '%%-'), output) +end