12 #include <QStandardPaths>
15 #include "sol/sol.hpp"
26 #include "tolua_common_a_gen.h"
27 #include "tolua_game_gen.h"
36 #define SCRIPT_FCDB_LUA_FILE "freeciv21/database.lua"
44 const char *format, ...)
48 static sol::state *
fcl =
nullptr;
82 for (
const auto &
name : {
89 qCritical(
"Database script '%s' does not define the required function "
97 for (
const auto &
name : {
103 qDebug(
"Database script '%s' does not define the optional "
116 const char *format, ...)
122 va_start(args, format);
155 (*fcl)[
"fcdb"] =
fcl->create_table_with(
162 "param",
fcl->create_table_with(
"HOST",
"host",
165 "PASSWORD",
"password",
166 "DATABASE",
"database",
167 "TABLE_USER",
"table_user",
168 "TABLE_LOG",
"table_log",
169 "BACKEND",
"backend")
180 if (
fcl !=
nullptr) {
184 auto fcdb_luafile_resolved =
185 fcdb_luafile.isEmpty() ?
187 QStandardPaths::locate(QStandardPaths::GenericConfigLocation,
190 if (fcdb_luafile_resolved.isEmpty()) {
192 " in the following directories:"
193 << QStandardPaths::standardLocations(
194 QStandardPaths::GenericConfigLocation);
199 fcl =
new sol::state();
200 fcl->open_libraries();
208 tolua_game_open(
fcl->lua_state());
211 }
catch (
const std::exception &e) {
212 qCritical() <<
"Error loading the Freeciv21 database lua definition:"
221 if (!
fcl->safe_script_file(qUtf8Printable(fcdb_luafile_resolved))
223 qCritical(
"Error loading the Freeciv21 database lua script '%s'.",
224 qUtf8Printable(fcdb_luafile_resolved));
231 qCritical(
"Error connecting to the database");
243 if (
fcl !=
nullptr) {
246 "Error closing the database connection. Continuing anyway...");
267 auto result =
fcl->safe_script(str).valid();
281 const sol::protected_function database_init = (*fcl)[
"database_init"];
282 auto result = database_init();
283 if (result.valid()) {
286 qCritical() << sol::error(result).what();
296 const sol::protected_function database_free = (*fcl)[
"database_free"];
297 auto result = database_free();
298 if (result.valid()) {
301 qCritical() << sol::error(result).what();
310 const char *delegate,
bool &success)
312 const sol::protected_function user_delegate_to =
313 (*fcl)[
"user_delegate_to"];
314 auto result = user_delegate_to(pconn, pplayer, delegate);
315 if (result.valid()) {
319 qCritical() << sol::error(result).what();
329 const sol::protected_function user_exists = (*fcl)[
"user_exists"];
330 auto result = user_exists(pconn);
331 if (result.valid()) {
335 qCritical() << sol::error(result).what();
345 const sol::protected_function user_save = (*fcl)[
"user_save"];
346 auto result = user_save(pconn, password);
347 if (result.valid()) {
350 qCritical() << sol::error(result).what();
361 const sol::protected_function user_take = (*fcl)[
"user_take"];
362 auto result = user_take(requester, taker,
player, will_observe);
363 if (result.valid()) {
367 qCritical() << sol::error(result).what();
378 const sol::protected_function user_verify = (*fcl)[
"user_verify"];
379 auto result = user_verify(pconn, username);
380 if (result.valid()) {
384 qCritical() << sol::error(result).what();
const char * auth_get_username(struct connection *pconn)
Get username for connection.
const char * auth_get_ipaddr(struct connection *pconn)
Get connection ip address.
const char * fcdb_option_get(const char *type)
Return the selected fcdb config value.
constexpr auto LOG_NORMAL
void luascript_init(fc_lua *fcl)
Sets the freeciv lua struct for a lua state.
void luascript_common_z(lua_State *L)
Runs tolua_common_z.lua.
void luascript_common_a(lua_State *L)
Runs tolua_common_a.lua.
static void script_fcdb_register_functions()
Registers FCDB-related functions in the Lua state.
static bool script_fcdb_functions_check(const char *fcdb_luafile)
fcdb callback functions that must be defined in the lua script 'database.lua':
bool script_fcdb_user_save(connection *pconn, const char *password)
Save a new user.
bool script_fcdb_user_delegate_to(connection *pconn, player *pplayer, const char *delegate, bool &success)
returns Bool, whether pconn is allowed to delegate player to delegate.
bool script_fcdb_user_exists(connection *pconn, bool &exists)
Check if the user exists.
bool script_fcdb_do_string(struct connection *caller, const char *str)
Parse and execute the script in str in the lua instance for the freeciv database.
bool script_fcdb_user_take(connection *requester, connection *taker, player *player, bool will_observe, bool &success)
returns Bool, whether requester is allowed to attach taker to pplayer.
#define SCRIPT_FCDB_LUA_FILE
static void static sol::state * fcl
Lua virtual machine state.
static void script_fcdb_cmd_reply(struct fc_lua *lfcl, QtMsgType level, const char *format,...) fc__attribute((__format__(__printf__
Send the message via cmd_reply().
static fc_lua fcl_compat
Tolua compatibility.
bool script_fcdb_init(const QString &fcdb_luafile)
Initialize the scripting state.
static bool script_fcdb_database_init()
test and initialise the database.
void script_fcdb_free()
Free the scripting data.
static bool script_fcdb_database_free()
free the database.
bool script_fcdb_user_verify(connection *pconn, const char *username, bool &success)
Check the credentials of the user.
struct setting_list * level[OLEVELS_NUM]
void cmd_reply(enum command_id cmd, struct connection *caller, enum rfc_status rfc_status, const char *format,...)
var-args version as above, no prefix
struct connection * caller
luascript_log_func_t output_fct
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap)
int fc__attribute((nonnull(1, 3)))