This commit is contained in:
Pilzinsel64
2025-07-10 06:57:09 +02:00
parent b9c6054185
commit c7b2a07dd0
2 changed files with 30 additions and 4 deletions

View File

@@ -7,10 +7,11 @@
</PropertyGroup>
<PropertyGroup>
<Version>3.2.4</Version>
<Version>3.2.5</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Castle.Core" Version="5.2.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

View File

@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using Castle.Core.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
@@ -12,6 +13,7 @@ public class Settings : ISettings
protected JsonSerializerSettings serializerSettings;
public virtual IReadOnlyCollection<ISettingsNode> Childs => settings.Values;
public virtual ILogger Logger { get; set; } = NullLogger.Instance;
public virtual T Get<T>() where T : ISettingsNode, ISettingsIdentifier
{
@@ -23,7 +25,10 @@ public class Settings : ISettings
if (Activator.CreateInstance<T>() is T settingsNew)
settingsNew.Reset();
else
{
settingsNew = default;
Logger.WarnFormat("Not able to create new settings node instance {0}.", T.Identifier);
}
// Try deserialize
if (settingsJson.TryGetValue(T.Identifier, out var valueRaw))
@@ -32,11 +37,31 @@ public class Settings : ISettings
// Populate
if (settingsNew != null)
{
try
{
serializer.Populate(valueRaw.CreateReader(), settingsNew);
Logger.InfoFormat("Populated settings node {0}.", T.Identifier);
}
catch (Exception ex)
{
Logger.Error("Error populating settings node instance.", ex);
}
}
// Deserialize (fallback)
else if (valueRaw.ToObject<T>() is T settingsDeserialized)
{
try
{
settingsNew = settingsDeserialized;
Logger.WarnFormat("Deserialied settings node {0} via fallback variant.", T.Identifier);
}
catch (Exception ex)
{
Logger.Error("Error deserializing settings node instance.", ex);
}
}
}
// Remember