-
Notifications
You must be signed in to change notification settings - Fork 300
Expand file tree
/
Copy pathProgram.cs
More file actions
104 lines (91 loc) · 2.89 KB
/
Copy pathProgram.cs
File metadata and controls
104 lines (91 loc) · 2.89 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
using System.Diagnostics;
using System.Reflection;
#if Platforms_Windows
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.UI.WindowsAndMessaging;
#endif
#if Platforms_Linux
using Mono.Unix;
#endif
// 获取对应平台的可执行文件路径
string executableName;
if (OperatingSystem.IsWindows())
{
executableName = "ClassIsland.Desktop.exe";
} else if (OperatingSystem.IsLinux())
{
executableName = "ClassIsland.Desktop";
}
else
{
ShowError("ClassIsland 正在不支持的平台上运行,无法继续。");
return 1;
}
var root = Path.GetFullPath(Path.GetDirectoryName(Environment.ProcessPath) ?? "");
var installation = Directory.GetDirectories(root)
.Where(x => Path.GetFileName(x).StartsWith("app") &&
!(File.Exists(Path.Combine(x, ".destroy")) || File.Exists(Path.Combine(x, ".partial"))) &&
File.Exists(Path.Combine(x, executableName)))
.OrderBy(x => File.Exists(Path.Combine(x, ".current")) ? 1 : 0)
.ThenByDescending(x =>
{
var filename = Path.GetFileName(x);
var split = filename.Split('-');
if (split.Length <= 1)
{
return new Version();
}
return Version.TryParse(split[1], out var version) ? version : new Version();
})
.ThenByDescending(x =>
{
var filename = Path.GetFileName(x);
var split = filename.Split('-');
if (split.Length <= 2)
{
return 0;
}
return int.TryParse(split[2], out var n) ? n : 0;
})
.FirstOrDefault();
if (installation == null)
{
ShowError("找不到有效的 ClassIsland 版本,可能是安装已损坏。请在 https://classisland.tech/download 重新下载并安装 ClassIsland。");
return 1;
}
var exePath = Path.Combine(Path.Combine(installation, executableName));
// 自动添加可执行权限,防止出现无法运行的问题 https://github.com/ClassIsland/ClassIsland/issues/1212
#if Platforms_Linux
try
{
var unixFileInfo = new UnixFileInfo(exePath);
unixFileInfo.FileAccessPermissions |= FileAccessPermissions.UserExecute | FileAccessPermissions.GroupExecute |
FileAccessPermissions.OtherExecute;
}
catch (Exception e)
{
Console.Error.WriteLine($"无法设置可执行文件 {exePath} 的执行权限,可能导致应用无法启动:{e}");
}
#endif
var startInfo = new ProcessStartInfo()
{
FileName = exePath,
WorkingDirectory = root
};
foreach (var i in args)
{
startInfo.ArgumentList.Add(i);
}
startInfo.EnvironmentVariables["ClassIsland_PackageRoot"] = root; // 防止因环境变量已设置导致启动器崩溃
Process.Start(startInfo);
return 0;
void ShowError(string message)
{
#if Platforms_Windows
PInvoke.MessageBox(HWND.Null, message,
"ClassIsland", MESSAGEBOX_STYLE.MB_APPLMODAL | MESSAGEBOX_STYLE.MB_ICONSTOP);
#else
Console.Error.WriteLine(message);
#endif
}