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 try
{ {
_ = pluginManager.SetPluginReposFromConfigAsync(false);
pluginManager.OnInstalledPluginsChanged += Troubleshooting.LogTroubleshooting; pluginManager.OnInstalledPluginsChanged += Troubleshooting.LogTroubleshooting;
Log.Information("[T3] PM OK!"); Log.Information("[T3] PM OK!");

View file

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

View file

@ -529,7 +529,7 @@ namespace Dalamud.Interface.Internal.Windows
if (this.thirdRepoListChanged) if (this.thirdRepoListChanged)
{ {
pluginManager.SetPluginReposFromConfig(true); _ = pluginManager.SetPluginReposFromConfigAsync(true);
this.thirdRepoListChanged = false; this.thirdRepoListChanged = false;
} }
@ -602,7 +602,7 @@ namespace Dalamud.Interface.Internal.Windows
configuration.Save(); 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")); this.PluginConfigs = new PluginConfigurations(Path.Combine(Path.GetDirectoryName(startInfo.ConfigurationPath) ?? string.Empty, "pluginConfigs"));
var bannedPluginsJson = File.ReadAllText(Path.Combine(startInfo.AssetDirectory, "UIRes", "bannedplugin.json")); var bannedPluginsJson = File.ReadAllText(Path.Combine(startInfo.AssetDirectory, "UIRes", "bannedplugin.json"));
this.bannedPlugins = JsonConvert.DeserializeObject<BannedPlugin[]>(bannedPluginsJson); this.bannedPlugins = JsonConvert.DeserializeObject<BannedPlugin[]>(bannedPluginsJson) ?? Array.Empty<BannedPlugin>();
this.SetPluginReposFromConfig(false);
this.ApplyPatches(); this.ApplyPatches();
} }
@ -146,10 +144,12 @@ namespace Dalamud.Plugin.Internal
} }
/// <summary> /// <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> /// </summary>
/// <param name="notify">Whether the available plugins changed should be evented after.</param> /// <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(); var configuration = Service<DalamudConfiguration>.Get();
@ -162,6 +162,11 @@ namespace Dalamud.Plugin.Internal
if (notify) if (notify)
this.NotifyAvailablePluginsChanged(); this.NotifyAvailablePluginsChanged();
foreach (var repo in repos)
{
await repo.ReloadPluginMasterAsync();
}
} }
/// <summary> /// <summary>
@ -301,11 +306,15 @@ namespace Dalamud.Plugin.Internal
/// <summary> /// <summary>
/// Reload the PluginMaster for each repo, filter, and event that the list has updated. /// Reload the PluginMaster for each repo, filter, and event that the list has updated.
/// </summary> /// </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())) foreach (var repo in this.Repos)
.ContinueWith(task => this.RefilterPluginMasters()) {
.Wait(); await repo.ReloadPluginMasterAsync();
}
this.RefilterPluginMasters();
} }
/// <summary> /// <summary>

View file

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