mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-12 02:10:30 +00:00
Merge pull request #1569 from cc-tweaked/feature/no-compression
Remove compression from terminal/monitor packets
This commit is contained in:
commit
500406f9eb
@ -5,18 +5,10 @@
|
|||||||
package dan200.computercraft.shared.computer.terminal;
|
package dan200.computercraft.shared.computer.terminal;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
import io.netty.buffer.ByteBufInputStream;
|
|
||||||
import io.netty.buffer.ByteBufOutputStream;
|
|
||||||
import io.netty.buffer.Unpooled;
|
import io.netty.buffer.Unpooled;
|
||||||
import net.minecraft.network.FriendlyByteBuf;
|
import net.minecraft.network.FriendlyByteBuf;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.io.UncheckedIOException;
|
|
||||||
import java.util.zip.GZIPInputStream;
|
|
||||||
import java.util.zip.GZIPOutputStream;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A snapshot of a terminal's state.
|
* A snapshot of a terminal's state.
|
||||||
@ -31,20 +23,10 @@ public class TerminalState {
|
|||||||
public final int width;
|
public final int width;
|
||||||
public final int height;
|
public final int height;
|
||||||
|
|
||||||
private final boolean compress;
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private final ByteBuf buffer;
|
private final ByteBuf buffer;
|
||||||
|
|
||||||
private @Nullable ByteBuf compressed;
|
|
||||||
|
|
||||||
public TerminalState(@Nullable NetworkedTerminal terminal) {
|
public TerminalState(@Nullable NetworkedTerminal terminal) {
|
||||||
this(terminal, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public TerminalState(@Nullable NetworkedTerminal terminal, boolean compress) {
|
|
||||||
this.compress = compress;
|
|
||||||
|
|
||||||
if (terminal == null) {
|
if (terminal == null) {
|
||||||
colour = false;
|
colour = false;
|
||||||
width = height = 0;
|
width = height = 0;
|
||||||
@ -61,14 +43,13 @@ public class TerminalState {
|
|||||||
|
|
||||||
public TerminalState(FriendlyByteBuf buf) {
|
public TerminalState(FriendlyByteBuf buf) {
|
||||||
colour = buf.readBoolean();
|
colour = buf.readBoolean();
|
||||||
compress = buf.readBoolean();
|
|
||||||
|
|
||||||
if (buf.readBoolean()) {
|
if (buf.readBoolean()) {
|
||||||
width = buf.readVarInt();
|
width = buf.readVarInt();
|
||||||
height = buf.readVarInt();
|
height = buf.readVarInt();
|
||||||
|
|
||||||
var length = buf.readVarInt();
|
var length = buf.readVarInt();
|
||||||
buffer = readCompressed(buf, length, compress);
|
buffer = buf.readBytes(length);
|
||||||
} else {
|
} else {
|
||||||
width = height = 0;
|
width = height = 0;
|
||||||
buffer = null;
|
buffer = null;
|
||||||
@ -77,16 +58,13 @@ public class TerminalState {
|
|||||||
|
|
||||||
public void write(FriendlyByteBuf buf) {
|
public void write(FriendlyByteBuf buf) {
|
||||||
buf.writeBoolean(colour);
|
buf.writeBoolean(colour);
|
||||||
buf.writeBoolean(compress);
|
|
||||||
|
|
||||||
buf.writeBoolean(buffer != null);
|
buf.writeBoolean(buffer != null);
|
||||||
if (buffer != null) {
|
if (buffer != null) {
|
||||||
buf.writeVarInt(width);
|
buf.writeVarInt(width);
|
||||||
buf.writeVarInt(height);
|
buf.writeVarInt(height);
|
||||||
|
buf.writeVarInt(buffer.readableBytes());
|
||||||
var sendBuffer = getCompressed();
|
buf.writeBytes(buffer, buffer.readerIndex(), buffer.readableBytes());
|
||||||
buf.writeVarInt(sendBuffer.readableBytes());
|
|
||||||
buf.writeBytes(sendBuffer, sendBuffer.readerIndex(), sendBuffer.readableBytes());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,40 +88,4 @@ public class TerminalState {
|
|||||||
terminal.read(new FriendlyByteBuf(buffer));
|
terminal.read(new FriendlyByteBuf(buffer));
|
||||||
return terminal;
|
return terminal;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ByteBuf getCompressed() {
|
|
||||||
if (buffer == null) throw new NullPointerException("buffer");
|
|
||||||
if (!compress) return buffer;
|
|
||||||
if (compressed != null) return compressed;
|
|
||||||
|
|
||||||
var compressed = Unpooled.buffer();
|
|
||||||
try (OutputStream stream = new GZIPOutputStream(new ByteBufOutputStream(compressed))) {
|
|
||||||
stream.write(buffer.array(), buffer.arrayOffset(), buffer.readableBytes());
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new UncheckedIOException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.compressed = compressed;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static ByteBuf readCompressed(ByteBuf buf, int length, boolean compress) {
|
|
||||||
if (compress) {
|
|
||||||
var buffer = Unpooled.buffer();
|
|
||||||
try (InputStream stream = new GZIPInputStream(new ByteBufInputStream(buf, length))) {
|
|
||||||
var swap = new byte[8192];
|
|
||||||
while (true) {
|
|
||||||
var bytes = stream.read(swap);
|
|
||||||
if (bytes == -1) break;
|
|
||||||
buffer.writeBytes(swap, 0, bytes);
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new UncheckedIOException(e);
|
|
||||||
}
|
|
||||||
return buffer;
|
|
||||||
} else {
|
|
||||||
var buffer = Unpooled.buffer(length);
|
|
||||||
buf.readBytes(buffer, length);
|
|
||||||
return buffer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -18,22 +18,11 @@ import static org.junit.jupiter.api.Assertions.*;
|
|||||||
*/
|
*/
|
||||||
public class TerminalStateTest {
|
public class TerminalStateTest {
|
||||||
@RepeatedTest(5)
|
@RepeatedTest(5)
|
||||||
public void testCompressed() {
|
public void testRoundTrip() {
|
||||||
var terminal = randomTerminal();
|
var terminal = randomTerminal();
|
||||||
|
|
||||||
var buffer = new FriendlyByteBuf(Unpooled.directBuffer());
|
var buffer = new FriendlyByteBuf(Unpooled.directBuffer());
|
||||||
new TerminalState(terminal, true).write(buffer);
|
new TerminalState(terminal).write(buffer);
|
||||||
|
|
||||||
checkEqual(terminal, read(buffer));
|
|
||||||
assertEquals(0, buffer.readableBytes());
|
|
||||||
}
|
|
||||||
|
|
||||||
@RepeatedTest(5)
|
|
||||||
public void testUncompressed() {
|
|
||||||
var terminal = randomTerminal();
|
|
||||||
|
|
||||||
var buffer = new FriendlyByteBuf(Unpooled.directBuffer());
|
|
||||||
new TerminalState(terminal, false).write(buffer);
|
|
||||||
|
|
||||||
checkEqual(terminal, read(buffer));
|
checkEqual(terminal, read(buffer));
|
||||||
assertEquals(0, buffer.readableBytes());
|
assertEquals(0, buffer.readableBytes());
|
||||||
|
Loading…
Reference in New Issue
Block a user