Большой рефактор

This commit is contained in:
2025-04-26 16:56:29 +04:00
parent 9d0683a23f
commit 76167f1b24
83 changed files with 943 additions and 864 deletions
+62 -42
View File
@@ -1,7 +1,5 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Pf2eModel;
using Pf2eModel.Model;
using Pf2eModel.Model.Base;
@@ -39,64 +37,86 @@ internal class EntityConverter : JsonConverter
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
Dictionary<string, object?> dictionary = DictionarySerialization(value!);
if (value == null)
{
serializer.Serialize(writer, null);
return;
}
Dictionary<string, object?> dictionary = DictionarySerialization(value);
if (value.GetType() != typeof(Entity))
{
dictionary.Remove("Entity");
Processor? processor = _processors.FirstOrDefault(p => p.Type == value.GetType());
processor?.Process(dictionary, (IEntity)value, this);
}
switch (value)
else
{
case Entity entity:
ProcessEntity(dictionary, entity);
break;
case Trait trait:
ProcessTrait(dictionary, trait);
break;
dictionary.Remove("Description");
}
serializer.Serialize(writer, dictionary);
}
/*
writer.WriteStartObject();
private abstract class Processor
{
public abstract Type Type { get; }
foreach (KeyValuePair<string, object> keyValuePair in dictionary)
public abstract void Process(
Dictionary<string, object?> dictionary,
IEntity entity,
EntityConverter converter
);
}
private class Processor<T>(Action<Dictionary<string, object?>, T, EntityConverter> method)
: Processor
where T : IEntity
{
public override Type Type { get; } = typeof(T);
public override void Process(
Dictionary<string, object?> dictionary,
IEntity entity,
EntityConverter converter
)
{
writer.WritePropertyName(keyValuePair.Key, true);
serializer.Serialize(writer, keyValuePair.Value);
method.Invoke(dictionary, (T)entity, converter);
}
writer.WriteEndObject();
*/
}
private void ProcessTrait(Dictionary<string, object?> dictionary, Trait trait)
{
LocaleString name =
trait.Entity?.Name
?? _context.Entities.Where(e => e.Id == trait.Id).Select(e => e.Name).FirstOrDefault()!;
private readonly ICollection<Processor> _processors =
[
new Processor<Trait>(
(dictionary, trait, converter) =>
{
LocaleString name =
// ReSharper disable once ConditionalAccessQualifierIsNonNullableAccordingToAPIContract
trait.Entity?.Name
?? converter
._context.Entities.Where(e => e.Id == trait.Id)
.Select(e => e.Name)
.FirstOrDefault()!;
dictionary.Remove("Entity");
dictionary.Remove("Entities");
dictionary.Add("Name", name.ToString());
}
private void ProcessEntity(Dictionary<string, object?> dictionary, Entity entity)
{
dictionary.Remove("Description");
}
private void ProcessSubclass(Dictionary<string, object?> dictionary, Subclass entity)
{
dictionary.Remove("Group");
}
dictionary.Remove("Entity");
dictionary.Remove("Entities");
dictionary.Add("Name", name.ToString());
}
),
new Processor<Subclass>(
(dictionary, _, _) =>
{
dictionary.Remove("Group");
}
),
];
public override bool CanRead => false;
public override object? ReadJson(
public override object ReadJson(
JsonReader reader,
Type objectType,
object? existingValue,