67 lines
1.8 KiB
C#
67 lines
1.8 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
using ServerCore;
|
|
|
|
|
|
public class ConfigManagerConfigurationProvider : ConfigurationProvider
|
|
{
|
|
private readonly ConfigManager m_config_manager;
|
|
|
|
public ConfigManagerConfigurationProvider(ConfigManager configManager)
|
|
{
|
|
m_config_manager = configManager;
|
|
|
|
Load();
|
|
}
|
|
|
|
public override void Load()
|
|
{
|
|
var data = new Dictionary<string, string>();
|
|
|
|
foreach (var jObj in m_config_manager.getAllPaths())
|
|
{
|
|
var jtoken = m_config_manager.getRawJsonByPath(jObj);
|
|
if (jtoken == null) continue;
|
|
|
|
flattenJson(jtoken, data, parentPath: null);
|
|
}
|
|
|
|
Data = data!;
|
|
}
|
|
|
|
private void flattenJson(JToken token, IDictionary<string, string> data, string? parentPath)
|
|
{
|
|
NullReferenceCheckHelper.throwIfNull(token, () => $"token is null !!!");
|
|
|
|
switch (token.Type)
|
|
{
|
|
case JTokenType.Object:
|
|
foreach (var prop in (JObject)token)
|
|
{
|
|
var path = parentPath == null ? prop.Key : $"{parentPath}:{prop.Key}";
|
|
NullReferenceCheckHelper.throwIfNull(prop.Value, () => $"prop.Value is null !!!");
|
|
flattenJson(prop.Value, data, path);
|
|
}
|
|
break;
|
|
|
|
case JTokenType.Array:
|
|
var index = 0;
|
|
foreach (var item in (JArray)token)
|
|
{
|
|
var path = $"{parentPath}:{index++}";
|
|
flattenJson(item, data, path);
|
|
}
|
|
break;
|
|
|
|
default:
|
|
if (token is JValue value && parentPath != null)
|
|
{
|
|
data[parentPath] = value.ToString();
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|