forked from srwh1234/L1MapViewer
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathProgram.cs
More file actions
216 lines (196 loc) · 8.82 KB
/
Program.cs
File metadata and controls
216 lines (196 loc) · 8.82 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
using System.Text;
using System.IO;
using L1FlyMapViewer;
using L1MapViewer;
using L1MapViewer.CLI;
using L1MapViewer.Helper;
using L1MapViewer.Localization;
using System.Diagnostics;
using Eto;
using Eto.Forms;
using Path = System.IO.Path;
using File = System.IO.File;
namespace L1MapViewerCore;
static class Program
{
// 效能 Log 開關(供 MapForm 讀取)
public static bool PerfLogEnabled { get; private set; } = false;
// 全域啟動計時器
public static Stopwatch StartupStopwatch { get; } = Stopwatch.StartNew();
[STAThread]
static int Main(string[] args)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
// 初始化閃退報告機制(最優先)
CrashReporter.Initialize();
CrashReporter.ClearOldLogs();
// 初始化 Debug Log(每次啟動清除舊 log)
DebugLog.Clear();
DebugLog.Log($"[PROGRAM] Version: {System.Reflection.Assembly.GetExecutingAssembly().GetName().Version}");
DebugLog.Log($"[PROGRAM] Args: {string.Join(" ", args)}");
// 檢查是否啟用效能 Log
var argsList = args.ToList();
if (argsList.Contains("--perf-log"))
{
PerfLogEnabled = true;
argsList.Remove("--perf-log");
args = argsList.ToArray();
LogPerf("[PROGRAM] PerfLog enabled");
}
// 檢查是否為 CLI 模式
if (args.Length > 0 && args[0].ToLower() == "-cli")
{
return CliHandler.Execute(args);
}
// GUI 模式
LogPerf("[PROGRAM] Starting GUI mode");
// 處理命令列參數 - 如果傳入資料夾路徑,設定為天堂路徑
if (args.Length > 0 && Directory.Exists(args[0]))
{
string folderPath = args[0];
// 檢查是否為有效的天堂資料夾(包含 map 子資料夾)
string mapFolder = Path.Combine(folderPath, "map");
if (Directory.Exists(mapFolder))
{
L1MapViewer.Share.LineagePath = folderPath;
DebugLog.Log($"[PROGRAM] Set LineagePath from args: {folderPath}");
}
else
{
DebugLog.Log($"[PROGRAM] Folder doesn't contain 'map' subfolder: {folderPath}");
}
}
// 初始化多語言支援
LogPerf("[PROGRAM] Initializing localization...");
LocalizationManager.Initialize();
LogPerf("[PROGRAM] Localization initialized: " + LocalizationManager.CurrentLanguage);
// 初始化 Eto.Forms 平台
DebugLog.Log("[PROGRAM] Initializing Eto.Forms platform...");
Platform platform;
try
{
#if MACOS_NATIVE
// net10.0-macos build: force native macOS platform to avoid Platform.Detect picking GTK
DebugLog.Log("[PROGRAM] Using MACOS_NATIVE path");
var basePath = AppContext.BaseDirectory;
DebugLog.Log($"[PROGRAM] BaseDirectory: {basePath}");
var assemblyPath = Path.Combine(basePath, "Eto.macOS.dll");
DebugLog.Log($"[PROGRAM] Loading: {assemblyPath} (exists: {File.Exists(assemblyPath)})");
var macPlatformAssembly = System.Reflection.Assembly.LoadFrom(assemblyPath);
var platformType = macPlatformAssembly.GetType("Eto.Mac.Platform");
platform = (Platform)Activator.CreateInstance(platformType!)!;
#else
platform = Platform.Detect;
#endif
}
catch (InvalidOperationException)
{
// Platform.Detect failed, try to load platform manually based on OS
var basePath = AppContext.BaseDirectory;
if (OperatingSystem.IsMacOS())
{
// Try native macOS platform first, fall back to GTK
try
{
var assemblyPath = Path.Combine(basePath, "Eto.macOS.dll");
if (File.Exists(assemblyPath))
{
var macPlatformAssembly = System.Reflection.Assembly.LoadFrom(assemblyPath);
var platformType = macPlatformAssembly.GetType("Eto.Mac.Platform");
platform = (Platform)Activator.CreateInstance(platformType!)!;
}
else
{
throw new FileNotFoundException("Eto.macOS.dll not found");
}
}
catch (Exception ex)
{
// Fall back to GTK on macOS (requires GTK installed via Homebrew: brew install gtk+3)
Console.WriteLine($"Note: Native macOS platform failed ({ex.GetType().Name}). Trying GTK backend...");
Console.WriteLine($" Detail: {ex.Message}");
if (ex.InnerException != null)
Console.WriteLine($" Inner: {ex.InnerException}");
Console.WriteLine("For native look, install: sudo dotnet workload install macos");
Console.WriteLine("For GTK backend, install: brew install gtk+3");
try
{
var gtkPath = Path.Combine(basePath, "Eto.Gtk.dll");
var gtkPlatformAssembly = System.Reflection.Assembly.LoadFrom(gtkPath);
var platformType = gtkPlatformAssembly.GetType("Eto.GtkSharp.Platform");
platform = (Platform)Activator.CreateInstance(platformType!)!;
}
catch (Exception gtkEx)
{
Console.WriteLine();
Console.WriteLine("ERROR: Could not initialize any UI platform on macOS.");
Console.WriteLine();
Console.WriteLine("Please install ONE of the following:");
Console.WriteLine(" Option 1 (Native): sudo dotnet workload install macos");
Console.WriteLine(" Option 2 (GTK): brew install gtk+3");
Console.WriteLine();
throw new InvalidOperationException($"No UI platform available. Native: {ex.Message}, GTK: {gtkEx.Message}");
}
}
}
else if (OperatingSystem.IsWindows())
{
var assemblyPath = Path.Combine(basePath, "Eto.Wpf.dll");
var wpfPlatformAssembly = System.Reflection.Assembly.LoadFrom(assemblyPath);
var platformType = wpfPlatformAssembly.GetType("Eto.Wpf.Platform");
platform = (Platform)Activator.CreateInstance(platformType!)!;
}
else if (OperatingSystem.IsLinux())
{
var assemblyPath = Path.Combine(basePath, "Eto.Gtk.dll");
var gtkPlatformAssembly = System.Reflection.Assembly.LoadFrom(assemblyPath);
var platformType = gtkPlatformAssembly.GetType("Eto.GtkSharp.Platform");
platform = (Platform)Activator.CreateInstance(platformType!)!;
}
else
{
throw;
}
}
LogPerf($"[PROGRAM] Platform: {platform.ID}");
using var app = new Application(platform);
LogPerf("[PROGRAM] Eto Application created");
#if MACOS_NATIVE
// Force light mode — app uses hardcoded Windows light-theme colors
AppKit.NSApplication.SharedApplication.Appearance =
AppKit.NSAppearance.GetAppearance(AppKit.NSAppearance.NameAqua);
#endif
// 預先初始化 Eto 樣式系統,避免在並行環境中發生競爭條件
// Eto.Forms 的 DefaultStyleProvider 不是線程安全的,需要在主執行緒先初始化
_ = Screen.PrimaryScreen;
LogPerf("[PROGRAM] Eto Screen initialized");
// 捕捉 Eto.Forms 的未處理例外
app.UnhandledException += (sender, e) =>
{
CrashReporter.ReportException(e.ExceptionObject as Exception, "Eto.UnhandledException");
DebugLog.Log($"[PROGRAM] Eto UnhandledException: {e.ExceptionObject}");
};
try
{
LogPerf("[PROGRAM] Creating MapForm...");
var form = new MapForm();
LogPerf("[PROGRAM] MapForm created");
LogPerf("[PROGRAM] Application.Run() starting...");
app.Run(form);
return 0;
}
catch (Exception ex)
{
CrashReporter.ReportException(ex, "Application.Run");
DebugLog.Log($"[PROGRAM] Fatal exception in Application.Run: {ex}");
throw;
}
}
public static void LogPerf(string message)
{
if (!PerfLogEnabled) return;
string timestamp = DateTime.Now.ToString("HH:mm:ss.fff");
string elapsed = $"+{StartupStopwatch.ElapsedMilliseconds}ms";
Console.WriteLine($"{timestamp} {elapsed,-10} {message}");
}
}