Skip to content

Commit 95e65aa

Browse files
committed
Migrate infiniteSky arg parsing to lua
1 parent a0bd8ee commit 95e65aa

File tree

3 files changed

+62
-10
lines changed

3 files changed

+62
-10
lines changed

plugins/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ if(BUILD_SUPPORTED)
125125
#dfhack_plugin(generated-creature-renamer generated-creature-renamer.cpp)
126126
dfhack_plugin(getplants getplants.cpp)
127127
dfhack_plugin(hotkeys hotkeys.cpp LINK_LIBRARIES lua)
128-
dfhack_plugin(infiniteSky infiniteSky.cpp)
128+
dfhack_plugin(infiniteSky infiniteSky.cpp LINK_LIBRARIES lua)
129129
#dfhack_plugin(isoworldremote isoworldremote.cpp PROTOBUFS isoworldremote)
130130
#dfhack_plugin(jobutils jobutils.cpp)
131131
dfhack_plugin(lair lair.cpp)

plugins/infiniteSky.cpp

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "DataDefs.h"
44
#include "Debug.h"
55
#include "Export.h"
6+
#include "LuaTools.h"
67
#include "PluginManager.h"
78

89
#include "modules/World.h"
@@ -228,18 +229,42 @@ void doInfiniteSky(color_ostream& out, int32_t howMany) {
228229
world->map_extras.z_level_flags = flags;
229230
}
230231

231-
command_result infiniteSky (color_ostream &out, std::vector <std::string> & parameters)
232-
{
233-
if ( parameters.size() > 1 )
232+
struct infinitesky_options {
233+
// whether to display help
234+
bool help = false;
235+
236+
// how many z levels to generate immediately (0 for none)
237+
int32_t n = 0;
238+
239+
static struct_identity _identity;
240+
};
241+
static const struct_field_info infinitesky_options_fields[] = {
242+
{struct_field_info::PRIMITIVE, "help", offsetof(infinitesky_options, help), &df::identity_traits<bool>::identity, 0, 0},
243+
{struct_field_info::PRIMITIVE, "n", offsetof(infinitesky_options, n), &df::identity_traits<int32_t>::identity, 0, 0}
244+
};
245+
struct_identity infinitesky_options::_identity{sizeof(infinitesky_options), &df::allocator_fn<infinitesky_options>, NULL, "infinitesky_options", NULL, infinitesky_options_fields};
246+
247+
command_result infiniteSky(color_ostream &out,
248+
std::vector<std::string> &parameters) {
249+
if (!Core::getInstance().isMapLoaded() || !World::isFortressMode()) {
250+
out.printerr("Cannot run %s without a loaded fort.\n", plugin_name);
251+
return CR_FAILURE;
252+
}
253+
254+
infinitesky_options opts;
255+
if (!Lua::CallLuaModuleFunction(out, "plugins.infiniteSky",
256+
"parse_commandline",
257+
std::make_tuple(&opts, parameters)) ||
258+
opts.help)
234259
return CR_WRONG_USAGE;
235-
if (parameters.size() == 0) {
260+
261+
if (opts.n != 0) {
262+
out.print("InfiniteSky: creating %d new z-level%s of sky.\n", opts.n,
263+
opts.n == 1 ? "" : "s");
264+
doInfiniteSky(out, opts.n);
265+
} else {
236266
out.print("Construction monitoring is %s.\n",
237267
is_enabled ? "enabled" : "disabled");
238-
return CR_OK;
239268
}
240-
int32_t howMany = 0;
241-
howMany = atoi(parameters[0].c_str());
242-
out.print("InfiniteSky: creating %d new z-level%s of sky.\n", howMany, howMany == 1 ? "" : "s" );
243-
doInfiniteSky(out, howMany);
244269
return CR_OK;
245270
}

plugins/lua/infiniteSky.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
local _ENV = mkmodule('plugins.infiniteSky')
2+
3+
local argparse = require('argparse')
4+
5+
local function process_args(opts, args)
6+
if args[1] == 'help' then
7+
opts.help = true
8+
return
9+
end
10+
11+
if args[1] ~= nil then
12+
opts.n = argparse.positiveInt(args[1])
13+
return
14+
end
15+
16+
return argparse.processArgsGetopt(args, {
17+
{'h', 'help', handler=function() opts.help = true end},
18+
})
19+
end
20+
21+
function parse_commandline(opts, args)
22+
local positionals = process_args(opts, args)
23+
24+
if opts.help then return end
25+
end
26+
27+
return _ENV

0 commit comments

Comments
 (0)