feat: read plugin zip from stream directly, make async

This commit is contained in:
goat 2021-08-14 03:28:02 +02:00
parent 4f26402893
commit ac9ed6490b
No known key found for this signature in database
GPG key ID: F18F057873895461
3 changed files with 15 additions and 27 deletions

View file

@ -262,7 +262,7 @@ namespace Dalamud.Game
this.dalamud.Configuration.Save(); 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 => .ContinueWith(t =>
{ {
if (t.IsFaulted) if (t.IsFaulted)

View file

@ -271,7 +271,7 @@ namespace Dalamud.Interface.Internal.Windows
{ {
this.updateStatus = OperationStatus.InProgress; this.updateStatus = OperationStatus.InProgress;
Task.Run(() => this.dalamud.PluginManager.UpdatePlugins()) Task.Run(() => this.dalamud.PluginManager.UpdatePluginsAsync())
.ContinueWith(task => .ContinueWith(task =>
{ {
this.updateStatus = OperationStatus.Complete; this.updateStatus = OperationStatus.Complete;
@ -657,7 +657,7 @@ namespace Dalamud.Interface.Internal.Windows
{ {
this.installStatus = OperationStatus.InProgress; 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 => .ContinueWith(task =>
{ {
// There is no need to set as Complete for an individual plugin installation // 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; this.installStatus = OperationStatus.InProgress;
Task.Run(() => this.dalamud.PluginManager.UpdateSinglePlugin(update, true, false)) Task.Run(() => this.dalamud.PluginManager.UpdateSinglePluginAsync(update, true, false))
.ContinueWith(task => .ContinueWith(task =>
{ {
// There is no need to set as Complete for an individual plugin installation // There is no need to set as Complete for an individual plugin installation

View file

@ -369,7 +369,8 @@ namespace Dalamud.Plugin.Internal
/// <param name="repoManifest">The plugin definition.</param> /// <param name="repoManifest">The plugin definition.</param>
/// <param name="useTesting">If the testing version should be used.</param> /// <param name="useTesting">If the testing version should be used.</param>
/// <param name="reason">The reason this plugin was loaded.</param> /// <param name="reason">The reason this plugin was loaded.</param>
public void InstallPlugin(RemotePluginManifest repoManifest, bool useTesting, PluginLoadReason reason) /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task InstallPluginAsync(RemotePluginManifest repoManifest, bool useTesting, PluginLoadReason reason)
{ {
Log.Debug($"Installing plugin {repoManifest.Name} (testing={useTesting})"); Log.Debug($"Installing plugin {repoManifest.Name} (testing={useTesting})");
@ -390,26 +391,13 @@ namespace Dalamud.Plugin.Internal
// ignored, since the plugin may be loaded already // ignored, since the plugin may be loaded already
} }
var tempZip = new FileInfo(Path.GetTempFileName()); using var client = new HttpClient();
var response = await client.GetAsync(downloadUrl);
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;
}
Log.Debug($"Extracting to {outputDir}"); Log.Debug($"Extracting to {outputDir}");
// This throws an error, even with overwrite=false // This throws an error, even with overwrite=false
// ZipFile.ExtractToDirectory(tempZip.FullName, outputDir.FullName, 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) foreach (var zipFile in archive.Entries)
{ {
@ -438,8 +426,6 @@ namespace Dalamud.Plugin.Internal
} }
} }
tempZip.Delete();
var dllFile = LocalPluginManifest.GetPluginFile(outputDir, repoManifest); var dllFile = LocalPluginManifest.GetPluginFile(outputDir, repoManifest);
var manifestFile = LocalPluginManifest.GetManifestFile(dllFile); var manifestFile = LocalPluginManifest.GetManifestFile(dllFile);
@ -651,7 +637,7 @@ namespace Dalamud.Plugin.Internal
/// </summary> /// </summary>
/// <param name="dryRun">Perform a dry run, don't install anything.</param> /// <param name="dryRun">Perform a dry run, don't install anything.</param>
/// <returns>Success or failure and a list of updated plugin metadata.</returns> /// <returns>Success or failure and a list of updated plugin metadata.</returns>
public List<PluginUpdateStatus> UpdatePlugins(bool dryRun = false) public async Task<List<PluginUpdateStatus>> UpdatePluginsAsync(bool dryRun = false)
{ {
Log.Information("Starting plugin update"); Log.Information("Starting plugin update");
@ -660,7 +646,9 @@ namespace Dalamud.Plugin.Internal
// Prevent collection was modified errors // Prevent collection was modified errors
for (var i = 0; i < this.updatablePlugins.Count; i++) 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(); this.NotifyInstalledPluginsChanged();
@ -678,7 +666,7 @@ namespace Dalamud.Plugin.Internal
/// <param name="dryRun">Whether or not to actually perform the update, or just indicate success.</param> /// <param name="dryRun">Whether or not to actually perform the update, or just indicate success.</param>
/// <returns>The status of the update.</returns> /// <returns>The status of the update.</returns>
[CanBeNull] [CanBeNull]
public PluginUpdateStatus UpdateSinglePlugin(AvailablePluginUpdate metadata, bool notify, bool dryRun) public async Task<PluginUpdateStatus?> UpdateSinglePluginAsync(AvailablePluginUpdate metadata, bool notify, bool dryRun)
{ {
var plugin = metadata.InstalledPlugin; var plugin = metadata.InstalledPlugin;
@ -730,7 +718,7 @@ namespace Dalamud.Plugin.Internal
try try
{ {
this.InstallPlugin(metadata.UpdateManifest, metadata.UseTesting, PluginLoadReason.Update); await this.InstallPluginAsync(metadata.UpdateManifest, metadata.UseTesting, PluginLoadReason.Update);
} }
catch (Exception ex) catch (Exception ex)
{ {