[gnome-games/wip/aplazas/781334-refactor-game-sources: 26/27] steam: Add SteamGameSource.game_for_uri()



commit 62052e730fdc0691fb5e19c849b9541844d2c170
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Thu Apr 13 15:56:20 2017 +0200

    steam: Add SteamGameSource.game_for_uri()
    
    This will be used in a subsequent commit to make a game from an URI.

 plugins/steam/src/steam-error.vala       |    1 +
 plugins/steam/src/steam-game-source.vala |   25 +++++++++++++++++++------
 2 files changed, 20 insertions(+), 6 deletions(-)
---
diff --git a/plugins/steam/src/steam-error.vala b/plugins/steam/src/steam-error.vala
index 42850d4..b87793b 100644
--- a/plugins/steam/src/steam-error.vala
+++ b/plugins/steam/src/steam-error.vala
@@ -1,6 +1,7 @@
 // This file is part of GNOME Games. License: GPL-3.0+.
 
 errordomain Games.SteamError {
+       INVALID_URI,
        NO_APPID,
        NO_NAME,
 }
diff --git a/plugins/steam/src/steam-game-source.vala b/plugins/steam/src/steam-game-source.vala
index 09db976..c2d577f 100644
--- a/plugins/steam/src/steam-game-source.vala
+++ b/plugins/steam/src/steam-game-source.vala
@@ -46,6 +46,17 @@ private class Games.SteamGameSource : Object, GameSource {
                }
        }
 
+       public async Game game_for_uri (string uri) throws Error {
+               var file = File.new_for_uri (uri);
+                if (!file.query_exists ())
+                       throw new SteamError.INVALID_URI ("Invalid URI: %s.", uri);
+
+               var path = file.get_path ();
+               string game_id;
+
+               return game_for_appmanifest_path (path, out game_id);
+       }
+
        public async void each_game (GameCallback game_callback) {
                if (games == null)
                        games = new HashTable<string, Game> (str_hash, str_equal);
@@ -76,7 +87,10 @@ private class Games.SteamGameSource : Object, GameSource {
                var name = info.get_name ();
                if (appmanifest_regex.match (name)) {
                        try {
-                               game_for_appmanifest_path (@"$directory/$name");
+                               string game_id;
+                               var game = game_for_appmanifest_path (@"$directory/$name", out game_id);
+                               if (!(game_id in games))
+                                       games[game_id] = game;
 
                                Idle.add (this.game_for_file_info.callback);
                                yield;
@@ -87,7 +101,7 @@ private class Games.SteamGameSource : Object, GameSource {
                }
        }
 
-       private void game_for_appmanifest_path (string appmanifest_path) throws Error {
+       private Game game_for_appmanifest_path (string appmanifest_path, out string id) throws Error {
                var registry = new SteamRegistry (appmanifest_path);
                var game_id = registry.get_data ({"AppState", "appid"});
                /* The game_id sometimes is identified by appID
@@ -98,15 +112,14 @@ private class Games.SteamGameSource : Object, GameSource {
                if (game_id == null)
                        throw new SteamError.NO_APPID (_("Couldn’t get Steam appid from manifest “%s”."), 
appmanifest_path);
 
-               if (game_id in games)
-                       return;
-
                var title = new SteamTitle (registry);
                var icon = new SteamIcon (game_id);
                var cover = new SteamCover (game_id);
                string[] args = { "steam", @"steam://rungameid/" + game_id };
                var runner = new CommandRunner (args, false);
 
-               games[game_id] = new GenericGame (title, icon, cover, runner);
+               id = game_id;
+
+               return new GenericGame (title, icon, cover, runner);
        }
 }


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