EffectManagerSerializer.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. namespace MeidoPhotoStudio.Plugin;
  6. public class EffectManagerSerializer : Serializer<EffectManager>
  7. {
  8. private const short version = 1;
  9. private static Dictionary<Type, IEffectManager> GetEffectManagers(EffectManager manager) =>
  10. Utility.GetFieldValue<EffectManager, Dictionary<Type, IEffectManager>>(manager, "EffectManagers");
  11. public override void Serialize(EffectManager manager, BinaryWriter writer)
  12. {
  13. writer.Write(EffectManager.header);
  14. writer.WriteVersion(version);
  15. foreach (var effectManager in GetEffectManagers(manager).Values)
  16. Serialization.Get(effectManager.GetType()).Serialize(effectManager, writer);
  17. writer.Write(EffectManager.footer);
  18. }
  19. public override void Deserialize(EffectManager manager, BinaryReader reader, SceneMetadata metadata)
  20. {
  21. _ = reader.ReadVersion();
  22. var headerToManager =
  23. GetEffectManagers(manager).ToDictionary(
  24. x => (string)x.Key.GetField("header").GetValue(null),
  25. y => y.Value
  26. );
  27. string header;
  28. while ((header = reader.ReadString()) != EffectManager.footer)
  29. {
  30. var effectManager = headerToManager[header];
  31. Serialization.Get(effectManager.GetType()).Deserialize(effectManager, reader, metadata);
  32. }
  33. }
  34. }