fix: better plugin update error handling

This commit is contained in:
goat 2020-08-08 13:01:41 +02:00
parent 162ba3f894
commit de29cd7d36

View file

@ -131,10 +131,10 @@ namespace Dalamud.Plugin
var pluginsDirectory = new DirectoryInfo(this.pluginDirectory); var pluginsDirectory = new DirectoryInfo(this.pluginDirectory);
foreach (var installed in pluginsDirectory.GetDirectories()) foreach (var installed in pluginsDirectory.GetDirectories())
{ {
try {
var versions = installed.GetDirectories(); var versions = installed.GetDirectories();
if (versions.Length == 0) if (versions.Length == 0) {
{
Log.Information("Has no versions: {0}", installed.FullName); Log.Information("Has no versions: {0}", installed.FullName);
continue; continue;
} }
@ -144,59 +144,53 @@ namespace Dalamud.Plugin
var localInfoFile = new FileInfo(Path.Combine(latest.FullName, $"{installed.Name}.json")); var localInfoFile = new FileInfo(Path.Combine(latest.FullName, $"{installed.Name}.json"));
if (!localInfoFile.Exists) if (!localInfoFile.Exists) {
{
Log.Information("Has no definition: {0}", localInfoFile.FullName); Log.Information("Has no definition: {0}", localInfoFile.FullName);
continue; continue;
} }
var info = JsonConvert.DeserializeObject<PluginDefinition>(File.ReadAllText(localInfoFile.FullName)); var info = JsonConvert.DeserializeObject<PluginDefinition>(
File.ReadAllText(localInfoFile.FullName));
var remoteInfo = this.PluginMaster.FirstOrDefault(x => x.Name == info.Name); var remoteInfo = this.PluginMaster.FirstOrDefault(x => x.Name == info.Name);
if (remoteInfo == null) if (remoteInfo == null) {
{
Log.Information("Is not in pluginmaster: {0}", info.Name); Log.Information("Is not in pluginmaster: {0}", info.Name);
continue; continue;
} }
if (remoteInfo.DalamudApiLevel != PluginManager.DALAMUD_API_LEVEL) if (remoteInfo.DalamudApiLevel != PluginManager.DALAMUD_API_LEVEL) {
{
Log.Information("Has not applicable API level: {0}", info.Name); Log.Information("Has not applicable API level: {0}", info.Name);
continue; continue;
} }
if (remoteInfo.AssemblyVersion != info.AssemblyVersion) if (remoteInfo.AssemblyVersion != info.AssemblyVersion) {
{
Log.Information("Eligible for update: {0}", remoteInfo.InternalName); Log.Information("Eligible for update: {0}", remoteInfo.InternalName);
// DisablePlugin() below immediately creates a .disabled file anyway, but will fail // DisablePlugin() below immediately creates a .disabled file anyway, but will fail
// with an exception if we try to do it twice in row like this // with an exception if we try to do it twice in row like this
if (!dryRun) if (!dryRun) {
{
var wasEnabled = var wasEnabled =
this.dalamud.PluginManager.Plugins.Where(x => x.Definition != null).Any( this.dalamud.PluginManager.Plugins.Where(x => x.Definition != null).Any(
x => x.Definition.InternalName == info.InternalName); ; x => x.Definition.InternalName == info.InternalName);
;
Log.Verbose("wasEnabled: {0}", wasEnabled); Log.Verbose("wasEnabled: {0}", wasEnabled);
// Try to disable plugin if it is loaded // Try to disable plugin if it is loaded
try try {
{
this.dalamud.PluginManager.DisablePlugin(info); this.dalamud.PluginManager.DisablePlugin(info);
} } catch (Exception ex) {
catch (Exception ex)
{
Log.Error(ex, "Plugin disable failed"); Log.Error(ex, "Plugin disable failed");
//hasError = true; //hasError = true;
} }
try { try {
// Just to be safe // Just to be safe
foreach (var sortedVersion in sortedVersions) foreach (var sortedVersion in sortedVersions) {
{ var disabledFile =
var disabledFile = new FileInfo(Path.Combine(sortedVersion.FullName, ".disabled")); new FileInfo(Path.Combine(sortedVersion.FullName, ".disabled"));
if (!disabledFile.Exists) if (!disabledFile.Exists)
disabledFile.Create(); disabledFile.Create();
} }
@ -206,8 +200,7 @@ namespace Dalamud.Plugin
var installSuccess = InstallPlugin(remoteInfo, wasEnabled); var installSuccess = InstallPlugin(remoteInfo, wasEnabled);
if (!installSuccess) if (!installSuccess) {
{
Log.Error("InstallPlugin failed."); Log.Error("InstallPlugin failed.");
hasError = true; hasError = true;
} }
@ -216,24 +209,23 @@ namespace Dalamud.Plugin
InternalName = remoteInfo.InternalName, InternalName = remoteInfo.InternalName,
WasUpdated = installSuccess WasUpdated = installSuccess
}); });
} } else {
else { updatedList.Add(new PluginUpdateStatus {
updatedList.Add(new PluginUpdateStatus
{
InternalName = remoteInfo.InternalName, InternalName = remoteInfo.InternalName,
WasUpdated = true WasUpdated = true
}); });
} }
} } else {
else
{
Log.Information("Up to date: {0}", remoteInfo.InternalName); Log.Information("Up to date: {0}", remoteInfo.InternalName);
} }
} catch (Exception ex) {
Log.Error(ex, "Could not update plugin: {0}", installed.FullName);
}
} }
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e, "Plugin update failed hard."); Log.Error(e, "Plugin update failed.");
hasError = true; hasError = true;
} }