The Battle for Wesnoth  1.15.2+dev
Macros | Typedefs | Functions | Variables
lua_kernel_base.cpp File Reference
#include "scripting/lua_kernel_base.hpp"
#include "game_config.hpp"
#include "game_errors.hpp"
#include "gui/core/gui_definition.hpp"
#include "log.hpp"
#include "lua_jailbreak_exception.hpp"
#include "random.hpp"
#include "seed_rng.hpp"
#include "deprecation.hpp"
#include "language.hpp"
#include "scripting/lua_common.hpp"
#include "scripting/lua_cpp_function.hpp"
#include "scripting/lua_fileops.hpp"
#include "scripting/lua_formula_bridge.hpp"
#include "scripting/lua_gui2.hpp"
#include "scripting/lua_map_location_ops.hpp"
#include "scripting/lua_rng.hpp"
#include "scripting/push_check.hpp"
#include "game_version.hpp"
#include "picture.hpp"
#include "formula/string_utils.hpp"
#include "serialization/string_utils.hpp"
#include "serialization/schema_validator.hpp"
#include "serialization/parser.hpp"
#include "serialization/preprocessor.hpp"
#include "utils/functional.hpp"
#include "utils/name_generator.hpp"
#include "utils/markov_generator.hpp"
#include "utils/context_free_grammar_generator.hpp"
#include "variable.hpp"
#include <cstring>
#include <exception>
#include <new>
#include <string>
#include <sstream>
#include <vector>
#include <fstream>
#include <boost/algorithm/string/trim.hpp>
#include "lua/lauxlib.h"
#include "lua/lua.h"
#include "lua/lualib.h"
Include dependency graph for lua_kernel_base.cpp:

Go to the source code of this file.

Macros

#define DBG_LUA   LOG_STREAM(debug, log_scripting_lua)
 
#define LOG_LUA   LOG_STREAM(info, log_scripting_lua)
 
#define WRN_LUA   LOG_STREAM(warn, log_scripting_lua)
 
#define ERR_LUA   LOG_STREAM(err, log_scripting_lua)
 

Typedefs

typedef int(lua_kernel_base::* member_callback) (lua_State *L)
 

Functions

static int intf_compare_versions (lua_State *L)
 Compares 2 version strings - which is newer. More...
 
static int intf_load (lua_State *L)
 Replacement load function. More...
 
static int impl_name_generator_call (lua_State *L)
 
static int impl_name_generator_collect (lua_State *L)
 
static int intf_name_generator (lua_State *L)
 
static int intf_random (lua_State *L)
 Returns a random numer, same interface as math.random. More...
 
static int intf_wml_matches_filter (lua_State *L)
 Tests if a WML table matches a filter Arg 1: table to test Arg 2: filter. More...
 
static int intf_wml_merge (lua_State *L)
 Merges two WML tables Arg 1: base table Arg 2: table to merge in. More...
 
static int intf_wml_diff (lua_State *L)
 Computes a diff of two WML tables Arg 1: left table Arg 2: right table. More...
 
static int intf_wml_patch (lua_State *L)
 Applies a diff to a WML table Arg 1: base table Arg 2: WML diff. More...
 
static int intf_wml_equal (lua_State *L)
 Tests if two WML tables are equal (have the same keys and values, same tags, recursively) Arg 1: left table Arg 2: right table. More...
 
static int intf_wml_valid (lua_State *L)
 Tests if a table represents a valid WML table Arg 1: table. More...
 
static int intf_log (lua_State *L)
 Logs a message Arg 1: (optional) Logger Arg 2: Message. More...
 
static int intf_deprecated_message (lua_State *L)
 Logs a deprecation message. More...
 
static int intf_get_image_size (lua_State *L)
 Gets the dimension of an image. More...
 
static int intf_get_time_stamp (lua_State *L)
 Returns the time stamp, exactly as [set_variable] time=stamp does. More...
 
static int intf_format (lua_State *L)
 Formats a message by interpolating WML variable syntax Arg 1: (optional) Logger Arg 2: Message. More...
 
template<bool conjunct>
static int intf_format_list (lua_State *L)
 Formats a list into human-readable format Arg 1: default value, used if the list is empty Arg 2: list of strings. More...
 
static int impl_str_index (lua_State *L)
 Enables indexing a string by an integer, while also treating the stringx module as its metatable.__index. More...
 
static int intf_str_split (lua_State *L)
 Splits a string into parts according to options Arg 1: String to split Arg 2: Separator Arg 3: Options table. More...
 
static int intf_str_paren_split (lua_State *L)
 Splits a string into parenthesized portions and portions between parenthesized portions Arg 1: String to split Arg 2: Possible left parentheses Arg 3: Matching right parentheses. More...
 
static int intf_str_map_split (lua_State *L)
 Splits a string into a map Arg 1: string to split Arg 2: Separator for items Arg 3: Separator for key and value. More...
 
static int intf_str_join (lua_State *L)
 Joins a list into a string; calls __tostring and __index metamethods Arg 1: list to join Arg 2: separator (arguments can be swapped) More...
 
static int intf_str_join_map (lua_State *L)
 Joins a map into a string; calls __tostring metamethods (on both key and value) but not __index Arg 1: list to join Arg 2: separator for items Arg 3: separator for key and value (list argument can be swapped to any position) More...
 
static int intf_str_trim (lua_State *L)
 Trims whitespace from the beginning and end of a string. More...
 
static int intf_str_format (lua_State *L)
 
static int intf_parse_range (lua_State *L)
 Parses a range string of the form a-b into an interval pair Accepts the string "infinity" as representing a Very Large Number. More...
 
static int intf_get_language (lua_State *L)
 
static int intf_wml_tostring (lua_State *L)
 Dumps a wml table or userdata wml object into a pretty string. More...
 
static int intf_load_wml (lua_State *L)
 Loads a WML file into a config. More...
 
static int intf_parse_wml (lua_State *L)
 Parses a WML string into a config; does not preprocess or validate. More...
 
static int intf_clone_wml (lua_State *L)
 Returns a clone (deep copy) of the passed config, which can be either a normal config or a vconfig If it is a vconfig, the underlying config is also cloned. More...
 
static int intf_wml_interpolate (lua_State *L)
 Interpolates variables into a WML table, including [insert_tag] Arg 1: WML table to interpolate into Arg 2: WML table of variables. More...
 
template<member_callback method>
int dispatch (lua_State *L)
 

Variables

static lg::log_domain log_scripting_lua ("scripting/lua")
 
static lg::log_domain log_user ("scripting/lua/user")
 
static const char * Gen = "name generator"
 
static const char * Interp = "lua interpreter"
 

Macro Definition Documentation

◆ DBG_LUA

#define DBG_LUA   LOG_STREAM(debug, log_scripting_lua)

◆ ERR_LUA

#define ERR_LUA   LOG_STREAM(err, log_scripting_lua)

Definition at line 72 of file lua_kernel_base.cpp.

Referenced by lua_kernel_base::log_error().

◆ LOG_LUA

#define LOG_LUA   LOG_STREAM(info, log_scripting_lua)

◆ WRN_LUA

#define WRN_LUA   LOG_STREAM(warn, log_scripting_lua)

Definition at line 71 of file lua_kernel_base.cpp.

Typedef Documentation

◆ member_callback

typedef int(lua_kernel_base::* member_callback) (lua_State *L)

Definition at line 886 of file lua_kernel_base.cpp.

Function Documentation

◆ dispatch()

template<member_callback method>
int dispatch ( lua_State L)

Definition at line 889 of file lua_kernel_base.cpp.

◆ impl_name_generator_call()

static int impl_name_generator_call ( lua_State L)
static

◆ impl_name_generator_collect()

static int impl_name_generator_collect ( lua_State L)
static

◆ impl_str_index()

static int impl_str_index ( lua_State L)
static

Enables indexing a string by an integer, while also treating the stringx module as its metatable.__index.

Definition at line 493 of file lua_kernel_base.cpp.

References lua_call, lua_getglobal(), lua_gettable(), lua_pushvalue(), LUA_TNUMBER, LUA_TSTRING, lua_type(), and luaW_getglobal().

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_clone_wml()

static int intf_clone_wml ( lua_State L)
static

Returns a clone (deep copy) of the passed config, which can be either a normal config or a vconfig If it is a vconfig, the underlying config is also cloned.

  • Arg 1: a config
  • Ret: the cloned config

Definition at line 855 of file lua_kernel_base.cpp.

References vconfig::get_config(), luaW_checkconfig(), luaW_pushconfig(), and luaW_pushvconfig().

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_compare_versions()

static int intf_compare_versions ( lua_State L)
static

Compares 2 version strings - which is newer.

  • Args 1,3: version strings
  • Arg 2: comparison operator (string)
  • Ret 1: comparison result

Definition at line 87 of file lua_kernel_base.cpp.

References do_version_check(), lua_pushboolean(), luaL_argerror(), luaL_checkstring, OP_INVALID, and parse_version_op().

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_deprecated_message()

static int intf_deprecated_message ( lua_State L)
static

Logs a deprecation message.

See deprecation.cpp for details Arg 1: Element to be deprecated. Arg 2: Deprecation level. Arg 3: Version when element may be removed. Arg 4: Additional detail message.

Definition at line 416 of file lua_kernel_base.cpp.

References deprecated_message(), game_config::images::level, lua_error(), lua_isnoneornil, lua_push(), luaL_checkinteger(), luaL_checkstring, luaW_checktstring(), wfl::msg(), REMOVED, version_info::str(), and game_config::wesnoth_version.

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_format()

static int intf_format ( lua_State L)
static

Formats a message by interpolating WML variable syntax Arg 1: (optional) Logger Arg 2: Message.

Definition at line 462 of file lua_kernel_base.cpp.

References utils::interpolate_variables_into_string(), utils::interpolate_variables_into_tstring(), lua_isstring(), lua_push(), lua_tostring, luaW_checkconfig(), and luaW_checktstring().

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_format_list()

template<bool conjunct>
static int intf_format_list ( lua_State L)
static

Formats a list into human-readable format Arg 1: default value, used if the list is empty Arg 2: list of strings.

Definition at line 482 of file lua_kernel_base.cpp.

References utils::format_conjunct_list(), utils::format_disjunct_list(), lua_push(), and luaW_checktstring().

◆ intf_get_image_size()

static int intf_get_image_size ( lua_State L)
static

Gets the dimension of an image.

  • Arg 1: string.
  • Ret 1: width.
  • Ret 2: height.

Definition at line 438 of file lua_kernel_base.cpp.

References image::locator::file_exists(), image::get_image(), lua_pushinteger(), luaL_checkstring, and s.

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_get_language()

static int intf_get_language ( lua_State L)
static

Definition at line 759 of file lua_kernel_base.cpp.

References get_language(), and lua_push().

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_get_time_stamp()

static int intf_get_time_stamp ( lua_State L)
static

Returns the time stamp, exactly as [set_variable] time=stamp does.

  • Ret 1: integer

Definition at line 452 of file lua_kernel_base.cpp.

References lua_pushinteger().

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_load()

static int intf_load ( lua_State L)
static

Replacement load function.

Mostly the same as regular load, but disallows loading binary chunks due to CVE-2018-1999023.

Definition at line 140 of file lua_kernel_base.cpp.

References lua_insert, lua_isnone, LUA_OK, lua_pop, lua_pushnil(), lua_pushvalue(), lua_setupvalue(), luaL_argerror(), luaL_checkstring, luaL_loadbufferx(), and luaL_optstring.

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_load_wml()

static int intf_load_wml ( lua_State L)
static

Loads a WML file into a config.

  • Arg 1: WML file path
  • Arg 2: (optional) Array of preprocessor defines, or false to skip preprocessing (true is also valid)
  • Arg 3: (optional) Path to a schema file for validation (omit for no validation)
  • Ret: config

Definition at line 785 of file lua_kernel_base.cpp.

References config::get(), filesystem::get_wml_location(), i, lua_geti(), lua_isnoneornil, lua_isstring(), lua_len(), lua_pop, LUA_TBOOLEAN, lua_tonumber, lua_tostring, LUA_TTABLE, LUA_TUSERDATA, lua_type(), luaL_argerror(), luaL_checkstring, luaL_optstring, luaW_pushconfig(), luaW_toboolean(), n, preprocess_file(), and read().

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_log()

static int intf_log ( lua_State L)
static

Logs a message Arg 1: (optional) Logger Arg 2: Message.

Definition at line 390 of file lua_kernel_base.cpp.

References debug, lg::err(), lg::info(), LOG_STREAM, log_user, lua_isstring(), luaL_checkstring, wfl::msg(), and lg::warn().

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_name_generator()

static int intf_name_generator ( lua_State L)
static

◆ intf_parse_range()

static int intf_parse_range ( lua_State L)
static

Parses a range string of the form a-b into an interval pair Accepts the string "infinity" as representing a Very Large Number.

Definition at line 750 of file lua_kernel_base.cpp.

References lua_pushnumber(), luaL_checkstring, and utils::parse_range().

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_parse_wml()

static int intf_parse_wml ( lua_State L)
static

Parses a WML string into a config; does not preprocess or validate.

  • Arg 1: WML string
  • Ret: config

Definition at line 834 of file lua_kernel_base.cpp.

References config::get(), filesystem::get_wml_location(), luaL_checkstring, luaL_optstring, luaW_pushconfig(), and read().

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_random()

static int intf_random ( lua_State L)
static

Returns a random numer, same interface as math.random.

Definition at line 264 of file lua_kernel_base.cpp.

References randomness::generator, lua_isnoneornil, lua_isnumber(), lua_push(), luaL_argerror(), and randomness::rng::next_random().

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_str_format()

static int intf_str_format ( lua_State L)
static

◆ intf_str_join()

static int intf_str_join ( lua_State L)
static

Joins a list into a string; calls __tostring and __index metamethods Arg 1: list to join Arg 2: separator (arguments can be swapped)

Definition at line 648 of file lua_kernel_base.cpp.

References i, utils::join(), lua_call, lua_getglobal(), lua_geti(), lua_istable, lua_push(), luaL_checkstring, luaL_error(), luaL_len(), and luaL_optstring.

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_str_join_map()

static int intf_str_join_map ( lua_State L)
static

Joins a map into a string; calls __tostring metamethods (on both key and value) but not __index Arg 1: list to join Arg 2: separator for items Arg 3: separator for key and value (list argument can be swapped to any position)

Definition at line 678 of file lua_kernel_base.cpp.

References utils::join_map(), lua_absindex(), lua_call, lua_getglobal(), lua_istable, lua_next(), lua_push(), lua_pushnil(), lua_pushvalue(), lua_settop(), luaL_checkstring, luaL_error(), and luaL_optstring.

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_str_map_split()

static int intf_str_map_split ( lua_State L)
static

Splits a string into a map Arg 1: string to split Arg 2: Separator for items Arg 3: Separator for key and value.

Definition at line 613 of file lua_kernel_base.cpp.

References lua_istable, lua_push(), luaL_checkstring, luaL_error(), luaL_optstring, luaW_table_get_def(), luaW_tableget(), utils::map_split(), utils::REMOVE_EMPTY, and utils::STRIP_SPACES.

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_str_paren_split()

static int intf_str_paren_split ( lua_State L)
static

Splits a string into parenthesized portions and portions between parenthesized portions Arg 1: String to split Arg 2: Possible left parentheses Arg 3: Matching right parentheses.

Definition at line 594 of file lua_kernel_base.cpp.

References left, lua_push(), luaL_checkstring, luaL_error(), luaL_opt, luaL_optstring, luaW_toboolean(), utils::parenthetical_split(), right, and utils::STRIP_SPACES.

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_str_split()

static int intf_str_split ( lua_State L)
static

Splits a string into parts according to options Arg 1: String to split Arg 2: Separator Arg 3: Options table.

Definition at line 519 of file lua_kernel_base.cpp.

References left, lua_istable, lua_push(), luaL_checkstring, luaL_error(), luaL_optstring, luaW_table_get_def(), luaW_tableget(), utils::parenthetical_split(), utils::quoted_split(), utils::REMOVE_EMPTY, right, utils::split(), utils::square_parenthetical_split(), and utils::STRIP_SPACES.

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_str_trim()

static int intf_str_trim ( lua_State L)
static

Trims whitespace from the beginning and end of a string.

Definition at line 717 of file lua_kernel_base.cpp.

References lua_pushlstring(), and luaL_checkstring.

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_wml_diff()

static int intf_wml_diff ( lua_State L)
static

Computes a diff of two WML tables Arg 1: left table Arg 2: right table.

Definition at line 336 of file lua_kernel_base.cpp.

References config::get_diff(), luaW_checkconfig(), and luaW_pushconfig().

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_wml_equal()

static int intf_wml_equal ( lua_State L)
static

Tests if two WML tables are equal (have the same keys and values, same tags, recursively) Arg 1: left table Arg 2: right table.

Definition at line 363 of file lua_kernel_base.cpp.

References left, lua_pushboolean(), luaW_checkconfig(), and right.

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_wml_interpolate()

static int intf_wml_interpolate ( lua_State L)
static

Interpolates variables into a WML table, including [insert_tag] Arg 1: WML table to interpolate into Arg 2: WML table of variables.

Definition at line 874 of file lua_kernel_base.cpp.

References vconfig::get_parsed_config(), luaW_checkconfig(), and luaW_pushconfig().

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_wml_matches_filter()

static int intf_wml_matches_filter ( lua_State L)
static

Tests if a WML table matches a filter Arg 1: table to test Arg 2: filter.

Definition at line 296 of file lua_kernel_base.cpp.

References lua_pushboolean(), luaW_checkconfig(), and config::matches().

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_wml_merge()

static int intf_wml_merge ( lua_State L)
static

◆ intf_wml_patch()

static int intf_wml_patch ( lua_State L)
static

Applies a diff to a WML table Arg 1: base table Arg 2: WML diff.

Definition at line 349 of file lua_kernel_base.cpp.

References config::apply_diff(), luaW_checkconfig(), and luaW_pushconfig().

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_wml_tostring()

static int intf_wml_tostring ( lua_State L)
static

Dumps a wml table or userdata wml object into a pretty string.

  • Arg 1: wml table or vconfig userdata
  • Ret 1: string

Definition at line 770 of file lua_kernel_base.cpp.

References lua_pushstring(), luaW_checkconfig(), and write().

Referenced by lua_kernel_base::lua_kernel_base().

◆ intf_wml_valid()

static int intf_wml_valid ( lua_State L)
static

Tests if a table represents a valid WML table Arg 1: table.

Definition at line 375 of file lua_kernel_base.cpp.

References lua_pushboolean(), luaW_toconfig(), and config::validate_wml().

Referenced by lua_kernel_base::lua_kernel_base().

Variable Documentation

◆ Gen

const char* Gen = "name generator"
static

Definition at line 75 of file lua_kernel_base.cpp.

Referenced by intf_name_generator(), and lua_kernel_base::lua_kernel_base().

◆ Interp

const char* Interp = "lua interpreter"
static

◆ log_scripting_lua

lg::log_domain log_scripting_lua("scripting/lua")
static

◆ log_user

lg::log_domain log_user("scripting/lua/user")
static

Referenced by intf_log().