36#ifdef HAVE_FCDB_POSTGRES
37#include "ls_postgres.h"
39#ifdef HAVE_FCDB_SQLITE3
40#include "ls_sqlite3.h"
69#define SCRIPT_FCDB_LUA_FILE "database.lua"
71static void script_fcdb_functions_define(
void);
72static bool script_fcdb_functions_check(
const char *fcdb_luafile);
75 const char *format, ...)
81static struct
fc_lua *fcl = NULL;
109static
void script_fcdb_functions_define(
void)
117 API_TYPE_STRING, API_TYPE_BOOL);
123 API_TYPE_CONNECTION, API_TYPE_PLAYER, API_TYPE_STRING,
126 API_TYPE_CONNECTION, API_TYPE_PLAYER, API_TYPE_BOOL,
133static bool script_fcdb_functions_check(
const char *fcdb_luafile)
140 missing_func_optional)) {
142 log_error(
"Database script '%s' does not define the required function "
143 "'%s'.", fcdb_luafile, func_name);
147 log_verbose(
"Database script '%s' does not define the optional "
148 "function '%s'.", fcdb_luafile, func_name);
162 const char *format, ...)
168 va_start(args, format);
198static int md5sum(lua_State *L)
200 int n = lua_gettop(L);
202 const char *plaintext;
205 if (
n != 1 || lua_type(L, -1) != LUA_TSTRING) {
206 lua_pushliteral(L,
"invalid argument");
210 plaintext = lua_tolstring(L, -1, &
len);
213 lua_pushstring(L, sum);
233 fcdb_luafile = FC_CONF_PATH
"/" SCRIPT_FCDB_LUA_FILE;
238 log_error(
"Error loading the Freeciv database lua definition.");
249 tolua_tolua_fcdb_open(fcl->state);
253 lua_register(fcl->state,
"md5sum", md5sum);
254#ifdef HAVE_FCDB_MYSQL
255 luaL_requiref(fcl->state,
"ls_mysql", luaopen_luasql_mysql, 1);
256 lua_pop(fcl->state, 1);
259 luaL_requiref(fcl->state,
"ls_odbc", luaopen_luasql_odbc, 1);
260 lua_pop(fcl->state, 1);
262#ifdef HAVE_FCDB_POSTGRES
263 luaL_requiref(fcl->state,
"ls_postgres", luaopen_luasql_postgres, 1);
264 lua_pop(fcl->state, 1);
266#ifdef HAVE_FCDB_SQLITE3
267 luaL_requiref(fcl->state,
"ls_sqlite3", luaopen_luasql_sqlite3, 1);
268 lua_pop(fcl->state, 1);
275 script_fcdb_functions_define();
278 || !script_fcdb_functions_check(fcdb_luafile)) {
279 log_error(
"Error loading the Freeciv database lua script '%s'.",
286 log_error(
"Error connecting to the database");
307 va_start(args, func_name);
323 log_error(
"Error closing the database connection. Continuing anyway...");
347 save_caller = fcl->caller;
348 save_output_fct = fcl->output_fct;
349 fcl->output_fct = script_fcdb_cmd_reply;
350 fcl->caller = caller;
355 fcl->caller = save_caller;
356 fcl->output_fct = save_output_fct;
#define log_verbose(message,...)
#define fc_assert_ret_val(condition, val)
#define log_fatal(message,...)
#define log_error(message,...)
int luascript_do_string(struct fc_lua *fcl, const char *str, const char *name)
int luascript_do_file(struct fc_lua *fcl, const char *filename)
struct fc_lua * luascript_new(luascript_log_func_t output_fct, bool secured_environment)
void luascript_destroy(struct fc_lua *fcl)
void(* luascript_log_func_t)(struct fc_lua *fcl, enum log_level level, const char *format,...) fc__attribute((__format__(__printf__
bool luascript_func_call_valist(struct fc_lua *fcl, const char *func_name, va_list args)
bool luascript_func_check(struct fc_lua *fcl, struct strvec *missing_func_required, struct strvec *missing_func_optional)
void luascript_func_init(struct fc_lua *fcl)
void luascript_func_add(struct fc_lua *fcl, const char *func_name, bool required, int nargs, int nreturns,...)
void create_md5sum(const unsigned char *input, int len, char output[MD5_HEX_BYTES+1])
void script_fcdb_free(void)
bool script_fcdb_do_string(struct connection *caller, const char *str)
bool script_fcdb_call(const char *func_name,...)
bool script_fcdb_init(const char *fcdb_luafile)
struct setting_list * level[OLEVELS_NUM]
void cmd_reply(enum command_id cmd, struct connection *caller, enum rfc_status rfc_status, const char *format,...)
void strvec_destroy(struct strvec *psv)
struct strvec * strvec_new(void)
#define strvec_iterate(psv, str)
#define strvec_iterate_end
struct connection * caller
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap)
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_fcdb_open(lua_State *tolua_S)
TOLUA_API int tolua_game_open(lua_State *tolua_S)