-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstaller.cs
More file actions
503 lines (403 loc) · 16 KB
/
Installer.cs
File metadata and controls
503 lines (403 loc) · 16 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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
using Godot;
using System;
using MTGInstaller;
using Environment = System.Environment;
using Directory = System.IO.Directory;
using Path = System.IO.Path;
using System.Collections.Generic;
using System.Threading;
using Thread = System.Threading.Thread;
using System.Reflection;
using System.Net;
using System.Text;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
//https://stackoverflow.com/a/18727100
public class FieldWriter : System.IO.TextWriter {
private object _Target;
private string _Field;
private Type _Type;
private FieldInfo _FieldInfo;
private int _LastLine;
public FieldWriter(object obj, string field) {
_Target = obj;
_Field = field;
_Type = obj.GetType();
_FieldInfo = _Type.GetField(field, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
_LastLine = 0;
}
public override void Write(char value) {
var val = _FieldInfo.GetValue(_Target) as string;
if (val == null) val = "";
if (value == '\r') {
for (var i = val.Length - 1; i >= 0; i--) {
if (val[i] == '\n') {
val = val.Substring(0, i - 1);
}
}
}
_FieldInfo.SetValue(_Target, $"{val}{value}");
}
public override void Write(string value) {
var val = _FieldInfo.GetValue(_Target) as string;
if (val == null) val = "";
if (value.Length > 0 && value[value.Length - 1] == '\r') {
for (var i = val.Length - 1; i >= 0; i--) {
if (val[i] == '\n') {
val = val.Substring(0, i - 1);
}
}
}
_FieldInfo.SetValue(_Target, $"{val}{value}");
}
public override System.Text.Encoding Encoding {
get { return System.Text.Encoding.ASCII; }
}
}
public class Installer : Control {
public enum ViewType {
Main,
Advanced,
HostController
}
public const string DISCORD_INVITE = "https://discord.gg/dVXqxNE";
public static VBoxContainer CurrentInterface;
public VBoxContainer MainInterface;
public VBoxContainer AdvancedInterface;
public VBoxContainer HostControllerInterface;
public LineEdit GungeonPath;
public MenuButton ArchitectureList;
public Button InstallButton;
public Button SettingsButton;
public Button ExeFileButton;
public LineEdit OptionComponent;
public LineEdit OptionArchitecture;
public CheckBox OptionShowLog;
public CheckBox OptionForceHTTP;
public CheckBox OptionSkipVersionCheck;
public CheckBox OptionForceBackup;
public CheckBox OptionLeavePatchDLLs;
public CheckBox OptionOffline;
public CheckBox OptionUnityDebug;
public CheckBox OptionILDebug;
public LineEdit OptionSevenZipExePath;
public TextureRect LargeImage;
public TextEdit InstallLog;
public Label DoneLabel;
public Label NewsContent;
public Label InstallErrorLabel;
public HBoxContainer InstallErrorButtons;
public Button InstallErrorDiscordButton;
public Button InstallErrorLogHasteButton;
public FileDialog ExeFileDialog;
public WindowDialog ETGModWarningDialog;
public bool PathIsAutodetected = true;
public bool InstallationDone = false;
public string QueuedLogText = "";
public Exception InstallException = null;
public void InitializeGungeonPath() {
var path = Settings.Instance.ExecutablePath;
if (path == null) {
path = Autodetector.ExePath ?? "";
PathIsAutodetected = true;
if (path != "") {
Settings.Instance.ExecutablePath = path;
}
}
GungeonPath.Text = path;
GungeonPath.CaretPosition = path.Length;
}
public void UpdateView(VBoxContainer view) {
CurrentInterface.Hide();
view.Show();
CurrentInterface = view;
}
public void InitializeAdvancedSettings() {
var arch_popup = ArchitectureList.GetPopup();
arch_popup.AddItem("Autodetect");
arch_popup.AddItem("x86");
arch_popup.AddItem("x86_64");
ArchitectureList.GetPopup().Set("custom_fonts/font", ArchitectureList.Get("custom_fonts/font"));
ArchitectureList.GetPopup().Connect("id_pressed", this, "_on_ArchList_id_pressed");
var mtginstaller_settings = MTGInstaller.Settings.Instance;
OptionForceHTTP.Pressed = mtginstaller_settings.ForceHTTP;
OptionSkipVersionCheck.Pressed = mtginstaller_settings.SkipVersionChecks;
OptionForceBackup.Pressed = mtginstaller_settings.ForceBackup;
OptionLeavePatchDLLs.Pressed = mtginstaller_settings.LeavePatchDLLs;
OptionOffline.Pressed = mtginstaller_settings.Offline;
OptionUnityDebug.Pressed = mtginstaller_settings.UnityDebug;
OptionILDebug.Pressed = mtginstaller_settings.ILDebug;
OptionSevenZipExePath.Text = mtginstaller_settings.SevenZipPath;
var settings = SemiInstaller.Settings.Instance;
switch (settings.Architecture) {
case null:
ArchitectureList.Text = arch_popup.GetItemText(0);
break;
case "x86":
Autodetector.Architecture = Architecture.X86;
ArchitectureList.Text = arch_popup.GetItemText(1);
break;
case "x86_64":
Autodetector.Architecture = Architecture.X86_64;
ArchitectureList.Text = arch_popup.GetItemText(2);
break;
default:
ArchitectureList.Text = arch_popup.GetItemText(0);
settings.Architecture = null;
break;
}
OptionComponent.Text = settings.Component;
OptionShowLog.Pressed = settings.ShowLog;
}
public class InstallThreadUserdata : Godot.Object {
public InstallerFrontend Frontend;
public string Component;
public string ExePath;
}
public void InstallThread(InstallThreadUserdata userdata) {
var inst = userdata.Frontend;
var components = new List<ComponentInfo>();
components.Add(new ComponentInfo(userdata.Component, null));
try {
inst.Install(components, userdata.ExePath);
if (Settings.Instance.UnityDebug) inst.InstallUnityDebug();
if (Settings.Instance.ILDebug) inst.InstallILDebug();
}
catch (Exception e) {
InstallException = e;
}
InstallationDone = true;
}
public void Install() {
DoneLabel.Hide();
var opts = OptionOffline.Pressed ? InstallerFrontend.InstallerOptions.Offline : InstallerFrontend.InstallerOptions.None;
if (OptionForceBackup.Pressed) opts |= InstallerFrontend.InstallerOptions.ForceBackup;
if (OptionForceHTTP.Pressed) opts |= InstallerFrontend.InstallerOptions.HTTP;
if (OptionLeavePatchDLLs.Pressed) opts |= InstallerFrontend.InstallerOptions.LeavePatchDLLs;
if (OptionSkipVersionCheck.Pressed) opts |= InstallerFrontend.InstallerOptions.SkipVersionChecks;
var inst = new InstallerFrontend(opts);
if (inst.HasETGModInstalled(GungeonPath.Text)) {
ETGModWarningDialog.Popup_();
return;
}
if (OptionShowLog.Pressed) {
LargeImage.Hide();
InstallLog.Show();
}
InstallButton.Disabled = true;
SettingsButton.Disabled = true;
ExeFileButton.Disabled = true;
var data = new InstallThreadUserdata {
Component = OptionComponent.Text,
ExePath = GungeonPath.Text,
Frontend = inst
};
InstallationDone = false;
InstallException = null;
InstallErrorLabel.Hide();
InstallErrorButtons.Hide();
var thread = new Thread(new ThreadStart(() => InstallThread(data)));
thread.Start();
}
public void HandleInstallError(Exception e) {
InstallErrorLabel.Show();
InstallErrorButtons.Show();
System.Console.WriteLine($"[{e.GetType().Name}]: {e.Message}\n{e.StackTrace}");
}
public string MakeLogHaste() {
var log = InstallLog.Text;
var data = Encoding.UTF8.GetBytes(log);
var request = (HttpWebRequest)WebRequest.Create("https://hastebin.com/documents");
request.Method = "POST";
request.ContentType = "text/plain";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream()) {
stream.Write(data, 0, data.Length);
}
var http_response = (HttpWebResponse)request.GetResponse();
if (http_response.StatusCode != HttpStatusCode.OK) {
InstallErrorLogHasteButton.Text = "Request to hastebin.com failed.";
return null;
}
var response = new StreamReader(http_response.GetResponseStream()).ReadToEnd();
var key = response.Replace("{\"key\":\"", "").Replace("\"}", "");
return $"https://hastebin.com/{key}";
}
public string GetNews() {
try {
GD.Print("getting news");
using (var wc = new System.Net.WebClient()) return wc.DownloadString("https://raw.githubusercontent.com/ModTheGungeon/ModTheGungeon.github.io/master/semi/news.txt");
} catch (WebException e) {
return $"Failed to get news - no internet? ({e.Message})";
}
}
public override void _Ready() {
MTGInstaller.Logger.Subscribe((logger, level, indent, str) => {
GD.Print($"[{logger.ID} {level}] {str}");
});
ServicePointManager.ServerCertificateValidationCallback = _RemoteCertificateValidationCallback;
MainInterface = GetNode<VBoxContainer>("MainPanel/ColumnBox/MainInterface");
AdvancedInterface = GetNode<VBoxContainer>("MainPanel/ColumnBox/AdvancedInterface");
HostControllerInterface = GetNode<VBoxContainer>("MainPanel/ColumnBox/HostControllerInterface");
GungeonPath = GetNode<LineEdit>("MainPanel/ColumnBox/MainInterface/ExeSelect/LineEdit");
InstallButton = GetNode<Button>("MainPanel/ColumnBox/MainInterface/Buttons/Install");
SettingsButton = GetNode<Button>("MainPanel/ColumnBox/MainInterface/Buttons/Settings");
ExeFileButton = GetNode<Button>("MainPanel/ColumnBox/MainInterface/ExeSelect/Button");
LargeImage = GetNode<TextureRect>("MainPanel/ColumnBox/LeftPane/LargeImage");
InstallLog = GetNode<TextEdit>("MainPanel/ColumnBox/LeftPane/InstallLog");
InstallErrorLabel = GetNode<Label>("MainPanel/ColumnBox/LeftPane/ErrorLabel");
InstallErrorButtons = GetNode<HBoxContainer>("MainPanel/ColumnBox/LeftPane/ErrorButtons");
InstallErrorDiscordButton = GetNode<Button>("MainPanel/ColumnBox/LeftPane/ErrorButtons/Discord");
InstallErrorLogHasteButton = GetNode<Button>("MainPanel/ColumnBox/LeftPane/ErrorButtons/LogHaste");
OptionComponent = GetNode<LineEdit>("MainPanel/ColumnBox/AdvancedInterface/Settings/SettingsList/Component/Content");
ArchitectureList = GetNode<MenuButton>("MainPanel/ColumnBox/AdvancedInterface/Settings/SettingsList/Architecture/ArchList");
OptionShowLog = GetNode<CheckBox>("MainPanel/ColumnBox/AdvancedInterface/Settings/SettingsList/Log");
OptionForceHTTP = GetNode<CheckBox>("MainPanel/ColumnBox/AdvancedInterface/Settings/SettingsList/ForceHTTP");
OptionSkipVersionCheck = GetNode<CheckBox>("MainPanel/ColumnBox/AdvancedInterface/Settings/SettingsList/SkipVersionChecks");
OptionForceBackup = GetNode<CheckBox>("MainPanel/ColumnBox/AdvancedInterface/Settings/SettingsList/ForceBackup");
OptionLeavePatchDLLs = GetNode<CheckBox>("MainPanel/ColumnBox/AdvancedInterface/Settings/SettingsList/LeavePatchDLLs");
OptionOffline = GetNode<CheckBox>("MainPanel/ColumnBox/AdvancedInterface/Settings/SettingsList/Offline");
OptionUnityDebug = GetNode<CheckBox>("MainPanel/ColumnBox/AdvancedInterface/Settings/SettingsList/UnityDebug");
OptionILDebug = GetNode<CheckBox>("MainPanel/ColumnBox/AdvancedInterface/Settings/SettingsList/ILDebug");
OptionSevenZipExePath = GetNode<LineEdit>("MainPanel/ColumnBox/AdvancedInterface/Settings/SettingsList/7zExePath/7zExePath");
ExeFileDialog = GetNode<FileDialog>("ExeFileDialog");
ETGModWarningDialog = GetNode<WindowDialog>("ETGModWarning");
DoneLabel = GetNode<Label>("MainPanel/ColumnBox/MainInterface/DoneLabel");
NewsContent = GetNode<Label>("MainPanel/ColumnBox/MainInterface/NewsContent");
NewsContent.Text = GetNews();
CurrentInterface = MainInterface;
//Logger.Subscribe((logger, loglevel, indent, str) => {
// var logline = logger.String(loglevel, str, indent);
// QueuedLogText += $"{logline}\n";
//});
Console.SetOut(new FieldWriter(this, "QueuedLogText"));
InitializeGungeonPath();
InitializeAdvancedSettings();
}
public override void _Process(float delta) {
if (QueuedLogText.Length > 0) {
var log_text = QueuedLogText;
QueuedLogText = "";
InstallLog.Text += log_text;
InstallLog.CursorSetLine(99999);
}
if (InstallationDone) {
InstallationDone = false;
InstallButton.Disabled = false;
SettingsButton.Disabled = false;
ExeFileButton.Disabled = false;
if (InstallException != null) {
HandleInstallError(InstallException);
} else {
DoneLabel.Show();
}
}
}
private void _on_Settings_pressed() {
UpdateView(AdvancedInterface);
}
private void _on_OK_pressed() {
UpdateView(MainInterface);
}
private void _on_ArchList_id_pressed(int id) {
ArchitectureList.Text = ArchitectureList.GetPopup().GetItemText(id);
switch (ArchitectureList.Text) {
case "Autodetect": SemiInstaller.Settings.Instance.Architecture = null; break;
default: SemiInstaller.Settings.Instance.Architecture = ArchitectureList.Text; break;
}
if (PathIsAutodetected) InitializeGungeonPath();
SemiInstaller.Settings.Instance.Save();
}
private void _on_ComponentContent_text_changed(String new_text) {
SemiInstaller.Settings.Instance.Component = new_text;
SemiInstaller.Settings.Instance.Save();
}
private void _on_Log_toggled(bool button_pressed) {
SemiInstaller.Settings.Instance.ShowLog = button_pressed;
SemiInstaller.Settings.Instance.Save();
}
private void _on_ForceHTTP_toggled(bool button_pressed)
{
Settings.Instance.ForceHTTP = button_pressed;
Settings.Instance.Save();
}
private void _on_SkipVersionChecks_toggled(bool button_pressed) {
Settings.Instance.SkipVersionChecks = button_pressed;
Settings.Instance.Save();
}
private void _on_ForceBackup_toggled(bool button_pressed) {
Settings.Instance.ForceBackup = button_pressed;
Settings.Instance.Save();
}
private void _on_LeavePatchDLLs_toggled(bool button_pressed) {
Settings.Instance.LeavePatchDLLs = button_pressed;
Settings.Instance.Save();
}
private void _on_Offline_toggled(bool button_pressed) {
Settings.Instance.Offline = button_pressed;
Settings.Instance.Save();
}
private void _on_ExePathButton_pressed() {
var exe_path = GungeonPath.Text;
if (exe_path != null && exe_path != "") {
ExeFileDialog.SetCurrentDir(Path.GetDirectoryName(exe_path));
ExeFileDialog.SetCurrentPath(exe_path);
ExeFileDialog.SetCurrentFile(Path.GetFileName(exe_path));
ExeFileDialog.Invalidate();
}
ExeFileDialog.Popup_();
}
private void _on_ExeFileDialog_file_selected(String path) {
Settings.Instance.ExecutablePath = path;
InitializeGungeonPath();
Settings.Instance.Save();
}
private void _on_ILDebug_toggled(bool button_pressed) {
Settings.Instance.ILDebug = button_pressed;
Settings.Instance.Save();
}
private void _on_UnityDebug_toggled(bool button_pressed) {
Settings.Instance.UnityDebug = button_pressed;
Settings.Instance.Save();
}
private void _on_7zExePath_text_changed(string new_text) {
if (new_text.Length == 0) new_text = null;
Settings.Instance.SevenZipPath = new_text;
Settings.Instance.Save();
}
private void _on_Install_pressed() {
Install();
}
private void _on_LogHaste_pressed() {
var haste = MakeLogHaste();
if (haste == null) return;
OS.SetClipboard(haste);
}
private void _on_Discord_pressed() {
OS.ShellOpen(DISCORD_INVITE);
}
//https://stackoverflow.com/a/33391290
private bool _RemoteCertificateValidationCallback(System.Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) {
bool isOk = true;
// If there are errors in the certificate chain,
// look at each error to determine the cause.
if (sslPolicyErrors != SslPolicyErrors.None) {
for (int i=0; i<chain.ChainStatus.Length; i++) {
if (chain.ChainStatus[i].Status == X509ChainStatusFlags.RevocationStatusUnknown) {
continue;
}
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain;
chain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan (0, 1, 0);
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
bool chainIsValid = chain.Build ((X509Certificate2)certificate);
if (!chainIsValid) {
isOk = false;
break;
}
}
}
return isOk;
}
}