|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Reflection;
- using NetworkFramework.Shared;
-
- namespace CLre_server.API.Synergy
- {
- struct SerializedCLreHandshake: ISerializedNetData
- {
- private byte major;
- private byte minor;
- private byte patch;
-
- private HandshakeFlag flags;
-
- private List<string> modInfo;
-
- public Version Version
- {
- get => new Version(major, minor, patch);
- set
- {
- major = (byte)value.Major;
- minor = (byte)value.Minor;
- patch = (byte)value.Build;
- }
- }
-
- public IEnumerable<string> Mods
- {
- get => modInfo.ToArray();
- set
- {
- modInfo.Clear();
- foreach (var mod in value)
- {
- modInfo.Add(mod);
- }
- }
- }
-
- public byte[] Serialize()
- {
- using (MemoryStream stream = new MemoryStream())
- {
- using (BinaryWriter writer = new BinaryWriter(stream))
- {
- // version
- writer.Write(major);
- writer.Write(minor);
- writer.Write(patch);
- writer.Write((uint)flags);
- writer.Write(modInfo.Count);
- foreach (string mod in modInfo)
- {
- writer.Write(mod);
- }
- return stream.ToArray();
- }
- }
- }
-
- public void Deserialize(byte[] data)
- {
- using (MemoryStream stream = new MemoryStream(data))
- {
- using (BinaryReader reader = new BinaryReader(stream))
- {
- // version
- major = reader.ReadByte();
- minor = reader.ReadByte();
- patch = reader.ReadByte();
- flags = (HandshakeFlag) reader.ReadUInt32();
- int modCount = reader.ReadInt32();
- modInfo = new List<string>(modCount);
- for (int i = 0; i < modCount; i++)
- {
- modInfo.Add(reader.ReadString());
- }
- }
- }
- }
-
- public bool HasFlag(HandshakeFlag f)
- {
- return (flags & f) != HandshakeFlag.None;
- }
-
- public void SetFlag(HandshakeFlag f)
- {
- flags |= f;
- }
-
- public void UnsetFlag(HandshakeFlag f)
- {
- flags &= ~f;
- }
-
- public static SerializedCLreHandshake Current()
- {
- Version v = Assembly.GetExecutingAssembly().GetName().Version;
- List<string> mods = new List<string>();
- foreach (var plugin in IllusionInjector.PluginManager.Plugins)
- {
- mods.Add(plugin.Name);
- }
-
- return new SerializedCLreHandshake
- {
- major = (byte) v.Major,
- minor = (byte) v.Minor,
- patch = (byte) v.Build,
- flags = HandshakeFlag.Server,
- modInfo = mods,
- };
- }
-
- public static SerializedCLreHandshake RequireCLre()
- {
- Version v = Assembly.GetExecutingAssembly().GetName().Version;
- List<string> mods = new List<string>(new []{Assembly.GetExecutingAssembly().GetName().Name});
-
- return new SerializedCLreHandshake
- {
- major = (byte) v.Major,
- minor = (byte) v.Minor,
- patch = (byte) v.Build,
- flags = HandshakeFlag.Client | HandshakeFlag.RequireAll,
- modInfo = mods,
- };
- }
-
- public static SerializedCLreHandshake Empty()
- {
- return new SerializedCLreHandshake
- {
- major = 0,
- minor = 0,
- patch = 0,
- modInfo = new List<string>(),
- };
- }
-
- public override string ToString()
- {
- return $"CLre {Version} ({modInfo.Count} mods)";
- }
- }
-
- [Flags]
- enum HandshakeFlag : uint
- {
- None = 0,
- Client = 1,
- Server = 1 << 1,
- RequireAll = 1 << 2,
- OptionalAll = 1 << 3,
- Confirm = 1 << 4,
- }
- }
|