- Added Cloneable deep clone source generator nuget package

- Configs are no cloned using generated clone, not by serializing/deserializing
- Arrays/Lists (collections in geneeral) are still not cloned properly
- Removed GetRawData from config as it is no longer needed, new clone is very fast
- Added ICloneable<T> which all configs implement
- Cleaned up config classes/code
This commit is contained in:
Kwoth
2021-07-06 17:08:16 +02:00
parent a8a4c9fb44
commit 0fc5f540d8
15 changed files with 237 additions and 236 deletions

View File

@@ -142,7 +142,7 @@ namespace NadekoBot.Services
private Task LogSuccessfulExecution(IUserMessage usrMsg, ITextChannel channel, params int[] execPoints)
{
if (_bss.GetRawData().ConsoleOutputType == ConsoleOutputType.Normal)
if (_bss.Data.ConsoleOutputType == ConsoleOutputType.Normal)
{
Log.Information($"Command Executed after " + string.Join("/", execPoints.Select(x => (x * _oneThousandth).ToString("F3"))) + "s\n\t" +
"User: {0}\n\t" +
@@ -168,7 +168,7 @@ namespace NadekoBot.Services
private void LogErroredExecution(string errorMessage, IUserMessage usrMsg, ITextChannel channel, params int[] execPoints)
{
if (_bss.GetRawData().ConsoleOutputType == ConsoleOutputType.Normal)
if (_bss.Data.ConsoleOutputType == ConsoleOutputType.Normal)
{
Log.Warning($"Command Errored after " + string.Join("/", execPoints.Select(x => (x * _oneThousandth).ToString("F3"))) + "s\n\t" +
"User: {0}\n\t" +

View File

@@ -83,7 +83,7 @@ namespace NadekoBot.Services
.EmbedAsync(new EmbedBuilder()
.WithOkColor()
.WithTitle($"Received Currency")
.AddField("Amount", amount + _gss.Data.Currency.Sign)
.AddField("Amount", amount + sign)
.AddField("Reason", reason));
}
catch

View File

@@ -4,14 +4,10 @@ using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.Json;
using System.Threading.Tasks;
using NadekoBot.Common.Yml;
using NadekoBot.Common;
using NadekoBot.Common.Configs;
using NadekoBot.Common.JsonConverters;
using Rgba32Converter = NadekoBot.Common.JsonConverters.Rgba32Converter;
using CultureInfoConverter = NadekoBot.Common.JsonConverters.CultureInfoConverter;
namespace NadekoBot.Services
{
@@ -20,7 +16,7 @@ namespace NadekoBot.Services
/// </summary>
/// <typeparam name="TSettings">Type of the settings</typeparam>
public abstract class ConfigServiceBase<TSettings> : IConfigService
where TSettings : new()
where TSettings : ICloneable<TSettings>, new()
{
protected readonly string _filePath;
protected readonly IConfigSeria _serializer;
@@ -29,8 +25,8 @@ namespace NadekoBot.Services
protected TSettings _data;
// todo this has to be protected from mutation
public TSettings Data => _data;
// todo future config arrays are not copied - they're not protected from mutations
public TSettings Data => _data.Clone();
public abstract string Name { get; }
@@ -65,21 +61,6 @@ namespace NadekoBot.Services
return default;
}
private static readonly JsonSerializerOptions serializerOptions = new JsonSerializerOptions()
{
MaxDepth = 0,
Converters = { new Rgba32Converter(), new CultureInfoConverter() }
};
private TSettings CreateCopy()
{
var serializedData = JsonSerializer.Serialize(_data, serializerOptions);
return JsonSerializer.Deserialize<TSettings>(serializedData, serializerOptions);
// var serializedData = _serializer.Serialize(_data);
//
// return _serializer.Deserialize<TSettings>(serializedData);
}
/// <summary>
/// Loads data from disk. If file doesn't exist, it will be created with default values
/// </summary>
@@ -114,14 +95,6 @@ namespace NadekoBot.Services
}
public void ModifyConfig(Action<TSettings> action)
{
var copy = CreateCopy();
action(copy);
_data = copy;
Save();
PublishChange();
}
private void Save()
{
var strData = _serializer.Serialize(_data);
@@ -162,9 +135,7 @@ namespace NadekoBot.Services
var expr = (MemberExpression)selector.Body;
var prop = (PropertyInfo)expr.Member;
var expressions = new List<MemberExpression>()
{
};
var expressions = new List<MemberExpression>();
while (true)
{
@@ -224,6 +195,13 @@ namespace NadekoBot.Services
return success;
}
public TSettings GetRawData() => _data;
public void ModifyConfig(Action<TSettings> action)
{
var copy = Data;
action(copy);
_data = copy;
Save();
PublishChange();
}
}
}