From ac9ed6490b3e90b0f7ac739c20596cab223839f4 Mon Sep 17 00:00:00 2001
From: goat <16760685+goaaats@users.noreply.github.com>
Date: Sat, 14 Aug 2021 03:28:02 +0200
Subject: [PATCH] feat: read plugin zip from stream directly, make async
---
Dalamud/Game/ChatHandlers.cs | 2 +-
.../Internal/Windows/PluginInstallerWindow.cs | 6 ++--
Dalamud/Plugin/Internal/PluginManager.cs | 34 ++++++-------------
3 files changed, 15 insertions(+), 27 deletions(-)
diff --git a/Dalamud/Game/ChatHandlers.cs b/Dalamud/Game/ChatHandlers.cs
index 5fdb28b73..9c4bc5559 100644
--- a/Dalamud/Game/ChatHandlers.cs
+++ b/Dalamud/Game/ChatHandlers.cs
@@ -262,7 +262,7 @@ namespace Dalamud.Game
this.dalamud.Configuration.Save();
}
- Task.Run(() => this.dalamud.PluginManager.UpdatePlugins(!this.dalamud.Configuration.AutoUpdatePlugins))
+ Task.Run(() => this.dalamud.PluginManager.UpdatePluginsAsync(!this.dalamud.Configuration.AutoUpdatePlugins))
.ContinueWith(t =>
{
if (t.IsFaulted)
diff --git a/Dalamud/Interface/Internal/Windows/PluginInstallerWindow.cs b/Dalamud/Interface/Internal/Windows/PluginInstallerWindow.cs
index 71a1d7eb4..8affa2eea 100644
--- a/Dalamud/Interface/Internal/Windows/PluginInstallerWindow.cs
+++ b/Dalamud/Interface/Internal/Windows/PluginInstallerWindow.cs
@@ -271,7 +271,7 @@ namespace Dalamud.Interface.Internal.Windows
{
this.updateStatus = OperationStatus.InProgress;
- Task.Run(() => this.dalamud.PluginManager.UpdatePlugins())
+ Task.Run(() => this.dalamud.PluginManager.UpdatePluginsAsync())
.ContinueWith(task =>
{
this.updateStatus = OperationStatus.Complete;
@@ -657,7 +657,7 @@ namespace Dalamud.Interface.Internal.Windows
{
this.installStatus = OperationStatus.InProgress;
- Task.Run(() => this.dalamud.PluginManager.InstallPlugin(manifest, useTesting, PluginLoadReason.Installer))
+ Task.Run(() => this.dalamud.PluginManager.InstallPluginAsync(manifest, useTesting, PluginLoadReason.Installer))
.ContinueWith(task =>
{
// There is no need to set as Complete for an individual plugin installation
@@ -993,7 +993,7 @@ namespace Dalamud.Interface.Internal.Windows
{
this.installStatus = OperationStatus.InProgress;
- Task.Run(() => this.dalamud.PluginManager.UpdateSinglePlugin(update, true, false))
+ Task.Run(() => this.dalamud.PluginManager.UpdateSinglePluginAsync(update, true, false))
.ContinueWith(task =>
{
// There is no need to set as Complete for an individual plugin installation
diff --git a/Dalamud/Plugin/Internal/PluginManager.cs b/Dalamud/Plugin/Internal/PluginManager.cs
index cec465f9e..e12b32b43 100644
--- a/Dalamud/Plugin/Internal/PluginManager.cs
+++ b/Dalamud/Plugin/Internal/PluginManager.cs
@@ -369,7 +369,8 @@ namespace Dalamud.Plugin.Internal
/// The plugin definition.
/// If the testing version should be used.
/// The reason this plugin was loaded.
- public void InstallPlugin(RemotePluginManifest repoManifest, bool useTesting, PluginLoadReason reason)
+ /// A representing the asynchronous operation.
+ public async Task InstallPluginAsync(RemotePluginManifest repoManifest, bool useTesting, PluginLoadReason reason)
{
Log.Debug($"Installing plugin {repoManifest.Name} (testing={useTesting})");
@@ -390,26 +391,13 @@ namespace Dalamud.Plugin.Internal
// ignored, since the plugin may be loaded already
}
- var tempZip = new FileInfo(Path.GetTempFileName());
-
- try
- {
- Log.Debug($"Downloading plugin to {tempZip} from {downloadUrl}");
- using var client = new HttpClient();
- var response = client.GetAsync(downloadUrl).Result;
- using var fs = new FileStream(tempZip.FullName, FileMode.CreateNew);
- response.Content.CopyToAsync(fs).GetAwaiter().GetResult();
- }
- catch (HttpRequestException ex)
- {
- Log.Error(ex, $"Download of plugin {repoManifest.Name} failed unexpectedly.");
- throw;
- }
+ using var client = new HttpClient();
+ var response = await client.GetAsync(downloadUrl);
Log.Debug($"Extracting to {outputDir}");
// This throws an error, even with overwrite=false
// ZipFile.ExtractToDirectory(tempZip.FullName, outputDir.FullName, false);
- using (var archive = ZipFile.OpenRead(tempZip.FullName))
+ using (var archive = new ZipArchive(response.Content.ReadAsStream()))
{
foreach (var zipFile in archive.Entries)
{
@@ -438,8 +426,6 @@ namespace Dalamud.Plugin.Internal
}
}
- tempZip.Delete();
-
var dllFile = LocalPluginManifest.GetPluginFile(outputDir, repoManifest);
var manifestFile = LocalPluginManifest.GetManifestFile(dllFile);
@@ -651,7 +637,7 @@ namespace Dalamud.Plugin.Internal
///
/// Perform a dry run, don't install anything.
/// Success or failure and a list of updated plugin metadata.
- public List UpdatePlugins(bool dryRun = false)
+ public async Task> UpdatePluginsAsync(bool dryRun = false)
{
Log.Information("Starting plugin update");
@@ -660,7 +646,9 @@ namespace Dalamud.Plugin.Internal
// Prevent collection was modified errors
for (var i = 0; i < this.updatablePlugins.Count; i++)
{
- updatedList.Add(this.UpdateSinglePlugin(this.updatablePlugins[i], false, dryRun));
+ var result = await this.UpdateSinglePluginAsync(this.updatablePlugins[i], false, dryRun);
+ if (result != null)
+ updatedList.Add(result);
}
this.NotifyInstalledPluginsChanged();
@@ -678,7 +666,7 @@ namespace Dalamud.Plugin.Internal
/// Whether or not to actually perform the update, or just indicate success.
/// The status of the update.
[CanBeNull]
- public PluginUpdateStatus UpdateSinglePlugin(AvailablePluginUpdate metadata, bool notify, bool dryRun)
+ public async Task UpdateSinglePluginAsync(AvailablePluginUpdate metadata, bool notify, bool dryRun)
{
var plugin = metadata.InstalledPlugin;
@@ -730,7 +718,7 @@ namespace Dalamud.Plugin.Internal
try
{
- this.InstallPlugin(metadata.UpdateManifest, metadata.UseTesting, PluginLoadReason.Update);
+ await this.InstallPluginAsync(metadata.UpdateManifest, metadata.UseTesting, PluginLoadReason.Update);
}
catch (Exception ex)
{