-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSteamLauncher.cs
102 lines (78 loc) · 3.11 KB
/
SteamLauncher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using Gamelib.Core.Util;
using GameLib.Core;
using GameLib.Plugin.Steam.Model;
using Microsoft.Win32;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
namespace GameLib.Plugin.Steam;
[Guid("5BB973D0-BF3D-4C3E-98B2-41AEFCB1506A")]
[Export(typeof(ILauncher))]
public class SteamLauncher : ILauncher
{
[ImportingConstructor]
public SteamLauncher(LauncherOptions? launcherOptions)
{
LauncherOptions = launcherOptions ?? new LauncherOptions();
}
#region Interface implementations
public LauncherOptions LauncherOptions { get; }
public Guid Id => GetType().GUID;
public string Name => "Steam";
public Image Logo => Properties.Resources.Logo512px;
public bool IsInstalled { get; private set; }
public bool IsRunning => ProcessUtil.IsProcessRunning(Executable);
public string InstallDir { get; private set; } = string.Empty;
public string Executable { get; private set; } = string.Empty;
public Icon? ExecutableIcon => PathUtil.GetFileIcon(Executable);
public IEnumerable<IGame> Games { get; private set; } = Enumerable.Empty<IGame>();
public bool Start() => IsRunning || ProcessUtil.StartProcess(Executable);
public void Stop()
{
if (IsRunning)
{
Process.Start(Executable, "-shutdown");
}
}
public void Refresh(CancellationToken cancellationToken = default)
{
Executable = string.Empty;
InstallDir = string.Empty;
IsInstalled = false;
Libraries = Enumerable.Empty<SteamLibrary>();
Games = Enumerable.Empty<IGame>();
Executable = GetExecutable() ?? string.Empty;
if (!string.IsNullOrEmpty(Executable))
{
InstallDir = Path.GetDirectoryName(Executable) ?? string.Empty;
IsInstalled = File.Exists(Executable);
Libraries = SteamLibraryFactory.GetLibraries(InstallDir);
Games = SteamGameFactory.GetGames(this, Libraries, cancellationToken);
}
}
#endregion
#region Public properties
public IEnumerable<SteamLibrary> Libraries { get; private set; } = Enumerable.Empty<SteamLibrary>();
#endregion
#region Private methods
private static string? GetExecutable()
{
string? executablePath = null;
executablePath ??= RegistryUtil.GetShellCommand("steam");
executablePath ??= RegistryUtil.GetValue(RegistryHive.CurrentUser, @"Software\Valve\Steam", "SteamExe");
executablePath ??= RegistryUtil.GetValue(RegistryHive.CurrentUser, @"Software\Valve\Steam", "SteamPath");
executablePath ??= RegistryUtil.GetValue(RegistryHive.LocalMachine, @"Software\Valve\Steam", "InstallPath");
executablePath = PathUtil.Sanitize(executablePath);
if (!string.IsNullOrEmpty(executablePath) && !PathUtil.IsExecutable(executablePath))
{
executablePath = Path.Combine(executablePath, "steam.exe");
}
if (!PathUtil.IsExecutable(executablePath))
{
executablePath = null;
}
return executablePath;
}
#endregion
}