no need to deprecate serialize methods of PacketContainer
This commit is contained in:
parent
9d912b7337
commit
e391e3637e
|
@ -17,7 +17,6 @@
|
|||
|
||||
package com.comphenix.protocol.events;
|
||||
|
||||
import com.comphenix.protocol.ProtocolLogger;
|
||||
import com.comphenix.protocol.reflect.accessors.Accessors;
|
||||
import com.comphenix.protocol.reflect.accessors.ConstructorAccessor;
|
||||
import com.comphenix.protocol.reflect.accessors.MethodAccessor;
|
||||
|
@ -27,7 +26,6 @@ import java.util.Map;
|
|||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
import java.util.logging.Level;
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
|
@ -62,7 +60,7 @@ import io.netty.buffer.ByteBuf;
|
|||
* @author Kristian
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class PacketContainer extends AbstractStructure implements Serializable { // TODO: remove Serializable
|
||||
public class PacketContainer extends AbstractStructure implements Serializable {
|
||||
private static final long serialVersionUID = 3;
|
||||
|
||||
private PacketType type;
|
||||
|
@ -269,7 +267,6 @@ public class PacketContainer extends AbstractStructure implements Serializable {
|
|||
};
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
private void writeObject(ObjectOutputStream output) throws IOException {
|
||||
// Default serialization
|
||||
output.defaultWriteObject();
|
||||
|
@ -285,7 +282,6 @@ public class PacketContainer extends AbstractStructure implements Serializable {
|
|||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
private void readObject(ObjectInputStream input) throws ClassNotFoundException, IOException {
|
||||
// Default deserialization
|
||||
input.defaultReadObject();
|
||||
|
@ -296,20 +292,24 @@ public class PacketContainer extends AbstractStructure implements Serializable {
|
|||
int dataLength = input.readInt();
|
||||
|
||||
ByteBuf byteBuf = (ByteBuf) MinecraftReflection.createPacketDataSerializer(dataLength);
|
||||
byteBuf.writeBytes(input, dataLength);
|
||||
while (true) {
|
||||
// ObjectInputStream only reads a specific amount of bytes before moving the cursor forwards and
|
||||
// allows reading the next byte chunk. So we need to read until the data is gone from the stream and
|
||||
// fully transferred into the buffer.
|
||||
int transferredBytes = byteBuf.writeBytes(input, dataLength);
|
||||
|
||||
// check if we reached the end of the stream, or if the stream has no more data available
|
||||
dataLength -= transferredBytes;
|
||||
if (dataLength <= 0 || transferredBytes <= 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Object packet = deserializeFromBuffer(this.type, byteBuf);
|
||||
|
||||
this.handle = packet;
|
||||
this.structureModifier = this.structureModifier.withTarget(packet);
|
||||
}
|
||||
|
||||
ProtocolLogger.log(
|
||||
Level.SEVERE,
|
||||
"Using ObjectInputStream to deserialize a PacketContainer is deprecated and will be removed in a "
|
||||
+ "future version as it can lead to unexpected results! Please DO NOT report this to ProtocolLib "
|
||||
+ "this is not a bug! Report it to the plugin you see in the following stack trace:",
|
||||
new Throwable());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -36,6 +36,7 @@ import com.comphenix.protocol.reflect.FuzzyReflection;
|
|||
import com.comphenix.protocol.reflect.StructureModifier;
|
||||
import com.comphenix.protocol.reflect.accessors.Accessors;
|
||||
import com.comphenix.protocol.reflect.accessors.FieldAccessor;
|
||||
import com.comphenix.protocol.reflect.cloning.SerializableCloner;
|
||||
import com.comphenix.protocol.utility.MinecraftReflection;
|
||||
import com.comphenix.protocol.wrappers.BlockPosition;
|
||||
import com.comphenix.protocol.wrappers.BukkitConverters;
|
||||
|
@ -61,6 +62,7 @@ import com.comphenix.protocol.wrappers.WrappedWatchableObject;
|
|||
import com.comphenix.protocol.wrappers.nbt.NbtCompound;
|
||||
import com.comphenix.protocol.wrappers.nbt.NbtFactory;
|
||||
import com.google.common.collect.Lists;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
@ -71,6 +73,7 @@ import java.util.Objects;
|
|||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
import net.md_5.bungee.api.chat.ClickEvent;
|
||||
import net.md_5.bungee.api.chat.ComponentBuilder;
|
||||
|
@ -381,7 +384,6 @@ public class PacketContainerTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
@Deprecated // TODO: remove when PacketContainer is no longer Serializable
|
||||
public void testSerialization() {
|
||||
PacketContainer useItem = new PacketContainer(PacketType.Play.Client.USE_ITEM);
|
||||
useItem.getMovingBlockPositions().write(0, new MovingObjectPositionBlock(
|
||||
|
@ -405,6 +407,32 @@ public class PacketContainerTest {
|
|||
assertFalse(pos.isInsideBlock());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigPacketSerialization() {
|
||||
PacketContainer payload = new PacketContainer(PacketType.Play.Server.CUSTOM_PAYLOAD);
|
||||
payload.getMinecraftKeys().write(0, new com.comphenix.protocol.wrappers.MinecraftKey("test"));
|
||||
|
||||
byte[] randomData = new byte[8192];
|
||||
ThreadLocalRandom.current().nextBytes(randomData);
|
||||
|
||||
ByteBuf serializer = (ByteBuf) MinecraftReflection.createPacketDataSerializer(randomData.length);
|
||||
serializer.writeBytes(randomData);
|
||||
|
||||
payload.getModifier().withType(MinecraftReflection.getPacketDataSerializerClass()).write(0, serializer);
|
||||
|
||||
PacketContainer cloned = SerializableCloner.clone(payload);
|
||||
com.comphenix.protocol.wrappers.MinecraftKey clonedKey = cloned.getMinecraftKeys().read(0);
|
||||
|
||||
byte[] clonedData = new byte[randomData.length];
|
||||
ByteBuf clonedBuffer = (ByteBuf) cloned.getModifier()
|
||||
.withType(MinecraftReflection.getPacketDataSerializerClass())
|
||||
.read(0);
|
||||
clonedBuffer.readBytes(clonedData);
|
||||
|
||||
assertEquals("minecraft:test", clonedKey.getFullKey());
|
||||
assertArrayEquals(randomData, clonedData);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntList() {
|
||||
PacketContainer destroy = new PacketContainer(PacketType.Play.Server.ENTITY_DESTROY);
|
||||
|
|
Loading…
Reference in New Issue