feat: entity finder added
This commit is contained in:
parent
336e7e16e7
commit
82cc25a9ef
70
Engine.Serialization/Converters/EntityFinder.cs
Normal file
70
Engine.Serialization/Converters/EntityFinder.cs
Normal file
@ -0,0 +1,70 @@
|
||||
using System.Collections;
|
||||
using System.Reflection;
|
||||
|
||||
using Syntriax.Engine.Core;
|
||||
|
||||
namespace Syntriax.Engine.Serialization;
|
||||
|
||||
public class EntityFinder
|
||||
{
|
||||
private readonly HashSet<IEntity> _entities = [];
|
||||
|
||||
public IReadOnlyCollection<IEntity> Entities => _entities;
|
||||
|
||||
public void FindEntitiesUnder(object @object)
|
||||
{
|
||||
TypeData typeData = Utils.GetTypeData(@object.GetType());
|
||||
|
||||
if (@object is IEntity entity)
|
||||
{
|
||||
if (_entities.Add(entity))
|
||||
{
|
||||
Console.WriteLine($"Found Entity: {entity.Id}");
|
||||
FindEntitiesUnder(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Duplicate Entity: {entity.Id}");
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (PropertyInfo propertyInfo in typeData.Properties)
|
||||
{
|
||||
if (propertyInfo.PropertyType?.IsPrimitive ?? true || propertyInfo.PropertyType == typeof(string))
|
||||
continue;
|
||||
|
||||
if (propertyInfo.HasAttribute<IgnoreSerializationAttribute>())
|
||||
continue;
|
||||
|
||||
object? value = propertyInfo.GetValue(@object);
|
||||
|
||||
if (value is IEnumerable enumerable && value.GetType() != typeof(string))
|
||||
foreach (object? listObject in enumerable)
|
||||
FindEntitiesUnder(listObject);
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
object? value = fieldInfo.GetValue(@object);
|
||||
|
||||
if (value is IEnumerable enumerable && value.GetType() != typeof(string))
|
||||
foreach (object? listObject in enumerable)
|
||||
FindEntitiesUnder(listObject);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user