From e60a695b83d72ef53022171842b87e8ad0ff2673 Mon Sep 17 00:00:00 2001 From: AlexZ Date: Mon, 27 Jan 2020 04:34:34 +0300 Subject: [PATCH] Add info directly from Steam --- .../EpicMorg.SteamPathsLib.csproj | 14 +- .../ActiveProcessSteamRegistryData.cs} | 4 +- .../Model/ConfigSteamData.cs | 11 ++ .../Model/LibrarySteamData.cs | 13 ++ .../Model/SteamAppManifestData.cs | 15 ++ .../SteamAppRegistryData.cs} | 4 +- .../SteamRegistryData.cs} | 4 +- src/EpicMorg.SteamPathsLib/SteamPathsUtil.cs | 136 ++++++++++++------ 8 files changed, 149 insertions(+), 52 deletions(-) rename src/EpicMorg.SteamPathsLib/{model/ActiveProcessSteamData.cs => Model/ActiveProcessSteamRegistryData.cs} (69%) create mode 100644 src/EpicMorg.SteamPathsLib/Model/ConfigSteamData.cs create mode 100644 src/EpicMorg.SteamPathsLib/Model/LibrarySteamData.cs create mode 100644 src/EpicMorg.SteamPathsLib/Model/SteamAppManifestData.cs rename src/EpicMorg.SteamPathsLib/{model/SteamAppData.cs => Model/SteamAppRegistryData.cs} (80%) rename src/EpicMorg.SteamPathsLib/{model/SteamData.cs => Model/SteamRegistryData.cs} (88%) diff --git a/src/EpicMorg.SteamPathsLib/EpicMorg.SteamPathsLib.csproj b/src/EpicMorg.SteamPathsLib/EpicMorg.SteamPathsLib.csproj index dd2e504..6219390 100644 --- a/src/EpicMorg.SteamPathsLib/EpicMorg.SteamPathsLib.csproj +++ b/src/EpicMorg.SteamPathsLib/EpicMorg.SteamPathsLib.csproj @@ -45,14 +45,22 @@ - - - + + + + + + + + + 0.5.0 + + \ No newline at end of file diff --git a/src/EpicMorg.SteamPathsLib/model/ActiveProcessSteamData.cs b/src/EpicMorg.SteamPathsLib/Model/ActiveProcessSteamRegistryData.cs similarity index 69% rename from src/EpicMorg.SteamPathsLib/model/ActiveProcessSteamData.cs rename to src/EpicMorg.SteamPathsLib/Model/ActiveProcessSteamRegistryData.cs index 477b12b..574bdc4 100644 --- a/src/EpicMorg.SteamPathsLib/model/ActiveProcessSteamData.cs +++ b/src/EpicMorg.SteamPathsLib/Model/ActiveProcessSteamRegistryData.cs @@ -4,9 +4,9 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace EpicMorg.SteamPathsLib.model +namespace EpicMorg.SteamPathsLib.Model { - public class ActiveProcessSteamData + public class ActiveProcessSteamRegistryData { public string RegistryKey; diff --git a/src/EpicMorg.SteamPathsLib/Model/ConfigSteamData.cs b/src/EpicMorg.SteamPathsLib/Model/ConfigSteamData.cs new file mode 100644 index 0000000..880af29 --- /dev/null +++ b/src/EpicMorg.SteamPathsLib/Model/ConfigSteamData.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EpicMorg.SteamPathsLib.Model { + public class ConfigSteamData { + public string[] SteamLibraryFolders; + } +} diff --git a/src/EpicMorg.SteamPathsLib/Model/LibrarySteamData.cs b/src/EpicMorg.SteamPathsLib/Model/LibrarySteamData.cs new file mode 100644 index 0000000..47ed97e --- /dev/null +++ b/src/EpicMorg.SteamPathsLib/Model/LibrarySteamData.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EpicMorg.SteamPathsLib.Model { + public class LibrarySteamData { + public string Path; + + public List AppManifestDataList; + } +} diff --git a/src/EpicMorg.SteamPathsLib/Model/SteamAppManifestData.cs b/src/EpicMorg.SteamPathsLib/Model/SteamAppManifestData.cs new file mode 100644 index 0000000..32130ba --- /dev/null +++ b/src/EpicMorg.SteamPathsLib/Model/SteamAppManifestData.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EpicMorg.SteamPathsLib.Model { + public class SteamAppManifestData { + public int AppId; + public string Name; + public string InstallDir; + + public string Path; + } +} diff --git a/src/EpicMorg.SteamPathsLib/model/SteamAppData.cs b/src/EpicMorg.SteamPathsLib/Model/SteamAppRegistryData.cs similarity index 80% rename from src/EpicMorg.SteamPathsLib/model/SteamAppData.cs rename to src/EpicMorg.SteamPathsLib/Model/SteamAppRegistryData.cs index 0318801..d659bbf 100644 --- a/src/EpicMorg.SteamPathsLib/model/SteamAppData.cs +++ b/src/EpicMorg.SteamPathsLib/Model/SteamAppRegistryData.cs @@ -4,9 +4,9 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace EpicMorg.SteamPathsLib.model +namespace EpicMorg.SteamPathsLib.Model { - public class SteamAppData + public class SteamAppRegistryData { public string RegistryKey; public string Name; diff --git a/src/EpicMorg.SteamPathsLib/model/SteamData.cs b/src/EpicMorg.SteamPathsLib/Model/SteamRegistryData.cs similarity index 88% rename from src/EpicMorg.SteamPathsLib/model/SteamData.cs rename to src/EpicMorg.SteamPathsLib/Model/SteamRegistryData.cs index ec71ae8..9529c89 100644 --- a/src/EpicMorg.SteamPathsLib/model/SteamData.cs +++ b/src/EpicMorg.SteamPathsLib/Model/SteamRegistryData.cs @@ -4,9 +4,9 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace EpicMorg.SteamPathsLib.model +namespace EpicMorg.SteamPathsLib.Model { - public class SteamData + public class SteamRegistryData { public string RegistryKey; public string LastGameNameUsed; diff --git a/src/EpicMorg.SteamPathsLib/SteamPathsUtil.cs b/src/EpicMorg.SteamPathsLib/SteamPathsUtil.cs index 3521e41..5ef696e 100644 --- a/src/EpicMorg.SteamPathsLib/SteamPathsUtil.cs +++ b/src/EpicMorg.SteamPathsLib/SteamPathsUtil.cs @@ -1,12 +1,15 @@ using System; using System.Collections.Generic; -using EpicMorg.SteamPathsLib.model; +using System.IO; +using System.Linq; +using System.Text.RegularExpressions; +using EpicMorg.SteamPathsLib.Model; +using Gameloop.Vdf; +using Gameloop.Vdf.Linq; using Microsoft.Win32; -namespace EpicMorg.SteamPathsLib -{ - public class SteamPathsUtil - { +namespace EpicMorg.SteamPathsLib { + public class SteamPathsUtil { private static readonly string _valveKey = @"Software\Valve"; private static readonly string _valveSteamKey = @"Software\Valve\Steam"; private static readonly string _valveSteamAppsKey = @"Software\Valve\Steam\Apps"; @@ -14,58 +17,44 @@ namespace EpicMorg.SteamPathsLib private static readonly string _valveSteamAppsPattern = @"Software\Valve\Steam\Apps\"; - public static string GetValveKeyRegistry() - { - try - { + public static string GetValveKeyRegistry() { + try { return Registry.CurrentUser.OpenSubKey(_valveKey).ToString(); - } - catch (Exception) - { + } catch (Exception) { return null; }; } - public static string GetSteamAppsKeyRegistry() - { - try - { + public static string GetSteamAppsKeyRegistry() { + try { return Registry.CurrentUser.OpenSubKey(_valveSteamAppsKey).ToString(); - } - catch (Exception) - { + } catch (Exception) { return null; }; } - public static ActiveProcessSteamData GetActiveProcessSteamData() - { - try - { + public static ActiveProcessSteamRegistryData GetActiveProcessSteamData() { + try { var regData = Registry.CurrentUser.OpenSubKey(_valveActiveProcessPID); - var result = new ActiveProcessSteamData(); + var result = new ActiveProcessSteamRegistryData(); result.RegistryKey = regData.ToString(); result.PID = Convert.ToInt32((regData.GetValue("pid") ?? 0)); return result; - } - catch (Exception) - { + } catch (Exception) { return null; } } - public static SteamAppData GetSteamAppDataById(int appId) - { + public static SteamAppRegistryData GetSteamAppDataById(int appId) { var appKey = _valveSteamAppsPattern + appId; - try - { + try { var regData = Registry.CurrentUser.OpenSubKey(appKey); - var result = new SteamAppData(); + var result = new SteamAppRegistryData(); result.RegistryKey = regData.ToString(); result.Name = (regData.GetValue("Name") ?? "").ToString(); @@ -77,20 +66,16 @@ namespace EpicMorg.SteamPathsLib result.Runnig = (regData.GetValue("Running") ?? 0).Equals(1); return result; - } - catch (Exception) - { + } catch (Exception) { return null; } } - public static SteamData GetSteamData() - { - try - { + public static SteamRegistryData GetSteamData() { + try { var regData = Registry.CurrentUser.OpenSubKey(_valveSteamKey); - var result = new SteamData(); + var result = new SteamRegistryData(); result.RegistryKey = regData.ToString(); result.LastGameNameUsed = (regData.GetValue("LastGameNameUsed") ?? "").ToString(); @@ -107,11 +92,76 @@ namespace EpicMorg.SteamPathsLib result.AlreadyRetriedOfflineMode = (regData.GetValue("AlreadyRetriedOfflineMode") ?? 0).Equals(1); return result; - } - catch (Exception) - { + } catch (Exception) { return null; } } + public static ConfigSteamData GetSteamConfig() { + var result = new ConfigSteamData(); + var libraryPaths = new List(); + + var steamPath = new DirectoryInfo(GetSteamData().SteamPath).FullName; + var configPath = Path.Combine(steamPath, "config", "config.vdf"); + + libraryPaths.Add(Path.Combine(steamPath, "steamapps")); + + dynamic configObject = VdfConvert.Deserialize(File.ReadAllText(configPath)).Value; + var configLibraryPaths = ((VObject)configObject.Software.Valve.Steam) + .Children() + .Where(item => item.Key.StartsWith("BaseInstallFolder")) + .Select(item => item.Value.ToString()) + .Select(line => new DirectoryInfo(line).FullName) + .Select(line => Path.Combine(line, "steamapps")); + + libraryPaths.AddRange(configLibraryPaths); + + result.SteamLibraryFolders = libraryPaths.ToArray(); + + return result; + } + + public static List GetLibrarySteamDataList() { + var configData = GetSteamConfig(); + var result = new List(); + + foreach (var path in configData.SteamLibraryFolders) { + var libraryData = new LibrarySteamData(); + libraryData.Path = path; + libraryData.AppManifestDataList = new List(); + + var files = new DirectoryInfo(path).GetFiles("appmanifest_*.acf"); + + foreach (var file in files) { + var appData = new SteamAppManifestData(); + dynamic appManifestObject = VdfConvert.Deserialize(File.ReadAllText(file.FullName)).Value; + + appData.AppId = Convert.ToInt32(appManifestObject.appid.Value); + appData.Name = appManifestObject.name.Value; + appData.InstallDir = appManifestObject.installdir.Value; + + appData.Path = Path.Combine(path, "common", appData.InstallDir); + + libraryData.AppManifestDataList.Add(appData); + } + + result.Add(libraryData); + } + + return result; + } + + public static List GetAllSteamAppManifestData() { + var result = new List(); + + var libraries = GetLibrarySteamDataList(); + + foreach (var library in libraries) { + result.AddRange(library.AppManifestDataList); + } + + return result; + } + + public static SteamAppManifestData GetSteamAppManifestDataById(int appId) => GetAllSteamAppManifestData().FirstOrDefault(appData => appData.AppId == appId); } }