[gnome-games/wip/aplazas/781334-refactor-game-sources: 4/6] steam: Use SteamUriSource and a generic game factory



commit c704a1a9ebbee2ca02709d489798494259203b3e
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Sat May 6 10:52:54 2017 +0200

    steam: Use SteamUriSource and a generic game factory
    
    Replace usage of SteamGameSource by a SteamUriSource and a
    GenericUriGameFactory accepting the steam+file URI scheme.

 plugins/steam/src/steam-plugin.vala |   54 +++++++++++++++++++++++++++++++++-
 1 files changed, 52 insertions(+), 2 deletions(-)
---
diff --git a/plugins/steam/src/steam-plugin.vala b/plugins/steam/src/steam-plugin.vala
index e9a682f..3974853 100644
--- a/plugins/steam/src/steam-plugin.vala
+++ b/plugins/steam/src/steam-plugin.vala
@@ -1,8 +1,58 @@
 // This file is part of GNOME Games. License: GPL-3.0+.
 
 private class Games.SteamPlugin : Object, Plugin {
-       public GameSource? get_game_source () throws Error {
-               return new SteamGameSource ();
+       private const string STEAM_FILE_SCHEME = "steam+file";
+
+       private static HashTable<string, Game> game_for_id;
+
+       static construct {
+               game_for_id = new HashTable<string, Game> (str_hash, str_equal);
+       }
+
+       public UriSource[] get_uri_sources () {
+               var source = new SteamUriSource ();
+
+               return { source };
+       }
+
+       public UriGameFactory[] get_uri_game_factories () {
+               var game_uri_adapter = new GenericGameUriAdapter (game_for_uri);
+               var factory = new GenericUriGameFactory (game_uri_adapter);
+               factory.add_scheme (STEAM_FILE_SCHEME);
+
+               return { factory };
+       }
+
+       private static Game game_for_uri (Uri uri) throws Error {
+               var file_uri = new Uri.from_uri_and_scheme (uri, "file");
+               var file = file_uri.to_file ();
+               var appmanifest_path = file.get_path ();
+               var registry = new SteamRegistry (appmanifest_path);
+               var game_id = registry.get_data ({"AppState", "appid"});
+               /* The gamegames_id sometimes is identified by appID
+                * see issue https://github.com/Kekun/gnome-games/issues/169 */
+               if (game_id == null)
+                       game_id = registry.get_data ({"AppState", "appID"});
+
+               if (game_id == null)
+                       throw new SteamError.NO_APPID (_("Couldn’t get Steam appid from manifest “%s”."), 
appmanifest_path);
+
+               if (game_id in game_for_id) {
+                       var game = game_for_id[game_id];
+
+                       return game;
+               }
+
+               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);
+
+               var game = new GenericGame (title, icon, cover, runner);
+               game_for_id[game_id] = game;
+
+               return game;
        }
 }
 


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