feat: batch config saves

This commit is contained in:
goat 2022-11-01 19:43:38 +01:00
parent 4769239b19
commit 9c16359914
No known key found for this signature in database
GPG key ID: 49E2AA8C6A76498B
17 changed files with 69 additions and 37 deletions

View file

@ -6,6 +6,7 @@ using System.Linq;
using Dalamud.Game.Text;
using Dalamud.Interface.Style;
using Dalamud.Utility;
using Newtonsoft.Json;
using Serilog;
using Serilog.Events;
@ -28,6 +29,9 @@ internal sealed class DalamudConfiguration : IServiceType
[JsonIgnore]
private string configPath;
[JsonIgnore]
private bool isSaveQueued;
/// <summary>
/// Delegate for the <see cref="DalamudConfiguration.DalamudConfigurationSaved"/> event that occurs when the dalamud configuration is saved.
/// </summary>
@ -368,8 +372,29 @@ internal sealed class DalamudConfiguration : IServiceType
/// <summary>
/// Save the configuration at the path it was loaded from.
/// </summary>
public void Save()
public void QueueSave()
{
this.isSaveQueued = true;
}
/// <summary>
/// Save the file, if needed. Only needs to be done once a frame.
/// </summary>
internal void Update()
{
if (this.isSaveQueued)
{
this.Save();
this.isSaveQueued = false;
Log.Verbose("Config saved");
}
}
private void Save()
{
ThreadSafety.AssertMainThread();
File.WriteAllText(this.configPath, JsonConvert.SerializeObject(this, SerializerSettings));
this.DalamudConfigurationSaved?.Invoke(this);
}