From 8b09c78f02793578f5a260e50240b7626c970537 Mon Sep 17 00:00:00 2001 From: Kodi Craft Date: Sun, 6 Oct 2024 15:49:55 +0200 Subject: [PATCH] Actually implement compression --- apps/amethyst/lib/apps/connection_handler.ex | 8 +++++--- apps/amethyst/lib/apps/connection_receiver.ex | 2 +- config/runtime.exs | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/apps/amethyst/lib/apps/connection_handler.ex b/apps/amethyst/lib/apps/connection_handler.ex index 60968d8..569e7ce 100644 --- a/apps/amethyst/lib/apps/connection_handler.ex +++ b/apps/amethyst/lib/apps/connection_handler.ex @@ -66,7 +66,7 @@ defmodule Amethyst.ConnectionHandler do loop(socket, connstate, version, state) {:set_compression, threshold} -> Logger.debug("Enabling comrpession with threshold #{threshold}") - state = Map.put(state, :compression_threshold, threshold) + state = Map.put(state, :compression, threshold) loop(socket, connstate, version, state) {:set_position, position} -> prev_position = Map.get(state, :position) @@ -118,7 +118,7 @@ defmodule Amethyst.ConnectionHandler do send(from, Map.get(state, :decryption_state)) loop(socket, connstate, version, state) {:get_compression, from} -> - send(from, Map.get(state, :compression_threshold)) + send(from, Map.get(state, :compression)) loop(socket, connstate, version, state) {:packet, id, data} -> state = handle_packet(id, data, connstate, version, state) @@ -258,6 +258,7 @@ defmodule Amethyst.ConnectionHandler do defp send_packet(socket, connstate, packet, version, state) do try do data = connstate.serialize(packet, version) + Logger.debug("#{inspect(Map.get(state, :compression))}") container = if Map.get(state, :compression) == nil do # Packet ID is included in data Write.varint(byte_size(data)) <> data @@ -265,13 +266,14 @@ defmodule Amethyst.ConnectionHandler do threshold = Map.get(state, :compression, 0) data_length = byte_size(data) if data_length >= threshold do - compressed = Write.varint(data_length) <> :zlib.zip(data) + compressed = Write.varint(data_length) <> :zlib.compress(data) Write.varint(byte_size(compressed)) <> compressed else compressed = Write.varint(0) <> data Write.varint(byte_size(compressed)) <> compressed end end + Logger.debug(inspect(container)) encrypted = if Map.get(state, :encryption_state) == nil do container else diff --git a/apps/amethyst/lib/apps/connection_receiver.ex b/apps/amethyst/lib/apps/connection_receiver.ex index 08c591a..3b8e033 100644 --- a/apps/amethyst/lib/apps/connection_receiver.ex +++ b/apps/amethyst/lib/apps/connection_receiver.ex @@ -104,7 +104,7 @@ defmodule Amethyst.ConnectionReceiver do rest else # Compressed data - rest |> :zlib.unzip() + rest |> :zlib.uncompress() end end diff --git a/config/runtime.exs b/config/runtime.exs index acd79ea..a3c94b6 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -4,5 +4,5 @@ config :amethyst, port: 25599, # Bogus port for testing, avoids unexpected conflicts encryption: true, # Whether or not to request encryption from clients. auth: false, # Whether or not users should be authenticated with Mojang. - compression: nil, # Packets larger than this amount are sent compressed. Set to nil to disable compression. + compression: 256, # Packets larger than this amount are sent compressed. Set to nil to disable compression. default_game: Example.Game # Which game new players should be sent to