A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SimpleEntitySerializer.cs 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using Svelto.ECS;
  3. using Svelto.ECS.Serialization;
  4. using RobocraftX.Common;
  5. namespace GamecraftModdingAPI.Persistence
  6. {
  7. /// <summary>
  8. /// Simple entity serializer sufficient for simple entity components.
  9. /// </summary>
  10. public class SimpleEntitySerializer<Descriptor> : IEntitySerializer where Descriptor : ISerializableEntityDescriptor, new()
  11. {
  12. /// <summary>
  13. /// Delegate function called when the serializer needs to know what entities to serialize.
  14. /// </summary>
  15. public delegate EGID[] GetEntitiesToSerialize(EntitiesDB entitiesDB);
  16. private GetEntitiesToSerialize getEntitiesToSerialize;
  17. protected int serializationType;
  18. public EntitiesDB entitiesDB { set; protected get; }
  19. public EntityComponentInitializer BuildDeserializedEntity(EGID egid, ISerializationData serializationData, ISerializableEntityDescriptor entityDescriptor, int serializationType, IEntitySerialization entitySerialization, IEntityFactory factory, bool enginesRootIsDeserializationOnly)
  20. {
  21. EntityComponentInitializer esi = factory.BuildEntity<Descriptor>(egid);
  22. entitySerialization.DeserializeEntityComponents(serializationData, entityDescriptor, ref esi, serializationType);
  23. return esi;
  24. }
  25. public bool Deserialize(ref ISerializationData serializationData, IEntitySerialization entitySerializer)
  26. {
  27. BinaryBufferReader bbr = new BinaryBufferReader(serializationData.data.ToArrayFast(out uint count), serializationData.dataPos);
  28. uint entityCount = bbr.ReadUint();
  29. serializationData.dataPos = bbr.Position;
  30. for (uint i = 0; i < entityCount; i++)
  31. {
  32. entitySerializer.DeserializeEntity(serializationData, serializationType);
  33. }
  34. return true;
  35. }
  36. public void Ready() { }
  37. public bool Serialize(ref ISerializationData serializationData, EntitiesDB entitiesDB, IEntitySerialization entitySerializer)
  38. {
  39. serializationData.data.ExpandBy(4u);
  40. BinaryBufferWriter bbw = new BinaryBufferWriter(serializationData.data.ToArrayFast(out uint count), serializationData.dataPos);
  41. EGID[] toSerialize = getEntitiesToSerialize(entitiesDB);
  42. bbw.Write((uint)toSerialize.Length);
  43. serializationData.dataPos = bbw.Position;
  44. for (uint i = 0; i < toSerialize.Length; i++)
  45. {
  46. entitySerializer.SerializeEntity(toSerialize[i], serializationData, serializationType);
  47. }
  48. return true;
  49. }
  50. /// <summary>
  51. /// Construct the entity serializer.
  52. /// </summary>
  53. /// <param name="getEntitiesToSerialize">Provider of entity IDs to serialize.</param>
  54. public SimpleEntitySerializer(GetEntitiesToSerialize getEntitiesToSerialize)
  55. {
  56. this.getEntitiesToSerialize = getEntitiesToSerialize;
  57. serializationType = (int)SerializationType.Storage;
  58. }
  59. }
  60. }