[gnome-games/wip/aplazas/781334-refactor-game-sources: 25/27] steam: Add SteamUriSource



commit aff49fb545c7dd7ce71211eae9a18afbb65f3f6f
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Wed May 3 23:29:43 2017 +0200

    steam: Add SteamUriSource

 plugins/steam/src/Makefile.am             |    2 +
 plugins/steam/src/steam-uri-iterator.vala |   57 +++++++++++++++++++++++++++++
 plugins/steam/src/steam-uri-source.vala   |   57 +++++++++++++++++++++++++++++
 3 files changed, 116 insertions(+), 0 deletions(-)
---
diff --git a/plugins/steam/src/Makefile.am b/plugins/steam/src/Makefile.am
index 29b1556..2d50867 100644
--- a/plugins/steam/src/Makefile.am
+++ b/plugins/steam/src/Makefile.am
@@ -13,6 +13,8 @@ libgames_steam_plugin_la_SOURCES = \
        steam-plugin.vala \
        steam-registry.vala \
        steam-title.vala \
+       steam-uri-iterator.vala \
+       steam-uri-source.vala \
        $(NULL)
 
 libgames_steam_plugin_la_VALAFLAGS = \
diff --git a/plugins/steam/src/steam-uri-iterator.vala b/plugins/steam/src/steam-uri-iterator.vala
new file mode 100644
index 0000000..9df2100
--- /dev/null
+++ b/plugins/steam/src/steam-uri-iterator.vala
@@ -0,0 +1,57 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.SteamUriIterator : Object, UriIterator {
+       private string[] directories;
+       private int directory_index;
+       private string? uri;
+
+       internal SteamUriIterator (string[] directories) {
+               this.directories = directories;
+               directory_index = 0;
+               uri = null;
+       }
+
+       public new string? get () {
+               return uri;
+       }
+
+       public bool next () {
+               while (directory_index < directories.length) {
+                       if (try_next_for_directory (directories[directory_index]))
+                               return true;
+
+                       directory_index++;
+               }
+
+               return false;
+       }
+
+       private bool try_next_for_directory (string directory) {
+               try {
+                       if (next_for_directory (directory))
+                               return true;
+               }
+               catch (Error e) {
+                       debug (e.message);
+               }
+
+               uri = null;
+
+               return false;
+       }
+
+       private bool next_for_directory (string directory) throws Error {
+               var file = File.new_for_path (directory);
+
+               var enumerator = file.enumerate_children (FileAttribute.STANDARD_NAME, 0);
+
+               var info = enumerator.next_file ();
+               if (info == null)
+                       return false;
+
+               var filename = Path.build_filename (directory, info.get_name ());
+               uri = Filename.to_uri (filename);
+
+               return true;
+       }
+}
diff --git a/plugins/steam/src/steam-uri-source.vala b/plugins/steam/src/steam-uri-source.vala
new file mode 100644
index 0000000..209c76d
--- /dev/null
+++ b/plugins/steam/src/steam-uri-source.vala
@@ -0,0 +1,57 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.SteamUriSource : Object, UriSource {
+       // From the home directory.
+       private const string REGISTRY_PATH = "/.steam/registry.vdf";
+       // From the home directory.
+       private const string DEFAULT_INSTALL_DIR_SYMLINK = "/.steam/steam";
+       // From an install directory.
+       private const string[] STEAMAPPS_DIRS = { "/SteamApps", "/steamapps" };
+       // From the default SteamApp directory.
+       private const string LIBRARY_DIRS_REG = "/libraryfolders.vdf";
+
+       private const string[] INSTALL_PATH_REGISTRY_PATH =
+               { "Registry", "HKLM", "Software", "Valve", "Steam", "InstallPath" };
+
+       private string[] directories;
+
+       public SteamUriSource () throws Error {
+               directories = {};
+
+               // Steam's installation path can be found in its registry.
+               var home = Environment.get_home_dir ();
+               var registry_path = home + REGISTRY_PATH;
+               var registry = new SteamRegistry (registry_path);
+               var install_path = registry.get_data (INSTALL_PATH_REGISTRY_PATH);
+
+               add_library (home + DEFAULT_INSTALL_DIR_SYMLINK);
+               add_library (install_path);
+
+               // `/LibraryFolders/$NUMBER` entries in the libraryfolders.vdf registry
+               // file are library directories.
+               foreach (var steamapps_dir in STEAMAPPS_DIRS) {
+                       var install_steamapps_dir = install_path + steamapps_dir;
+                       var file = File.new_for_path (install_steamapps_dir);
+                       if (!file.query_exists ())
+                               continue;
+
+                       var library_reg_path = install_steamapps_dir + LIBRARY_DIRS_REG;
+                       var library_reg = new SteamRegistry (library_reg_path);
+                       foreach (var child in library_reg.get_children ({ "LibraryFolders" }))
+                               if (/^\d+$/.match (child))
+                                       add_library (library_reg.get_data ({ "LibraryFolders", child }));
+               }
+       }
+
+       public UriIterator iterator () {
+               return new SteamUriIterator (directories);
+       }
+
+       private void add_library (string library) {
+               foreach (var steamapps_dir in STEAMAPPS_DIRS) {
+                       var library_steamapps_dir = library + steamapps_dir;
+                       if (FileUtils.test (library_steamapps_dir, FileTest.EXISTS))
+                               directories += library_steamapps_dir;
+               }
+       }
+}


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