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();
}
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)

View file

@ -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

View file

@ -369,7 +369,8 @@ namespace Dalamud.Plugin.Internal
/// <param name="repoManifest">The plugin definition.</param>
/// <param name="useTesting">If the testing version should be used.</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})");
@ -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
/// </summary>
/// <param name="dryRun">Perform a dry run, don't install anything.</param>
/// <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");
@ -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
/// <param name="dryRun">Whether or not to actually perform the update, or just indicate success.</param>
/// <returns>The status of the update.</returns>
[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;
@ -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)
{