Unofficial CardLife revival project, pronounced like "celery"
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.

SerializedCLreHandshake.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using NetworkFramework.Shared;
  6. namespace CLre_server.API.Synergy
  7. {
  8. struct SerializedCLreHandshake: ISerializedNetData
  9. {
  10. private byte major;
  11. private byte minor;
  12. private byte patch;
  13. private HandshakeFlag flags;
  14. private List<string> modInfo;
  15. public Version Version
  16. {
  17. get => new Version(major, minor, patch);
  18. set
  19. {
  20. major = (byte)value.Major;
  21. minor = (byte)value.Minor;
  22. patch = (byte)value.Build;
  23. }
  24. }
  25. public IEnumerable<string> Mods
  26. {
  27. get => modInfo.ToArray();
  28. set
  29. {
  30. modInfo.Clear();
  31. foreach (var mod in value)
  32. {
  33. modInfo.Add(mod);
  34. }
  35. }
  36. }
  37. public byte[] Serialize()
  38. {
  39. using (MemoryStream stream = new MemoryStream())
  40. {
  41. using (BinaryWriter writer = new BinaryWriter(stream))
  42. {
  43. // version
  44. writer.Write(major);
  45. writer.Write(minor);
  46. writer.Write(patch);
  47. writer.Write((uint)flags);
  48. writer.Write(modInfo.Count);
  49. foreach (string mod in modInfo)
  50. {
  51. writer.Write(mod);
  52. }
  53. return stream.ToArray();
  54. }
  55. }
  56. }
  57. public void Deserialize(byte[] data)
  58. {
  59. using (MemoryStream stream = new MemoryStream(data))
  60. {
  61. using (BinaryReader reader = new BinaryReader(stream))
  62. {
  63. // version
  64. major = reader.ReadByte();
  65. minor = reader.ReadByte();
  66. patch = reader.ReadByte();
  67. flags = (HandshakeFlag) reader.ReadUInt32();
  68. int modCount = reader.ReadInt32();
  69. modInfo = new List<string>(modCount);
  70. for (int i = 0; i < modCount; i++)
  71. {
  72. modInfo.Add(reader.ReadString());
  73. }
  74. }
  75. }
  76. }
  77. public bool HasFlag(HandshakeFlag f)
  78. {
  79. return (flags & f) != HandshakeFlag.None;
  80. }
  81. public void SetFlag(HandshakeFlag f)
  82. {
  83. flags |= f;
  84. }
  85. public void UnsetFlag(HandshakeFlag f)
  86. {
  87. flags &= ~f;
  88. }
  89. public static SerializedCLreHandshake Current()
  90. {
  91. Version v = Assembly.GetExecutingAssembly().GetName().Version;
  92. List<string> mods = new List<string>();
  93. foreach (var plugin in IllusionInjector.PluginManager.Plugins)
  94. {
  95. mods.Add(plugin.Name);
  96. }
  97. return new SerializedCLreHandshake
  98. {
  99. major = (byte) v.Major,
  100. minor = (byte) v.Minor,
  101. patch = (byte) v.Build,
  102. flags = HandshakeFlag.Server,
  103. modInfo = mods,
  104. };
  105. }
  106. public static SerializedCLreHandshake RequireCLre()
  107. {
  108. Version v = Assembly.GetExecutingAssembly().GetName().Version;
  109. List<string> mods = new List<string>(new []{Assembly.GetExecutingAssembly().GetName().Name});
  110. return new SerializedCLreHandshake
  111. {
  112. major = (byte) v.Major,
  113. minor = (byte) v.Minor,
  114. patch = (byte) v.Build,
  115. flags = HandshakeFlag.Client | HandshakeFlag.RequireAll,
  116. modInfo = mods,
  117. };
  118. }
  119. public static SerializedCLreHandshake Empty()
  120. {
  121. return new SerializedCLreHandshake
  122. {
  123. major = 0,
  124. minor = 0,
  125. patch = 0,
  126. modInfo = new List<string>(),
  127. };
  128. }
  129. public override string ToString()
  130. {
  131. return $"CLre {Version} ({modInfo.Count} mods)";
  132. }
  133. }
  134. [Flags]
  135. enum HandshakeFlag : uint
  136. {
  137. None = 0,
  138. Client = 1,
  139. Server = 1 << 1,
  140. RequireAll = 1 << 2,
  141. OptionalAll = 1 << 3,
  142. Confirm = 1 << 4,
  143. }
  144. }