Skip to content

Commit 71d82c5

Browse files
authored
Add encoded_len method to Encode trait. (#125)
This allows packets to calculate their exact length up front. This isn't currently tested or being used for anything, but that will come in later changes.
1 parent 9b8fbc5 commit 71d82c5

File tree

16 files changed

+325
-52
lines changed

16 files changed

+325
-52
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ sha2 = "0.10.6"
4040
thiserror = "1.0.35"
4141
url = { version = "2.2.2", features = ["serde"] }
4242
uuid = { version = "1.1.2", features = ["serde"] }
43-
valence_nbt = "0.2.0"
43+
valence_nbt = "0.3.0"
4444
vek = "0.15.8"
4545

4646
[dependencies.tokio]

packet_inspector/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl Cli {
7878
let len = VarInt(read.packet_buf().len() as i32);
7979
len.encode(&mut len_buf.as_mut_slice())?;
8080

81-
write.write_all(&len_buf[..len.written_size()]).await?;
81+
write.write_all(&len_buf[..len.encoded_len()]).await?;
8282
write.write_all(read.packet_buf()).await?;
8383

8484
pkt

src/block.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ impl Encode for BlockState {
5656
fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> {
5757
VarInt(self.0 as i32).encode(w)
5858
}
59+
60+
fn encoded_len(&self) -> usize {
61+
VarInt(self.0 as i32).encoded_len()
62+
}
5963
}
6064

6165
impl Decode for BlockState {

src/block_pos.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ impl Encode for BlockPos {
5858
_ => bail!("out of range: {self:?}"),
5959
}
6060
}
61+
62+
fn encoded_len(&self) -> usize {
63+
8
64+
}
6165
}
6266

6367
impl Decode for BlockPos {

src/entity/types.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ impl Encode for OptionalInt {
2727
fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> {
2828
VarInt(self.0 as i32).encode(w)
2929
}
30+
31+
fn encoded_len(&self) -> usize {
32+
VarInt(self.0 as i32).encoded_len()
33+
}
3034
}
3135

3236
impl Decode for OptionalInt {
@@ -54,6 +58,10 @@ impl Encode for EulerAngle {
5458
self.yaw.encode(w)?;
5559
self.roll.encode(w)
5660
}
61+
62+
fn encoded_len(&self) -> usize {
63+
self.pitch.encoded_len() + self.yaw.encoded_len() + self.roll.encoded_len()
64+
}
5765
}
5866

5967
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
@@ -70,6 +78,10 @@ impl Encode for Facing {
7078
fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> {
7179
VarInt(*self as i32).encode(w)
7280
}
81+
82+
fn encoded_len(&self) -> usize {
83+
VarInt(*self as i32).encoded_len()
84+
}
7385
}
7486

7587
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
@@ -105,6 +117,12 @@ impl Encode for VillagerData {
105117
VarInt(self.profession as i32).encode(w)?;
106118
VarInt(self.level).encode(w)
107119
}
120+
121+
fn encoded_len(&self) -> usize {
122+
VarInt(self.kind as i32).encoded_len()
123+
+ VarInt(self.profession as i32).encoded_len()
124+
+ VarInt(self.level).encoded_len()
125+
}
108126
}
109127

110128
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
@@ -162,6 +180,10 @@ impl Encode for Pose {
162180
fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> {
163181
VarInt(*self as i32).encode(w)
164182
}
183+
184+
fn encoded_len(&self) -> usize {
185+
VarInt(*self as i32).encoded_len()
186+
}
165187
}
166188

167189
/// The main hand of a player.
@@ -176,6 +198,10 @@ impl Encode for MainArm {
176198
fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> {
177199
(*self as u8).encode(w)
178200
}
201+
202+
fn encoded_len(&self) -> usize {
203+
1
204+
}
179205
}
180206

181207
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
@@ -193,6 +219,10 @@ impl Encode for BoatKind {
193219
fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> {
194220
VarInt(*self as i32).encode(w)
195221
}
222+
223+
fn encoded_len(&self) -> usize {
224+
VarInt(*self as i32).encoded_len()
225+
}
196226
}
197227

198228
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
@@ -215,6 +245,10 @@ impl Encode for CatKind {
215245
fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> {
216246
VarInt(*self as i32).encode(w)
217247
}
248+
249+
fn encoded_len(&self) -> usize {
250+
VarInt(*self as i32).encoded_len()
251+
}
218252
}
219253

220254
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
@@ -229,6 +263,10 @@ impl Encode for FrogKind {
229263
fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> {
230264
VarInt(*self as i32).encode(w)
231265
}
266+
267+
fn encoded_len(&self) -> usize {
268+
VarInt(*self as i32).encoded_len()
269+
}
232270
}
233271

234272
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
@@ -270,6 +308,10 @@ impl Encode for PaintingKind {
270308
fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> {
271309
VarInt(*self as i32).encode(w)
272310
}
311+
312+
fn encoded_len(&self) -> usize {
313+
VarInt(*self as i32).encoded_len()
314+
}
273315
}
274316

275317
// TODO
@@ -282,4 +324,8 @@ impl Encode for Particle {
282324
fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> {
283325
VarInt(*self as i32).encode(w)
284326
}
327+
328+
fn encoded_len(&self) -> usize {
329+
VarInt(*self as i32).encoded_len()
330+
}
285331
}

src/ident.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ impl<S: Encode> Encode for Ident<S> {
225225
fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> {
226226
self.string.encode(w)
227227
}
228+
229+
fn encoded_len(&self) -> usize {
230+
self.string.encoded_len()
231+
}
228232
}
229233

230234
impl<S> Decode for Ident<S>

src/item.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ impl Encode for ItemKind {
1212
fn encode(&self, w: &mut impl std::io::Write) -> anyhow::Result<()> {
1313
VarInt(self.to_raw() as i32).encode(w)
1414
}
15+
16+
fn encoded_len(&self) -> usize {
17+
VarInt(self.to_raw() as i32).encoded_len()
18+
}
1519
}
1620

1721
impl Decode for ItemKind {

0 commit comments

Comments
 (0)