Skip to content

Commit 122f433

Browse files
authored
Rewrite proxy (#511)
1 parent 4c9dfbf commit 122f433

File tree

17 files changed

+621
-501
lines changed

17 files changed

+621
-501
lines changed

Cargo.lock

Lines changed: 175 additions & 176 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ members = [
4444
"feather/server",
4545

4646
# Other
47-
"tools/proxy",
47+
"proxy",
4848
]
4949

5050
# No longer need to use release for "development":

feather/base/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,3 @@ bytemuck = { version = "1", features = ["derive"] }
4040
rand = "0.8"
4141
rand_pcg = "0.3"
4242
serde_test = "1"
43-
44-
[features]
45-
proxy = []

feather/base/src/chunk/blocks.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ impl BlockStore {
5151
&self.blocks
5252
}
5353

54-
#[cfg(feature = "proxy")]
5554
pub fn data_mut(&mut self) -> &mut PackedArray {
5655
&mut self.blocks
5756
}
@@ -60,7 +59,6 @@ impl BlockStore {
6059
self.palette.as_ref()
6160
}
6261

63-
#[cfg(feature = "proxy")]
6462
pub fn palette_mut(&mut self) -> Option<&mut Palette> {
6563
self.palette.as_mut()
6664
}
@@ -83,7 +81,6 @@ impl BlockStore {
8381
self.air_block_count
8482
}
8583

86-
#[cfg(feature = "proxy")]
8784
pub fn set_air_blocks(&mut self, new_value: u32) {
8885
self.air_block_count = new_value;
8986
}

feather/base/src/chunk/heightmap.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ where
142142
self.heights.set(index, height as u64);
143143
}
144144

145-
#[cfg(feature = "proxy")]
146145
pub fn set_height_index(&mut self, index: usize, height: i64) {
147146
self.heights.as_u64_mut_vec()[index] = height as u64;
148147
}

feather/base/src/chunk/packed_array.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ impl PackedArray {
166166
self.bits_per_value
167167
}
168168

169-
#[cfg(feature = "proxy")]
170169
pub fn set_bits_per_value(&mut self, new_value: usize) {
171170
self.bits_per_value = new_value;
172171
}
@@ -176,7 +175,6 @@ impl PackedArray {
176175
&self.bits
177176
}
178177

179-
#[cfg(feature = "proxy")]
180178
pub fn as_u64_mut_vec(&mut self) -> &mut Vec<u64> {
181179
&mut self.bits
182180
}

feather/protocol/Cargo.toml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,3 @@ thiserror = "1"
2424
uuid = "0.8"
2525
libcraft-core = { path = "../../libcraft/core" }
2626
libcraft-items = { path = "../../libcraft/items" }
27-
28-
29-
30-
[features]
31-
proxy = ["base/proxy"]

feather/protocol/src/io.rs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use libcraft_items::InventorySlot::*;
1111
use num_traits::{FromPrimitive, ToPrimitive};
1212
use quill_common::components::PreviousGamemode;
1313
use serde::{de::DeserializeOwned, Serialize};
14+
use std::io::ErrorKind;
1415
use std::{
1516
borrow::Cow,
1617
collections::BTreeMap,
@@ -160,31 +161,11 @@ where
160161
pub struct VarInt(pub i32);
161162

162163
impl Readable for VarInt {
163-
fn read(buffer: &mut Cursor<&[u8]>, version: ProtocolVersion) -> anyhow::Result<Self>
164+
fn read(buffer: &mut Cursor<&[u8]>, _version: ProtocolVersion) -> anyhow::Result<Self>
164165
where
165166
Self: Sized,
166167
{
167-
let mut num_read = 0;
168-
let mut result = 0;
169-
170-
loop {
171-
let read = u8::read(buffer, version)?;
172-
let value = i32::from(read & 0b0111_1111);
173-
result |= value.overflowing_shl(7 * num_read).0;
174-
175-
num_read += 1;
176-
177-
if num_read > 5 {
178-
bail!(
179-
"VarInt too long (max length: 5, value read so far: {})",
180-
result
181-
);
182-
}
183-
if read & 0b1000_0000 == 0 {
184-
break;
185-
}
186-
}
187-
Ok(VarInt(result))
168+
Self::read_from(buffer).map_err(Into::into)
188169
}
189170
}
190171

@@ -214,8 +195,9 @@ impl From<i32> for VarInt {
214195
}
215196

216197
impl VarInt {
217-
pub fn write_to(&self, mut writer: impl Write) -> io::Result<()> {
198+
pub fn write_to(&self, mut writer: impl Write) -> io::Result<usize> {
218199
let mut x = self.0 as u32;
200+
let mut i = 0;
219201
loop {
220202
let mut temp = (x & 0b0111_1111) as u8;
221203
x >>= 7;
@@ -225,11 +207,35 @@ impl VarInt {
225207

226208
writer.write_all(&[temp])?;
227209

210+
i += 1;
228211
if x == 0 {
229212
break;
230213
}
231214
}
232-
Ok(())
215+
Ok(i)
216+
}
217+
pub fn read_from(mut reader: impl Read) -> io::Result<Self> {
218+
let mut num_read = 0;
219+
let mut result = 0;
220+
221+
loop {
222+
let read = reader.read_u8()?;
223+
let value = i32::from(read & 0b0111_1111);
224+
result |= value.overflowing_shl(7 * num_read).0;
225+
226+
num_read += 1;
227+
228+
if num_read > 5 {
229+
return Err(io::Error::new(
230+
ErrorKind::InvalidData,
231+
"VarInt too long (max length: 5)",
232+
));
233+
}
234+
if read & 0b1000_0000 == 0 {
235+
break;
236+
}
237+
}
238+
Ok(VarInt(result))
233239
}
234240
}
235241

feather/protocol/src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub mod codec;
44
pub mod io;
55
pub mod packets;
66

7+
use crate::codec::CompressionThreshold;
78
#[doc(inline)]
89
pub use codec::MinecraftCodec;
910
pub use io::Nbt;
@@ -62,6 +63,10 @@ impl ClientPacketCodec {
6263
self.state = state
6364
}
6465

66+
pub fn set_compression(&mut self, threshold: CompressionThreshold) {
67+
self.codec.enable_compression(threshold)
68+
}
69+
6570
/// Decodes a `ClientPacket` using the provided data.
6671
pub fn decode(&mut self, data: &[u8]) -> anyhow::Result<Option<ClientPacket>> {
6772
self.codec.accept(data);
@@ -120,6 +125,10 @@ impl ServerPacketCodec {
120125
self.state = state
121126
}
122127

128+
pub fn set_compression(&mut self, threshold: CompressionThreshold) {
129+
self.codec.enable_compression(threshold)
130+
}
131+
123132
/// Decodes a `ServerPacket` using the provided data.
124133
pub fn decode(&mut self, data: &[u8]) -> anyhow::Result<Option<ServerPacket>> {
125134
self.codec.accept(data);
@@ -159,6 +168,17 @@ pub enum ClientPacket {
159168
Play(ClientPlayPacket),
160169
}
161170

171+
impl ClientPacket {
172+
pub fn id(&self) -> u32 {
173+
match self {
174+
ClientPacket::Handshake(packet) => packet.id(),
175+
ClientPacket::Status(packet) => packet.id(),
176+
ClientPacket::Login(packet) => packet.id(),
177+
ClientPacket::Play(packet) => packet.id(),
178+
}
179+
}
180+
}
181+
162182
impl From<ClientHandshakePacket> for ClientPacket {
163183
fn from(packet: ClientHandshakePacket) -> Self {
164184
ClientPacket::Handshake(packet)
@@ -191,6 +211,16 @@ pub enum ServerPacket {
191211
Play(ServerPlayPacket),
192212
}
193213

214+
impl ServerPacket {
215+
pub fn id(&self) -> u32 {
216+
match self {
217+
ServerPacket::Status(packet) => packet.id(),
218+
ServerPacket::Login(packet) => packet.id(),
219+
ServerPacket::Play(packet) => packet.id(),
220+
}
221+
}
222+
}
223+
194224
impl From<ServerStatusPacket> for ServerPacket {
195225
fn from(packet: ServerStatusPacket) -> Self {
196226
ServerPacket::Status(packet)

feather/protocol/src/packets/server/play/chunk_data.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ fn encode_section(
145145
Ok(())
146146
}
147147

148-
#[cfg(feature = "proxy")]
149148
impl Readable for ChunkData {
150149
fn read(buffer: &mut std::io::Cursor<&[u8]>, version: ProtocolVersion) -> anyhow::Result<Self>
151150
where

0 commit comments

Comments
 (0)