46 #include <boost/scoped_ptr.hpp>
60 #define DBG_LUA LOG_STREAM(debug, log_scripting_lua)
61 #define LOG_LUA LOG_STREAM(info, log_scripting_lua)
62 #define WRN_LUA LOG_STREAM(warn, log_scripting_lua)
63 #define ERR_LUA LOG_STREAM(err, log_scripting_lua)
66 static const char *
Gen =
"name generator";
81 if(vop ==
OP_INVALID)
return luaL_argerror(L, 2,
"unknown version comparison operator - allowed are ==, !=, <, <=, > and >=");
97 DBG_LUA <<
"intf_print called:\n";
100 for (
size_t i = 1;
i <= nargs; ++
i) {
109 DBG_LUA <<
"'" << str <<
"'\n";
122 ERR_LUA <<
"Cannot show dialog, no video object is available to this lua kernel.";
133 ERR_LUA <<
"Cannot show dialog, no video object is available to this lua kernel.";
144 ERR_LUA <<
"Cannot show dialog, no video object is available to this lua kernel.";
156 ERR_LUA <<
"Cannot show dialog, no video object is available to this lua kernel.";
162 std::string message =
"There is already an external logger attached to this lua kernel, you cannot open the lua console right now.";
189 if(type ==
"markov" || type ==
"markov_chain") {
190 std::vector<std::string>
input;
192 input = lua_check<std::vector<std::string>>(L, 2);
200 assert(static_cast<void*>(gen) == dynamic_cast<markov_generator*>(gen));
201 }
else if(type ==
"context_free" || type ==
"cfg" || type ==
"CFG") {
204 std::map<std::string, std::vector<std::string>>
data;
207 lua_pushstring(L,
"CFG generator: invalid nonterminal name (must be a string)");
213 data[
lua_tostring(L,-2)] = lua_check<std::vector<std::string>>(L, -1);
215 lua_pushstring(L,
"CFG generator: invalid noterminal value (must be a string or list of strings)");
226 assert(static_cast<void*>(gen) == dynamic_cast<context_free_grammar_generator*>(gen));
229 return luaL_argerror(L, 1,
"should be either 'markov_chain' or 'context_free'");
231 static const char*
const generic_err =
"error initializing name generator";
252 template <member_callback method>
254 return ((lua_kernel_base::get_lua_kernel<lua_kernel_base>(L)).*method)(L);
270 cmd_log_ <<
"Adding standard libs...\n";
272 static const luaL_Reg safe_libs[] = {
295 if(strcmp(
function,
"clock") == 0 || strcmp(
function,
"date") == 0
296 || strcmp(
function,
"time") == 0 || strcmp(
function,
"difftime") == 0)
continue;
308 if(strcmp(
function,
"traceback") == 0 || strcmp(
function,
"getinfo") == 0)
continue;
321 cmd_log_ <<
"Adding error handler...\n";
341 cmd_log_ <<
"Adding boost function proxy...\n";
346 cmd_log_ <<
"Registering basic wesnoth API...\n";
348 static luaL_Reg const callbacks[] = {
364 {
"dofile", &dispatch<&lua_kernel_base::intf_dofile> },
365 {
"require", &dispatch<&lua_kernel_base::intf_require> },
366 {
"show_dialog", &dispatch<&lua_kernel_base::intf_show_dialog> },
367 {
"show_message_dialog", &dispatch<&lua_kernel_base::intf_show_message_dialog> },
368 {
"show_popup_dialog", &dispatch<&lua_kernel_base::intf_show_popup_dialog> },
369 {
"show_lua_console", &dispatch<&lua_kernel_base::intf_show_lua_console> },
394 cmd_log_ <<
"Redirecting print function...\n";
410 cmd_log_ <<
"Adding map_location table...\n";
412 static luaL_Reg const map_callbacks[] = {
436 cmd_log_ <<
"Adding rng tables...\n";
439 cmd_log_ <<
"Adding name generator metatable...\n";
462 cmd_log_ <<
"Failed to activate strict mode.\n";
464 cmd_log_ <<
"Activated strict mode.\n";
469 cmd_log_ <<
"Error: failed to load ilua.\n";
515 int error_handler_index =
lua_gettop(L) - nArgs - 1;
518 int errcode =
lua_pcall(L, nArgs, nRets, -2 - nArgs);
530 context +=
"Lua runtime error";
532 context +=
"Lua error in attached debugger";
534 context +=
"Lua out of memory error";
536 context +=
"Lua error in garbage collection metamethod";
538 context +=
"unknown lua error";
543 e_h(message.c_str(), context.c_str());
558 std::string context =
"When parsing a string to lua, ";
561 context +=
" a syntax error";
563 context +=
" a memory error";
565 context +=
" an error in garbage collection metamethod";
567 context +=
" an unknown error";
572 e_h(message.c_str(), context.c_str());
586 run(cfg[
"code"].str().c_str(), nArgs);
654 luaL_argerror(L, 1,
"found a null string argument to wesnoth require");
672 DBG_LUA <<
"require: loaded a file, now calling it\n";
706 std::vector<std::string> ret;
729 std::vector<std::string> ret;
737 size_t idx = var_path.find(
'.');
739 while (idx != std::string::npos ) {
749 var_path = var_path.substr(idx+1);
750 idx = var_path.find(
'.');
int dispatch(lua_State *L)
int intf_set_dialog_active(lua_State *L)
Sets a widget's state to active or inactive.
void interactive_run(char const *prog)
Tests if a program resolves to an expression, and pretty prints it if it is, otherwise it runs it nor...
LUALIB_API void luaL_requiref(lua_State *L, const char *modname, lua_CFunction openf, int glb)
int show_lua_console(lua_State *, CVideo &video, lua_kernel_base *lk)
LUA_API void lua_pushlightuserdata(lua_State *L, void *p)
static int intf_name_generator(lua_State *L)
#define lua_pushcfunction(L, f)
LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
std::string register_tstring_metatable(lua_State *L)
Adds the tstring metatable.
int intf_add_dialog_tree_node(lua_State *L)
Sets a widget's state to active or inactive.
int intf_read_file(lua_State *L)
Checks if a file exists (not necessarily a Lua script).
LUALIB_API lua_State * luaL_newstate(void)
LUAMOD_API int luaopen_debug(lua_State *L)
int intf_vector_zero(lua_State *L)
Expose map_location::ZERO to lua.
VERSION_COMP_OP parse_version_op(const std::string &op_str)
LUA_API void lua_settop(lua_State *L, int idx)
int intf_have_file(lua_State *L)
Checks if a file exists (not necessarily a Lua script).
LUALIB_API int luaL_loadstring(lua_State *L, const char *s)
const char * what() const
LUA_API void lua_pushboolean(lua_State *L, int b)
rng * generator
This generator is automatically synced during synced context.
std::function< void(char const *, char const *)> error_handler
GLuint GLuint GLsizei GLenum type
LUA_API void lua_getglobal(lua_State *L, const char *var)
GLenum GLenum GLenum input
int intf_get_direction(lua_State *L)
Expose map_location::get_direction function to lua Arg 1: a location Arg 2: a direction Arg 3: number...
LUA_API int lua_gettop(lua_State *L)
virtual bool is_valid() const
LUAMOD_API int luaopen_math(lua_State *L)
LUA_API void lua_settable(lua_State *L, int idx)
#define LUA_KERNEL_BASE_OFFSET
int intf_textdomain(lua_State *L)
Creates an interface for gettext.
GLfloat GLfloat GLfloat v2
#define luaL_getmetatable(L, n)
virtual ~lua_kernel_base()
LUA_API int lua_isuserdata(lua_State *L, int idx)
int intf_remove_dialog_item(lua_State *L)
Removes an entry from a list.
int intf_show_dialog(lua_State *L)
int intf_set_dialog_markup(lua_State *L)
Enables/disables Pango markup on the label of a widget of the current dialog.
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
static lg::log_domain log_scripting_lua("scripting/lua")
LUAMOD_API int luaopen_base(lua_State *L)
LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int narg, lua_Integer def)
LUA_API void * lua_newuserdata(lua_State *L, size_t size)
int intf_distance_between(lua_State *L)
Expose map_location distance_between.
int intf_set_dialog_focus(lua_State *L)
Sets a widget to have the focus.
lua_kernel_base(CVideo *ptr)
int intf_rotate_right_around_center(lua_State *L)
Expose map_location::rotate_right_around_center to lua.
int intf_parse_direction(lua_State *L)
Expose map_location parse_direction.
int show_dialog(lua_State *L, CVideo &video)
Displays a window.
#define lua_pcall(L, n, r, f)
int intf_get_relative_dir(lua_State *L)
Expose map_location get_relative_dir.
LUAMOD_API int luaopen_coroutine(lua_State *L)
LUA_API void lua_setglobal(lua_State *L, const char *var)
LUAMOD_API int luaopen_os(lua_State *L)
void throwing_run(char const *prog, int nArgs)
Runs a plain script, but reports errors by throwing lua_error.
LUAMOD_API int luaopen_string(lua_State *L)
LUA_API int lua_isstring(lua_State *L, int idx)
int intf_show_lua_console(lua_State *L)
virtual ~name_generator()
LUAMOD_API int luaopen_table(lua_State *L)
LUAMOD_API int luaopen_package(lua_State *L)
void load_package()
Loads the "package" package into the Lua environment.
LUA_API void lua_close(lua_State *L)
bool protected_call(int nArgs, int nRets, error_handler)
int show_message_dialog(lua_State *L, CVideo &video)
Displays a message window.
GLenum GLuint GLsizei const char * buf
LUA_API int lua_setmetatable(lua_State *L, int objindex)
static int impl_name_generator_collect(lua_State *L)
LUA_API void lua_remove(lua_State *L, int idx)
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
int intf_set_dialog_canvas(lua_State *L)
Sets a canvas on a widget of the current dialog.
LUA_API void lua_pushnil(lua_State *L)
int intf_tiles_adjacent(lua_State *L)
Expose map_location tiles_adjacent.
bool load_string(char const *prog, error_handler)
int(lua_kernel_base::* member_callback)(lua_State *L)
void run(char const *prog, int nArgs=0)
Runs a plain script.
int intf_vector_negation(lua_State *L)
Expose map_location::vector_negation to lua.
LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
luatypekey const executeKey
LUA_API void * lua_touserdata(lua_State *L, int idx)
int intf_write_direction(lua_State *L)
Expose map_location write_direction.
static void rethrow()
Rethrows the stored exception.
int intf_print(lua_State *L)
Replacement print function – instead of printing to std::cout, print to the command log...
int intf_set_dialog_value(lua_State *L)
Sets the value of a widget on the current dialog.
virtual std::string generate() const =0
LUA_API void lua_rawset(lua_State *L, int idx)
std::string register_gettext_metatable(lua_State *L)
Adds the gettext metatable.
std::ostream * external_log_
int intf_get_in_basis_N_NE(lua_State *L)
Expose map_location get_in_basis_N_NE.
int show_popup_dialog(lua_State *L, CVideo &video)
Displays a popup message.
LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *extramsg)
#define lua_tostring(L, i)
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
static int intf_compare_versions(lua_State *L)
Compares 2 version strings - which is newer.
LUA_API void lua_insert(lua_State *L, int idx)
LUA_API void lua_pushvalue(lua_State *L, int idx)
int intf_get_adjacent_tiles(lua_State *L)
Expose map_location get_adjacent_tiles.
int load_file(lua_State *L)
Loads a Lua file and pushes the contents on the stack.
virtual void log_error(char const *msg, char const *context="Lua error")
#define lua_call(L, n, r)
int intf_set_dialog_callback(lua_State *L)
Sets a callback on a widget of the current dialog.
Represents version numbers.
virtual boost::uint32_t get_random_seed()
LUA_API void lua_gettable(lua_State *L, int idx)
std::vector< std::string > get_global_var_names()
Gets all the global variable names in the Lua environment.
int intf_vector_sum(lua_State *L)
Expose map_location::vector_sum to lua.
#define LUA_REGISTRYINDEX
static int impl_name_generator_call(lua_State *L)
std::vector< std::string > parenthetical_split(std::string const &val, const char separator, std::string const &left, std::string const &right, const int flags)
Splits a string based either on a separator where text within parenthesis is protected from splitting...
LUA_API int lua_error(lua_State *L)
std::vector< std::string > get_attribute_names(const std::string &var_path)
Gets all attribute names of an extended variable name.
#define lua_istable(L, n)
virtual void throw_exception(char const *msg, char const *context="Lua error")
void luaW_pushconfig(lua_State *L, config const &cfg)
Converts a config object to a Lua table pushed at the top of the stack.
config & child(const std::string &key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Standard logging facilities (interface).
GLsizei GLenum GLuint GLuint GLsizei char * message
void register_metatable(lua_State *L)
LUAMOD_API int luaopen_bit32(lua_State *L)
std::vector< std::string > split(std::string const &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
virtual std::string my_name()
bool do_version_check(const version_info &a, VERSION_COMP_OP op, const version_info &b)
int intf_get_dialog_value(lua_State *L)
Gets the value of a widget on the current dialog.
int intf_show_message_dialog(lua_State *L)
LUALIB_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup)
A config object defines a single node in a WML file, with access to child nodes.
int intf_dofile(lua_State *L)
Loads and executes a Lua file.
int intf_tovconfig(lua_State *L)
Creates a vconfig containing the WML table.
Interfaces for manipulating version numbers of engine, add-ons, etc.
LUA_API void lua_rawget(lua_State *L, int idx)
GLsizei const GLcharARB ** string
int intf_require(lua_State *L)
Loads and executes a Lua file, if there is no corresponding entry in wesnoth.package.
void load_tables(lua_State *L)
Creates the metatable for RNG objects, and adds the Rng table which contains the constructor.
void run_lua_tag(const config &cfg)
Runs a [lua] tag.
LUA_API const char * lua_pushstring(lua_State *L, const char *s)
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
int intf_set_dialog_visible(lua_State *L)
Sets the visiblity of a widget in the current dialog.
LUA_API int lua_next(lua_State *L, int idx)
static lua_kernel_base *& get_lua_kernel_base_ptr(lua_State *L)
int intf_show_popup_dialog(lua_State *L)
#define luaL_checkstring(L, n)
Error used to report an error in a lua script or in the lua interpreter.