EffectManagerSerializer.cs 1.5 KB

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