convert to C# & update config system
This commit is contained in:
36
Pilz.Configuration/Settings.cs
Normal file
36
Pilz.Configuration/Settings.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Pilz.Configuration
|
||||
{
|
||||
public class Settings : ISettings
|
||||
{
|
||||
[JsonProperty(nameof(Settings))]
|
||||
protected readonly Dictionary<string, IChildSettings> mySettings = [];
|
||||
|
||||
[JsonIgnore]
|
||||
public IReadOnlyCollection<IChildSettings> Childs => mySettings.Values;
|
||||
|
||||
public T Get<T>() where T : IChildSettings, ISettingsIdentifier
|
||||
{
|
||||
if (mySettings.TryGetValue(T.Identifier, out IChildSettings valueExisting) && valueExisting is T settingsExisting)
|
||||
return settingsExisting;
|
||||
|
||||
if (Activator.CreateInstance<T>() is T settingsNew)
|
||||
{
|
||||
mySettings.Add(T.Identifier, settingsNew);
|
||||
return settingsNew;
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
foreach (var s in mySettings.Values)
|
||||
s.Reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user