[gnome-games/wip/aplazas/781334-refactor-game-sources: 4/4] playstation: Make game creation more flexible
- From: Adrien Plazas <aplazas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-games/wip/aplazas/781334-refactor-game-sources: 4/4] playstation: Make game creation more flexible
- Date: Thu, 4 May 2017 19:39:43 +0000 (UTC)
commit 14cd9eabfd3285b7d2cda127bf2332b0a7488f9e
Author: Adrien Plazas <kekun plazas laposte net>
Date: Fri Apr 14 15:48:04 2017 +0200
playstation: Make game creation more flexible
.../playstation/src/playstation-game-factory.vala | 150 ++++++++------------
plugins/playstation/src/playstation-uid.vala | 9 +-
2 files changed, 62 insertions(+), 97 deletions(-)
---
diff --git a/plugins/playstation/src/playstation-game-factory.vala
b/plugins/playstation/src/playstation-game-factory.vala
index f29ea92..894e356 100644
--- a/plugins/playstation/src/playstation-game-factory.vala
+++ b/plugins/playstation/src/playstation-game-factory.vala
@@ -9,14 +9,16 @@ public class Games.PlayStationGameFactory : Object, UriGameFactory {
private static GameinfoDoc gameinfo;
- private HashTable<string, string> discs;
- private HashTable<string, GenericSet<string>> disc_sets;
- private HashTable<string, Game> games;
+ private GenericSet<string> uris;
+ private HashTable<string, Media> medias; // Key: disc ID.
+ private HashTable<string, MediaSet> media_sets; // Key: disc set ID.
+ private HashTable<string, Game> games; // Key: URI.
public PlayStationGameFactory () {
- discs = new HashTable<string, string> (GLib.str_hash, GLib.str_equal);
- disc_sets = new HashTable<string, GenericSet<string>> (GLib.str_hash, GLib.str_equal);
+ uris = new GenericSet<string> (str_hash, str_equal);
+ medias = new HashTable<string, Media> (str_hash, str_equal);
+ media_sets = new HashTable<string, MediaSet> (str_hash, str_equal);
games = new HashTable<string, Game> (GLib.str_hash, GLib.str_equal);
}
@@ -31,55 +33,67 @@ public class Games.PlayStationGameFactory : Object, UriGameFactory {
}
public void add_uri (string uri) {
- string disc_id;
-
try {
- disc_id = get_disc_id (uri);
+ add_uri_with_error (uri);
}
catch (Error e) {
- return;
+ debug (e.message);
}
-
- discs[disc_id] = uri;
}
- public async void foreach_game (GameCallback game_callback) {
- GameinfoDoc gameinfo = null;
- try {
- gameinfo = get_gameinfo ();
+ // TODO support unknown games (not in DB)
+ public void add_uri_with_error (string uri) throws Error {
+ if (uris.contains (uri))
+ return;
+
+ uris.add (uri);
+
+ var gameinfo = get_gameinfo ();
+ var disc_id = get_disc_id (uri);
+ var disc_set_id = gameinfo.get_disc_set_id_for_disc_id (disc_id);
+
+ return_if_fail (medias.contains (disc_id) == media_sets.contains (disc_set_id));
+
+ if (medias.contains (disc_id)) {
+ var media = medias.lookup (disc_id);
+ media.add_uri (uri);
+
+ return;
}
- catch (Error e) {
- warning (e.message);
+
+ // A game correspond to this URI but we don't have it yet: create it.
+
+ var new_medias = new HashTable<string, Media> (str_hash, str_equal);
+ Media[] new_medias_array = {};
+ var new_disc_ids = gameinfo.get_disc_set_ids_for_disc_id (disc_id);
+ foreach (var new_disc_id in new_disc_ids) {
+ // TODO If disc ID already known, throw error?
+ var title = new GameinfoDiscIdDiscTitle (gameinfo, new_disc_id);
+ var media = new Media (title);
+ new_medias_array += media;
+ new_medias[new_disc_id] = media;
}
- discs.foreach((disc_id, uri) => {
- var disc_set_id = disc_id;
+ var media = new_medias.lookup (disc_id);
+ media.add_uri (uri);
- try {
- if (gameinfo != null)
- disc_set_id = gameinfo.get_disc_set_id_for_disc_id (disc_id);
- }
- catch (Error e) {
- debug (_("Disc with disc_id %s is unknown"), disc_id);
- }
+ // FIXME Create the game when we have a URI for the first disc, not before.
- if (!(disc_set_id in disc_sets))
- disc_sets[disc_set_id] = new GenericSet<string> (GLib.str_hash,
GLib.str_equal);
+ var icon = GLib.Icon.new_for_string (ICON_NAME);
+ var media_set = new MediaSet (new_medias_array, icon);
+ var game = create_game (media_set, disc_set_id, uri);
- disc_sets[disc_set_id].add (disc_id);
- });
+ // Creating the Medias, MediaSet and Game worked, we can save them.
- disc_sets.foreach ((disc_set_id, disc_set) => {
- try {
- if (!(disc_set_id in games))
- games[disc_set_id] = game_for_disc_set (disc_set);
- }
- catch (Error e) {
- warning (e.message);
- }
- });
+ foreach (var new_disc_id in new_medias.get_keys ())
+ medias[new_disc_id] = new_medias[new_disc_id];
+
+ media_sets[disc_set_id] = media_set;
+ games[uri] = game; // For "game_for_uri ()".
+ }
- games.foreach ((disc_set_id, game) => {
+ public async void foreach_game (GameCallback game_callback) {
+ games.foreach ((uri, game) => {
game_callback (game);
});
}
@@ -96,59 +110,11 @@ public class Games.PlayStationGameFactory : Object, UriGameFactory {
return header.disc_id;
}
- private Game game_for_disc_set (GenericSet<string> disc_set) throws Error {
- Media[] medias = {};
-
- var gameinfo = get_gameinfo ();
- var disc_list = disc_set.get_values ();
- disc_list.sort_with_data ((disc_id_a, disc_id_b) => {
- int index_a;
- int index_b;
- try {
- index_a = gameinfo.get_disc_set_index_for_disc_id (disc_id_a);
- index_b = gameinfo.get_disc_set_index_for_disc_id (disc_id_b);
- }
- catch (Error e) {
- debug (e.message);
-
- return 0;
- }
-
- return (int) (index_a > index_b) - (int) (index_a < index_b);
- });
-
- disc_list.foreach ((disc_id) => {
- var uri = discs[disc_id];
- var title = new GameinfoDiscIdDiscTitle (gameinfo, disc_id);
- var media = new Media (title);
- media.add_uri (uri);
- medias += media;
- });
-
- var icon = GLib.Icon.new_for_string (ICON_NAME);
- var media_set = new MediaSet (medias, icon);
-
- var game = game_for_uris (media_set);
-
- return game;
- }
-
- private Game game_for_uris (MediaSet media_set) throws Error {
- var selected_media = media_set.get_selected_media (0);
- var uris = selected_media.get_uris ();
- // FIXME Check that there is at least one URI.
- var uri = uris[0];
- var cue_file = File.new_for_uri (uri);
- var cue_sheet = new CueSheet (cue_file);
- var cue_track_node = cue_sheet.get_track (0);
- var bin_file = cue_track_node.file.file;
- var header = new PlayStationHeader (bin_file);
- header.check_validity ();
-
+ private Game create_game (MediaSet media_set, string disc_set_id, string uri) throws Error {
var gameinfo = get_gameinfo ();
- var uid = new PlayStationUid (header);
+ var uid = new PlayStationUid (disc_set_id);
var title = new CompositeTitle ({
- new GameinfoDiscIdGameTitle (gameinfo, header.disc_id),
+ new GameinfoDiscIdGameTitle (gameinfo, disc_set_id),
new FilenameTitle (uri)
});
var icon = new DummyIcon ();
@@ -156,7 +122,7 @@ public class Games.PlayStationGameFactory : Object, UriGameFactory {
var cover = new CompositeCover ({
new LocalCover (uri),
new GriloCover (media, uid)});
- var input_capabilities = new GameinfoDiscIdInputCapabilities (gameinfo, header.disc_id);
+ var input_capabilities = new GameinfoDiscIdInputCapabilities (gameinfo, disc_set_id);
var core_source = new RetroCoreSource (PLATFORM, { SEARCHED_MIME_TYPE, SPECIFIC_MIME_TYPE });
var runner = new RetroRunner.for_media_set_and_input_capabilities (core_source, media_set,
uid, input_capabilities, title);
diff --git a/plugins/playstation/src/playstation-uid.vala b/plugins/playstation/src/playstation-uid.vala
index b4aa489..279349c 100644
--- a/plugins/playstation/src/playstation-uid.vala
+++ b/plugins/playstation/src/playstation-uid.vala
@@ -1,19 +1,18 @@
// This file is part of GNOME Games. License: GPL-3.0+.
private class Games.PlayStationUid: Object, Uid {
- private PlayStationHeader header;
+ private string disc_set_id;
private string uid;
- public PlayStationUid (PlayStationHeader header) {
- this.header = header;
+ public PlayStationUid (string disc_set_id) {
+ this.disc_set_id = disc_set_id;
}
public string get_uid () throws Error {
if (uid != null)
return uid;
- var disc_id = header.disc_id;
- uid = @"playstation-$disc_id".down ();
+ uid = @"playstation-$disc_set_id".down ();
return uid;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]