[gnome-games/wip/exalm/rebrand: 51/102] dreamcast: Migrate to a game parser




commit 9e1422a191ad777838075710992b8efd4608af9b
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Tue Mar 30 01:59:42 2021 +0500

    dreamcast: Migrate to a game parser

 plugins/dreamcast/src/dreamcast-parser.vala | 62 +++++++++++++++++++++++++++++
 plugins/dreamcast/src/dreamcast-plugin.vala | 61 +---------------------------
 plugins/dreamcast/src/meson.build           |  1 +
 3 files changed, 65 insertions(+), 59 deletions(-)
---
diff --git a/plugins/dreamcast/src/dreamcast-parser.vala b/plugins/dreamcast/src/dreamcast-parser.vala
new file mode 100644
index 00000000..cd162c7b
--- /dev/null
+++ b/plugins/dreamcast/src/dreamcast-parser.vala
@@ -0,0 +1,62 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+public class Games.DreamcastParser : GameParser {
+       private const string GDI_MIME_TYPE = "application/x-gd-rom-cue";
+       private const string CDI_MIME_TYPE = "application/x-discjuggler-cd-image";
+       private const string DREAMCAST_MIME_TYPE = "application/x-dreamcast-rom";
+
+       private string uid;
+
+       public DreamcastParser (Platform platform, Uri uri) {
+               base (platform, uri);
+       }
+
+       private static File get_binary_file (Gdi gdi) throws Error {
+               if (gdi.tracks_number == 0)
+                       throw new DreamcastError.INVALID_GDI ("The file “%s” doesn’t have a track.", 
gdi.file.get_uri ());
+
+               var track = gdi.get_track (0);
+               var file = track.file;
+
+               var file_info = file.query_info ("*", FileQueryInfoFlags.NONE);
+               if (file_info.get_content_type () != DREAMCAST_MIME_TYPE)
+                       throw new DreamcastError.INVALID_FILE_TYPE ("The file “%s” doesn’t have a valid 
Dreamcast binary file.", gdi.file.get_uri ());
+
+               return file;
+       }
+
+       public override void parse () throws Error {
+               var file = uri.to_file ();
+               var file_info = file.query_info (FileAttribute.STANDARD_CONTENT_TYPE, 
FileQueryInfoFlags.NONE);
+               var mime_type = file_info.get_content_type ();
+
+               File bin_file;
+               switch (mime_type) {
+               case GDI_MIME_TYPE:
+                       var gdi = new Gdi (file);
+                       gdi.parse ();
+                       bin_file = get_binary_file (gdi);
+
+                       break;
+               case CDI_MIME_TYPE:
+                       bin_file = file;
+
+                       break;
+               default:
+                       throw new DreamcastError.INVALID_FILE_TYPE ("Invalid file type: expected %s or %s but 
got %s for file %s.", GDI_MIME_TYPE, CDI_MIME_TYPE, mime_type, uri.to_string ());
+               }
+
+               var header = new DreamcastHeader (bin_file);
+               header.check_validity ();
+
+               var prefix = platform.get_uid_prefix ();
+               var product_number = header.get_product_number ();
+               var areas = header.get_areas ();
+
+               uid = @"$prefix-$product_number-$areas".down ();
+       }
+
+       public override string get_uid () {
+               return uid;
+       }
+}
diff --git a/plugins/dreamcast/src/dreamcast-plugin.vala b/plugins/dreamcast/src/dreamcast-plugin.vala
index 5b7cd54b..1bf5e9fb 100644
--- a/plugins/dreamcast/src/dreamcast-plugin.vala
+++ b/plugins/dreamcast/src/dreamcast-plugin.vala
@@ -13,6 +13,7 @@ private class Games.DreamcastPlugin : Object, Plugin {
 
        static construct {
                platform = new Platform.with_mime_types (PLATFORM_ID, PLATFORM_NAME, MIME_TYPES, 
DREAMCAST_MIME_TYPE, PLATFORM_UID_PREFIX);
+               platform.parser_type = typeof (DreamcastParser);
        }
 
        public Platform[] get_platforms () {
@@ -20,71 +21,13 @@ private class Games.DreamcastPlugin : Object, Plugin {
        }
 
        public UriGameFactory[] get_uri_game_factories () {
-               var game_uri_adapter = new GenericGameUriAdapter (game_for_uri);
+               var game_uri_adapter = new RetroSimpleGameUriAdapter (platform);
                var factory = new GenericUriGameFactory (game_uri_adapter);
                foreach (var mime_type in MIME_TYPES)
                        factory.add_mime_type (mime_type);
 
                return { factory };
        }
-
-       private static string get_uid (DreamcastHeader header) throws Error {
-               var product_number = header.get_product_number ();
-               var areas = header.get_areas ();
-
-               return @"$PLATFORM_UID_PREFIX-$product_number-$areas".down ();
-       }
-
-       private static Game game_for_uri (Uri uri) throws Error {
-               var file = uri.to_file ();
-               var file_info = file.query_info (FileAttribute.STANDARD_CONTENT_TYPE, 
FileQueryInfoFlags.NONE);
-               var mime_type = file_info.get_content_type ();
-
-               File bin_file;
-               switch (mime_type) {
-               case GDI_MIME_TYPE:
-                       var gdi = new Gdi (file);
-                       gdi.parse ();
-                       bin_file = get_binary_file (gdi);
-
-                       break;
-               case CDI_MIME_TYPE:
-                       bin_file = file;
-
-                       break;
-               default:
-                       throw new DreamcastError.INVALID_FILE_TYPE ("Invalid file type: expected %s or %s but 
got %s for file %s.", GDI_MIME_TYPE, CDI_MIME_TYPE, mime_type, uri.to_string ());
-               }
-
-               var header = new DreamcastHeader (bin_file);
-               header.check_validity ();
-
-               var uid = new Uid (get_uid (header));
-               var title = new FilenameTitle (uri);
-               var media = new GriloMedia (title, DREAMCAST_MIME_TYPE);
-               var cover = new CompositeCover ({
-                       new LocalCover (uri),
-                       new GriloCover (media, uid)});
-
-               var game = new Game (uid, uri, title, platform);
-               game.set_cover (cover);
-
-               return game;
-       }
-
-       private static File get_binary_file (Gdi gdi) throws Error {
-               if (gdi.tracks_number == 0)
-                       throw new DreamcastError.INVALID_GDI ("The file “%s” doesn’t have a track.", 
gdi.file.get_uri ());
-
-               var track = gdi.get_track (0);
-               var file = track.file;
-
-               var file_info = file.query_info ("*", FileQueryInfoFlags.NONE);
-               if (file_info.get_content_type () != DREAMCAST_MIME_TYPE)
-                       throw new DreamcastError.INVALID_FILE_TYPE ("The file “%s” doesn’t have a valid 
Dreamcast binary file.", gdi.file.get_uri ());
-
-               return file;
-       }
 }
 
 [ModuleInit]
diff --git a/plugins/dreamcast/src/meson.build b/plugins/dreamcast/src/meson.build
index 90af8614..7aa8ce80 100644
--- a/plugins/dreamcast/src/meson.build
+++ b/plugins/dreamcast/src/meson.build
@@ -1,6 +1,7 @@
 vala_sources = [
   'dreamcast-error.vala',
   'dreamcast-header.vala',
+  'dreamcast-parser.vala',
   'dreamcast-plugin.vala',
   'gdi.vala',
 ]


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