forked from sinscove/Yuzu-Updater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.cs
830 lines (767 loc) · 35.1 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
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
using SevenZipNET;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using static Yuzu_Updater.DownloadManager;
namespace Yuzu_Updater
{
public partial class MainForm : Form
{
private Settings settings = Settings.GetInstance();
private Logger logger = new Logger();
private HttpClient httpClient = new HttpClient();
private Dictionary<string, string> archivedVersions = new Dictionary<string, string>();
private Stopwatch stopwatch = new Stopwatch();
private SettingsView settingsView = new SettingsView();
public MainForm()
{
httpClient.Timeout = TimeSpan.FromSeconds(20);
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
}
private async Task CheckForUpdates()
{
try
{
SetControlsEnabled(false);
var response = await httpClient.GetAsync("https://pastebin.com/raw/tc6Pk7rz");
if (response.IsSuccessStatusCode)
{
try
{
var stringResponse = await response.Content.ReadAsStringAsync();
string[] lines = stringResponse.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
if (lines.Length > 1)
{
SetStatusAndProgress("Retrieved Database Info", 25);
string version = lines[0];
string nextURL = lines[2];
int latestArchivedVersion = 0;
int versionAsInt = 0;
int currentVersion = 0;
try
{
latestArchivedVersion = int.Parse(VersionDropdown.Items[1].ToString());
versionAsInt = int.Parse(version);
currentVersion = int.Parse(settings.Version);
}catch(Exception)
{
// Ignore
}
if (latestArchivedVersion > versionAsInt)
{
if (latestArchivedVersion > currentVersion)
{
if (settings.BackupSaveFiles)
{
BackupSaveFiles();
}
await DownloadVersion(VersionDropdown.Items[1].ToString(), true);
}
else
{
SetStatusAndProgress("You have the latest version!", 100);
FinishUp();
}
}
else
{
if (settings.Version.Equals(version))
{
SetStatusAndProgress("You have the latest version!", 100);
FinishUp();
}
else
{
if (settings.BackupSaveFiles)
{
BackupSaveFiles();
}
if (nextURL.Length > 0)
{
response = await httpClient.GetAsync(nextURL);
if (response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync();
content = content.Substring(content.IndexOf("https://download"));
content = content.Substring(0, content.IndexOf("\""));
nextURL = content;
String fileName = "YuzuEA-" + version + ".7z";
SetStatusAndProgress("Downloading Update: " + version, 35);
String address = nextURL;
if (settings.AcceleratedDownloads)
{
DownloadResult downloadResult = await DownloadManager.Download(address, Directory.GetCurrentDirectory(), Math.Min(settings.MaxConnections, 16), DownloadProgressChanged);
SetStatus($"Download Took: {downloadResult.TimeTaken}");
DownloadCompleted(new FileDownloadInfo(downloadResult.FilePath, version, false));
}
else
{
using (WebClient webClient = new WebClient())
{
webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
webClient.DownloadFileCompleted += WebClient_DownloadFileCompleted;
stopwatch.Start();
webClient.DownloadFileAsync(new Uri(address), Directory.GetCurrentDirectory() + "\\" + fileName, new FileDownloadInfo(fileName, version, false));
}
}
}
}
else
{
SetStatusAndProgress("Initial attempt failed", 0);
logger.Log(LogLevel.WARNING, "Couldn't parse URL for download");
SetControlsEnabled(true);
}
}
}
}
else
{
SetStatusAndProgress("Initial attempt failed", 0);
logger.Log(LogLevel.WARNING, "Couldn't parse database for URL fetch.");
if (settings.SinMode)
{
SetStatusAndProgress("You have the latest version (Sin hasn't updated yet)!", 0);
}
else
{
await AttemptFallbackDownload();
}
}
}
catch (Exception e)
{
if (!e.GetType().IsAssignableFrom(typeof(ObjectDisposedException)))
{
logger.Log(LogLevel.WARNING, e.Message);
if (settings.SinMode)
{
SetStatusAndProgress("You have the latest version (Sin hasn't updated yet)!", 0);
}
else
{
await AttemptFallbackDownload();
}
}
}
}
else
{
if (settings.SinMode)
{
SetStatusAndProgress("You have the latest version (Sin hasn't updated yet)!", 0);
}
else
{
await AttemptFallbackDownload();
}
}
}
catch (Exception e)
{
if (e.GetType().IsAssignableFrom(typeof(HttpRequestException)) || e.GetType().IsAssignableFrom(typeof(TaskCanceledException)))
{
if (settings.SinMode)
{
SetStatusAndProgress("You have the latest version (Sin hasn't updated yet)!", 0);
}
else
{
await AttemptFallbackDownload();
}
}
else
{
SetStatusAndProgress("An error occurred, please check the logs for the issue", 0);
logger.Log(LogLevel.ERROR, e.Message);
logger.Log(LogLevel.ERROR, e.StackTrace);
SetControlsEnabled(true);
}
}
}
private async Task AttemptFallbackDownload()
{
if (VersionDropdown.Items.Count > 1)
{
int latestArchivedVersion = int.Parse(VersionDropdown.Items[1].ToString());
int settingsVersion = 0;
try
{
settingsVersion = int.Parse(settings.Version);
}
catch (Exception)
{
//Do nothing
}
if (latestArchivedVersion > settingsVersion)
{
await DownloadVersion(VersionDropdown.Items[1].ToString(), true);
}
else
{
LaunchEmulator();
}
}
}
private async Task DownloadAvailableArchivedVersions()
{
SetStatusAndProgress("Fetching list of older versions", 0);
try
{
var response = await httpClient.GetAsync("https://pineappleea.github.io/");
if (response.IsSuccessStatusCode)
{
var patternAnon = new Regex("https://anonfiles.com/.*/YuzuEA-(.*)_7z", RegexOptions.Multiline);
var contentAnon = await response.Content.ReadAsStringAsync();
var matchesAnon = patternAnon.Matches(contentAnon);
var patternGit = new Regex("https://github.com/pineappleEA/pineapple-src/releases/tag/EA-(\\d*)", RegexOptions.Multiline);
var contentGit = await response.Content.ReadAsStringAsync();
var matchesGit = patternGit.Matches(contentGit);
if (matchesGit.Count > 0)
{
foreach (Match match in matchesGit)
{
archivedVersions.Add(match.Groups[1].Value, match.Groups[0].Value);
Invoke(new MethodInvoker(() =>
{
VersionDropdown.Items.Add(match.Groups[1].Value);
}));
}
}
if (matchesAnon.Count > 0)
{
foreach (Match match in matchesAnon)
{
archivedVersions.Add(match.Groups[1].Value, match.Groups[0].Value);
Invoke(new MethodInvoker(() =>
{
VersionDropdown.Items.Add(match.Groups[1].Value);
}));
}
}
SetStatusAndProgress("Fetched list of older versions!", 100);
}
else
{
SetStatusAndProgress("Couldn't fetch older versions, code: " + response.StatusCode, 100);
}
}
catch (Exception e)
{
SetStatusAndProgress("An error occurred, please check the logs for the issue", 0);
logger.Log(LogLevel.ERROR, e.Message);
logger.Log(LogLevel.ERROR, e.StackTrace);
}
}
private void BackupSaveFiles()
{
try
{
string appDataPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\yuzu\\nand\\user";
string version = settings.Version;
if (version.Trim().Equals("")) version = "0";
if(Directory.Exists(appDataPath))
{
if (!Directory.Exists($"{Directory.GetCurrentDirectory()}\\backup-{version}"))
{
Directory.CreateDirectory($"{Directory.GetCurrentDirectory()}\\backup-{version}");
}
if (Directory.Exists($"{appDataPath}\\save"))
{
Copy($"{appDataPath}\\save", $"{Directory.GetCurrentDirectory()}\\backup-{version}\\save", "Backing up save data...");
}
if (Directory.Exists($"{appDataPath}\\saveMeta"))
{
Copy($"{appDataPath}\\saveMeta", $"{Directory.GetCurrentDirectory()}\\backup-{version}\\saveMeta", "Backing up save metadata...");
}
}
}
catch(Exception e)
{
SetStatus("Couldn't create backup.");
logger.Log(LogLevel.ERROR, e.Message);
logger.Log(LogLevel.ERROR, e.StackTrace);
}
}
private async Task DownloadVersion(string version, bool replaceAsLatest)
{
try
{
if (archivedVersions.ContainsKey(version))
{
SetStatusAndProgress("Downloading version " + version + "\r\nThis may take a while..", 0);
SetControlsEnabled(false);
var gitUrl = "https://github.com/pineappleEA/pineapple-src/releases/tag/EA-";
var gitLink = gitUrl + version;
var anonResponse = await httpClient.GetAsync(archivedVersions[version]);
var gitResponse = await httpClient.GetAsync(gitLink);
if (gitResponse.IsSuccessStatusCode)
{
String fileName = "Windows-Yuzu-EA-" + version + ".7z";
String address = gitLink.Replace("tag","download") + "/Windows-Yuzu-EA-" + version + ".7z";
if (settings.AcceleratedDownloads)
{
DownloadResult downloadResult = await DownloadManager.Download(address, Directory.GetCurrentDirectory(), settings.MaxConnections, ArchiveOctaneClient_DownloadProgressChanged);
SetStatus($"Download Took: {downloadResult.TimeTaken}");
ArchiveDownloadCompleted(new FileDownloadInfo(fileName, version, replaceAsLatest));
}
else
{
using (WebClient archiveWebClient = new WebClient())
{
archiveWebClient.DownloadProgressChanged += ArchiveWebClient_DownloadProgressChanged;
archiveWebClient.DownloadFileCompleted += ArchiveWebClient_DownloadFileCompleted;
stopwatch.Start();
archiveWebClient.DownloadFileAsync(new Uri(address), Directory.GetCurrentDirectory() + "\\" + fileName, new FileDownloadInfo(Directory.GetCurrentDirectory() + "\\" + fileName, version, replaceAsLatest));
}
}
}
if (anonResponse.IsSuccessStatusCode && !gitResponse.IsSuccessStatusCode)
{
var content = await anonResponse.Content.ReadAsStringAsync();
var pattern = new Regex("https://cdn.*.anonfiles.com/.*/(YuzuEA-.*.7z)");
var match = pattern.Match(content);
if (match.Success)
{
String address = match.Groups[0].Value;
String fileName = match.Groups[1].Value;
if (settings.AcceleratedDownloads)
{
DownloadResult downloadResult = await DownloadManager.Download(address, Directory.GetCurrentDirectory(), settings.MaxConnections, ArchiveOctaneClient_DownloadProgressChanged);
SetStatus($"Download Took: {downloadResult.TimeTaken}");
ArchiveDownloadCompleted(new FileDownloadInfo(fileName, version, replaceAsLatest));
}
else
{
using (WebClient archiveWebClient = new WebClient())
{
archiveWebClient.DownloadProgressChanged += ArchiveWebClient_DownloadProgressChanged;
archiveWebClient.DownloadFileCompleted += ArchiveWebClient_DownloadFileCompleted;
stopwatch.Start();
archiveWebClient.DownloadFileAsync(new Uri(address), Directory.GetCurrentDirectory() + "\\" + fileName, new FileDownloadInfo(Directory.GetCurrentDirectory() + "\\" + fileName, version, replaceAsLatest));
}
}
}
}
}
else
{
SetStatusAndProgress("Please select a valid version to download.", 0);
SetControlsEnabled(true);
}
}
catch (Exception e)
{
SetStatusAndProgress("An error occurred, please check the logs for the issue", 0);
logger.Log(LogLevel.ERROR, e.Message);
logger.Log(LogLevel.ERROR, e.StackTrace);
SetControlsEnabled(true);
}
}
private void ArchiveOctaneClient_DownloadProgressChanged(int progress)
{
SetStatus($"Accelerated Downloading from Pineapple ({progress})%");
SetProgress(progress);
}
private void DownloadProgressChanged(int progress)
{
SetStatus($"Accelerated Downloading from Pastebin ({progress})%");
SetProgress(progress);
}
private void ArchiveDownloadCompleted(FileDownloadInfo info)
{
stopwatch.Reset();
ZipArchive archive = null;
try
{
SetStatus("Extracting files..\r\nThis may take a while..");
SevenZipExtractor extractor = new SevenZipExtractor(Directory.GetCurrentDirectory() + "\\" + info.fileName);
if (extractor.TestArchive())
{
extractor.ProgressUpdated += Extractor_ProgressUpdated;
if (!Directory.Exists(Directory.GetCurrentDirectory() + "\\" + info.version))
{
Directory.CreateDirectory(Directory.GetCurrentDirectory() + "\\" + info.version);
}
extractor.ExtractAll(Directory.GetCurrentDirectory() + "\\" + info.version, true);
if (info.replaceAsLatest)
{
string path = GetInstalledVersionPath(info.version);
if (path.Length > 0)
{
// Remove the yuzu.exe from the path
path = path.Substring(0, path.Length - 8);
}
SetStatusAndProgress("Transferring Files", 75);
Directory.CreateDirectory(Directory.GetCurrentDirectory() + "\\Yuzu Early Access\\");
Copy(path, Directory.GetCurrentDirectory() + "\\Yuzu Early Access\\");
settings.Version = info.version;
settings.Save();
}
SetStatusAndProgress("Cleaning Up", 75);
File.Delete(Directory.GetCurrentDirectory() + "\\" + info.fileName);
SetStatusAndProgress("Done!", 100);
Invoke(new MethodInvoker(() =>
{
InstallLaunchButton.Text = "Launch";
}));
FinishUp();
}
else
{
SetStatusAndProgress("The archive file was invalid, please try again later.", 0);
SetControlsEnabled(true);
}
}
catch (Exception ex)
{
SetStatusAndProgress("An error occurred, please check the logs for the issue", 0);
if (null != archive)
{
archive.Dispose();
}
logger.Log(LogLevel.ERROR, ex.Message);
logger.Log(LogLevel.ERROR, ex.StackTrace);
SetControlsEnabled(true);
}
}
private void ArchiveWebClient_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
{
// Calculate download speed and output it to labelSpeed.
string speedInKB = string.Format("{0} kb/s", (e.BytesReceived / 1024d / stopwatch.Elapsed.TotalSeconds).ToString("0.00"));
double bytesPerSecond = e.BytesReceived / stopwatch.Elapsed.TotalSeconds;
long bytesLeft = e.TotalBytesToReceive - e.BytesReceived;
long secondsRemaining = (long)(bytesLeft / bytesPerSecond);
long minutesRemaining = secondsRemaining / 60;
secondsRemaining -= (minutesRemaining * 60);
string timeRemaining = string.Format("{0}m {1}s", minutesRemaining, secondsRemaining);
string percentage = e.ProgressPercentage.ToString() + "%";
SetStatus($"Downloading from Pineapple\r\n{speedInKB} ( {percentage} )\r\nTime Remaining: {timeRemaining}");
SetProgress(e.ProgressPercentage);
}
private void ArchiveWebClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
FileDownloadInfo info = e.UserState as FileDownloadInfo;
ArchiveDownloadCompleted(info);
}
private void WebClient_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
{
// Calculate download speed and output it to labelSpeed.
string speedInKB = string.Format("{0} kb/s", (e.BytesReceived / 1024d / stopwatch.Elapsed.TotalSeconds).ToString("0.00"));
double bytesPerSecond = e.BytesReceived / stopwatch.Elapsed.TotalSeconds;
long bytesLeft = e.TotalBytesToReceive - e.BytesReceived;
long secondsRemaining = (long)(bytesLeft / bytesPerSecond);
long minutesRemaining = secondsRemaining / 60;
secondsRemaining -= (minutesRemaining * 60);
string timeRemaining = string.Format("{0}m {1}s", minutesRemaining, secondsRemaining);
string percentage = e.ProgressPercentage.ToString() + "%";
SetStatus($"Downloading from Pastebin...\r\n{speedInKB} ( {percentage} )\r\nTime Remaining: {timeRemaining}");
SetProgress(e.ProgressPercentage);
}
private void SetStatusAndProgress(String status, int value)
{
try
{
if (StatusLabel.InvokeRequired)
{
Invoke(new MethodInvoker(() =>
{
StatusLabel.Text = status;
DownloadInstallProgressBar.Value = value;
logger.Log(status);
}));
}
}
catch (Exception e)
{
logger.Log(LogLevel.ERROR, e.Message);
logger.Log(LogLevel.ERROR, e.StackTrace);
}
}
private void SetStatus(String status)
{
if (!StatusLabel.IsDisposed && StatusLabel.InvokeRequired)
{
Invoke(new MethodInvoker(() =>
{
StatusLabel.Text = status;
}));
}
}
private void SetProgress(int value)
{
if (!DownloadInstallProgressBar.IsDisposed && DownloadInstallProgressBar.InvokeRequired)
{
Invoke(new MethodInvoker(() =>
{
DownloadInstallProgressBar.Value = value;
}));
}
}
private void Extractor_ProgressUpdated(int progress)
{
SetProgress(progress);
}
private void WebClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
try
{
FileDownloadInfo info = e.UserState as FileDownloadInfo;
DownloadCompleted(info);
}
catch(Exception ex)
{
logger.Log(LogLevel.ERROR, ex.Message);
logger.Log(LogLevel.ERROR, ex.StackTrace);
}
}
private void DownloadCompleted(FileDownloadInfo info)
{
ZipArchive archive = null;
try
{
SetStatusAndProgress("Extracting files..\r\nThis may take a while..", 0);
SevenZipNET.SevenZipExtractor extractor = new SevenZipNET.SevenZipExtractor(info.fileName);
if (extractor.TestArchive())
{
extractor.ProgressUpdated += Extractor_ProgressUpdated;
extractor.ExtractAll(Directory.GetCurrentDirectory(), true);
SetStatusAndProgress("Cleaning Up", 75);
File.Delete(info.fileName);
settings.Version = info.version;
settings.Save();
SetStatusAndProgress("Done!", 100);
Invoke(new MethodInvoker(() =>
{
InstallLaunchButton.Text = "Launch";
}));
FinishUp();
}
else
{
SetStatusAndProgress("The archive file was invalid, please try again later.", 0);
SetControlsEnabled(true);
logger.Log("The archive file was invalid, please try again later.");
}
}
catch (Exception ex)
{
SetStatusAndProgress("An error occurred, please check the logs for the issue", 0);
if (null != archive)
{
archive.Dispose();
}
logger.Log(LogLevel.ERROR, ex.Message);
logger.Log(LogLevel.ERROR, ex.StackTrace);
SetControlsEnabled(true);
}
}
private void FinishUp()
{
try
{
if (settings.AutoLaunch)
{
LaunchEmulator();
}
}
catch (Exception e)
{
SetStatusAndProgress("An error occurred, please check the logs for the issue", 0);
logger.Log(LogLevel.ERROR, "Couldn't start the emulator!");
logger.Log(LogLevel.ERROR, e.Message);
logger.Log(LogLevel.ERROR, e.StackTrace);
}
if (settings.AutoClose)
{
try
{
Application.Exit();
}
catch (Exception e)
{
SetStatusAndProgress("An error occurred, please check the logs for the issue", 0);
logger.Log(LogLevel.ERROR, "Couldn't auto-close!");
logger.Log(LogLevel.ERROR, e.Message);
logger.Log(LogLevel.ERROR, e.StackTrace);
}
}
SetControlsEnabled(true);
}
private void SetControlsEnabled(bool enabled)
{
Invoke(new MethodInvoker(() =>
{
VersionDropdown.Enabled = enabled;
InstallLaunchButton.Enabled = enabled;
}));
}
private void MainForm_Shown(object sender, EventArgs e)
{
Task.Run(async () =>
{
await DownloadAvailableArchivedVersions();
Invoke(new MethodInvoker(() =>
{
if (VersionDropdown.Items.Contains(settings.DesiredVersion))
{
VersionDropdown.SelectedItem = settings.DesiredVersion;
}
else
{
VersionDropdown.SelectedIndex = 0;
}
}));
if (settings.DesiredVersion.Equals("Latest"))
{
await CheckForUpdates();
}
else if (!settings.DesiredVersion.Equals("Latest") && settings.AutoLaunch)
{
Invoke(new MethodInvoker(() =>
{
var version = VersionDropdown.SelectedItem.ToString();
if (IsVersionInstalled(version))
{
LaunchEmulator();
}
else
{
Task.Run(async () =>
{
await DownloadVersion(version, false);
});
}
}));
}
});
}
private void InstallLaunchButton_Click(object sender, EventArgs e)
{
string version = VersionDropdown.SelectedItem.ToString();
if (version.Equals("Latest") || IsVersionInstalled(version))
{
LaunchEmulator();
}
else
{
Task.Run(async () =>
{
await DownloadVersion(version, false);
});
}
}
private void VersionDropdown_SelectedIndexChanged(object sender, EventArgs e)
{
if (VersionDropdown.SelectedItem.ToString().Equals("Latest"))
{
settings.DesiredVersion = "Latest";
InstallLaunchButton.Text = "Launch";
}
else
{
if (IsVersionInstalled(VersionDropdown.SelectedItem.ToString()))
{
InstallLaunchButton.Text = "Launch";
}
else
{
InstallLaunchButton.Text = "Install";
}
settings.DesiredVersion = VersionDropdown.SelectedItem.ToString();
}
settings.Save();
}
private bool IsVersionInstalled(string version)
{
return File.Exists(Directory.GetCurrentDirectory() + "\\" + VersionDropdown.SelectedItem.ToString() + "\\" + "yuzu-windows-msvc-early-access" + "\\" + "yuzu.exe") || File.Exists(Directory.GetCurrentDirectory() + "\\" + version + "\\" + "Yuzu Early Access " + version + "\\" + "Yuzu Early Access\\yuzu.exe") || File.Exists(Directory.GetCurrentDirectory() + "\\" + version + "\\" + "YuzuEA-" + version + "\\" + "yuzu.exe");
}
private string GetInstalledVersionPath(string version)
{
if (File.Exists(Directory.GetCurrentDirectory() + "\\" + version + "\\" + "yuzu-windows-msvc-early-access" + "\\yuzu.exe"))
{
return Directory.GetCurrentDirectory() + "\\" + version + "\\" + "yuzu-windows-msvc-early-access" + "\\yuzu.exe";
}
else if (File.Exists(Directory.GetCurrentDirectory() + "\\" + version + "\\" + "Yuzu Early Access " + version + "\\" + "Yuzu Early Access\\yuzu.exe"))
{
return Directory.GetCurrentDirectory() + "\\" + version + "\\" + "Yuzu Early Access " + version + "\\" + "Yuzu Early Access\\yuzu.exe";
}
else if (File.Exists(Directory.GetCurrentDirectory() + "\\" + version + "\\" + "YuzuEA-" + version + "\\yuzu.exe"))
{
return Directory.GetCurrentDirectory() + "\\" + version + "\\" + "YuzuEA-" + version + "\\yuzu.exe";
}
else
{
return "";
}
}
private void LaunchEmulator()
{
string emulatorPath;
if (settings.DesiredVersion.Equals("Latest"))
{
emulatorPath = Directory.GetCurrentDirectory() + "\\Yuzu Early Access\\yuzu.exe";
}
else
{
emulatorPath = GetInstalledVersionPath(settings.DesiredVersion);
}
Process yuzuEmulator = new Process();
yuzuEmulator.StartInfo.FileName = emulatorPath;
if (settings.LaunchAsAdmin)
{
yuzuEmulator.StartInfo.UseShellExecute = true;
yuzuEmulator.StartInfo.Verb = "runas";
}
try
{
yuzuEmulator.Start();
}
catch(Exception e)
{
logger.Log(LogLevel.ERROR, e.Message);
logger.Log(LogLevel.ERROR, e.StackTrace);
}
}
private void Copy(string sourceDirectory, string targetDirectory, string message = "Copying Files...", string endMessage = "Done")
{
DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
SetStatus(message);
CopyAll(diSource, diTarget);
SetStatus(endMessage);
}
private void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
Directory.CreateDirectory(target.FullName);
// Copy each file into the new directory.
foreach (FileInfo fi in source.GetFiles())
{
fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
}
// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
DirectoryInfo nextTargetSubDir =
target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
}
private void SettingsButton_Click(object sender, EventArgs e)
{
settingsView.ShowDialog();
}
}
}