Freeciv-3.3: /fast/freeciv/nightly/main/client/luascript/script_client.c Source File

Freeciv-3.3

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*****************************************************************************/
54 static struct fc_lua *main_fcl = NULL;
55
56/*****************************************************************************
57 Optional game script code (useful for scenarios).
58*****************************************************************************/
59 static 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, ...)
72 fc__attribute((__format__ (__printf__, 3, 4)));
73
74 static void script_client_signal_create(void);
75
76/*************************************************************************/
79 bool script_client_do_string(const char *str)
80{
81 int status = luascript_do_string(main_fcl, str, "cmd");
82
83 return (status == 0);
84}
85
86/*************************************************************************/
89 bool 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/*************************************************************************/
99 bool script_client_callback_invoke(const char *callback_name, int nargs,
100 enum api_types *parg_types, va_list args)
101{
102 return luascript_callback_invoke(main_fcl, callback_name, nargs, parg_types,
103 args);
104}
105
106/*************************************************************************/
111 void script_client_remove_exported_object(void *object)
112{
113 luascript_remove_exported_object(main_fcl, object);
114}
115
116/*************************************************************************/
119 static void script_client_vars_init(void)
120{
121 /* nothing */
122}
123
124/*************************************************************************/
127 static void script_client_vars_free(void)
128{
129 /* nothing */
130}
131
132/*************************************************************************/
135 static void script_client_vars_load(struct section_file *file)
136{
137 luascript_vars_load(main_fcl, file, "script.vars");
138}
139
140/*************************************************************************/
143 static void script_client_vars_save(struct section_file *file)
144{
145 luascript_vars_save(main_fcl, file, "script.vars");
146}
147
148/*************************************************************************/
151 static void script_client_code_init(void)
152{
153 script_client_code = NULL;
154}
155
156/*************************************************************************/
159 static void script_client_code_free(void)
160{
161 if (script_client_code) {
162 free(script_client_code);
163 script_client_code = NULL;
164 }
165}
166
167/*************************************************************************/
170 static 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);
177 script_client_code = fc_strdup(code);
178 luascript_do_string(main_fcl, script_client_code, section);
179 }
180}
181
182/*************************************************************************/
185 static void script_client_code_save(struct section_file *file)
186{
187 if (script_client_code) {
188 secfile_insert_str_noescape(file, script_client_code, "script.code");
189 }
190}
191
192/*************************************************************************/
195 bool script_client_init(void)
196{
197 if (main_fcl != NULL) {
198 fc_assert_ret_val(main_fcl->state != NULL, FALSE);
199
200 return TRUE;
201 }
202
203 main_fcl = luascript_new(script_client_output, TRUE);
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
211 tolua_common_a_open(main_fcl->state);
212 api_game_specenum_open(main_fcl->state);
213 tolua_game_open(main_fcl->state);
214 tolua_signal_open(main_fcl->state);
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. */
220 tolua_tolua_client_open(main_fcl->state);
221#else /* MESON_BUILD */
222 tolua_client_open(main_fcl->state);
223#endif /* MESON_BUILD */
224
225 tolua_common_z_open(main_fcl->state);
226
227 script_client_code_init();
228 script_client_vars_init();
229
230 luascript_signal_init(main_fcl);
231 script_client_signal_create();
232
233 return TRUE;
234}
235
236/*************************************************************************/
239 static void script_client_output(struct fc_lua *fcl, enum log_level level,
240 const char *format, ...)
241{
242 va_list args;
243 struct ft_color ftc_luaconsole = ftc_luaconsole_error;
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:
259 ftc_luaconsole = ftc_luaconsole_error;
260 break;
261 case LOG_WARN:
262 ftc_luaconsole = ftc_luaconsole_warn;
263 break;
264 case LOG_NORMAL:
265 ftc_luaconsole = ftc_luaconsole_normal;
266 break;
267 case LOG_VERBOSE:
268 ftc_luaconsole = ftc_luaconsole_verbose;
269 break;
270 case LOG_DEBUG:
271 ftc_luaconsole = ftc_luaconsole_debug;
272 break;
273 }
274
275 va_start(args, format);
276 luaconsole_vprintf(ftc_luaconsole, format, args);
277 va_end(args);
278}
279
280/*************************************************************************/
283 void script_client_free(void)
284{
285 if (main_fcl != NULL) {
286 script_client_code_free();
287 script_client_vars_free();
288
289 luascript_signal_free(main_fcl);
290
291 luascript_destroy(main_fcl);
292 main_fcl = NULL;
293 }
294}
295
296/*************************************************************************/
299 void script_client_state_load(struct section_file *file)
300{
301 script_client_code_load(file);
302
303 /* Variables must be loaded after code is loaded and executed,
304 * so we restore their saved state properly */
305 script_client_vars_load(file);
306}
307
308/*************************************************************************/
311 void script_client_state_save(struct section_file *file)
312{
313 script_client_code_save(file);
314 script_client_vars_save(file);
315}
316
317/*************************************************************************/
320 void script_client_signal_emit(const char *signal_name, ...)
321{
322 va_list args;
323
324 va_start(args, signal_name);
325 luascript_signal_emit_valist(main_fcl, signal_name, args);
326 va_end(args);
327}
328
329/*************************************************************************/
332 static void script_client_signal_create(void)
333{
334 luascript_signal_create(main_fcl, "new_tech", 0);
335}
api_game_specenum_open
int api_game_specenum_open(lua_State *L)
Definition api_game_specenum.c:48
api_game_specenum.h
str
#define str
Definition astring.c:76
incite_cost
char * incite_cost
Definition comments.c:74
ftc_luaconsole_debug
const struct ft_color ftc_luaconsole_debug
Definition featured_text.c:104
ftc_luaconsole_verbose
const struct ft_color ftc_luaconsole_verbose
Definition featured_text.c:103
ftc_luaconsole_error
const struct ft_color ftc_luaconsole_error
Definition featured_text.c:100
ftc_luaconsole_normal
const struct ft_color ftc_luaconsole_normal
Definition featured_text.c:102
ftc_luaconsole_warn
const struct ft_color ftc_luaconsole_warn
Definition featured_text.c:101
featured_text.h
log.h
fc_assert_ret_val
#define fc_assert_ret_val(condition, val)
Definition log.h:194
log_fatal
#define log_fatal(message,...)
Definition log.h:100
log_level
log_level
Definition log.h:28
LOG_ERROR
@ LOG_ERROR
Definition log.h:30
LOG_DEBUG
@ LOG_DEBUG
Definition log.h:34
LOG_NORMAL
@ LOG_NORMAL
Definition log.h:32
LOG_FATAL
@ LOG_FATAL
Definition log.h:29
LOG_VERBOSE
@ LOG_VERBOSE
Definition log.h:33
LOG_WARN
@ LOG_WARN
Definition log.h:31
luaconsole_vprintf
void luaconsole_vprintf(const struct ft_color color, const char *format, va_list args)
Definition luaconsole_common.c:73
luaconsole_common.h
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)
Definition luascript.c:658
luascript_remove_exported_object
void luascript_remove_exported_object(struct fc_lua *fcl, void *object)
Definition luascript.c:701
luascript_do_string
int luascript_do_string(struct fc_lua *fcl, const char *str, const char *name)
Definition luascript.c:618
luascript_do_file
int luascript_do_file(struct fc_lua *fcl, const char *filename)
Definition luascript.c:638
luascript_vars_load
void luascript_vars_load(struct fc_lua *fcl, struct section_file *file, const char *section)
Definition luascript.c:764
luascript_vars_save
void luascript_vars_save(struct fc_lua *fcl, struct section_file *file, const char *section)
Definition luascript.c:738
luascript_new
struct fc_lua * luascript_new(luascript_log_func_t output_fct, bool secured_environment)
Definition luascript.c:333
luascript_destroy
void luascript_destroy(struct fc_lua *fcl)
Definition luascript.c:387
luascript.h
luascript_signal_free
void luascript_signal_free(struct fc_lua *fcl)
Definition luascript_signal.c:415
luascript_signal_init
void luascript_signal_init(struct fc_lua *fcl)
Definition luascript_signal.c:402
luascript_signal_create
struct signal_deprecator * luascript_signal_create(struct fc_lua *fcl, const char *signal_name, int nargs,...)
Definition luascript_signal.c:255
luascript_signal_emit_valist
void luascript_signal_emit_valist(struct fc_lua *fcl, const char *signal_name, va_list args)
Definition luascript_signal.c:169
fc_strdup
#define fc_strdup(str)
Definition mem.h:43
secfile_lookup_str_default
const char * secfile_lookup_str_default(const struct section_file *secfile, const char *def, const char *path,...)
Definition registry_ini.c:2205
secfile_insert_str_noescape
#define secfile_insert_str_noescape(secfile, string, path,...)
Definition registry_ini.h:173
script_client_vars_free
static void script_client_vars_free(void)
Definition script_client.c:127
script_client_code_load
static void script_client_code_load(struct section_file *file)
Definition script_client.c:170
script_client_code
static char * script_client_code
Definition script_client.c:59
script_client_vars_init
static void script_client_vars_init(void)
Definition script_client.c:119
script_client_do_file
bool script_client_do_file(const char *filename)
Definition script_client.c:89
script_client_signal_create
static void static void script_client_signal_create(void)
Definition script_client.c:332
script_client_state_load
void script_client_state_load(struct section_file *file)
Definition script_client.c:299
script_client_output
static void script_client_output(struct fc_lua *fcl, enum log_level level, const char *format,...) fc__attribute((__format__(__printf__
Definition script_client.c:239
script_client_code_init
static void script_client_code_init(void)
Definition script_client.c:151
script_client_vars_save
static void script_client_vars_save(struct section_file *file)
Definition script_client.c:143
script_client_init
bool script_client_init(void)
Definition script_client.c:195
script_client_vars_load
static void script_client_vars_load(struct section_file *file)
Definition script_client.c:135
script_client_code_free
static void script_client_code_free(void)
Definition script_client.c:159
script_client_do_string
bool script_client_do_string(const char *str)
Definition script_client.c:79
script_client_state_save
void script_client_state_save(struct section_file *file)
Definition script_client.c:311
script_client_remove_exported_object
void script_client_remove_exported_object(void *object)
Definition script_client.c:111
script_client_code_save
static void script_client_code_save(struct section_file *file)
Definition script_client.c:185
script_client_callback_invoke
bool script_client_callback_invoke(const char *callback_name, int nargs, enum api_types *parg_types, va_list args)
Definition script_client.c:99
script_client_free
void script_client_free(void)
Definition script_client.c:283
script_client_signal_emit
void script_client_signal_emit(const char *signal_name,...)
Definition script_client.c:320
main_fcl
static struct fc_lua * main_fcl
Definition script_client.c:54
script_client.h
level
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:190
fc_lua
Definition luascript.h:44
fc_lua::state
lua_State * state
Definition luascript.h:45
ft_color
Definition featured_text.h:141
section_file
Definition section_file.h:36
section
Definition section_file.h:28
fc_vsnprintf
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap)
Definition support.c:886
fc__attribute
#define fc__attribute(x)
Definition support.h:99
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47
tolua_client_open
TOLUA_API int tolua_client_open(lua_State *tolua_S)
Definition tolua_client_gen.c:88
tolua_client_gen.h
tolua_common_a_open
TOLUA_API int tolua_common_a_open(lua_State *tolua_S)
Definition tolua_common_a_gen.c:829
tolua_common_a_gen.h
tolua_common_z_open
TOLUA_API int tolua_common_z_open(lua_State *tolua_S)
Definition tolua_common_z_gen.c:144
tolua_common_z_gen.h
tolua_game_open
TOLUA_API int tolua_game_open(lua_State *tolua_S)
Definition tolua_game_gen.c:5817
tolua_game_gen.h
tolua_signal_open
TOLUA_API int tolua_signal_open(lua_State *tolua_S)
Definition tolua_signal_gen.c:259
tolua_signal_gen.h