-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.cs
407 lines (364 loc) · 12.9 KB
/
MainWindow.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
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Thorg_Installer
{
public partial class MainWindow : Form
{
const int
PAGE_WLECOME = 0,
PAGE_LICENCE = 1,
PAGE_TOS = 2,
PAGE_SETUP = 3,
PAGE_INSTALL = 4,
PAGE_UNINSTALL = 5;
public MainWindow()
{
InitializeComponent();
tabWizard.TabPages.Remove(tabUninstall);
lbVersion.Text = "Loading...";
Task.Run(async () =>
{
try
{
var app = await Config.App.Load();
Debug.WriteLine("app= {0} / {1}", app.Name, app.Version);
this.BeginInvoke((MethodInvoker)delegate ()
{
lbVersion.Text = $"v{app.Version}";
lbOutputPath.Text = _installer.InstallDir;
Action<CheckBox, Config.Component> decorateComponent = (control, component) =>
{
if (component != null)
{
control.Text = $"{control.Text} v{component.Version}";
}
};
decorateComponent(chkCompYagna, app["yagna"]);
decorateComponent(chkCompThorg, app["thorg"]);
decorateComponent(chkCompWasi, app["wasi"]);
decorateComponent(chkCompGMiner, app["gminer"]);
_installer.Descriptor = app;
if (!_installer.Prepare())
{
SwitchTo(4);
}
});
}
catch (Exception e)
{
MessageBox.Show("Unable to connect to the installation server.\nCheck your internet connection and try again later.\n\n",
"Thorg Installer",
MessageBoxButtons.OK, MessageBoxIcon.Error);
this.BeginInvoke((MethodInvoker)delegate ()
{
Close();
});
return;
}
});
}
private void tabWizard_Selected(object sender, TabControlEventArgs e)
{
btnBack.Enabled = _step > PAGE_WLECOME && _step != PAGE_INSTALL;
btnUninstall.Visible = _step == PAGE_INSTALL;
btnNext.Enabled = CanDoNext();
btnCancel.Visible = _step == PAGE_WLECOME;
btnRepair.Visible = false;
lbProgressError.Visible = false;
if ("Setup".Equals(e.TabPage.Tag))
{
btnNext.Text = "Install";
btnUninstall.Visible = false;
}
else if ("Install".Equals(e.TabPage.Tag))
{
_installer.InstallDir = lbOutputPath.Text;
if (_installer.Prepare())
{
btnUninstall.Visible = false;
DoInstall();
}
else
{
btnNext.Enabled = true;
lbProgress.Text = "Thorg is already installed on your PC.";
prgTotal.Visible = false;
btnRepair.Visible = true;
btnUninstall.Visible = true;
}
btnNext.Text = "Done";
}
else
{
btnNext.Text = "Next";
}
}
private void DoInstall()
{
prgTotal.Visible = true;
_installer.Run((ev) => this.BeginInvoke((MethodInvoker)delegate ()
{
Debug.WriteLine($"{ev.Message}");
if (ev.IsError)
{
lbProgressError.Visible = true;
lbProgressError.Text = ev.Message;
}
else
{
lbProgressError.Visible = false;
lbProgress.Text = ev.Message;
}
if (ev.Total >= 0)
{
prgTotal.Maximum = ev.Total;
}
if (ev.Step >= 0)
{
prgTotal.Value = ev.Step;
}
btnNext.Enabled = ev.IsDone;
}));
}
private void tabWizard_Selecting(object sender, TabControlCancelEventArgs e)
{
e.Cancel = e.TabPageIndex != _step;
}
private void btnNext_Click(object sender, EventArgs e)
{
CommitPage(_step);
SwitchTo(_step + 1);
}
private void CommitPage(int step)
{
switch (step)
{
case PAGE_TOS:
UpdateSentryFlag(chkSentry.Checked);
break;
}
}
private void UpdateSentryFlag(bool sendReports)
{
using (var entry = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\GolemFactory\ThorgMiner"))
{
entry.SetValue("sendReports", sendReports ? "yes" : "no");
}
}
public bool GetSentryFlag()
{
using (var entry = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\GolemFactory\ThorgMiner"))
{
return entry == null || "yes" == entry?.GetValue("sendReports") as string;
}
}
private void AlreadyInstalledStep()
{
}
private void btnBack_Click(object sender, EventArgs e)
{
if (tabWizard.SelectedTab == tabUninstall)
{
tabWizard.ItemSize = new Size(69, 21);
tabWizard.TabPages.Remove(tabUninstall);
tabWizard.SizeMode = TabSizeMode.Normal;
SwitchTo(PAGE_INSTALL);
}
else
{
SwitchTo(_step - 1);
}
}
private bool CanDoNext()
{
switch (_step)
{
case PAGE_WLECOME:
return true;
case PAGE_LICENCE:
return chkLicense.Checked;
case PAGE_TOS:
return chkToS.Checked;
case PAGE_SETUP:
return chkLicense.Checked && chkToS.Checked;
default:
return false;
}
}
private void SwitchTo(int newStep)
{
if (newStep >= tabWizard.TabPages.Count)
{
Process.Start(new ProcessStartInfo()
{
FileName = _installer.ThorgExePath,
WorkingDirectory = _installer.VersionPath
});
Close();
return;
}
_step = newStep;
tabWizard.SelectedIndex = _step;
}
private void ChkUpdate(object sender, EventArgs e)
{
btnNext.Enabled = CanDoNext();
}
private void btnBrowseLocation_Click(object sender, EventArgs e)
{
var openFileDialog = new OpenFileDialog();
openFileDialog.ValidateNames = false;
openFileDialog.CheckFileExists = false;
openFileDialog.CheckPathExists = true;
openFileDialog.FileName = "Folder Selection.";
openFileDialog.InitialDirectory = lbOutputPath.Text;
DialogResult result = openFileDialog.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{
lbOutputPath.Text = Path.GetDirectoryName(openFileDialog.FileName);
}
}
private async Task<bool> DoKillThorgProcess()
{
var exePath = _installer.ThorgExePath;
var processName = Path.GetFileNameWithoutExtension(exePath);
bool result = false;
for (var retry = 0; retry < 5; ++retry)
{
bool sendKill = false;
var processess = Process.GetProcessesByName(processName);
if (processess.Length == 0) return true;
foreach (var thorgProcess in processess)
{
try
{
//if (thorgProcess.MainModule.FileName == exePath)
{
result = true;
thorgProcess.Kill();
sendKill = true;
}
}
catch (Win32Exception ex)
{
Debug.WriteLine($"unable to kill {thorgProcess}: {ex}");
result = false;
}
}
if (sendKill)
{
await Task.Delay(1000);
}
else
{
break;
}
}
return result;
}
private async void btnReinstall_Click(object sender, EventArgs e)
{
btnRepair.Visible = false;
btnNext.Enabled = false;
btnUninstall.Visible = false;
var processKilled = await DoKillThorgProcess();
if (processKilled) DoInstall();
}
private void cboUninstallConfiguration_CheckedChanged(object sender, EventArgs e)
{
BuildUninstallButtonName();
}
void BuildUninstallButtonName()
{
if (cboUninstallYagna.Checked && cboUninstallConfiguration.Checked)
{
btnConfirmUninstall.Text = "Yes, I want to remove Yagna + Thorg and their configuration files";
}
else if (cboUninstallYagna.Checked)
{
btnConfirmUninstall.Text = "Yes, I want to remove Thorg and Yagna's configuration";
}
else if (cboUninstallConfiguration.Checked)
{
btnConfirmUninstall.Text = "Yes, I want to remove Thorg and its configuration";
}
else
{
btnConfirmUninstall.Text = "Yes, I want to remove Thorg";
}
}
private void DoUninstall()
{
try
{
var result = _installer.UninstallThorg(cboUninstallConfiguration.Checked, cboUninstallYagna.Checked);
}
catch (DirectoryNotFoundException)
{
lblUninstallError.Text = "uninstallation failed - directory not found"; return;
}
catch (UnauthorizedAccessException)
{
lblUninstallError.Text = "uninstallation failed - unathorized access"; return;
}
catch (ArgumentException)
{
lblUninstallError.Text = "uninstallation failed"; return;
}
catch (IOException)
{
lblUninstallError.Text = "uninstallation failed - problem with deleting files"; return;
}
catch
{
lblUninstallError.Text = "uninstallation failed";
return;
}
MessageBox.Show("Thorg has been removed from your system.\n\n", "Thorg Installer", MessageBoxButtons.OK, MessageBoxIcon.Information);
Close();
}
private async void btnConfirmUninstall_Click(object sender, EventArgs e)
{
var processKilled = await DoKillThorgProcess();
if (processKilled) DoUninstall();
}
private void cboUninstallYagna_CheckedChanged(object sender, EventArgs e)
{
BuildUninstallButtonName();
}
private void btnUninstall_Click(object sender, EventArgs e)
{
btnUninstall.Visible = false;
lblUninstallError.Text = "";
btnRepair.Visible = false;
btnBack.Visible = true;
btnBack.Enabled = true;
btnNext.Visible = false;
btnCancel.Visible = true;
tabWizard.TabPages.Add(tabUninstall);
SwitchTo(PAGE_UNINSTALL);
tabWizard.ItemSize = new Size(0, 1); // hides tabs
tabWizard.SizeMode = TabSizeMode.Fixed;
}
private void btnCancel_Click(object sender, EventArgs e)
{
Close();
}
private void MainWindow_Load(object sender, EventArgs e)
{
chkSentry.Checked = GetSentryFlag();
}
private int _step = 0;
private Installer _installer = new Installer(new Uri("https://golem-releases.cdn.golem.network/thorg/"));
}
}