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.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. _registrations[name].Invoke(_lastEnginesRoot.GenerateEntitySerializer());
  27. _lastEnginesRoot.AddEngine(serializer);
  28. }
  29. }
  30. public static bool ExistsSerializer(string name)
  31. {
  32. return _serializers.ContainsKey(name);
  33. }
  34. public static bool ExistsSerializer<T>(IEntitySerializer serializer) where T : ISerializableEntityDescriptor, new()
  35. {
  36. return ExistsSerializer(typeof(T).FullName);
  37. }
  38. public static IEntitySerializer GetSerializer(string name)
  39. {
  40. return _serializers[name];
  41. }
  42. public static string[] GetSerializerNames()
  43. {
  44. return _serializers.Keys.ToArray();
  45. }
  46. public static int GetSerializersCount()
  47. {
  48. return _serializers.Count;
  49. }
  50. public static void RegisterSerializers(EnginesRoot enginesRoot)
  51. {
  52. _lastEnginesRoot = enginesRoot;
  53. IEntitySerialization ies = enginesRoot.GenerateEntitySerializer();
  54. foreach (string key in _serializers.Keys)
  55. {
  56. Logging.MetaDebugLog($"Registering IEntitySerializer for {key}");
  57. _registrations[key].Invoke(ies);
  58. enginesRoot.AddEngine(_serializers[key]);
  59. }
  60. }
  61. }
  62. }