45 lines
1.2 KiB
C#
45 lines
1.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
|
|
using Engine.Core.Config;
|
|
|
|
namespace Engine.Serializers.Yaml;
|
|
|
|
public class YamlConfiguration : BasicConfiguration
|
|
{
|
|
public readonly string FilePath;
|
|
|
|
private readonly YamlSerializer yamlSerializer = new();
|
|
|
|
public YamlConfiguration(string filePath)
|
|
{
|
|
if (!filePath.EndsWith(".yaml"))
|
|
filePath += ".yaml";
|
|
|
|
FilePath = filePath;
|
|
|
|
bool isRelativePath = Path.GetFullPath(filePath).CompareTo(filePath) != 0;
|
|
|
|
if (isRelativePath)
|
|
FilePath = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filePath));
|
|
|
|
if (Path.GetDirectoryName(FilePath) is string directoryPath)
|
|
Directory.CreateDirectory(directoryPath);
|
|
|
|
if (!File.Exists(FilePath))
|
|
return;
|
|
|
|
string yamlFileText = File.ReadAllText(FilePath);
|
|
Dictionary<string, string> valuePairs = yamlSerializer.Deserialize<Dictionary<string, string>>(yamlFileText);
|
|
|
|
foreach ((string key, string value) in valuePairs)
|
|
Set(key, value);
|
|
}
|
|
|
|
public void Save()
|
|
{
|
|
File.WriteAllText(FilePath, yamlSerializer.Serialize(Values));
|
|
}
|
|
}
|