-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
190 lines (170 loc) · 8.12 KB
/
Program.cs
File metadata and controls
190 lines (170 loc) · 8.12 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
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.
using NLog;
using Planetoid_DB.Properties;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
namespace Planetoid_DB;
/// <summary>Provides the main entry point and core initialization logic for the application.</summary>
/// <remarks>This class is responsible for starting the application, configuring essential features, handling
/// startup errors, and ensuring proper shutdown of resources. It cannot be instantiated and contains only static
/// members.</remarks>
internal static class Program
{
/// <summary>NLog logger instance for logging application events.</summary>
/// <remarks>This logger is used throughout the application to log important events and errors.</remarks>
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
/// <summary>Feature ID for disabling navigation sounds.</summary>
/// <remarks>This feature ID is used to disable navigation sounds in the application.</remarks>
private const int FeatureDisableNavigationSounds = 21;
/// <summary>Feature ID for setting the feature on the current thread.</summary>
/// <remarks>This feature ID is used to set the feature on the current thread.</remarks>
private const int SetFeatureOnThread = 0x00000001;
/// <summary>Feature ID for setting the feature on the current process.</summary>
/// <remarks>This feature ID is used to set the feature on the current process.</remarks>
private const int SetFeatureOnProcess = 0x00000002;
/// <summary>Feature ID for setting the feature in the registry.</summary>
/// <remarks>This feature ID is used to set the feature in the registry.</remarks>
private const int SetFeatureInRegistry = 0x00000004;
/// <summary>Feature ID for setting the feature on the current thread for local machine.</summary>
/// <remarks>This feature ID is used to set the feature on the current thread for local machine.</remarks>
private const int SetFeatureOnThreadLocalMachine = 0x00000008;
/// <summary>Feature ID for setting the feature on the current thread for intranet.</summary>
/// <remarks>This feature ID is used to set the feature on the current thread for intranet.</remarks>
private const int SetFeatureOnThreadIntranet = 0x00000010;
/// <summary>Feature ID for setting the feature on the current thread for trusted sites.</summary>
/// <remarks>This feature ID is used to set the feature on the current thread for trusted sites.</remarks>
private const int SetFeatureOnThreadTrusted = 0x00000020;
/// <summary>Feature ID for setting the feature on the current thread for internet.</summary>
/// <remarks>This feature ID is used to set the feature on the current thread for internet.</remarks>
private const int SetFeatureOnThreadInternet = 0x00000040;
/// <summary>Feature ID for setting the feature on the current thread for restricted sites.</summary>
/// <remarks>This feature ID is used to set the feature on the current thread for restricted sites.</remarks>
private const int SetFeatureOnThreadRestricted = 0x00000080;
/// <summary>Disables navigation sounds.</summary>
/// <param name="featureEntry">The feature ID.</param>
/// <param name="dwFlags">The flags.</param>
/// <param name="fEnable">Specifies whether the feature should be enabled or disabled.</param>
/// <returns>An HRESULT value indicating success or failure.</returns>
/// <remarks>This method sets the specified feature for the current process.</remarks>
[DllImport(dllName: "urlmon.dll")]
[PreserveSig]
[return: MarshalAs(unmanagedType: UnmanagedType.Error)]
private static extern int CoInternetSetFeatureEnabled(int featureEntry, [MarshalAs(unmanagedType: UnmanagedType.U4)] int dwFlags, bool fEnable);
/// <summary>The main entry point for the application.</summary>
/// <remarks>This method is responsible for starting the application and initializing the main form.</remarks>
[STAThread]
private static void Main()
{
// Try to set the application to use the default font settings
try
{
// Disable navigation sounds
DisableNavigationSounds();
// Initialize the application configuration
ApplicationConfiguration.Initialize();
if (!File.Exists(path: Settings.Default.systemFilenameMpcorb))
{
// Show the PreLoadForm if the file is missing
HandleMissingFile();
}
else
{
// Start the main form
Application.Run(mainForm: new PlanetoidDbForm());
}
}
// Catch specific exceptions and handle them accordingly
catch (UnauthorizedAccessException ex)
{
// Log the error and show a message box
logger.Error(exception: ex, message: "Access denied");
ShowErrorMessage(message: $"Access denied: {ex.Message}");
// Exit the application with a non-zero exit code
Environment.Exit(exitCode: 1);
}
catch (FileNotFoundException ex)
{
// Log the error and show a message box
logger.Error(exception: ex, message: "File not found");
ShowErrorMessage(message: $"File not found: {ex.Message}");
// Exit the application with a non-zero exit code
Environment.Exit(exitCode: 1);
}
catch (IOException ex)
{
// Log the error and show a message box
logger.Error(exception: ex, message: "I/O error");
ShowErrorMessage(message: $"I/O error: {ex.Message}");
// Exit the application with a non-zero exit code
Environment.Exit(exitCode: 1);
}
catch (NetworkInformationException ex)
{
// Log the error and show a message box
logger.Error(exception: ex, message: "network error");
ShowErrorMessage(message: $"network error: {ex.Message}");
// Exit the application with a non-zero exit code
Environment.Exit(exitCode: 1);
}
catch (Exception ex)
{
// Log the error and show a message box
logger.Error(exception: ex, message: "An unexpected error occurred.");
ShowErrorMessage(message: $"An unexpected error occurred: {ex.Message}");
// Exit the application with a non-zero exit code
Environment.Exit(exitCode: 1);
}
finally
{
// Ensure that the logger is properly shut down
LogManager.Shutdown();
}
}
/// <summary>Disables the navigation sounds.</summary>
/// <remarks>This method disables the navigation sounds for the current process.</remarks>
private static void DisableNavigationSounds() =>
// Disable navigation sounds for the current process
_ = CoInternetSetFeatureEnabled(featureEntry: FeatureDisableNavigationSounds, dwFlags: SetFeatureOnProcess, fEnable: true);
/// <summary>
/// Handles the case when the file is missing.
/// </summary>
/// <remarks>
/// This method handles the case when the file is missing.
/// </remarks>
private static void HandleMissingFile()
{
// Create an instance of the PreLoadForm
using PreloadForm formPreload = new();
// Show the PreLoadForm
_ = formPreload.ShowDialog();
// Check if the form is exited with a cancel result
if (formPreload.DialogResult == DialogResult.Cancel)
{
// Exit the application with a non-zero exit code
Environment.Exit(exitCode: 1);
}
// Check if the file path is empty
if (string.IsNullOrEmpty(value: formPreload.MpcOrbDatFilePath))
{
// Show an error message if the file path is empty
logger.Error(message: "File not found");
ShowErrorMessage(message: "File not found");
// Exit the application with a non-zero exit code
Environment.Exit(exitCode: 1);
}
else
{
// Start the main form with the specified file path
Application.Run(mainForm: new PlanetoidDbForm(mpcorbDatFilePath: formPreload.MpcOrbDatFilePath));
}
}
/// <summary>Displays an error message.</summary>
/// <param name="message">The error message.</param>
/// <remarks>This method displays an error message to the user.</remarks>
private static void ShowErrorMessage(string message) =>
// Log the error message
_ = MessageBox.Show(text: message, caption: I18nStrings.ErrorCaption, buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error);
}