Freeciv21
Develop your civilization from humble roots to a global empire
luascript.cpp File Reference
#include <cstdarg>
#include <ctime>
#include "lua.h"
#include "lualib.h"
#include "tolua.h"
#include "sol/sol.hpp"
#include "log.h"
#include "registry.h"
#include "registry_ini.h"
#include "map.h"
#include "api_common_intl.h"
#include "api_common_utilities.h"
#include "luascript_func.h"
#include "luascript_signal.h"
#include "tolua_common_a_gen.h"
#include "luascript.h"
+ Include dependency graph for luascript.cpp:

Go to the source code of this file.

Macros

#define LUASCRIPT_MAX_EXECUTION_TIME_SEC   5.0
 Configuration for script execution time limits. More...
 
#define LUASCRIPT_CHECKINTERVAL   10000
 
#define LUASCRIPT_GLOBAL_VAR_NAME   "__fcl"
 
#define LUASCRIPT_SECURE_LUA_VERSION1   503
 Unsafe Lua builtin symbols that we to remove access to. More...
 
#define LUASCRIPT_SECURE_LUA_VERSION2   504
 

Functions

static int luascript_report (struct fc_lua *fcl, int status, const char *code)
 Lua libraries to load (all default libraries, excluding operating system and library loading modules). More...
 
static void luascript_traceback_func_save (lua_State *L)
 Find the debug.traceback function and store in the registry. More...
 
static void luascript_traceback_func_push (lua_State *L)
 Push the traceback function to the stack. More...
 
static void luascript_exec_check (lua_State *L, lua_Debug *ar)
 Check currently excecuting lua function for execution time limit. More...
 
static void luascript_hook_start (lua_State *L)
 Setup function execution guard. More...
 
static void luascript_hook_end (lua_State *L)
 Clear function execution guard. More...
 
static void luascript_openlibs (lua_State *L, const luaL_Reg *llib)
 Open lua libraries in the array of library definitions in llib. More...
 
static void luascript_blacklist (lua_State *L, const char *lsymbols[])
 Remove global symbols from lua state L. More...
 
int luascript_error (lua_State *L, const char *format,...)
 Internal api error function - varg version. More...
 
int luascript_error_vargs (lua_State *L, const char *format, va_list vargs)
 Internal api error function. More...
 
int luascript_arg_error (lua_State *L, int narg, const char *msg)
 Like script_error, but using a prefix identifying the called lua function: More...
 
struct fc_lualuascript_new (luascript_log_func_t output_fct, bool secured_environment)
 Initialize the scripting state. More...
 
void luascript_init (fc_lua *fcl)
 Sets the freeciv lua struct for a lua state. More...
 
struct fc_lualuascript_get_fcl (lua_State *L)
 Get the freeciv lua struct from a lua state. More...
 
void luascript_destroy (struct fc_lua *fcl)
 Free the scripting data. More...
 
static void luascript_exec_resource (lua_State *L, const QString &filename)
 Loads a script from a Qt resource file and executes it. More...
 
static void luascript_common_a_register (sol::state_view state)
 Registers tolua_common_a functions and modules. More...
 
void luascript_common_a (lua_State *L)
 Runs tolua_common_a.lua. More...
 
void luascript_common_z (lua_State *L)
 Runs tolua_common_z.lua. More...
 
void luascript_log (struct fc_lua *fcl, QtMsgType level, const char *format,...)
 Print a message to the selected output handle. More...
 
void luascript_log_vargs (struct fc_lua *fcl, QtMsgType level, const char *format, va_list args)
 Print a message to the selected output handle. More...
 
void luascript_pop_returns (struct fc_lua *fcl, const char *func_name, int nreturns, enum api_types *preturn_types, va_list args)
 Pop return values from the Lua stack. More...
 
void luascript_push_args (struct fc_lua *fcl, int nargs, enum api_types *parg_types, va_list args)
 Push arguments into the Lua stack. More...
 
bool luascript_check_function (struct fc_lua *fcl, const char *funcname)
 Return if the function 'funcname' is define in the lua state 'fcl->state'. More...
 
int luascript_call (struct fc_lua *fcl, int narg, int nret, const char *code)
 Evaluate a Lua function call or loaded script on the stack. More...
 
int luascript_do_string (struct fc_lua *fcl, const char *str, const char *name)
 lua_dostring replacement with error message showing on errors. More...
 
int luascript_do_file (struct fc_lua *fcl, const char *filename)
 Parse and execute the script at filename. More...
 
bool luascript_callback_invoke (struct fc_lua *fcl, const char *callback_name, int nargs, enum api_types *parg_types, va_list args)
 Invoke the 'callback_name' Lua function. More...
 
void luascript_remove_exported_object (struct fc_lua *fcl, void *object)
 Mark any, if exported, full userdata representing 'object' in the current script state as 'Nonexistent'. More...
 
void luascript_vars_save (struct fc_lua *fcl, struct section_file *file, const char *section)
 Save lua variables to file. More...
 
void luascript_vars_load (struct fc_lua *fcl, struct section_file *file, const char *section)
 Load lua variables from file. More...
 
const Directionluascript_dir (enum direction8 dir)
 Returns a pointer to a given value of enum direction8 (always the same address for the same value), or nullptr if the direction is invalid on the current map. More...
 

Variables

static const char * luascript_unsafe_symbols_secure []
 
static const char * luascript_unsafe_symbols_permissive []
 

Macro Definition Documentation

◆ LUASCRIPT_CHECKINTERVAL

#define LUASCRIPT_CHECKINTERVAL   10000

Definition at line 50 of file luascript.cpp.

◆ LUASCRIPT_GLOBAL_VAR_NAME

#define LUASCRIPT_GLOBAL_VAR_NAME   "__fcl"

Definition at line 53 of file luascript.cpp.

◆ LUASCRIPT_MAX_EXECUTION_TIME_SEC

#define LUASCRIPT_MAX_EXECUTION_TIME_SEC   5.0

Configuration for script execution time limits.

Checkinterval is the number of executed lua instructions between checking. Disabled if 0.

Definition at line 49 of file luascript.cpp.

◆ LUASCRIPT_SECURE_LUA_VERSION1

#define LUASCRIPT_SECURE_LUA_VERSION1   503

Unsafe Lua builtin symbols that we to remove access to.

If Freeciv's Lua version changes, you have to check how the set of unsafe functions and modules changes in the new version. Update the list of loaded libraries in luascript_lualibs, then update the unsafe symbols blacklist in luascript_unsafe_symbols.

Once the variables are updated for the new version, update the value of LUASCRIPT_SECURE_LUA_VERSION

In general, unsafe is all functionality that gives access to: Reading files and running processes Loading lua files or libraries

Definition at line 70 of file luascript.cpp.

◆ LUASCRIPT_SECURE_LUA_VERSION2

#define LUASCRIPT_SECURE_LUA_VERSION2   504

Definition at line 71 of file luascript.cpp.

Function Documentation

◆ luascript_arg_error()

int luascript_arg_error ( lua_State *  L,
int  narg,
const char *  msg 
)

Like script_error, but using a prefix identifying the called lua function:

bad argument #narg to 'func': msg

Definition at line 264 of file luascript.cpp.

◆ luascript_blacklist()

static void luascript_blacklist ( lua_State *  L,
const char *  lsymbols[] 
)
static

Remove global symbols from lua state L.

Definition at line 218 of file luascript.cpp.

Referenced by luascript_new().

◆ luascript_call()

int luascript_call ( struct fc_lua fcl,
int  narg,
int  nret,
const char *  code 
)

Evaluate a Lua function call or loaded script on the stack.

Return nonzero if an error occurred.

If available pass the source code string as code, else nullptr.

Will pop function and arguments (1 + narg values) from the stack. Will push nret return values to the stack.

On error, print an error message with traceback. Nothing is pushed to the stack.

Definition at line 580 of file luascript.cpp.

Referenced by luascript_callback_invoke(), luascript_do_file(), luascript_do_string(), luascript_func_call_valist(), and luascript_vars_save().

◆ luascript_callback_invoke()

bool luascript_callback_invoke ( struct fc_lua fcl,
const char *  callback_name,
int  nargs,
enum api_types *  parg_types,
va_list  args 
)

Invoke the 'callback_name' Lua function.

Definition at line 657 of file luascript.cpp.

Referenced by luascript_signal_emit_valist(), and script_client_callback_invoke().

◆ luascript_check_function()

bool luascript_check_function ( struct fc_lua fcl,
const char *  funcname 
)

Return if the function 'funcname' is define in the lua state 'fcl->state'.

Definition at line 554 of file luascript.cpp.

Referenced by luascript_func_check().

◆ luascript_common_a()

void luascript_common_a ( lua_State *  L)

Runs tolua_common_a.lua.

Definition at line 410 of file luascript.cpp.

Referenced by script_client_init(), script_fcdb_init(), and script_server_init().

◆ luascript_common_a_register()

static void luascript_common_a_register ( sol::state_view  state)
static

Registers tolua_common_a functions and modules.

Definition at line 379 of file luascript.cpp.

Referenced by luascript_common_a().

◆ luascript_common_z()

void luascript_common_z ( lua_State *  L)

Runs tolua_common_z.lua.

Definition at line 420 of file luascript.cpp.

Referenced by script_client_init(), script_fcdb_init(), and script_server_init().

◆ luascript_destroy()

void luascript_destroy ( struct fc_lua fcl)

Free the scripting data.

Definition at line 335 of file luascript.cpp.

Referenced by script_client_free(), script_client_init(), script_server_free(), and script_server_init().

◆ luascript_dir()

const Direction* luascript_dir ( enum direction8  dir)

Returns a pointer to a given value of enum direction8 (always the same address for the same value), or nullptr if the direction is invalid on the current map.

Definition at line 789 of file luascript.cpp.

Referenced by api_methods_unit_orientation_get(), api_utilities_dir_ccw(), api_utilities_dir_cw(), api_utilities_opposite_dir(), and api_utilities_str2dir().

◆ luascript_do_file()

int luascript_do_file ( struct fc_lua fcl,
const char *  filename 
)

Parse and execute the script at filename.

Definition at line 638 of file luascript.cpp.

Referenced by script_client_do_file(), and script_server_do_file_shared().

◆ luascript_do_string()

int luascript_do_string ( struct fc_lua fcl,
const char *  str,
const char *  name 
)

lua_dostring replacement with error message showing on errors.

Definition at line 618 of file luascript.cpp.

Referenced by luascript_vars_load(), script_client_code_load(), script_client_do_string(), script_server_code_load(), and script_server_do_string_shared().

◆ luascript_error()

int luascript_error ( lua_State *  L,
const char *  format,
  ... 
)

Internal api error function - varg version.

Definition at line 231 of file luascript.cpp.

Referenced by api_methods_unit_type_has_flag(), api_methods_unit_type_has_role(), and luascript_signal_callback().

◆ luascript_error_vargs()

int luascript_error_vargs ( lua_State *  L,
const char *  format,
va_list  vargs 
)

Internal api error function.

Invoking this will cause Lua to stop executing the current context and throw an exception, so to speak.

Definition at line 248 of file luascript.cpp.

Referenced by luascript_error().

◆ luascript_exec_check()

static void luascript_exec_check ( lua_State *  L,
lua_Debug *  ar 
)
static

Check currently excecuting lua function for execution time limit.

Definition at line 165 of file luascript.cpp.

Referenced by luascript_hook_end(), and luascript_hook_start().

◆ luascript_exec_resource()

static void luascript_exec_resource ( lua_State *  L,
const QString &  filename 
)
static

Loads a script from a Qt resource file and executes it.

Definition at line 358 of file luascript.cpp.

Referenced by luascript_common_a(), and luascript_common_z().

◆ luascript_get_fcl()

struct fc_lua* luascript_get_fcl ( lua_State *  L)

◆ luascript_hook_end()

static void luascript_hook_end ( lua_State *  L)
static

Clear function execution guard.

Definition at line 196 of file luascript.cpp.

Referenced by luascript_call().

◆ luascript_hook_start()

static void luascript_hook_start ( lua_State *  L)
static

Setup function execution guard.

Definition at line 182 of file luascript.cpp.

Referenced by luascript_call().

◆ luascript_init()

void luascript_init ( fc_lua fcl)

Sets the freeciv lua struct for a lua state.

Definition at line 304 of file luascript.cpp.

Referenced by luascript_new(), and script_fcdb_init().

◆ luascript_log()

void luascript_log ( struct fc_lua fcl,
QtMsgType  level,
const char *  format,
  ... 
)

◆ luascript_log_vargs()

void luascript_log_vargs ( struct fc_lua fcl,
QtMsgType  level,
const char *  format,
va_list  args 
)

Print a message to the selected output handle.

Definition at line 441 of file luascript.cpp.

Referenced by luascript_log().

◆ luascript_new()

struct fc_lua* luascript_new ( luascript_log_func_t  output_fct,
bool  secured_environment 
)

Initialize the scripting state.

Definition at line 272 of file luascript.cpp.

Referenced by script_client_init(), and script_server_init().

◆ luascript_openlibs()

static void luascript_openlibs ( lua_State *  L,
const luaL_Reg *  llib 
)
static

Open lua libraries in the array of library definitions in llib.

Definition at line 206 of file luascript.cpp.

Referenced by luascript_new().

◆ luascript_pop_returns()

void luascript_pop_returns ( struct fc_lua fcl,
const char *  func_name,
int  nreturns,
enum api_types *  preturn_types,
va_list  args 
)

Pop return values from the Lua stack.

Definition at line 461 of file luascript.cpp.

Referenced by luascript_func_call_valist().

◆ luascript_push_args()

void luascript_push_args ( struct fc_lua fcl,
int  nargs,
enum api_types *  parg_types,
va_list  args 
)

Push arguments into the Lua stack.

Definition at line 512 of file luascript.cpp.

Referenced by luascript_callback_invoke(), and luascript_func_call_valist().

◆ luascript_remove_exported_object()

void luascript_remove_exported_object ( struct fc_lua fcl,
void *  object 
)

Mark any, if exported, full userdata representing 'object' in the current script state as 'Nonexistent'.

This changes the type of the lua variable.

Definition at line 699 of file luascript.cpp.

Referenced by script_server_remove_exported_object().

◆ luascript_report()

static int luascript_report ( struct fc_lua fcl,
int  status,
const char *  code 
)
static

Lua libraries to load (all default libraries, excluding operating system and library loading modules).

Report a lua error.

See linit.c in Lua 5.1 for the default list.

Definition at line 117 of file luascript.cpp.

Referenced by luascript_call(), luascript_do_file(), and luascript_do_string().

◆ luascript_traceback_func_push()

static void luascript_traceback_func_push ( lua_State *  L)
static

Push the traceback function to the stack.

Definition at line 157 of file luascript.cpp.

Referenced by luascript_call().

◆ luascript_traceback_func_save()

static void luascript_traceback_func_save ( lua_State *  L)
static

Find the debug.traceback function and store in the registry.

Definition at line 143 of file luascript.cpp.

Referenced by luascript_new().

◆ luascript_vars_load()

void luascript_vars_load ( struct fc_lua fcl,
struct section_file file,
const char *  section 
)

Load lua variables from file.

Definition at line 763 of file luascript.cpp.

Referenced by script_client_vars_load(), and script_server_vars_load().

◆ luascript_vars_save()

void luascript_vars_save ( struct fc_lua fcl,
struct section_file file,
const char *  section 
)

Save lua variables to file.

Definition at line 737 of file luascript.cpp.

Referenced by script_client_vars_save(), and script_server_vars_save().

Variable Documentation

◆ luascript_unsafe_symbols_permissive

const char* luascript_unsafe_symbols_permissive[]
static
Initial value:
= {
"debug", "dofile", "loadfile", nullptr}

Definition at line 76 of file luascript.cpp.

Referenced by luascript_new().

◆ luascript_unsafe_symbols_secure

const char* luascript_unsafe_symbols_secure[]
static
Initial value:
= {"debug", "dofile",
"loadfile", nullptr}

Definition at line 73 of file luascript.cpp.

Referenced by luascript_new().