2020-04-29 01:56:34 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
using Svelto.ECS;
|
|
|
|
|
using Svelto.ECS.Serialization;
|
|
|
|
|
|
|
|
|
|
using RobocraftX.Common;
|
|
|
|
|
|
|
|
|
|
namespace GamecraftModdingAPI.Persistence
|
|
|
|
|
{
|
2020-04-29 02:59:36 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Simple entity serializer sufficient for simple entity components.
|
|
|
|
|
/// </summary>
|
2020-04-29 01:56:34 +00:00
|
|
|
|
public class SimpleEntitySerializer<Descriptor> : IEntitySerializer where Descriptor : ISerializableEntityDescriptor, new()
|
|
|
|
|
{
|
2020-04-29 02:59:36 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Delegate function called when the serializer needs to know what entities to serialize.
|
|
|
|
|
/// </summary>
|
2020-04-29 01:56:34 +00:00
|
|
|
|
public delegate EGID[] GetEntitiesToSerialize(EntitiesDB entitiesDB);
|
|
|
|
|
|
|
|
|
|
private GetEntitiesToSerialize getEntitiesToSerialize;
|
|
|
|
|
|
|
|
|
|
protected int serializationType;
|
|
|
|
|
|
|
|
|
|
public EntitiesDB entitiesDB { set; protected get; }
|
|
|
|
|
|
2021-04-10 00:02:47 +00:00
|
|
|
|
public EntityInitializer BuildDeserializedEntity(EGID egid, ISerializationData serializationData, ISerializableEntityDescriptor entityDescriptor, int serializationType, IEntitySerialization entitySerialization, IEntityFactory factory, bool enginesRootIsDeserializationOnly)
|
2020-04-29 01:56:34 +00:00
|
|
|
|
{
|
2021-04-10 00:02:47 +00:00
|
|
|
|
EntityInitializer esi = factory.BuildEntity<Descriptor>(egid);
|
2020-04-29 01:56:34 +00:00
|
|
|
|
entitySerialization.DeserializeEntityComponents(serializationData, entityDescriptor, ref esi, serializationType);
|
|
|
|
|
return esi;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Deserialize(ref ISerializationData serializationData, IEntitySerialization entitySerializer)
|
|
|
|
|
{
|
|
|
|
|
BinaryBufferReader bbr = new BinaryBufferReader(serializationData.data.ToArrayFast(out uint count), serializationData.dataPos);
|
|
|
|
|
uint entityCount = bbr.ReadUint();
|
|
|
|
|
serializationData.dataPos = bbr.Position;
|
|
|
|
|
for (uint i = 0; i < entityCount; i++)
|
|
|
|
|
{
|
|
|
|
|
entitySerializer.DeserializeEntity(serializationData, serializationType);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Ready() { }
|
|
|
|
|
|
|
|
|
|
public bool Serialize(ref ISerializationData serializationData, EntitiesDB entitiesDB, IEntitySerialization entitySerializer)
|
|
|
|
|
{
|
|
|
|
|
serializationData.data.ExpandBy(4u);
|
|
|
|
|
BinaryBufferWriter bbw = new BinaryBufferWriter(serializationData.data.ToArrayFast(out uint count), serializationData.dataPos);
|
|
|
|
|
EGID[] toSerialize = getEntitiesToSerialize(entitiesDB);
|
|
|
|
|
bbw.Write((uint)toSerialize.Length);
|
|
|
|
|
serializationData.dataPos = bbw.Position;
|
|
|
|
|
for (uint i = 0; i < toSerialize.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
entitySerializer.SerializeEntity(toSerialize[i], serializationData, serializationType);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 02:59:36 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Construct the entity serializer.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="getEntitiesToSerialize">Provider of entity IDs to serialize.</param>
|
2020-04-29 01:56:34 +00:00
|
|
|
|
public SimpleEntitySerializer(GetEntitiesToSerialize getEntitiesToSerialize)
|
|
|
|
|
{
|
|
|
|
|
this.getEntitiesToSerialize = getEntitiesToSerialize;
|
|
|
|
|
serializationType = (int)SerializationType.Storage;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|