-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstallerWrapper.cs
445 lines (382 loc) · 11.4 KB
/
InstallerWrapper.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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
using Microsoft.Win32;
using System.Text;
using System.Runtime.InteropServices;
using static Crackdown_Installer.InstallerManager;
namespace Crackdown_Installer
{
internal static class InstallerWrapper
{
[Flags]
public enum AssocF
{
Init_NoRemapCLSID = 0x1,
Init_ByExeName = 0x2,
Open_ByExeName = 0x2,
Init_DefaultToStar = 0x4,
Init_DefaultToFolder = 0x8,
NoUserSettings = 0x10,
NoTruncate = 0x20,
Verify = 0x40,
RemapRunDll = 0x80,
NoFixUps = 0x100,
IgnoreBaseClass = 0x200
}
public enum AssocStr
{
Command = 1,
Executable,
FriendlyDocName,
FriendlyAppName,
NoOpen,
ShellNewValue,
DDECommand,
DDEIfExec,
DDEApplication,
DDETopic
}
public static InstallerManager? instMgr;
public const int CURRENT_INSTALLER_VERSION = 2;
const string URL_CRACKDOWN_DISCORD = "https://discord.gg/dak2zQ2";
const string URL_CRACKDOWN_HOMEPAGE = "http://crackdownmod.com/";
const string URL_CRACKDOWN_WIKI = "https://totalcrackdown.wiki.gg/";
const string URL_CRACKDOWN_INSTRUCTIONS = "https://github.com/Crackdown-PD2/Crackdown-Installer";
const string CRASHLOG_PATH = "CRASHLOG.txt";
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//instantiate new client to handle all outgoing http reqs
HttpClient client = new HttpClient();
client.Timeout = TimeSpan.FromMinutes(1);
instMgr = new(client);
// create temp directory in the working directory of the application for this session;
// this temp directory will hold any downloaded zip archives,
// as well as the debug log generated by the installer.
// (this also deletes the temp directory if it already exists)
CreateTemporaryDirectory();
// the StreamWriter object for the installer's debug logs is active from application startup til application close
CreateLogWriter();
LogMessage($"Installer application launched (version {CURRENT_INSTALLER_VERSION})");
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(OnApplicationUnhandledException);
Application.ApplicationExit += new EventHandler(OnApplicationExit);
Application.Run(new Form_InstallerWindow());
}
public static void CreateTemporaryDirectory()
{
if (instMgr != null)
{
instMgr.CreateTempDirectory();
return;
}
throw new Exception("CreateTemporaryDirectory() failed- InstallerManager not initalized");
}
public static string? GetTempDirectory()
{
if (instMgr != null)
{
DirectoryInfo? tempDir = instMgr.GetTempDirectory();
return tempDir?.FullName;
}
throw new Exception("GetTempDirectory() failed- InstallerManager not initalized");
}
private static void OnApplicationUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
using (StreamWriter a = new(CRASHLOG_PATH))
{
a.WriteLine(DateTime.Now.ToString());
a.WriteLine("Application has crashed:");
a.WriteLine(e.ExceptionObject);
a.Flush();
}
Application.Exit();
}
/// <summary>
/// Returns the api version that this installer is rated to handle, for getting the dependency manifest from the update server.
/// </summary>
/// <returns></returns>
public static int GetInstallerVersion()
{
return CURRENT_INSTALLER_VERSION;
}
public static async Task<string?> DownloadDependency(ModDependencyEntry dependency,Action<double?, long, long?> callbackUpdateProgress)
{
if (instMgr != null)
{
return await instMgr.DownloadDependency(dependency, callbackUpdateProgress);
}
throw new Exception("DownloadDependency() failed- InstallerManager not initalized");
}
public static void LaunchPD2()
{
if (instMgr != null)
{
instMgr.LaunchPD2Game();
}
}
public static void OpenTempDirectory()
{
if (instMgr != null)
{
DirectoryInfo? tempDir = instMgr.GetTempDirectory();
if (tempDir != null)
{
string path = tempDir.FullName;
try
{
LogMessage($"Opening temp dir at {path}");
System.Diagnostics.Process.Start("explorer.exe", path);
}
catch (Exception e)
{
LogMessage($"An error occurred while trying to open temp dir at {path}: {e}");
}
}
else
{
LogMessage("Could not open temp directory - no folder found.");
}
return;
}
throw new Exception("OpenTempDirectory() failed- InstallerManager not initalized");
}
public static string? GetSteamDirectory()
{
if (instMgr != null)
{
return instMgr.GetSteamInstallDirectory();
}
throw new Exception("GetSteamDirectory() failed- InstallerManager not initalized");
}
public static string GetPd2InstallPath()
{
if (instMgr != null)
{
return instMgr.GetPd2InstallDirectory();
}
throw new Exception("GetPd2InstallPath() failed- InstallerManager not initalized");
}
public static List<ModDependencyEntry> GetModDependencyList()
{
if (instMgr != null)
{
return instMgr.GetDependencyEntries();
}
throw new Exception("GetModDependencyList() failed- InstallerManager not initalized");
}
public static async void GetModDependencyListAsync(bool forceReevaluate,Action<List<ModDependencyEntry>> clbk)
{
if (instMgr != null)
{
if (forceReevaluate)
{
List<ModDependencyEntry> result = await instMgr.CollectDependencies();
clbk(result);
}
else
{
clbk(instMgr.GetDependencyEntries());
}
return;
}
throw new Exception("GetModDependencyList(bool, Action) failed- InstallerManager not initalized");
}
public static void CollectExistingMods()
{
if (instMgr != null) {
instMgr.CollectPd2Mods();
return;
}
throw new Exception("CollectExistingMods() failed- InstallerManager not initalized");
}
//there is no generic GetModFolder method because
//1) mod folders are often renamed by the user
//2) mod folders can be in either of two locations, meaning that there is possibility for collision between two identically named folders in both locations
//therefore, one should always opt to specify searching for a mod by the name defined in the json file or the xml file
public static Pd2ModFolder? GetBltMod(string name)
{
if (instMgr != null)
{
return instMgr.GetInstalledBltMod(name);
}
throw new Exception("GetBltMod() failed- InstallerManager not initalized");
}
public static Pd2ModFolder? GetBeardlibMod(string name)
{
if (instMgr != null)
{
return instMgr.GetInstalledBeardlibMod(name);
}
throw new Exception("GetBeardlibMod() failed- InstallerManager not initalized");
}
public static Pd2ModData? GetMiscMod(string name)
{
if (instMgr != null)
{
return instMgr.GetInstalledMiscMod(name);
}
throw new Exception("GetMiscMod() failed- InstallerManager not initalized");
}
public static bool IsDirectory(string entryName)
{
return entryName.EndsWith(@"\") || entryName.EndsWith(@"/");
}
public static void BrowserOpenDiscord()
{
OpenWebLink(URL_CRACKDOWN_DISCORD);
}
public static void BrowserOpenHomepage()
{
OpenWebLink(URL_CRACKDOWN_HOMEPAGE);
}
public static void BrowserOpenWiki()
{
OpenWebLink(URL_CRACKDOWN_WIKI);
}
public static void BrowserOpenInstructions()
{
OpenWebLink(URL_CRACKDOWN_INSTRUCTIONS);
}
[DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern void AssocQueryString(AssocF flags, AssocStr str, string pszAssoc, string? pszExtra, [Out] StringBuilder? pszOut, [In][Out] ref uint pcchOut);
public static string GetAssoc(string assocType)
{
string? verb = null;
UInt32 pcchOut = 0;
StringBuilder? sb = null;
AssocQueryString(AssocF.Verify, AssocStr.Executable, assocType, verb, sb, ref pcchOut);
sb = new((int)pcchOut);
AssocQueryString(AssocF.Verify, AssocStr.Executable, assocType, verb, sb, ref pcchOut);
return sb.ToString();
}
public static string? GetPreferredBrowser()
{
string? browser = null;
try
{
browser = GetAssoc(".html");
if (!String.IsNullOrEmpty(browser))
{
if (!browser.EndsWith("exe"))
{
// trim any arguments after the path end (after ".exe")
LogMessage($"Detected default browser {browser}");
}
}
}
catch (Exception e)
{
LogMessage($"Error getting reg key for default browser: {e.Message}");
}
return browser;
}
public static void OpenWebLink(string url)
{
string? preferredBrowser = GetPreferredBrowser();
if (preferredBrowser != null)
{
try
{
System.Diagnostics.Process.Start(String.Format(preferredBrowser,""), url);
}
catch (System.ComponentModel.Win32Exception ex)
{
LogMessage($"Could not open web link {url}: {ex.Message}");
}
}
else
{
LogMessage("OpenWebLink() failed- Could not get preferred browser.");
}
}
public static void CreateLogWriter()
{
if (instMgr != null)
{
instMgr.CreateLogStreamWriter();
return;
}
}
public static void DisposeLogWriter()
{
if (instMgr != null)
{
instMgr.DisposeLogStreamWriter();
return;
}
}
public static void WriteLog(string message)
{
if (instMgr != null)
{
instMgr.WriteLogMessage(message);
return;
}
}
/// <summary>
/// This callback is called only when the user manually closes the installer via the "Quit", "Finish and Open PD2", or "Finish and Close" buttons. This occurs before the actual Application Exit event.
/// </summary>
public static void OnApplicationClose()
{
LogMessage("User requested installer application close");
// dispose temp directory only if closed manually, and if no errors were found;
DisposeLogWriter();
// dispose stream writer so that the log file isn't considered open (which could prevent directory deletion)
// InstallerWrapper.DisposeTemporaryDirectory();
}
/// <summary>
/// This callback is called only when the application is closed (eg by the Windows "Close window" button).
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public static void OnApplicationExit(object? sender, EventArgs e)
{
LogMessage("Installer application terminated (exit event)");
// close and flush log writer
DisposeLogWriter();
}
}
}
/* Outline:
*
* STAGE 0: (init)
* - Build download options checkbox list
*
* STAGE 1:
* - Welcome
* STAGE 2:
* - Find and confirm PD2 directory
* - Save directory path to class member
* STAGE 3:
* - Bool flag to indicate whether a query is in progress
* - Add button to trigger dependencies query again
*
* - Query dependencies url
* - Check provider for each dependency
* - For providers with secondary queries for versions or download urls, query them now;
* - Save dependency entries to class member
*
* - Populate download options checkbox list
* STAGE 4:
* - Bool flag to indicate whether a download is in progress
* - Add button to trigger download again
*
* - Create temp download directory
* - For each dependency, download archive from given download url into temp download directory
* - unpack archive into temp download directory
* - and then install (destructive copy) into correct directory
* - Dispose temp download directory
*
*
*
*
*
*
*
*/