remove all lua integration from the examples

develop
myk002 2022-08-03 00:11:18 -07:00 committed by Myk
parent a4c8535977
commit a28cf6d444
6 changed files with 9 additions and 238 deletions

@ -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)

@ -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<string*> 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<bool>::identity, 0, 0 },
{ struct_field_info::PRIMITIVE, "now", offsetof(command_options, now), &df::identity_traits<bool>::identity, 0, 0 },
{ struct_field_info::PRIMITIVE, "ticks", offsetof(command_options, ticks), &df::identity_traits<int32_t>::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<string>::get(), 0, 0 },
{ struct_field_info::STL_VECTOR_PTR, "list", offsetof(command_options, list), df::identity_traits<string>::get(), 0, 0 },
{ struct_field_info::END }
};
struct_identity command_options::_identity(sizeof(command_options), &df::allocator_fn<command_options>, NULL, "command_options", NULL, command_options_fields);
static command_result do_command(color_ostream &out, vector<string> &parameters);
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<string> &parameters)
{
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 &param : 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<string> &parameters) {
// 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<string> &parameters)
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;
}

@ -5,7 +5,6 @@
#include <vector>
#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<string*> 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<bool>::identity, 0, 0 },
{ struct_field_info::PRIMITIVE, "ticks", offsetof(command_options, ticks), &df::identity_traits<int32_t>::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<string>::get(), 0, 0 },
{ struct_field_info::STL_VECTOR_PTR, "list", offsetof(command_options, list), df::identity_traits<string>::get(), 0, 0 },
{ struct_field_info::END }
};
struct_identity command_options::_identity(sizeof(command_options), &df::allocator_fn<command_options>, NULL, "command_options", NULL, command_options_fields);
static command_result do_command(color_ostream &out, vector<string> &parameters);
DFhackCExport command_result plugin_init(color_ostream &out, std::vector <PluginCommand> &commands) {
@ -55,41 +31,10 @@ DFhackCExport command_result plugin_init(color_ostream &out, std::vector <Plugin
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<string> &parameters)
{
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 &param : 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<string> &parameters) {
// 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;

@ -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<string*> 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<bool>::identity, 0, 0 },
{ struct_field_info::PRIMITIVE, "now", offsetof(command_options, now), &df::identity_traits<bool>::identity, 0, 0 },
{ struct_field_info::PRIMITIVE, "ticks", offsetof(command_options, ticks), &df::identity_traits<int32_t>::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<string>::get(), 0, 0 },
{ struct_field_info::STL_VECTOR_PTR, "list", offsetof(command_options, list), df::identity_traits<string>::get(), 0, 0 },
{ struct_field_info::END }
};
struct_identity command_options::_identity(sizeof(command_options), &df::allocator_fn<command_options>, 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<string> &parameters)
{
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 &param : 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<string> &para
// 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

@ -10,7 +10,6 @@
#include "df/viewscreen_titlest.h"
#include "Debug.h"
#include "LuaTools.h"
#include "PluginManager.h"
#include "VTableInterpose.h"

@ -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