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

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