mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-01-02 05:43:40 +01:00
feat: first pass at ReliableFileStorage service
This commit is contained in:
parent
f96ab7aa90
commit
125034155b
6 changed files with 379 additions and 13 deletions
|
|
@ -7,6 +7,7 @@ using System.Linq;
|
|||
using Dalamud.Game.Text;
|
||||
using Dalamud.Interface.Style;
|
||||
using Dalamud.Plugin.Internal.Profiles;
|
||||
using Dalamud.Storage;
|
||||
using Dalamud.Utility;
|
||||
using Newtonsoft.Json;
|
||||
using Serilog;
|
||||
|
|
@ -18,7 +19,7 @@ namespace Dalamud.Configuration.Internal;
|
|||
/// Class containing Dalamud settings.
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
internal sealed class DalamudConfiguration : IServiceType
|
||||
internal sealed class DalamudConfiguration : IServiceType, IDisposable
|
||||
{
|
||||
private static readonly JsonSerializerSettings SerializerSettings = new()
|
||||
{
|
||||
|
|
@ -422,23 +423,33 @@ internal sealed class DalamudConfiguration : IServiceType
|
|||
/// <summary>
|
||||
/// Load a configuration from the provided path.
|
||||
/// </summary>
|
||||
/// <param name="path">The path to load the configuration file from.</param>
|
||||
/// <param name="path">Path to read from.</param>
|
||||
/// <param name="fs">File storage.</param>
|
||||
/// <returns>The deserialized configuration file.</returns>
|
||||
public static DalamudConfiguration Load(string path)
|
||||
public static DalamudConfiguration Load(string path, ReliableFileStorage fs)
|
||||
{
|
||||
DalamudConfiguration deserialized = null;
|
||||
|
||||
try
|
||||
{
|
||||
deserialized = JsonConvert.DeserializeObject<DalamudConfiguration>(File.ReadAllText(path), SerializerSettings);
|
||||
fs.ReadAllText(path, text =>
|
||||
{
|
||||
deserialized =
|
||||
JsonConvert.DeserializeObject<DalamudConfiguration>(text, SerializerSettings);
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch (FileNotFoundException)
|
||||
{
|
||||
Log.Warning(ex, "Failed to load DalamudConfiguration at {0}", path);
|
||||
// ignored
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e, "Could not load DalamudConfiguration at {Path}, creating new", path);
|
||||
}
|
||||
|
||||
deserialized ??= new DalamudConfiguration();
|
||||
deserialized.configPath = path;
|
||||
|
||||
|
||||
return deserialized;
|
||||
}
|
||||
|
||||
|
|
@ -457,6 +468,13 @@ internal sealed class DalamudConfiguration : IServiceType
|
|||
{
|
||||
this.Save();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
// Make sure that we save, if a save is queued while we are shutting down
|
||||
this.Update();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Save the file, if needed. Only needs to be done once a frame.
|
||||
|
|
@ -476,7 +494,8 @@ internal sealed class DalamudConfiguration : IServiceType
|
|||
{
|
||||
ThreadSafety.AssertMainThread();
|
||||
|
||||
Util.WriteAllTextSafe(this.configPath, JsonConvert.SerializeObject(this, SerializerSettings));
|
||||
Service<ReliableFileStorage>.Get().WriteAllText(
|
||||
this.configPath, JsonConvert.SerializeObject(this, SerializerSettings));
|
||||
this.DalamudConfigurationSaved?.Invoke(this);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue