[retro-gtk/wip/aplazas/test: 1/3] Port GameInfo to C



commit 17e43b60528886121da956b43d9b6ad3dc7f0460
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Fri Sep 1 09:13:06 2017 +0200

    Port GameInfo to C

 retro-gtk/Makefile.am                   |    2 +-
 retro-gtk/game-info.vala                |   72 -------------------------------
 retro-gtk/retro-core.c                  |   40 +++++++++--------
 retro-gtk/retro-disk-control-callback.h |    2 +-
 retro-gtk/retro-game-info.c             |   63 +++++++++++++++++++++++++++
 retro-gtk/retro-game-info.h             |   32 ++++++++++++++
 retro-gtk/retro-module.h                |    1 +
 7 files changed, 119 insertions(+), 93 deletions(-)
---
diff --git a/retro-gtk/Makefile.am b/retro-gtk/Makefile.am
index 1265bae..41546ac 100644
--- a/retro-gtk/Makefile.am
+++ b/retro-gtk/Makefile.am
@@ -44,7 +44,6 @@ libretro_gtk_la_SOURCES = \
        av-info.vala \
        core.vala \
        core-error.vala \
-       game-info.vala \
        memory.vala \
        retro.vala \
        retro-core-descriptor.vala \
@@ -52,6 +51,7 @@ libretro_gtk_la_SOURCES = \
        retro-core-view.vala \
        retro-core-view-extern.c \
        retro-core-view-input-device.c \
+       retro-game-info.c \
        retro-log.c \
        retro-module.c \
        retro-module-query.vala \
diff --git a/retro-gtk/retro-core.c b/retro-gtk/retro-core.c
index 243b575..4886f5e 100644
--- a/retro-gtk/retro-core.c
+++ b/retro-gtk/retro-core.c
@@ -409,7 +409,7 @@ retro_core_load_discs (RetroCore  *self,
   gchar *path;
   RetroSystemInfo system_info = { 0 };
   guint index;
-  RetroGameInfo game_info = { 0 };
+  RetroGameInfo *game_info = NULL;
   GError *tmp_error = NULL;
 
   g_return_if_fail (self != NULL);
@@ -446,34 +446,38 @@ retro_core_load_discs (RetroCore  *self,
     path = g_file_get_path (file);
 
     if (fullpath) {
-      retro_game_info_destroy (&game_info);
-      retro_game_info_init (&game_info, path);
+      if (game_info != NULL)
+        retro_game_info_free (game_info);
+      game_info = retro_game_info_new (path);
     }
     else {
-      retro_game_info_destroy (&game_info);
-      retro_game_info_init_with_data (&game_info, path, &tmp_error);
+      if (game_info != NULL)
+        retro_game_info_free (game_info);
+      game_info = retro_game_info_new_with_data (path, &tmp_error);
       if (G_UNLIKELY (tmp_error != NULL)) {
         g_propagate_error (error, tmp_error);
 
-        retro_game_info_destroy (&game_info);
+        if (game_info != NULL)
+          retro_game_info_free (game_info);
         g_free (path);
         g_object_unref (file);
+
         return;
       }
     }
 
-    retro_core_replace_disk_image_index (self, index, &game_info, &tmp_error);
+    retro_core_replace_disk_image_index (self, index, game_info, &tmp_error);
     if (G_UNLIKELY (tmp_error != NULL)) {
       g_propagate_error (error, tmp_error);
 
-      retro_game_info_destroy (&game_info);
+      retro_game_info_free (game_info);
       g_free (path);
       g_object_unref (file);
 
       return;
     }
 
-    retro_game_info_destroy (&game_info);
+    retro_game_info_free (game_info);
     g_free (path);
     g_object_unref (file);
   }
@@ -556,7 +560,7 @@ retro_core_load_medias (RetroCore* self,
   gchar *path;
   gboolean fullpath;
   RetroSystemInfo system_info = { 0 };
-  RetroGameInfo game_info = { 0 };
+  RetroGameInfo *game_info = NULL;
   GError *tmp_error = NULL;
 
   g_return_if_fail (self != NULL);
@@ -575,15 +579,13 @@ retro_core_load_medias (RetroCore* self,
   path = g_file_get_path (file);
   fullpath = retro_core_get_needs_full_path (self);
   if (fullpath) {
-    retro_game_info_destroy (&game_info);
-    retro_game_info_init (&game_info, path);
+    game_info = retro_game_info_new (path);
   }
   else {
-    retro_game_info_destroy (&game_info);
-    retro_game_info_init_with_data (&game_info, path, &tmp_error);
+    game_info = retro_game_info_new_with_data (path, &tmp_error);
     if (G_UNLIKELY (tmp_error != NULL)) {
       g_propagate_error (error, tmp_error);
-      retro_game_info_destroy (&game_info);
+      retro_game_info_free (game_info);
       g_free (path);
       g_object_unref (file);
       g_free (uri);
@@ -591,8 +593,8 @@ retro_core_load_medias (RetroCore* self,
       return;
     }
   }
-  if (!retro_core_load_game (self, &game_info)) {
-    retro_game_info_destroy (&game_info);
+  if (!retro_core_load_game (self, game_info)) {
+    retro_game_info_free (game_info);
     g_free (path);
     g_object_unref (file);
     g_free (uri);
@@ -603,7 +605,7 @@ retro_core_load_medias (RetroCore* self,
     retro_core_load_discs (self, &tmp_error);
     if (G_UNLIKELY (tmp_error != NULL)) {
       g_propagate_error (error, tmp_error);
-      retro_game_info_destroy (&game_info);
+      retro_game_info_free (game_info);
       g_free (path);
       g_object_unref (file);
       g_free (uri);
@@ -611,7 +613,7 @@ retro_core_load_medias (RetroCore* self,
       return;
     }
   }
-  retro_game_info_destroy (&game_info);
+  retro_game_info_free (game_info);
   g_free (path);
   g_object_unref (file);
   g_free (uri);
diff --git a/retro-gtk/retro-disk-control-callback.h b/retro-gtk/retro-disk-control-callback.h
index fc29cc5..6abefd1 100644
--- a/retro-gtk/retro-disk-control-callback.h
+++ b/retro-gtk/retro-disk-control-callback.h
@@ -3,7 +3,7 @@
 #ifndef RETRO_DISK_CONTROL_CALLBACK_H
 #define RETRO_DISK_CONTROL_CALLBACK_H
 
-#include "retro-gtk-internal.h"
+#include "retro-game-info.h"
 
 G_BEGIN_DECLS
 
diff --git a/retro-gtk/retro-game-info.c b/retro-gtk/retro-game-info.c
new file mode 100644
index 0000000..da14158
--- /dev/null
+++ b/retro-gtk/retro-game-info.c
@@ -0,0 +1,63 @@
+// This file is part of retro-gtk. License: GPL-3.0+.
+
+#include "retro-game-info.h"
+
+G_DEFINE_BOXED_TYPE (RetroGameInfo, retro_game_info, retro_game_info_copy, retro_game_info_free)
+
+RetroGameInfo *
+retro_game_info_new (const gchar *file_name)
+{
+  RetroGameInfo *self;
+
+  g_return_val_if_fail (file_name != NULL, NULL);
+
+  self = g_slice_new0 (RetroGameInfo);
+
+  self->path = g_strdup (file_name);
+  self->data = g_new0 (guint8, 0);
+  return self;
+}
+
+RetroGameInfo *
+retro_game_info_new_with_data (const gchar  *file_name,
+                               GError      **error)
+{
+  RetroGameInfo *self;
+
+  g_return_val_if_fail (file_name != NULL, NULL);
+
+  self = g_slice_new0 (RetroGameInfo);
+
+  self->path = g_strdup (file_name);
+  g_file_get_contents (file_name, (gchar **) &self->data, &self->size, error);
+  return self;
+}
+
+RetroGameInfo *
+retro_game_info_copy (RetroGameInfo *self)
+{
+  RetroGameInfo *copy;
+
+  g_return_val_if_fail (self, NULL);
+
+  copy = g_slice_new0 (RetroGameInfo);
+
+  copy->path = g_strdup (self->path);
+  copy->data = g_memdup (self, self->size);
+  copy->size = self->size;
+  copy->meta = g_strdup (self->meta);
+
+  return copy;
+}
+
+void
+retro_game_info_free (RetroGameInfo *self)
+{
+  g_return_if_fail (self);
+
+  g_free (self->path);
+  g_free (self->data);
+  g_free (self->meta);
+
+  g_slice_free (RetroGameInfo, self);
+}
diff --git a/retro-gtk/retro-game-info.h b/retro-gtk/retro-game-info.h
new file mode 100644
index 0000000..bbd43fa
--- /dev/null
+++ b/retro-gtk/retro-game-info.h
@@ -0,0 +1,32 @@
+// This file is part of retro-gtk. License: GPL-3.0+.
+
+#ifndef RETRO_GAME_INFO_H
+#define RETRO_GAME_INFO_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define RETRO_TYPE_GAME_INFO (retro_game_info_get_type())
+
+typedef struct _RetroGameInfo RetroGameInfo;
+
+struct _RetroGameInfo
+{
+  gchar *path;
+  gpointer data;
+  gsize size;
+  gchar *meta;
+};
+
+RetroGameInfo *retro_game_info_new (const gchar *file_name);
+RetroGameInfo * retro_game_info_new_with_data (const gchar  *file_name,
+                                               GError      **error);
+RetroGameInfo *retro_game_info_copy (RetroGameInfo *self);
+void retro_game_info_free (RetroGameInfo *self);
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (RetroGameInfo, retro_game_info_free)
+
+G_END_DECLS
+
+#endif /* RETRO_GAME_INFO_H */
diff --git a/retro-gtk/retro-module.h b/retro-gtk/retro-module.h
index 07b106c..fa53433 100644
--- a/retro-gtk/retro-module.h
+++ b/retro-gtk/retro-module.h
@@ -5,6 +5,7 @@
 
 #include <glib-object.h>
 #include "retro-gtk-internal.h"
+#include "retro-game-info.h"
 #include "retro-system-info.h"
 
 G_BEGIN_DECLS


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]