-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
117 lines (100 loc) · 4.56 KB
/
App.xaml.cs
File metadata and controls
117 lines (100 loc) · 4.56 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
using System;
using System.Linq;
using System.Windows;
using System.Windows.Threading;
using Microsoft.Win32;
using SNIBypassGUI.Common.Network;
using SNIBypassGUI.Common.System;
using SNIBypassGUI.Common.Text;
using SNIBypassGUI.Common.Tools;
using SNIBypassGUI.Consts;
using SNIBypassGUI.Services;
using SNIBypassGUI.Views;
using static SNIBypassGUI.Common.LogManager;
namespace SNIBypassGUI
{
public partial class App : Application
{
/// <summary>
/// Constructor. Initializes global exception handling and logging.
/// </summary>
public App()
{
if (!IsDotNet472OrHigherInstalled())
{
if (MessageBox.Show("此应用程序需要 .NET Framework 4.7.2 或更高版本。\n是否需要打开 Microsoft 官方下载页面?", "缺少必要组件", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
ProcessUtils.StartProcess(LinksConsts.Net472DownloadUrl, useShellExecute: true);
Environment.Exit(1);
}
DispatcherUnhandledException += App_DispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
// Load configuration synchronously at startup to ensure settings are available immediately.
ConfigManager.Instance.LoadAsync().GetAwaiter().GetResult();
if (ConfigManager.Instance.Settings.Advanced.GUIDebug)
{
// Get log path
string logPath = GetLogPath();
// Start tracking log file
TailUtils.StartTracking(logPath, "GUIDebug", true);
// Enable logging
EnableLog();
}
}
/// <summary>
/// Checks if .NET Framework 4.7.2 or higher is installed.
/// </summary>
private bool IsDotNet472OrHigherInstalled()
{
const string registryKeyPath = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full";
const int RequiredReleaseKey = 461808;
using RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKeyPath);
if (key != null)
{
object releaseValue = key.GetValue("Release");
if (releaseValue != null && (int)releaseValue >= RequiredReleaseKey) return true;
}
return false;
}
private async void App_Startup(object sender, StartupEventArgs e)
{
var startupService = new StartupService();
startupService.CheckSingleInstance();
startupService.InitializeDirectoriesAndFiles();
string[] args = e.Args;
if (ArgumentUtils.ContainsArgument(args, AppConsts.CleanUpArgument))
{
var tempStore = ConfigManager.Instance.Settings.TemporaryData;
if (tempStore != null && tempStore.Count > 0)
{
var proxyService = new ProxyService();
var adapters = await NetworkAdapterUtils.GetNetworkAdaptersAsync(NetworkAdapterUtils.ScopeNeeded.FriendlyNameNotNullOnly);
foreach (var adapterName in tempStore.Keys.ToList())
{
var adapter = adapters.FirstOrDefault(a => a.FriendlyName == adapterName);
if (adapter != null)
await proxyService.RestoreAdapterDNSAsync(adapter, true);
}
await ConfigManager.Instance.SaveNowAsync();
}
Shutdown();
return;
}
var mainWindow = new MainWindow();
if (ArgumentUtils.ContainsArgument(args, AppConsts.AutoStartArgument))
mainWindow.RunInSilentMode();
else mainWindow.Show();
}
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
WriteLog("Unhandled Dispatcher Exception!", LogLevel.Error, e.Exception);
MessageBox.Show($"遇到未处理的异常:{e.Exception.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
e.Handled = true;
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var ex = (Exception)e.ExceptionObject;
WriteLog("Unhandled Domain Exception!", LogLevel.Error, ex);
MessageBox.Show($"遇到未处理的异常:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}