forked from BeyondDimension/SteamTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
299 lines (278 loc) · 11.9 KB
/
Copy pathProgram.cs
File metadata and controls
299 lines (278 loc) · 11.9 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
using System.Diagnostics;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using NuGet.Versioning;
using Microsoft.Win32;
using Microsoft.DotNet.Tools.Uninstall.Windows;
using Microsoft.DotNet.Tools.Uninstall.Shared.BundleInfo;
using FDELauncher.Properties;
using _ThisAssembly = System.Properties.ThisAssembly;
using R = FDELauncher.Properties.Resources;
namespace FDELauncher;
internal static class Program
{
const string ExecutiveName = "Steam++";
static BundleArch matchArch;
static readonly SemanticVersion runtimeVersion = new(6, 0, 8);
static readonly SemanticVersion sdkVersion1 = new(6, 0, 108);
static readonly SemanticVersion sdkVersion3 = new(6, 0, 303);
static readonly SemanticVersion sdkVersion4 = new(6, 0, 400);
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static int Main(string[] args)
{
try
{
if (IsProgramInCompatibilityMode())
{
MessageBox.Show(R.ProgramInCompatibilityModeError, R.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
return 0;
}
if (args.Length == 1 && args[0] == "--query")
{
var installed = RegistryQuery.GetAllInstalledBundles().ToArray();
var text = string.Join(Environment.NewLine, installed.Select(x => $"{x.DisplayName} [{x.Version.Type} {x.Version} {ToString(x.Arch)}]").ToArray());
MessageBox.Show(text, _ThisAssembly.AssemblyTrademark);
return 0;
}
if (!IsSupportedPlatform(out var error)) return ShowError(error);
var executivePath = GetExecutivePath();
if (!File.Exists(executivePath)) return ShowError(R.ExecutiveNotExistsFailure);
if (!VerificationExecutiveInfo(executivePath)) return ShowError(R.VerificationExecutiveInfoFailure);
if (IsFrameworkDependentExecutable())
{
matchArch = GetArchByPeHeader(executivePath);
switch (matchArch)
{
case BundleArch.X64:
if (!Environment2.Is64BitOperatingSystem) return ShowError(R.ThisAppOnlySupport64BitOS);
break;
case BundleArch.Arm64:
if (RuntimeInformation2.OSArchitecture != Architecture.Arm64) throw new PlatformNotSupportedException();
break;
}
if (IsRuntimeInstalled(matchArch, runtimeVersion, sdkVersion1, sdkVersion3, sdkVersion4))
{
Run();
}
else
{
ShowRuntimeMissingFailure();
}
}
else
{
Run();
}
return 0;
void Run() => StartProcess(executivePath, args);
}
catch (Exception ex)
{
return ShowError(ex.ToString());
}
}
static int ShowError(string errMsg, int errCode = 0)
{
MessageBox.Show(errMsg, _ThisAssembly.AssemblyTrademark, MessageBoxButtons.OK, MessageBoxIcon.Error);
return errCode;
}
static bool IsSupportedPlatform([NotNullWhen(false)] out string? error)
{
error = null;
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
var osVersion = Environment.OSVersion.Version;
if (osVersion.Major > 6) return true;
if (osVersion.Major == 6)
{
if (osVersion.Minor == 1) // NT 6.1 / Win7 / WinServer 2008 R2
{
if (Environment.OSVersion.ServicePack == "Service Pack 1")
return true;
}
else if (osVersion.Minor == 2) // NT 6.2 / Win8 / WinServer 2012
{
error = R.NotSupportedWin8PlatformError;
return false;
}
else if (osVersion.Minor == 3) // NT 6.3 / Win8.1 / WinServer 2012 R2
{
return true;
}
}
}
error = R.NotSupportedPlatformError;
return false;
}
static string ToString(BundleArch value) => value switch
{
BundleArch.X86 => "x86",
BundleArch.X64 => "x64",
BundleArch.Arm64 => "Arm64",
_ => value.ToString(),
};
static BundleArch GetArchByPeHeader(string filePath)
{
var imageFileHeader = PeHeaderReader.ReadImageFileHeader(filePath);
var machine = imageFileHeader.Machine;
return machine switch
{
PeHeaderReader.IMAGE_FILE_MACHINE_ARM64 => BundleArch.Arm64,
PeHeaderReader.IMAGE_FILE_MACHINE_I386 => BundleArch.X86,
PeHeaderReader.IMAGE_FILE_MACHINE_IA64 or PeHeaderReader.IMAGE_FILE_MACHINE_AMD64 => BundleArch.X64,
_ => throw new ArgumentOutOfRangeException(nameof(machine), machine, null),
};
}
/// <summary>
/// 未安装运行时或 SDK 的弹窗提示
/// </summary>
static void ShowRuntimeMissingFailure()
{
var archStr = ToString(matchArch);
var runtimeVersionStr = runtimeVersion.ToString();
var _AspNetCoreRuntime = String2.TryFormat(
R.AspNetCoreRuntimeFormat2,
runtimeVersionStr,
archStr);
var _NetRuntime = String2.TryFormat(
R.NetRuntimeFormat2,
runtimeVersionStr,
archStr);
var _Runtime = $"{_AspNetCoreRuntime} {R.And} {_NetRuntime}";
var text = String2.TryFormat(
R.RuntimeMissingFailureFormat2,
_ThisAssembly.AssemblyTrademark,
_Runtime);
var result = MessageBox.Show(text, _ThisAssembly.AssemblyTrademark, MessageBoxButtons.YesNo, MessageBoxIcon.Error);
if (result == DialogResult.Yes)
{
const string urlFormat3 = "https://" + "dotnet.microsoft.com/{0}/download/dotnet/{1}.{2}";
var url = string.Format(urlFormat3, GetLang(), runtimeVersion.Major, runtimeVersion.Minor);
OpenCoreByProcess(url);
}
}
static string GetLang() => R.GetString(l => l switch
{
Language.ChineseSimplified => "zh-cn",
Language.Japanese => "ja-jp",
_ => "en-us",
});
static bool OpenCoreByProcess(string url)
{
return Process2.OpenCoreByProcess(url, OnError);
static void OnError(Exception e)
{
string text;
if (e is Win32Exception win32Ex)
{
text = String2.TryFormat(R.OpenCoreByProcessOnExceptionFormat1, $" 0x{Convert.ToString(win32Ex.NativeErrorCode, 16)}");
}
else
{
text = String2.TryFormat(R.OpenCoreByProcessOnExceptionFormat1, string.Empty) + Environment.NewLine + e.ToString();
}
MessageBox.Show(text, _ThisAssembly.AssemblyTrademark, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 是否已安装运行时或 SDK
/// </summary>
/// <returns></returns>
static bool IsRuntimeInstalled(BundleArch matchArch, SemanticVersion runtimeVersion, SemanticVersion sdkVersion1, SemanticVersion sdkVersion3, SemanticVersion sdkVersion4)
{
// ArchiSteamFarm 依赖 ASP.NET Core
// ASP.NET Core 运行时安装程序除 Hosting Bundle 不包含 .NET 运行时 缺少文件 C:\Program Files\dotnet\host\fxr\{version}\hostfxr.dll
// 缺少 hostfxr.dll 会提示 To run this application, you must install .NET Desktop Runtime
// https://github.com/dotnet/runtime/blob/v6.0.5/src/native/corehost/apphost/apphost.windows.cpp#L62-L64
// https://github.com/dotnet/runtime/blob/v6.0.5/src/native/corehost/apphost/apphost.windows.cpp#L106-L113
var installed = RegistryQuery.GetAllInstalledBundles().Where(m => m.Arch.HasFlags(matchArch)).ToArray();
var query_sdk = installed.Where(m => m.Version.Type == BundleType.Sdk &&
(
(m.Version.SemVer >= sdkVersion1 && m.Version.SemVer < new SemanticVersion(sdkVersion1.Major, sdkVersion1.Minor, 199)) ||
(m.Version.SemVer >= sdkVersion3 && m.Version.SemVer < new SemanticVersion(sdkVersion3.Major, sdkVersion3.Minor, 399)) ||
(m.Version.SemVer >= sdkVersion4 && m.Version.SemVer < new SemanticVersion(sdkVersion4.Major, sdkVersion4.Minor, 499))
));
if (query_sdk.Any()) return true;
var query_runtime = installed.Where(m => (m.Version.Type == BundleType.Runtime || m.Version.Type == BundleType.WindowsDesktopRuntime) && m.Version.SemVer >= runtimeVersion);
if (!query_runtime.Any()) return false;
var query_aspnetruntime = installed.Where(m => m.Version.Type == BundleType.AspNetRuntime && m.Version.SemVer >= runtimeVersion);
if (!query_aspnetruntime.Any()) return false;
return true;
}
/// <summary>
/// 获取主程序执行文件路径
/// </summary>
/// <returns></returns>
static string GetExecutivePath()
{
const string ExecutiveNameWithExtension = $"{ExecutiveName}.exe";
var executivePath = Path.Combine(Application.StartupPath, ExecutiveNameWithExtension);
return executivePath;
}
/// <summary>
/// 验证执行文件信息
/// </summary>
/// <param name="executivePath"></param>
/// <returns></returns>
static bool VerificationExecutiveInfo(string executivePath)
{
const string OLD_AssemblyProduct = "SteamTools";
const string OLD_AssemblyTrademark = "Steam++";
const string OLD_AssemblyDescription = "「Steam++ 工具箱」是一个开源跨平台的多功能游戏工具箱。";
var fileVersionInfo = FileVersionInfo.GetVersionInfo(executivePath);
var result = (fileVersionInfo.Comments == _ThisAssembly.AssemblyDescription || fileVersionInfo.Comments == OLD_AssemblyDescription) &&
fileVersionInfo.CompanyName == _ThisAssembly.AssemblyCompany &&
(fileVersionInfo.FileDescription == _ThisAssembly.AssemblyTrademark || fileVersionInfo.FileDescription == OLD_AssemblyTrademark) &&
(fileVersionInfo.LegalTrademarks == _ThisAssembly.AssemblyTrademark || fileVersionInfo.LegalTrademarks == OLD_AssemblyTrademark) &&
fileVersionInfo.LegalCopyright == _ThisAssembly.AssemblyCopyright &&
(fileVersionInfo.ProductName == _ThisAssembly.AssemblyProduct || fileVersionInfo.ProductName == OLD_AssemblyProduct);
return result;
}
static bool IsFrameworkDependentExecutable()
{
return true;
//var dllPath = new[] {
// Application.StartupPath,
// "Bin",
// "coreclr.dll",
//}.Aggregate(Path.Combine);
//return !File.Exists(dllPath);
}
static void StartProcess(string executivePath, string[] args)
{
Process.Start(new ProcessStartInfo
{
FileName = executivePath,
Arguments = string.Join(" ", args),
UseShellExecute = false,
});
}
static bool IsProgramInCompatibilityMode()
{
try
{
foreach (var item in new[] { Registry.CurrentUser, Registry.LocalMachine })
{
using var layers = item.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers");
var value = layers?.GetValue(Environment2.ProcessPath)?.ToString();
if (value != null)
{
value = value.ToUpperInvariant();
if (value.Contains("WIN8")) return true;
if (value.Contains("WIN7")) return true;
if (value.Contains("VISTA")) return true;
if (value.Contains("WINXP")) return true;
}
}
}
catch
{
}
return false;
}
}