fix: don't save manifests every time a plugin loads, note reason for save if save fails

This commit is contained in:
goat 2023-06-26 17:39:11 +02:00
parent 1443c751f5
commit 22a764ed82
No known key found for this signature in database
GPG key ID: 49E2AA8C6A76498B
3 changed files with 36 additions and 11 deletions

View file

@ -865,7 +865,7 @@ internal partial class PluginManager : IDisposable, IServiceType
// Document the url the plugin was installed from
manifest.InstalledFromUrl = repoManifest.SourceRepo.IsThirdParty ? repoManifest.SourceRepo.PluginMasterUrl : LocalPluginManifest.FlagMainRepo;
manifest.Save(manifestFile);
manifest.Save(manifestFile, "installation");
Log.Information($"Installed plugin {manifest.Name} (testing={useTesting})");

View file

@ -125,13 +125,15 @@ internal class LocalPlugin : IDisposable
// Save the manifest to disk so there won't be any problems later.
// We'll update the name property after it can be retrieved from the instance.
this.Manifest.Save(this.manifestFile);
this.Manifest.Save(this.manifestFile, "manifest was null");
}
else
{
this.Manifest = manifest;
}
var needsSaveDueToLegacyFiles = false;
// This converts from the ".disabled" file feature to the manifest instead.
this.disabledFile = LocalPluginManifest.GetDisabledFile(this.DllFile);
if (this.disabledFile.Exists)
@ -140,6 +142,8 @@ internal class LocalPlugin : IDisposable
this.Manifest.Disabled = true;
#pragma warning restore CS0618
this.disabledFile.Delete();
needsSaveDueToLegacyFiles = true;
}
// This converts from the ".testing" file feature to the manifest instead.
@ -148,13 +152,16 @@ internal class LocalPlugin : IDisposable
{
this.Manifest.Testing = true;
this.testingFile.Delete();
needsSaveDueToLegacyFiles = true;
}
var pluginManager = Service<PluginManager>.Get();
this.IsBanned = pluginManager.IsManifestBanned(this.Manifest) && !this.IsDev;
this.BanReason = pluginManager.GetBanReason(this.Manifest);
this.SaveManifest();
if (needsSaveDueToLegacyFiles)
this.SaveManifest("legacy");
}
/// <summary>
@ -322,8 +329,11 @@ internal class LocalPlugin : IDisposable
}
// If we reload a plugin we don't want to delete it. Makes sense, right?
this.Manifest.ScheduledForDeletion = false;
this.SaveManifest();
if (this.Manifest.ScheduledForDeletion)
{
this.Manifest.ScheduledForDeletion = false;
this.SaveManifest("Scheduled for deletion, but loading");
}
switch (this.State)
{
@ -470,10 +480,10 @@ internal class LocalPlugin : IDisposable
}
// In-case the manifest name was a placeholder. Can occur when no manifest was included.
if (this.Manifest.Name.IsNullOrEmpty())
if (this.Manifest.Name.IsNullOrEmpty() && !this.IsDev)
{
this.Manifest.Name = this.instance.Name;
this.Manifest.Save(this.manifestFile);
this.Manifest.Save(this.manifestFile, "manifest name null or empty");
}
this.State = PluginState.Loaded;
@ -618,7 +628,7 @@ internal class LocalPlugin : IDisposable
public void ScheduleDeletion(bool status = true)
{
this.Manifest.ScheduledForDeletion = status;
this.SaveManifest();
this.SaveManifest("scheduling for deletion");
}
/// <summary>
@ -633,7 +643,7 @@ internal class LocalPlugin : IDisposable
this.Manifest = LocalPluginManifest.Load(manifest) ?? throw new Exception("Could not reload manifest.");
// this.Manifest.Disabled = isDisabled;
this.SaveManifest();
this.SaveManifest("dev reload");
}
}
@ -665,5 +675,5 @@ internal class LocalPlugin : IDisposable
config.SharedAssemblies.Add(typeof(Lumina.Excel.ExcelSheetImpl).Assembly.GetName());
}
private void SaveManifest() => this.Manifest.Save(this.manifestFile);
private void SaveManifest(string reason) => this.Manifest.Save(this.manifestFile, reason);
}

View file

@ -3,6 +3,7 @@ using System.IO;
using Dalamud.Utility;
using Newtonsoft.Json;
using Serilog;
namespace Dalamud.Plugin.Internal.Types;
@ -69,7 +70,21 @@ internal record LocalPluginManifest : PluginManifest
/// Save a plugin manifest to file.
/// </summary>
/// <param name="manifestFile">Path to save at.</param>
public void Save(FileInfo manifestFile) => Util.WriteAllTextSafe(manifestFile.FullName, JsonConvert.SerializeObject(this, Formatting.Indented));
/// <param name="reason">The reason the manifest was saved.</param>
public void Save(FileInfo manifestFile, string reason)
{
Log.Verbose("Saving manifest for '{PluginName}' because '{Reason}'", this.InternalName, reason);
try
{
Util.WriteAllTextSafe(manifestFile.FullName, JsonConvert.SerializeObject(this, Formatting.Indented));
}
catch
{
Log.Error("Could not write out manifest for '{PluginName}' because '{Reason}'", this.InternalName, reason);
throw;
}
}
/// <summary>
/// Loads a plugin manifest from file.