protected override ThreeOneSevenDecoderMessages.Types.WalkingQueueMessage Decode(Player player, MessageFrame frame) { var reader = new MessageFrameReader(frame); var length = frame.Payload.ReadableBytes; if (frame.Id == 248) { length -= 14; // strip off anti-cheat data } int steps = (length - 5) / 2; int[,] path = new int[steps, 2]; int x = (int)reader.GetUnsigned(MessageType.Short, DataOrder.Little, DataTransformation.Add); for (int i = 0; i < steps; i++) { path[i, 0] = (int)reader.GetSigned(MessageType.Byte); path[i, 1] = (int)reader.GetSigned(MessageType.Byte); } int y = (int)reader.GetUnsigned(MessageType.Short, DataOrder.Little); var run = reader.GetUnsigned(MessageType.Byte, DataTransformation.Negate) == 1; var positions = new Position[steps + 1]; positions[0] = new Position(x, y); for (int i = 0; i < steps; i++) { positions[i + 1] = new Position(path[i, 0] + x, path[i, 1] + y); } ThreeOneSevenDecoderMessages.Types.WalkingQueueMessage walkingQueueMessage = new() { Run = run, }; walkingQueueMessage.X.Add(positions.Select(t => t.X)); walkingQueueMessage.Y.Add(positions.Select(t => t.Y)); return(walkingQueueMessage); }
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output) { int?isaacValue = CipherPair?.DecodingRandom?.NextInt() ?? null; int opcode = input.ReadByte(); int unencodedOpcode = isaacValue.HasValue ? opcode - isaacValue.Value & 0xFF : opcode & 0XFF; var protoCodec = _protoMessageCodecHandler.DecoderCodecs.ContainsKey(unencodedOpcode) ? _protoMessageCodecHandler.DecoderCodecs[unencodedOpcode] : null; var decoder = _messageDecoders .Where(t => t.Ids != null) .FirstOrDefault(decoder => decoder.Ids.Contains(unencodedOpcode)); var frameType = protoCodec?.MessageCodec.SizeType.GetFrameType() ?? decoder?.FrameType; var size = 0; if (!frameType.HasValue) { Log.Logger.Warning("Opcode {0} not recognised, sent by player {1}", unencodedOpcode, Player.Username); _ = context.CloseAsync(); return; } if (frameType != FrameType.Fixed) { size = frameType.Value.GetBytes(input); } else { size = protoCodec.FieldCodec.Sum(t => t.FieldCodec.Type.GetSize()); } var buffer = input.ReadBytes(size); var messageFrame = new MessageFrame(unencodedOpcode, frameType.Value, buffer); if (protoCodec?.MessageCodec?.Custom ?? true) { Log.Logger.Debug("Decoding Opcode: {0} Player Name: {1} from {2} Size {3}", unencodedOpcode, Player.Username, context.Channel.RemoteAddress, size); decoder?.DecodeAndPublish(Player, messageFrame); } else { var message = protoCodec.CreationMethod.Invoke(); var messageReader = new MessageFrameReader(messageFrame); var protoDecoder = _messageDecoders.First(t => t.TypeName == message.Descriptor.ClrType.Name); foreach (var field in protoCodec.FieldCodec) { var fieldType = field.FieldDescriptor.FieldType; var isString = field.FieldCodec.Type == Models.FieldType.String; MessageType?messageType = isString ? null : field.FieldCodec.Type.GetMessageType(); var order = field.FieldCodec.Order.GetDataOrder(); var transform = field.FieldCodec.Transform.GetDataTransformation(); object rawValue = isString ? messageReader.ReadString() : messageReader.GetUnsigned(messageType.Value, order, transform); object value = fieldType == Google.Protobuf.Reflection.FieldType.Bool ? ((ulong)rawValue == 1) : isString ? (string)rawValue : field.ToObject((ulong)rawValue); try { field.FieldDescriptor.Accessor.SetValue(message, value); } catch (Exception e) { Log.Logger.Error("Error decoding field {0}, Error: {1}", field.FieldCodec, e); } } Log.Logger.Debug("Message Received: {0} TypeName: {1} Player: {2} Size: {3} Opcode: {4}", message, protoDecoder.TypeName, Player.Username, size, unencodedOpcode); protoDecoder.Publish(Player, message); } }