forked from BornToBeRoot/NETworkManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
364 lines (295 loc) · 13.8 KB
/
Copy pathApp.xaml.cs
File metadata and controls
364 lines (295 loc) · 13.8 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
using NETworkManager.Utilities;
using NETworkManager.Settings;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using NETworkManager.Profiles;
using NETworkManager.Localization;
using System.IO;
using log4net;
namespace NETworkManager;
/*
* Class: App
* 1) Get command line args
* 2) Detect current configuration
* 3) Get assembly info
* 4) Load settings
* 5) Load localization / language
*
* Class: MainWindow
* 6) Load appearance
* 7) Load profiles
*/
public partial class App
{
private static readonly ILog _log = LogManager.GetLogger(typeof(App));
// Single instance identifier
private const string GUID = "6A3F34B2-161F-4F70-A8BC-A19C40F79CFB";
private Mutex _mutex;
private DispatcherTimer _dispatcherTimer;
private bool _singleInstanceClose;
public App()
{
ShutdownMode = ShutdownMode.OnMainWindowClose;
}
private void Application_Startup(object sender, StartupEventArgs e)
{
var startLog = $@"
_ _ _____ _____ _ __ __
| \ | | ____|_ _|_ _____ _ __| | _| \/ | __ _ _ __ __ _ __ _ ___ _ __
| \| | _| | | \ \ /\ / / _ \| '__| |/ / |\/| |/ _` | '_ \ / _` |/ _` |/ _ \ '__|
| |\ | |___ | | \ V V / (_) | | | <| | | | (_| | | | | (_| | (_| | __/ |
|_| \_|_____| |_| \_/\_/ \___/|_| |_|\_\_| |_|\__,_|_| |_|\__,_|\__, |\___|_|
|___/
by BornToBeRoot
GitHub.com/BornToBeRoot/NETworkManager
Version: {AssemblyManager.Current.Version}
";
_log.Info(startLog);
// Catch unhandled exception globally
AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
{
_log.Fatal("Unhandled exception occured!");
if (e.ExceptionObject != null)
_log.Fatal($"Exception raised by: {e.ExceptionObject}");
};
// If we have restart our application... wait until it has finished
if (CommandLineManager.Current.RestartPid != -1)
{
_log.Info($"Waiting for another NETworkManager process with Pid {CommandLineManager.Current.RestartPid} to exit...");
var processList = Process.GetProcesses();
var process = processList.FirstOrDefault(x => x.Id == CommandLineManager.Current.RestartPid);
process?.WaitForExit();
_log.Info($"NETworkManager process with Pid {CommandLineManager.Current.RestartPid} has been exited.");
}
MigrateAppDataToDocuments();
// Load settings
try
{
_log.Info("Application settings are being loaded...");
if (CommandLineManager.Current.ResetSettings)
SettingsManager.InitDefault();
else
SettingsManager.Load();
}
catch (InvalidOperationException ex)
{
_log.Error("Could not load application settings!");
_log.Error(ex.Message + "-" + ex.StackTrace);
// Create backup of corrupted file
var destinationFile = $"{TimestampHelper.GetTimestamp()}_corrupted_" + SettingsManager.GetSettingsFileName();
File.Copy(SettingsManager.GetSettingsFilePath(), Path.Combine(SettingsManager.GetSettingsFolderLocation(), destinationFile));
_log.Info($"A backup of the corrupted settings file has been saved under {destinationFile}");
// Initialize default application settings
_log.Info("Initialize default application settings...");
SettingsManager.InitDefault();
ConfigurationManager.Current.ShowSettingsResetNoteOnStartup = true;
}
// Perform settings update if settings version is lower than application version
if (SettingsManager.Current.FirstRun || string.IsNullOrEmpty(SettingsManager.Current.Version))
{
_log.Info($"Application settings version is empty and will be set to {AssemblyManager.Current.Version}.");
SettingsManager.Current.Version = AssemblyManager.Current.Version.ToString();
}
else
{
Version settingsVersion = Version.Parse(SettingsManager.Current.Version);
if (settingsVersion < AssemblyManager.Current.Version)
{
_log.Info($"Application settings are on version {settingsVersion} and will be upgraded to {AssemblyManager.Current.Version}");
SettingsManager.Upgrade(settingsVersion, AssemblyManager.Current.Version);
_log.Info($"Application settings upgraded to version {AssemblyManager.Current.Version}");
}
else
{
_log.Info($"Application settings are already on version {AssemblyManager.Current.Version}.");
}
}
// Init the location with the culture code...
var localizationManager = LocalizationManager.GetInstance(SettingsManager.Current.Localization_CultureCode);
Localization.Resources.Strings.Culture = localizationManager.Culture;
_log.Info($"Application localization culture has been set to {localizationManager.Current.Code} (Settings value is \"{SettingsManager.Current.Localization_CultureCode}\").");
// Show help window
if (CommandLineManager.Current.Help)
{
_log.Info("Set StartupUri to CommandLineWindow.xaml...");
StartupUri = new Uri("CommandLineWindow.xaml", UriKind.Relative);
return;
}
// Create mutex
_log.Info($"Try to acquire mutex with GUID {GUID} for single instance detection...");
_mutex = new Mutex(true, "{" + GUID + "}");
var mutexIsAcquired = _mutex.WaitOne(TimeSpan.Zero, true);
_log.Info($"Mutex value for {GUID} is {mutexIsAcquired}");
// Release mutex
if (mutexIsAcquired)
_mutex.ReleaseMutex();
if (mutexIsAcquired || SettingsManager.Current.Window_MultipleInstances)
{
// Setup background job
if (SettingsManager.Current.General_BackgroundJobInterval != 0)
{
_log.Info($"Setup background job with interval {SettingsManager.Current.General_BackgroundJobInterval} minute(s)...");
_dispatcherTimer = new DispatcherTimer
{
Interval = TimeSpan.FromMinutes(SettingsManager.Current.General_BackgroundJobInterval)
};
_dispatcherTimer.Tick += DispatcherTimer_Tick;
_dispatcherTimer.Start();
}
else
{
_log.Info("Background job is disabled.");
}
// Setup ThreadPool for the application
ThreadPool.GetMaxThreads(out var workerThreadsMax, out var completionPortThreadsMax);
ThreadPool.GetMinThreads(out var workerThreadsMin, out var completionPortThreadsMin);
var workerThreadsMinNew = workerThreadsMin + SettingsManager.Current.General_ThreadPoolAdditionalMinThreads;
var completionPortThreadsMinNew = completionPortThreadsMin + SettingsManager.Current.General_ThreadPoolAdditionalMinThreads;
if (workerThreadsMinNew > workerThreadsMax)
workerThreadsMinNew = workerThreadsMax;
if (completionPortThreadsMinNew > completionPortThreadsMax)
completionPortThreadsMinNew = completionPortThreadsMax;
if (ThreadPool.SetMinThreads(workerThreadsMinNew, completionPortThreadsMinNew))
_log.Info($"ThreadPool min threads set to: workerThreads: {workerThreadsMinNew}, completionPortThreads: {completionPortThreadsMinNew}");
else
_log.Warn($"ThreadPool min threads could not be set to workerThreads: {workerThreadsMinNew}, completionPortThreads: {completionPortThreadsMinNew}");
// Show splash screen
if (SettingsManager.Current.SplashScreen_Enabled)
{
_log.Info("Show SplashScreen while application is loading...");
new SplashScreen(@"SplashScreen.png").Show(true, true);
}
// Show main window
_log.Info("Set StartupUri to MainWindow.xaml...");
StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);
}
else
{
// Bring the already running application into the foreground
_log.Info("Another NETworkManager process is already running. Try to bring the window to the foreground...");
SingleInstance.PostMessage((IntPtr)SingleInstance.HWND_BROADCAST, SingleInstance.WM_SHOWME, IntPtr.Zero, IntPtr.Zero);
// Close the application
_singleInstanceClose = true;
Shutdown();
}
}
[Obsolete("Temp method to migrate settings and profiles... should be removed after in 2-3 updates.")]
private void MigrateAppDataToDocuments()
{
// Migrate settings and profiles from old paths to new paths
if (!ConfigurationManager.Current.IsPortable)
{
string oldSettingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "NETworkManager", "Settings");
if (Directory.Exists(oldSettingsPath))
{
string oldSettingsFile = Path.Combine(oldSettingsPath, "Settings.xml");
var oldSettingsError = false;
if (File.Exists(oldSettingsFile))
{
_log.Info($"Migrate settings file from \"{oldSettingsFile}\" to \"{SettingsManager.GetSettingsFilePath()}\"...");
Directory.CreateDirectory(SettingsManager.GetSettingsFolderLocation());
try
{
File.Move(oldSettingsFile, SettingsManager.GetSettingsFilePath());
}
catch (Exception ex)
{
oldSettingsError = true;
_log.Error("Could not migrate settings file!", ex);
}
}
try
{
if (!oldSettingsError)
{
_log.Info($"Delete folder \"{oldSettingsPath}\"...");
Directory.Delete(oldSettingsPath, true);
}
}
catch (Exception ex)
{
_log.Error($"Could not delete folder!", ex);
}
}
string oldProfilesPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "NETworkManager", "Profiles");
if (Directory.Exists(oldProfilesPath))
{
var profileExtensions = new[] { ".xml", ".encrypted" };
var oldProfileFilePaths = Directory.EnumerateFiles(oldProfilesPath, "*.*", SearchOption.TopDirectoryOnly)
.Where(f => profileExtensions.Contains(Path.GetExtension(f).ToLowerInvariant()));
var oldProfilesError = false;
if (oldProfileFilePaths != null && oldProfileFilePaths.Any())
{
_log.Info($"Migrate \"{oldProfileFilePaths.Count()}\" profile(s)...");
var newProfilesPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "NETworkManager", "Profiles");
Directory.CreateDirectory(newProfilesPath);
foreach (var oldProfileFilePath in oldProfileFilePaths)
{
var newProfileFilePath = Path.Combine(newProfilesPath, Path.GetFileName(oldProfileFilePath));
_log.Info($"Migrate profile file from \"{oldProfileFilePath}\" to \"{newProfileFilePath}\"");
try
{
File.Move(oldProfileFilePath, newProfileFilePath);
}
catch (Exception ex)
{
oldProfilesError = true;
_log.Error("Could not migrate profile file!", ex);
}
}
}
try
{
if (!oldProfilesError)
{
_log.Info($"Delete folder \"{oldProfilesPath}\"...");
Directory.Delete(oldProfilesPath, true);
}
}
catch (Exception ex)
{
_log.Error($"Could not delete folder!", ex);
}
}
}
}
private void DispatcherTimer_Tick(object sender, EventArgs e)
{
_log.Info("Run background job...");
Save();
}
protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
{
base.OnSessionEnding(e);
e.Cancel = true;
Shutdown();
}
private void Application_Exit(object sender, ExitEventArgs e)
{
_log.Info("Exiting NETworkManager...");
// Save settings, when the application is normally closed
if (_singleInstanceClose || CommandLineManager.Current.Help)
return;
_log.Info("Stop background job (if it exists)...");
_dispatcherTimer?.Stop();
Save();
_log.Info("Bye!");
}
private void Save()
{
if (SettingsManager.Current.SettingsChanged)
{
_log.Info("Save application settings...");
SettingsManager.Save();
}
if (ProfileManager.ProfilesChanged)
{
_log.Info("Save current profiles...");
ProfileManager.Save();
}
}
}