From c0a85622beb380b861d5db9faef0531826b9ea95 Mon Sep 17 00:00:00 2001 From: "Gergely Polonkai (W00d5t0ck)" Date: Thu, 22 Mar 2012 12:21:05 +0100 Subject: [PATCH] Added basic configuration file parsing * Created example configuration file * Added wmud_config_init() to parse configuration file Signed-off-by: Gergely Polonkai (W00d5t0ck) --- conf/.gitignore | 1 + conf/wmud.conf.example | 3 ++ configure.ac | 2 + src/main.c | 92 ++++++++++++++++++++++++++++++++++++++++-- src/wmud_types.h | 6 +++ 5 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 conf/.gitignore create mode 100644 conf/wmud.conf.example diff --git a/conf/.gitignore b/conf/.gitignore new file mode 100644 index 0000000..7bc6d90 --- /dev/null +++ b/conf/.gitignore @@ -0,0 +1 @@ +wmud.conf diff --git a/conf/wmud.conf.example b/conf/wmud.conf.example new file mode 100644 index 0000000..0c3a386 --- /dev/null +++ b/conf/wmud.conf.example @@ -0,0 +1,3 @@ +[global] +world file = iminiru.db +port = 4000 diff --git a/configure.ac b/configure.ac index fbd2865..a134ce9 100644 --- a/configure.ac +++ b/configure.ac @@ -20,6 +20,8 @@ if test "$enable_memcached" = "yes"; then AC_DEFINE([ENABLE_MEMCACHED], [1], [Define to compile with Memcached support]) fi +AC_DEFINE([DEFAULT_PORT], [4000], [The default port number if not specified in the config file]) + PKG_CHECK_MODULES([GIO], gio-2.0) PKG_CHECK_MODULES([GLIB], glib-2.0) PKG_CHECK_MODULES([GTHREAD], gthread-2.0) diff --git a/src/main.c b/src/main.c index dc459c8..030ea3f 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,7 @@ #include "config.h" #endif +#include "wmud_types.h" #include "main.h" #include "networking.h" #include "interpreter.h" @@ -19,6 +20,9 @@ GMainContext *game_context; guint32 elapsed_seconds = 0; guint32 elapsed_cycle = 0; GRand *main_rand = NULL; +GQuark WMUD_CONFIG_ERROR = 0; +guint port = 0; +gchar *database_file = NULL; /* rl_sec_elapsed() * @@ -59,17 +63,84 @@ debug_context(char *file, int line) #define DebugContext #endif +void +wmud_type_init(void) +{ + WMUD_CONFIG_ERROR = g_quark_from_string("wmud_config_error"); +} + +gboolean +wmud_config_init(GError **err) +{ + GString *config_file = g_string_new(WMUD_CONFDIR); + GKeyFile *config; + GError *in_err; + + g_string_append(config_file, "/wmud.conf"); + + config = g_key_file_new(); + /* TODO: Error checking */ + g_key_file_load_from_file(config, config_file->str, 0, &in_err); + + if (!g_key_file_has_group(config, "global")) + { + g_set_error(err, WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_NOGLOBAL, "Config file (%s) does not contain a [global] group", config_file->str); + g_key_file_free(config); + g_string_free(config_file, TRUE); + + return FALSE; + } + + port = g_key_file_get_integer(config, "global", "port", &in_err); + if (in_err) + { + if (g_error_matches(in_err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) + { + port = DEFAULT_PORT; + } + else if (g_error_matches(in_err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE)) + { + g_set_error(err, WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_BADPORT, "Config file (%s) contains an invalid port number", config_file->str); + g_key_file_free(config); + g_string_free(config_file, TRUE); + port = 0; + + return FALSE; + } + } + g_clear_error(&in_err); + + database_file = g_key_file_get_string(config, "global", "world file", &in_err); + if (in_err) + { + if (g_error_matches(in_err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) + { + g_set_error(err, WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_NOWORLD, "Config file (%s) does not contain a world file path", config_file->str); + g_string_free(config_file, TRUE); + g_string_free(config_file, TRUE); + database_file = NULL; + + return FALSE; + } + } + + return TRUE; +} + int main(int argc, char **argv) { GMainLoop *game_loop; GSource *timeout_source; guint timeout_id; - GSocketListener *game_listener; + GError *err = NULL; /* Initialize the thread and type system */ g_thread_init(NULL); g_type_init(); + wmud_type_init(); + + /* TODO: Command line parsing */ /* Initialize random number generator */ main_rand = g_rand_new(); @@ -87,10 +158,23 @@ main(int argc, char **argv) /* TODO: Create signal handlers! */ + if (!wmud_config_init(&err)) + { + if (err) + { + g_critical("Config file parsing error: %s", err->message); + } + else + { + g_critical("Config file parsing error!"); + } - wmud_interpreter_init(); - /* TODO: Change 4000 to the port number coming from configuration */ - wmud_networking_init(4000); + return 1; + } + + g_assert(port != 0); + g_assert(database_file != NULL); + wmud_networking_init(port); /* Run the game loop */ g_main_loop_run(game_loop); diff --git a/src/wmud_types.h b/src/wmud_types.h index 9f5e5a5..3a11e7d 100644 --- a/src/wmud_types.h +++ b/src/wmud_types.h @@ -29,5 +29,11 @@ typedef struct _wmudClient { wmudPlayer *player; } wmudClient; +enum { + WMUD_CONFIG_ERROR_SUCCESS, + WMUD_CONFIG_ERROR_NOGLOBAL, + WMUD_CONFIG_ERROR_BADPORT, + WMUD_CONFIG_ERROR_NOWORLD +}; #endif /* __WMUD_TYPES_H__ */