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.

SerializerManager.cs 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Svelto.ECS;
  5. using Svelto.ECS.Serialization;
  6. using GamecraftModdingAPI.Utility;
  7. namespace GamecraftModdingAPI.Persistence
  8. {
  9. /// <summary>
  10. /// Keeps track of serializers.
  11. /// This is used to add and retrieve serializers.
  12. /// Added IEntitySerializations are used in serializing and deserializing Gamecraft save files (GameSave.GC).
  13. /// </summary>
  14. public static class SerializerManager
  15. {
  16. private static Dictionary<string, IEntitySerializer> _serializers = new Dictionary<string, IEntitySerializer>();
  17. private static Dictionary<string, Action<IEntitySerialization>> _registrations = new Dictionary<string, Action<IEntitySerialization>>();
  18. private static EnginesRoot _lastEnginesRoot;
  19. public static void AddSerializer<T>(IEntitySerializer serializer) where T : ISerializableEntityDescriptor, new()
  20. {
  21. string name = typeof(T).FullName;
  22. _serializers[name] = serializer;
  23. _registrations[name] = (IEntitySerialization ies) => { ies.RegisterSerializationFactory<T>(serializer); };
  24. if (_lastEnginesRoot != null)
  25. {
  26. serializer.EntityFactory = _lastEnginesRoot.GenerateEntityFactory();
  27. _registrations[name].Invoke(_lastEnginesRoot.GenerateEntitySerializer());
  28. _lastEnginesRoot.AddEngine(serializer);
  29. }
  30. }
  31. public static bool ExistsSerializer(string name)
  32. {
  33. return _serializers.ContainsKey(name);
  34. }
  35. public static bool ExistsSerializer<T>(IEntitySerializer serializer) where T : ISerializableEntityDescriptor, new()
  36. {
  37. return ExistsSerializer(typeof(T).FullName);
  38. }
  39. public static IEntitySerializer GetSerializer(string name)
  40. {
  41. return _serializers[name];
  42. }
  43. public static string[] GetSerializerNames()
  44. {
  45. return _serializers.Keys.ToArray();
  46. }
  47. public static int GetSerializersCount()
  48. {
  49. return _serializers.Count;
  50. }
  51. public static void RegisterSerializers(EnginesRoot enginesRoot)
  52. {
  53. _lastEnginesRoot = enginesRoot;
  54. IEntityFactory factory = enginesRoot.GenerateEntityFactory();
  55. IEntitySerialization ies = enginesRoot.GenerateEntitySerializer();
  56. foreach (string key in _serializers.Keys)
  57. {
  58. Logging.MetaDebugLog($"Registering IEntitySerializer for {key}");
  59. _serializers[key].EntityFactory = factory;
  60. _registrations[key].Invoke(ies);
  61. enginesRoot.AddEngine(_serializers[key]);
  62. }
  63. }
  64. }
  65. }