[gnome-games/wip/exalm/rebrand: 69/102] Merge PlayStationGameFactory and GenericUriGameFactory




commit a7420e4739130b7bbb49e7c36696de418b4eb3b7
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Tue Mar 30 15:53:05 2021 +0500

    Merge PlayStationGameFactory and GenericUriGameFactory
    
    Finally, we only have one uri game factory implementation.

 plugins/playstation/src/meson.build                |   1 -
 .../playstation/src/playstation-game-factory.vala  | 126 ---------------------
 plugins/playstation/src/playstation-plugin.vala    |   2 +-
 src/generic/generic-uri-game-factory.vala          | 108 +++++++++++++++---
 4 files changed, 94 insertions(+), 143 deletions(-)
---
diff --git a/plugins/playstation/src/meson.build b/plugins/playstation/src/meson.build
index af1fd603..db45406c 100644
--- a/plugins/playstation/src/meson.build
+++ b/plugins/playstation/src/meson.build
@@ -1,6 +1,5 @@
 vala_sources = [
   'playstation-error.vala',
-  'playstation-game-factory.vala',
   'playstation-header.vala',
   'playstation-parser.vala',
   'playstation-plugin.vala',
diff --git a/plugins/playstation/src/playstation-plugin.vala b/plugins/playstation/src/playstation-plugin.vala
index 526c7a9d..738069e7 100644
--- a/plugins/playstation/src/playstation-plugin.vala
+++ b/plugins/playstation/src/playstation-plugin.vala
@@ -20,7 +20,7 @@ private class Games.PlayStation : Object, Plugin {
        }
 
        public UriGameFactory[] get_uri_game_factories () {
-               var factory = new PlayStationGameFactory (platform);
+               var factory = new GenericUriGameFactory (platform);
 
                return { factory };
        }
diff --git a/src/generic/generic-uri-game-factory.vala b/src/generic/generic-uri-game-factory.vala
index bd538c32..8f0c4394 100644
--- a/src/generic/generic-uri-game-factory.vala
+++ b/src/generic/generic-uri-game-factory.vala
@@ -5,9 +5,17 @@ public class Games.GenericUriGameFactory : Object, UriGameFactory {
        private HashTable<Uri, Game> game_for_uri;
        private unowned GameCallback game_added_callback;
 
+       private HashTable<string, Media> medias;
+       private HashTable<string, Game> game_for_media_set;
+       private GenericSet<Game> games;
+
        public GenericUriGameFactory (Platform platform) {
                this.platform = platform;
+
                game_for_uri = new HashTable<Uri, Game> (Uri.hash, Uri.equal);
+               medias = new HashTable<string, Media> (str_hash, str_equal);
+               game_for_media_set = new HashTable<string, Game> (str_hash, str_equal);
+               games = new GenericSet<Game> (direct_hash, direct_equal);
        }
 
        public string[] get_mime_types () {
@@ -15,19 +23,85 @@ public class Games.GenericUriGameFactory : Object, UriGameFactory {
        }
 
        public void add_uri (Uri uri) {
+               try {
+                       add_uri_with_error (uri);
+               }
+               catch (Error e) {
+                       debug (e.message);
+               }
+       }
+
+       private void add_uri_with_error (Uri uri) throws Error {
                if (game_for_uri.contains (uri))
                        return;
 
-               try {
-                       var game = get_game_for_uri (uri);
+               var parser = Object.new (
+                       platform.parser_type,
+                       platform: platform,
+                       uri: uri
+               ) as GameParser;
+
+               parser.parse ();
+
+               var media_id = parser.get_media_id ();
+               var media_set_id = parser.get_media_set_id ();
+
+               if (media_id == null || media_set_id == null) {
+                       var game = create_game (parser);
                        game_for_uri[uri] = game;
+                       games.add (game);
 
                        if (game_added_callback != null)
                                game_added_callback (game);
+
+                       return;
                }
-               catch (Error e) {
-                       debug (e.message);
+
+               return_if_fail (medias.contains (media_id) == game_for_media_set.contains (media_set_id));
+
+               // Check whether we already have a media and by extension a media set
+               // and a game for this disc ID. If such a case, simply add the new URI.
+               if (medias.contains (media_id)) {
+                       var media = medias.lookup (media_id);
+                       media.add_uri (uri);
+                       game_for_uri[uri] = game_for_media_set[media_set_id];
+
+                       try_add_game (game_for_uri[uri]);
+
+                       return;
                }
+
+               // A game correspond to this URI but we don't have it yet: create it.
+
+               var media_set = parser.create_media_set ();
+               var game = create_game (parser, media_set);
+
+               // Creating the Medias, MediaSet and Game worked, we can save them.
+
+               media_set.foreach_media (media => {
+                       medias[media.id] = media;
+               });
+
+               game_for_uri[uri] = game;
+               game_for_media_set[media_set_id] = game;
+               games.add (game);
+
+               try_add_game (game);
+       }
+
+       private void try_add_game (Game game) {
+               if (game_added_callback == null)
+                       return;
+
+               bool is_complete = true;
+               game.media_set.foreach_media (media => {
+                       is_complete &= (media.get_uris ().length != 0);
+               });
+
+               if (!is_complete)
+                       return;
+
+               game_added_callback (game);
        }
 
        public Game? query_game_for_uri (Uri uri) {
@@ -38,30 +112,34 @@ public class Games.GenericUriGameFactory : Object, UriGameFactory {
        }
 
        public void foreach_game (GameCallback game_callback) {
-               var games = game_for_uri.get_values ();
-               foreach (var game in games)
-                       game_callback (game);
+               games.foreach ((game) => game_callback (game));
        }
 
        public void set_game_added_callback (GameCallback game_callback) {
                game_added_callback = game_callback;
        }
 
-       private Game get_game_for_uri (Uri uri) throws Error {
-               var parser = Object.new (platform.parser_type, platform: platform, uri: uri) as GameParser;
-
-               parser.parse ();
-
+       private Game create_game (GameParser parser, MediaSet? media_set = null) throws Error {
                var uid = new Uid (parser.get_uid ());
                var title = parser.get_title ();
                var media = new GriloMedia (title, platform.get_presentation_mime_type ());
                var cover = new CompositeCover ({
-                       new LocalCover (uri),
-                       new GriloCover (media, uid)});
+                       new LocalCover (parser.uri),
+                       new GriloCover (media, uid)}
+               );
                var icon = parser.get_icon ();
 
-               var game = new Game (uid, uri, title, platform);
+               if (media_set != null) {
+                       title = new CompositeTitle ({
+                               media_set.title,
+                               title
+                       });
+               }
+
+               var game = new Game (uid, parser.uri, title, platform);
                game.set_cover (cover);
+               game.media_set = media_set;
+
                if (icon != null)
                        game.set_icon (icon);
 


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