2012-03-08 16:39:45 +00:00
|
|
|
#include <glib.h>
|
|
|
|
#include <gio/gio.h>
|
2012-03-10 17:24:23 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2012-03-08 16:39:45 +00:00
|
|
|
|
2012-03-22 11:21:05 +00:00
|
|
|
#include "wmud_types.h"
|
2012-03-12 12:55:53 +00:00
|
|
|
#include "main.h"
|
|
|
|
#include "networking.h"
|
2012-03-13 01:22:22 +00:00
|
|
|
#include "interpreter.h"
|
2012-03-22 17:34:39 +00:00
|
|
|
#include "db.h"
|
2012-03-12 12:55:53 +00:00
|
|
|
|
2012-03-08 16:39:45 +00:00
|
|
|
struct {
|
|
|
|
char *file;
|
|
|
|
int line;
|
|
|
|
} debug_context_loc = {NULL, 0};
|
|
|
|
|
2012-03-12 12:55:53 +00:00
|
|
|
GMainContext *game_context;
|
2012-03-12 15:42:13 +00:00
|
|
|
guint32 elapsed_seconds = 0;
|
|
|
|
guint32 elapsed_cycle = 0;
|
2012-03-14 19:19:05 +00:00
|
|
|
GRand *main_rand = NULL;
|
2012-03-22 11:21:05 +00:00
|
|
|
GQuark WMUD_CONFIG_ERROR = 0;
|
2012-03-22 17:34:39 +00:00
|
|
|
GQuark WMUD_DB_ERROR = 0;
|
2012-03-22 11:21:05 +00:00
|
|
|
guint port = 0;
|
|
|
|
gchar *database_file = NULL;
|
2012-03-22 17:34:39 +00:00
|
|
|
gchar *admin_email = NULL;
|
2012-03-08 16:39:45 +00:00
|
|
|
|
2012-03-22 10:00:59 +00:00
|
|
|
/* rl_sec_elapsed()
|
|
|
|
*
|
|
|
|
* This function keeps track of elapsed real-world time. It is inaccurate by
|
|
|
|
* design, but it doesn't actually matter.
|
|
|
|
*/
|
2012-03-08 16:39:45 +00:00
|
|
|
gboolean
|
|
|
|
rl_sec_elapsed(gpointer user_data)
|
|
|
|
{
|
2012-03-12 15:42:13 +00:00
|
|
|
elapsed_seconds++;
|
|
|
|
if (elapsed_seconds == G_MAXUINT32)
|
|
|
|
{
|
|
|
|
elapsed_seconds = 0;
|
|
|
|
elapsed_cycle++;
|
|
|
|
}
|
2012-03-08 16:39:45 +00:00
|
|
|
|
2012-03-22 17:34:39 +00:00
|
|
|
if (elapsed_seconds % 30 == 0)
|
|
|
|
{
|
|
|
|
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "Heartbeat");
|
|
|
|
}
|
|
|
|
|
2012-03-08 16:39:45 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-03-10 17:24:23 +00:00
|
|
|
#ifdef DEBUG
|
2012-03-08 16:39:45 +00:00
|
|
|
void
|
2012-03-22 10:00:59 +00:00
|
|
|
/* debug_context()
|
|
|
|
*
|
|
|
|
* This function keeps track of the code flow in some way. It can help with
|
|
|
|
* debugging, as during a SIGSEGV or such signal this will print out the last
|
|
|
|
* place of DebugContext in the code.
|
|
|
|
*/
|
2012-03-08 16:39:45 +00:00
|
|
|
debug_context(char *file, int line)
|
|
|
|
{
|
|
|
|
if (debug_context_loc.file != NULL)
|
|
|
|
g_free(debug_context_loc.file);
|
|
|
|
|
|
|
|
debug_context_loc.file = g_strdup(file);
|
|
|
|
debug_context_loc.line = line;
|
|
|
|
}
|
|
|
|
#define DebugContext debug_context(__FILE__, __LINE__)
|
2012-03-10 17:24:23 +00:00
|
|
|
#else
|
|
|
|
#define DebugContext
|
|
|
|
#endif
|
2012-03-08 16:39:45 +00:00
|
|
|
|
2012-03-22 11:21:05 +00:00
|
|
|
void
|
|
|
|
wmud_type_init(void)
|
|
|
|
{
|
|
|
|
WMUD_CONFIG_ERROR = g_quark_from_string("wmud_config_error");
|
2012-03-22 17:34:39 +00:00
|
|
|
WMUD_DB_ERROR = g_quark_from_string("wmud_db_error");
|
2012-03-22 11:21:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
wmud_config_init(GError **err)
|
|
|
|
{
|
|
|
|
GString *config_file = g_string_new(WMUD_CONFDIR);
|
|
|
|
GKeyFile *config;
|
2012-03-22 17:34:39 +00:00
|
|
|
GError *in_err = NULL;
|
2012-03-22 11:21:05 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-03-22 17:34:39 +00:00
|
|
|
g_clear_error(&in_err);
|
2012-03-22 11:21:05 +00:00
|
|
|
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;
|
|
|
|
}
|
2012-03-22 17:34:39 +00:00
|
|
|
|
|
|
|
return FALSE;
|
2012-03-22 11:21:05 +00:00
|
|
|
}
|
|
|
|
|
2012-03-22 17:34:39 +00:00
|
|
|
g_clear_error(&in_err);
|
2012-03-22 11:21:05 +00:00
|
|
|
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);
|
2012-03-22 17:34:39 +00:00
|
|
|
g_key_file_free(config);
|
2012-03-22 11:21:05 +00:00
|
|
|
g_string_free(config_file, TRUE);
|
|
|
|
database_file = NULL;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-22 17:34:39 +00:00
|
|
|
g_clear_error(&in_err);
|
|
|
|
admin_email = g_key_file_get_string(config, "global", "admin email", &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_NOEMAIL, "Config file (%s) does not contain an admin e-mail address", config_file->str);
|
|
|
|
g_key_file_free(config);
|
|
|
|
g_string_free(config_file, TRUE);
|
|
|
|
admin_email = NULL;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_key_file_free(config);
|
|
|
|
g_string_free(config_file, TRUE);
|
|
|
|
|
2012-03-22 11:21:05 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-03-22 17:46:15 +00:00
|
|
|
gpointer
|
|
|
|
game_thread_func(GMainLoop *game_loop)
|
|
|
|
{
|
|
|
|
/* Run the game loop */
|
|
|
|
g_main_loop_run(game_loop);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-07 22:00:58 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2012-03-08 16:39:45 +00:00
|
|
|
GMainLoop *game_loop;
|
2012-03-12 12:55:53 +00:00
|
|
|
GSource *timeout_source;
|
2012-03-08 16:39:45 +00:00
|
|
|
guint timeout_id;
|
2012-03-22 11:21:05 +00:00
|
|
|
GError *err = NULL;
|
2012-03-22 17:46:15 +00:00
|
|
|
GThread *game_thread;
|
2012-03-08 16:39:45 +00:00
|
|
|
|
2012-03-22 10:00:59 +00:00
|
|
|
/* Initialize the thread and type system */
|
2012-03-12 15:42:13 +00:00
|
|
|
g_thread_init(NULL);
|
2012-03-08 16:39:45 +00:00
|
|
|
g_type_init();
|
2012-03-22 11:21:05 +00:00
|
|
|
wmud_type_init();
|
|
|
|
|
|
|
|
/* TODO: Command line parsing */
|
2012-03-08 16:39:45 +00:00
|
|
|
|
2012-03-22 10:00:59 +00:00
|
|
|
/* Initialize random number generator */
|
2012-03-14 19:19:05 +00:00
|
|
|
main_rand = g_rand_new();
|
|
|
|
|
2012-03-22 10:00:59 +00:00
|
|
|
/* Create the game context and main loop */
|
2012-03-08 16:39:45 +00:00
|
|
|
game_context = g_main_context_new();
|
|
|
|
game_loop = g_main_loop_new(game_context, FALSE);
|
|
|
|
|
2012-03-22 10:00:59 +00:00
|
|
|
/* Create the timeout source which keeps track of elapsed real-world
|
|
|
|
* time */
|
2012-03-08 16:39:45 +00:00
|
|
|
timeout_source = g_timeout_source_new(1000);
|
|
|
|
g_source_set_callback(timeout_source, rl_sec_elapsed, NULL, NULL);
|
|
|
|
timeout_id = g_source_attach(timeout_source, game_context);
|
|
|
|
g_source_unref(timeout_source);
|
|
|
|
|
2012-03-22 10:00:59 +00:00
|
|
|
/* TODO: Create signal handlers! */
|
|
|
|
|
2012-03-22 11:21:05 +00:00
|
|
|
if (!wmud_config_init(&err))
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
g_critical("Config file parsing error: %s", err->message);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_critical("Config file parsing error!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2012-03-08 16:39:45 +00:00
|
|
|
|
2012-03-22 11:21:05 +00:00
|
|
|
g_assert(port != 0);
|
|
|
|
g_assert(database_file != NULL);
|
2012-03-22 17:34:39 +00:00
|
|
|
|
|
|
|
g_clear_error(&err);
|
|
|
|
if (!wmud_db_init(&err))
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
g_critical("Database initialization error: %s", err->message);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_critical("Database initialization error!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
g_clear_error(&err);
|
|
|
|
if (!wmud_networking_init(port, &err))
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
g_critical("Database initialization error: %s", err->message);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_critical("Database initialization error!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_clear_error(&err);
|
|
|
|
wmud_db_players_load(&err);
|
2012-03-08 16:39:45 +00:00
|
|
|
|
2012-03-22 17:46:15 +00:00
|
|
|
/* Initialization ends here */
|
|
|
|
|
|
|
|
g_clear_error(&err);
|
|
|
|
game_thread = g_thread_create((GThreadFunc)game_thread_func, game_loop, TRUE, &err);
|
|
|
|
|
|
|
|
/* Initialize other threads here */
|
|
|
|
|
|
|
|
g_thread_join(game_thread);
|
2012-03-07 22:00:58 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|