Skip to content

Commit 5d35ed4

Browse files
committed
Some more cleanup. Definitely a lot to be done here but let's start fixing after this...
1 parent c0e4d10 commit 5d35ed4

5 files changed

Lines changed: 52 additions & 117 deletions

File tree

SRTPluginManager/Core/Utilities.cs

Lines changed: 37 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
using SRTPluginManager.Properties;
22
using System.Diagnostics;
33
using System.IO;
4-
using System.Windows;
54
using System.IO.Compression;
65
using System.Net.Http;
7-
using System.Reflection;
86
using System.Text.Json;
97
using System.Threading;
108
using System.Threading.Tasks;
119
using System.Windows.Controls;
1210
using System;
1311
using System.Linq;
14-
using System.Collections.Generic;
1512

1613
namespace SRTPluginManager.Core
1714
{
@@ -104,59 +101,61 @@ public static void KillSRT()
104101
process.Kill();
105102
}
106103

107-
public static async Task DownloadManagerAsync(string fileName, string url, Button button, string destination)
104+
public static async Task DownloadManagerAsync(string fileName, string url, Button button, string destination, CancellationToken cancellationToken)
108105
{
109106
var file = Path.Combine(TempFolderPath, fileName);
110107

111108
// Download file.
112-
using (var hc = new HttpClient())
113-
using (var s = await hc.GetStreamAsync(url))
114-
using (var fs = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete))
115-
await s.CopyToAsync(fs);
109+
using (var httpClient = new HttpClient())
110+
using (var downloadStream = await httpClient.GetStreamAsync(url))
111+
using (var fileStream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete))
112+
await downloadStream.CopyToAsync(fileStream);
116113

117114
// Unzip file.
118-
UnzipPackage(file, destination);
115+
await UnzipPackageAsync(file, destination, cancellationToken);
119116
autoResetEvent.Set();
120117
}
121118

122-
public static void UnzipPackage(string file, string destination)
123-
{
124-
ZipFile.ExtractToDirectory(file, destination, true);
125-
File.Delete(file);
126-
//if (!Directory.Exists(file))
127-
//{
128-
// ZipFile.ExtractToDirectory(file, TempFolderPath);
129-
//}
130-
//File.Delete(file); // deletes temp zip
131-
}
132-
133-
public static async Task DownloadFileAsync(string pluginName, string fileName, string url, Button button, string destination, bool isSRT)
119+
public static async Task DownloadFileAsync(string pluginName, string fileName, Uri downloadUri, Button button, string destination, bool isSRT, CancellationToken cancellationToken)
134120
{
135121
var file = Path.Combine(TempFolderPath, fileName);
136122

137123
// Download file.
138-
using (var hc = new HttpClient())
139-
using (var s = await hc.GetStreamAsync(url))
140-
using (var fs = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete))
141-
await s.CopyToAsync(fs);
124+
using (var httpClient = new HttpClient())
125+
using (var downloadStream = await httpClient.GetStreamAsync(downloadUri, cancellationToken))
126+
using (var fileStream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete))
127+
await downloadStream.CopyToAsync(fileStream, cancellationToken);
142128

143129
// Unzip file.
144-
UnzipPackage(pluginName, file, destination, isSRT);
130+
await UnzipPackageAsync(pluginName, file, destination, isSRT, cancellationToken);
145131
autoResetEvent.Set();
146132
}
147133

148-
public static void UnzipPackage(string pluginName, string file, string destination, bool isSRT)
134+
public static async Task UnzipPackageAsync(string file, string destination, CancellationToken cancellationToken)
135+
{
136+
try
137+
{
138+
await using (var zipArchive = ZipFile.Open(file, ZipArchiveMode.Read))
139+
await zipArchive.ExtractToDirectoryAsync(destination, true, cancellationToken);
140+
}
141+
finally
142+
{
143+
File.Delete(file);
144+
}
145+
}
146+
147+
public static async Task UnzipPackageAsync(string pluginName, string file, string destination, bool isSRT, CancellationToken cancellationToken)
149148
{
150149
// Ensure the SRT is closed before we start trying to replace files.
151150
KillSRT();
152151

153152
if (!isSRT)
154153
{
155-
DirectoryInfo pluginDirectory = new DirectoryInfo(Path.Combine(destination, pluginName));
154+
var pluginDirectory = new DirectoryInfo(Path.Combine(destination, pluginName));
156155
if (pluginDirectory.Exists)
157156
{
158157
// Save plugin config file.
159-
FileInfo configFile = pluginDirectory.EnumerateFiles(string.Format("{0}.cfg", pluginName), SearchOption.TopDirectoryOnly).FirstOrDefault();
158+
var configFile = pluginDirectory.EnumerateFiles(string.Format("{0}.cfg", pluginName), SearchOption.TopDirectoryOnly).FirstOrDefault();
160159

161160
// If the config file exists, copy it to the temp folder until after the delete and unzip completes.
162161
if (configFile != default && configFile.Exists)
@@ -167,8 +166,15 @@ public static void UnzipPackage(string pluginName, string file, string destinati
167166
}
168167
}
169168

170-
ZipFile.ExtractToDirectory(file, destination, true);
171-
File.Delete(file);
169+
try
170+
{
171+
await using (var zipArchive = ZipFile.Open(file, ZipArchiveMode.Read))
172+
await zipArchive.ExtractToDirectoryAsync(destination, true, cancellationToken);
173+
}
174+
finally
175+
{
176+
File.Delete(file);
177+
}
172178

173179
if (!isSRT)
174180
{
@@ -179,83 +185,8 @@ public static void UnzipPackage(string pluginName, string file, string destinati
179185
File.Delete(Path.Combine(TempFolderPath, string.Format("{0}.cfg", pluginName)));
180186
}
181187
}
182-
183-
184-
//if (!Directory.Exists(file))
185-
//{
186-
// ZipFile.ExtractToDirectory(file, TempFolderPath);
187-
//}
188-
189-
//if (isSRT)
190-
//{
191-
// File.Delete(file);
192-
// UpdateSRTPackage(TempFolderPath, destination);
193-
//}
194-
//else
195-
//{
196-
// var dirs = Directory.GetDirectories(TempFolderPath);
197-
// UpdatePackage(dirs[0], destination);
198-
//}
199188
}
200189

201-
//public static void UpdateSRTPackage(string source, string destination)
202-
//{
203-
// var filesSource = Directory.GetFiles(source);
204-
// CopyTmpFiles(filesSource, destination);
205-
// var directoriesSource = Directory.GetDirectories(source);
206-
// if (directoriesSource.Length > 0) CopyTmpFolders(directoriesSource, destination, source);
207-
// DeleteTmpFiles();
208-
//}
209-
210-
//public static void UpdatePackage(string source, string destination)
211-
//{
212-
// var dest = Path.Combine(destination, Path.GetFileName(source));
213-
// if (!Directory.Exists(dest)) Directory.CreateDirectory(dest);
214-
// var filesSource = Directory.GetFiles(source);
215-
// CopyTmpFiles(filesSource, dest);
216-
// var directoriesSource = Directory.GetDirectories(source);
217-
// if (directoriesSource.Length > 0) CopyTmpFolders(directoriesSource, dest, source);
218-
// DeleteTmpFiles();
219-
//}
220-
221-
//private static void CopyTmpFolders(string[] folders, string destination, string source)
222-
//{
223-
// foreach (string folder in folders)
224-
// {
225-
// var folderPath = Path.Combine(destination, Path.GetFileName(folder));
226-
// if (!Directory.Exists(folderPath)) Directory.CreateDirectory(folderPath);
227-
// var files = Directory.GetFiles(folder);
228-
// CopyTmpFiles(files, folderPath);
229-
// }
230-
// DeleteDirectories(source);
231-
//}
232-
233-
//private static void CopyTmpFiles(string[] files, string destination)
234-
//{
235-
// var i = 1;
236-
// foreach (string file in files)
237-
// {
238-
// File.Copy(file, Path.Combine(destination, Path.GetFileName(file)), true);
239-
// File.Delete(file);
240-
// i++;
241-
// }
242-
//}
243-
244-
//private static void DeleteTmpFiles()
245-
//{
246-
// DeleteDirectories(TempFolderPath);
247-
// DeleteFiles(TempFolderPath);
248-
//}
249-
250-
//public static void DeleteDirectories(string source)
251-
//{
252-
// var dirs = Directory.GetDirectories(source);
253-
// foreach (string directory in dirs)
254-
// {
255-
// Directory.Delete(directory, true);
256-
// }
257-
//}
258-
259190
public static void UninstallExtension(string source, string extensionName)
260191
{
261192
var dirs = Directory.GetDirectories(source);

SRTPluginManager/MVVM/View/ExtensionsView.xaml.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using SRTPluginManager.Core;
2-
using System;
1+
using System;
32
using System.Diagnostics;
43
using System.IO;
54
using System.Net.Http;
5+
using System.Threading;
66
using System.Threading.Tasks;
77
using System.Windows;
88
using System.Windows.Controls;
9+
using SRTPluginManager.Core;
910
using static SRTPluginManager.Core.Utilities;
1011

1112
namespace SRTPluginManager.MVVM.View
@@ -120,7 +121,7 @@ private void VersionCheck(string current, string latest)
120121

121122
private async void InstallUpdate_Click(object sender, RoutedEventArgs e)
122123
{
123-
await DownloadFileAsync(Config.ExtensionsConfig[CurrentExtension].pluginName, Config.ExtensionsConfig[CurrentExtension].pluginName + ".zip", Config.ExtensionsConfig[CurrentExtension].downloadURL, InstallUpdate, PluginFolderPath, false);
124+
await DownloadFileAsync(Config.ExtensionsConfig[CurrentExtension].pluginName, Config.ExtensionsConfig[CurrentExtension].pluginName + ".zip", new Uri(Config.ExtensionsConfig[CurrentExtension].downloadURL, UriKind.Absolute), InstallUpdate, PluginFolderPath, false, CancellationToken.None);
124125
await Task.Run(() =>
125126
{
126127
autoResetEvent.WaitOne();

SRTPluginManager/MVVM/View/InterfaceView.xaml.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using SRTPluginManager.Core;
2-
using System;
1+
using System;
32
using System.Diagnostics;
43
using System.IO;
54
using System.Net.Http;
5+
using System.Threading;
66
using System.Threading.Tasks;
77
using System.Windows;
88
using System.Windows.Controls;
9+
using SRTPluginManager.Core;
910
using static SRTPluginManager.Core.Utilities;
1011

1112
namespace SRTPluginManager.MVVM.View
@@ -176,7 +177,7 @@ private void VersionCheck(string current, string latest)
176177

177178
private async void InstallUpdate_Click(object sender, RoutedEventArgs e)
178179
{
179-
await DownloadFileAsync(Config.InterfaceConfig[CurrentInterface].pluginName, Config.InterfaceConfig[CurrentInterface].pluginName + ".zip", Config.InterfaceConfig[CurrentInterface].downloadURL, InstallUpdate, PluginFolderPath, false);
180+
await DownloadFileAsync(Config.InterfaceConfig[CurrentInterface].pluginName, Config.InterfaceConfig[CurrentInterface].pluginName + ".zip", new Uri(Config.InterfaceConfig[CurrentInterface].downloadURL, UriKind.Absolute), InstallUpdate, PluginFolderPath, false, CancellationToken.None);
180181
await Task.Run(() =>
181182
{
182183
autoResetEvent.WaitOne();

SRTPluginManager/MVVM/View/PluginView.xaml.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
using SRTPluginManager.Core;
2-
using System;
1+
using System;
32
using System.Diagnostics;
43
using System.IO;
54
using System.Linq;
65
using System.Net.Http;
6+
using System.Threading;
77
using System.Threading.Tasks;
88
using System.Windows;
99
using System.Windows.Controls;
10+
using SRTPluginManager.Core;
1011
using static SRTPluginManager.Core.Utilities;
1112

1213
namespace SRTPluginManager.MVVM.View
@@ -317,7 +318,7 @@ private void StartSRTHost_Click(object sender, RoutedEventArgs e)
317318

318319
private async void GetUpdate_Click(object sender, RoutedEventArgs e)
319320
{
320-
await DownloadFileAsync(Config.PluginConfig[(int)CurrentPlugin].pluginName, Config.PluginConfig[(int)CurrentPlugin].pluginName + ".zip", Config.PluginConfig[(int)CurrentPlugin].downloadURL, GetUpdate, PluginFolderPath, false);
321+
await DownloadFileAsync(Config.PluginConfig[(int)CurrentPlugin].pluginName, Config.PluginConfig[(int)CurrentPlugin].pluginName + ".zip", new Uri(Config.PluginConfig[(int)CurrentPlugin].downloadURL, UriKind.Absolute), GetUpdate, PluginFolderPath, false, CancellationToken.None);
321322
await Task.Run(() =>
322323
{
323324
autoResetEvent.WaitOne();
@@ -428,7 +429,7 @@ public void Log(string[] lines)
428429
private async void SRTGetUpdate_Click(object sender, RoutedEventArgs e)
429430
{
430431
SRTGetUpdate.Content = "Please Wait! Installing...";
431-
await DownloadFileAsync("SRTHost", "SRTHost.zip", Config.SRTConfig.downloadURL, GetUpdate, ApplicationPath, true);
432+
await DownloadFileAsync("SRTHost", "SRTHost.zip", new Uri(Config.SRTConfig.downloadURL, UriKind.Absolute), GetUpdate, ApplicationPath, true, CancellationToken.None);
432433
InitSRTData();
433434
}
434435

SRTPluginManager/MainWindow.xaml.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Diagnostics;
33
using System.IO;
4+
using System.Threading;
45
using System.Windows;
56
using System.Windows.Input;
67
using static SRTPluginManager.Core.Utilities;
@@ -61,7 +62,7 @@ private void MaximizeWindow(object sender, RoutedEventArgs e)
6162

6263
private async void InstallUpdate_Click(object sender, RoutedEventArgs e)
6364
{
64-
await DownloadManagerAsync("ManagerUpdate.zip", Config.ManagerConfig.downloadURL, InstallUpdate, TempFolderPath);
65+
await DownloadManagerAsync("ManagerUpdate.zip", Config.ManagerConfig.downloadURL, InstallUpdate, TempFolderPath, CancellationToken.None);
6566
Process.Start(Path.Combine(TempFolderPath, "SRTPluginManager.exe"), "--LoadUpdate");
6667
Environment.Exit(0);
6768
}

0 commit comments

Comments
 (0)