using System.IO; using YamlDotNet.Serialization; namespace TrueCraft.API { /// /// Abstract base class for configurations read from YAML files. /// public abstract class Configuration { /// /// Creates and returns a new configuration read from a YAML file. /// /// The configuration type. /// The path to the YAML file. /// public static T LoadConfiguration(string configFileName) where T : new() { T config; if (File.Exists(configFileName)) { var deserializer = new Deserializer(ignoreUnmatched: true); using (var file = File.OpenText(configFileName)) config = deserializer.Deserialize(file); } else { config = new T(); } var serializer = new Serializer(); using (var writer = new StreamWriter(configFileName)) serializer.Serialize(writer, config); return config; } } }