Genericise configurations

This is a breaking change. Enables easy creation of config files without copy/pasting the Read/Write code all the time
This commit is contained in:
Chris 2020-12-03 17:42:46 +10:30
parent 3776a71a20
commit a03647ce38
4 changed files with 919 additions and 0 deletions

View file

@ -0,0 +1,103 @@
using Newtonsoft.Json;
using System;
using System.IO;
namespace TShockAPI.Configuration
{
/// <summary>
/// Implements <see cref="IConfigFile{TSettings}"/> to provide a generic config file containing some settings
/// </summary>
/// <typeparam name="TSettings"></typeparam>
public class ConfigFile<TSettings> : IConfigFile<TSettings>
{
/// <summary>
/// Settings managed by this config file
/// </summary>
public virtual TSettings Settings { get; set; }
/// <summary>
/// Action invoked when the config file is read
/// </summary>
public static Action<ConfigFile<TSettings>> OnConfigRead;
/// <summary>
/// Reads json-formatted settings from a given path
/// </summary>
/// <param name="path">The path to the file containing the settings</param>
/// <param name="incompleteSettings">
/// Whether the config object has any new fields in it, meaning that the config file has to be
/// overwritten.
/// </param>
/// <returns>Settings object</returns>
public virtual TSettings Read(string path, out bool incompleteSettings)
{
if (!File.Exists(path))
{
incompleteSettings = true;
return default;
}
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
return Read(fs, out incompleteSettings);
}
}
/// <summary>
/// Reads json-formatted settings from a given stream
/// </summary>
/// <param name="stream">stream</param>
/// <param name="incompleteSettings">
/// Whether the config object has any new fields in it, meaning that the config file has to be
/// overwritten.
/// </param>
/// <returns>Settings object</returns>
public virtual TSettings Read(Stream stream, out bool incompleteSettings)
{
using (var sr = new StreamReader(stream))
{
return ConvertJson(sr.ReadToEnd(), out incompleteSettings);
}
}
/// <summary>
/// Converts a json-formatted string into the settings object used by this configuration
/// </summary>
/// <param name="json">Json string to parse</param>
/// <param name="incompleteSettings">Whether or not the json string contained an incomplete set of settings</param>
/// <returns>Settings object</returns>
public virtual TSettings ConvertJson(string json, out bool incompleteSettings)
{
var settings = FileTools.LoadConfigAndCheckForMissingFields<TSettings>(json, out incompleteSettings);
Settings = settings;
OnConfigRead?.Invoke(this);
return settings;
}
/// <summary>
/// Writes the configuration to a given path
/// </summary>
/// <param name="path">string path - Location to put the config file</param>
public virtual void Write(string path)
{
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Write))
{
Write(fs);
}
}
/// <summary>
/// Writes the configuration to a stream
/// </summary>
/// <param name="stream">stream</param>
public virtual void Write(Stream stream)
{
var str = JsonConvert.SerializeObject(this, Formatting.Indented);
using (var sw = new StreamWriter(stream))
{
sw.Write(str);
}
}
}
}