Restructured code for better readability

This commit is contained in:
Gergely Polonkai 2013-01-02 02:04:53 +01:00
parent f71ff426e5
commit 78b21fdb1a
11 changed files with 291 additions and 484 deletions

View File

@ -98,8 +98,7 @@ wmud_config_init(ConfigData **config_data, GError **err)
if (!config_data) if (!config_data)
return FALSE; return FALSE;
if (*config_data) if (*config_data) {
{
g_clear_error(err); g_clear_error(err);
g_set_error(err, WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_REUSE, "Configuration pointer reuse. Please file a bug report!"); g_set_error(err, WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_REUSE, "Configuration pointer reuse. Please file a bug report!");
return FALSE; return FALSE;
@ -113,8 +112,7 @@ wmud_config_init(ConfigData **config_data, GError **err)
/* TODO: Error checking */ /* TODO: Error checking */
g_key_file_load_from_file(config, config_file->str, 0, &in_err); g_key_file_load_from_file(config, config_file->str, 0, &in_err);
if (!g_key_file_has_group(config, "global")) 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_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_key_file_free(config);
g_string_free(config_file, TRUE); g_string_free(config_file, TRUE);
@ -122,8 +120,7 @@ wmud_config_init(ConfigData **config_data, GError **err)
return FALSE; return FALSE;
} }
if (!g_key_file_has_group(config, "smtp")) if (!g_key_file_has_group(config, "smtp")) {
{
g_set_error(err, WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_NOSMTP, "Config file (%s) does not contain an [smtp] group", config_file->str); g_set_error(err, WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_NOSMTP, "Config file (%s) does not contain an [smtp] group", config_file->str);
g_key_file_free(config); g_key_file_free(config);
g_string_free(config_file, TRUE); g_string_free(config_file, TRUE);
@ -135,12 +132,9 @@ wmud_config_init(ConfigData **config_data, GError **err)
(*config_data)->port = g_key_file_get_integer(config, "global", "port", &in_err); (*config_data)->port = g_key_file_get_integer(config, "global", "port", &in_err);
if (in_err) if (in_err)
{ {
if (g_error_matches(in_err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) if (g_error_matches(in_err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) {
{
(*config_data)->port = DEFAULT_PORT; (*config_data)->port = DEFAULT_PORT;
} } else if (g_error_matches(in_err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE)) {
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_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_key_file_free(config);
g_string_free(config_file, TRUE); g_string_free(config_file, TRUE);
@ -154,62 +148,46 @@ wmud_config_init(ConfigData **config_data, GError **err)
g_clear_error(&in_err); g_clear_error(&in_err);
(*config_data)->database_file = g_key_file_get_string(config, "global", "world file", &in_err); (*config_data)->database_file = g_key_file_get_string(config, "global", "world file", &in_err);
if (in_err) if (in_err && 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);
if (g_error_matches(in_err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) g_key_file_free(config);
{ g_string_free(config_file, TRUE);
g_set_error(err, WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_NOWORLD, "Config file (%s) does not contain a world file path", config_file->str); wmud_configdata_free(config_data);
g_key_file_free(config);
g_string_free(config_file, TRUE);
wmud_configdata_free(config_data);
return FALSE; return FALSE;
}
} }
g_clear_error(&in_err); g_clear_error(&in_err);
(*config_data)->admin_email = g_key_file_get_string(config, "global", "admin email", &in_err); (*config_data)->admin_email = g_key_file_get_string(config, "global", "admin email", &in_err);
if (in_err) if (in_err && 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);
if (g_error_matches(in_err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) g_key_file_free(config);
{ g_string_free(config_file, TRUE);
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); wmud_configdata_free(config_data);
g_key_file_free(config);
g_string_free(config_file, TRUE);
wmud_configdata_free(config_data);
return FALSE; return FALSE;
}
} }
g_clear_error(&in_err); g_clear_error(&in_err);
(*config_data)->smtp_server = g_key_file_get_string(config, "smtp", "smtp server", &in_err); (*config_data)->smtp_server = g_key_file_get_string(config, "smtp", "smtp server", &in_err);
if (in_err) if (in_err && 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_NOSMTPSERVER, "Config file (%s) does not contain an smtp server address", config_file->str);
if (g_error_matches(in_err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) g_key_file_free(config);
{ g_string_free(config_file, TRUE);
g_set_error(err, WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_NOSMTPSERVER, "Config file (%s) does not contain an smtp server address", config_file->str); wmud_configdata_free(config_data);
g_key_file_free(config);
g_string_free(config_file, TRUE);
wmud_configdata_free(config_data);
return FALSE; return FALSE;
}
} }
g_clear_error(&in_err); g_clear_error(&in_err);
(*config_data)->smtp_sender = g_key_file_get_string(config, "smtp", "smtp sender", &in_err); (*config_data)->smtp_sender = g_key_file_get_string(config, "smtp", "smtp sender", &in_err);
if (in_err) if (in_err && 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_NOSMTPSENDER, "Config file (%s) does not contain an smtp sender name", config_file->str);
if (g_error_matches(in_err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) g_key_file_free(config);
{ g_string_free(config_file, TRUE);
g_set_error(err, WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_NOSMTPSENDER, "Config file (%s) does not contain an smtp sender name", config_file->str); wmud_configdata_free(config_data);
g_key_file_free(config);
g_string_free(config_file, TRUE);
wmud_configdata_free(config_data);
return FALSE; return FALSE;
}
} }
g_key_file_free(config); g_key_file_free(config);
@ -217,4 +195,3 @@ wmud_config_init(ConfigData **config_data, GError **err)
return TRUE; return TRUE;
} }

201
wmud/db.c
View File

@ -59,8 +59,7 @@ wmud_db_init(GError **err)
g_string_append_printf(db_file, "/%s", active_config->database_file); g_string_append_printf(db_file, "/%s", active_config->database_file);
if ((sqlite_code = sqlite3_open(db_file->str, &dbh)) != SQLITE_OK) if ((sqlite_code = sqlite3_open(db_file->str, &dbh)) != SQLITE_OK) {
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_CANTOPEN, "Can not open databsae file (%s): %s", db_file->str, sqlite3_errmsg(dbh)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_CANTOPEN, "Can not open databsae file (%s): %s", db_file->str, sqlite3_errmsg(dbh));
return FALSE; return FALSE;
@ -89,18 +88,15 @@ wmud_db_load_players(GError **err)
return FALSE; return FALSE;
} }
if ((sqlite_code = sqlite3_prepare_v2(dbh, "SELECT id, login, password, email FROM players", -1, &sth, NULL)) != SQLITE_OK) if ((sqlite_code = sqlite3_prepare_v2(dbh, "SELECT id, login, password, email FROM players", -1, &sth, NULL)) != SQLITE_OK) {
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad query in wmud_db_load_players(): %s", sqlite3_errmsg(dbh)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad query in wmud_db_load_players(): %s", sqlite3_errmsg(dbh));
return FALSE; return FALSE;
} }
while (1) while (1) {
{
sqlite_code = sqlite3_step(sth); sqlite_code = sqlite3_step(sth);
if (sqlite_code == SQLITE_ROW) if (sqlite_code == SQLITE_ROW) {
{
wmudPlayer *player = g_new0(wmudPlayer, 1); wmudPlayer *player = g_new0(wmudPlayer, 1);
player->id = sqlite3_column_int(sth, 0); player->id = sqlite3_column_int(sth, 0);
player->player_name = g_strdup((gchar *)sqlite3_column_text(sth, 1)); player->player_name = g_strdup((gchar *)sqlite3_column_text(sth, 1));
@ -110,13 +106,9 @@ wmud_db_load_players(GError **err)
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded player _%s_", player->player_name); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded player _%s_", player->player_name);
players = g_slist_prepend(players, player); players = g_slist_prepend(players, player);
} } else if (sqlite_code == SQLITE_DONE) {
else if (sqlite_code == SQLITE_DONE)
{
break; break;
} } else {
else
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Query error in wmud_db_load_players(): %s", sqlite3_errmsg(dbh)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Query error in wmud_db_load_players(): %s", sqlite3_errmsg(dbh));
sqlite3_finalize(sth); sqlite3_finalize(sth);
return FALSE; return FALSE;
@ -144,37 +136,32 @@ wmud_db_save_player(wmudPlayer *player, GError **err)
sqlite3_stmt *sth = NULL; sqlite3_stmt *sth = NULL;
int sqlite_code; int sqlite_code;
if (dbh == NULL) if (dbh == NULL) {
{
if (err) if (err)
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized"); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized");
return FALSE; return FALSE;
} }
if ((sqlite_code = sqlite3_prepare_v2(dbh, "INSERT INTO players (id, login, password, email) VALUES (NULL, ?, NULL, ?)", -1, &sth, NULL)) != SQLITE_OK) if ((sqlite_code = sqlite3_prepare_v2(dbh, "INSERT INTO players (id, login, password, email) VALUES (NULL, ?, NULL, ?)", -1, &sth, NULL)) != SQLITE_OK) {
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad query in wmud_db_save_player(): %s", sqlite3_errmsg(dbh)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad query in wmud_db_save_player(): %s", sqlite3_errmsg(dbh));
return FALSE; return FALSE;
} }
if ((sqlite_code = sqlite3_bind_text(sth, 1, player->player_name, -1, SQLITE_STATIC)) != SQLITE_OK) if ((sqlite_code = sqlite3_bind_text(sth, 1, player->player_name, -1, SQLITE_STATIC)) != SQLITE_OK) {
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad parameter in wmud_db_save_player(): %s", sqlite3_errmsg(dbh)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad parameter in wmud_db_save_player(): %s", sqlite3_errmsg(dbh));
return FALSE; return FALSE;
} }
if ((sqlite_code = sqlite3_bind_text(sth, 2, player->email, -1, SQLITE_STATIC)) != SQLITE_OK) if ((sqlite_code = sqlite3_bind_text(sth, 2, player->email, -1, SQLITE_STATIC)) != SQLITE_OK) {
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad parameter in wmud_db_save_player(): %s", sqlite3_errmsg(dbh)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad parameter in wmud_db_save_player(): %s", sqlite3_errmsg(dbh));
return FALSE; return FALSE;
} }
if ((sqlite_code = sqlite3_step(sth)) != SQLITE_DONE) if ((sqlite_code = sqlite3_step(sth)) != SQLITE_DONE) {
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Statement cannot be executed in wmud_db_save_player(): %s", sqlite3_errmsg(dbh)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Statement cannot be executed in wmud_db_save_player(): %s", sqlite3_errmsg(dbh));
return FALSE; return FALSE;
@ -191,26 +178,22 @@ wmud_db_load_planes(GSList **planes, GError **err)
int sqlite_code; int sqlite_code;
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading planes"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading planes");
if (dbh == NULL) if (dbh == NULL) {
{
if (err) if (err)
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized"); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized");
return FALSE; return FALSE;
} }
if ((sqlite_code = sqlite3_prepare_v2(dbh, "SELECT id, name FROM planes", -1, &sth, NULL)) != SQLITE_OK) if ((sqlite_code = sqlite3_prepare_v2(dbh, "SELECT id, name FROM planes", -1, &sth, NULL)) != SQLITE_OK) {
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad query in wmud_db_load_planes(): %s", sqlite3_errmsg(dbh)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad query in wmud_db_load_planes(): %s", sqlite3_errmsg(dbh));
return FALSE; return FALSE;
} }
while (1) while (1) {
{
sqlite_code = sqlite3_step(sth); sqlite_code = sqlite3_step(sth);
if (sqlite_code == SQLITE_ROW) if (sqlite_code == SQLITE_ROW) {
{
wmudPlane *plane = g_new0(wmudPlane, 1); wmudPlane *plane = g_new0(wmudPlane, 1);
plane->id = sqlite3_column_int(sth, 0); plane->id = sqlite3_column_int(sth, 0);
plane->name = g_strdup((gchar *)sqlite3_column_text(sth, 1)); plane->name = g_strdup((gchar *)sqlite3_column_text(sth, 1));
@ -218,13 +201,9 @@ wmud_db_load_planes(GSList **planes, GError **err)
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded plane _%s_", plane->name); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded plane _%s_", plane->name);
*planes = g_slist_prepend(*planes, plane); *planes = g_slist_prepend(*planes, plane);
} } else if (sqlite_code == SQLITE_DONE) {
else if (sqlite_code == SQLITE_DONE)
{
break; break;
} } else {
else
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Query error in wmud_db_load_planes(): %s", sqlite3_errmsg(dbh)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Query error in wmud_db_load_planes(): %s", sqlite3_errmsg(dbh));
sqlite3_finalize(sth); sqlite3_finalize(sth);
return FALSE; return FALSE;
@ -243,26 +222,22 @@ wmud_db_load_planets(GSList **planets, GError **err)
int sqlite_code; int sqlite_code;
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading planets"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading planets");
if (dbh == NULL) if (dbh == NULL) {
{
if (err) if (err)
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized"); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized");
return FALSE; return FALSE;
} }
if ((sqlite_code = sqlite3_prepare_v2(dbh, "SELECT id, name FROM planets", -1, &sth, NULL)) != SQLITE_OK) if ((sqlite_code = sqlite3_prepare_v2(dbh, "SELECT id, name FROM planets", -1, &sth, NULL)) != SQLITE_OK) {
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad query in wmud_db_load_planets(): %s", sqlite3_errmsg(dbh)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad query in wmud_db_load_planets(): %s", sqlite3_errmsg(dbh));
return FALSE; return FALSE;
} }
while (1) while (1) {
{
sqlite_code = sqlite3_step(sth); sqlite_code = sqlite3_step(sth);
if (sqlite_code == SQLITE_ROW) if (sqlite_code == SQLITE_ROW) {
{
wmudPlanet *planet = g_new0(wmudPlanet, 1); wmudPlanet *planet = g_new0(wmudPlanet, 1);
planet->id = sqlite3_column_int(sth, 0); planet->id = sqlite3_column_int(sth, 0);
planet->name = g_strdup((gchar *)sqlite3_column_text(sth, 1)); planet->name = g_strdup((gchar *)sqlite3_column_text(sth, 1));
@ -270,15 +245,12 @@ wmud_db_load_planets(GSList **planets, GError **err)
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded planet _%s_", planet->name); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded planet _%s_", planet->name);
*planets = g_slist_prepend(*planets, planet); *planets = g_slist_prepend(*planets, planet);
} } else if (sqlite_code == SQLITE_DONE) {
else if (sqlite_code == SQLITE_DONE)
{
break; break;
} } else {
else
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Query error in wmud_db_load_planets(): %s", sqlite3_errmsg(dbh)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Query error in wmud_db_load_planets(): %s", sqlite3_errmsg(dbh));
sqlite3_finalize(sth); sqlite3_finalize(sth);
return FALSE; return FALSE;
} }
} }
@ -294,26 +266,22 @@ wmud_db_load_directions(GSList **directions, GError **err)
sqlite3_stmt *sth = NULL; sqlite3_stmt *sth = NULL;
int sqlite_code; int sqlite_code;
if (dbh == NULL) if (dbh == NULL) {
{
if (err) if (err)
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized"); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized");
return FALSE; return FALSE;
} }
if ((sqlite_code = sqlite3_prepare_v2(dbh, "SELECT id, short_name, name FROM directions", -1, &sth, NULL)) != SQLITE_OK) if ((sqlite_code = sqlite3_prepare_v2(dbh, "SELECT id, short_name, name FROM directions", -1, &sth, NULL)) != SQLITE_OK) {
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad query in wmud_db_load_directions(): %s", sqlite3_errmsg(dbh)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad query in wmud_db_load_directions(): %s", sqlite3_errmsg(dbh));
return FALSE; return FALSE;
} }
while (1) while (1) {
{
sqlite_code = sqlite3_step(sth); sqlite_code = sqlite3_step(sth);
if (sqlite_code == SQLITE_ROW) if (sqlite_code == SQLITE_ROW) {
{
wmudDirection *dir = g_new0(wmudDirection, 1); wmudDirection *dir = g_new0(wmudDirection, 1);
dir->id = sqlite3_column_int(sth, 0); dir->id = sqlite3_column_int(sth, 0);
dir->short_name = g_strdup((gchar *)sqlite3_column_text(sth, 1)); dir->short_name = g_strdup((gchar *)sqlite3_column_text(sth, 1));
@ -322,13 +290,9 @@ wmud_db_load_directions(GSList **directions, GError **err)
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded direction _%s_", dir->name); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded direction _%s_", dir->name);
*directions = g_slist_prepend(*directions, dir); *directions = g_slist_prepend(*directions, dir);
} } else if (sqlite_code == SQLITE_DONE) {
else if (sqlite_code == SQLITE_DONE)
{
break; break;
} } else {
else
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Query error in wmud_db_load_directions(): %s", sqlite3_errmsg(dbh)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Query error in wmud_db_load_directions(): %s", sqlite3_errmsg(dbh));
return FALSE; return FALSE;
} }
@ -346,26 +310,22 @@ wmud_db_load_areas(GSList **areas, GError **err)
int sqlite_code; int sqlite_code;
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading areas"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading areas");
if (dbh == NULL) if (dbh == NULL) {
{
if (err) if (err)
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized"); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized");
return FALSE; return FALSE;
} }
if ((sqlite_code = sqlite3_prepare_v2(dbh, "SELECT id, name FROM areas", -1, &sth, NULL)) != SQLITE_OK) if ((sqlite_code = sqlite3_prepare_v2(dbh, "SELECT id, name FROM areas", -1, &sth, NULL)) != SQLITE_OK) {
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad query in wmud_db_load_areas(): %s", sqlite3_errmsg(dbh)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad query in wmud_db_load_areas(): %s", sqlite3_errmsg(dbh));
return FALSE; return FALSE;
} }
while (1) while (1) {
{
sqlite_code = sqlite3_step(sth); sqlite_code = sqlite3_step(sth);
if (sqlite_code == SQLITE_ROW) if (sqlite_code == SQLITE_ROW) {
{
wmudArea *area = g_new0(wmudArea, 1); wmudArea *area = g_new0(wmudArea, 1);
area->id = sqlite3_column_int(sth, 0); area->id = sqlite3_column_int(sth, 0);
area->name = g_strdup((gchar *)sqlite3_column_text(sth, 1)); area->name = g_strdup((gchar *)sqlite3_column_text(sth, 1));
@ -373,13 +333,9 @@ wmud_db_load_areas(GSList **areas, GError **err)
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded area _%s_", area->name); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded area _%s_", area->name);
*areas = g_slist_prepend(*areas, area); *areas = g_slist_prepend(*areas, area);
} } else if (sqlite_code == SQLITE_DONE) {
else if (sqlite_code == SQLITE_DONE)
{
break; break;
} } else {
else
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Query error in wmud_db_load_areas(): %s", sqlite3_errmsg(dbh)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Query error in wmud_db_load_areas(): %s", sqlite3_errmsg(dbh));
sqlite3_finalize(sth); sqlite3_finalize(sth);
return FALSE; return FALSE;
@ -398,26 +354,22 @@ wmud_db_load_rooms(GSList **rooms, GError **err)
int sqlite_code; int sqlite_code;
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading rooms"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading rooms");
if (dbh == NULL) if (dbh == NULL) {
{
if (err) if (err)
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized"); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized");
return FALSE; return FALSE;
} }
if ((sqlite_code = sqlite3_prepare_v2(dbh, "SELECT id, area, name, distant_description, close_description FROM rooms", -1, &sth, NULL)) != SQLITE_OK) if ((sqlite_code = sqlite3_prepare_v2(dbh, "SELECT id, area, name, distant_description, close_description FROM rooms", -1, &sth, NULL)) != SQLITE_OK) {
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad query in wmud_db_load_rooms(): %s", sqlite3_errmsg(dbh)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad query in wmud_db_load_rooms(): %s", sqlite3_errmsg(dbh));
return FALSE; return FALSE;
} }
while (1) while (1) {
{
sqlite_code = sqlite3_step(sth); sqlite_code = sqlite3_step(sth);
if (sqlite_code == SQLITE_ROW) if (sqlite_code == SQLITE_ROW) {
{
wmudRoom *room = g_new0(wmudRoom, 1); wmudRoom *room = g_new0(wmudRoom, 1);
room->id = sqlite3_column_int(sth, 0); room->id = sqlite3_column_int(sth, 0);
room->area_id = sqlite3_column_int(sth, 1); room->area_id = sqlite3_column_int(sth, 1);
@ -428,13 +380,9 @@ wmud_db_load_rooms(GSList **rooms, GError **err)
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded room %d/_%s_", room->area_id, room->name); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded room %d/_%s_", room->area_id, room->name);
*rooms = g_slist_prepend(*rooms, room); *rooms = g_slist_prepend(*rooms, room);
} } else if (sqlite_code == SQLITE_DONE) {
else if (sqlite_code == SQLITE_DONE)
{
break; break;
} } else {
else
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Query error in wmud_db_load_rooms(): %s", sqlite3_errmsg(dbh)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Query error in wmud_db_load_rooms(): %s", sqlite3_errmsg(dbh));
sqlite3_finalize(sth); sqlite3_finalize(sth);
return FALSE; return FALSE;
@ -453,26 +401,22 @@ wmud_db_load_exits(GSList **exits, GError **err)
int sqlite_code; int sqlite_code;
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading rooms"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading rooms");
if (dbh == NULL) if (dbh == NULL) {
{
if (err) if (err)
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized"); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized");
return FALSE; return FALSE;
} }
if ((sqlite_code = sqlite3_prepare_v2(dbh, "SELECT room_id, direction, other_side FROM room_exits", -1, &sth, NULL)) != SQLITE_OK) if ((sqlite_code = sqlite3_prepare_v2(dbh, "SELECT room_id, direction, other_side FROM room_exits", -1, &sth, NULL)) != SQLITE_OK) {
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad query in wmud_db_load_exits(): %s", sqlite3_errmsg(dbh)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad query in wmud_db_load_exits(): %s", sqlite3_errmsg(dbh));
return FALSE; return FALSE;
} }
while (1) while (1) {
{
sqlite_code = sqlite3_step(sth); sqlite_code = sqlite3_step(sth);
if (sqlite_code == SQLITE_ROW) if (sqlite_code == SQLITE_ROW) {
{
wmudExit *room_exit = g_new0(wmudExit, 1); wmudExit *room_exit = g_new0(wmudExit, 1);
room_exit->source_room_id = sqlite3_column_int(sth, 0); room_exit->source_room_id = sqlite3_column_int(sth, 0);
room_exit->direction_id = sqlite3_column_int(sth, 1); room_exit->direction_id = sqlite3_column_int(sth, 1);
@ -481,15 +425,12 @@ wmud_db_load_exits(GSList **exits, GError **err)
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded exit %d =%d=> %d", room_exit->source_room_id, room_exit->direction_id, room_exit->destination_room_id); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded exit %d =%d=> %d", room_exit->source_room_id, room_exit->direction_id, room_exit->destination_room_id);
*exits = g_slist_prepend(*exits, room_exit); *exits = g_slist_prepend(*exits, room_exit);
} } else if (sqlite_code == SQLITE_DONE) {
else if (sqlite_code == SQLITE_DONE)
{
break; break;
} } else {
else
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Query error in wmud_db_load_exits(): %s", sqlite3_errmsg(dbh)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Query error in wmud_db_load_exits(): %s", sqlite3_errmsg(dbh));
sqlite3_finalize(sth); sqlite3_finalize(sth);
return FALSE; return FALSE;
} }
} }
@ -506,26 +447,23 @@ wmud_db_load_planet_planes(GSList **planet_planes, GError **err)
int sqlite_code; int sqlite_code;
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading rooms"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading rooms");
if (dbh == NULL) if (dbh == NULL) {
{
if (err) if (err)
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized"); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized");
return FALSE; return FALSE;
} }
if ((sqlite_code = sqlite3_prepare_v2(dbh, "SELECT planet_id, plane_id FROM planet_planes", -1, &sth, NULL)) != SQLITE_OK) if ((sqlite_code = sqlite3_prepare_v2(dbh, "SELECT planet_id, plane_id FROM planet_planes", -1, &sth, NULL)) != SQLITE_OK) {
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad query in wmud_db_load_planet_planes(): %s", sqlite3_errmsg(dbh)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad query in wmud_db_load_planet_planes(): %s", sqlite3_errmsg(dbh));
return FALSE; return FALSE;
} }
while (1) while (1) {
{
sqlite_code = sqlite3_step(sth); sqlite_code = sqlite3_step(sth);
if (sqlite_code == SQLITE_ROW)
{ if (sqlite_code == SQLITE_ROW) {
wmudPlanetPlaneAssoc *planet_plane = g_new0(wmudPlanetPlaneAssoc, 1); wmudPlanetPlaneAssoc *planet_plane = g_new0(wmudPlanetPlaneAssoc, 1);
planet_plane->planet_id = sqlite3_column_int(sth, 0); planet_plane->planet_id = sqlite3_column_int(sth, 0);
planet_plane->plane_id = sqlite3_column_int(sth, 1); planet_plane->plane_id = sqlite3_column_int(sth, 1);
@ -533,15 +471,12 @@ wmud_db_load_planet_planes(GSList **planet_planes, GError **err)
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded planet-plane association %d <> %d", planet_plane->planet_id, planet_plane->plane_id); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded planet-plane association %d <> %d", planet_plane->planet_id, planet_plane->plane_id);
*planet_planes = g_slist_prepend(*planet_planes, planet_plane); *planet_planes = g_slist_prepend(*planet_planes, planet_plane);
} } else if (sqlite_code == SQLITE_DONE) {
else if (sqlite_code == SQLITE_DONE)
{
break; break;
} } else {
else
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Query error in wmud_db_load_exits(): %s", sqlite3_errmsg(dbh)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Query error in wmud_db_load_exits(): %s", sqlite3_errmsg(dbh));
sqlite3_finalize(sth); sqlite3_finalize(sth);
return FALSE; return FALSE;
} }
} }
@ -558,26 +493,22 @@ wmud_db_load_menu(GSList **menu_items, GError **err)
int sqlite_code; int sqlite_code;
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading menu items"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading menu items");
if (dbh == NULL) if (dbh == NULL) {
{
if (err) if (err)
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized"); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized");
return FALSE; return FALSE;
} }
if ((sqlite_code = sqlite3_prepare_v2(dbh, "SELECT id, menuchar, need_active_char, placement, display_text, fnctn FROM menu ORDER BY placement", -1, &sth, NULL)) != SQLITE_OK) if ((sqlite_code = sqlite3_prepare_v2(dbh, "SELECT id, menuchar, need_active_char, placement, display_text, fnctn FROM menu ORDER BY placement", -1, &sth, NULL)) != SQLITE_OK) {
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad query in wmud_db_load_menu(): %s", sqlite3_errmsg(dbh)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad query in wmud_db_load_menu(): %s", sqlite3_errmsg(dbh));
return FALSE; return FALSE;
} }
while (1) while (1) {
{
sqlite_code = sqlite3_step(sth); sqlite_code = sqlite3_step(sth);
if (sqlite_code == SQLITE_ROW) if (sqlite_code == SQLITE_ROW) {
{
wmudMenu *menu_item = g_new0(wmudMenu, 1); wmudMenu *menu_item = g_new0(wmudMenu, 1);
menu_item->id = sqlite3_column_int(sth, 0); menu_item->id = sqlite3_column_int(sth, 0);
menu_item->menuchar = *(sqlite3_column_text(sth, 1)); menu_item->menuchar = *(sqlite3_column_text(sth, 1));
@ -589,15 +520,12 @@ wmud_db_load_menu(GSList **menu_items, GError **err)
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded menu item %d: %s", menu_item->id, menu_item->text); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded menu item %d: %s", menu_item->id, menu_item->text);
*menu_items = g_slist_prepend(*menu_items, menu_item); *menu_items = g_slist_prepend(*menu_items, menu_item);
} } else if (sqlite_code == SQLITE_DONE) {
else if (sqlite_code == SQLITE_DONE)
{
break; break;
} } else {
else
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Query error in wmud_db_load_menu_items(): %s", sqlite3_errmsg(dbh)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Query error in wmud_db_load_menu_items(): %s", sqlite3_errmsg(dbh));
sqlite3_finalize(sth); sqlite3_finalize(sth);
return FALSE; return FALSE;
} }
} }
@ -605,5 +533,4 @@ wmud_db_load_menu(GSList **menu_items, GError **err)
sqlite3_finalize(sth); sqlite3_finalize(sth);
return TRUE; return TRUE;
return FALSE;
} }

View File

@ -82,11 +82,13 @@ wmud_client_close(wmudClient *client, gboolean send_goodbye)
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Connection closed."); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Connection closed.");
g_socket_shutdown(client->socket, TRUE, TRUE, NULL); g_socket_shutdown(client->socket, TRUE, TRUE, NULL);
clients = g_slist_remove(clients, client); clients = g_slist_remove(clients, client);
if (client->player)
if (!client->player->registered) if (client->player && !client->player->registered)
wmud_player_free(&(client->player)); wmud_player_free(&(client->player));
if (client->buffer) if (client->buffer)
g_string_free(client->buffer, TRUE); g_string_free(client->buffer, TRUE);
g_source_destroy(client->socket_source); g_source_destroy(client->socket_source);
g_free(client); g_free(client);
} }
@ -114,19 +116,16 @@ wmud_client_callback(GSocket *client_socket, GIOCondition condition, wmudClient
{ {
GError *err = NULL; GError *err = NULL;
if (condition & G_IO_HUP) if (condition & G_IO_HUP) {
{
wmud_client_close(client, FALSE); wmud_client_close(client, FALSE);
return FALSE; return FALSE;
} } else if ((condition & G_IO_IN) || (condition & G_IO_PRI)) {
else if ((condition & G_IO_IN) || (condition & G_IO_PRI))
{
gssize len; gssize len;
gchar *buf2; gchar *buf2;
gchar *buf = g_malloc0(sizeof(gchar) * (MAX_RECV_LEN + 1)); gchar *buf = g_malloc0(sizeof(gchar) * (MAX_RECV_LEN + 1));
if ((len = g_socket_receive(client_socket, buf, MAX_RECV_LEN, NULL, &err)) == 0) if ((len = g_socket_receive(client_socket, buf, MAX_RECV_LEN, NULL, &err)) == 0) {
{
g_free(buf); g_free(buf);
wmud_client_close(client, FALSE); wmud_client_close(client, FALSE);
@ -134,26 +133,21 @@ wmud_client_callback(GSocket *client_socket, GIOCondition condition, wmudClient
} }
buf2 = buf; buf2 = buf;
while (TRUE) while (TRUE) {
{
char *r = strchr((char *)buf2, '\r'), char *r = strchr((char *)buf2, '\r'),
*n = strchr((char *)buf2, '\n'); *n = strchr((char *)buf2, '\n');
if (r || n) if (r || n) {
{
gint i, gint i,
sloc = -1; sloc = -1;
if ((r < n) && r) if ((r < n) && r) {
{
if (client->buffer->len > 0) if (client->buffer->len > 0)
g_string_append_len(client->buffer, buf2, (r - buf2)); g_string_append_len(client->buffer, buf2, (r - buf2));
else else
g_string_overwrite_len(client->buffer, 0, buf2, (r - buf2)); g_string_overwrite_len(client->buffer, 0, buf2, (r - buf2));
buf2 = r; buf2 = r;
} } else if (n) {
else if (n)
{
if (client->buffer->len > 0) if (client->buffer->len > 0)
g_string_append_len(client->buffer, buf2, (n - buf2)); g_string_append_len(client->buffer, buf2, (n - buf2));
else else
@ -162,28 +156,22 @@ wmud_client_callback(GSocket *client_socket, GIOCondition condition, wmudClient
} }
/* Remove telnet codes from the string */ /* Remove telnet codes from the string */
for (i = 0; i < client->buffer->len; i++) for (i = 0; i < client->buffer->len; i++) {
{
guchar c = (client->buffer->str)[i]; guchar c = (client->buffer->str)[i];
if ((c >= 240) || (c == 1)) if ((c >= 240) || (c == 1)) {
{
if (sloc == -1) if (sloc == -1)
sloc = i; sloc = i;
} } else {
else if (sloc != -1) {
{
if (sloc != -1)
{
g_string_erase(client->buffer, sloc, i - sloc); g_string_erase(client->buffer, sloc, i - sloc);
sloc = -1; sloc = -1;
} }
} }
} }
if (sloc != -1) if (sloc != -1)
{
g_string_erase(client->buffer, sloc, -1); g_string_erase(client->buffer, sloc, -1);
}
switch (client->state) switch (client->state)
{ {
@ -349,11 +337,10 @@ wmud_client_callback(GSocket *client_socket, GIOCondition condition, wmudClient
g_string_erase(client->buffer, 0, -1); g_string_erase(client->buffer, 0, -1);
for (; ((*buf2 == '\r') || (*buf2 == '\n')) && *buf2; buf2++); for (; ((*buf2 == '\r') || (*buf2 == '\n')) && *buf2; buf2++);
if (!*buf2) if (!*buf2)
break; break;
} } else {
else
{
if (client->buffer->len > 0) if (client->buffer->len > 0)
g_string_append(client->buffer, buf2); g_string_append(client->buffer, buf2);
else else
@ -405,8 +392,8 @@ game_source_callback(GSocket *socket, GIOCondition condition, struct AcceptData
g_source_attach(client_source, accept_data->context); g_source_attach(client_source, accept_data->context);
g_clear_error(&err); g_clear_error(&err);
if ((remote_addr = g_socket_get_remote_address(client_socket, &err)) != NULL)
{ if ((remote_addr = g_socket_get_remote_address(client_socket, &err)) != NULL) {
GInetAddress *addr; GInetAddress *addr;
gchar *ip_addr; gchar *ip_addr;
@ -416,18 +403,13 @@ game_source_callback(GSocket *socket, GIOCondition condition, struct AcceptData
g_free(ip_addr); g_free(ip_addr);
g_object_unref(addr); g_object_unref(addr);
g_object_unref(remote_addr); g_object_unref(remote_addr);
} } else {
else
{
if (err) if (err)
{
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "New game connection. The remote address is unknown. This is a bug. Message from upper level: %s", err->message); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "New game connection. The remote address is unknown. This is a bug. Message from upper level: %s", err->message);
}
else else
{
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "New game connection. The remote address is unknown. This is a bug."); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "New game connection. The remote address is unknown. This is a bug.");
}
} }
g_clear_error(&err); g_clear_error(&err);
wmud_client_send(client, "By what name shall we call you? "); wmud_client_send(client, "By what name shall we call you? ");
@ -452,10 +434,10 @@ wmud_networking_init(guint port_number, GMainContext *game_context, GSList *menu
GSocketListener *game_listener; GSocketListener *game_listener;
gboolean need_ipv4_socket = TRUE; gboolean need_ipv4_socket = TRUE;
GSocket *game_socket6, GSocket *game_socket6,
*game_socket4; *game_socket4;
GError *in_err = NULL; GError *in_err = NULL;
GSource *game_net_source4 = NULL, GSource *game_net_source4 = NULL,
*game_net_source6 = NULL; *game_net_source6 = NULL;
clients = NULL; clients = NULL;
game_listener = g_socket_listener_new(); game_listener = g_socket_listener_new();
@ -463,8 +445,7 @@ wmud_networking_init(guint port_number, GMainContext *game_context, GSList *menu
/* The following snippet is borrowed from GLib 2.30's gsocketlistener.c /* The following snippet is borrowed from GLib 2.30's gsocketlistener.c
* code, to create the necessary sockets to listen on both IPv4 and * code, to create the necessary sockets to listen on both IPv4 and
* IPv6 address */ * IPv6 address */
if ((game_socket6 = g_socket_new(G_SOCKET_FAMILY_IPV6, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, NULL)) != NULL) if ((game_socket6 = g_socket_new(G_SOCKET_FAMILY_IPV6, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, NULL)) != NULL) {
{
GInetAddress *inet_address; GInetAddress *inet_address;
GSocketAddress *address; GSocketAddress *address;
gboolean result; gboolean result;
@ -475,15 +456,16 @@ wmud_networking_init(guint port_number, GMainContext *game_context, GSList *menu
g_socket_set_listen_backlog(game_socket6, 10); g_socket_set_listen_backlog(game_socket6, 10);
result = g_socket_bind(game_socket6, address, TRUE, NULL) result =
&& g_socket_listen(game_socket6, NULL); g_socket_bind(game_socket6, address, TRUE, NULL)
&& g_socket_listen(game_socket6, NULL);
g_object_unref(address); g_object_unref(address);
if (!result) if (!result) {
{
g_object_unref(game_socket6); g_object_unref(game_socket6);
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Unable to create listener IPv6 socket"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Unable to create listener IPv6 socket");
return FALSE; return FALSE;
} }
@ -491,14 +473,14 @@ wmud_networking_init(guint port_number, GMainContext *game_context, GSList *menu
need_ipv4_socket = FALSE; need_ipv4_socket = FALSE;
game_net_source6 = g_socket_create_source(game_socket6, G_IO_IN, NULL); game_net_source6 = g_socket_create_source(game_socket6, G_IO_IN, NULL);
/* This function should never return error. If so, that would be a really big bug which will trigger a higher level problem for sure */
/* This function should never return error. If so, that would be a
* really big bug which will trigger a higher level problem for sure */
g_socket_listener_add_socket(game_listener, game_socket6, NULL, NULL); g_socket_listener_add_socket(game_listener, game_socket6, NULL, NULL);
} }
if (need_ipv4_socket) if (need_ipv4_socket) {
{ if ((game_socket4 = g_socket_new(G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, NULL)) != NULL) {
if ((game_socket4 = g_socket_new(G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, NULL)) != NULL)
{
GInetAddress *inet_address; GInetAddress *inet_address;
GSocketAddress *address; GSocketAddress *address;
gboolean result; gboolean result;
@ -514,22 +496,21 @@ wmud_networking_init(guint port_number, GMainContext *game_context, GSList *menu
g_object_unref(address); g_object_unref(address);
if (!result) if (!result) {
{
g_object_unref(game_socket4); g_object_unref(game_socket4);
if (!game_socket6) if (!game_socket6)
g_object_unref(game_socket6); g_object_unref(game_socket6);
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Unable to create listener IPv4 socket!\n"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Unable to create listener IPv4 socket!\n");
return FALSE; return FALSE;
} }
game_net_source4 = g_socket_create_source(game_socket4, G_IO_IN, NULL); game_net_source4 = g_socket_create_source(game_socket4, G_IO_IN, NULL);
g_socket_listener_add_socket(game_listener, game_socket4, NULL, NULL); g_socket_listener_add_socket(game_listener, game_socket4, NULL, NULL);
} }
} } else {
else
{
if (game_socket6 != NULL) if (game_socket6 != NULL)
g_clear_error(&in_err); g_clear_error(&in_err);
else else
@ -540,13 +521,12 @@ wmud_networking_init(guint port_number, GMainContext *game_context, GSList *menu
accept_data->listener = game_listener; accept_data->listener = game_listener;
accept_data->context = game_context; accept_data->context = game_context;
if (game_net_source6) if (game_net_source6) {
{
g_source_set_callback(game_net_source6, (GSourceFunc)game_source_callback, (gpointer)accept_data, NULL); g_source_set_callback(game_net_source6, (GSourceFunc)game_source_callback, (gpointer)accept_data, NULL);
g_source_attach(game_net_source6, game_context); g_source_attach(game_net_source6, game_context);
} }
if (game_net_source4)
{ if (game_net_source4) {
g_source_set_callback(game_net_source4, (GSourceFunc)game_source_callback, (gpointer)accept_data, NULL); g_source_set_callback(game_net_source4, (GSourceFunc)game_source_callback, (gpointer)accept_data, NULL);
g_source_attach(game_net_source4, game_context); g_source_attach(game_net_source4, game_context);
} }
@ -554,6 +534,7 @@ wmud_networking_init(guint port_number, GMainContext *game_context, GSList *menu
game_menu = menu_items; game_menu = menu_items;
email_regex = g_regex_new("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$", G_REGEX_CASELESS, 0, NULL); email_regex = g_regex_new("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$", G_REGEX_CASELESS, 0, NULL);
return TRUE; return TRUE;
} }
@ -583,12 +564,9 @@ wmud_client_send(wmudClient *client, const gchar *fmt, ...)
void void
wmud_client_quitanswer(wmudClient *client, gboolean answer) wmud_client_quitanswer(wmudClient *client, gboolean answer)
{ {
if (answer) if (answer) {
{
wmud_client_close(client, TRUE); wmud_client_close(client, TRUE);
} } else {
else
{
wmud_client_send(client, "Good boy!\r\n"); wmud_client_send(client, "Good boy!\r\n");
client->state = WMUD_CLIENT_STATE_MENU; client->state = WMUD_CLIENT_STATE_MENU;
} }
@ -597,14 +575,11 @@ wmud_client_quitanswer(wmudClient *client, gboolean answer)
void void
wmud_client_newchar_answer(wmudClient *client, gboolean answer) wmud_client_newchar_answer(wmudClient *client, gboolean answer)
{ {
if (answer) if (answer) {
{
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Creating new player\n"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Creating new player\n");
wmud_client_send(client, "Welcome to this MUD!\r\nPlease enter your e-mail address: "); wmud_client_send(client, "Welcome to this MUD!\r\nPlease enter your e-mail address: ");
client->state = WMUD_CLIENT_STATE_REGISTERING; client->state = WMUD_CLIENT_STATE_REGISTERING;
} } else {
else
{
wmud_client_send(client, "What is your player-name, then? "); wmud_client_send(client, "What is your player-name, then? ");
client->state = WMUD_CLIENT_STATE_FRESH; client->state = WMUD_CLIENT_STATE_FRESH;
} }

View File

@ -66,14 +66,12 @@ gboolean
rl_sec_elapsed(gpointer user_data) rl_sec_elapsed(gpointer user_data)
{ {
elapsed_seconds++; elapsed_seconds++;
if (elapsed_seconds == G_MAXUINT32) if (elapsed_seconds == G_MAXUINT32) {
{
elapsed_seconds = 0; elapsed_seconds = 0;
elapsed_cycle++; elapsed_cycle++;
} }
if (elapsed_seconds % 30 == 0) if (elapsed_seconds % 30 == 0) {
{
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "Heartbeat"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "Heartbeat");
} }
@ -116,7 +114,7 @@ wmud_game_init(GThread **game_thread, GMainContext **game_context)
g_source_unref(timeout_source); g_source_unref(timeout_source);
g_clear_error(&err); g_clear_error(&err);
#if GLIB_CHECK_VERSION(2,32,0) #if GLIB_CHECK_VERSION(2, 32, 0)
*game_thread = g_thread_new("game", (GThreadFunc)game_thread_func, game_loop); *game_thread = g_thread_new("game", (GThreadFunc)game_thread_func, game_loop);
#else #else
*game_thread = g_thread_create((GThreadFunc)game_thread_func, game_loop, TRUE, &err); *game_thread = g_thread_create((GThreadFunc)game_thread_func, game_loop, TRUE, &err);
@ -124,4 +122,3 @@ wmud_game_init(GThread **game_thread, GMainContext **game_context)
return TRUE; return TRUE;
} }

View File

@ -83,8 +83,7 @@ check_direction_dups2(wmudDirection *dir1, wmudDirection *dir2)
static void static void
check_direction_dups1(wmudDirection *dir, struct findData *find_data) check_direction_dups1(wmudDirection *dir, struct findData *find_data)
{ {
if (find_data->last != dir->name) if (find_data->last != dir->name) {
{
find_data->found = (find_data->found > 1) ? find_data->found : 0; find_data->found = (find_data->found > 1) ? find_data->found : 0;
find_data->last = dir->name; find_data->last = dir->name;
} }
@ -98,17 +97,16 @@ check_direction_command(wmudDirection *dir, gboolean *found)
{ {
wmudCommand *cmd; wmudCommand *cmd;
for (cmd = command_list; cmd->command; cmd++) for (cmd = command_list; cmd->command; cmd++) {
{ if (g_ascii_strcasecmp(dir->short_name, cmd->command) == 0) {
if (g_ascii_strcasecmp(dir->short_name, cmd->command) == 0)
{
*found = TRUE; *found = TRUE;
return; return;
} }
if (g_ascii_strcasecmp(dir->name, cmd->command) == 0) if (g_ascii_strcasecmp(dir->name, cmd->command) == 0) {
{
*found = TRUE; *found = TRUE;
return; return;
} }
} }
@ -131,16 +129,14 @@ wmud_interpreter_check_directions(GSList *directions, GError **err)
g_slist_foreach(directions, (GFunc)check_direction_command, &command_found); g_slist_foreach(directions, (GFunc)check_direction_command, &command_found);
if (command_found) if (command_found) {
{
g_set_error(err, WMUD_INTERPRETER_ERROR, WMUD_INTERPRETER_ERROR_DUPCMD, "Direction commands are not unique. Please check the database!"); g_set_error(err, WMUD_INTERPRETER_ERROR, WMUD_INTERPRETER_ERROR_DUPCMD, "Direction commands are not unique. Please check the database!");
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Direction command are not unique. Please check the database!"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Direction command are not unique. Please check the database!");
} }
g_slist_foreach(directions, (GFunc)check_direction_dups1, &find_data); g_slist_foreach(directions, (GFunc)check_direction_dups1, &find_data);
if (find_data.found > 1) if (find_data.found > 1) {
{
g_set_error(err, WMUD_INTERPRETER_ERROR, WMUD_INTERPRETER_ERROR_DUPCMD, "Direction commands defined in the database are not unique!"); g_set_error(err, WMUD_INTERPRETER_ERROR, WMUD_INTERPRETER_ERROR_DUPCMD, "Direction commands defined in the database are not unique!");
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Direction commands defined in the databsae are not unique."); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Direction commands defined in the databsae are not unique.");
@ -170,9 +166,10 @@ wmud_interpret_game_command(wmudClient *client)
match_count = 0; match_count = 0;
GSList *matches = NULL; GSList *matches = NULL;
if (strchr(client->buffer->str, '\r') || strchr(client->buffer->str, '\n')) if (strchr(client->buffer->str, '\r') || strchr(client->buffer->str, '\n')) {
{ /* We should NEVER reach this point! */
/* TODO: We should NEVER reach this point! */ g_assert_not_reached();
return; return;
} }
@ -180,44 +177,33 @@ wmud_interpret_game_command(wmudClient *client)
GString *token; GString *token;
while (*a) while (*a) {
{ for (start = a; *start; start++) {
for (start = a; *start; start++) if (!str_delim) {
{ if ((*start == '"') || (*start == '\'')) {
if (!str_delim)
{
if ((*start == '"') || (*start == '\''))
{
str_delim = *start; str_delim = *start;
start++; start++;
break; break;
} } else if (g_ascii_isspace(*start) || (!*start)) {
else if (g_ascii_isspace(*start) || (!*start))
{
break; break;
} }
} }
} }
for (end = start; *end; end++) for (end = start; *end; end++) {
{ if (!str_delim && strchr("'\" \t", *end)) {
if (!str_delim && strchr("'\" \t", *end))
{
break; break;
} } else if (str_delim && (*end == str_delim)) {
else if (str_delim && (*end == str_delim))
{
str_delim = 0; str_delim = 0;
break; break;
} } else if (!*end) {
else if (!*end)
{
break; break;
} }
} }
if (*start) if (*start) {
{
token = g_string_new_len(start, end - start); token = g_string_new_len(start, end - start);
command_parts = g_slist_prepend(command_parts, token); command_parts = g_slist_prepend(command_parts, token);
command_parts_count++; command_parts_count++;
@ -228,8 +214,7 @@ wmud_interpret_game_command(wmudClient *client)
a++; a++;
} }
if (str_delim) if (str_delim) {
{
wmud_client_send(client, "You should close quotes of any kind, like %c, shouldn't you?\r\n", str_delim); wmud_client_send(client, "You should close quotes of any kind, like %c, shouldn't you?\r\n", str_delim);
#if GLIB_CHECK_VERSION(2, 28, 0) #if GLIB_CHECK_VERSION(2, 28, 0)
g_slist_free_full(command_parts, (GDestroyNotify)destroy_string); g_slist_free_full(command_parts, (GDestroyNotify)destroy_string);
@ -240,29 +225,26 @@ wmud_interpret_game_command(wmudClient *client)
return; return;
} }
if (command_parts_count == 0) if (command_parts_count == 0) {
{
/* TODO: handle empty command */ /* TODO: handle empty command */
return; return;
} }
command_parts = g_slist_reverse(command_parts); command_parts = g_slist_reverse(command_parts);
for (cmd = command_list; cmd->command; cmd++) for (cmd = command_list; cmd->command; cmd++) {
{
GString *input = (GString *)(command_parts->data); GString *input = (GString *)(command_parts->data);
gint cmp; gint cmp;
if (((cmp = g_ascii_strncasecmp(input->str, cmd->command, input->len)) == 0) && !cmd->command[input->len]) if (((cmp = g_ascii_strncasecmp(input->str, cmd->command, input->len)) == 0) && !cmd->command[input->len]) {
{
g_slist_free(matches); g_slist_free(matches);
match_count = 1; match_count = 1;
matches = NULL; matches = NULL;
matches = g_slist_prepend(matches, cmd); matches = g_slist_prepend(matches, cmd);
break; break;
} } else if (cmp == 0) {
else if (cmp == 0)
{
matches = g_slist_prepend(matches, cmd); matches = g_slist_prepend(matches, cmd);
match_count++; match_count++;
} }
@ -305,4 +287,3 @@ WMUD_COMMAND(quit)
client->state = WMUD_CLIENT_STATE_YESNO; client->state = WMUD_CLIENT_STATE_YESNO;
client->yesNoCallback = wmud_client_quitanswer; client->yesNoCallback = wmud_client_quitanswer;
} }

View File

@ -68,8 +68,7 @@ wmud_random_string(gint len)
gchar *ret = g_malloc0(len + 1); gchar *ret = g_malloc0(len + 1);
gint i; gint i;
for (i = 0; i < len; i++) for (i = 0; i < len; i++) {
{
gchar c = 0; gchar c = 0;
/* Include only printable characters, but exclude $ because of /* Include only printable characters, but exclude $ because of
* salt generation, and space to avoid misunderstanding in the * salt generation, and space to avoid misunderstanding in the
@ -177,31 +176,21 @@ main(int argc, char **argv)
/* TODO: Command line parsing */ /* TODO: Command line parsing */
/* TODO: Create signal handlers! */ /* TODO: Create signal handlers! */
if (!wmud_config_init(&active_config, &err)) if (!wmud_config_init(&active_config, &err)) {
{
if (err) if (err)
{
g_critical("Config file parsing error: %s", err->message); g_critical("Config file parsing error: %s", err->message);
}
else else
{
g_critical("Config file parsing error!"); g_critical("Config file parsing error!");
}
return 1; return 1;
} }
g_clear_error(&err); g_clear_error(&err);
if (!wmud_db_init(&err)) if (!wmud_db_init(&err)) {
{
if (err) if (err)
{
g_critical("Database initialization error: %s", err->message); g_critical("Database initialization error: %s", err->message);
}
else else
{
g_critical("Database initialization error!"); g_critical("Database initialization error!");
}
return 1; return 1;
} }
@ -209,13 +198,9 @@ main(int argc, char **argv)
g_clear_error(&err); g_clear_error(&err);
wmud_db_load_players(&err); wmud_db_load_players(&err);
if (!wmud_world_load(&err)) if (!wmud_world_load(&err))
{
/* TODO: Send some kind of an error? */
return 1; return 1;
}
if (!wmud_menu_init(&game_menu)) if (!wmud_menu_init(&game_menu)) {
{
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "An error occured during menu loading."); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "An error occured during menu loading.");
return 1; return 1;
@ -228,16 +213,11 @@ main(int argc, char **argv)
wmud_game_init(&game_thread, &game_context); wmud_game_init(&game_thread, &game_context);
g_clear_error(&err); g_clear_error(&err);
if (!wmud_networking_init(active_config->port, game_context, game_menu, &err)) if (!wmud_networking_init(active_config->port, game_context, game_menu, &err)) {
{
if (err) if (err)
{
g_critical("Database initialization error: %s", err->message); g_critical("Database initialization error: %s", err->message);
}
else else
{
g_critical("Database initialization error: unknown error!"); g_critical("Database initialization error: unknown error!");
}
return 1; return 1;
} }

View File

@ -51,8 +51,7 @@
void void
wmud_maintenance_check_players(wmudPlayer *player, gpointer user_data) wmud_maintenance_check_players(wmudPlayer *player, gpointer user_data)
{ {
if (player->cpassword == NULL) if (player->cpassword == NULL) {
{
gchar *pw, gchar *pw,
*salt, *salt,
*cpw; *cpw;
@ -68,8 +67,8 @@ wmud_maintenance_check_players(wmudPlayer *player, gpointer user_data)
" password set", player->player_name); " password set", player->player_name);
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "New password will be %s", pw); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "New password will be %s", pw);
player->cpassword = cpw; player->cpassword = cpw;
/* TODO: Send e-mail about the new password. Upon completion, /* TODO: Send e-mail about the new password. Upon completion, set it in
* set it in the database */ * the database */
g_free(pw); g_free(pw);
g_free(salt); g_free(salt);
@ -130,14 +129,11 @@ wmud_smtp_read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
const char *data; const char *data;
if (size * nmemb < 1) if (size * nmemb < 1)
{
return 0; return 0;
}
data = text[pooh->counter]; data = text[pooh->counter];
if (data) if (data) {
{
size_t len = strlen(data); size_t len = strlen(data);
memcpy(ptr, data, len); memcpy(ptr, data, len);
pooh->counter++; pooh->counter++;
@ -161,26 +157,25 @@ wmud_maintenance_init(void)
curl_global_init(CURL_GLOBAL_DEFAULT); curl_global_init(CURL_GLOBAL_DEFAULT);
if (!(curl = curl_easy_init())) if (!(curl = curl_easy_init()))
{
g_error("Could not initialize the CURL library!"); g_error("Could not initialize the CURL library!");
}
if (!(mcurl = curl_multi_init())) if (!(mcurl = curl_multi_init()))
{
g_error("Could not initialize the CURL library!"); g_error("Could not initialize the CURL library!");
}
smtp_server_real = g_strconcat("smtp://", active_config->smtp_server, NULL); smtp_server_real = g_strconcat("smtp://", active_config->smtp_server, NULL);
curl_easy_setopt(curl, CURLOPT_URL, smtp_server_real); curl_easy_setopt(curl, CURLOPT_URL, smtp_server_real);
g_free(smtp_server_real); g_free(smtp_server_real);
if (active_config->smtp_username && active_config->smtp_password)
{ if (active_config->smtp_username && active_config->smtp_password) {
curl_easy_setopt(curl, CURLOPT_USERNAME, active_config->smtp_username); curl_easy_setopt(curl, CURLOPT_USERNAME, active_config->smtp_username);
curl_easy_setopt(curl, CURLOPT_PASSWORD, active_config->smtp_password); curl_easy_setopt(curl, CURLOPT_PASSWORD, active_config->smtp_password);
} }
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, active_config->smtp_sender); curl_easy_setopt(curl, CURLOPT_MAIL_FROM, active_config->smtp_sender);
curl_easy_setopt(curl, CURLOPT_USE_SSL, (CURLUSESSL_ALL && active_config->smtp_tls)); curl_easy_setopt(curl, CURLOPT_USE_SSL, (CURLUSESSL_ALL && active_config->smtp_tls));
/* TODO: Maybe these could go into the configuration as well */ /* TODO: Maybe these could go into the configuration as well */
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_SSLVERSION, 0L); curl_easy_setopt(curl, CURLOPT_SSLVERSION, 0L);
@ -204,10 +199,9 @@ wmud_maintenance_init(void)
g_source_attach(timeout_source, maint_context); g_source_attach(timeout_source, maint_context);
g_source_unref(timeout_source); g_source_unref(timeout_source);
#if GLIB_CHECK_VERSION(2,32,0) #if GLIB_CHECK_VERSION(2, 32, 0)
g_thread_new("maintenance", (GThreadFunc)maint_thread_func, maint_loop); g_thread_new("maintenance", (GThreadFunc)maint_thread_func, maint_loop);
#else #else
g_thread_create((GThreadFunc)maint_thread_func, maint_loop, TRUE, NULL); g_thread_create((GThreadFunc)maint_thread_func, maint_loop, TRUE, NULL);
#endif #endif
} }

View File

@ -48,6 +48,7 @@ wmud_menu_items_check(GSList *menu_items, GError **err)
/* TODO: Check for duplicate menuchars */ /* TODO: Check for duplicate menuchars */
/* TODO: Check for duplicate menu texts */ /* TODO: Check for duplicate menu texts */
/* TODO: Check for duplicate placements */ /* TODO: Check for duplicate placements */
return TRUE; return TRUE;
} }
@ -56,10 +57,13 @@ menu_item_free(wmudMenu *menu_item)
{ {
if (menu_item->text) if (menu_item->text)
g_free(menu_item->text); g_free(menu_item->text);
if (menu_item->display_text) if (menu_item->display_text)
g_free(menu_item->display_text); g_free(menu_item->display_text);
if (menu_item->display_text_ansi) if (menu_item->display_text_ansi)
g_free(menu_item->display_text_ansi); g_free(menu_item->display_text_ansi);
if (menu_item->func) if (menu_item->func)
g_free(menu_item->func); g_free(menu_item->func);
@ -77,6 +81,7 @@ wmud_menu_items_free(GSList **menu_items)
g_slist_foreach(*menu_items, (GFunc)menu_item_free, NULL); g_slist_foreach(*menu_items, (GFunc)menu_item_free, NULL);
g_slist_free(*menu_items); g_slist_free(*menu_items);
#endif #endif
*menu_items = NULL; *menu_items = NULL;
} }
} }
@ -93,14 +98,12 @@ menu_item_prepare(wmudMenu *item, GHashTable *cmdtable)
m1 = g_ascii_tolower(item->menuchar); m1 = g_ascii_tolower(item->menuchar);
m2 = g_ascii_toupper(item->menuchar); m2 = g_ascii_toupper(item->menuchar);
for (a = item->text; *a; a++) for (a = item->text; *a; a++)
if ((*a == m1) || (*a == m2)) if ((*a == m1) || (*a == m2)) {
{
found = a; found = a;
break; break;
} }
if (found) if (found) {
{
gchar *tmp; gchar *tmp;
tmp = g_ascii_strdown(item->text, -1); tmp = g_ascii_strdown(item->text, -1);
@ -110,9 +113,7 @@ menu_item_prepare(wmudMenu *item, GHashTable *cmdtable)
ds->str[found - item->text] = g_ascii_toupper(item->menuchar); ds->str[found - item->text] = g_ascii_toupper(item->menuchar);
dsa->str[found - item->text] = g_ascii_toupper(item->menuchar); dsa->str[found - item->text] = g_ascii_toupper(item->menuchar);
} } else {
else
{
found = item->text; found = item->text;
ds = g_string_new(item->text); ds = g_string_new(item->text);
dsa = g_string_new(item->text); dsa = g_string_new(item->text);
@ -130,7 +131,6 @@ menu_item_prepare(wmudMenu *item, GHashTable *cmdtable)
g_string_insert(dsa, found - item->text + 8, "\x1b[0m"); g_string_insert(dsa, found - item->text + 8, "\x1b[0m");
item->display_text = g_string_free(ds, FALSE); item->display_text = g_string_free(ds, FALSE);
item->display_text_ansi = g_string_free(dsa, FALSE); item->display_text_ansi = g_string_free(dsa, FALSE);
} }
WMUD_MENU_COMMAND(enter_world) WMUD_MENU_COMMAND(enter_world)
@ -199,23 +199,20 @@ wmud_menu_init(GSList **menu)
GError *in_err = NULL; GError *in_err = NULL;
GHashTable *cmdtable; GHashTable *cmdtable;
if (!wmud_db_load_menu(&menu_items, &in_err)) if (!wmud_db_load_menu(&menu_items, &in_err)) {
{
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Unable to load menu items from the database: %s", in_err->message); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Unable to load menu items from the database: %s", in_err->message);
wmud_menu_items_free(&menu_items); wmud_menu_items_free(&menu_items);
return FALSE; return FALSE;
} }
if (!menu_items) if (!menu_items) {
{
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "No menu items were found in the database!"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "No menu items were found in the database!");
return FALSE; return FALSE;
} }
if (!wmud_menu_items_check(menu_items, &in_err)) if (!wmud_menu_items_check(menu_items, &in_err)) {
{
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Menu items pre-flight check error: %s", in_err->message); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Menu items pre-flight check error: %s", in_err->message);
wmud_menu_items_free(&menu_items); wmud_menu_items_free(&menu_items);
@ -223,9 +220,8 @@ wmud_menu_init(GSList **menu)
} }
if (*menu) if (*menu)
{
wmud_menu_items_free(menu_items); wmud_menu_items_free(menu_items);
}
*menu = menu_items; *menu = menu_items;
cmdtable = g_hash_table_new(g_str_hash, g_str_equal); cmdtable = g_hash_table_new(g_str_hash, g_str_equal);
@ -257,9 +253,7 @@ static gint
find_by_menuchar(wmudMenu *item, gchar *menuchar) find_by_menuchar(wmudMenu *item, gchar *menuchar)
{ {
if (g_ascii_toupper(*menuchar) == g_ascii_toupper(item->menuchar)) if (g_ascii_toupper(*menuchar) == g_ascii_toupper(item->menuchar))
{
return 0; return 0;
}
return 1; return 1;
} }
@ -270,9 +264,7 @@ wmud_menu_get_command_by_menuchar(gchar menuchar, GSList *game_menu)
GSList *item; GSList *item;
if ((item = g_slist_find_custom(game_menu, &menuchar, (GCompareFunc)find_by_menuchar)) != NULL) if ((item = g_slist_find_custom(game_menu, &menuchar, (GCompareFunc)find_by_menuchar)) != NULL)
{
return ((wmudMenu *)(item->data))->func; return ((wmudMenu *)(item->data))->func;
}
return NULL; return NULL;
} }
@ -283,12 +275,7 @@ wmud_menu_execute_command(wmudClient *client, gchar *command)
wmudMenuCommandFunc func; wmudMenuCommandFunc func;
if ((func = g_hash_table_lookup(mcmd_table, command)) == NULL) if ((func = g_hash_table_lookup(mcmd_table, command)) == NULL)
{
wmud_client_send(client, "Unknown menu command.\r\n"); wmud_client_send(client, "Unknown menu command.\r\n");
}
else else
{
func(client); func(client);
}
} }

View File

@ -125,13 +125,16 @@ wmud_player_free(wmudPlayer **player)
{ {
if (!*player) if (!*player)
return; return;
if ((*player)->player_name) if ((*player)->player_name)
g_free((*player)->player_name); g_free((*player)->player_name);
if ((*player)->cpassword) if ((*player)->cpassword)
g_free((*player)->cpassword); g_free((*player)->cpassword);
if ((*player)->email) if ((*player)->email)
g_free((*player)->email); g_free((*player)->email);
g_free(*player); g_free(*player);
*player = NULL; *player = NULL;
} }

View File

@ -37,8 +37,7 @@ wmud_texts_init(void)
text_table = g_hash_table_new(g_str_hash, g_str_equal); text_table = g_hash_table_new(g_str_hash, g_str_equal);
for (i = 0; i < g_strv_length((gchar **)text_files); i++) for (i = 0; i < g_strv_length((gchar **)text_files); i++) {
{
GFile *tf; GFile *tf;
GFileInfo *tfi; GFileInfo *tfi;
GError *err = NULL; GError *err = NULL;
@ -56,12 +55,13 @@ wmud_texts_init(void)
contents = g_malloc0(tfs + 1); contents = g_malloc0(tfs + 1);
g_clear_error(&err); g_clear_error(&err);
if (!g_file_load_contents(tf, NULL, &contents, &length, NULL, &err))
{ if (!g_file_load_contents(tf, NULL, &contents, &length, NULL, &err)) {
g_object_unref(tfi); g_object_unref(tfi);
g_object_unref(tf); g_object_unref(tf);
continue; continue;
} }
g_hash_table_insert(text_table, (char *)text_files[i], contents); g_hash_table_insert(text_table, (char *)text_files[i], contents);
g_object_unref(tfi); g_object_unref(tfi);
@ -78,4 +78,3 @@ wmud_text_send_to_client(gchar *text_name, wmudClient *client)
gchar *text = g_hash_table_lookup(text_table, text_name); gchar *text = g_hash_table_lookup(text_table, text_name);
wmud_client_send(client, "%s\r\n", text); wmud_client_send(client, "%s\r\n", text);
} }

View File

@ -35,21 +35,25 @@ struct findData {
guint found; guint found;
gchar *last; gchar *last;
}; };
struct dirCheckData { struct dirCheckData {
GSList *directions; GSList *directions;
GSList *rooms; GSList *rooms;
gboolean sane; gboolean sane;
}; };
struct assocPlanetPlanes { struct assocPlanetPlanes {
GSList *planets; GSList *planets;
GSList *planes; GSList *planes;
gboolean bad_planet; gboolean bad_planet;
gboolean bad_plane; gboolean bad_plane;
}; };
struct assocRoomAreas { struct assocRoomAreas {
GSList *areas; GSList *areas;
gboolean found; gboolean found;
}; };
struct assocExitRooms { struct assocExitRooms {
GSList *rooms; GSList *rooms;
GSList *directions; GSList *directions;
@ -70,8 +74,7 @@ check_plane_dups2(wmudPlane *plane, gchar *name)
static void static void
check_plane_dups1(wmudPlane *plane, struct findData *find_data) check_plane_dups1(wmudPlane *plane, struct findData *find_data)
{ {
if (find_data->last != plane->name) if (find_data->last != plane->name) {
{
find_data->last = plane->name; find_data->last = plane->name;
find_data->found = (find_data->found > 1) ? find_data->found : 0; find_data->found = (find_data->found > 1) ? find_data->found : 0;
} }
@ -113,8 +116,7 @@ check_planet_dups2(wmudPlanet *planet, gchar *name)
static void static void
check_planet_dups1(wmudPlanet *planet, struct findData *find_data) check_planet_dups1(wmudPlanet *planet, struct findData *find_data)
{ {
if (find_data->last != planet->name) if (find_data->last != planet->name) {
{
find_data->last = planet->name; find_data->last = planet->name;
find_data->found = (find_data->found > 1) ? find_data->found : 0; find_data->found = (find_data->found > 1) ? find_data->found : 0;
} }
@ -156,8 +158,7 @@ check_area_dups2(wmudArea *area, gchar *name)
static void static void
check_area_dups1(wmudArea *area, struct findData *find_data) check_area_dups1(wmudArea *area, struct findData *find_data)
{ {
if (find_data->last != area->name) if (find_data->last != area->name) {
{
find_data->last = area->name; find_data->last = area->name;
find_data->found = (find_data->found > 1) ? find_data->found : 0; find_data->found = (find_data->found > 1) ? find_data->found : 0;
} }
@ -239,18 +240,13 @@ wmud_world_check_exits(GSList *exits, GSList *directions, GSList *rooms, GError
static gint static gint
check_room_dups2(wmudRoom *room1, wmudRoom *room2) check_room_dups2(wmudRoom *room1, wmudRoom *room2)
{ {
if (room1->area_id == room2->area_id) return (room1->area_id == room2->area_id) ? g_ascii_strcasecmp(room1->name, room2->name) : 1;
{
return g_ascii_strcasecmp(room1->name, room2->name);
}
return 1;
} }
static void static void
check_room_dups1(wmudRoom *room, struct findData *find_data) check_room_dups1(wmudRoom *room, struct findData *find_data)
{ {
if (find_data->last != (gchar *)room) if (find_data->last != (gchar *)room) {
{
find_data->last = (gchar *)room; find_data->last = (gchar *)room;
find_data->found = (find_data->found > 1) ? find_data->found : 0; find_data->found = (find_data->found > 1) ? find_data->found : 0;
} }
@ -307,15 +303,14 @@ planet_plane_assoc(wmudPlanetPlaneAssoc *association, struct assocPlanetPlanes *
GSList *planet, GSList *planet,
*plane; *plane;
if ((planet = g_slist_find_custom(assoc_data->planets, &(association->planet_id), (GCompareFunc)find_planet_by_id)) == NULL) if ((planet = g_slist_find_custom(assoc_data->planets, &(association->planet_id), (GCompareFunc)find_planet_by_id)) == NULL) {
{
g_debug("Planet: %d", association->planet_id); g_debug("Planet: %d", association->planet_id);
assoc_data->bad_planet = TRUE; assoc_data->bad_planet = TRUE;
return; return;
} }
if ((plane = g_slist_find_custom(assoc_data->planes, &(association->plane_id), (GCompareFunc)find_plane_by_id)) == NULL)
{ if ((plane = g_slist_find_custom(assoc_data->planes, &(association->plane_id), (GCompareFunc)find_plane_by_id)) == NULL) {
g_debug("Plane: %d", association->plane_id); g_debug("Plane: %d", association->plane_id);
assoc_data->bad_plane = TRUE; assoc_data->bad_plane = TRUE;
@ -354,15 +349,13 @@ wmud_world_assoc_planets_planes(GSList *planets, GSList *planes, GSList *planet_
g_slist_foreach(planet_planes, (GFunc)planet_plane_assoc, &planet_plane_assoc_data); g_slist_foreach(planet_planes, (GFunc)planet_plane_assoc, &planet_plane_assoc_data);
if (planet_plane_assoc_data.bad_planet || planet_plane_assoc_data.bad_plane) if (planet_plane_assoc_data.bad_planet || planet_plane_assoc_data.bad_plane) {
{
g_set_error(err, WMUD_WORLD_ERROR, WMUD_WORLD_ERROR_BADASSOC, "An illegal planet <-> plane association was found in the database!"); g_set_error(err, WMUD_WORLD_ERROR, WMUD_WORLD_ERROR_BADASSOC, "An illegal planet <-> plane association was found in the database!");
return FALSE; return FALSE;
} }
if (g_slist_find_custom(planets, NULL, (GCompareFunc)find_noplane_planet) != NULL) if (g_slist_find_custom(planets, NULL, (GCompareFunc)find_noplane_planet) != NULL) {
{
g_set_error(err, WMUD_WORLD_ERROR, WMUD_WORLD_ERROR_BADPLANET, "A planet with no planes associated was found in the database!"); g_set_error(err, WMUD_WORLD_ERROR, WMUD_WORLD_ERROR_BADPLANET, "A planet with no planes associated was found in the database!");
return FALSE; return FALSE;
@ -385,12 +378,9 @@ assoc_room_area(wmudRoom *room, struct assocRoomAreas *find_data)
{ {
GSList *area_item; GSList *area_item;
if ((area_item = g_slist_find_custom(find_data->areas, &(room->area_id), (GCompareFunc)find_area_by_id)) == NULL) if ((area_item = g_slist_find_custom(find_data->areas, &(room->area_id), (GCompareFunc)find_area_by_id)) == NULL) {
{
find_data->found = TRUE; find_data->found = TRUE;
} } else {
else
{
wmudArea *area = area_item->data; wmudArea *area = area_item->data;
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Associating room _%s_[%d] to area _%s_[%d]", room->name, room->id, area->name, area->id); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Associating room _%s_[%d] to area _%s_[%d]", room->name, room->id, area->name, area->id);
area->rooms = g_slist_append(area->rooms, room); area->rooms = g_slist_append(area->rooms, room);
@ -416,8 +406,7 @@ wmud_world_assoc_rooms_areas(GSList *rooms, GSList *areas, GError **err)
g_slist_foreach(rooms, (GFunc)assoc_room_area, &find_data); g_slist_foreach(rooms, (GFunc)assoc_room_area, &find_data);
if (find_data.found) if (find_data.found) {
{
g_set_error(err, WMUD_WORLD_ERROR, WMUD_WORLD_ERROR_BADASSOC, "Found a bad Area <-> Room association in the database!"); g_set_error(err, WMUD_WORLD_ERROR, WMUD_WORLD_ERROR_BADASSOC, "Found a bad Area <-> Room association in the database!");
return FALSE; return FALSE;
@ -441,10 +430,7 @@ assoc_room_planets(wmudRoom *room, GSList *planets)
gint gint
find_noplane_room(wmudRoom *room, gconstpointer notused) find_noplane_room(wmudRoom *room, gconstpointer notused)
{ {
if (room->planes == NULL) return (room->planes == NULL) ? 0 : 1;
return 0;
return 1;
} }
/** /**
@ -511,6 +497,7 @@ void
wmud_world_assoc_exits_rooms(GSList *exits, GSList *directions, GSList *rooms, GError **err) wmud_world_assoc_exits_rooms(GSList *exits, GSList *directions, GSList *rooms, GError **err)
{ {
struct assocExitRooms assoc_data = {rooms, directions}; struct assocExitRooms assoc_data = {rooms, directions};
g_slist_foreach(exits, (GFunc)assoc_room_exit, &assoc_data); g_slist_foreach(exits, (GFunc)assoc_room_exit, &assoc_data);
} }
@ -519,8 +506,10 @@ free_direction(wmudDirection *dir)
{ {
if (dir->short_name) if (dir->short_name)
g_free(dir->short_name); g_free(dir->short_name);
if (dir->name) if (dir->name)
g_free(dir->name); g_free(dir->name);
g_free(dir); g_free(dir);
} }
@ -529,8 +518,7 @@ wmud_world_free_directions(GSList *directions)
{ {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Freeing direction list"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Freeing direction list");
if (directions) if (directions) {
{
#if GLIB_CHECK_VERSION(2, 28, 0) #if GLIB_CHECK_VERSION(2, 28, 0)
g_slist_free_full(directions, (GDestroyNotify)free_direction); g_slist_free_full(directions, (GDestroyNotify)free_direction);
#else #else
@ -554,8 +542,7 @@ wmud_world_free_planes(GSList *planes)
{ {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Freeing planes list"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Freeing planes list");
if (planes) if (planes) {
{
#if GLIB_CHECK_VERSION(2, 28, 0) #if GLIB_CHECK_VERSION(2, 28, 0)
g_slist_free_full(planes, (GDestroyNotify)free_plane); g_slist_free_full(planes, (GDestroyNotify)free_plane);
#else #else
@ -579,8 +566,7 @@ wmud_world_free_planets(GSList *planets)
{ {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Freeing planets list"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Freeing planets list");
if (planets) if (planets) {
{
#if GLIB_CHECK_VERSION(2, 28, 0) #if GLIB_CHECK_VERSION(2, 28, 0)
g_slist_free_full(planets, (GDestroyNotify)free_planet); g_slist_free_full(planets, (GDestroyNotify)free_planet);
#else #else
@ -604,8 +590,7 @@ wmud_world_free_areas(GSList *areas)
{ {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Freeing areas list"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Freeing areas list");
if (areas) if (areas) {
{
#if GLIB_CHECK_VERSION(2, 28, 0) #if GLIB_CHECK_VERSION(2, 28, 0)
g_slist_free_full(areas, (GDestroyNotify)free_area); g_slist_free_full(areas, (GDestroyNotify)free_area);
#else #else
@ -620,8 +605,10 @@ free_room(wmudRoom *room)
{ {
if (room->name) if (room->name)
g_free(room->name); g_free(room->name);
if (room->distant_description) if (room->distant_description)
g_free(room->distant_description); g_free(room->distant_description);
if (room->close_description) if (room->close_description)
g_free(room->close_description); g_free(room->close_description);
@ -633,8 +620,7 @@ wmud_world_free_rooms(GSList *rooms)
{ {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Freeing rooms list"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Freeing rooms list");
if (rooms) if (rooms) {
{
#if GLIB_CHECK_VERSION(2, 28, 0) #if GLIB_CHECK_VERSION(2, 28, 0)
g_slist_free_full(rooms, (GDestroyNotify)free_room); g_slist_free_full(rooms, (GDestroyNotify)free_room);
#else #else
@ -649,8 +635,7 @@ wmud_world_free_exits(GSList *exits)
{ {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Freeing exits list"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Freeing exits list");
if (exits) if (exits) {
{
#if GLIB_CHECK_VERSION(2, 28, 0) #if GLIB_CHECK_VERSION(2, 28, 0)
g_slist_free_full(exits, (GDestroyNotify)g_free); g_slist_free_full(exits, (GDestroyNotify)g_free);
#else #else
@ -665,8 +650,7 @@ wmud_world_free_planet_planes(GSList *planet_planes)
{ {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Freeing planet <-> plane associations list"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Freeing planet <-> plane associations list");
if (planet_planes) if (planet_planes) {
{
#if GLIB_CHECK_VERSION(2, 28, 0) #if GLIB_CHECK_VERSION(2, 28, 0)
g_slist_free_full(planet_planes, (GDestroyNotify)g_free); g_slist_free_full(planet_planes, (GDestroyNotify)g_free);
#else #else
@ -695,24 +679,24 @@ wmud_world_load(GError **err)
GError *in_err = NULL; GError *in_err = NULL;
/* Load directions from the database and check them */ /* Load directions from the database and check them */
if (!wmud_db_load_directions(&directions, &in_err)) if (!wmud_db_load_directions(&directions, &in_err)) {
{
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Could not load directions from database: %s", in_err->message); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Could not load directions from database: %s", in_err->message);
wmud_world_free_directions(directions); wmud_world_free_directions(directions);
g_clear_error(&in_err); g_clear_error(&in_err);
return FALSE; return FALSE;
} }
if (!directions)
{ if (!directions) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "No directions were found in the database!"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "No directions were found in the database!");
g_clear_error(&in_err); g_clear_error(&in_err);
return FALSE; return FALSE;
} }
g_clear_error(&in_err); g_clear_error(&in_err);
if (!wmud_interpreter_check_directions(directions, &in_err))
{ if (!wmud_interpreter_check_directions(directions, &in_err)) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Direction list pre-flight check error: %s", in_err->message); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Direction list pre-flight check error: %s", in_err->message);
wmud_world_free_directions(directions); wmud_world_free_directions(directions);
g_clear_error(&in_err); g_clear_error(&in_err);
@ -722,8 +706,8 @@ wmud_world_load(GError **err)
/* Load planes from the database and check them */ /* Load planes from the database and check them */
g_clear_error(&in_err); g_clear_error(&in_err);
if (!wmud_db_load_planes(&planes, &in_err))
{ if (!wmud_db_load_planes(&planes, &in_err)) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Could not load planes from database: %s", in_err->message); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Could not load planes from database: %s", in_err->message);
wmud_world_free_planes(planes); wmud_world_free_planes(planes);
wmud_world_free_directions(directions); wmud_world_free_directions(directions);
@ -731,17 +715,18 @@ wmud_world_load(GError **err)
return FALSE; return FALSE;
} }
if (!planes)
{ if (!planes) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "No planes were found in the database!"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "No planes were found in the database!");
wmud_world_free_directions(directions); wmud_world_free_directions(directions);
g_clear_error(&in_err); g_clear_error(&in_err);
return FALSE; return FALSE;
} }
g_clear_error(&in_err); g_clear_error(&in_err);
if (!wmud_world_check_planes(planes, &in_err))
{ if (!wmud_world_check_planes(planes, &in_err)) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Plane list pre-flight check error: %s", in_err->message); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Plane list pre-flight check error: %s", in_err->message);
wmud_world_free_planes(planes); wmud_world_free_planes(planes);
wmud_world_free_directions(directions); wmud_world_free_directions(directions);
@ -752,8 +737,8 @@ wmud_world_load(GError **err)
/* Load planets from the database and check them */ /* Load planets from the database and check them */
g_clear_error(&in_err); g_clear_error(&in_err);
if (!wmud_db_load_planets(&planets, &in_err))
{ if (!wmud_db_load_planets(&planets, &in_err)) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Could not load planets from database: %s", in_err->message); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Could not load planets from database: %s", in_err->message);
wmud_world_free_planets(planets); wmud_world_free_planets(planets);
wmud_world_free_planes(planes); wmud_world_free_planes(planes);
@ -762,8 +747,8 @@ wmud_world_load(GError **err)
return FALSE; return FALSE;
} }
if (!planets)
{ if (!planets) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "No planets were found in the database!"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "No planets were found in the database!");
wmud_world_free_planes(planes); wmud_world_free_planes(planes);
wmud_world_free_directions(directions); wmud_world_free_directions(directions);
@ -771,9 +756,10 @@ wmud_world_load(GError **err)
return FALSE; return FALSE;
} }
g_clear_error(&in_err); g_clear_error(&in_err);
if (!wmud_world_check_planets(planets, &in_err))
{ if (!wmud_world_check_planets(planets, &in_err)) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Planet list pre-flight check error: %s", in_err->message); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Planet list pre-flight check error: %s", in_err->message);
wmud_world_free_planets(planets); wmud_world_free_planets(planets);
wmud_world_free_planes(planes); wmud_world_free_planes(planes);
@ -785,8 +771,8 @@ wmud_world_load(GError **err)
/* Load areas from the database and check them */ /* Load areas from the database and check them */
g_clear_error(&in_err); g_clear_error(&in_err);
if (!wmud_db_load_areas(&areas, &in_err))
{ if (!wmud_db_load_areas(&areas, &in_err)) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Could not load areas from database: %s", in_err->message); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Could not load areas from database: %s", in_err->message);
wmud_world_free_areas(areas); wmud_world_free_areas(areas);
wmud_world_free_planets(planets); wmud_world_free_planets(planets);
@ -796,8 +782,8 @@ wmud_world_load(GError **err)
return FALSE; return FALSE;
} }
if (!areas)
{ if (!areas) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "No areas were found in the database!"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "No areas were found in the database!");
wmud_world_free_planets(planets); wmud_world_free_planets(planets);
wmud_world_free_planes(planes); wmud_world_free_planes(planes);
@ -806,9 +792,10 @@ wmud_world_load(GError **err)
return FALSE; return FALSE;
} }
g_clear_error(&in_err); g_clear_error(&in_err);
if (!wmud_world_check_areas(areas, &in_err))
{ if (!wmud_world_check_areas(areas, &in_err)) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Area list pre-flight check error: %s", in_err->message); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Area list pre-flight check error: %s", in_err->message);
wmud_world_free_areas(areas); wmud_world_free_areas(areas);
wmud_world_free_planets(planets); wmud_world_free_planets(planets);
@ -821,8 +808,8 @@ wmud_world_load(GError **err)
/* Load rooms from the database and check them */ /* Load rooms from the database and check them */
g_clear_error(&in_err); g_clear_error(&in_err);
if (!wmud_db_load_rooms(&rooms, &in_err))
{ if (!wmud_db_load_rooms(&rooms, &in_err)) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Could not load areas from database: %s", in_err->message); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Could not load areas from database: %s", in_err->message);
wmud_world_free_rooms(rooms); wmud_world_free_rooms(rooms);
wmud_world_free_areas(areas); wmud_world_free_areas(areas);
@ -833,8 +820,8 @@ wmud_world_load(GError **err)
return FALSE; return FALSE;
} }
if (!rooms)
{ if (!rooms) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "No rooms were found in the database!"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "No rooms were found in the database!");
wmud_world_free_areas(areas); wmud_world_free_areas(areas);
wmud_world_free_planets(planets); wmud_world_free_planets(planets);
@ -844,9 +831,10 @@ wmud_world_load(GError **err)
return FALSE; return FALSE;
} }
g_clear_error(&in_err); g_clear_error(&in_err);
if (!wmud_world_check_rooms(rooms, &in_err))
{ if (!wmud_world_check_rooms(rooms, &in_err)) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Room list pre-flight check error: %s", in_err->message); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Room list pre-flight check error: %s", in_err->message);
wmud_world_free_rooms(rooms); wmud_world_free_rooms(rooms);
wmud_world_free_areas(areas); wmud_world_free_areas(areas);
@ -860,8 +848,8 @@ wmud_world_load(GError **err)
/* Load room exits from the database and check them */ /* Load room exits from the database and check them */
g_clear_error(&in_err); g_clear_error(&in_err);
if (!wmud_db_load_exits(&exits, &in_err))
{ if (!wmud_db_load_exits(&exits, &in_err)) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Could not load exits from database: %s", in_err->message); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Could not load exits from database: %s", in_err->message);
wmud_world_free_exits(exits); wmud_world_free_exits(exits);
wmud_world_free_rooms(rooms); wmud_world_free_rooms(rooms);
@ -873,8 +861,8 @@ wmud_world_load(GError **err)
return FALSE; return FALSE;
} }
if (!exits)
{ if (!exits) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "No exits were found in the database!"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "No exits were found in the database!");
wmud_world_free_rooms(rooms); wmud_world_free_rooms(rooms);
wmud_world_free_areas(areas); wmud_world_free_areas(areas);
@ -885,9 +873,10 @@ wmud_world_load(GError **err)
return FALSE; return FALSE;
} }
g_clear_error(&in_err); g_clear_error(&in_err);
if (!wmud_world_check_exits(exits, directions, rooms, &in_err))
{ if (!wmud_world_check_exits(exits, directions, rooms, &in_err)) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Exit list pre-flight check error: %s", in_err->message); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Exit list pre-flight check error: %s", in_err->message);
wmud_world_free_rooms(rooms); wmud_world_free_rooms(rooms);
wmud_world_free_areas(areas); wmud_world_free_areas(areas);
@ -900,8 +889,7 @@ wmud_world_load(GError **err)
} }
/* Load planet-plane associations from the database */ /* Load planet-plane associations from the database */
if (!wmud_db_load_planet_planes(&planet_planes, &in_err)) if (!wmud_db_load_planet_planes(&planet_planes, &in_err)) {
{
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Could not load exits from database: %s", in_err->message); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Could not load exits from database: %s", in_err->message);
wmud_world_free_planet_planes(planet_planes); wmud_world_free_planet_planes(planet_planes);
wmud_world_free_exits(exits); wmud_world_free_exits(exits);
@ -914,8 +902,8 @@ wmud_world_load(GError **err)
return FALSE; return FALSE;
} }
if (!planet_planes)
{ if (!planet_planes) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "No planet-plane associations were found in the database!"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "No planet-plane associations were found in the database!");
wmud_world_free_exits(exits); wmud_world_free_exits(exits);
wmud_world_free_rooms(rooms); wmud_world_free_rooms(rooms);
@ -927,14 +915,15 @@ wmud_world_load(GError **err)
return FALSE; return FALSE;
} }
g_clear_error(&in_err); g_clear_error(&in_err);
/* World loading finished. Now let's tie the parts together... */ /* World loading finished. Now let's tie the parts together... */
/* Put the planets on the planes */ /* Put the planets on the planes */
g_clear_error(&in_err); g_clear_error(&in_err);
if (!wmud_world_assoc_planets_planes(planets, planes, planet_planes, &in_err))
{ if (!wmud_world_assoc_planets_planes(planets, planes, planet_planes, &in_err)) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Planets <-> Planes association error: %s", in_err->message); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Planets <-> Planes association error: %s", in_err->message);
wmud_world_free_planet_planes(planet_planes); wmud_world_free_planet_planes(planet_planes);
wmud_world_free_exits(exits); wmud_world_free_exits(exits);
@ -949,8 +938,7 @@ wmud_world_load(GError **err)
/* Generate the areas */ /* Generate the areas */
g_clear_error(&in_err); g_clear_error(&in_err);
if (!wmud_world_assoc_rooms_areas(rooms, areas, &in_err)) if (!wmud_world_assoc_rooms_areas(rooms, areas, &in_err)) {
{
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Rooms <-> Areas association error: %s", in_err->message); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Rooms <-> Areas association error: %s", in_err->message);
wmud_world_free_planet_planes(planet_planes); wmud_world_free_planet_planes(planet_planes);
wmud_world_free_exits(exits); wmud_world_free_exits(exits);
@ -965,8 +953,8 @@ wmud_world_load(GError **err)
/* Teleport the previously built areas to the planets */ /* Teleport the previously built areas to the planets */
g_clear_error(&in_err); g_clear_error(&in_err);
if (!wmud_world_assoc_rooms_planets(rooms, planets, &in_err))
{ if (!wmud_world_assoc_rooms_planets(rooms, planets, &in_err)) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Rooms <-> Planets association error: %s", in_err->message); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Rooms <-> Planets association error: %s", in_err->message);
wmud_world_free_planet_planes(planet_planes); wmud_world_free_planet_planes(planet_planes);
wmud_world_free_exits(exits); wmud_world_free_exits(exits);
@ -992,4 +980,3 @@ wmud_world_load(GError **err)
return TRUE; return TRUE;
} }