diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 1e2019091..969d3774a 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -188,7 +188,7 @@ endif() # this is the skeleton plugin. If you want to make your own, make a copy and then change it option(BUILD_SKELETON "Build the skeleton plugin." OFF) if(BUILD_SKELETON) - dfhack_plugin(skeleton examples/skeleton.cpp LINK_LIBRARIES lua) + dfhack_plugin(skeleton examples/skeleton.cpp) endif() macro(subdirlist result subdir) diff --git a/plugins/examples/persistent_per_save_example.cpp b/plugins/examples/persistent_per_save_example.cpp index b620b7703..be2f1730c 100644 --- a/plugins/examples/persistent_per_save_example.cpp +++ b/plugins/examples/persistent_per_save_example.cpp @@ -12,7 +12,6 @@ #include "Core.h" #include "Debug.h" -#include "LuaTools.h" #include "PluginManager.h" #include "modules/Persistence.h" @@ -60,35 +59,6 @@ static void set_config_bool(int index, bool value) { static int32_t cycle_timestamp = 0; // world->frame_counter at last cycle -// define the structure that will represent the possible commandline options -struct command_options { - // whether to display help - bool help = false; - - // whether to run a cycle right now - bool now = false; - - // how many ticks to wait between cycles when enabled, -1 means unset - int32_t ticks = -1; - - // example params of different types - df::coord start; - string format; - vector list; // note this must be a vector of pointers, not objects - - static struct_identity _identity; -}; -static const struct_field_info command_options_fields[] = { - { struct_field_info::PRIMITIVE, "help", offsetof(command_options, help), &df::identity_traits::identity, 0, 0 }, - { struct_field_info::PRIMITIVE, "now", offsetof(command_options, now), &df::identity_traits::identity, 0, 0 }, - { struct_field_info::PRIMITIVE, "ticks", offsetof(command_options, ticks), &df::identity_traits::identity, 0, 0 }, - { struct_field_info::SUBSTRUCT, "start", offsetof(command_options, start), &df::coord::_identity, 0, 0 }, - { struct_field_info::PRIMITIVE, "format", offsetof(command_options, format), df::identity_traits::get(), 0, 0 }, - { struct_field_info::STL_VECTOR_PTR, "list", offsetof(command_options, list), df::identity_traits::get(), 0, 0 }, - { struct_field_info::END } -}; -struct_identity command_options::_identity(sizeof(command_options), &df::allocator_fn, NULL, "command_options", NULL, command_options_fields); - static command_result do_command(color_ostream &out, vector ¶meters); static void do_cycle(color_ostream &out); @@ -165,33 +135,6 @@ DFhackCExport command_result plugin_onupdate(color_ostream &out) { return CR_OK; } -// load the lua module associated with the plugin and parse the commandline -// in lua (which has better facilities than C++ for string parsing). -static bool get_options(color_ostream &out, - command_options &opts, - const vector ¶meters) -{ - auto L = Lua::Core::State; - Lua::StackUnwinder top(L); - - if (!lua_checkstack(L, parameters.size() + 2) || - !Lua::PushModulePublic( - out, L, ("plugins." + string(plugin_name)).c_str(), - "parse_commandline")) { - out.printerr("Failed to load %s Lua code\n", plugin_name); - return false; - } - - Lua::Push(L, &opts); - for (const string ¶m : parameters) - Lua::Push(L, param); - - if (!Lua::SafeCall(out, L, parameters.size() + 1, 0)) - return false; - - return true; -} - static command_result do_command(color_ostream &out, vector ¶meters) { // be sure to suspend the core if any DF state is read or modified CoreSuspender suspend; @@ -201,20 +144,11 @@ static command_result do_command(color_ostream &out, vector ¶meters) return CR_FAILURE; } - command_options opts; - if (!get_options(out, opts, parameters) || opts.help) - return CR_WRONG_USAGE; + // TODO: configuration logic + // simple commandline parsing can be done in C++, but there are lua libraries + // that can easily handle more complex commandlines. see the blueprint plugin + // for an example. - if (opts.ticks > -1) { - set_config_val(CONFIG_CYCLE_TICKS, opts.ticks); - INFO(status,out).print("New cycle timer: %d ticks.\n", opts.ticks); - } - else if (opts.now) { - do_cycle(out); - } - else { - out.print("%s is %srunning\n", plugin_name, (is_enabled ? "" : "not ")); - } return CR_OK; } diff --git a/plugins/examples/simple_command_example.cpp b/plugins/examples/simple_command_example.cpp index 0f1aad4f1..7b12a1271 100644 --- a/plugins/examples/simple_command_example.cpp +++ b/plugins/examples/simple_command_example.cpp @@ -5,7 +5,6 @@ #include #include "Debug.h" -#include "LuaTools.h" #include "PluginManager.h" using std::string; @@ -19,29 +18,6 @@ namespace DFHack { DBG_DECLARE(simple_command_example, log); } -// define the structure that will represent the possible commandline options -struct command_options { - // whether to display help - bool help = false; - - // example params of different types - int32_t ticks = -1; - df::coord start; - string format; - vector list; // note this must be a vector of pointers, not objects - - static struct_identity _identity; -}; -static const struct_field_info command_options_fields[] = { - { struct_field_info::PRIMITIVE, "help", offsetof(command_options, help), &df::identity_traits::identity, 0, 0 }, - { struct_field_info::PRIMITIVE, "ticks", offsetof(command_options, ticks), &df::identity_traits::identity, 0, 0 }, - { struct_field_info::SUBSTRUCT, "start", offsetof(command_options, start), &df::coord::_identity, 0, 0 }, - { struct_field_info::PRIMITIVE, "format", offsetof(command_options, format), df::identity_traits::get(), 0, 0 }, - { struct_field_info::STL_VECTOR_PTR, "list", offsetof(command_options, list), df::identity_traits::get(), 0, 0 }, - { struct_field_info::END } -}; -struct_identity command_options::_identity(sizeof(command_options), &df::allocator_fn, NULL, "command_options", NULL, command_options_fields); - static command_result do_command(color_ostream &out, vector ¶meters); DFhackCExport command_result plugin_init(color_ostream &out, std::vector &commands) { @@ -55,41 +31,10 @@ DFhackCExport command_result plugin_init(color_ostream &out, std::vector ¶meters) -{ - auto L = Lua::Core::State; - Lua::StackUnwinder top(L); - - if (!lua_checkstack(L, parameters.size() + 2) || - !Lua::PushModulePublic( - out, L, ("plugins." + string(plugin_name)).c_str(), - "parse_commandline")) { - out.printerr("Failed to load %s Lua code\n", plugin_name); - return false; - } - - Lua::Push(L, &opts); - for (const string ¶m : parameters) - Lua::Push(L, param); - - if (!Lua::SafeCall(out, L, parameters.size() + 1, 0)) - return false; - - return true; -} - static command_result do_command(color_ostream &out, vector ¶meters) { // be sure to suspend the core if any DF state is read or modified CoreSuspender suspend; - command_options opts; - if (!get_options(out, opts, parameters) || opts.help) - return CR_WRONG_USAGE; - // TODO: command logic return CR_OK; diff --git a/plugins/examples/skeleton.cpp b/plugins/examples/skeleton.cpp index 27621af4a..434a7e7b5 100644 --- a/plugins/examples/skeleton.cpp +++ b/plugins/examples/skeleton.cpp @@ -15,7 +15,6 @@ #include "Core.h" #include "Debug.h" -#include "LuaTools.h" #include "PluginManager.h" #include "modules/Persistence.h" @@ -157,67 +156,6 @@ DFhackCExport command_result plugin_load_data (color_ostream &out) { return CR_OK; } -// define the structure that will represent the possible commandline options -struct command_options { - // whether to display help - bool help = false; - - // whether to run a cycle right now - bool now = false; - - // how many ticks to wait between cycles when enabled, -1 means unset - int32_t ticks = -1; - - // example params of different types - df::coord start; - string format; - vector list; // note this must be a vector of pointers, not objects - - static struct_identity _identity; -}; -static const struct_field_info command_options_fields[] = { - { struct_field_info::PRIMITIVE, "help", offsetof(command_options, help), &df::identity_traits::identity, 0, 0 }, - { struct_field_info::PRIMITIVE, "now", offsetof(command_options, now), &df::identity_traits::identity, 0, 0 }, - { struct_field_info::PRIMITIVE, "ticks", offsetof(command_options, ticks), &df::identity_traits::identity, 0, 0 }, - { struct_field_info::SUBSTRUCT, "start", offsetof(command_options, start), &df::coord::_identity, 0, 0 }, - { struct_field_info::PRIMITIVE, "format", offsetof(command_options, format), df::identity_traits::get(), 0, 0 }, - { struct_field_info::STL_VECTOR_PTR, "list", offsetof(command_options, list), df::identity_traits::get(), 0, 0 }, - { struct_field_info::END } -}; -struct_identity command_options::_identity(sizeof(command_options), &df::allocator_fn, NULL, "command_options", NULL, command_options_fields); - -// load the lua module associated with the plugin and parse the commandline -// in lua (which has better facilities than C++ for string parsing). You should -// create a file named after your plugin in the plugins/lua directory. This -// example expects you to define a global function in that file named -// "parse_commandline" that takes the options struct defined above along with -// the commandline parameters. It should parse the parameters and set data in -// the options structure. See plugins/lua/skeleton.lua for an example. -static bool get_options(color_ostream &out, - command_options &opts, - const vector ¶meters) -{ - auto L = Lua::Core::State; - Lua::StackUnwinder top(L); - - if (!lua_checkstack(L, parameters.size() + 2) || - !Lua::PushModulePublic( - out, L, ("plugins." + string(plugin_name)).c_str(), - "parse_commandline")) { - out.printerr("Failed to load %s Lua code\n", plugin_name); - return false; - } - - Lua::Push(L, &opts); - for (const string ¶m : parameters) - Lua::Push(L, param); - - if (!Lua::SafeCall(out, L, parameters.size() + 1, 0)) - return false; - - return true; -} - // This is the callback we registered in plugin_init. Note that while plugin // callbacks are called with the core suspended, command callbacks are called // from a different thread and need to explicity suspend the core if they @@ -233,9 +171,10 @@ static command_result command_callback1(color_ostream &out, vector ¶ // Return CR_WRONG_USAGE to print out your help text. The help text is // sourced from the associated rst file in docs/plugins/. The same help will // also be returned by 'help your-command'. - command_options opts; - if (!get_options(out, opts, parameters) || opts.help) - return CR_WRONG_USAGE; + + // simple commandline parsing can be done in C++, but there are lua libraries + // that can easily handle more complex commandlines. see the blueprint plugin + // for an example. // TODO: do something according to the flags set in the options struct diff --git a/plugins/examples/ui_addition_example.cpp b/plugins/examples/ui_addition_example.cpp index 43350a159..bbd3af3de 100644 --- a/plugins/examples/ui_addition_example.cpp +++ b/plugins/examples/ui_addition_example.cpp @@ -10,7 +10,6 @@ #include "df/viewscreen_titlest.h" #include "Debug.h" -#include "LuaTools.h" #include "PluginManager.h" #include "VTableInterpose.h" diff --git a/plugins/lua/skeleton.lua b/plugins/lua/skeleton.lua deleted file mode 100644 index 60af229cc..000000000 --- a/plugins/lua/skeleton.lua +++ /dev/null @@ -1,46 +0,0 @@ -local _ENV = mkmodule('plugins.skeleton') - -local argparse = require('argparse') -local utils = require('utils') - -local VALID_FORMATS = utils.invert{'pretty', 'normal', 'ugly'} - -local function do_commandline(opts, args) - print(('called with %d arguments:'):format(#args)) - for _,arg in ipairs(args) do - print(' ' .. arg) - end - - local positionals = argparse.processArgsGetopt(args, { - {'t', 'ticks', hasArg=true, - handler=function(arg) opts.ticks = - argparse.check_positive_int(arg, 'ticks') end}, - {'s', 'start', hasArg=true, - handler=function(arg) utils.assign( - opts.start, argpars.coors(arg, 'start')) end}, - {'h', 'help', handler=function() opts.help = true end}, - {'f', 'format', hasArg=true, - handler=function(arg) opts.format = arg end}, - {'z', 'cur-zlevel', handler=function() use_zlevel = true end}, - }) - - if positionals[1] == 'help' then opts.help = true end - if opts.help then return end - - if positionals[1] == 'now' then - opts.now = true - end - - if #opts.format > 0 and not VALID_FORMATS[opts.format] then - qerror(('invalid format name: "%s"'):format(opts.format)) - end -end - -function parse_commandline(opts, ...) - do_commandline(opts, {...}) - - print('populated options data structure:') - printall_recurse(opts) -end - -return _ENV