61 lines
1.8 KiB
C#
61 lines
1.8 KiB
C#
using System.Collections;
|
|
using System.Reflection;
|
|
|
|
using Syntriax.Engine.Core;
|
|
|
|
namespace Syntriax.Engine.Serialization;
|
|
|
|
public class EntityFinder
|
|
{
|
|
private readonly Dictionary<string, IEntity> _entities = [];
|
|
|
|
public IReadOnlyDictionary<string, IEntity> Entities => _entities;
|
|
|
|
public void FindEntitiesUnder(object @object)
|
|
{
|
|
TypeData typeData = Utils.GetTypeData(@object.GetType());
|
|
|
|
if (@object is not IEntity entity)
|
|
{
|
|
if (@object is IEnumerable enumerable && @object.GetType() != typeof(string))
|
|
foreach (object? listObject in enumerable)
|
|
FindEntitiesUnder(listObject);
|
|
return;
|
|
}
|
|
|
|
if (Entities.ContainsKey(entity.Id))
|
|
return;
|
|
|
|
_entities.Add(entity.Id, entity);
|
|
|
|
foreach (PropertyInfo propertyInfo in typeData.Properties)
|
|
{
|
|
if (propertyInfo.PropertyType?.IsPrimitive ?? true || propertyInfo.PropertyType == typeof(string))
|
|
continue;
|
|
|
|
if (propertyInfo.HasAttribute<IgnoreSerializationAttribute>())
|
|
continue;
|
|
|
|
if (propertyInfo.GetValue(@object) is object propertyObject)
|
|
FindEntitiesUnder(propertyObject);
|
|
}
|
|
|
|
foreach (FieldInfo fieldInfo in typeData.Fields)
|
|
{
|
|
if (fieldInfo.FieldType?.IsPrimitive ?? true || fieldInfo.FieldType == typeof(string))
|
|
continue;
|
|
|
|
if (fieldInfo.HasAttribute<System.Runtime.CompilerServices.CompilerGeneratedAttribute>())
|
|
continue;
|
|
|
|
// if (!fieldInfo.HasAttribute<SerializeAttribute>())
|
|
// continue;
|
|
|
|
if (fieldInfo.GetValue(@object) is object fieldObject)
|
|
FindEntitiesUnder(fieldObject);
|
|
}
|
|
|
|
FindEntitiesUnder(entity);
|
|
}
|
|
}
|