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.

74 lines
2.7KB

  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 IEntityFactory EntityFactory { set; protected get; }
  19. public EntitiesDB entitiesDB { set; protected get; }
  20. public EntityComponentInitializer BuildDeserializedEntity(EGID egid, ISerializationData serializationData, ISerializableEntityDescriptor entityDescriptor, int serializationType, IEntitySerialization entitySerialization)
  21. {
  22. EntityComponentInitializer esi = EntityFactory.BuildEntity<Descriptor>(egid);
  23. entitySerialization.DeserializeEntityComponents(serializationData, entityDescriptor, ref esi, serializationType);
  24. return esi;
  25. }
  26. public bool Deserialize(ref ISerializationData serializationData, IEntitySerialization entitySerializer)
  27. {
  28. BinaryBufferReader bbr = new BinaryBufferReader(serializationData.data.ToArrayFast(out uint count), serializationData.dataPos);
  29. uint entityCount = bbr.ReadUint();
  30. serializationData.dataPos = bbr.Position;
  31. for (uint i = 0; i < entityCount; i++)
  32. {
  33. entitySerializer.DeserializeEntity(serializationData, serializationType);
  34. }
  35. return true;
  36. }
  37. public void Ready() { }
  38. public bool Serialize(ref ISerializationData serializationData, EntitiesDB entitiesDB, IEntitySerialization entitySerializer)
  39. {
  40. serializationData.data.ExpandBy(4u);
  41. BinaryBufferWriter bbw = new BinaryBufferWriter(serializationData.data.ToArrayFast(out uint count), serializationData.dataPos);
  42. EGID[] toSerialize = getEntitiesToSerialize(entitiesDB);
  43. bbw.Write((uint)toSerialize.Length);
  44. serializationData.dataPos = bbw.Position;
  45. for (uint i = 0; i < toSerialize.Length; i++)
  46. {
  47. entitySerializer.SerializeEntity(toSerialize[i], serializationData, serializationType);
  48. }
  49. return true;
  50. }
  51. /// <summary>
  52. /// Construct the entity serializer.
  53. /// </summary>
  54. /// <param name="getEntitiesToSerialize">Provider of entity IDs to serialize.</param>
  55. public SimpleEntitySerializer(GetEntitiesToSerialize getEntitiesToSerialize)
  56. {
  57. this.getEntitiesToSerialize = getEntitiesToSerialize;
  58. serializationType = (int)SerializationType.Storage;
  59. }
  60. }
  61. }