private void GenerateHandlerReferences(Type type, bool isWorldServer) { IEnumerable <Type> handlerTypes = !isWorldServer?type.Assembly.GetTypes().Where(t => t.Name.Equals("LoginPacketHandler")) // shitty but it works : type.Assembly.GetTypes().Where(p => !p.IsInterface && type.GetInterfaces().FirstOrDefault().IsAssignableFrom(p)); // iterate thru each type in the given assembly foreach (Type handlerType in handlerTypes) { IPacketHandler handler = (IPacketHandler)Activator.CreateInstance(handlerType, new object[] { this }); // include PacketDefinition foreach (MethodInfo methodInfo in handlerType.GetMethods().Where(x => x.GetCustomAttributes(false).OfType <PacketAttribute>().Any() || x.GetParameters().FirstOrDefault()?.ParameterType?.BaseType == typeof(PacketDefinition))) { PacketAttribute packetAttribute = methodInfo.GetCustomAttributes(false).OfType <PacketAttribute>().FirstOrDefault(); // assume PacketDefinition based handler method if (packetAttribute == null) { HandlerMethodReference methodReference = new HandlerMethodReference(DelegateBuilder.BuildDelegate <Action <object, object> >(methodInfo), handler, methodInfo.GetParameters().FirstOrDefault()?.ParameterType); HandlerMethods.Add(methodReference.Identification, methodReference); } else { // assume string based handler method HandlerMethodReference methodReference = new HandlerMethodReference(DelegateBuilder.BuildDelegate <Action <object, object> >(methodInfo), handler, packetAttribute); HandlerMethods.Add(methodReference.Identification, methodReference); } } } }
public PacketObject( string class_name, PacketFacility facility, string alias, PacketPriority prio, PacketAttribute attr, DateTime dt, string info, PacketDirection dir, string src, string dst, byte mark, string message, byte[] data ) { Class = class_name; Facility = facility; Alias = alias; Priority = prio; Attribute = attr; MakeTime = dt; Information = info; Direction = dir; Source = src; Destination = dst; UserMark = mark; Message = message; Data = data; }
private static Packet[] MapPackets() { List <Packet> packets = new List <Packet>(capacity: byte.MaxValue); int max = 0; foreach (Type type in Assembly.GetAssembly(typeof(Packet)).GetTypes()) { if (!type.IsSubclassOf(typeof(Packet))) { continue; } if (type == typeof(Packet) || type == typeof(ChannelMessage)) { continue; } PacketAttribute attribute = type.GetCustomAttribute <PacketAttribute>(); Packet instance = (Packet)Activator.CreateInstance(type); instance.Id = attribute.PacketId; instance.Policies = attribute.PacketPoliies; if (instance is ChannelMessage message) { object[] flags = type.GetCustomAttributes(typeof(MessageFlagsAttribute), inherit: true); var messageFlags = (MessageFlagsAttribute)flags.FirstOrDefault(x => x.GetType() == typeof(MessageFlagsAttribute)); var requiredFlags = (RequiredFlagsAttribute)flags.FirstOrDefault(x => x.GetType() == typeof(RequiredFlagsAttribute)); var allowedFlags = (AllowedFlagsAttribute)flags.FirstOrDefault(x => x.GetType() == typeof(AllowedFlagsAttribute)); if (messageFlags != null) { message.RequiredFlags = messageFlags.Flags; message.AllowedFlags = messageFlags.Flags; } else { if (requiredFlags != null) { message.RequiredFlags = requiredFlags.Flags; } if (allowedFlags != null) { message.AllowedFlags = allowedFlags.Flags; } } } packets.Add(instance); max = Math.Max(max, attribute.PacketId); } var result = new Packet[max + 1]; foreach (Packet packet in packets) { result[packet.Id] = packet; } return(result); }
private void DrawBufferPushBegin(PacketAttribute type) { /* データタイプが変化した場合は現在溜まっているバッファを出力 */ if (draw_data_type_ != type) { if (TBox_Main.TextLength > 0) { DrawBufferPushEndLine(); } DrawBufferFlush(); } draw_data_type_ = type; }
private void GenerateHandlerReferences(Type type, bool isWorldServer) { IEnumerable <Type> handlerTypes = !isWorldServer?type.Assembly.GetTypes().Where(t => t.Name.Equals("LoginPacketHandler")) //shitty but it works : type.Assembly.GetTypes().Where(p => !p.IsInterface && type.GetInterfaces().FirstOrDefault().IsAssignableFrom(p)); //iterate thru each type in the given assembly, the IPacketHandler is expected in the same dll foreach (Type handlerType in handlerTypes) { object handler = Activator.CreateInstance(handlerType, new object[] { this }); foreach (MethodInfo methodInfo in handlerType.GetMethods().Where(x => x.GetCustomAttributes(false).OfType <PacketAttribute>().Any())) { PacketAttribute Packet = methodInfo.GetCustomAttributes(false).OfType <PacketAttribute>().FirstOrDefault(); if (Packet != null) { HandlerMethods.Add(Packet, new Tuple <Action <object, string>, object>(DelegateBuilder.BuildDelegate <Action <object, string> >(methodInfo), handler)); } } } }
public static short GetPacketId <T>() { PacketAttribute packetAttr = typeof(T).GetCustomAttribute(typeof(PacketAttribute)) as PacketAttribute; return(packetAttr == null ? (short)0 : packetAttr.Id); }
public static short GetPacketId(Type packetType) { PacketAttribute packetAttr = packetType.GetCustomAttribute(typeof(PacketAttribute)) as PacketAttribute; return(packetAttr == null ? (short)0 : packetAttr.Id); }
public byte[] Encode() { PacketAttribute packetAttr = this.GetType().GetCustomAttribute(typeof(PacketAttribute)) as PacketAttribute; var ms = new MemoryStream(); var writer = new BinaryWriter(ms); ushort bitmask = 0; foreach (var p in this.GetType().GetProperties()) { SerializeAttribute serializeAttr = p.GetCustomAttribute(typeof(SerializeAttribute)) as SerializeAttribute; if (serializeAttr == null) { continue; } if (p.PropertyType == typeof(Int32) || p.PropertyType == typeof(Int16) || p.PropertyType == typeof(Byte)) { var _data = (Int32)(dynamic)p.GetValue(this); if (serializeAttr.Dict.Length == 8) { switch (_data) { case -1: case 0: case 1: case 2: bitmask = bitmask.SetRange(serializeAttr.BitIndex, serializeAttr.Bits, (ushort)serializeAttr.Dict.ToList().IndexOf((uint)_data)); break; default: var cryptOperationHashes = serializeAttr.Dict.Where(x => x > 7 && x != unchecked ((uint)-1)).ToList(); var cryptOperation = cryptOperationHashes[random.Next() % cryptOperationHashes.Count]; bitmask = bitmask.SetRange(serializeAttr.BitIndex, serializeAttr.Bits, (ushort)serializeAttr.Dict.ToList().IndexOf(cryptOperation)); Serializer.Encode(p.GetValue(this), p.PropertyType, writer, Operations.GetOperations(cryptOperation), serializeAttr.ReverseByteOrder); break; } } else if (serializeAttr.Dict.Length == 1) { if (_data == 0) { bitmask = bitmask.SetRange(serializeAttr.BitIndex, serializeAttr.Bits, 1); } else { bitmask = bitmask.SetRange(serializeAttr.BitIndex, serializeAttr.Bits, 0); Serializer.Encode(p.GetValue(this), p.PropertyType, writer, Operations.GetOperations(serializeAttr.Dict[0]), serializeAttr.ReverseByteOrder); } } } else if (p.PropertyType == typeof(Vector3)) { var _data = (Vector3)p.GetValue(this); if (_data.X != 0.0f || _data.Y != 0.0f || _data.Z != 0.0f) { bitmask = bitmask.SetRange(serializeAttr.BitIndex, serializeAttr.Bits, 0); Serializer.Encode(_data, p.PropertyType, writer, Operations.GetOperations(serializeAttr.Dict[0]), serializeAttr.ReverseByteOrder); } else { bitmask = bitmask.SetRange(serializeAttr.BitIndex, serializeAttr.Bits, 1); } } else if (p.PropertyType == typeof(Vector2)) { var _data = (Vector2)p.GetValue(this); if (_data.X != 0.0f || _data.Y != 0.0f) { bitmask = bitmask.SetRange(serializeAttr.BitIndex, serializeAttr.Bits, 0); Serializer.Encode(_data, p.PropertyType, writer, Operations.GetOperations(serializeAttr.Dict[0]), serializeAttr.ReverseByteOrder); } else { bitmask = bitmask.SetRange(serializeAttr.BitIndex, serializeAttr.Bits, 1); } } else if (p.PropertyType == typeof(Boolean)) { bitmask = bitmask.SetRange(serializeAttr.BitIndex, serializeAttr.Bits, (ushort)((bool)p.GetValue(this) == true ? 1 : 0)); } } var packet = new byte[ms.Length + 8]; BitConverter.GetBytes(packetAttr.Id).CopyTo(packet, 0); BitConverter.GetBytes(NetworkId).CopyTo(packet, 2); BitConverter.GetBytes(packetAttr.BitmaskType == typeof(Byte) ? (byte)bitmask : bitmask).CopyTo(packet, 6); Array.Copy(ms.GetBuffer(), 0, packet, packetAttr.BitmaskType == typeof(Byte) ? 7 : 8, ms.Length); return(packet); }
/// <summary>Gets a type's packet identifier</summary> public static PacketIdentifier Get(Type type) => PacketAttribute.Get(type).Identifier;