-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
123 lines (112 loc) · 5.55 KB
/
Program.cs
File metadata and controls
123 lines (112 loc) · 5.55 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
using Avalonia;
using Avalonia.ReactiveUI;
using System;
using System.Text;
namespace LuckyLilliaDesktop;
class Program
{
[STAThread]
public static void Main(string[] args)
{
try
{
// 开机自启时工作目录为 System32,需要切换到 exe 所在目录
var exeDir = AppContext.BaseDirectory;
if (!string.IsNullOrEmpty(exeDir))
{
// macOS .app bundle 特殊处理
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX))
{
// 检查是否在 .app bundle 中运行
if (exeDir.Contains(".app/Contents/MacOS"))
{
// 从 /path/to/App.app/Contents/MacOS 提取 .app 所在目录
var appBundlePath = exeDir.Substring(0, exeDir.IndexOf(".app/Contents/MacOS"));
var parentDir = System.IO.Path.GetDirectoryName(appBundlePath);
// 如果在 /Applications 目录,使用标准的 Application Support 目录
if (!string.IsNullOrEmpty(parentDir) && parentDir == "/Applications")
{
var homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var appSupportDir = System.IO.Path.Combine(homeDir, "Library", "Application Support", "LuckyLilliaDesktop");
// 确保目录存在
if (!System.IO.Directory.Exists(appSupportDir))
{
System.IO.Directory.CreateDirectory(appSupportDir);
}
Environment.CurrentDirectory = appSupportDir;
}
else if (!string.IsNullOrEmpty(parentDir) && parentDir.Contains("/AppTranslocation/"))
{
// macOS App Translocation:尝试获取原始路径,去除隔离标记后重启
var translocatedAppPath = exeDir.Substring(0, exeDir.IndexOf(".app/Contents/MacOS")) + ".app";
var originalAppPath = Utils.AppTranslocationHelper.GetOriginalPath(translocatedAppPath);
if (!string.IsNullOrEmpty(originalAppPath) && originalAppPath != translocatedAppPath)
{
// 去除隔离标记
var xattr = new System.Diagnostics.Process();
xattr.StartInfo.FileName = "/usr/bin/xattr";
xattr.StartInfo.Arguments = $"-cr \"{originalAppPath}\"";
xattr.StartInfo.UseShellExecute = false;
xattr.StartInfo.CreateNoWindow = true;
xattr.Start();
xattr.WaitForExit();
// 从原始路径重新启动
System.Diagnostics.Process.Start("/usr/bin/open", $"\"{originalAppPath}\"");
Environment.Exit(0);
return;
}
// 获取原始路径失败时,回退到 Application Support
var homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var appSupportDir = System.IO.Path.Combine(homeDir, "Library", "Application Support", "LuckyLilliaDesktop");
if (!System.IO.Directory.Exists(appSupportDir))
System.IO.Directory.CreateDirectory(appSupportDir);
Environment.CurrentDirectory = appSupportDir;
}
else if (!string.IsNullOrEmpty(parentDir) && System.IO.Directory.Exists(parentDir))
{
// 其他位置:使用 .app 的父目录(自定义工作区)
Environment.CurrentDirectory = parentDir;
}
else
{
// 后备方案
Environment.CurrentDirectory = exeDir;
}
}
else
{
// 开发环境:使用 exe 所在目录
Environment.CurrentDirectory = exeDir;
}
}
else
{
Environment.CurrentDirectory = exeDir;
}
}
try
{
Console.OutputEncoding = Encoding.UTF8;
Console.InputEncoding = Encoding.UTF8;
}
catch { }
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
}
catch (Exception ex)
{
System.IO.File.WriteAllText("crash.log", $"{DateTime.Now}: {ex}");
throw;
}
}
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.With(new Win32PlatformOptions
{
// 使用软件渲染,避免低配机器GPU加速导致高CPU占用
RenderingMode = new[] { Win32RenderingMode.Software }
})
.WithInterFont()
.LogToTrace()
.UseReactiveUI();
}