Search in sources :

Example 26 with ByteBuffer

use of org.lanternpowered.server.network.buffer.ByteBuffer in project LanternServer by LanternPowered.

the class CodecPlayOutSelectAdvancementTree method encode.

@Override
public ByteBuffer encode(CodecContext context, MessagePlayOutSelectAdvancementTree message) throws CodecException {
    final ByteBuffer buf = context.byteBufAlloc().buffer();
    final String id = message.getId();
    buf.writeBoolean(id != null);
    if (id != null) {
        buf.writeString(id);
    }
    return buf;
}
Also used : ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer)

Example 27 with ByteBuffer

use of org.lanternpowered.server.network.buffer.ByteBuffer in project LanternServer by LanternPowered.

the class CodecPlayOutAdvancements method encode.

@Override
public ByteBuffer encode(CodecContext context, MessagePlayOutAdvancements message) throws CodecException {
    final ByteBuffer buf = context.byteBufAlloc().buffer();
    buf.writeBoolean(message.getClear());
    final List<MessagePlayOutAdvancements.AdvStruct> addedAdvStructs = message.getAddedAdvStructs();
    buf.writeVarInt(addedAdvStructs.size());
    for (MessagePlayOutAdvancements.AdvStruct struct : addedAdvStructs) {
        buf.writeString(struct.getId());
        final Optional<String> optParent = struct.getParentId();
        buf.writeBoolean(optParent.isPresent());
        optParent.ifPresent(buf::writeString);
        final Optional<MessagePlayOutAdvancements.AdvStruct.Display> optDisplay = struct.getDisplay();
        buf.writeBoolean(optDisplay.isPresent());
        if (optDisplay.isPresent()) {
            final MessagePlayOutAdvancements.AdvStruct.Display display = optDisplay.get();
            buf.write(Types.LOCALIZED_TEXT, display.getTitle());
            buf.write(Types.LOCALIZED_TEXT, display.getDescription());
            buf.write(Types.ITEM_STACK, display.getIcon().createStack());
            buf.writeVarInt(((LanternAdvancementType) display.getType()).getInternalId());
            final Optional<String> optBackground = display.getBackground();
            int flags = 0;
            if (optBackground.isPresent()) {
                flags |= 0x1;
            }
            if (display.doesShowToast()) {
                flags |= 0x2;
            }
            if (display.isHidden()) {
                flags |= 0x4;
            }
            buf.writeInteger(flags);
            optBackground.ifPresent(buf::writeString);
            buf.writeFloat((float) display.getX());
            buf.writeFloat((float) display.getY());
        }
        final Collection<String> criteria = struct.getCriteria();
        buf.writeVarInt(criteria.size());
        criteria.forEach(buf::writeString);
        final String[][] requirements = struct.getRequirements();
        buf.writeVarInt(requirements.length);
        for (String[] requirements1 : requirements) {
            buf.writeVarInt(requirements1.length);
            for (String requirement : requirements1) {
                buf.writeString(requirement);
            }
        }
    }
    final List<String> removed = message.getRemovedAdvs();
    buf.writeVarInt(removed.size());
    removed.forEach(buf::writeString);
    final Map<String, Object2LongMap<String>> progress = message.getProgress();
    buf.writeVarInt(progress.size());
    for (Map.Entry<String, Object2LongMap<String>> entry : progress.entrySet()) {
        buf.writeString(entry.getKey());
        buf.writeVarInt(entry.getValue().size());
        for (Object2LongMap.Entry<String> entry1 : entry.getValue().object2LongEntrySet()) {
            buf.writeString(entry1.getKey());
            final long time = entry1.getLongValue();
            buf.writeBoolean(time != -1L);
            if (time != -1L) {
                buf.writeLong(time);
            }
        }
    }
    return buf;
}
Also used : Object2LongMap(it.unimi.dsi.fastutil.objects.Object2LongMap) MessagePlayOutAdvancements(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutAdvancements) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) Object2LongMap(it.unimi.dsi.fastutil.objects.Object2LongMap) Map(java.util.Map)

Example 28 with ByteBuffer

use of org.lanternpowered.server.network.buffer.ByteBuffer in project LanternServer by LanternPowered.

the class MessageCodecHandler method encode.

@Override
protected void encode(ChannelHandlerContext ctx, Message message, List<Object> output) throws Exception {
    final Protocol protocol = this.codecContext.getSession().getProtocol();
    final MessageRegistration<Message> registration = (MessageRegistration<Message>) protocol.outbound().findByMessageType(message.getClass()).orElse(null);
    if (registration == null) {
        throw new EncoderException("Message type (" + message.getClass().getName() + ") is not registered!");
    }
    CodecRegistration codecRegistration = registration.getCodecRegistration().orElse(null);
    if (codecRegistration == null) {
        throw new EncoderException("Message type (" + message.getClass().getName() + ") is not registered to allow encoding!");
    }
    /*
        if (message instanceof MessagePlayOutWorldTime ||
                message instanceof MessageInOutKeepAlive) {
        } else {
            System.out.println(message.getClass().getName());
        }
        */
    final ByteBuf opcode = ctx.alloc().buffer();
    // Write the opcode of the message
    writeVarInt(opcode, codecRegistration.getOpcode());
    final Codec codec = codecRegistration.getCodec();
    final ByteBuffer content;
    try {
        content = codec.encode(this.codecContext, message);
    } finally {
        ReferenceCountUtil.release(message);
    }
    // Add the buffer to the output
    output.add(Unpooled.wrappedBuffer(opcode, ((LanternByteBuffer) content).getDelegate()));
}
Also used : EncoderException(io.netty.handler.codec.EncoderException) Codec(org.lanternpowered.server.network.message.codec.Codec) MessageToMessageCodec(io.netty.handler.codec.MessageToMessageCodec) MessageRegistration(org.lanternpowered.server.network.message.MessageRegistration) HandlerMessage(org.lanternpowered.server.network.message.HandlerMessage) NullMessage(org.lanternpowered.server.network.message.NullMessage) Message(org.lanternpowered.server.network.message.Message) BulkMessage(org.lanternpowered.server.network.message.BulkMessage) CodecRegistration(org.lanternpowered.server.network.message.CodecRegistration) Protocol(org.lanternpowered.server.network.protocol.Protocol) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) LanternByteBuffer(org.lanternpowered.server.network.buffer.LanternByteBuffer) LanternByteBuffer(org.lanternpowered.server.network.buffer.LanternByteBuffer)

Example 29 with ByteBuffer

use of org.lanternpowered.server.network.buffer.ByteBuffer in project LanternServer by LanternPowered.

the class MessageCodecHandler method decode.

@Override
public void decode(ChannelHandlerContext ctx, ByteBuf input, List<Object> output) throws Exception {
    if (input.readableBytes() == 0) {
        return;
    }
    final int opcode = readVarInt(input);
    final ProtocolState state = this.codecContext.getSession().getProtocolState();
    final Protocol protocol = state.getProtocol();
    final CodecRegistration registration = protocol.inbound().find(opcode).orElse(null);
    if (registration == null) {
        if (warnedMissingOpcodes.add(opcode)) {
            Lantern.getLogger().warn("Failed to find a message registration with opcode 0x{} in state {}!", Integer.toHexString(opcode), state);
        }
        return;
    }
    // Copy the remaining content of the buffer to a new buffer used by the
    // message decoding
    final ByteBuffer content = this.codecContext.byteBufAlloc().buffer(input.readableBytes());
    input.readBytes(((LanternByteBuffer) content).getDelegate(), input.readableBytes());
    // Read the content of the message
    final Message message;
    try {
        message = registration.getCodec().decode(this.codecContext, content);
        if (content.available() > 0) {
            Lantern.getLogger().warn("Trailing bytes {}b after decoding with message codec {} with opcode 0x{} in state {}!\n{}", content.available(), registration.getCodec().getClass().getName(), Integer.toHexString(opcode), state, message);
        }
    } finally {
        content.release();
    }
    /*
        if (message instanceof MessageInOutKeepAlive ||
                message instanceof MessagePlayInPlayerMovement) {
        } else {
            System.out.println(message.getClass().getName());
        }
        */
    processMessage(message, output, protocol, state, this.codecContext);
}
Also used : HandlerMessage(org.lanternpowered.server.network.message.HandlerMessage) NullMessage(org.lanternpowered.server.network.message.NullMessage) Message(org.lanternpowered.server.network.message.Message) BulkMessage(org.lanternpowered.server.network.message.BulkMessage) CodecRegistration(org.lanternpowered.server.network.message.CodecRegistration) ProtocolState(org.lanternpowered.server.network.protocol.ProtocolState) Protocol(org.lanternpowered.server.network.protocol.Protocol) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) LanternByteBuffer(org.lanternpowered.server.network.buffer.LanternByteBuffer)

Example 30 with ByteBuffer

use of org.lanternpowered.server.network.buffer.ByteBuffer in project LanternServer by LanternPowered.

the class ProcessorForgeHandshakeOutRegistryData method process.

@Override
public void process(CodecContext context, MessageForgeHandshakeOutRegistryData message, List<Message> output) throws CodecException {
    final Iterator<Entry> it = message.getEntries().iterator();
    if (!it.hasNext()) {
        throw new CodecException("There must be at least one entry present!");
    }
    while (it.hasNext()) {
        final Entry entry = it.next();
        final ByteBuffer buf = context.byteBufAlloc().buffer();
        buf.writeByte((byte) CodecPlayInOutCustomPayload.FML_HANDSHAKE_REGISTRY_DATA);
        buf.writeBoolean(it.hasNext());
        buf.writeString(entry.getName());
        final Map<String, Integer> ids = entry.getIds();
        buf.writeVarInt(ids.size());
        for (Map.Entry<String, Integer> en : ids.entrySet()) {
            buf.writeString(en.getKey());
            buf.writeVarInt(en.getValue());
        }
        final List<String> substitutions = entry.getSubstitutions();
        buf.writeVarInt(substitutions.size());
        substitutions.forEach(buf::writeString);
        output.add(new MessagePlayInOutChannelPayload("FML|HS", buf));
    }
}
Also used : Entry(org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeOutRegistryData.Entry) MessagePlayInOutChannelPayload(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload) CodecException(io.netty.handler.codec.CodecException) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) Map(java.util.Map)

Aggregations

ByteBuffer (org.lanternpowered.server.network.buffer.ByteBuffer)75 EncoderException (io.netty.handler.codec.EncoderException)9 Vector3d (com.flowpowered.math.vector.Vector3d)7 Message (org.lanternpowered.server.network.message.Message)5 Map (java.util.Map)4 MessagePlayInOutChannelPayload (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload)4 AbstractParameterList (org.lanternpowered.server.network.entity.parameter.AbstractParameterList)3 NullMessage (org.lanternpowered.server.network.message.NullMessage)3 ItemStack (org.spongepowered.api.item.inventory.ItemStack)3 Vector3i (com.flowpowered.math.vector.Vector3i)2 CodecException (io.netty.handler.codec.CodecException)2 LanternPotionEffectType (org.lanternpowered.server.effect.potion.LanternPotionEffectType)2 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)2 NetworkSession (org.lanternpowered.server.network.NetworkSession)2 LanternByteBuffer (org.lanternpowered.server.network.buffer.LanternByteBuffer)2 BulkMessage (org.lanternpowered.server.network.message.BulkMessage)2 CodecRegistration (org.lanternpowered.server.network.message.CodecRegistration)2 HandlerMessage (org.lanternpowered.server.network.message.HandlerMessage)2 Codec (org.lanternpowered.server.network.message.codec.Codec)2 RawItemStack (org.lanternpowered.server.network.objects.RawItemStack)2