36 lines
1.1 KiB
C#
36 lines
1.1 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using Tiger.Storage;
|
|
|
|
namespace Tiger.Game.Settings;
|
|
|
|
public class SettingManager : ISettingManager
|
|
{
|
|
private IReadOnlyDictionary<string, string> _settings;
|
|
private readonly IRepository<Setting> _settingRepository;
|
|
private readonly ILogger<ISettingManager> _logger;
|
|
|
|
public SettingManager(IRepository<Setting> settingRepository, ILogger<ISettingManager> logger)
|
|
{
|
|
_settingRepository = settingRepository;
|
|
_logger = logger;
|
|
_settings = new Dictionary<string, string>();
|
|
}
|
|
|
|
public async Task ReloadSettingsAsync()
|
|
{
|
|
_settings = (await _settingRepository.FindByAsync()).ToDictionary(s => s.Key, s => s.Value);
|
|
|
|
_logger.LogInformation("Loaded {Count} settings", _settings.Count);
|
|
}
|
|
|
|
public T GetSetting<T>(string key)
|
|
{
|
|
if (!_settings.TryGetValue(key, out var value))
|
|
throw new KeyNotFoundException($"No setting found with key '{key}'");
|
|
|
|
if (value is T castedValue)
|
|
return castedValue;
|
|
|
|
throw new InvalidCastException($"Setting '{key}' is not of type '{typeof(T)}'");
|
|
}
|
|
} |