From f3ab98158021e20f9d6ffdafd6029487a69efd44 Mon Sep 17 00:00:00 2001 From: "Gergely Polonkai (W00d5t0ck)" Date: Fri, 18 May 2012 15:55:02 +0200 Subject: [PATCH] Added a static text and template handling bundle skeleton Signed-off-by: Gergely Polonkai (W00d5t0ck) --- data/texts/motd | 1 + wmud/Makefile.am | 2 +- wmud/game-networking.c | 1 + wmud/main.c | 2 ++ wmud/texts.c | 81 ++++++++++++++++++++++++++++++++++++++++++ wmud/texts.h | 27 ++++++++++++++ 6 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 data/texts/motd create mode 100644 wmud/texts.c create mode 100644 wmud/texts.h diff --git a/data/texts/motd b/data/texts/motd new file mode 100644 index 0000000..99c3e6c --- /dev/null +++ b/data/texts/motd @@ -0,0 +1 @@ +Hello, and welcome to Iminiru! diff --git a/wmud/Makefile.am b/wmud/Makefile.am index 8320055..1d50198 100644 --- a/wmud/Makefile.am +++ b/wmud/Makefile.am @@ -1,5 +1,5 @@ bin_PROGRAMS = wmud AM_CPPFLAGS = -DWMUD_STATEDIR=\""$(localstatedir)"\" -DWMUD_CONFDIR=\""$(sysconfdir)"\" $(MEMCACHED_CFLAGS) $(GLIB_CFLAGS) $(GIO_CFLAGS) $(GTHREAD_CFLAGS) $(SQLITE3_CFLAGS) $(CURL_CFLAGS) -wmud_SOURCES = main.c game-networking.c interpreter.c db.c players.c maintenance.c game.c configuration.c world.c menu.c +wmud_SOURCES = main.c game-networking.c interpreter.c db.c players.c maintenance.c game.c configuration.c world.c menu.c texts.c wmud_LDADD = $(MEMCACHED_LIBS) $(GLIB_LIBS) $(GIO_LIBS) $(GTHREAD_LIBS) $(SQLITE3_LIBS) $(CURL_LIBS) diff --git a/wmud/game-networking.c b/wmud/game-networking.c index 4ceb255..be2304b 100644 --- a/wmud/game-networking.c +++ b/wmud/game-networking.c @@ -231,6 +231,7 @@ wmud_client_callback(GSocket *client_socket, GIOCondition condition, wmudClient wmud_client_send(client, "There %s %d failed login attempt%s with your account since your last visit\r\n", (client->player->fail_count == 1) ? "was" : "were", client->player->fail_count, (client->player->fail_count == 1) ? "" : "s"); } /* TODO: send MOTD */ + wmud_text_send_to_client("motd", client); /* TODO: send menu items */ g_slist_foreach(game_menu, (GFunc)send_menu_item, client); client->state = WMUD_CLIENT_STATE_MENU; diff --git a/wmud/main.c b/wmud/main.c index c7ead53..fe4dc2b 100644 --- a/wmud/main.c +++ b/wmud/main.c @@ -221,6 +221,8 @@ main(int argc, char **argv) return 1; } + wmud_texts_init(); + /* Non-thread initialization ends here */ wmud_game_init(&game_thread, &game_context); diff --git a/wmud/texts.c b/wmud/texts.c new file mode 100644 index 0000000..f20fc59 --- /dev/null +++ b/wmud/texts.c @@ -0,0 +1,81 @@ +/* wMUD - Yet another MUD codebase by W00d5t0ck + * Copyright (C) 2012 - Gergely POLONKAI + * + * texts.c: static text and template handling functions + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include + +#include "wmud-types.h" +#include "game-networking.h" +#include "texts.h" + +static const gchar *text_files[] = { + "motd", + NULL +}; +GHashTable *text_table = NULL; + +void +wmud_texts_init(void) +{ + int i; + gchar *texts_dir = g_strconcat(WMUD_STATEDIR, "/texts/", NULL); + + text_table = g_hash_table_new(g_str_hash, g_str_equal); + + for (i = 0; i < g_strv_length((gchar **)text_files); i++) + { + GFile *tf; + GFileInfo *tfi; + GError *err = NULL; + guint64 tfs; + gchar *contents; + gsize length; + + gchar *text_file = g_strconcat(texts_dir, text_files[i], NULL); + g_debug("Loading text file %s from %s", text_files[i], text_file); + tf = g_file_new_for_path(text_file); + tfi = g_file_query_info(tf, G_FILE_ATTRIBUTE_STANDARD_SIZE, G_FILE_QUERY_INFO_NONE, NULL, &err); + /* TODO: Error checking! */ + tfs = g_file_info_get_attribute_uint64(tfi, G_FILE_ATTRIBUTE_STANDARD_SIZE); + + contents = g_malloc0(tfs + 1); + + g_clear_error(&err); + if (!g_file_load_contents(tf, NULL, &contents, &length, NULL, &err)) + { + g_object_unref(tfi); + g_object_unref(tf); + continue; + } + g_hash_table_insert(text_table, (char *)text_files[i], contents); + + g_object_unref(tfi); + g_object_unref(tf); + g_free(text_file); + } + + g_free(texts_dir); +} + +void +wmud_text_send_to_client(gchar *text_name, wmudClient *client) +{ + gchar *text = g_hash_table_lookup(text_table, text_name); + wmud_client_send(client, "%s\r\n", text); +} + diff --git a/wmud/texts.h b/wmud/texts.h new file mode 100644 index 0000000..d06bd31 --- /dev/null +++ b/wmud/texts.h @@ -0,0 +1,27 @@ +/* wMUD - Yet another MUD codebase by W00d5t0ck + * Copyright (C) 2012 - Gergely POLONKAI + * + * texts.h: static text and template handling functions + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef __WMUD_TEXTS_H__ +#define __WMUD_TEXTS_H__ + +#include "wmud-types.h" + +void wmud_texts_init(void); +void wmud_text_send_to_client(gchar *text, wmudClient *client); + +#endif /* __WMUD_TEXTS_H__ */