The Battle for Wesnoth  1.19.7+dev
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Static Private Member Functions | Private Attributes | List of all members
lua_kernel_base Class Reference

#include <lua_kernel_base.hpp>

Inheritance diagram for lua_kernel_base:

Classes

struct  command_log
 Log implementation. More...
 

Public Types

using external_log_type = std::function< void(const std::string &)>
 
typedef std::function< void(char const *, char const *)> error_handler
 

Public Member Functions

 lua_kernel_base ()
 
virtual ~lua_kernel_base ()
 
void run_lua_tag (const config &cfg)
 Runs a [lua] tag. More...
 
void run (char const *prog, const std::string &name, int nArgs=0)
 Runs a plain script. More...
 
void throwing_run (char const *prog, const std::string &name, int nArgs, bool in_interpreter=false)
 Runs a plain script, but reports errors by throwing lua_error. More...
 
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 normally. More...
 
void load_package ()
 Loads the package library into lua environment. More...
 
void load_core ()
 Loads the "core" library into the Lua environment. More...
 
std::vector< std::string > get_global_var_names ()
 Get tab completion strings. More...
 
std::vector< std::string > get_attribute_names (const std::string &var_path)
 Gets all attribute names of an extended variable name. More...
 
virtual std::string my_name ()
 User-visible name of the lua kernel that they are talking to. More...
 
const std::stringstream & get_log ()
 Access / manipulate logging of lua kernel activities. More...
 
void add_log (const char *str)
 
void clear_log ()
 
void set_external_log (external_log_type lg)
 
virtual void log_error (char const *msg, char const *context="Lua error")
 Error reporting mechanisms, used by virtual methods protected_call and load_string. More...
 
virtual void throw_exception (char const *msg, char const *context="Lua error")
 
virtual uint32_t get_random_seed ()
 
lua_State * get_state ()
 
void add_widget_definition (const std::string &type, const std::string &id)
 
void add_log_to_console (const std::string &msg)
 
int intf_show_lua_console (lua_State *L)
 

Static Public Member Functions

template<typename T >
static T & get_lua_kernel (lua_State *L)
 

Protected Member Functions

int intf_print (lua_State *L)
 Replacement print function – instead of printing to std::cout, print to the command log. More...
 
bool protected_call (int nArgs, int nRets, const error_handler &)
 
bool load_string (char const *prog, const std::string &name, const error_handler &)
 
virtual bool protected_call (int nArgs, int nRets)
 
virtual bool load_string (char const *prog, const std::string &name)
 
int intf_dofile (lua_State *L)
 Loads and executes a Lua file. More...
 
int intf_require (lua_State *L)
 Loads and executes a Lua file, if there is no corresponding entry in wesnoth.package. More...
 
int intf_kernel_type (lua_State *L)
 
int impl_game_config_get (lua_State *L)
 Gets some game_config data (__index metamethod). More...
 
int impl_game_config_set (lua_State *L)
 Sets some game_config data (__newindex metamethod). More...
 
int impl_game_config_dir (lua_State *L)
 Gets a list of game_config data (__dir metamethod). More...
 

Static Protected Member Functions

static bool protected_call (lua_State *L, int nArgs, int nRets, const error_handler &)
 

Protected Attributes

lua_State * mState
 
command_log cmd_log_
 

Static Private Member Functions

static lua_kernel_base *& get_lua_kernel_base_ptr (lua_State *L)
 

Private Attributes

std::vector< std::tuple< std::string, std::string > > registered_widget_definitions_
 

Detailed Description

Definition at line 27 of file lua_kernel_base.hpp.

Member Typedef Documentation

◆ error_handler

typedef std::function<void(char const*, char const*)> lua_kernel_base::error_handler

Definition at line 70 of file lua_kernel_base.hpp.

◆ external_log_type

using lua_kernel_base::external_log_type = std::function<void(const std::string &)>

Definition at line 63 of file lua_kernel_base.hpp.

Constructor & Destructor Documentation

◆ lua_kernel_base()

lua_kernel_base::lua_kernel_base ( )

Definition at line 741 of file lua_kernel_base.cpp.

References cmd_log_, Gen, randomness::generator, get_lua_kernel_base_ptr(), impl_name_generator_call(), impl_name_generator_collect(), impl_warn(), Interp, lua_formula_bridge::intf_compile_formula(), intf_current_version(), intf_deprecated_message(), lua_map_location::intf_distance_between(), lua_formula_bridge::intf_eval_formula(), lua_map_location::intf_get_adjacent_tiles(), lua_map_location::intf_get_direction(), lua_map_location::intf_get_from_cubic(), lua_map_location::intf_get_in_cubic(), intf_get_language(), lua_map_location::intf_get_relative_dir(), lua_map_location::intf_get_tile_ring(), lua_map_location::intf_get_tiles_in_radius(), intf_load(), intf_log(), intf_make_shroud_bitmap(), intf_make_version(), intf_ms_since_init(), intf_name_generator(), intf_named_tuple(), intf_object_dir(), intf_parse_shroud_bitmap(), intf_pcall(), intf_require(), lua_map_location::intf_rotate_right_around_center(), lua_common::intf_textdomain(), lua_map_location::intf_tiles_adjacent(), lua_map_location::intf_vector_diff(), lua_map_location::intf_vector_negation(), lua_map_location::intf_vector_sum(), lua_rng::load_tables(), log_error(), luaW_getglobal(), lua_fileops::luaW_open(), lua_gui2::luaW_open(), lua_mathx::luaW_open(), lua_stringx::luaW_open(), lua_wml::luaW_open(), mState, my_name(), protected_call(), push_error_handler(), lua_common::register_gettext_metatable(), lua_cpp::register_metatable(), lua_widget::register_metatable(), lua_formula_bridge::register_metatables(), lua_colors::register_metatables(), and lua_common::register_tstring_metatable().

◆ ~lua_kernel_base()

lua_kernel_base::~lua_kernel_base ( )
virtual

Member Function Documentation

◆ add_log()

void lua_kernel_base::add_log ( const char *  str)
inline

Definition at line 60 of file lua_kernel_base.hpp.

References cmd_log_.

◆ add_log_to_console()

void lua_kernel_base::add_log_to_console ( const std::string &  msg)

Definition at line 260 of file lua_kernel_base.cpp.

References cmd_log_, DBG_LUA, and wfl::msg().

◆ add_widget_definition()

void lua_kernel_base::add_widget_definition ( const std::string &  type,
const std::string &  id 
)
inline

Definition at line 80 of file lua_kernel_base.hpp.

References registered_widget_definitions_.

◆ clear_log()

void lua_kernel_base::clear_log ( )
inline

◆ get_attribute_names()

std::vector< std::string > lua_kernel_base::get_attribute_names ( const std::string &  input)

Gets all attribute names of an extended variable name.

This is useful for tab completion.

Definition at line 1422 of file lua_kernel_base.cpp.

References c, gui2::tip_of_the_day::load(), LOG_LUA, luaW_pcall(), and mState.

Referenced by gui2::dialogs::lua_interpreter::lua_model::get_attribute_names().

◆ get_global_var_names()

std::vector< std::string > lua_kernel_base::get_global_var_names ( )

Get tab completion strings.

Gets all the global variable names in the Lua environment.

This is useful for tab completion.

Definition at line 1399 of file lua_kernel_base.cpp.

References mState.

Referenced by gui2::dialogs::lua_interpreter::lua_model::get_globals().

◆ get_log()

const std::stringstream& lua_kernel_base::get_log ( )
inline

Access / manipulate logging of lua kernel activities.

Definition at line 59 of file lua_kernel_base.hpp.

References cmd_log_, flush(), and lua_kernel_base::command_log::log_.

Referenced by gui2::dialogs::lua_interpreter::lua_model::lua_model().

◆ get_lua_kernel()

template<typename T >
static T& lua_kernel_base::get_lua_kernel ( lua_State *  L)
inlinestatic

Definition at line 73 of file lua_kernel_base.hpp.

References get_lua_kernel_base_ptr().

◆ get_lua_kernel_base_ptr()

lua_kernel_base *& lua_kernel_base::get_lua_kernel_base_ptr ( lua_State *  L)
staticprivate

Definition at line 1484 of file lua_kernel_base.cpp.

Referenced by get_lua_kernel(), and lua_kernel_base().

◆ get_random_seed()

uint32_t lua_kernel_base::get_random_seed ( )
virtual

Reimplemented in mapgen_lua_kernel.

Definition at line 1496 of file lua_kernel_base.cpp.

References seed_rng::next_seed().

Referenced by mapgen_lua_kernel::get_random_seed().

◆ get_state()

lua_State* lua_kernel_base::get_state ( )
inline

Definition at line 79 of file lua_kernel_base.hpp.

References mState.

◆ impl_game_config_dir()

int lua_kernel_base::impl_game_config_dir ( lua_State *  L)
protected

Gets a list of game_config data (__dir metamethod).

Definition at line 1365 of file lua_kernel_base.cpp.

References luaW_Registry::dir(), and gameConfigReg().

◆ impl_game_config_get()

int lua_kernel_base::impl_game_config_get ( lua_State *  L)
protected

Gets some game_config data (__index metamethod).

  • Arg 1: userdata (ignored).
  • Arg 2: string containing the name of the property.
  • Ret 1: something containing the attribute.

Definition at line 1348 of file lua_kernel_base.cpp.

References gameConfigReg(), and luaW_Registry::get().

◆ impl_game_config_set()

int lua_kernel_base::impl_game_config_set ( lua_State *  L)
protected

Sets some game_config data (__newindex metamethod).

  • Arg 1: userdata (ignored).
  • Arg 2: string containing the name of the property.
  • Arg 3: something containing the attribute.

Definition at line 1358 of file lua_kernel_base.cpp.

References gameConfigReg(), and luaW_Registry::set().

◆ interactive_run()

void lua_kernel_base::interactive_run ( char const *  prog)

Tests if a program resolves to an expression, and pretty prints it if it is, otherwise it runs it normally.

Throws exceptions.

Definition at line 1132 of file lua_kernel_base.cpp.

References cmd_log_, i, Interp, load_string(), luaW_getglobal(), mState, protected_call(), throw_exception(), and throwing_run().

Referenced by gui2::dialogs::lua_interpreter::lua_model::execute().

◆ intf_dofile()

int lua_kernel_base::intf_dofile ( lua_State *  L)
protected

Loads and executes a Lua file.

  • Arg 1: string containing the file name.
  • Ret *: values returned by executing the file body.

Definition at line 1181 of file lua_kernel_base.cpp.

References lua_fileops::load_file().

◆ intf_kernel_type()

int lua_kernel_base::intf_kernel_type ( lua_State *  L)
protected

Definition at line 1244 of file lua_kernel_base.cpp.

References lua_push(), and my_name().

◆ intf_print()

int lua_kernel_base::intf_print ( lua_State *  L)
protected

Replacement print function – instead of printing to std::cout, print to the command log.

Intended to be bound to this' command_log at registration time.

Definition at line 213 of file lua_kernel_base.cpp.

References cmd_log_, DBG_LUA, i, and LOG_LUA.

◆ intf_require()

int lua_kernel_base::intf_require ( lua_State *  L)
protected

Loads and executes a Lua file, if there is no corresponding entry in wesnoth.package.

Stores the result of the script in wesnoth.package and returns it.

  • Arg 1: string containing the file name.
  • Ret 1: value returned by the script.

Definition at line 1201 of file lua_kernel_base.cpp.

References DBG_LUA, game_config::debug_lua, lua_fileops::load_file(), log_error(), and protected_call().

Referenced by lua_kernel_base().

◆ intf_show_lua_console()

int lua_kernel_base::intf_show_lua_console ( lua_State *  L)

◆ load_core()

void lua_kernel_base::load_core ( )

Loads the "core" library into the Lua environment.

Without this, most Wesnoth Lua won't work. Cannot be called from the constructor because it needs to call virtual functions.

Definition at line 1383 of file lua_kernel_base.cpp.

References cmd_log_, luaW_getglobal(), mState, and protected_call().

Referenced by lua_map_generator::lua_map_generator().

◆ load_package()

void lua_kernel_base::load_package ( )

Loads the package library into lua environment.

Loads the "package" package into the Lua environment.

Only in allow in unsafe modes.

This action is inherently unsafe, as Lua scripts will now be able to load C libraries on their own, hence granting them the same privileges as the Wesnoth binary itself.

Definition at line 1375 of file lua_kernel_base.cpp.

References mState.

Referenced by game_launcher::init_lua_script().

◆ load_string() [1/2]

bool lua_kernel_base::load_string ( char const *  prog,
const std::string &  name 
)
protectedvirtual

Definition at line 1024 of file lua_kernel_base.cpp.

References load_string(), and log_error().

◆ load_string() [2/2]

bool lua_kernel_base::load_string ( char const *  prog,
const std::string &  name,
const error_handler e_h 
)
protected

◆ log_error()

void lua_kernel_base::log_error ( char const *  msg,
char const *  context = "Lua error" 
)
virtual

Error reporting mechanisms, used by virtual methods protected_call and load_string.

Reimplemented in game_lua_kernel.

Definition at line 1008 of file lua_kernel_base.cpp.

References ERR_LUA, and wfl::msg().

Referenced by intf_require(), intf_show_lua_console(), application_lua_kernel::load_script_from_file(), application_lua_kernel::load_script_from_string(), load_string(), game_lua_kernel::log_error(), lua_kernel_base(), protected_call(), and run().

◆ my_name()

virtual std::string lua_kernel_base::my_name ( )
inlinevirtual

User-visible name of the lua kernel that they are talking to.

Reimplemented in game_lua_kernel, mapgen_lua_kernel, and application_lua_kernel.

Definition at line 56 of file lua_kernel_base.hpp.

Referenced by gui2::dialogs::lua_interpreter::lua_model::get_name(), intf_kernel_type(), and lua_kernel_base().

◆ protected_call() [1/3]

bool lua_kernel_base::protected_call ( int  nArgs,
int  nRets 
)
protectedvirtual

Definition at line 1018 of file lua_kernel_base.cpp.

References log_error(), and protected_call().

◆ protected_call() [2/3]

bool lua_kernel_base::protected_call ( int  nArgs,
int  nRets,
const error_handler e_h 
)
protected

◆ protected_call() [3/3]

bool lua_kernel_base::protected_call ( lua_State *  L,
int  nArgs,
int  nRets,
const error_handler e_h 
)
staticprotected

Definition at line 1035 of file lua_kernel_base.cpp.

References luaW_pcall_internal(), and wfl::msg().

◆ run()

void lua_kernel_base::run ( char const *  prog,
const std::string &  name,
int  nArgs = 0 
)

Runs a plain script.

Doesn't throw lua_error.

Definition at line 1121 of file lua_kernel_base.cpp.

References cmd_log_, e, log_error(), and throwing_run().

Referenced by game_launcher::init_lua_script(), run_lua_tag(), and SYNCED_COMMAND_HANDLER_FUNCTION().

◆ run_lua_tag()

void lua_kernel_base::run_lua_tag ( const config cfg)

Runs a [lua] tag.

Doesn't throw lua_error.

Definition at line 1095 of file lua_kernel_base.cpp.

References luaW_pushconfig(), mState, config::optional_child(), and run().

Referenced by game_lua_kernel::initialize().

◆ set_external_log()

void lua_kernel_base::set_external_log ( external_log_type  lg)
inline

◆ throw_exception()

void lua_kernel_base::throw_exception ( char const *  msg,
char const *  context = "Lua error" 
)
virtual

Definition at line 1013 of file lua_kernel_base.cpp.

References wfl::msg().

Referenced by interactive_run(), mapgen_lua_kernel::run_generator(), and throwing_run().

◆ throwing_run()

void lua_kernel_base::throwing_run ( char const *  prog,
const std::string &  name,
int  nArgs,
bool  in_interpreter = false 
)

Runs a plain script, but reports errors by throwing lua_error.

Definition at line 1106 of file lua_kernel_base.cpp.

References cmd_log_, Interp, load_string(), mState, protected_call(), and throw_exception().

Referenced by interactive_run(), and run().

Member Data Documentation

◆ cmd_log_

command_log lua_kernel_base::cmd_log_
protected

◆ mState

lua_State* lua_kernel_base::mState
protected

◆ registered_widget_definitions_

std::vector<std::tuple<std::string, std::string> > lua_kernel_base::registered_widget_definitions_
private

Definition at line 146 of file lua_kernel_base.hpp.

Referenced by add_widget_definition(), and ~lua_kernel_base().


The documentation for this class was generated from the following files: