|
3 | 3 | using System.Runtime.InteropServices; |
4 | 4 | using Microsoft.Win32; |
5 | 5 |
|
6 | | -namespace SAM.API |
| 6 | +namespace SAM.API; |
| 7 | + |
| 8 | +public static class Steam |
7 | 9 | { |
8 | | - public static class Steam |
9 | | - { |
10 | | - private static string _installPath; |
| 10 | + private static string _installPath; |
11 | 11 |
|
12 | | - private static nint _handle = nint.Zero; |
13 | | - private static NativeCreateInterface _callCreateInterface; |
14 | | - private static NativeSteamGetCallback _callSteamBGetCallback; |
15 | | - private static NativeSteamFreeLastCallback _callSteamFreeLastCallback; |
| 12 | + private static nint _handle = nint.Zero; |
| 13 | + private static NativeCreateInterface _callCreateInterface; |
| 14 | + private static NativeSteamGetCallback _callSteamBGetCallback; |
| 15 | + private static NativeSteamFreeLastCallback _callSteamFreeLastCallback; |
16 | 16 |
|
17 | | - private static string SteamClientDll => Environment.Is64BitProcess ? @"steamclient64.dll" : @"steamclient.dll"; |
| 17 | + private static string SteamClientDll => Environment.Is64BitProcess ? @"steamclient64.dll" : @"steamclient.dll"; |
18 | 18 |
|
19 | | - private static Delegate GetExportDelegate<TDelegate>(nint module, string name) |
20 | | - { |
21 | | - var address = Native.GetProcAddress(module, name); |
22 | | - return address == nint.Zero ? null : Marshal.GetDelegateForFunctionPointer(address, typeof(TDelegate)); |
23 | | - } |
| 19 | + private static Delegate GetExportDelegate<TDelegate>(nint module, string name) |
| 20 | + { |
| 21 | + var address = Native.GetProcAddress(module, name); |
| 22 | + return address == nint.Zero ? null : Marshal.GetDelegateForFunctionPointer(address, typeof(TDelegate)); |
| 23 | + } |
24 | 24 |
|
25 | | - private static TDelegate GetExportFunction<TDelegate>(nint module, string name) |
26 | | - where TDelegate : class |
27 | | - { |
28 | | - return GetExportDelegate<TDelegate>(module, name) as TDelegate; |
29 | | - } |
| 25 | + private static TDelegate GetExportFunction<TDelegate>(nint module, string name) |
| 26 | + where TDelegate : class |
| 27 | + { |
| 28 | + return GetExportDelegate<TDelegate>(module, name) as TDelegate; |
| 29 | + } |
30 | 30 |
|
31 | | - public static string GetInstallPath() |
32 | | - { |
33 | | - if (!string.IsNullOrEmpty(_installPath)) return _installPath; |
| 31 | + public static string GetInstallPath() |
| 32 | + { |
| 33 | + if (!string.IsNullOrEmpty(_installPath)) return _installPath; |
34 | 34 |
|
35 | | - // TODO: consider switching this to HKCU:\SOFTWARE\Valve\Steam\ActiveProcess\SteamClientDll |
36 | | - using var view32 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32); |
37 | | - using var clsid32 = view32.OpenSubKey(@"Software\Valve\Steam", false); |
| 35 | + // TODO: consider switching this to HKCU:\SOFTWARE\Valve\Steam\ActiveProcess\SteamClientDll |
| 36 | + using var view32 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32); |
| 37 | + using var clsid32 = view32.OpenSubKey(@"Software\Valve\Steam", false); |
38 | 38 |
|
39 | | - _installPath = (string) clsid32?.GetValue(@"InstallPath"); |
40 | | - return _installPath; |
| 39 | + _installPath = (string) clsid32?.GetValue(@"InstallPath"); |
| 40 | + return _installPath; |
41 | 41 |
|
42 | | - //return (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Valve\Steam", "InstallPath", null); |
43 | | - } |
| 42 | + //return (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Valve\Steam", "InstallPath", null); |
| 43 | + } |
44 | 44 |
|
45 | | - public static TClass CreateInterface<TClass>(string version) |
46 | | - where TClass : INativeWrapper, new() |
47 | | - { |
48 | | - var address = _callCreateInterface(version, nint.Zero); |
| 45 | + public static TClass CreateInterface<TClass>(string version) |
| 46 | + where TClass : INativeWrapper, new() |
| 47 | + { |
| 48 | + var address = _callCreateInterface(version, nint.Zero); |
49 | 49 |
|
50 | | - if (address == nint.Zero) return default; |
| 50 | + if (address == nint.Zero) return default; |
51 | 51 |
|
52 | | - var rez = new TClass(); |
53 | | - rez.SetupFunctions(address); |
54 | | - return rez; |
55 | | - } |
| 52 | + var rez = new TClass(); |
| 53 | + rez.SetupFunctions(address); |
| 54 | + return rez; |
| 55 | + } |
56 | 56 |
|
57 | | - public static bool GetCallback(int pipe, out CallbackMessage message, out int call) |
58 | | - { |
59 | | - return _callSteamBGetCallback(pipe, out message, out call); |
60 | | - } |
| 57 | + public static bool GetCallback(int pipe, out CallbackMessage message, out int call) |
| 58 | + { |
| 59 | + return _callSteamBGetCallback(pipe, out message, out call); |
| 60 | + } |
61 | 61 |
|
62 | | - public static bool FreeLastCallback(int pipe) |
63 | | - { |
64 | | - return _callSteamFreeLastCallback(pipe); |
65 | | - } |
| 62 | + public static bool FreeLastCallback(int pipe) |
| 63 | + { |
| 64 | + return _callSteamFreeLastCallback(pipe); |
| 65 | + } |
66 | 66 |
|
67 | | - public static bool Load() |
| 67 | + public static bool Load() |
| 68 | + { |
| 69 | + if (_handle != nint.Zero) |
68 | 70 | { |
69 | | - if (_handle != nint.Zero) |
70 | | - { |
71 | | - Native.FreeLibrary(_handle); |
72 | | - _handle = nint.Zero; |
73 | | - } |
| 71 | + Native.FreeLibrary(_handle); |
| 72 | + _handle = nint.Zero; |
| 73 | + } |
74 | 74 |
|
75 | | - var path = GetInstallPath(); |
76 | | - if (path == null) return false; |
| 75 | + var path = GetInstallPath(); |
| 76 | + if (path == null) return false; |
77 | 77 |
|
78 | | - Native.SetDllDirectory(path + ";" + Path.Combine(path, "bin")); |
79 | | - path = Path.Combine(path, SteamClientDll); |
| 78 | + Native.SetDllDirectory(path + ";" + Path.Combine(path, "bin")); |
| 79 | + path = Path.Combine(path, SteamClientDll); |
80 | 80 |
|
81 | | - var module = Native.LoadLibraryEx(path, nint.Zero, Native.LoadWithAlteredSearchPath); |
82 | | - if (module == nint.Zero) return false; |
| 81 | + var module = Native.LoadLibraryEx(path, nint.Zero, Native.LoadWithAlteredSearchPath); |
| 82 | + if (module == nint.Zero) return false; |
83 | 83 |
|
84 | | - _callCreateInterface = GetExportFunction<NativeCreateInterface>(module, "CreateInterface"); |
85 | | - if (_callCreateInterface == null) return false; |
| 84 | + _callCreateInterface = GetExportFunction<NativeCreateInterface>(module, "CreateInterface"); |
| 85 | + if (_callCreateInterface == null) return false; |
86 | 86 |
|
87 | | - _callSteamBGetCallback = GetExportFunction<NativeSteamGetCallback>(module, "Steam_BGetCallback"); |
88 | | - if (_callSteamBGetCallback == null) return false; |
| 87 | + _callSteamBGetCallback = GetExportFunction<NativeSteamGetCallback>(module, "Steam_BGetCallback"); |
| 88 | + if (_callSteamBGetCallback == null) return false; |
89 | 89 |
|
90 | | - _callSteamFreeLastCallback = GetExportFunction<NativeSteamFreeLastCallback>(module, "Steam_FreeLastCallback"); |
91 | | - if (_callSteamFreeLastCallback == null) return false; |
| 90 | + _callSteamFreeLastCallback = GetExportFunction<NativeSteamFreeLastCallback>(module, "Steam_FreeLastCallback"); |
| 91 | + if (_callSteamFreeLastCallback == null) return false; |
92 | 92 |
|
93 | | - _handle = module; |
94 | | - return true; |
95 | | - } |
| 93 | + _handle = module; |
| 94 | + return true; |
| 95 | + } |
96 | 96 |
|
97 | | - // TODO: replace with CSWin32's PInvoke |
98 | | - private struct Native |
99 | | - { |
100 | | - [DllImport("kernel32.dll", SetLastError = true, BestFitMapping = false, ThrowOnUnmappableChar = true)] |
101 | | - internal static extern nint GetProcAddress(nint module, string name); |
| 97 | + // TODO: replace with CSWin32's PInvoke |
| 98 | + private struct Native |
| 99 | + { |
| 100 | + [DllImport("kernel32.dll", SetLastError = true, BestFitMapping = false, ThrowOnUnmappableChar = true)] |
| 101 | + internal static extern nint GetProcAddress(nint module, string name); |
102 | 102 |
|
103 | | - [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] |
104 | | - internal static extern nint LoadLibraryEx(string path, nint file, uint flags); |
| 103 | + [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] |
| 104 | + internal static extern nint LoadLibraryEx(string path, nint file, uint flags); |
105 | 105 |
|
106 | | - [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] |
107 | | - [return: MarshalAs(UnmanagedType.Bool)] |
108 | | - internal static extern bool SetDllDirectory(string path); |
| 106 | + [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] |
| 107 | + [return: MarshalAs(UnmanagedType.Bool)] |
| 108 | + internal static extern bool SetDllDirectory(string path); |
109 | 109 |
|
110 | | - [DllImport("kernel32.dll", SetLastError = true)] |
111 | | - public static extern bool FreeLibrary(nint hModule); |
| 110 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 111 | + public static extern bool FreeLibrary(nint hModule); |
112 | 112 |
|
113 | | - internal const uint LoadWithAlteredSearchPath = 8; |
114 | | - } |
| 113 | + internal const uint LoadWithAlteredSearchPath = 8; |
| 114 | + } |
115 | 115 |
|
116 | | - [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)] |
117 | | - private delegate nint NativeCreateInterface(string version, nint returnCode); |
| 116 | + [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)] |
| 117 | + private delegate nint NativeCreateInterface(string version, nint returnCode); |
118 | 118 |
|
119 | | - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
120 | | - [return: MarshalAs(UnmanagedType.I1)] |
121 | | - private delegate bool NativeSteamGetCallback(int pipe, out CallbackMessage message, out int call); |
| 119 | + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
| 120 | + [return: MarshalAs(UnmanagedType.I1)] |
| 121 | + private delegate bool NativeSteamGetCallback(int pipe, out CallbackMessage message, out int call); |
122 | 122 |
|
123 | | - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
124 | | - [return: MarshalAs(UnmanagedType.I1)] |
125 | | - private delegate bool NativeSteamFreeLastCallback(int pipe); |
126 | | - } |
| 123 | + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
| 124 | + [return: MarshalAs(UnmanagedType.I1)] |
| 125 | + private delegate bool NativeSteamFreeLastCallback(int pipe); |
127 | 126 | } |
0 commit comments