refactor: clean up async logic for PluginMaster loads

This commit is contained in:
goat 2021-09-09 01:09:04 +02:00
parent bd48297745
commit 86d9f01877
No known key found for this signature in database
GPG key ID: 7773BB5B43BA52E5
5 changed files with 42 additions and 30 deletions

View file

@ -250,6 +250,8 @@ namespace Dalamud
try
{
_ = pluginManager.SetPluginReposFromConfigAsync(false);
pluginManager.OnInstalledPluginsChanged += Troubleshooting.LogTroubleshooting;
Log.Information("[T3] PM OK!");

View file

@ -67,7 +67,7 @@ namespace Dalamud.Interface.Internal.Windows
private string errorModalMessage = string.Empty;
private int updatePluginCount = 0;
private List<PluginUpdateStatus> updatedPlugins;
private List<PluginUpdateStatus>? updatedPlugins;
private List<RemotePluginManifest> pluginListAvailable = new();
private List<LocalPlugin> pluginListInstalled = new();
@ -163,7 +163,7 @@ namespace Dalamud.Interface.Internal.Windows
{
var pluginManager = Service<PluginManager>.Get();
Task.Run(pluginManager.ReloadPluginMasters);
_ = pluginManager.ReloadPluginMastersAsync();
this.updatePluginCount = 0;
this.updatedPlugins = null;

View file

@ -529,7 +529,7 @@ namespace Dalamud.Interface.Internal.Windows
if (this.thirdRepoListChanged)
{
pluginManager.SetPluginReposFromConfig(true);
_ = pluginManager.SetPluginReposFromConfigAsync(true);
this.thirdRepoListChanged = false;
}
@ -602,7 +602,7 @@ namespace Dalamud.Interface.Internal.Windows
configuration.Save();
Service<PluginManager>.Get().ReloadPluginMasters();
_ = Service<PluginManager>.Get().ReloadPluginMastersAsync();
}
}
}

View file

@ -72,9 +72,7 @@ namespace Dalamud.Plugin.Internal
this.PluginConfigs = new PluginConfigurations(Path.Combine(Path.GetDirectoryName(startInfo.ConfigurationPath) ?? string.Empty, "pluginConfigs"));
var bannedPluginsJson = File.ReadAllText(Path.Combine(startInfo.AssetDirectory, "UIRes", "bannedplugin.json"));
this.bannedPlugins = JsonConvert.DeserializeObject<BannedPlugin[]>(bannedPluginsJson);
this.SetPluginReposFromConfig(false);
this.bannedPlugins = JsonConvert.DeserializeObject<BannedPlugin[]>(bannedPluginsJson) ?? Array.Empty<BannedPlugin>();
this.ApplyPatches();
}
@ -146,10 +144,12 @@ namespace Dalamud.Plugin.Internal
}
/// <summary>
/// Set the list of repositories to use. Should be called when the Settings window has been updated or at instantiation.
/// Set the list of repositories to use and downloads their contents.
/// Should be called when the Settings window has been updated or at instantiation.
/// </summary>
/// <param name="notify">Whether the available plugins changed should be evented after.</param>
public void SetPluginReposFromConfig(bool notify)
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task SetPluginReposFromConfigAsync(bool notify)
{
var configuration = Service<DalamudConfiguration>.Get();
@ -162,6 +162,11 @@ namespace Dalamud.Plugin.Internal
if (notify)
this.NotifyAvailablePluginsChanged();
foreach (var repo in repos)
{
await repo.ReloadPluginMasterAsync();
}
}
/// <summary>
@ -301,11 +306,15 @@ namespace Dalamud.Plugin.Internal
/// <summary>
/// Reload the PluginMaster for each repo, filter, and event that the list has updated.
/// </summary>
public void ReloadPluginMasters()
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task ReloadPluginMastersAsync()
{
Task.WhenAll(this.Repos.Select(repo => repo.ReloadPluginMasterAsync()))
.ContinueWith(task => this.RefilterPluginMasters())
.Wait();
foreach (var repo in this.Repos)
{
await repo.ReloadPluginMasterAsync();
}
this.RefilterPluginMasters();
}
/// <summary>

View file

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net.Http;
@ -29,8 +30,6 @@ namespace Dalamud.Plugin.Internal
this.PluginMasterUrl = pluginMasterUrl;
this.IsThirdParty = pluginMasterUrl != DalamudPluginsMasterUrl;
this.IsEnabled = isEnabled;
this.ReloadPluginMasterAsync();
}
/// <summary>
@ -56,7 +55,7 @@ namespace Dalamud.Plugin.Internal
/// <summary>
/// Gets the plugin master list of available plugins.
/// </summary>
public ReadOnlyCollection<RemotePluginManifest> PluginMaster { get; private set; }
public ReadOnlyCollection<RemotePluginManifest>? PluginMaster { get; private set; }
/// <summary>
/// Gets the initialization state of the plugin repository.
@ -67,12 +66,12 @@ namespace Dalamud.Plugin.Internal
/// Reload the plugin master asynchronously in a task.
/// </summary>
/// <returns>The new state.</returns>
public Task ReloadPluginMasterAsync()
public async Task ReloadPluginMasterAsync()
{
this.State = PluginRepositoryState.InProgress;
this.PluginMaster = new List<RemotePluginManifest>().AsReadOnly();
return Task.Run(async () =>
try
{
Log.Information($"Fetching repo: {this.PluginMasterUrl}");
using var client = new HttpClient();
@ -80,6 +79,12 @@ namespace Dalamud.Plugin.Internal
var data = await response.Content.ReadAsStringAsync();
var pluginMaster = JsonConvert.DeserializeObject<List<RemotePluginManifest>>(data);
if (pluginMaster == null)
{
throw new Exception("Deserialized PluginMaster was null.");
}
pluginMaster.Sort((pm1, pm2) => pm1.Name.CompareTo(pm2.Name));
// Set the source for each remote manifest. Allows for checking if is 3rd party.
@ -89,19 +94,15 @@ namespace Dalamud.Plugin.Internal
}
this.PluginMaster = pluginMaster.AsReadOnly();
}).ContinueWith(task =>
Log.Debug($"Successfully fetched repo: {this.PluginMasterUrl}");
this.State = PluginRepositoryState.Success;
}
catch (Exception ex)
{
if (task.IsCompletedSuccessfully)
{
Log.Debug($"Successfully fetched repo: {this.PluginMasterUrl}");
this.State = PluginRepositoryState.Success;
}
else
{
Log.Error(task.Exception, $"PluginMaster failed: {this.PluginMasterUrl}");
this.State = PluginRepositoryState.Fail;
}
});
Log.Error(ex, $"PluginMaster failed: {this.PluginMasterUrl}");
this.State = PluginRepositoryState.Fail;
}
}
}
}