Rework IConfigurationProvider API

This commit is contained in:
ghorsington committed 2021-06-27 21:36:21 +03:00
1 parent b510b2a7bb
commit 4a7fb01175
4 files changed
+75 -41

No files matched your search

+11 -10
View File
@@ -16,30 +16,31 @@ namespace BepInEx.Configuration
{
private readonly BepInPlugin _ownerMetadata;
/// <inheritdoc cref="ConfigFile(string, bool, BepInPlugin)" />
/// <inheritdoc />
public ConfigFile(string configPath, bool saveOnInit) : this(configPath, saveOnInit, null) { }
/// <inheritdoc />
public ConfigFile(string configPath, bool saveOnInit, BepInPlugin ownerMetadata) : this(new LegacyConfigurationProvider(configPath), saveOnInit, ownerMetadata) { }
/// <summary>
/// Create a new config file at the specified config path.
/// </summary>
/// <param name="configPath">Full path to a file that contains settings. The file will be created as needed.</param>
/// <param name="saveOnInit">If the config file/directory doesn't exist, create it immediately.</param>
/// <param name="ownerMetadata">Information about the plugin that owns this setting file.</param>
public ConfigFile(string configPath, bool saveOnInit, BepInPlugin ownerMetadata)
public ConfigFile(IConfigurationProvider configurationProvider, bool saveOnInit, BepInPlugin ownerMetadata)
{
_ownerMetadata = ownerMetadata;
if (configPath == null) throw new ArgumentNullException(nameof(configPath));
configPath = Path.GetFullPath(configPath);
ConfigFilePath = configPath;
if (File.Exists(ConfigFilePath))
Reload();
else if (saveOnInit) Save();
ConfigurationProvider = configurationProvider;
Reload();
if (saveOnInit) Save();
}
public static ConfigFile CoreConfig { get; } = new(Paths.BepInExConfigPath, true);
public IConfigurationProvider ConfigurationProvider { get; set; }
/// <summary>
/// All config entries inside
/// </summary>
@@ -1,17 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace BepInEx.Configuration
{
public static class ConfigurationPath
{
public static readonly string KeyDelimiter = ":";
public static string Combine(IEnumerable<string> segements)
{
segements = segements ?? throw new ArgumentNullException(nameof(segements));
return string.Join(KeyDelimiter, segements.ToArray());
}
}
}
@@ -1,13 +1,26 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
namespace BepInEx.Configuration
{
public interface IConfigurationProvider
{
public IDictionary<string, string> RawData { get; }
public void Load();
public void Load(string resourceUri);
public void Save();
public void Save(string resourceUri);
public ConfigurationNode Get(string[] path);
public void Set(string[] path, ConfigurationNode node);
public IEnumerable<string[]> EntryPaths { get; }
}
public class ConfigurationNode
{
public string Value { get; set; }
public string Comment { get; set; }
public Type ValueType { get; set; }
}
}
@@ -6,17 +6,40 @@ namespace BepInEx.Configuration
{
public class LegacyConfigurationProvider : IConfigurationProvider
{
public IDictionary<string, string> RawData { get; private set; }
private string filePath;
public void Load(string resourceUri)
private Dictionary<string[], string> items = new(new ArrayComparer());
private class ArrayComparer : IEqualityComparer<string[]>
{
if (!File.Exists(resourceUri))
throw new FileNotFoundException(resourceUri);
public bool Equals(string[] x, string[] y)
{
if (ReferenceEquals(x, y)) return true;
if (ReferenceEquals(x, null)) return false;
if (ReferenceEquals(y, null)) return false;
return x.GetType() == y.GetType() && x.SequenceEqual(y);
}
public int GetHashCode(string[] arr)
{
return arr.Aggregate(1009, (current, s) => current * 9176 + s.GetHashCode());
}
}
public LegacyConfigurationProvider(string filePath)
{
if (!File.Exists(filePath))
throw new FileNotFoundException(filePath);
this.filePath = filePath;
}
public void Load()
{
items.Clear();
var dict = new Dictionary<string, string>();
var currentSection = new string[0];
foreach (var rawLine in File.ReadAllLines(resourceUri))
foreach (var rawLine in File.ReadAllLines(filePath))
{
var line = rawLine.Trim();
@@ -36,12 +59,26 @@ namespace BepInEx.Configuration
var currentKey = split[0].Trim();
var currentValue = split[1].Trim();
dict[ConfigurationPath.Combine(currentSection.Concat(new []{ currentKey }))] = currentValue;
items[currentSection.Concat(new[] { currentKey }).ToArray()] = currentValue;
}
RawData = dict;
}
public void Save(string resourceUri) => throw new System.NotImplementedException();
// TODO: Write to TOML instead
public void Save() => throw new System.NotImplementedException();
public ConfigurationNode Get(string[] path)
{
if (!items.TryGetValue(path, out var value))
return null;
return new ConfigurationNode
{
ValueType = typeof(string),
Value = value
};
}
public void Set(string[] path, ConfigurationNode node) => items[path] = node.Value;
public IEnumerable<string[]> EntryPaths => items.Keys.ToList();
}
}