Skip to content

Commit 5528231

Browse files
authored
Create PluginDownloader.xaml.cs
1 parent 4d01b97 commit 5528231

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
using Torch.API.WebAPI;
9+
using System.Windows.Controls;
10+
using System.Windows.Data;
11+
using System.Windows.Documents;
12+
using System.Windows.Input;
13+
using System.Windows.Media;
14+
using System.Windows.Media.Imaging;
15+
using System.Windows.Shapes;
16+
using System.ComponentModel;
17+
18+
namespace Torch.Server.Views
19+
{
20+
21+
/// <summary>
22+
/// Interaction logic for PluginDownloadProgressBar.xaml
23+
/// </summary>
24+
public partial class PluginDownloader : Window
25+
{
26+
27+
private bool downloadNoFailures = true;
28+
private int successfulDownloads = 0;
29+
private int failedDownloads = 0;
30+
private IList PluginsToDownload;
31+
32+
public PluginDownloader(IList SelectedItems) {
33+
InitializeComponent();
34+
PluginsToDownload = SelectedItems;
35+
}
36+
37+
38+
private void DownloadProgress_ContentRendered(object sender, EventArgs e) {
39+
BackgroundWorker worker = new BackgroundWorker();
40+
worker.WorkerReportsProgress = true;
41+
worker.DoWork += DownloadPlugins;
42+
worker.ProgressChanged += PluginDownloaded;
43+
worker.RunWorkerCompleted += DownloadCompleted;
44+
45+
worker.RunWorkerAsync();
46+
}
47+
48+
void DownloadPlugins (object sender, DoWorkEventArgs e) {
49+
var DownloadProgress = 0;
50+
var PercentChangeOnDownload = 100 / PluginsToDownload.Count;
51+
52+
foreach (PluginItem PluginItem in PluginsToDownload) {
53+
if (!Task.Run(async () => await PluginQuery.Instance.DownloadPlugin(PluginItem.ID)).Result) {
54+
failedDownloads++;
55+
DownloadProgress += PercentChangeOnDownload;
56+
(sender as BackgroundWorker).ReportProgress(DownloadProgress);
57+
continue;
58+
}
59+
DownloadProgress += PercentChangeOnDownload;
60+
(sender as BackgroundWorker).ReportProgress(DownloadProgress);
61+
successfulDownloads++;
62+
}
63+
(sender as BackgroundWorker).ReportProgress(100);
64+
}
65+
66+
void PluginDownloaded(object sender, ProgressChangedEventArgs e) {
67+
downloadProgress.Value = e.ProgressPercentage;
68+
}
69+
70+
void DownloadCompleted(object sender, RunWorkerCompletedEventArgs e) {
71+
MessageBox.Show(downloadNoFailures ? $"{successfulDownloads} out of {PluginsToDownload.Count} Plugin(s) downloaded successfully! Please restart the server to load changes."
72+
: $"{failedDownloads} out of {PluginsToDownload.Count} Plugin(s) failed to download! See log for details.",
73+
"Plugin Downloader",
74+
MessageBoxButton.OK, downloadNoFailures ? MessageBoxImage.Information : MessageBoxImage.Warning);
75+
Close();
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)