Freeciv-3.2
Loading...
Searching...
No Matches
script_client.c
Go to the documentation of this file.
1/*****************************************************************************
2 Freeciv - Copyright (C) 2005 - The Freeciv Project
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12*****************************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include <stdarg.h>
19#include <stdlib.h>
20#include <time.h>
21
22/* dependencies/lua */
23#include "lua.h"
24#include "lualib.h"
25
26/* dependencies/tolua */
27#include "tolua.h"
28
29/* utility */
30#include "log.h"
31
32/* common */
33#include "featured_text.h"
34
35/* common/scriptcore */
36#include "api_game_specenum.h"
37#include "luascript.h"
38#include "tolua_common_a_gen.h"
39#include "tolua_common_z_gen.h"
40#include "tolua_game_gen.h"
41#include "tolua_signal_gen.h"
42
43/* client */
44#include "luaconsole_common.h"
45
46/* client/luascript */
47#include <tolua_client_gen.h> /* <> so looked from the build directory first. */
48
49#include "script_client.h"
50
51/*****************************************************************************
52 Lua virtual machine state.
53*****************************************************************************/
54static struct fc_lua *main_fcl = NULL;
55
56/*****************************************************************************
57 Optional game script code (useful for scenarios).
58*****************************************************************************/
59static char *script_client_code = NULL;
60
61static void script_client_vars_init(void);
62static void script_client_vars_free(void);
63static void script_client_vars_load(struct section_file *file);
64static void script_client_vars_save(struct section_file *file);
65static void script_client_code_init(void);
66static void script_client_code_free(void);
67static void script_client_code_load(struct section_file *file);
68static void script_client_code_save(struct section_file *file);
69
70static void script_client_output(struct fc_lua *fcl, enum log_level level,
71 const char *format, ...)
73
75
76/*************************************************************************/
80{
81 int status = luascript_do_string(main_fcl, str, "cmd");
82
83 return (status == 0);
84}
85
86/*************************************************************************/
89bool script_client_do_file(const char *filename)
90{
91 int status = luascript_do_file(main_fcl, filename);
92
93 return (status == 0);
94}
95
96/*************************************************************************/
99bool script_client_callback_invoke(const char *callback_name, int nargs,
100 enum api_types *parg_types, va_list args)
101{
103 args);
104}
105
106/*************************************************************************/
115
116/*************************************************************************/
119static void script_client_vars_init(void)
120{
121 /* nothing */
122}
123
124/*************************************************************************/
127static void script_client_vars_free(void)
128{
129 /* nothing */
130}
131
132/*************************************************************************/
135static void script_client_vars_load(struct section_file *file)
136{
137 luascript_vars_load(main_fcl, file, "script.vars");
138}
139
140/*************************************************************************/
143static void script_client_vars_save(struct section_file *file)
144{
145 luascript_vars_save(main_fcl, file, "script.vars");
146}
147
148/*************************************************************************/
151static void script_client_code_init(void)
152{
154}
155
156/*************************************************************************/
159static void script_client_code_free(void)
160{
161 if (script_client_code) {
164 }
165}
166
167/*************************************************************************/
170static void script_client_code_load(struct section_file *file)
171{
172 if (!script_client_code) {
173 const char *code;
174 const char *section = "script.code";
175
176 code = secfile_lookup_str_default(file, "", "%s", section);
179 }
180}
181
182/*************************************************************************/
185static void script_client_code_save(struct section_file *file)
186{
187 if (script_client_code) {
189 }
190}
191
192/*************************************************************************/
196{
197 if (main_fcl != NULL) {
199
200 return TRUE;
201 }
202
204 if (main_fcl == NULL) {
205 luascript_destroy(main_fcl); /* TODO: main_fcl is NULL here... */
206 main_fcl = NULL;
207
208 return FALSE;
209 }
210
215
216#ifdef MESON_BUILD
217 /* Tolua adds 'tolua_' prefix to _open() function names,
218 * and we can't pass it a basename where the original
219 * 'tolua_' has been stripped when generating from meson. */
221#else /* MESON_BUILD */
223#endif /* MESON_BUILD */
224
226
229
232
233 return TRUE;
234}
235
236/*************************************************************************/
240 const char *format, ...)
241{
242 va_list args;
244
245 switch (level) {
246 case LOG_FATAL:
247 /* Special case - will quit the client. */
248 {
249 char buf[1024];
250
251 va_start(args, format);
252 fc_vsnprintf(buf, sizeof(buf), format, args);
253 va_end(args);
254
255 log_fatal("%s", buf);
256 }
257 break;
258 case LOG_ERROR:
260 break;
261 case LOG_WARN:
263 break;
264 case LOG_NORMAL:
266 break;
267 case LOG_VERBOSE:
269 break;
270 case LOG_DEBUG:
272 break;
273 }
274
275 va_start(args, format);
276 luaconsole_vprintf(ftc_luaconsole, format, args);
277 va_end(args);
278}
279
280/*************************************************************************/
295
296/*************************************************************************/
300{
302
303 /* Variables must be loaded after code is loaded and executed,
304 * so we restore their saved state properly */
306}
307
308/*************************************************************************/
316
317/*************************************************************************/
321{
322 va_list args;
323
324 va_start(args, signal_name);
326 va_end(args);
327}
328
329/*************************************************************************/
333{
334 luascript_signal_create(main_fcl, "new_tech", 0);
335}
int api_game_specenum_open(lua_State *L)
#define str
Definition astring.c:76
char * incite_cost
Definition comments.c:75
const struct ft_color ftc_luaconsole_debug
const struct ft_color ftc_luaconsole_verbose
const struct ft_color ftc_luaconsole_error
const struct ft_color ftc_luaconsole_normal
const struct ft_color ftc_luaconsole_warn
#define fc_assert_ret_val(condition, val)
Definition log.h:194
#define log_fatal(message,...)
Definition log.h:100
log_level
Definition log.h:28
@ LOG_ERROR
Definition log.h:30
@ LOG_DEBUG
Definition log.h:34
@ LOG_NORMAL
Definition log.h:32
@ LOG_FATAL
Definition log.h:29
@ LOG_VERBOSE
Definition log.h:33
@ LOG_WARN
Definition log.h:31
void luaconsole_vprintf(const struct ft_color color, const char *format, va_list args)
bool luascript_callback_invoke(struct fc_lua *fcl, const char *callback_name, int nargs, enum api_types *parg_types, va_list args)
Definition luascript.c:657
void luascript_remove_exported_object(struct fc_lua *fcl, void *object)
Definition luascript.c:700
int luascript_do_string(struct fc_lua *fcl, const char *str, const char *name)
Definition luascript.c:617
int luascript_do_file(struct fc_lua *fcl, const char *filename)
Definition luascript.c:637
void luascript_vars_load(struct fc_lua *fcl, struct section_file *file, const char *section)
Definition luascript.c:763
void luascript_vars_save(struct fc_lua *fcl, struct section_file *file, const char *section)
Definition luascript.c:737
struct fc_lua * luascript_new(luascript_log_func_t output_fct, bool secured_environment)
Definition luascript.c:332
void luascript_destroy(struct fc_lua *fcl)
Definition luascript.c:386
void luascript_signal_free(struct fc_lua *fcl)
void luascript_signal_init(struct fc_lua *fcl)
struct signal_deprecator * luascript_signal_create(struct fc_lua *fcl, const char *signal_name, int nargs,...)
void luascript_signal_emit_valist(struct fc_lua *fcl, const char *signal_name, va_list args)
#define fc_strdup(str)
Definition mem.h:43
const char * secfile_lookup_str_default(const struct section_file *secfile, const char *def, const char *path,...)
#define secfile_insert_str_noescape(secfile, string, path,...)
static void script_client_vars_free(void)
static void script_client_code_load(struct section_file *file)
static char * script_client_code
static void script_client_vars_init(void)
bool script_client_do_file(const char *filename)
static void static void script_client_signal_create(void)
void script_client_state_load(struct section_file *file)
static void script_client_output(struct fc_lua *fcl, enum log_level level, const char *format,...) fc__attribute((__format__(__printf__
static void script_client_code_init(void)
static void script_client_vars_save(struct section_file *file)
bool script_client_init(void)
static void script_client_vars_load(struct section_file *file)
static void script_client_code_free(void)
bool script_client_do_string(const char *str)
void script_client_state_save(struct section_file *file)
void script_client_remove_exported_object(void *object)
static void script_client_code_save(struct section_file *file)
bool script_client_callback_invoke(const char *callback_name, int nargs, enum api_types *parg_types, va_list args)
void script_client_free(void)
void script_client_signal_emit(const char *signal_name,...)
static struct fc_lua * main_fcl
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:190
lua_State * state
Definition luascript.h:45
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap)
Definition support.c:900
#define fc__attribute(x)
Definition support.h:99
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
TOLUA_API int tolua_client_open(lua_State *tolua_S)
TOLUA_API int tolua_common_a_open(lua_State *tolua_S)
TOLUA_API int tolua_common_z_open(lua_State *tolua_S)
TOLUA_API int tolua_game_open(lua_State *tolua_S)
TOLUA_API int tolua_signal_open(lua_State *tolua_S)