Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a28cf6d

Browse files
committed
remove all lua integration from the examples
1 parent a4c8535 commit a28cf6d

6 files changed

Lines changed: 9 additions & 238 deletions

File tree

plugins/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ endif()
188188
# this is the skeleton plugin. If you want to make your own, make a copy and then change it
189189
option(BUILD_SKELETON "Build the skeleton plugin." OFF)
190190
if(BUILD_SKELETON)
191-
dfhack_plugin(skeleton examples/skeleton.cpp LINK_LIBRARIES lua)
191+
dfhack_plugin(skeleton examples/skeleton.cpp)
192192
endif()
193193

194194
macro(subdirlist result subdir)

plugins/examples/persistent_per_save_example.cpp

Lines changed: 4 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
#include "Core.h"
1414
#include "Debug.h"
15-
#include "LuaTools.h"
1615
#include "PluginManager.h"
1716

1817
#include "modules/Persistence.h"
@@ -60,35 +59,6 @@ static void set_config_bool(int index, bool value) {
6059

6160
static int32_t cycle_timestamp = 0; // world->frame_counter at last cycle
6261

63-
// define the structure that will represent the possible commandline options
64-
struct command_options {
65-
// whether to display help
66-
bool help = false;
67-
68-
// whether to run a cycle right now
69-
bool now = false;
70-
71-
// how many ticks to wait between cycles when enabled, -1 means unset
72-
int32_t ticks = -1;
73-
74-
// example params of different types
75-
df::coord start;
76-
string format;
77-
vector<string*> list; // note this must be a vector of pointers, not objects
78-
79-
static struct_identity _identity;
80-
};
81-
static const struct_field_info command_options_fields[] = {
82-
{ struct_field_info::PRIMITIVE, "help", offsetof(command_options, help), &df::identity_traits<bool>::identity, 0, 0 },
83-
{ struct_field_info::PRIMITIVE, "now", offsetof(command_options, now), &df::identity_traits<bool>::identity, 0, 0 },
84-
{ struct_field_info::PRIMITIVE, "ticks", offsetof(command_options, ticks), &df::identity_traits<int32_t>::identity, 0, 0 },
85-
{ struct_field_info::SUBSTRUCT, "start", offsetof(command_options, start), &df::coord::_identity, 0, 0 },
86-
{ struct_field_info::PRIMITIVE, "format", offsetof(command_options, format), df::identity_traits<string>::get(), 0, 0 },
87-
{ struct_field_info::STL_VECTOR_PTR, "list", offsetof(command_options, list), df::identity_traits<string>::get(), 0, 0 },
88-
{ struct_field_info::END }
89-
};
90-
struct_identity command_options::_identity(sizeof(command_options), &df::allocator_fn<command_options>, NULL, "command_options", NULL, command_options_fields);
91-
9262
static command_result do_command(color_ostream &out, vector<string> &parameters);
9363
static void do_cycle(color_ostream &out);
9464

@@ -165,33 +135,6 @@ DFhackCExport command_result plugin_onupdate(color_ostream &out) {
165135
return CR_OK;
166136
}
167137

168-
// load the lua module associated with the plugin and parse the commandline
169-
// in lua (which has better facilities than C++ for string parsing).
170-
static bool get_options(color_ostream &out,
171-
command_options &opts,
172-
const vector<string> &parameters)
173-
{
174-
auto L = Lua::Core::State;
175-
Lua::StackUnwinder top(L);
176-
177-
if (!lua_checkstack(L, parameters.size() + 2) ||
178-
!Lua::PushModulePublic(
179-
out, L, ("plugins." + string(plugin_name)).c_str(),
180-
"parse_commandline")) {
181-
out.printerr("Failed to load %s Lua code\n", plugin_name);
182-
return false;
183-
}
184-
185-
Lua::Push(L, &opts);
186-
for (const string &param : parameters)
187-
Lua::Push(L, param);
188-
189-
if (!Lua::SafeCall(out, L, parameters.size() + 1, 0))
190-
return false;
191-
192-
return true;
193-
}
194-
195138
static command_result do_command(color_ostream &out, vector<string> &parameters) {
196139
// be sure to suspend the core if any DF state is read or modified
197140
CoreSuspender suspend;
@@ -201,20 +144,11 @@ static command_result do_command(color_ostream &out, vector<string> &parameters)
201144
return CR_FAILURE;
202145
}
203146

204-
command_options opts;
205-
if (!get_options(out, opts, parameters) || opts.help)
206-
return CR_WRONG_USAGE;
147+
// TODO: configuration logic
148+
// simple commandline parsing can be done in C++, but there are lua libraries
149+
// that can easily handle more complex commandlines. see the blueprint plugin
150+
// for an example.
207151

208-
if (opts.ticks > -1) {
209-
set_config_val(CONFIG_CYCLE_TICKS, opts.ticks);
210-
INFO(status,out).print("New cycle timer: %d ticks.\n", opts.ticks);
211-
}
212-
else if (opts.now) {
213-
do_cycle(out);
214-
}
215-
else {
216-
out.print("%s is %srunning\n", plugin_name, (is_enabled ? "" : "not "));
217-
}
218152
return CR_OK;
219153
}
220154

plugins/examples/simple_command_example.cpp

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <vector>
66

77
#include "Debug.h"
8-
#include "LuaTools.h"
98
#include "PluginManager.h"
109

1110
using std::string;
@@ -19,29 +18,6 @@ namespace DFHack {
1918
DBG_DECLARE(simple_command_example, log);
2019
}
2120

22-
// define the structure that will represent the possible commandline options
23-
struct command_options {
24-
// whether to display help
25-
bool help = false;
26-
27-
// example params of different types
28-
int32_t ticks = -1;
29-
df::coord start;
30-
string format;
31-
vector<string*> list; // note this must be a vector of pointers, not objects
32-
33-
static struct_identity _identity;
34-
};
35-
static const struct_field_info command_options_fields[] = {
36-
{ struct_field_info::PRIMITIVE, "help", offsetof(command_options, help), &df::identity_traits<bool>::identity, 0, 0 },
37-
{ struct_field_info::PRIMITIVE, "ticks", offsetof(command_options, ticks), &df::identity_traits<int32_t>::identity, 0, 0 },
38-
{ struct_field_info::SUBSTRUCT, "start", offsetof(command_options, start), &df::coord::_identity, 0, 0 },
39-
{ struct_field_info::PRIMITIVE, "format", offsetof(command_options, format), df::identity_traits<string>::get(), 0, 0 },
40-
{ struct_field_info::STL_VECTOR_PTR, "list", offsetof(command_options, list), df::identity_traits<string>::get(), 0, 0 },
41-
{ struct_field_info::END }
42-
};
43-
struct_identity command_options::_identity(sizeof(command_options), &df::allocator_fn<command_options>, NULL, "command_options", NULL, command_options_fields);
44-
4521
static command_result do_command(color_ostream &out, vector<string> &parameters);
4622

4723
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
5531
return CR_OK;
5632
}
5733

58-
// load the lua module associated with the plugin and parse the commandline
59-
// in lua (which has better facilities than C++ for string parsing).
60-
static bool get_options(color_ostream &out,
61-
command_options &opts,
62-
const vector<string> &parameters)
63-
{
64-
auto L = Lua::Core::State;
65-
Lua::StackUnwinder top(L);
66-
67-
if (!lua_checkstack(L, parameters.size() + 2) ||
68-
!Lua::PushModulePublic(
69-
out, L, ("plugins." + string(plugin_name)).c_str(),
70-
"parse_commandline")) {
71-
out.printerr("Failed to load %s Lua code\n", plugin_name);
72-
return false;
73-
}
74-
75-
Lua::Push(L, &opts);
76-
for (const string &param : parameters)
77-
Lua::Push(L, param);
78-
79-
if (!Lua::SafeCall(out, L, parameters.size() + 1, 0))
80-
return false;
81-
82-
return true;
83-
}
84-
8534
static command_result do_command(color_ostream &out, vector<string> &parameters) {
8635
// be sure to suspend the core if any DF state is read or modified
8736
CoreSuspender suspend;
8837

89-
command_options opts;
90-
if (!get_options(out, opts, parameters) || opts.help)
91-
return CR_WRONG_USAGE;
92-
9338
// TODO: command logic
9439

9540
return CR_OK;

plugins/examples/skeleton.cpp

Lines changed: 4 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
#include "Core.h"
1717
#include "Debug.h"
18-
#include "LuaTools.h"
1918
#include "PluginManager.h"
2019

2120
#include "modules/Persistence.h"
@@ -157,67 +156,6 @@ DFhackCExport command_result plugin_load_data (color_ostream &out) {
157156
return CR_OK;
158157
}
159158

160-
// define the structure that will represent the possible commandline options
161-
struct command_options {
162-
// whether to display help
163-
bool help = false;
164-
165-
// whether to run a cycle right now
166-
bool now = false;
167-
168-
// how many ticks to wait between cycles when enabled, -1 means unset
169-
int32_t ticks = -1;
170-
171-
// example params of different types
172-
df::coord start;
173-
string format;
174-
vector<string*> list; // note this must be a vector of pointers, not objects
175-
176-
static struct_identity _identity;
177-
};
178-
static const struct_field_info command_options_fields[] = {
179-
{ struct_field_info::PRIMITIVE, "help", offsetof(command_options, help), &df::identity_traits<bool>::identity, 0, 0 },
180-
{ struct_field_info::PRIMITIVE, "now", offsetof(command_options, now), &df::identity_traits<bool>::identity, 0, 0 },
181-
{ struct_field_info::PRIMITIVE, "ticks", offsetof(command_options, ticks), &df::identity_traits<int32_t>::identity, 0, 0 },
182-
{ struct_field_info::SUBSTRUCT, "start", offsetof(command_options, start), &df::coord::_identity, 0, 0 },
183-
{ struct_field_info::PRIMITIVE, "format", offsetof(command_options, format), df::identity_traits<string>::get(), 0, 0 },
184-
{ struct_field_info::STL_VECTOR_PTR, "list", offsetof(command_options, list), df::identity_traits<string>::get(), 0, 0 },
185-
{ struct_field_info::END }
186-
};
187-
struct_identity command_options::_identity(sizeof(command_options), &df::allocator_fn<command_options>, NULL, "command_options", NULL, command_options_fields);
188-
189-
// load the lua module associated with the plugin and parse the commandline
190-
// in lua (which has better facilities than C++ for string parsing). You should
191-
// create a file named after your plugin in the plugins/lua directory. This
192-
// example expects you to define a global function in that file named
193-
// "parse_commandline" that takes the options struct defined above along with
194-
// the commandline parameters. It should parse the parameters and set data in
195-
// the options structure. See plugins/lua/skeleton.lua for an example.
196-
static bool get_options(color_ostream &out,
197-
command_options &opts,
198-
const vector<string> &parameters)
199-
{
200-
auto L = Lua::Core::State;
201-
Lua::StackUnwinder top(L);
202-
203-
if (!lua_checkstack(L, parameters.size() + 2) ||
204-
!Lua::PushModulePublic(
205-
out, L, ("plugins." + string(plugin_name)).c_str(),
206-
"parse_commandline")) {
207-
out.printerr("Failed to load %s Lua code\n", plugin_name);
208-
return false;
209-
}
210-
211-
Lua::Push(L, &opts);
212-
for (const string &param : parameters)
213-
Lua::Push(L, param);
214-
215-
if (!Lua::SafeCall(out, L, parameters.size() + 1, 0))
216-
return false;
217-
218-
return true;
219-
}
220-
221159
// This is the callback we registered in plugin_init. Note that while plugin
222160
// callbacks are called with the core suspended, command callbacks are called
223161
// 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
233171
// Return CR_WRONG_USAGE to print out your help text. The help text is
234172
// sourced from the associated rst file in docs/plugins/. The same help will
235173
// also be returned by 'help your-command'.
236-
command_options opts;
237-
if (!get_options(out, opts, parameters) || opts.help)
238-
return CR_WRONG_USAGE;
174+
175+
// simple commandline parsing can be done in C++, but there are lua libraries
176+
// that can easily handle more complex commandlines. see the blueprint plugin
177+
// for an example.
239178

240179
// TODO: do something according to the flags set in the options struct
241180

plugins/examples/ui_addition_example.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "df/viewscreen_titlest.h"
1111

1212
#include "Debug.h"
13-
#include "LuaTools.h"
1413
#include "PluginManager.h"
1514
#include "VTableInterpose.h"
1615

plugins/lua/skeleton.lua

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)