forked from CnCNet/xna-cncnet-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PreStartup.cs
308 lines (255 loc) · 12.8 KB
/
PreStartup.cs
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
using System;
#if WINFORMS
using System.Windows.Forms;
#endif
using System.Diagnostics;
using System.IO;
using DTAClient.Domain;
using Rampastring.Tools;
using ClientCore;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Collections.Generic;
using Localization;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
namespace DTAClient
{
/// <summary>
/// Contains client startup parameters.
/// </summary>
struct StartupParams
{
public StartupParams(bool noAudio, bool multipleInstanceMode,
List<string> unknownParams)
{
NoAudio = noAudio;
MultipleInstanceMode = multipleInstanceMode;
UnknownStartupParams = unknownParams;
}
public bool NoAudio { get; }
public bool MultipleInstanceMode { get; }
public List<string> UnknownStartupParams { get; }
}
static class PreStartup
{
/// <summary>
/// Initializes various basic systems like the client's logger,
/// constants, and the general exception handler.
/// Reads the user's settings from an INI file,
/// checks for necessary permissions and starts the client if
/// everything goes as it should.
/// </summary>
/// <param name="parameters">The client's startup parameters.</param>
public static void Initialize(StartupParams parameters)
{
#if WINFORMS
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
Application.ThreadException += (sender, args) => HandleException(sender, args.Exception);
#endif
AppDomain.CurrentDomain.UnhandledException += (sender, args) => HandleException(sender, (Exception)args.ExceptionObject);
DirectoryInfo gameDirectory = SafePath.GetDirectory(ProgramConstants.GamePath);
Environment.CurrentDirectory = gameDirectory.FullName;
DirectoryInfo clientUserFilesDirectory = SafePath.GetDirectory(ProgramConstants.ClientUserFilesPath);
FileInfo clientLogFile = SafePath.GetFile(clientUserFilesDirectory.FullName, "client.log");
ProgramConstants.LogFileName = clientLogFile.FullName;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
CheckPermissions();
Logger.Initialize(clientUserFilesDirectory.FullName, clientLogFile.Name);
Logger.WriteLogFile = true;
if (!clientUserFilesDirectory.Exists)
clientUserFilesDirectory.Create();
clientLogFile.Delete();
MainClientConstants.Initialize();
Logger.Log("***Logfile for " + MainClientConstants.GAME_NAME_LONG + " client***");
Logger.Log("Client version: " + Assembly.GetAssembly(typeof(PreStartup)).GetName().Version);
// Log information about given startup params
if (parameters.NoAudio)
{
Logger.Log("Startup parameter: No audio");
// TODO fix
throw new NotImplementedException("-NOAUDIO is currently not implemented, please run the client without it.".L10N("UI:Main:NoAudio"));
}
if (parameters.MultipleInstanceMode)
Logger.Log("Startup parameter: Allow multiple client instances");
parameters.UnknownStartupParams.ForEach(p => Logger.Log("Unknown startup parameter: " + p));
Logger.Log("Loading settings.");
UserINISettings.Initialize(ClientConfiguration.Instance.SettingsIniName);
// Try to load translations
try
{
TranslationTable translation;
var iniFileInfo = SafePath.GetFile(ProgramConstants.GamePath, ClientConfiguration.Instance.TranslationIniName);
if (iniFileInfo.Exists)
{
translation = TranslationTable.LoadFromIniFile(iniFileInfo.FullName);
}
else
{
Logger.Log("Failed to load the translation file. File does not exist.");
translation = new TranslationTable();
}
TranslationTable.Instance = translation;
Logger.Log("Load translation: " + translation.LanguageName);
}
catch (Exception ex)
{
Logger.Log("Failed to load the translation file. " + ex.Message);
TranslationTable.Instance = new TranslationTable();
}
try
{
if (ClientConfiguration.Instance.GenerateTranslationStub)
{
string stubPath = SafePath.CombineFilePath(ProgramConstants.ClientUserFilesPath, "Translation.stub.ini");
var stubTable = TranslationTable.Instance.Clone();
TranslationTable.Instance.MissingTranslationEvent += (sender, e) =>
{
stubTable.Table.Add(e.Label, e.DefaultValue);
};
AppDomain.CurrentDomain.ProcessExit += (sender, e) =>
{
Logger.Log("Writing the translation stub file.");
var ini = stubTable.SaveIni();
ini.WriteIniFile(stubPath);
};
Logger.Log("Generating translation stub feature is now enabled. The stub file will be written when the client exits.");
}
}
catch (Exception ex)
{
Logger.Log("Failed to generate the translation stub. " + ex.Message);
}
// Delete obsolete files from old target project versions
gameDirectory.EnumerateFiles("mainclient.log").SingleOrDefault()?.Delete();
gameDirectory.EnumerateFiles("aunchupdt.dat").SingleOrDefault()?.Delete();
try
{
gameDirectory.EnumerateFiles("wsock32.dll").SingleOrDefault()?.Delete();
}
catch (Exception ex)
{
LogException(ex);
string error = "Deleting wsock32.dll failed! Please close any " +
"applications that could be using the file, and then start the client again."
+ Environment.NewLine + Environment.NewLine +
"Message: " + ex.Message;
ProgramConstants.DisplayErrorAction(null, error, true);
}
#if WINFORMS
ApplicationConfiguration.Initialize();
#endif
new Startup().Execute();
}
public static void LogException(Exception ex, bool innerException = false)
{
if (!innerException)
Logger.Log("KABOOOOOOM!!! Info:");
else
Logger.Log("InnerException info:");
Logger.Log("Type: " + ex.GetType());
Logger.Log("Message: " + ex.Message);
Logger.Log("Source: " + ex.Source);
Logger.Log("TargetSite.Name: " + ex.TargetSite.Name);
Logger.Log("Stacktrace: " + ex.StackTrace);
if (ex.InnerException is not null)
LogException(ex.InnerException, true);
}
static void HandleException(object sender, Exception ex)
{
LogException(ex);
string errorLogPath = SafePath.CombineFilePath(ProgramConstants.ClientUserFilesPath, "ClientCrashLogs", FormattableString.Invariant($"ClientCrashLog{DateTime.Now.ToString("_yyyy_MM_dd_HH_mm")}.txt"));
bool crashLogCopied = false;
try
{
DirectoryInfo crashLogsDirectoryInfo = SafePath.GetDirectory(ProgramConstants.ClientUserFilesPath, "ClientCrashLogs");
if (!crashLogsDirectoryInfo.Exists)
crashLogsDirectoryInfo.Create();
File.Copy(SafePath.CombineFilePath(ProgramConstants.ClientUserFilesPath, "client.log"), errorLogPath, true);
crashLogCopied = true;
}
catch { }
string error = string.Format("{0} has crashed. Error message:".L10N("UI:Main:FatalErrorText1") + Environment.NewLine + Environment.NewLine +
ex.Message + Environment.NewLine + Environment.NewLine + (crashLogCopied ?
"A crash log has been saved to the following file:".L10N("UI:Main:FatalErrorText2") + " " + Environment.NewLine + Environment.NewLine +
errorLogPath + Environment.NewLine + Environment.NewLine : "") +
(crashLogCopied ? "If the issue is repeatable, contact the {1} staff at {2} and provide the crash log file.".L10N("UI:Main:FatalErrorText3") :
"If the issue is repeatable, contact the {1} staff at {2}.".L10N("UI:Main:FatalErrorText4")),
MainClientConstants.GAME_NAME_LONG,
MainClientConstants.GAME_NAME_SHORT,
MainClientConstants.SUPPORT_URL_SHORT);
ProgramConstants.DisplayErrorAction("KABOOOOOOOM".L10N("UI:Main:FatalErrorTitle"), error, true);
}
[SupportedOSPlatform("windows")]
private static void CheckPermissions()
{
if (UserHasDirectoryAccessRights(ProgramConstants.GamePath, FileSystemRights.Modify))
return;
string error = string.Format(("You seem to be running {0} from a write-protected directory." + Environment.NewLine + Environment.NewLine +
"For {1} to function properly when run from a write-protected directory, it needs administrative priveleges." + Environment.NewLine + Environment.NewLine +
"Would you like to restart the client with administrative rights?" + Environment.NewLine + Environment.NewLine +
"Please also make sure that your security software isn't blocking {1}.").L10N("UI:Main:AdminRequiredText"), MainClientConstants.GAME_NAME_LONG, MainClientConstants.GAME_NAME_SHORT);
ProgramConstants.DisplayErrorAction("Administrative privileges required".L10N("UI:Main:AdminRequiredTitle"), error, false);
using var _ = Process.Start(new ProcessStartInfo
{
FileName = "dotnet",
Arguments = SafePath.CombineFilePath(ProgramConstants.StartupExecutable),
Verb = "runas",
CreateNoWindow = true
});
Environment.Exit(1);
}
/// <summary>
/// Checks whether the client has specific file system rights to a directory.
/// See ssds's answer at https://stackoverflow.com/questions/1410127/c-sharp-test-if-user-has-write-access-to-a-folder
/// </summary>
/// <param name="path">The path to the directory.</param>
/// <param name="accessRights">The file system rights.</param>
[SupportedOSPlatform("windows")]
private static bool UserHasDirectoryAccessRights(string path, FileSystemRights accessRights)
{
var currentUser = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(currentUser);
// If the user is not running the client with administrator privileges in Program Files, they need to be prompted to do so.
if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
{
string progfiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
string progfilesx86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
if (ProgramConstants.GamePath.Contains(progfiles) || ProgramConstants.GamePath.Contains(progfilesx86))
return false;
}
var isInRoleWithAccess = false;
try
{
var di = new DirectoryInfo(path);
var acl = di.GetAccessControl();
var rules = acl.GetAccessRules(true, true, typeof(NTAccount));
foreach (AuthorizationRule rule in rules)
{
var fsAccessRule = rule as FileSystemAccessRule;
if (fsAccessRule == null)
continue;
if ((fsAccessRule.FileSystemRights & accessRights) > 0)
{
var ntAccount = rule.IdentityReference as NTAccount;
if (ntAccount == null)
continue;
if (principal.IsInRole(ntAccount.Value))
{
if (fsAccessRule.AccessControlType == AccessControlType.Deny)
return false;
isInRoleWithAccess = true;
}
}
}
}
catch (UnauthorizedAccessException)
{
return false;
}
return isInRoleWithAccess;
}
}
}