mirror of
https://github.com/Ottermandias/Glamourer.git
synced 2025-12-22 08:29:20 +01:00
,
This commit is contained in:
parent
7463aafa13
commit
27f151c55a
32 changed files with 1744 additions and 151 deletions
|
|
@ -9,89 +9,12 @@ namespace Glamourer.Services;
|
|||
/// <summary>
|
||||
/// Any file type that we want to save via SaveService.
|
||||
/// </summary>
|
||||
public interface ISavable
|
||||
public interface ISavable : ISavable<FilenameService>
|
||||
{ }
|
||||
|
||||
public sealed class SaveService : SaveServiceBase<FilenameService>
|
||||
{
|
||||
/// <summary> The full file name of a given object. </summary>
|
||||
public string ToFilename(FilenameService fileNames);
|
||||
|
||||
/// <summary> Write the objects data to the given stream writer. </summary>
|
||||
public void Save(StreamWriter writer);
|
||||
|
||||
/// <summary> An arbitrary message printed to Debug before saving. </summary>
|
||||
public string LogName(string fileName)
|
||||
=> fileName;
|
||||
|
||||
public string TypeName
|
||||
=> GetType().Name;
|
||||
}
|
||||
|
||||
public class SaveService
|
||||
{
|
||||
private readonly Logger _log;
|
||||
private readonly FrameworkManager _framework;
|
||||
|
||||
public readonly FilenameService FileNames;
|
||||
|
||||
public SaveService(Logger log, FrameworkManager framework, FilenameService fileNames)
|
||||
{
|
||||
_log = log;
|
||||
_framework = framework;
|
||||
FileNames = fileNames;
|
||||
}
|
||||
|
||||
/// <summary> Queue a save for the next framework tick. </summary>
|
||||
public void QueueSave(ISavable value)
|
||||
{
|
||||
var file = value.ToFilename(FileNames);
|
||||
_framework.RegisterOnTick(value.GetType().Name + file, () =>
|
||||
{
|
||||
ImmediateSave(value);
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary> Immediately trigger a save. </summary>
|
||||
public void ImmediateSave(ISavable value)
|
||||
{
|
||||
var name = value.ToFilename(FileNames);
|
||||
try
|
||||
{
|
||||
if (name.Length == 0)
|
||||
{
|
||||
throw new Exception("Invalid object returned empty filename.");
|
||||
}
|
||||
|
||||
_log.Debug($"Saving {value.TypeName} {value.LogName(name)}...");
|
||||
var file = new FileInfo(name);
|
||||
file.Directory?.Create();
|
||||
using var s = file.Exists ? file.Open(FileMode.Truncate) : file.Open(FileMode.CreateNew);
|
||||
using var w = new StreamWriter(s, Encoding.UTF8);
|
||||
value.Save(w);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.Error($"Could not save {value.GetType().Name} {value.LogName(name)}:\n{ex}");
|
||||
}
|
||||
}
|
||||
|
||||
public void ImmediateDelete(ISavable value)
|
||||
{
|
||||
var name = value.ToFilename(FileNames);
|
||||
try
|
||||
{
|
||||
if (name.Length == 0)
|
||||
{
|
||||
throw new Exception("Invalid object returned empty filename.");
|
||||
}
|
||||
|
||||
if (!File.Exists(name))
|
||||
return;
|
||||
|
||||
_log.Information($"Deleting {value.GetType().Name} {value.LogName(name)}...");
|
||||
File.Delete(name);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.Error($"Could not delete {value.GetType().Name} {value.LogName(name)}:\n{ex}");
|
||||
}
|
||||
}
|
||||
: base(log, framework, fileNames)
|
||||
{ }
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue