oecs-sharp/OECS/ComponentDescriptor.cs

58 lines
1.9 KiB
C#

namespace OECS;
/// <summary>
/// Describes a component type discovered at compile time by the source generator.
/// Used by <see cref="WorldSerializer"/> to save/load components without reflection.
/// </summary>
public sealed class ComponentDescriptor
{
/// <summary>
/// The assembly-qualified name of the component type, for stable serialization
/// across assemblies.
/// </summary>
public string TypeName { get; }
/// <summary>
/// The <see cref="System.Type"/> of the component.
/// </summary>
public Type Type { get; }
/// <summary>
/// Serializes a boxed component instance to a MessagePack byte array.
/// </summary>
public Func<object, byte[]> Serialize { get; }
/// <summary>
/// Deserializes a MessagePack byte array and adds the component to the entity
/// via the typed <see cref="World.AddComponent{T}"/> method. No reflection.
/// </summary>
public Action<World, Entity, byte[]> DeserializeAndAdd { get; }
/// <summary>
/// Deserializes a MessagePack byte array to a boxed component object.
/// Used by <see cref="WorldSerializer"/> for relationship fixup before adding.
/// </summary>
public Func<byte[], object> Deserialize { get; }
/// <summary>
/// True if this component type is used as a singleton (via <c>SetSingleton</c>).
/// Set by the source generator at compile time.
/// </summary>
public bool IsSingleton { get; }
public ComponentDescriptor(
string typeName,
Type type,
Func<object, byte[]> serialize,
Action<World, Entity, byte[]> deserializeAndAdd,
Func<byte[], object> deserialize,
bool isSingleton = false)
{
TypeName = typeName;
Type = type;
Serialize = serialize;
DeserializeAndAdd = deserializeAndAdd;
Deserialize = deserialize;
IsSingleton = isSingleton;
}
}