Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions PCL.Core.SourceGenerators/ConfigGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -550,11 +550,11 @@ void AccessorInitializer(StringBuilder sb)
sb.Append(indentStr)
.Append("public ").Append(staticKeyword)
.Append("ConfigItem<").Append(typeName).Append("> ")
.Append(configItemName).Append(" => ConfigService.GetConfigItem<")
.Append(configItemName).Append(" { get => field ??= ConfigService.GetConfigItem<")
.Append(typeName)
.Append(">(")
.Append(item.Key.ToLiteral())
.AppendLine(");");
.AppendLine("); } = null!;");

return accessorInitializer;
}
Expand Down
43 changes: 21 additions & 22 deletions PCL.Core/App/Configuration/ConfigItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ ConfigSource source
{
public string Key { get; } = key;

public ConfigSource Source { get; set; } = source;
public ConfigSource Source { get; } = source;

Type ConfigItem.Type => typeof(TValue);
public Type Type => typeof(TValue);

private Func<TValue>? _defaultValueGetter = defaultValue;
private Func<TValue>? _defaultValueConstructor = defaultValue;
private TValue? _defaultValue;
private bool _defaultValueHasSet = false;

Expand All @@ -30,9 +30,9 @@ ConfigSource source
private TValue _GetDefaultValue()
{
if (_defaultValueHasSet) return _defaultValue!;
_defaultValue = _defaultValueGetter!();
_defaultValue = _defaultValueConstructor!();
_defaultValueHasSet = true;
_defaultValueGetter = null;
_defaultValueConstructor = null;
return _defaultValue;
}

Expand All @@ -52,20 +52,19 @@ public ConfigItem(string key, TValue defaultValue, ConfigSource source)

#region 值获取和修改

private readonly IConfigProvider _provider = ConfigService.GetProvider(source);
private IConfigProvider _Provider { get => field ??= ConfigService.GetProvider(Source); } = null!;

private bool _enableCache = true;
private ConfigValueCache<TValue> _valueCache = new();

public bool EnableCache
{
get => _enableCache;
get;
set
{
if (!_enableCache) _valueCache.InvalidateAll();
_enableCache = value;
if (!field) _valueCache.InvalidateAll();
field = value;
}
}
} = true;

/// <summary>
/// 获取配置值。
Expand All @@ -75,11 +74,11 @@ public bool EnableCache
public TValue GetValue(object? argument = null)
{
TValue? value = default; // 这个初始化是多余的,但是煞笔巨硬不初始化会报错
var exists = _enableCache && _valueCache.TryRead(out value, argument);
var exists = EnableCache && _valueCache.TryRead(out value, argument);
if (!exists)
{
exists = _provider.GetValue(Key, out value, argument);
if (exists && _enableCache) _valueCache.Write(value!, argument);
exists = _Provider.GetValue(Key, out value, argument);
if (exists && EnableCache) _valueCache.Write(value!, argument);
}
var e = _TriggerEvent(ConfigEvent.Get, argument, value, true);
if (e != null)
Expand Down Expand Up @@ -109,8 +108,8 @@ public bool SetValue(TValue value, object? argument = null)
if (e.Cancelled) return false;
if (e.NewValueReplacement != null) value = (TValue)e.NewValueReplacement;
}
_provider.SetValue(Key, value, argument);
if (_enableCache) _valueCache.Write(value, argument);
_Provider.SetValue(Key, value, argument);
if (EnableCache) _valueCache.Write(value, argument);
_TriggerEvent(ConfigEvent.Set, argument, value, e: e, isPreview: false);
return true;
}
Expand All @@ -125,7 +124,7 @@ public bool SetValueNoType(object value, object? argument = null)
{
// 兼容龙猫妙妙小代码直接传入 string 值的行为
if (value is string v) return SetValue(v.Convert<TValue>()!, argument);
var msg = $"Value convert failed (required: {typeof(TValue).FullName}, provided: {value.GetType().FullName})";
var msg = $"Value convert failed (required: {Type.FullName}, provided: {value.GetType().FullName})";
throw new InvalidCastException(msg);
}
}
Expand All @@ -139,15 +138,15 @@ public bool Reset(object? argument = null)
{
var e = _TriggerEvent(ConfigEvent.Reset, argument, null, isPreview: true);
if (e is { Cancelled: true }) return false;
_provider.Delete(Key, argument);
if (_enableCache) _valueCache.Invalidate(argument);
_Provider.Delete(Key, argument);
if (EnableCache) _valueCache.Invalidate(argument);
_TriggerEvent(ConfigEvent.Reset, argument, null, isPreview: false);
return true;
}

public bool IsDefault(object? argument = null)
{
var result = !_provider.Exists(Key, argument);
var result = !_Provider.Exists(Key, argument);
var e = _TriggerEvent(ConfigEvent.CheckDefault, argument, result);
if (e is { NewValueReplacement: not null }) result = (bool)e.NewValueReplacement;
return result;
Expand All @@ -172,7 +171,7 @@ public bool Unobserve(ConfigObserver observer)
// 获取值,若未设置则返回 null
private object? _GetValueOrNull(object? argument)
{
var exists = _provider.GetValue<TValue>(Key, out var value, argument);
var exists = _Provider.GetValue<TValue>(Key, out var value, argument);
return exists ? value : null;
}

Expand Down Expand Up @@ -235,7 +234,7 @@ public interface ConfigItem
/// <summary>
/// 配置来源。
/// </summary>
public ConfigSource Source { get; set; }
public ConfigSource Source { get; }

/// <summary>
/// 配置的 CLR 类型。
Expand Down
36 changes: 18 additions & 18 deletions PCL.Core/App/Configuration/ConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using PCL.Core.App.Configuration.Impl;
using PCL.Core.App.Configuration.NTraffic;
using PCL.Core.App.Configuration.Storage;
using PCL.Core.IO;
using PCL.Core.Logging;
using PCL.Core.Utils.Exts;
Expand Down Expand Up @@ -52,7 +51,7 @@ public sealed partial class ConfigService
#region Getters & Setters

/// <summary>
/// 尝试获取配置项的可观察对象
/// 尝试获取无泛型的配置项
/// </summary>
/// <param name="key">配置键</param>
/// <param name="item">返回可观察对象</param>
Expand Down Expand Up @@ -111,10 +110,10 @@ public static void RegisterObserver(IConfigScope scope, ConfigObserver observer)

#region Providers

private static TrafficCenter? _sharedConfigProvider;
private static TrafficCenter? _sharedEncryptedConfigProvider;
private static TrafficCenter? _localConfigProvider;
private static TrafficCenter? _instanceConfigProvider;
private static ConfigStorage? _sharedConfigProvider;
private static ConfigStorage? _sharedEncryptedConfigProvider;
private static ConfigStorage? _localConfigProvider;
private static ConfigStorage? _instanceConfigProvider;

/// <summary>
/// 获取配置提供方。
Expand Down Expand Up @@ -153,9 +152,9 @@ private static void _InitializeProviders()
}
// load
var fileProvider = new JsonFileProvider(SharedConfigPath);
var trafficCenter = new FileTrafficCenter(fileProvider);
_sharedConfigProvider = trafficCenter;
_sharedEncryptedConfigProvider = new EncryptedFileTrafficCenter(trafficCenter);
var storage = new FileConfigStorage(fileProvider);
_sharedConfigProvider = storage;
_sharedEncryptedConfigProvider = new EncryptedFileConfigStorage(storage);
},
() => // local config file
{
Expand All @@ -170,14 +169,15 @@ private static void _InitializeProviders()
]);
// load
var fileProvider = new YamlFileProvider(LocalConfigPath);
_localConfigProvider = new FileTrafficCenter(fileProvider);
_localConfigProvider = new FileConfigStorage(fileProvider);
},
() => // instance config file(s)
{
_instanceConfigProvider = new DynamicCacheTrafficCenter
_instanceConfigProvider = new DynamicCacheConfigStorage
{
TrafficCenterFactory = argument =>
StorageFactory = argument =>
{
ArgumentNullException.ThrowIfNull(argument);
var dir = Path.GetFullPath(argument.ToString()!);
var configPath = Path.Combine(dir, "PCL", "config.v1.yml");
if (!File.Exists(dir)) _TryMigrate(dir, [
Expand All @@ -189,8 +189,8 @@ private static void _InitializeProviders()
}
]);
var fileProvider = new YamlFileProvider(configPath);
var trafficCenter = new FileTrafficCenter(fileProvider);
return trafficCenter;
var storage = new FileConfigStorage(fileProvider);
return storage;
}
};
}
Expand Down Expand Up @@ -255,13 +255,13 @@ private static void _Start()
Context.Info("Config initialization started");
try
{
Context.Trace("Initializing providers...");
_InitializeProviders();
_isProvidersInitialized = true;
Context.Trace("Initializing config items...");
_InitializeConfigItems();
Context.Debug($"Finished initialize {_Items.Count} item(s)");
_isConfigItemsInitialized = true;
Context.Trace("Initializing providers...");
_InitializeProviders();
_isProvidersInitialized = true;
Context.Trace("Initializing observers...");
_InitializeObservers();
Context.Info("Invoking init events...");
Expand Down
2 changes: 1 addition & 1 deletion PCL.Core/App/Configuration/IConfigProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public interface IConfigProvider
/// 获取一个值。
/// </summary>
/// <param name="key">键</param>
/// <param name="value">返回值,若不存在则返回默认值</param>
/// <param name="value">返回值,若不存在则为该类型默认值</param>
/// <param name="argument">上下文参数</param>
/// <typeparam name="T">值的类型</typeparam>
/// <returns>值是否存在,若存在则为 <c>true</c></returns>
Expand Down
56 changes: 0 additions & 56 deletions PCL.Core/App/Configuration/Impl/DynamicCacheTrafficCenter.cs

This file was deleted.

61 changes: 0 additions & 61 deletions PCL.Core/App/Configuration/Impl/EncryptedFileTrafficCenter.cs

This file was deleted.

25 changes: 0 additions & 25 deletions PCL.Core/App/Configuration/Impl/FallbackTrafficCenter.cs

This file was deleted.

Loading
Loading