From 166ef16dd188758f9622ce772a68c78737768bbb Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Sun, 16 Oct 2011 11:46:38 -0700 Subject: resource: Add a resource for files --- libgame/Makefile.am | 2 + libgame/game-file-resource.c | 176 +++++++++++++++++++++++++++++++++++++++++++ libgame/game-file-resource.h | 56 ++++++++++++++ libgame/libgame.h | 1 + 4 files changed, 235 insertions(+) create mode 100644 libgame/game-file-resource.c create mode 100644 libgame/game-file-resource.h diff --git a/libgame/Makefile.am b/libgame/Makefile.am index a1fa703..fe88c51 100644 --- a/libgame/Makefile.am +++ b/libgame/Makefile.am @@ -22,6 +22,7 @@ libgame_@LIBGAME_VERS@_la_SOURCES = \ game-colored.c \ game-container.c \ game-data-resource.c \ + game-file-resource.c \ game-filter.c \ game-game.c \ game-geom-basics.c \ @@ -60,6 +61,7 @@ basic_headers = \ game-colored.h \ game-container.h \ game-data-resource.h \ + game-file-resource.h \ game-filter.h \ game-game.h \ game-geom-basics.h \ diff --git a/libgame/game-file-resource.c b/libgame/game-file-resource.c new file mode 100644 index 0000000..0f200cc --- /dev/null +++ b/libgame/game-file-resource.c @@ -0,0 +1,176 @@ +/* + * Copyright (C) 2011 Benjamin Otte + * + * 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, 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "game-private.h" + +#include "game-file-resource.h" + +#include "game-portable.h" + +enum { + PROP_0, + PROP_FILENAME +}; + +G_DEFINE_TYPE (GameFileResource, game_file_resource, GAME_TYPE_DATA_RESOURCE) + +static void +game_file_resource_get_property (GObject *object, guint param_id, GValue *value, + GParamSpec * pspec) +{ + GameFileResource *resource = GAME_FILE_RESOURCE (object); + + switch (param_id) { + case PROP_FILENAME: + g_value_set_string (value, resource->filename); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); + break; + } +} + +static void +game_file_resource_set_property (GObject *object, guint param_id, const GValue *value, + GParamSpec *pspec) +{ + GameFileResource *resource = GAME_FILE_RESOURCE (object); + + switch (param_id) { + case PROP_FILENAME: + game_file_resource_set_filename (resource, g_value_get_string (value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); + break; + } +} + +static void +game_file_resource_dispose (GObject *object) +{ + GameFileResource *resource = GAME_FILE_RESOURCE (object); + + game_file_resource_set_filename (resource, NULL); + + G_OBJECT_CLASS (game_file_resource_parent_class)->dispose (object); +} + +static gboolean +game_file_resource_load (GameResource *res, GVariantIter *iter) +{ + GameFileResource *resource = GAME_FILE_RESOURCE (res); + const char *filename; + + if (!GAME_RESOURCE_CLASS (game_file_resource_parent_class)->load (res, iter)) + return FALSE; + + if (!g_variant_iter_next (iter, "&s", &filename)) + return FALSE; + + game_file_resource_set_filename (resource, filename); + + return TRUE; +} + +static void +game_file_resource_save (GameResource *res, GVariantBuilder *builder) +{ + GameFileResource *resource = GAME_FILE_RESOURCE (res); + + GAME_RESOURCE_CLASS (game_file_resource_parent_class)->save (res, builder); + + g_variant_builder_add (builder, "s", resource->filename); +} + +static void +game_file_resource_class_init (GameFileResourceClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + GameResourceClass *resource_class = GAME_RESOURCE_CLASS (klass); + + object_class->set_property = game_file_resource_set_property; + object_class->get_property = game_file_resource_get_property; + object_class->dispose = game_file_resource_dispose; + + g_object_class_install_property (object_class, PROP_FILENAME, + g_param_spec_string ("filename", _("file name"), _("file providing data for this resource"), + NULL, G_PARAM_READWRITE)); + + resource_class->load = game_file_resource_load; + resource_class->save = game_file_resource_save; +} + +static void +game_file_resource_init (GameFileResource *resource) +{ +} + +const char * +game_file_resource_get_filename (GameFileResource *resource) +{ + g_return_val_if_fail (GAME_IS_FILE_RESOURCE (resource), NULL); + + return resource->filename; +} + +void +game_file_resource_set_filename (GameFileResource *resource, + const char * filename) +{ + const char *real_name; + + g_return_if_fail (GAME_IS_FILE_RESOURCE (resource)); + + g_free (resource->filename); + resource->filename = g_strdup (filename); + + if (filename == NULL) + real_name = NULL; + else if (g_path_is_absolute (filename)) + real_name = g_strdup (filename); + else + real_name = g_build_filename (game_get_data_dir (), + game_game_get_name (GAME_OBJECT (resource)->game), + //"resources", + filename, + NULL); + + if (real_name) + { + GError *error = NULL; + char *data; + gsize size; + + if (!g_file_get_contents (real_name, &data, &size, &error)) + { + g_warning ("Did not find resource `%s': %s", resource->filename, error->message); + data = NULL; + size = 0; + } + + game_data_resource_set_data (GAME_DATA_RESOURCE (resource), (guchar *) data, size); + } + else + { + game_data_resource_set_data (GAME_DATA_RESOURCE (resource), NULL, 0); + } + + g_object_notify (G_OBJECT (resource), "filename"); +} + diff --git a/libgame/game-file-resource.h b/libgame/game-file-resource.h new file mode 100644 index 0000000..116c295 --- /dev/null +++ b/libgame/game-file-resource.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2011 Benjamin Otte + * + * 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, 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef __GAME_FILE_RESOURCE_H__ +#define __GAME_FILE_RESOURCE_H__ + +#include + +G_BEGIN_DECLS + +#define GAME_TYPE_FILE_RESOURCE (game_file_resource_get_type ()) +#define GAME_FILE_RESOURCE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GAME_TYPE_FILE_RESOURCE, GameFileResource)) +#define GAME_FILE_RESOURCE_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), GAME_TYPE_FILE_RESOURCE, GameFileResourceClass)) +#define GAME_IS_FILE_RESOURCE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GAME_TYPE_FILE_RESOURCE)) +#define GAME_IS_FILE_RESOURCE_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GAME_TYPE_FILE_RESOURCE)) +#define GAME_FILE_RESOURCE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GAME_TYPE_FILE_RESOURCE, GameFileResourceClass)) + +typedef struct _GameFileResource GameFileResource; +typedef struct _GameFileResourceClass GameFileResourceClass; + +struct _GameFileResource { + GameDataResource data_resource; + + /*< private >*/ + char * filename; +}; + +struct _GameFileResourceClass { + GameDataResourceClass data_resource_class; +}; + +GType game_file_resource_get_type (void) G_GNUC_CONST; + +const char * game_file_resource_get_filename (GameFileResource * resource); +void game_file_resource_set_filename (GameFileResource * resource, + const char * filename); + + +G_END_DECLS + +#endif /* __GAME_FILE_RESOURCE_H__ */ diff --git a/libgame/libgame.h b/libgame/libgame.h index 3c7c3e2..6e4ba66 100644 --- a/libgame/libgame.h +++ b/libgame/libgame.h @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include -- cgit v1.2.3