Freeciv21
Develop your civilization from humble roots to a global empire
luascript.h
Go to the documentation of this file.
1 /**************************************************************************
2  Copyright (c) 1996-2020 Freeciv21 and Freeciv contributors. This file is
3  part of Freeciv21. Freeciv21 is free software: you can redistribute it
4  and/or modify it under the terms of the GNU General Public License as
5  published by the Free Software Foundation, either version 3 of the
6  License, or (at your option) any later version. You should have received
7  a copy of the GNU General Public License along with Freeciv21. If not,
8  see https://www.gnu.org/licenses/.
9 **************************************************************************/
10 #pragma once
11 
12 #include <QHash>
13 #include <QVector>
14 /* dependencies/tolua */
15 #include "tolua.h"
16 
17 // utility
18 #include "log.h"
19 #include "support.h" // fc__attribute()
20 
21 /* common/scriptcore */
22 #include "luascript_func.h"
23 #include "luascript_signal.h"
24 #include "luascript_types.h"
25 
26 struct section_file;
27 struct luascript_func_hash;
28 struct luascript_signal_hash;
29 struct luascript_signal_name_list;
30 struct connection;
31 struct fc_lua;
32 
33 typedef void (*luascript_log_func_t)(struct fc_lua *fcl, QtMsgType level,
34  const char *format, ...)
35  fc__attribute((__format__(__printf__, 3, 4)));
36 
37 struct fc_lua {
38  lua_State *state;
39 
41  // This is needed for server 'lua' and 'luafile' commands.
42  struct connection *caller;
43 
44  QHash<QString, struct luascript_func *> *funcs;
45 
46  QHash<QString, struct signal *> *signals_hash;
48 };
49 
50 // Error functions for lua scripts.
51 int luascript_error(lua_State *L, const char *format, ...)
52  fc__attribute((__format__(__printf__, 2, 3)));
53 int luascript_error_vargs(lua_State *L, const char *format, va_list vargs);
54 int luascript_arg_error(lua_State *L, int narg, const char *msg);
55 
56 /* Create / destroy a freeciv lua instance. */
57 struct fc_lua *luascript_new(luascript_log_func_t outputfct,
58  bool secured_environment);
60 struct fc_lua *luascript_get_fcl(lua_State *L);
61 void luascript_destroy(struct fc_lua *fcl);
62 
63 void luascript_common_a(lua_State *L);
64 void luascript_common_z(lua_State *L);
65 
66 void luascript_log(struct fc_lua *fcl, QtMsgType level, const char *format,
67  ...) fc__attribute((__format__(__printf__, 3, 4)));
68 void luascript_log_vargs(struct fc_lua *fcl, QtMsgType level,
69  const char *format, va_list args);
70 
71 void luascript_push_args(struct fc_lua *fcl, int nargs,
72  enum api_types *parg_types, va_list args);
73 void luascript_pop_returns(struct fc_lua *fcl, const char *func_name,
74  int nreturns, enum api_types *preturn_types,
75  va_list args);
76 bool luascript_check_function(struct fc_lua *fcl, const char *funcname);
77 
78 int luascript_call(struct fc_lua *fcl, int narg, int nret, const char *code);
79 int luascript_do_string(struct fc_lua *fcl, const char *str,
80  const char *name);
81 int luascript_do_file(struct fc_lua *fcl, const char *filename);
82 
83 // Callback invocation function.
84 bool luascript_callback_invoke(struct fc_lua *fcl, const char *callback_name,
85  int nargs, enum api_types *parg_types,
86  va_list args);
87 
88 void luascript_remove_exported_object(struct fc_lua *fcl, void *object);
89 
90 /* Load / save variables. */
91 void luascript_vars_save(struct fc_lua *fcl, struct section_file *file,
92  const char *section);
93 void luascript_vars_load(struct fc_lua *fcl, struct section_file *file,
94  const char *section);
95 
96 const Direction *luascript_dir(enum direction8);
97 
98 // Script assertion (for debugging only)
99 #ifdef FREECIV_DEBUG
100 #define LUASCRIPT_ASSERT(L, check, ...) \
101  if (!(check)) { \
102  luascript_error(L, "in %s() [%s::%d]: the assertion '%s' failed.", \
103  __FUNCTION__, __FILE__, __FC_LINE__, #check); \
104  return __VA_ARGS__; \
105  }
106 #else
107 #define LUASCRIPT_ASSERT(check, ...)
108 #endif
109 
110 #define LUASCRIPT_CHECK_STATE(L, ...) \
111  if (!L) { \
112  qCritical("No lua state available"); \
113  return __VA_ARGS__; \
114  }
115 
116 // script_error on failed check
117 #define LUASCRIPT_CHECK(L, check, msg, ...) \
118  if (!(check)) { \
119  luascript_error(L, msg); \
120  return __VA_ARGS__; \
121  }
122 
123 // script_arg_error on failed check
124 #define LUASCRIPT_CHECK_ARG(L, check, narg, msg, ...) \
125  if (!(check)) { \
126  luascript_arg_error(L, narg, msg); \
127  return __VA_ARGS__; \
128  }
129 
130 // script_arg_error on nil value
131 #define LUASCRIPT_CHECK_ARG_NIL(L, value, narg, type, ...) \
132  if ((value) == nullptr) { \
133  luascript_arg_error(L, narg, "got 'nil', '" #type "' expected"); \
134  return __VA_ARGS__; \
135  }
136 
137 /* script_arg_error on nil value. The first argument is the lua state and the
138  * second is the pointer to self. */
139 #define LUASCRIPT_CHECK_SELF(L, value, ...) \
140  if ((value) == nullptr) { \
141  luascript_arg_error(L, 2, "got 'nil' for self"); \
142  return __VA_ARGS__; \
143  }
const char * name
Definition: inputfile.cpp:118
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: luascript.cpp:461
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: luascript.cpp:512
void luascript_log(struct fc_lua *fcl, QtMsgType level, const char *format,...) fc__attribute((__format__(__printf__
void luascript_init(fc_lua *fcl)
Sets the freeciv lua struct for a lua state.
Definition: luascript.cpp:304
struct fc_lua * luascript_new(luascript_log_func_t outputfct, bool secured_environment)
Initialize the scripting state.
Definition: luascript.cpp:272
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: luascript.cpp:554
int luascript_error(lua_State *L, const char *format,...) fc__attribute((__format__(__printf__
int int luascript_error_vargs(lua_State *L, const char *format, va_list vargs)
Internal api error function.
Definition: luascript.cpp:248
int luascript_arg_error(lua_State *L, int narg, const char *msg)
Like script_error, but using a prefix identifying the called lua function:
Definition: luascript.cpp:264
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: luascript.cpp:657
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 'Nonexisten...
Definition: luascript.cpp:699
int luascript_do_string(struct fc_lua *fcl, const char *str, const char *name)
lua_dostring replacement with error message showing on errors.
Definition: luascript.cpp:618
int luascript_do_file(struct fc_lua *fcl, const char *filename)
Parse and execute the script at filename.
Definition: luascript.cpp:638
void luascript_vars_load(struct fc_lua *fcl, struct section_file *file, const char *section)
Load lua variables from file.
Definition: luascript.cpp:763
const Direction * luascript_dir(enum direction8)
Returns a pointer to a given value of enum direction8 (always the same address for the same value),...
Definition: luascript.cpp:789
void luascript_common_z(lua_State *L)
Runs tolua_common_z.lua.
Definition: luascript.cpp:420
void 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: luascript.cpp:441
void luascript_vars_save(struct fc_lua *fcl, struct section_file *file, const char *section)
Save lua variables to file.
Definition: luascript.cpp:737
void luascript_common_a(lua_State *L)
Runs tolua_common_a.lua.
Definition: luascript.cpp:410
void(* luascript_log_func_t)(struct fc_lua *fcl, QtMsgType level, const char *format,...) fc__attribute((__format__(__printf__
Definition: luascript.h:33
struct fc_lua * luascript_get_fcl(lua_State *L)
Get the freeciv lua struct from a lua state.
Definition: luascript.cpp:315
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.
Definition: luascript.cpp:580
void luascript_destroy(struct fc_lua *fcl)
Free the scripting data.
Definition: luascript.cpp:335
enum direction8 Direction
static void static sol::state * fcl
Lua virtual machine state.
Definition: script_fcdb.cpp:48
struct setting_list * level[OLEVELS_NUM]
Definition: settings.cpp:167
struct connection * caller
Definition: luascript.h:42
QVector< QString > * signal_names
Definition: luascript.h:47
luascript_log_func_t output_fct
Definition: luascript.h:40
QHash< QString, struct signal * > * signals_hash
Definition: luascript.h:46
QHash< QString, struct luascript_func * > * funcs
Definition: luascript.h:44
lua_State * state
Definition: luascript.h:38
int fc__attribute((nonnull(1, 3)))