3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-03 14:50:30 +01:00

Fix component read length

Fixes #1822, fixes #1696
Dieser Commit ist enthalten in:
KennyTV 2020-06-23 19:15:20 +02:00
Ursprung 9220c5ae40
Commit 0b1c210f77
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 6BE3B555EBC5982B
6 geänderte Dateien mit 28 neuen und 6 gelöschten Zeilen

Datei anzeigen

@ -77,6 +77,7 @@ public abstract class Type<T> implements ByteBufReader<T>, ByteBufWriter<T> {
@Deprecated
public static final Type<Integer[]> UNSIGNED_SHORT_ARRAY = new ArrayType<>(Type.UNSIGNED_SHORT);
/* Other Types */
public static final Type<String> COMPONENT_STRING = new ComponentStringType();
public static final Type<String> STRING = new StringType();
public static final Type<String[]> STRING_ARRAY = new ArrayType<>(Type.STRING);

Datei anzeigen

@ -0,0 +1,15 @@
package us.myles.ViaVersion.api.type.types;
import us.myles.ViaVersion.api.type.Type;
public class ComponentStringType extends StringType {
public ComponentStringType() {
super(262144);
}
@Override
public Class<? extends Type> getBaseClass() {
return StringType.class;
}
}

Datei anzeigen

@ -10,22 +10,28 @@ public class StringType extends Type<String> {
// String#length() (used to limit the string in Minecraft source code) uses char[]#length
private static final int maxJavaCharUtf8Length = Character.toString(Character.MAX_VALUE)
.getBytes(StandardCharsets.UTF_8).length;
private final int maxLength;
public StringType() {
this(Short.MAX_VALUE);
}
public StringType(int maxLength) {
super(String.class);
this.maxLength = maxLength;
}
@Override
public String read(ByteBuf buffer) throws Exception {
int len = Type.VAR_INT.readPrimitive(buffer);
Preconditions.checkArgument(len <= Short.MAX_VALUE * maxJavaCharUtf8Length,
Preconditions.checkArgument(len <= maxLength * maxJavaCharUtf8Length,
"Cannot receive string longer than Short.MAX_VALUE * " + maxJavaCharUtf8Length + " bytes (got %s bytes)", len);
String string = buffer.toString(buffer.readerIndex(), len, StandardCharsets.UTF_8);
buffer.skipBytes(len);
Preconditions.checkArgument(string.length() <= Short.MAX_VALUE,
Preconditions.checkArgument(string.length() <= maxLength,
"Cannot receive string longer than Short.MAX_VALUE characters (got %s bytes)", string.length());
return string;
@ -33,7 +39,7 @@ public class StringType extends Type<String> {
@Override
public void write(ByteBuf buffer, String object) throws Exception {
Preconditions.checkArgument(object.length() <= Short.MAX_VALUE, "Cannot send string longer than Short.MAX_VALUE (got %s characters)", object.length());
Preconditions.checkArgument(object.length() <= maxLength, "Cannot send string longer than Short.MAX_VALUE (got %s characters)", object.length());
byte[] b = object.getBytes(StandardCharsets.UTF_8);
Type.VAR_INT.writePrimitive(buffer, b.length);

Datei anzeigen

@ -101,7 +101,7 @@ public class InventoryPackets {
}
wrapper.write(Type.VAR_INT, typeId);
wrapper.write(Type.STRING, title);
wrapper.write(Type.COMPONENT_STRING, title);
}
}
});

Datei anzeigen

@ -56,7 +56,7 @@ public class Protocol1_16To1_15_2 extends Protocol<ClientboundPackets1_15, Clien
registerOutgoing(ClientboundPackets1_15.CHAT_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.STRING);
map(Type.COMPONENT_STRING);
map(Type.BYTE);
handler(wrapper -> wrapper.write(Type.UUID, ZERO_UUID)); // sender uuid
}

Datei anzeigen

@ -27,7 +27,7 @@ public class InventoryPackets {
public void registerMap() {
map(Type.VAR_INT);
map(Type.VAR_INT);
map(Type.STRING);
map(Type.COMPONENT_STRING);
handler(wrapper -> {
int windowType = wrapper.get(Type.VAR_INT, 1);