The Battle for Wesnoth  1.15.0-dev
lua_kernel_base.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by Chris Beck <render787@gmail.com>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
16 
17 #include "game_config.hpp"
18 #include "game_errors.hpp"
20 #include "log.hpp"
21 #include "lua_jailbreak_exception.hpp" // for lua_jailbreak_exception
22 #include "random.hpp"
23 #include "seed_rng.hpp"
24 #include "deprecation.hpp"
25 
26 #ifdef DEBUG_LUA
27 #include "scripting/debug_lua.hpp"
28 #endif
29 
30 #include "scripting/lua_common.hpp"
34 #include "scripting/lua_gui2.hpp"
36 #include "scripting/lua_rng.hpp"
37 #include "scripting/push_check.hpp"
38 
39 #include "version.hpp" // for do_version_check, etc
40 #include "image.hpp"
41 
42 #include "formula/string_utils.hpp"
43 #include "utils/functional.hpp"
44 #include "utils/name_generator.hpp"
47 #include "variable.hpp" // for config_variable_set
48 
49 #include <cstring>
50 #include <exception>
51 #include <new>
52 #include <string>
53 #include <sstream>
54 #include <vector>
55 
56 #include "lua/lauxlib.h"
57 #include "lua/lua.h"
58 #include "lua/lualib.h"
59 
60 static lg::log_domain log_scripting_lua("scripting/lua");
61 static lg::log_domain log_user("scripting/lua/user");
62 #define DBG_LUA LOG_STREAM(debug, log_scripting_lua)
63 #define LOG_LUA LOG_STREAM(info, log_scripting_lua)
64 #define WRN_LUA LOG_STREAM(warn, log_scripting_lua)
65 #define ERR_LUA LOG_STREAM(err, log_scripting_lua)
66 
67 // Registry key for metatable
68 static const char * Gen = "name generator";
69 
70 // Callback implementations
71 
72 /**
73  * Compares 2 version strings - which is newer.
74  * - Args 1,3: version strings
75  * - Arg 2: comparison operator (string)
76  * - Ret 1: comparison result
77  */
79 {
80  char const *v1 = luaL_checkstring(L, 1);
81 
83  if(vop == OP_INVALID) return luaL_argerror(L, 2, "unknown version comparison operator - allowed are ==, !=, <, <=, > and >=");
84 
85  char const *v2 = luaL_checkstring(L, 3);
86 
87  const bool result = do_version_check(version_info(v1), vop, version_info(v2));
88  lua_pushboolean(L, result);
89 
90  return 1;
91 }
92 
93 /**
94  * Replacement print function -- instead of printing to std::cout, print to the command log.
95  * Intended to be bound to this' command_log at registration time.
96  */
98 {
99  DBG_LUA << "intf_print called:\n";
100  std::size_t nargs = lua_gettop(L);
101 
102  lua_getglobal(L, "tostring");
103  for (std::size_t i = 1; i <= nargs; ++i) {
104  lua_pushvalue(L, -1); // function to call: "tostring"
105  lua_pushvalue(L, i); // value to pass through tostring() before printing
106  lua_call(L, 1, 1);
107  const char * str = lua_tostring(L, -1);
108  if (!str) {
109  LOG_LUA << "'tostring' must return a value to 'print'\n";
110  str = "";
111  }
112  if (i > 1) {
113  cmd_log_ << "\t"; //separate multiple args with tab character
114  }
115  cmd_log_ << str;
116  DBG_LUA << "'" << str << "'\n";
117  lua_pop(L, 1); // Pop the output of tostrring()
118  }
119  lua_pop(L, 1); // Pop 'tostring' global
120 
121  cmd_log_ << "\n";
122  DBG_LUA << "\n";
123 
124  return 0;
125 }
126 
127 // The show lua console callback is similarly a method of lua kernel
129 {
130  if (cmd_log_.external_log_) {
131  std::string message = "There is already an external logger attached to this lua kernel, you cannot open the lua console right now.";
132  log_error(message.c_str());
133  cmd_log_ << message << "\n";
134  return 0;
135  }
136 
137  return lua_gui2::show_lua_console(L, this);
138 }
139 
141 {
142  name_generator* gen = static_cast<name_generator*>(lua_touserdata(L, 1));
143  lua_pushstring(L, gen->generate().c_str());
144  return 1;
145 }
146 
148 {
149  name_generator* gen = static_cast<name_generator*>(lua_touserdata(L, 1));
150  gen->~name_generator();
151  return 0;
152 }
153 
155 {
156  std::string type = luaL_checkstring(L, 1);
157  name_generator* gen = nullptr;
158  try {
159  if(type == "markov" || type == "markov_chain") {
160  std::vector<std::string> input;
161  if(lua_istable(L, 2)) {
162  input = lua_check<std::vector<std::string>>(L, 2);
163  } else {
164  input = utils::parenthetical_split(luaL_checkstring(L, 2), ',');
165  }
166  int chain_sz = luaL_optinteger(L, 3, 2);
167  int max_len = luaL_optinteger(L, 4, 12);
168  gen = new(L) markov_generator(input, chain_sz, max_len);
169  // Ensure the pointer didn't change when cast
170  assert(static_cast<void*>(gen) == dynamic_cast<markov_generator*>(gen));
171  } else if(type == "context_free" || type == "cfg" || type == "CFG") {
172  if(lua_istable(L, 2)) {
173  std::map<std::string, std::vector<std::string>> data;
174  for(lua_pushnil(L); lua_next(L, 2); lua_pop(L, 1)) {
175  if(lua_type(L, -2) != LUA_TSTRING) {
176  lua_pushstring(L, "CFG generator: invalid nonterminal name (must be a string)");
177  return lua_error(L);
178  }
179  if(lua_isstring(L, -1)) {
180  data[lua_tostring(L,-2)] = utils::split(lua_tostring(L,-1),'|');
181  } else if(lua_istable(L, -1)) {
182  data[lua_tostring(L,-2)] = lua_check<std::vector<std::string>>(L, -1);
183  } else {
184  lua_pushstring(L, "CFG generator: invalid noterminal value (must be a string or list of strings)");
185  return lua_error(L);
186  }
187  }
188  if(!data.empty()) {
189  gen = new(L) context_free_grammar_generator(data);
190  }
191  } else {
193  }
194  if(gen) {
195  assert(static_cast<void*>(gen) == dynamic_cast<context_free_grammar_generator*>(gen));
196  }
197  } else {
198  return luaL_argerror(L, 1, "should be either 'markov_chain' or 'context_free'");
199  }
200  }
201  catch (const name_generator_invalid_exception& ex) {
202  lua_pushstring(L, ex.what());
203  return lua_error(L);
204  }
205 
206  // We set the metatable now, even if the generator is invalid, so that it
207  // will be properly collected if it was invalid.
209  lua_setmetatable(L, -2);
210 
211  return 1;
212 }
213 
214 /**
215 * Returns a random numer, same interface as math.random.
216 */
217 static int intf_random(lua_State *L)
218 {
219  if (lua_isnoneornil(L, 1)) {
220  double r = static_cast<double>(randomness::generator->next_random());
221  double r_max = static_cast<double>(std::numeric_limits<uint32_t>::max());
222  lua_push(L, r / (r_max + 1));
223  return 1;
224  }
225  else {
226  int32_t min;
227  int32_t max;
228  if (lua_isnumber(L, 2)) {
229  min = lua_check<int32_t>(L, 1);
230  max = lua_check<int32_t>(L, 2);
231  }
232  else {
233  min = 1;
234  max = lua_check<int32_t>(L, 1);
235  }
236  if (min > max) {
237  return luaL_argerror(L, 1, "min > max");
238  }
239  lua_push(L, randomness::generator->get_random_int(min, max));
240  return 1;
241  }
242 }
243 
245 {
246  config cfg = luaW_checkconfig(L, 1);
247  config filter = luaW_checkconfig(L, 2);
248  lua_pushboolean(L, cfg.matches(filter));
249  return 1;
250 }
251 
252 /**
253 * Logs a message
254 * Arg 1: (optional) Logger
255 * Arg 2: Message
256 */
257 static int intf_log(lua_State *L) {
258  const std::string& logger = lua_isstring(L, 2) ? luaL_checkstring(L, 1) : "";
259  std::string msg = lua_isstring(L, 2) ? luaL_checkstring(L, 2) : luaL_checkstring(L, 1);
260  if(msg.empty() || msg[msg.size() - 1] != '\n') {
261  msg += '\n';
262  }
263 
264  if(logger == "err" || logger == "error") {
265  LOG_STREAM(err, log_user) << msg;
266  } else if(logger == "warn" || logger == "wrn" || logger == "warning") {
268  } else if((logger == "debug" || logger == "dbg")) {
270  } else {
272  }
273  return 0;
274 }
275 
276 /**
277  * Logs a deprecation message. See deprecation.cpp for details
278  * Arg 1: Element to be deprecated.
279  * Arg 2: Deprecation level.
280  * Arg 3: Version when element may be removed.
281  * Arg 4: Additional detail message.
282  */
284  const std::string elem = luaL_checkstring(L, 1);
285  // This could produce an invalid deprecation level, but that possibility is handled in deprecated_message()
287  const std::string ver_str = lua_isnoneornil(L, 3) ? "" : luaL_checkstring(L, 3);
288  const std::string detail = luaW_checktstring(L, 4);
289  const version_info ver = ver_str.empty() ? game_config::wesnoth_version.str() : ver_str;
290  const std::string msg = deprecated_message(elem, level, ver, detail);
291  if(level < DEP_LEVEL::INDEFINITE || level >= DEP_LEVEL::REMOVED) {
292  // Invalid deprecation level or level 4 deprecation should raise an interpreter error
293  lua_push(L, msg);
294  return lua_error(L);
295  }
296  return 0;
297 }
298 
299 /**
300 * Gets the dimension of an image.
301 * - Arg 1: string.
302 * - Ret 1: width.
303 * - Ret 2: height.
304 */
306  char const *m = luaL_checkstring(L, 1);
307  image::locator img(m);
308  if(!img.file_exists()) return 0;
309  surface s = get_image(img);
310  lua_pushinteger(L, s->w);
311  lua_pushinteger(L, s->h);
312  return 2;
313 }
314 
315 /**
316 * Returns the time stamp, exactly as [set_variable] time=stamp does.
317 * - Ret 1: integer
318 */
320  lua_pushinteger(L, SDL_GetTicks());
321  return 1;
322 }
323 
324 static int intf_format(lua_State* L)
325 {
326  config cfg = luaW_checkconfig(L, 2);
327  config_variable_set variables(cfg);
328  if(lua_isstring(L, 1)) {
329  std::string str = lua_tostring(L, 1);
331  return 1;
332  }
333  t_string str = luaW_checktstring(L, 1);
335  return 1;
336 }
337 
338 template<bool conjunct>
340 {
341  const t_string empty = luaW_checktstring(L, 1);
342  auto values = lua_check<std::vector<t_string>>(L, 2);
343  lua_push(L, (conjunct ? utils::format_conjunct_list : utils::format_disjunct_list)(empty, values));
344  return 1;
345 }
346 
347 /**
348 * Dumps a wml table or userdata wml object into a pretty string.
349 * - Arg 1: wml table or vconfig userdata
350 * - Ret 1: string
351 */
352 static int intf_debug(lua_State* L) {
353  const config& arg = luaW_checkconfig(L, 1);
354  const std::string& result = arg.debug();
355  lua_pushstring(L, result.c_str());
356  return 1;
357 }
358 
359 // End Callback implementations
360 
361 // Template which allows to push member functions to the lua kernel base into lua as C functions, using a shim
363 
364 template <member_callback method>
366  return ((lua_kernel_base::get_lua_kernel<lua_kernel_base>(L)).*method)(L);
367 }
368 
369 // Ctor, initialization
371  : mState(luaL_newstate())
372  , cmd_log_()
373 {
375  lua_State *L = mState;
376 
377  cmd_log_ << "Initializing " << my_name() << "...\n";
378 
379  // Open safe libraries.
380  // Debug and OS are not, but most of their functions will be disabled below.
381  cmd_log_ << "Adding standard libs...\n";
382 
383  static const luaL_Reg safe_libs[] {
384  { "", luaopen_base },
385  { "table", luaopen_table },
386  { "string", luaopen_string },
387  { "math", luaopen_math },
388  { "coroutine", luaopen_coroutine },
389  { "debug", luaopen_debug },
390  { "os", luaopen_os },
391  { "utf8", luaopen_utf8 }, // added in Lua 5.3
392  { nullptr, nullptr }
393  };
394  for (luaL_Reg const *lib = safe_libs; lib->func; ++lib)
395  {
396  luaL_requiref(L, lib->name, lib->func, 1);
397  lua_pop(L, 1); /* remove lib */
398  }
399 
400  // Disable functions from os which we don't want.
401  lua_getglobal(L, "os");
402  lua_pushnil(L);
403  while(lua_next(L, -2) != 0) {
404  lua_pop(L, 1);
405  char const* function = lua_tostring(L, -1);
406  if(strcmp(function, "clock") == 0 || strcmp(function, "date") == 0
407  || strcmp(function, "time") == 0 || strcmp(function, "difftime") == 0) continue;
408  lua_pushnil(L);
409  lua_setfield(L, -3, function);
410  }
411  lua_pop(L, 1);
412 
413  // Disable functions from debug which we don't want.
414  lua_getglobal(L, "debug");
415  lua_pushnil(L);
416  while(lua_next(L, -2) != 0) {
417  lua_pop(L, 1);
418  char const* function = lua_tostring(L, -1);
419  if(strcmp(function, "traceback") == 0 || strcmp(function, "getinfo") == 0) continue; //traceback is needed for our error handler
420  lua_pushnil(L); //getinfo is needed for ilua strict mode
421  lua_setfield(L, -3, function);
422  }
423  lua_pop(L, 1);
424 
425  // Delete dofile and loadfile.
426  lua_pushnil(L);
427  lua_setglobal(L, "dofile");
428  lua_pushnil(L);
429  lua_setglobal(L, "loadfile");
430 
431  // Store the error handler.
432  cmd_log_ << "Adding error handler...\n";
434 
435  // Create the gettext metatable.
437 
438  // Create the tstring metatable.
440 
441 
442  lua_settop(L, 0);
443 
444  // Define the CPP_function metatable ( so we can override print to point to a C++ member function, add "show_dialog" for this kernel, etc. )
445  cmd_log_ << "Adding boost function proxy...\n";
446 
448 
449  // Add some callback from the wesnoth lib
450  cmd_log_ << "Registering basic wesnoth API...\n";
451 
452  static luaL_Reg const callbacks[] {
453  { "compare_versions", &intf_compare_versions },
454  { "debug", &intf_debug },
455  { "deprecated_message", &intf_deprecated_message },
456  { "have_file", &lua_fileops::intf_have_file },
457  { "read_file", &lua_fileops::intf_read_file },
458  { "textdomain", &lua_common::intf_textdomain },
459  { "tovconfig", &lua_common::intf_tovconfig },
460  { "get_dialog_value", &lua_gui2::intf_get_dialog_value },
461  { "set_dialog_active", &lua_gui2::intf_set_dialog_active },
462  { "set_dialog_visible", &lua_gui2::intf_set_dialog_visible },
463  { "add_dialog_tree_node", &lua_gui2::intf_add_dialog_tree_node },
464  { "add_widget_definition", &lua_gui2::intf_add_widget_definition },
465  { "set_dialog_callback", &lua_gui2::intf_set_dialog_callback },
466  { "set_dialog_canvas", &lua_gui2::intf_set_dialog_canvas },
467  { "set_dialog_focus", &lua_gui2::intf_set_dialog_focus },
468  { "set_dialog_markup", &lua_gui2::intf_set_dialog_markup },
469  { "set_dialog_value", &lua_gui2::intf_set_dialog_value },
470  { "remove_dialog_item", &lua_gui2::intf_remove_dialog_item },
471  { "dofile", &dispatch<&lua_kernel_base::intf_dofile> },
472  { "require", &dispatch<&lua_kernel_base::intf_require> },
473  { "kernel_type", &dispatch<&lua_kernel_base::intf_kernel_type> },
474  { "show_dialog", &lua_gui2::show_dialog },
475  { "show_menu", &lua_gui2::show_menu },
476  { "show_message_dialog", &lua_gui2::show_message_dialog },
477  { "show_popup_dialog", &lua_gui2::show_popup_dialog },
478  { "show_story", &lua_gui2::show_story },
479  { "show_message_box", &lua_gui2::show_message_box },
480  { "show_lua_console", &dispatch<&lua_kernel_base::intf_show_lua_console> },
481  { "compile_formula", &lua_formula_bridge::intf_compile_formula},
482  { "eval_formula", &lua_formula_bridge::intf_eval_formula},
483  { "name_generator", &intf_name_generator },
484  { "random", &intf_random },
485  { "wml_matches_filter", &intf_wml_matches_filter },
486  { "log", &intf_log },
487  { "get_image_size", &intf_get_image_size },
488  { "get_time_stamp", &intf_get_time_stamp },
489  { "format", &intf_format },
490  { "format_conjunct_list", &intf_format_list<true> },
491  { "format_disjunct_list", &intf_format_list<false> },
492  { nullptr, nullptr }
493  };
494 
495  lua_getglobal(L, "wesnoth");
496  if (!lua_istable(L,-1)) {
497  lua_newtable(L);
498  }
499  luaL_setfuncs(L, callbacks, 0);
500  //lua_cpp::set_functions(L, cpp_callbacks, 0);
501  lua_setglobal(L, "wesnoth");
502 
503  // Override the print function
504  cmd_log_ << "Redirecting print function...\n";
505 
506  lua_getglobal(L, "print");
507  lua_setglobal(L, "std_print"); //storing original impl as 'std_print'
508  lua_settop(L, 0); //clear stack, just to be sure
509 
510  lua_pushcfunction(L, &dispatch<&lua_kernel_base::intf_print>);
511  lua_setglobal(L, "print");
512 
513  cmd_log_ << "Initializing package repository...\n";
514  // Create the package table.
515  lua_getglobal(L, "wesnoth");
516  lua_newtable(L);
517  lua_setfield(L, -2, "package");
518  lua_pop(L, 1);
519  lua_settop(L, 0);
520  lua_pushstring(L, "lua/package.lua");
521  int res = intf_require(L);
522  if(res != 1) {
523  cmd_log_ << "Error: Failed to initialize package repository. Falling back to less flexible C++ implementation.\n";
524  }
525 
526  // Get some callbacks for map locations
527  cmd_log_ << "Adding map table...\n";
528 
529  static luaL_Reg const map_callbacks[] {
530  { "get_direction", &lua_map_location::intf_get_direction },
531  { "vector_sum", &lua_map_location::intf_vector_sum },
532  { "vector_diff", &lua_map_location::intf_vector_diff },
533  { "vector_negation", &lua_map_location::intf_vector_negation },
534  { "rotate_right_around_center", &lua_map_location::intf_rotate_right_around_center },
535  { "tiles_adjacent", &lua_map_location::intf_tiles_adjacent },
536  { "get_adjacent_tiles", &lua_map_location::intf_get_adjacent_tiles },
537  { "distance_between", &lua_map_location::intf_distance_between },
538  { "get_in_basis_N_NE", &lua_map_location::intf_get_in_basis_N_NE },
539  { "get_relative_dir", &lua_map_location::intf_get_relative_dir },
540  { nullptr, nullptr }
541  };
542 
543  // Create the map_location table.
544  lua_getglobal(L, "wesnoth");
545  lua_newtable(L);
546  luaL_setfuncs(L, map_callbacks, 0);
547  lua_setfield(L, -2, "map");
548  lua_pop(L, 1);
549 
550  // Create the game_config variable with its metatable.
551  cmd_log_ << "Adding game_config table...\n";
552 
553  lua_getglobal(L, "wesnoth");
554  lua_newuserdata(L, 0);
555  lua_createtable(L, 0, 3);
556  lua_pushcfunction(L, &dispatch<&lua_kernel_base::impl_game_config_get>);
557  lua_setfield(L, -2, "__index");
558  lua_pushcfunction(L, &dispatch<&lua_kernel_base::impl_game_config_set>);
559  lua_setfield(L, -2, "__newindex");
560  lua_pushstring(L, "game config");
561  lua_setfield(L, -2, "__metatable");
562  lua_setmetatable(L, -2);
563  lua_setfield(L, -2, "game_config");
564  lua_pop(L, 1);
565 
566  // Add mersenne twister rng wrapper
567  cmd_log_ << "Adding rng tables...\n";
569 
570  cmd_log_ << "Adding name generator metatable...\n";
572  static luaL_Reg const generator[] {
573  { "__call", &impl_name_generator_call},
574  { "__gc", &impl_name_generator_collect},
575  { nullptr, nullptr}
576  };
577  luaL_setfuncs(L, generator, 0);
578 
579  // Create formula bridge metatables
581 
582  // Loading ilua:
583  cmd_log_ << "Loading ilua...\n";
584 
585  lua_settop(L, 0);
586  luaW_getglobal(L, "wesnoth", "require");
587  lua_pushstring(L, "lua/ilua.lua");
588  if(protected_call(1, 1)) {
589  //run "ilua.set_strict()"
590  lua_pushstring(L, "set_strict");
591  lua_gettable(L, -2);
592  if (!this->protected_call(0,0, std::bind(&lua_kernel_base::log_error, this, _1, _2))) {
593  cmd_log_ << "Failed to activate strict mode.\n";
594  } else {
595  cmd_log_ << "Activated strict mode.\n";
596  }
597 
598  lua_setglobal(L, "ilua"); //save ilua table as a global
599  } else {
600  cmd_log_ << "Error: failed to load ilua.\n";
601  }
602  lua_settop(L, 0);
603 }
604 
606 {
607  for (const auto& pair : this->registered_widget_definitions_) {
608  gui2::remove_single_widget_definition(std::get<0>(pair), std::get<1>(pair));
609  }
610  lua_close(mState);
611 }
612 
613 void lua_kernel_base::log_error(char const * msg, char const * context)
614 {
615  ERR_LUA << context << ": " << msg << '\n';
616 }
617 
618 void lua_kernel_base::throw_exception(char const * msg, char const * context)
619 {
620  throw game::lua_error(msg, context);
621 }
622 
623 bool lua_kernel_base::protected_call(int nArgs, int nRets)
624 {
625  error_handler eh = std::bind(&lua_kernel_base::log_error, this, _1, _2 );
626  return this->protected_call(nArgs, nRets, eh);
627 }
628 
629 bool lua_kernel_base::load_string(char const * prog)
630 {
631  error_handler eh = std::bind(&lua_kernel_base::log_error, this, _1, _2 );
632  return this->load_string(prog, eh);
633 }
634 
635 bool lua_kernel_base::protected_call(int nArgs, int nRets, error_handler e_h)
636 {
637  return this->protected_call(mState, nArgs, nRets, e_h);
638 }
639 
640 bool lua_kernel_base::protected_call(lua_State * L, int nArgs, int nRets, error_handler e_h)
641 {
642  int errcode = luaW_pcall_internal(L, nArgs, nRets);
643 
644  if (errcode != LUA_OK) {
645  char const * msg = lua_tostring(L, -1);
646 
647  std::string context = "When executing, ";
648  if (errcode == LUA_ERRRUN) {
649  context += "Lua runtime error: ";
650  } else if (errcode == LUA_ERRERR) {
651  context += "Lua error in attached debugger: ";
652  } else if (errcode == LUA_ERRMEM) {
653  context += "Lua out of memory error: ";
654  } else if (errcode == LUA_ERRGCMM) {
655  context += "Lua error in garbage collection metamethod: ";
656  } else {
657  context += "unknown lua error: ";
658  }
659  if(lua_isstring(L, -1)) {
660  context += msg ? msg : "null string";
661  } else {
662  context += lua_typename(L, lua_type(L, -1));
663  }
664 
665  lua_pop(L, 1);
666 
667  e_h(context.c_str(), "Lua Error");
668 
669  return false;
670  }
671 
672  return true;
673 }
674 
675 bool lua_kernel_base::load_string(char const * prog, error_handler e_h)
676 {
677  int errcode = luaL_loadstring(mState, prog);
678  if (errcode != LUA_OK) {
679  char const * msg = lua_tostring(mState, -1);
680  std::string message = msg ? msg : "null string";
681 
682  std::string context = "When parsing a string to lua, ";
683 
684  if (errcode == LUA_ERRSYNTAX) {
685  context += " a syntax error";
686  } else if(errcode == LUA_ERRMEM){
687  context += " a memory error";
688  } else if(errcode == LUA_ERRGCMM) {
689  context += " an error in garbage collection metamethod";
690  } else {
691  context += " an unknown error";
692  }
693 
694  lua_pop(mState, 1);
695 
696  e_h(message.c_str(), context.c_str());
697 
698  return false;
699  }
700  return true;
701 }
702 
704 {
705  int nArgs = 0;
706  if (const config& args = cfg.child("args")) {
707  luaW_pushconfig(this->mState, args);
708  ++nArgs;
709  }
710  this->run(cfg["code"].str().c_str(), nArgs);
711 }
712 // Call load_string and protected call. Make them throw exceptions.
713 //
714 void lua_kernel_base::throwing_run(const char * prog, int nArgs)
715 {
716  cmd_log_ << "$ " << prog << "\n";
717  error_handler eh = std::bind(&lua_kernel_base::throw_exception, this, _1, _2 );
718  this->load_string(prog, eh);
719  lua_insert(mState, -nArgs - 1);
720  this->protected_call(nArgs, 0, eh);
721 }
722 
723 // Do a throwing run, but if we catch a lua_error, reformat it with signature for this function and log it.
724 void lua_kernel_base::run(const char * prog, int nArgs)
725 {
726  try {
727  this->throwing_run(prog, nArgs);
728  } catch (const game::lua_error & e) {
729  cmd_log_ << e.what() << "\n";
730  lua_kernel_base::log_error(e.what(), "In function lua_kernel::run()");
731  }
732 }
733 
734 // Tests if a program resolves to an expression, and pretty prints it if it is, otherwise it runs it normally. Throws exceptions.
735 void lua_kernel_base::interactive_run(char const * prog) {
736  std::string experiment = "ilua._pretty_print(";
737  experiment += prog;
738  experiment += ")";
739 
740  error_handler eh = std::bind(&lua_kernel_base::throw_exception, this, _1, _2 );
741 
742  try {
743  // Try to load the experiment without syntax errors
744  this->load_string(experiment.c_str(), eh);
745  } catch (const game::lua_error &) {
746  this->throwing_run(prog, 0); // Since it failed, fall back to the usual throwing_run, on the original input.
747  return;
748  }
749  // experiment succeeded, now run but log normally.
750  cmd_log_ << "$ " << prog << "\n";
751  this->protected_call(0, 0, eh);
752 }
753 /**
754  * Loads and executes a Lua file.
755  * - Arg 1: string containing the file name.
756  * - Ret *: values returned by executing the file body.
757  */
759 {
760  luaL_checkstring(L, 1);
761  lua_rotate(L, 1, -1);
762  if (lua_fileops::load_file(L) != 1) return 0;
763  //^ should end with the file contents loaded on the stack. actually it will call lua_error otherwise, the return 0 is redundant.
764  lua_rotate(L, 1, 1);
765  // Using a non-protected call here appears to fix an issue in plugins.
766  // The protected call isn't technically necessary anyway, because this function is called from Lua code,
767  // which should already be in a protected environment.
768  lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
769  return lua_gettop(L);
770 }
771 
772 /**
773  * Loads and executes a Lua file, if there is no corresponding entry in wesnoth.package.
774  * Stores the result of the script in wesnoth.package and returns it.
775  * - Arg 1: string containing the file name.
776  * - Ret 1: value returned by the script.
777  */
779 {
780  const char * m = luaL_checkstring(L, 1);
781  if(!m) {
782  return luaL_argerror(L, 1, "found a null string argument to wesnoth require");
783  }
784 
785  // Check if there is already an entry.
786 
787  lua_getglobal(L, "wesnoth");
788  lua_pushstring(L, "package");
789  lua_rawget(L, -2);
790  lua_pushvalue(L, 1);
791  lua_rawget(L, -2);
792  if(!lua_isnil(L, -1) && !game_config::debug_lua) {
793  return 1;
794  }
795  lua_pop(L, 1);
796  lua_pushvalue(L, 1);
797  // stack is now [packagename] [wesnoth] [package] [packagename]
798 
799  if(lua_fileops::load_file(L) != 1) {
800  // should end with the file contents loaded on the stack. actually it will call lua_error otherwise, the return 0 is redundant.
801  // stack is now [packagename] [wesnoth] [package] [chunk]
802  return 0;
803  }
804  DBG_LUA << "require: loaded a file, now calling it\n";
805 
806  if (!this->protected_call(L, 0, 1, std::bind(&lua_kernel_base::log_error, this, _1, _2))) {
807  // historically if wesnoth.require fails it just yields nil and some logging messages, not a lua error
808  return 0;
809  }
810  // stack is now [packagename] [wesnoth] [package] [results]
811 
812  lua_pushvalue(L, 1);
813  lua_pushvalue(L, -2);
814  // stack is now [packagename] [wesnoth] [package] [results] [packagename] [results]
815  // Add the return value to the table.
816 
817  lua_settable(L, -4);
818  // stack is now [packagename] [wesnoth] [package] [results]
819  return 1;
820 }
822 {
823  lua_push(L, my_name());
824  return 1;
825 }
827 {
828  char const *m = luaL_checkstring(L, 2);
840  return 0;
841 }
843 {
844  std::string err_msg = "unknown modifiable property of game_config: ";
845  err_msg += luaL_checkstring(L, 2);
846  return luaL_argerror(L, 2, err_msg.c_str());
847 }
848 /**
849  * Loads the "package" package into the Lua environment.
850  * This action is inherently unsafe, as Lua scripts will now be able to
851  * load C libraries on their own, hence granting them the same privileges
852  * as the Wesnoth binary itself.
853  */
855 {
856  lua_State *L = mState;
858  lua_pushstring(L, "package");
859  lua_call(L, 1, 0);
860 }
861 
863 {
864  lua_State* L = mState;
865  lua_settop(L, 0);
866  cmd_log_ << "Loading core...\n";
867  luaW_getglobal(L, "wesnoth", "require");
868  lua_pushstring(L, "lua/core.lua");
869  if(!protected_call(1, 1)) {
870  cmd_log_ << "Error: Failed to load core.\n";
871  }
872  lua_settop(L, 0);
873 }
874 
875 /**
876  * Gets all the global variable names in the Lua environment. This is useful for tab completion.
877  */
878 std::vector<std::string> lua_kernel_base::get_global_var_names()
879 {
880  std::vector<std::string> ret;
881 
882  lua_State *L = mState;
883 
884  int idx = lua_gettop(L);
885  lua_getglobal(L, "_G");
886  lua_pushnil(L);
887 
888  while (lua_next(L, idx+1) != 0) {
889  if (lua_isstring(L, -2)) {
890  ret.push_back(lua_tostring(L,-2));
891  }
892  lua_pop(L,1);
893  }
894  lua_settop(L, idx);
895  return ret;
896 }
897 
898 /**
899  * Gets all attribute names of an extended variable name. This is useful for tab completion.
900  */
901 std::vector<std::string> lua_kernel_base::get_attribute_names(const std::string & input)
902 {
903  std::vector<std::string> ret;
904  std::string base_path = input;
905  std::size_t last_dot = base_path.find_last_of('.');
906  std::string partial_name = base_path.substr(last_dot + 1);
907  base_path.erase(last_dot);
908  std::string load = "return " + base_path;
909 
910  lua_State* L = mState;
911  int save_stack = lua_gettop(L);
912  int result = luaL_loadstring(L, load.c_str());
913  if(result != LUA_OK) {
914  // This isn't at error level because it's a really low priority error; it just means the user tried to tab-complete something that doesn't exist.
915  LOG_LUA << "Error when attempting tab completion:\n";
916  LOG_LUA << luaL_checkstring(L, -1) << '\n';
917  // Just return an empty list; no matches were found
918  lua_settop(L, save_stack);
919  return ret;
920  }
921 
922  luaW_pcall(L, 0, 1);
923  if(lua_istable(L, -1) || lua_isuserdata(L, -1)) {
924  int top = lua_gettop(L);
925  int obj = lua_absindex(L, -1);
926  if(luaL_getmetafield(L, obj, "__tab_enum") == LUA_TFUNCTION) {
927  lua_pushvalue(L, obj);
928  lua_pushlstring(L, partial_name.c_str(), partial_name.size());
929  luaW_pcall(L, 2, 1);
930  ret = lua_check<std::vector<std::string>>(L, -1);
931  } else if(lua_type(L, -1) != LUA_TTABLE) {
932  LOG_LUA << "Userdata missing __tab_enum meta-function for tab completion";
933  lua_settop(L, save_stack);
934  return ret;
935  } else {
936  lua_settop(L, top);
937  // Metafunction not found, so use lua_next to enumerate the table
938  for(lua_pushnil(L); lua_next(L, obj); lua_pop(L, 1)) {
939  if(lua_type(L, -2) == LUA_TSTRING) {
940  std::string attr = lua_tostring(L, -2);
941  if(attr.empty()) {
942  continue;
943  }
944  if(!isalpha(attr[0]) && attr[0] != '_') {
945  continue;
946  }
947  if(std::any_of(attr.begin(), attr.end(), [](char c){
948  return !isalpha(c) && !isdigit(c) && c != '_';
949  })) {
950  continue;
951  }
952  if(attr.substr(0, partial_name.size()) == partial_name) {
953  ret.push_back(base_path + "." + attr);
954  }
955  }
956  }
957  }
958  }
959  lua_settop(L, save_stack);
960  return ret;
961 }
962 
964 {
965  #ifdef __GNUC__
966  #pragma GCC diagnostic push
967  #pragma GCC diagnostic ignored "-Wold-style-cast"
968  #endif
969  return *reinterpret_cast<lua_kernel_base**>(lua_getextraspace(L));
970  #ifdef __GNUC__
971  #pragma GCC diagnostic pop
972  #endif
973 }
974 
976 {
977  return seed_rng::next_seed();
978 }
int dispatch(lua_State *L)
int intf_set_dialog_active(lua_State *L)
Sets a widget&#39;s state to active or inactive.
Definition: lua_gui2.cpp:868
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)
Definition: lauxlib.cpp:971
#define lua_isnoneornil(L, n)
Definition: lua.h:359
surface get_image(const image::locator &i_locator, TYPE type)
function to get the surface corresponding to an image.
Definition: image.cpp:989
static int intf_name_generator(lua_State *L)
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
Definition: lapi.cpp:683
#define lua_pushcfunction(L, f)
Definition: lua.h:350
int show_lua_console(lua_State *, lua_kernel_base *lk)
Definition: lua_gui2.cpp:933
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:423
bool matches(const config &filter) const
Definition: config.cpp:1182
std::string register_tstring_metatable(lua_State *L)
Adds the tstring metatable.
Definition: lua_common.cpp:419
std::string format_conjunct_list(const t_string &empty, const std::vector< t_string > &elems)
Format a conjunctive list.
int intf_add_dialog_tree_node(lua_State *L)
Sets a widget&#39;s state to active or inactive.
Definition: lua_gui2.cpp:952
int intf_read_file(lua_State *L)
Reads a file into a string, or a directory into a list of files therein.
std::string interpolate_variables_into_string(const std::string &str, const string_map *const symbols)
Function which will interpolate variables, starting with &#39;$&#39; in the string &#39;str&#39; with the equivalent ...
LUALIB_API lua_State * luaL_newstate(void)
Definition: lauxlib.cpp:1026
LUAMOD_API int luaopen_debug(lua_State *L)
Definition: ldblib.cpp:452
VERSION_COMP_OP parse_version_op(const std::string &op_str)
Definition: version.cpp:222
int show_story(lua_State *L)
Displays a story screen.
Definition: lua_gui2.cpp:386
void luaW_pushconfig(lua_State *L, const config &cfg)
Converts a config object to a Lua table pushed at the top of the stack.
Definition: lua_common.cpp:712
LUA_API void lua_settop(lua_State *L, int idx)
Definition: lapi.cpp:172
LUA_API int lua_type(lua_State *L, int idx)
Definition: lapi.cpp:251
int village_support
Definition: game_config.cpp:55
bool luaW_pcall(lua_State *L, int nArgs, int nRets, bool allow_wml_error)
Calls a Lua function stored below its nArgs arguments at the top of the stack.
Definition: lua_common.cpp:982
static int intf_format(lua_State *L)
int intf_have_file(lua_State *L)
Checks if a file exists (not necessarily a Lua script).
static int intf_get_image_size(lua_State *L)
Gets the dimension of an image.
LUALIB_API int luaL_loadstring(lua_State *L, const char *s)
Definition: lauxlib.cpp:764
LUA_API void lua_pushboolean(lua_State *L, int b)
Definition: lapi.cpp:556
#define LUA_MULTRET
Definition: lua.h:34
std::vector< game_tip > load(const config &cfg)
Loads the tips from a config.
Definition: tips.cpp:35
std::function< void(char const *, char const *)> error_handler
logger & info()
Definition: log.cpp:90
std::string generate(const std::map< std::string, std::string > &variables) const
static int intf_format_list(lua_State *L)
int show_dialog(lua_State *L)
Displays a window.
Definition: lua_gui2.cpp:243
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: (optio...
LUALIB_API int luaL_getmetafield(lua_State *L, int obj, const char *event)
Definition: lauxlib.cpp:772
LUA_API int lua_gettop(lua_State *L)
Definition: lapi.cpp:167
LUA_API int lua_gettable(lua_State *L, int idx)
Definition: lapi.cpp:611
LUAMOD_API int luaopen_math(lua_State *L)
Definition: lmathlib.cpp:398
LUA_API int lua_getglobal(lua_State *L, const char *name)
Definition: lapi.cpp:604
LUA_API void lua_settable(lua_State *L, int idx)
Definition: lapi.cpp:765
#define return_string_attrib(name, accessor)
Definition: lua_common.hpp:210
int intf_textdomain(lua_State *L)
Creates an interface for gettext.
Definition: lua_common.cpp:83
#define LOG_LUA
std::vector< std::tuple< std::string, std::string > > registered_widget_definitions_
LUA_API int lua_rawget(lua_State *L, int idx)
Definition: lapi.cpp:646
std::string format_disjunct_list(const t_string &empty, const std::vector< t_string > &elems)
Format a disjunctive list.
#define luaL_getmetatable(L, n)
Definition: lauxlib.h:135
int intf_kernel_type(lua_State *L)
virtual ~lua_kernel_base()
LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int arg, lua_Integer def)
Definition: lauxlib.cpp:440
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
#define LUA_TFUNCTION
Definition: lua.h:70
LUA_API int lua_isuserdata(lua_State *L, int idx)
Definition: lapi.cpp:289
int show_message_dialog(lua_State *L)
Displays a message window.
Definition: lua_gui2.cpp:274
int intf_remove_dialog_item(lua_State *L)
Removes an entry from a list.
Definition: lua_gui2.cpp:688
void lua_push(lua_State *L, const T &val)
Definition: push_check.hpp:293
int intf_set_dialog_markup(lua_State *L)
Enables/disables Pango markup on the label of a widget of the current dialog.
Definition: lua_gui2.cpp:814
#define LOG_STREAM(level, domain)
Definition: log.hpp:189
static lg::log_domain log_scripting_lua("scripting/lua")
LUA_API int lua_absindex(lua_State *L, int idx)
Definition: lapi.cpp:160
int intf_add_widget_definition(lua_State *L)
Definition: lua_gui2.cpp:977
std::vector< std::string > split(const std::string &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
int intf_vector_diff(lua_State *L)
Expose map_location::vector_difference to lua.
LUAMOD_API int luaopen_base(lua_State *L)
Definition: lbaselib.cpp:486
int rest_heal_amount
Definition: game_config.cpp:60
LUA_API void * lua_newuserdata(lua_State *L, size_t size)
Definition: lapi.cpp:1183
int intf_distance_between(lua_State *L)
Expose map_location distance_between.
void remove_single_widget_definition(const std::string &widget_type, const std::string &definition_id)
Removes a widget definition from the default GUI.
#define lua_pop(L, n)
Definition: lua.h:344
int intf_set_dialog_focus(lua_State *L)
Sets a widget to have the focus.
Definition: lua_gui2.cpp:856
int intf_rotate_right_around_center(lua_State *L)
Expose map_location::rotate_right_around_center to lua.
lua_CFunction func
Definition: lauxlib.h:33
int intf_eval_formula(lua_State *)
Evaluates a formula in the formula engine.
int intf_get_relative_dir(lua_State *L)
Expose map_location get_relative_dir.
#define LUA_TSTRING
Definition: lua.h:68
LUAMOD_API int luaopen_coroutine(lua_State *L)
Definition: lcorolib.cpp:164
LUALIB_API int luaL_argerror(lua_State *L, int arg, const char *extramsg)
Definition: lauxlib.cpp:164
LUAMOD_API int luaopen_os(lua_State *L)
Definition: loslib.cpp:403
std::string deprecated_message(const std::string &elem_name, DEP_LEVEL level, const version_info &version, const std::string &detail)
Definition: deprecation.cpp:29
#define return_int_attrib(name, accessor)
Definition: lua_common.hpp:219
void load_core()
Loads the "core" library into the Lua environment.
void throwing_run(char const *prog, int nArgs)
Runs a plain script, but reports errors by throwing lua_error.
bool luaW_getglobal(lua_State *L, const std::vector< std::string > &path)
Pushes the value found by following the variadic names (char *), if the value is not nil...
Definition: lua_common.cpp:851
LUAMOD_API int luaopen_string(lua_State *L)
Definition: lstrlib.cpp:1579
LUA_API int lua_isstring(lua_State *L, int idx)
Definition: lapi.cpp:283
int intf_show_lua_console(lua_State *L)
static lg::log_domain log_user("scripting/lua/user")
virtual ~name_generator()
LUAMOD_API int luaopen_table(lua_State *L)
Definition: ltablib.cpp:441
t_string luaW_checktstring(lua_State *L, int index)
Converts a scalar to a translatable string.
Definition: lua_common.cpp:602
static int intf_deprecated_message(lua_State *L)
Logs a deprecation message.
LUAMOD_API int luaopen_package(lua_State *L)
Definition: loadlib.cpp:767
void load_package()
Loads the package library into lua environment.
LUA_API void lua_close(lua_State *L)
Definition: lstate.cpp:341
bool protected_call(int nArgs, int nRets, error_handler)
#define lua_getextraspace(L)
Definition: lua.h:339
LUA_API const char * lua_pushlstring(lua_State *L, const char *s, size_t len)
Definition: lapi.cpp:479
int kill_experience
Definition: game_config.cpp:57
LUA_API void lua_setglobal(lua_State *L, const char *name)
Definition: lapi.cpp:758
LUA_API int lua_setmetatable(lua_State *L, int objindex)
Definition: lapi.cpp:845
int show_menu(lua_State *L)
Displays a popup menu at the current mouse position Best used from a [set_menu_item], to show a submenu.
Definition: lua_gui2.cpp:399
static int impl_name_generator_collect(lua_State *L)
const char * what() const noexcept
Definition: exceptions.hpp:37
int intf_compile_formula(lua_State *)
int(lua_kernel_base::* member_callback)(lua_State *L)
int intf_set_dialog_canvas(lua_State *L)
Sets a canvas on a widget of the current dialog.
Definition: lua_gui2.cpp:833
int luaW_pcall_internal(lua_State *L, int nArgs, int nRets)
Definition: lua_common.cpp:959
#define lua_newtable(L)
Definition: lua.h:346
LUA_API void lua_pushnil(lua_State *L)
Definition: lapi.cpp:450
int intf_tiles_adjacent(lua_State *L)
Expose map_location tiles_adjacent.
LUAMOD_API int() luaopen_utf8(lua_State *L)
Definition: lutf8lib.cpp:250
bool load_string(char const *prog, error_handler)
void run(char const *prog, int nArgs=0)
Runs a plain script.
virtual int impl_game_config_get(lua_State *L)
#define LUA_ERRGCMM
Definition: lua.h:52
int show_popup_dialog(lua_State *L)
Displays a popup message.
Definition: lua_gui2.cpp:372
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)
Definition: lauxlib.cpp:299
LUA_API void * lua_touserdata(lua_State *L, int idx)
Definition: lapi.cpp:413
#define LUA_ERRERR
Definition: lua.h:53
static int intf_random(lua_State *L)
Returns a random numer, same interface as math.random.
#define lua_isnil(L, n)
Definition: lua.h:355
int intf_print(lua_State *L)
Replacement print function – instead of printing to std::cout, print to the command log...
std::size_t i
Definition: function.cpp:933
int intf_set_dialog_value(lua_State *L)
Sets the value of a widget on the current dialog.
Definition: lua_gui2.cpp:462
VERSION_COMP_OP
Definition: version.hpp:195
logger & err()
Definition: log.cpp:78
LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int arg)
Definition: lauxlib.cpp:430
std::string register_metatables(lua_State *)
std::string register_gettext_metatable(lua_State *L)
Adds the gettext metatable.
Definition: lua_common.cpp:400
config luaW_checkconfig(lua_State *L, int index)
Converts an optional table or vconfig to a config object.
Definition: lua_common.cpp:799
int village_income
Definition: game_config.cpp:54
int intf_get_in_basis_N_NE(lua_State *L)
Expose map_location get_in_basis_N_NE.
static map_location::DIRECTION s
virtual uint32_t get_random_seed()
#define LUA_ERRSYNTAX
Definition: lua.h:50
#define lua_tostring(L, i)
Definition: lua.h:366
static int intf_get_time_stamp(lua_State *L)
Returns the time stamp, exactly as [set_variable] time=stamp does.
static int intf_debug(lua_State *L)
Dumps a wml table or userdata wml object into a pretty string.
static int intf_compare_versions(lua_State *L)
Compares 2 version strings - which is newer.
LUA_API void lua_rotate(lua_State *L, int idx, int n)
Definition: lapi.cpp:207
LUA_API void lua_pushvalue(lua_State *L, int idx)
Definition: lapi.cpp:237
const bool & debug
#define debug(x)
#define LUA_ERRMEM
Definition: lua.h:51
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.
LUA_API int lua_isnumber(lua_State *L, int idx)
Definition: lapi.cpp:276
virtual void log_error(char const *msg, char const *context="Lua error")
Error reporting mechanisms, used by virtual methods protected_call and load_string.
const version_info wesnoth_version(VERSION)
Definition: version.hpp:210
rng * generator
This generator is automatically synced during synced context.
Definition: random.cpp:60
#define lua_call(L, n, r)
Definition: lua.h:274
int intf_set_dialog_callback(lua_State *L)
Sets a callback on a widget of the current dialog.
Definition: lua_gui2.cpp:750
virtual int impl_game_config_set(lua_State *L)
#define DBG_LUA
Represents version numbers.
Definition: version.hpp:43
std::vector< std::string > get_global_var_names()
Get tab completion strings.
command_log cmd_log_
int intf_vector_sum(lua_State *L)
Expose map_location::vector_sum to lua.
#define return_bool_attrib(name, accessor)
Definition: lua_common.hpp:235
static int impl_name_generator_call(lua_State *L)
uint32_t next_seed()
Definition: seed_rng.cpp:45
LUA_API int lua_error(lua_State *L)
Definition: lapi.cpp:1113
logger & warn()
Definition: log.cpp:84
std::vector< std::string > get_attribute_names(const std::string &var_path)
Gets all attribute names of an extended variable name.
#define LUA_OK
Definition: lua.h:47
#define lua_istable(L, n)
Definition: lua.h:353
DEP_LEVEL
See https://wiki.wesnoth.org/CompatibilityStandards for more info.
Definition: deprecation.hpp:19
virtual void throw_exception(char const *msg, char const *context="Lua error")
static int intf_wml_matches_filter(lua_State *L)
#define lua_insert(L, idx)
Definition: lua.h:369
Standard logging facilities (interface).
std::string str() const
Serializes the version number into string form.
Definition: version.cpp:115
uint32_t next_random()
Provides the next random draw.
Definition: random.cpp:84
void register_metatable(lua_State *L)
#define ERR_LUA
virtual std::string my_name()
User-visible name of the lua kernel that they are talking to.
#define e
bool do_version_check(const version_info &a, VERSION_COMP_OP op, const version_info &b)
Definition: version.cpp:241
int intf_get_dialog_value(lua_State *L)
Gets the value of a widget on the current dialog.
Definition: lua_gui2.cpp:593
LUALIB_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup)
Definition: lauxlib.cpp:934
void push_error_handler(lua_State *L)
Definition: lua_common.cpp:953
int show_message_box(lua_State *L)
Displays a simple message box.
Definition: lua_gui2.cpp:425
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
int intf_dofile(lua_State *L)
Loads and executes a Lua file.
mock_char c
int intf_tovconfig(lua_State *L)
Creates a vconfig containing the WML table.
Definition: lua_common.cpp:390
lua_State * mState
t_string interpolate_variables_into_tstring(const t_string &tstr, const variable_set &variables)
Function that does the same as the above, for t_stringS.
#define LUA_TTABLE
Definition: lua.h:69
Interfaces for manipulating version numbers of engine, add-ons, etc.
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
Definition: lapi.cpp:466
bool file_exists() const
Tests whether the file the locater points at exists.
Definition: image.cpp:796
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.
Definition: lua_rng.cpp:80
void run_lua_tag(const config &cfg)
Runs a [lua] tag.
LUA_API const char * lua_pushstring(lua_State *L, const char *s)
Definition: lapi.cpp:491
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
Definition: lapi.cpp:776
#define LUA_ERRRUN
Definition: lua.h:49
int intf_set_dialog_visible(lua_State *L)
Sets the visiblity of a widget in the current dialog.
Definition: lua_gui2.cpp:886
LUA_API int lua_next(lua_State *L, int idx)
Definition: lapi.cpp:1122
std::string debug() const
Definition: config.cpp:1230
const char * what() const noexcept
static lua_kernel_base *& get_lua_kernel_base_ptr(lua_State *L)
LUA_API const char * lua_typename(lua_State *L, int t)
Definition: lapi.cpp:257
static const char * Gen
std::vector< std::string > parenthetical_split(const std::string &val, const char separator, const std::string &left, const std::string &right, const int flags)
Splits a string based either on a separator, except then the text appears within specified parenthesi...
static int intf_log(lua_State *L)
Logs a message Arg 1: (optional) Logger Arg 2: Message.
#define luaL_checkstring(L, n)
Definition: lauxlib.h:124
Error used to report an error in a lua script or in the lua interpreter.
Definition: game_errors.hpp:53