From e81b85dd5bdd6d42ee84f24ed4a142f6141f170e Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 1 Jan 2022 19:44:51 -0600 Subject: add a couple more packets --- azalea-protocol/src/mc_buf.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'azalea-protocol/src/mc_buf.rs') diff --git a/azalea-protocol/src/mc_buf.rs b/azalea-protocol/src/mc_buf.rs index 538fc212..860f61f2 100644 --- a/azalea-protocol/src/mc_buf.rs +++ b/azalea-protocol/src/mc_buf.rs @@ -166,7 +166,8 @@ pub trait Readable { fn get_varint_size(&mut self, value: i32) -> u8; fn get_varlong_size(&mut self, value: i32) -> u8; async fn read_byte_array(&mut self) -> Result, String>; - async fn read_bytes(&mut self, n: usize) -> Result, String>; + async fn read_bytes_with_len(&mut self, n: usize) -> Result, String>; + async fn read_bytes(&mut self) -> Result, String>; async fn read_utf(&mut self) -> Result; async fn read_utf_with_len(&mut self, max_length: u32) -> Result; async fn read_byte(&mut self) -> Result; @@ -230,10 +231,10 @@ where async fn read_byte_array(&mut self) -> Result, String> { let length = self.read_varint().await? as usize; - Ok(self.read_bytes(length).await?) + Ok(self.read_bytes_with_len(length).await?) } - async fn read_bytes(&mut self, n: usize) -> Result, String> { + async fn read_bytes_with_len(&mut self, n: usize) -> Result, String> { let mut bytes = vec![0; n]; match AsyncReadExt::read_exact(self, &mut bytes).await { Ok(_) => Ok(bytes), @@ -241,6 +242,15 @@ where } } + async fn read_bytes(&mut self) -> Result, String> { + // read to end of the buffer + let mut bytes = vec![]; + AsyncReadExt::read_to_end(self, &mut bytes) + .await + .map_err(|_| "Error reading bytes".to_string())?; + Ok(bytes) + } + async fn read_utf(&mut self) -> Result { self.read_utf_with_len(MAX_STRING_LENGTH.into()).await } -- cgit v1.2.3 From a1afbb6031527c1db5831fc8e916bc0ecce633b4 Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 1 Jan 2022 23:55:19 -0600 Subject: start adding packet macros --- Cargo.lock | 26 +++- azalea-protocol/Cargo.toml | 3 + azalea-protocol/packet-macros/Cargo.toml | 14 ++ azalea-protocol/packet-macros/src/lib.rs | 164 +++++++++++++++++++++ azalea-protocol/src/mc_buf.rs | 14 +- .../game/clientbound_custom_payload_packet.rs | 28 +--- .../clientbound_update_view_distance_packet.rs | 23 +-- azalea-protocol/src/packets/game/mod.rs | 7 +- .../packets/handshake/client_intention_packet.rs | 60 +++++--- azalea-protocol/src/packets/handshake/mod.rs | 2 +- .../login/clientbound_custom_query_packet.rs | 3 +- .../login/clientbound_game_profile_packet.rs | 3 +- .../src/packets/login/clientbound_hello_packet.rs | 2 +- .../login/clientbound_login_compression_packet.rs | 3 +- azalea-protocol/src/packets/login/mod.rs | 2 +- .../src/packets/login/serverbound_hello_packet.rs | 3 +- azalea-protocol/src/packets/mod.rs | 8 +- .../status/clientbound_status_response_packet.rs | 4 +- azalea-protocol/src/packets/status/mod.rs | 2 +- .../status/serverbound_status_request_packet.rs | 2 +- 20 files changed, 285 insertions(+), 88 deletions(-) create mode 100644 azalea-protocol/packet-macros/Cargo.toml create mode 100644 azalea-protocol/packet-macros/src/lib.rs (limited to 'azalea-protocol/src/mc_buf.rs') diff --git a/Cargo.lock b/Cargo.lock index c037630d..eca4d4fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -118,6 +118,9 @@ dependencies = [ "azalea-nbt", "byteorder", "bytes", + "num-derive", + "num-traits", + "packet-macros", "serde", "serde_json", "thiserror", @@ -172,6 +175,15 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" +[[package]] +name = "casey" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fabe85130dda9cf267715582ce6cf1ab581c8dfe3cb33f7065fee0f14e3fea14" +dependencies = [ + "syn", +] + [[package]] name = "cast" version = "0.2.7" @@ -676,6 +688,16 @@ version = "11.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" +[[package]] +name = "packet-macros" +version = "0.1.0" +dependencies = [ + "casey", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "parking_lot" version = "0.11.2" @@ -755,9 +777,9 @@ checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba" [[package]] name = "proc-macro2" -version = "1.0.32" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba508cc11742c0dc5c1659771673afbab7a0efab23aa17e854cbab0837ed0b43" +checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" dependencies = [ "unicode-xid", ] diff --git a/azalea-protocol/Cargo.toml b/azalea-protocol/Cargo.toml index c9883195..ff3bd9d4 100644 --- a/azalea-protocol/Cargo.toml +++ b/azalea-protocol/Cargo.toml @@ -15,6 +15,9 @@ azalea-core = {path = "../azalea-core"} azalea-nbt = {path = "../azalea-nbt"} byteorder = "^1.4.3" bytes = "^1.1.0" +num-derive = "^0.3.3" +num-traits = "^0.2.14" +packet-macros = {path = "./packet-macros"} serde = {version = "1.0.130", features = ["serde_derive"]} serde_json = "^1.0.72" thiserror = "^1.0.30" diff --git a/azalea-protocol/packet-macros/Cargo.toml b/azalea-protocol/packet-macros/Cargo.toml new file mode 100644 index 00000000..5a301756 --- /dev/null +++ b/azalea-protocol/packet-macros/Cargo.toml @@ -0,0 +1,14 @@ +[package] +edition = "2021" +name = "packet-macros" +version = "0.1.0" + +[lib] +proc-macro = true +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +casey = "^0.3.3" +proc-macro2 = "^1.0.36" +quote = "^1.0.10" +syn = "^1.0.82" diff --git a/azalea-protocol/packet-macros/src/lib.rs b/azalea-protocol/packet-macros/src/lib.rs new file mode 100644 index 00000000..470ac4c1 --- /dev/null +++ b/azalea-protocol/packet-macros/src/lib.rs @@ -0,0 +1,164 @@ +use quote::{quote, quote_spanned, ToTokens}; +use syn::{self, parse_macro_input, spanned::Spanned, DeriveInput, FieldsNamed}; + +fn as_packet_derive( + input: proc_macro::TokenStream, + state: proc_macro2::TokenStream, +) -> proc_macro::TokenStream { + let DeriveInput { ident, data, .. } = parse_macro_input!(input); + + let fields = match data { + syn::Data::Struct(syn::DataStruct { fields, .. }) => fields, + _ => panic!("#[derive(*Packet)] can only be used on structs"), + }; + let FieldsNamed { named, .. } = match fields { + syn::Fields::Named(f) => f, + _ => panic!("#[derive(*Packet)] can only be used on structs with named fields"), + }; + + let write_fields = named + .iter() + .map(|f| { + let field_name = &f.ident; + let field_type = &f.ty; + // do a different buf.write_* for each field depending on the type + // if it's a string, use buf.write_string + match field_type { + syn::Type::Path(syn::TypePath { path, .. }) => { + if path.is_ident("String") { + quote! { buf.write_utf(&self.#field_name)?; } + } else if path.is_ident("ResourceLocation") { + quote! { buf.write_resource_location(&self.#field_name)?; } + // i don't know how to do this in a way that isn't terrible + } else if path.to_token_stream().to_string() == "Vec < u8 >" { + quote! { buf.write_bytes(&self.#field_name)?; } + } else if path.is_ident("i32") { + // only treat it as a varint if it has the varint attribute + if f.attrs.iter().any(|attr| attr.path.is_ident("varint")) { + quote! { buf.write_varint(self.#field_name)?; } + } else { + quote! { buf.write_i32(self.#field_name)?; } + } + } else if path.is_ident("u32") { + if f.attrs.iter().any(|attr| attr.path.is_ident("varint")) { + quote! { buf.write_varint(self.#field_name as i32)?; } + } else { + quote! { buf.write_u32(self.#field_name)?; } + } + } else if path.is_ident("u16") { + quote! { buf.write_short(self.#field_name as i16)?; } + } else if path.is_ident("ConnectionProtocol") { + quote! { buf.write_varint(self.#field_name.clone() as i32)?; } + } else { + panic!( + "#[derive(*Packet)] doesn't know how to write {}", + path.to_token_stream() + ); + } + } + _ => panic!( + "Error writing field {}: {}", + field_name.clone().unwrap(), + field_type.to_token_stream() + ), + } + }) + .collect::>(); + + let read_fields = named + .iter() + .map(|f| { + let field_name = &f.ident; + let field_type = &f.ty; + // do a different buf.write_* for each field depending on the type + // if it's a string, use buf.write_string + match field_type { + syn::Type::Path(syn::TypePath { path, .. }) => { + if path.is_ident("String") { + quote! { let #field_name = buf.read_utf().await?; } + } else if path.is_ident("ResourceLocation") { + quote! { let #field_name = buf.read_resource_location().await?; } + // i don't know how to do this in a way that isn't terrible + } else if path.to_token_stream().to_string() == "Vec < u8 >" { + quote! { let #field_name = buf.read_bytes().await?; } + } else if path.is_ident("i32") { + // only treat it as a varint if it has the varint attribute + if f.attrs.iter().any(|a| a.path.is_ident("varint")) { + quote! { let #field_name = buf.read_varint().await?; } + } else { + quote! { let #field_name = buf.read_i32().await?; } + } + } else if path.is_ident("u32") { + if f.attrs.iter().any(|a| a.path.is_ident("varint")) { + quote! { let #field_name = buf.read_varint().await? as u32; } + } else { + quote! { let #field_name = buf.read_u32().await?; } + } + } else if path.is_ident("u16") { + quote! { let #field_name = buf.read_short().await? as u16; } + } else if path.is_ident("ConnectionProtocol") { + quote! { + let #field_name = ConnectionProtocol::from_i32(buf.read_varint().await?) + .ok_or_else(|| "Invalid intention".to_string())?; + } + } else { + panic!( + "#[derive(*Packet)] doesn't know how to read {}", + path.to_token_stream() + ); + } + } + _ => panic!( + "Error reading field {}: {}", + field_name.clone().unwrap(), + field_type.to_token_stream() + ), + } + }) + .collect::>(); + let read_field_names = named.iter().map(|f| &f.ident).collect::>(); + + let gen = quote! { + impl #ident { + pub fn get(self) -> #state { + #state::#ident(self) + } + + pub fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + #(#write_fields)* + Ok(()) + } + + pub async fn read( + buf: &mut T, + ) -> Result<#state, String> { + #(#read_fields)* + Ok(#ident { + #(#read_field_names: #read_field_names),* + }.get()) + } + } + }; + + gen.into() +} + +#[proc_macro_derive(GamePacket, attributes(varint))] +pub fn derive_game_packet(input: proc_macro::TokenStream) -> proc_macro::TokenStream { + as_packet_derive(input, quote! {crate::packets::game::GamePacket}) +} + +#[proc_macro_derive(HandshakePacket, attributes(varint))] +pub fn derive_handshake_packet(input: proc_macro::TokenStream) -> proc_macro::TokenStream { + as_packet_derive(input, quote! {crate::packets::handshake::HandshakePacket}) +} + +#[proc_macro_derive(LoginPacket, attributes(varint))] +pub fn derive_login_packet(input: proc_macro::TokenStream) -> proc_macro::TokenStream { + as_packet_derive(input, quote! {crate::packets::login::LoginPacket}) +} + +#[proc_macro_derive(StatusPacket, attributes(varint))] +pub fn derive_status_packet(input: proc_macro::TokenStream) -> proc_macro::TokenStream { + as_packet_derive(input, quote! {crate::packets::status::StatusPacket}) +} diff --git a/azalea-protocol/src/mc_buf.rs b/azalea-protocol/src/mc_buf.rs index 860f61f2..72583d5a 100644 --- a/azalea-protocol/src/mc_buf.rs +++ b/azalea-protocol/src/mc_buf.rs @@ -35,7 +35,7 @@ pub trait Writable { fn write_varint(&mut self, value: i32) -> Result<(), std::io::Error>; fn write_utf_with_len(&mut self, string: &str, len: usize) -> Result<(), std::io::Error>; fn write_utf(&mut self, string: &str) -> Result<(), std::io::Error>; - fn write_short(&mut self, n: u16) -> Result<(), std::io::Error>; + fn write_short(&mut self, n: i16) -> Result<(), std::io::Error>; fn write_byte_array(&mut self, bytes: &[u8]) -> Result<(), std::io::Error>; fn write_int(&mut self, n: i32) -> Result<(), std::io::Error>; fn write_boolean(&mut self, b: bool) -> Result<(), std::io::Error>; @@ -125,8 +125,8 @@ impl Writable for Vec { self.write_utf_with_len(string, MAX_STRING_LENGTH.into()) } - fn write_short(&mut self, n: u16) -> Result<(), std::io::Error> { - WriteBytesExt::write_u16::(self, n) + fn write_short(&mut self, n: i16) -> Result<(), std::io::Error> { + WriteBytesExt::write_i16::(self, n) } fn write_byte_array(&mut self, bytes: &[u8]) -> Result<(), std::io::Error> { @@ -176,6 +176,7 @@ pub trait Readable { async fn read_nbt(&mut self) -> Result; async fn read_long(&mut self) -> Result; async fn read_resource_location(&mut self) -> Result; + async fn read_short(&mut self) -> Result; } #[async_trait] @@ -334,6 +335,13 @@ where let location = ResourceLocation::new(&location_string)?; Ok(location) } + + async fn read_short(&mut self) -> Result { + match AsyncReadExt::read_i16(self).await { + Ok(r) => Ok(r), + Err(_) => Err("Error reading short".to_string()), + } + } } #[cfg(test)] diff --git a/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs b/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs index 63047801..24220a83 100644 --- a/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs @@ -1,30 +1,10 @@ -use super::GamePacket; use crate::mc_buf::{Readable, Writable}; -use azalea_core::{game_type::GameType, resource_location::ResourceLocation}; +use crate::packets::game::GamePacket; +use azalea_core::resource_location::ResourceLocation; +use packet_macros::GamePacket; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, GamePacket)] pub struct ClientboundCustomPayloadPacket { pub identifier: ResourceLocation, pub data: Vec, } - -impl ClientboundCustomPayloadPacket { - pub fn get(self) -> GamePacket { - GamePacket::ClientboundCustomPayloadPacket(self) - } - - pub fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - buf.write_resource_location(&self.identifier)?; - buf.write_bytes(&self.data)?; - Ok(()) - } - - pub async fn read( - buf: &mut T, - ) -> Result { - let identifier = buf.read_resource_location().await?; - let data = buf.read_bytes().await?; - - Ok(ClientboundCustomPayloadPacket { identifier, data }.get()) - } -} diff --git a/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs index 562f8fc2..f6028e6c 100644 --- a/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs @@ -2,27 +2,10 @@ use super::GamePacket; use crate::mc_buf::{Readable, Writable}; +use packet_macros::GamePacket; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, GamePacket)] pub struct ClientboundUpdateViewDistancePacket { + #[varint] pub view_distance: i32, } - -impl ClientboundUpdateViewDistancePacket { - pub fn get(self) -> GamePacket { - GamePacket::ClientboundUpdateViewDistancePacket(self) - } - - pub fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - buf.write_varint(self.view_distance)?; - Ok(()) - } - - pub async fn read( - buf: &mut T, - ) -> Result { - let view_distance = buf.read_varint().await?; - - Ok(ClientboundUpdateViewDistancePacket { view_distance }.get()) - } -} diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs index ab5ca7e8..43b3ca3d 100644 --- a/azalea-protocol/src/packets/game/mod.rs +++ b/azalea-protocol/src/packets/game/mod.rs @@ -30,7 +30,9 @@ impl ProtocolPacket for GamePacket { } } - fn write(&self, _buf: &mut Vec) {} + fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + Ok(()) + } /// Read a packet by its id, ConnectionProtocol, and flow async fn read( @@ -48,7 +50,8 @@ impl ProtocolPacket for GamePacket { 0x4a => clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket ::read(buf) .await?, - _ => return Err(format!("Unknown ServerToClient game packet id: {}", id)), + // _ => return Err(format!("Unknown ServerToClient game packet id: {}", id)), + _ => panic!("Unknown ServerToClient game packet id: {}", id), }, PacketFlow::ClientToServer => match id { // 0x00 => serverbound_hello_packet::ServerboundHelloPacket::read(buf).await?, diff --git a/azalea-protocol/src/packets/handshake/client_intention_packet.rs b/azalea-protocol/src/packets/handshake/client_intention_packet.rs index 939a695e..b3eb8301 100644 --- a/azalea-protocol/src/packets/handshake/client_intention_packet.rs +++ b/azalea-protocol/src/packets/handshake/client_intention_packet.rs @@ -1,34 +1,48 @@ +use crate::{ + mc_buf::{Readable, Writable}, + packets::ConnectionProtocol, +}; +use num_traits::FromPrimitive; +use packet_macros::HandshakePacket; use std::hash::Hash; -use crate::{mc_buf::Writable, packets::ConnectionProtocol}; - -use super::HandshakePacket; - -#[derive(Hash, Clone, Debug)] +#[derive(Hash, Clone, Debug, HandshakePacket)] pub struct ClientIntentionPacket { + #[varint] pub protocol_version: u32, pub hostname: String, pub port: u16, - /// 1 for status, 2 for login pub intention: ConnectionProtocol, } -impl ClientIntentionPacket { - pub fn get(self) -> HandshakePacket { - HandshakePacket::ClientIntentionPacket(self) - } +// impl ClientIntentionPacket { +// pub fn get(self) -> HandshakePacket { +// HandshakePacket::ClientIntentionPacket(self) +// } - pub fn write(&self, buf: &mut Vec) { - buf.write_varint(self.protocol_version as i32).unwrap(); - buf.write_utf(&self.hostname).unwrap(); - buf.write_short(self.port).unwrap(); - buf.write_varint(self.intention.clone() as i32).unwrap(); - } +// pub fn write(&self, buf: &mut Vec) { +// buf.write_varint(self.protocol_version as i32).unwrap(); +// buf.write_utf(&self.hostname).unwrap(); +// buf.write_short(self.port).unwrap(); +// buf.write_varint(self.intention.clone() as i32).unwrap(); +// } - pub async fn read( - _buf: &mut T, - ) -> Result { - Err("ClientIntentionPacket::parse not implemented".to_string()) - // Ok(ClientIntentionPacket {}.get()) - } -} +// pub async fn read( +// buf: &mut T, +// ) -> Result { +// let protocol_version = buf.read_varint().await? as u32; +// let hostname = buf.read_utf().await?; +// let port = buf.read_short().await? as u16; +// let intention = buf.read_varint().await?; + +// Ok(HandshakePacket::ClientIntentionPacket( +// ClientIntentionPacket { +// protocol_version, +// hostname, +// port, +// intention: ConnectionProtocol::from_i32(intention) +// .ok_or_else(|| "Invalid intention".to_string())?, +// }, +// )) +// } +// } diff --git a/azalea-protocol/src/packets/handshake/mod.rs b/azalea-protocol/src/packets/handshake/mod.rs index 1d753645..17465fca 100644 --- a/azalea-protocol/src/packets/handshake/mod.rs +++ b/azalea-protocol/src/packets/handshake/mod.rs @@ -22,7 +22,7 @@ impl ProtocolPacket for HandshakePacket { } } - fn write(&self, buf: &mut Vec) { + fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { match self { HandshakePacket::ClientIntentionPacket(packet) => packet.write(buf), } diff --git a/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs b/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs index 048fa53f..22e58b0d 100644 --- a/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs @@ -15,10 +15,11 @@ impl ClientboundCustomQueryPacket { LoginPacket::ClientboundCustomQueryPacket(self) } - pub fn write(&self, buf: &mut Vec) { + pub fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { buf.write_varint(self.transaction_id as i32).unwrap(); buf.write_utf(self.identifier.to_string().as_str()).unwrap(); buf.write_bytes(&self.data).unwrap(); + Ok(()) } pub async fn read( diff --git a/azalea-protocol/src/packets/login/clientbound_game_profile_packet.rs b/azalea-protocol/src/packets/login/clientbound_game_profile_packet.rs index 1a752c1a..c54aa819 100644 --- a/azalea-protocol/src/packets/login/clientbound_game_profile_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_game_profile_packet.rs @@ -14,11 +14,12 @@ impl ClientboundGameProfilePacket { LoginPacket::ClientboundGameProfilePacket(self) } - pub fn write(&self, buf: &mut Vec) { + pub fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { for n in self.game_profile.uuid.to_int_array() { buf.write_int(n as i32).unwrap(); } buf.write_utf(self.game_profile.name.as_str()).unwrap(); + Ok(()) } pub async fn read( diff --git a/azalea-protocol/src/packets/login/clientbound_hello_packet.rs b/azalea-protocol/src/packets/login/clientbound_hello_packet.rs index e0b865be..9d0cec39 100644 --- a/azalea-protocol/src/packets/login/clientbound_hello_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_hello_packet.rs @@ -16,7 +16,7 @@ impl ClientboundHelloPacket { LoginPacket::ClientboundHelloPacket(self) } - pub fn write(&self, _buf: &mut Vec) { + pub fn write(&self, _buf: &mut Vec) -> Result<(), std::io::Error> { panic!("ClientboundHelloPacket::write not implemented") } diff --git a/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs b/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs index af355192..a88c6cbf 100644 --- a/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs @@ -14,8 +14,9 @@ impl ClientboundLoginCompressionPacket { LoginPacket::ClientboundLoginCompressionPacket(self) } - pub fn write(&self, buf: &mut Vec) { + pub fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { buf.write_varint(self.compression_threshold).unwrap(); + Ok(()) } pub async fn read( diff --git a/azalea-protocol/src/packets/login/mod.rs b/azalea-protocol/src/packets/login/mod.rs index 4d490d08..b1f61746 100644 --- a/azalea-protocol/src/packets/login/mod.rs +++ b/azalea-protocol/src/packets/login/mod.rs @@ -34,7 +34,7 @@ impl ProtocolPacket for LoginPacket { } } - fn write(&self, buf: &mut Vec) { + fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { match self { LoginPacket::ClientboundCustomQueryPacket(packet) => packet.write(buf), LoginPacket::ClientboundGameProfilePacket(packet) => packet.write(buf), diff --git a/azalea-protocol/src/packets/login/serverbound_hello_packet.rs b/azalea-protocol/src/packets/login/serverbound_hello_packet.rs index 0039cbce..a72480f2 100644 --- a/azalea-protocol/src/packets/login/serverbound_hello_packet.rs +++ b/azalea-protocol/src/packets/login/serverbound_hello_packet.rs @@ -14,8 +14,9 @@ impl ServerboundHelloPacket { LoginPacket::ServerboundHelloPacket(self) } - pub fn write(&self, buf: &mut Vec) { + pub fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { buf.write_utf(&self.username).unwrap(); + Ok(()) } pub async fn read( diff --git a/azalea-protocol/src/packets/mod.rs b/azalea-protocol/src/packets/mod.rs index e065b65c..0f1cd2f0 100644 --- a/azalea-protocol/src/packets/mod.rs +++ b/azalea-protocol/src/packets/mod.rs @@ -3,13 +3,13 @@ pub mod handshake; pub mod login; pub mod status; -use async_trait::async_trait; - use crate::connect::PacketFlow; +use async_trait::async_trait; +use num_derive::FromPrimitive; pub const PROTOCOL_VERSION: u32 = 757; -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, Eq, Hash, FromPrimitive)] pub enum ConnectionProtocol { Handshake = -1, Game = 0, @@ -42,5 +42,5 @@ where where Self: Sized; - fn write(&self, buf: &mut Vec); + fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error>; } diff --git a/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs b/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs index 38270ad1..58f5b701 100644 --- a/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs +++ b/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs @@ -39,7 +39,9 @@ impl ClientboundStatusResponsePacket { StatusPacket::ClientboundStatusResponsePacket(Box::new(self)) } - pub fn write(&self, _buf: &mut Vec) {} + pub fn write(&self, _buf: &mut Vec) -> Result<(), std::io::Error> { + Ok(()) + } pub async fn read( buf: &mut T, diff --git a/azalea-protocol/src/packets/status/mod.rs b/azalea-protocol/src/packets/status/mod.rs index 6383bae8..31fedfb9 100644 --- a/azalea-protocol/src/packets/status/mod.rs +++ b/azalea-protocol/src/packets/status/mod.rs @@ -29,7 +29,7 @@ impl ProtocolPacket for StatusPacket { } } - fn write(&self, buf: &mut Vec) { + fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { match self { StatusPacket::ServerboundStatusRequestPacket(packet) => packet.write(buf), StatusPacket::ClientboundStatusResponsePacket(packet) => packet.write(buf), diff --git a/azalea-protocol/src/packets/status/serverbound_status_request_packet.rs b/azalea-protocol/src/packets/status/serverbound_status_request_packet.rs index 3a25ac42..af98f7cb 100644 --- a/azalea-protocol/src/packets/status/serverbound_status_request_packet.rs +++ b/azalea-protocol/src/packets/status/serverbound_status_request_packet.rs @@ -10,7 +10,7 @@ impl ServerboundStatusRequestPacket { StatusPacket::ServerboundStatusRequestPacket(self) } - pub fn write(&self, _buf: &mut Vec) { + pub fn write(&self, _buf: &mut Vec) -> Result<(), std::io::Error> { panic!("ServerboundStatusRequestPacket::write not implemented") } -- cgit v1.2.3 From bb57273f48294355d7ac863c291d80878f711c16 Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 2 Jan 2022 17:03:34 -0600 Subject: start improving packet macros --- azalea-protocol/packet-macros/src/lib.rs | 42 ++- azalea-protocol/src/mc_buf.rs | 496 ------------------------------- azalea-protocol/src/mc_buf/mod.rs | 163 ++++++++++ azalea-protocol/src/mc_buf/read.rs | 230 ++++++++++++++ azalea-protocol/src/mc_buf/write.rs | 180 +++++++++++ 5 files changed, 592 insertions(+), 519 deletions(-) delete mode 100644 azalea-protocol/src/mc_buf.rs create mode 100644 azalea-protocol/src/mc_buf/mod.rs create mode 100644 azalea-protocol/src/mc_buf/read.rs create mode 100644 azalea-protocol/src/mc_buf/write.rs (limited to 'azalea-protocol/src/mc_buf.rs') diff --git a/azalea-protocol/packet-macros/src/lib.rs b/azalea-protocol/packet-macros/src/lib.rs index 11878abf..22c39ea9 100644 --- a/azalea-protocol/packet-macros/src/lib.rs +++ b/azalea-protocol/packet-macros/src/lib.rs @@ -32,13 +32,6 @@ fn as_packet_derive( // i don't know how to do this in a way that isn't terrible } else if path.to_token_stream().to_string() == "Vec < u8 >" { quote! { buf.write_bytes(&self.#field_name)?; } - } else if path.is_ident("i32") { - // only treat it as a varint if it has the varint attribute - if f.attrs.iter().any(|attr| attr.path.is_ident("varint")) { - quote! { buf.write_varint(self.#field_name)?; } - } else { - quote! { buf.write_i32(self.#field_name)?; } - } } else if path.is_ident("u32") { if f.attrs.iter().any(|attr| attr.path.is_ident("varint")) { quote! { buf.write_varint(self.#field_name as i32)?; } @@ -50,10 +43,16 @@ fn as_packet_derive( } else if path.is_ident("ConnectionProtocol") { quote! { buf.write_varint(self.#field_name.clone() as i32)?; } } else { - panic!( - "#[derive(*Packet)] doesn't know how to write {}", - path.to_token_stream() - ); + if f.attrs.iter().any(|attr| attr.path.is_ident("varint")) { + quote! { + crate::mc_buf::McBufVarintWritable::varint_write_into(&self.#field_name, buf)?; + } + } else { + quote! { + crate::mc_buf::McBufVarintWritable::write_into(&self.#field_name, buf)?; + } + } + } } _ => panic!( @@ -78,16 +77,8 @@ fn as_packet_derive( quote! { let #field_name = buf.read_utf().await?; } } else if path.is_ident("ResourceLocation") { quote! { let #field_name = buf.read_resource_location().await?; } - // i don't know how to do this in a way that isn't terrible } else if path.to_token_stream().to_string() == "Vec < u8 >" { quote! { let #field_name = buf.read_bytes().await?; } - } else if path.is_ident("i32") { - // only treat it as a varint if it has the varint attribute - if f.attrs.iter().any(|a| a.path.is_ident("varint")) { - quote! { let #field_name = buf.read_varint().await?; } - } else { - quote! { let #field_name = buf.read_i32().await?; } - } } else if path.is_ident("u32") { if f.attrs.iter().any(|a| a.path.is_ident("varint")) { quote! { let #field_name = buf.read_varint().await? as u32; } @@ -102,10 +93,15 @@ fn as_packet_derive( .ok_or_else(|| "Invalid intention".to_string())?; } } else { - panic!( - "#[derive(*Packet)] doesn't know how to read {}", - path.to_token_stream() - ); + if f.attrs.iter().any(|a| a.path.is_ident("varint")) { + quote! { + let #field_name = crate::mc_buf::McBufVarintReadable::varint_read_into(buf).await?; + } + } else { + quote! { + let #field_name = crate::mc_buf::McBufReadable::read_into(buf).await?; + } + } } } _ => panic!( diff --git a/azalea-protocol/src/mc_buf.rs b/azalea-protocol/src/mc_buf.rs deleted file mode 100644 index 72583d5a..00000000 --- a/azalea-protocol/src/mc_buf.rs +++ /dev/null @@ -1,496 +0,0 @@ -//! Utilities for reading and writing for the Minecraft protocol - -use std::io::Write; - -use async_trait::async_trait; -use azalea_core::resource_location::ResourceLocation; -use byteorder::{BigEndian, WriteBytesExt}; -use tokio::io::{AsyncRead, AsyncReadExt}; - -// const DEFAULT_NBT_QUOTA: u32 = 2097152; -const MAX_STRING_LENGTH: u16 = 32767; -// const MAX_COMPONENT_STRING_LENGTH: u32 = 262144; - -#[async_trait] -pub trait Writable { - fn write_list(&mut self, list: &[T], writer: F) -> Result<(), std::io::Error> - where - F: FnOnce(&mut Self, &T) -> Result<(), std::io::Error> + Copy, - T: Sized, - Self: Sized; - fn write_int_id_list(&mut self, list: Vec) -> Result<(), std::io::Error>; - fn write_map( - &mut self, - map: Vec<(KT, VT)>, - key_writer: KF, - value_writer: VF, - ) -> Result<(), std::io::Error> - where - KF: Fn(&mut Self, KT) -> Result<(), std::io::Error> + Copy, - VF: Fn(&mut Self, VT) -> Result<(), std::io::Error> + Copy, - Self: Sized; - - fn write_byte(&mut self, n: u8) -> Result<(), std::io::Error>; - fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), std::io::Error>; - fn write_varint(&mut self, value: i32) -> Result<(), std::io::Error>; - fn write_utf_with_len(&mut self, string: &str, len: usize) -> Result<(), std::io::Error>; - fn write_utf(&mut self, string: &str) -> Result<(), std::io::Error>; - fn write_short(&mut self, n: i16) -> Result<(), std::io::Error>; - fn write_byte_array(&mut self, bytes: &[u8]) -> Result<(), std::io::Error>; - fn write_int(&mut self, n: i32) -> Result<(), std::io::Error>; - fn write_boolean(&mut self, b: bool) -> Result<(), std::io::Error>; - fn write_nbt(&mut self, nbt: &azalea_nbt::Tag) -> Result<(), std::io::Error>; - fn write_long(&mut self, n: i64) -> Result<(), std::io::Error>; - fn write_resource_location( - &mut self, - location: &ResourceLocation, - ) -> Result<(), std::io::Error>; -} - -#[async_trait] -impl Writable for Vec { - fn write_list(&mut self, list: &[T], writer: F) -> Result<(), std::io::Error> - where - F: FnOnce(&mut Self, &T) -> Result<(), std::io::Error> + Copy, - Self: Sized, - { - self.write_varint(list.len() as i32)?; - for item in list { - writer(self, item)?; - } - Ok(()) - } - - fn write_int_id_list(&mut self, list: Vec) -> Result<(), std::io::Error> { - self.write_list(&list, |buf, n| buf.write_varint(*n)) - } - - fn write_map( - &mut self, - map: Vec<(KT, VT)>, - key_writer: KF, - value_writer: VF, - ) -> Result<(), std::io::Error> - where - KF: Fn(&mut Self, KT) -> Result<(), std::io::Error> + Copy, - VF: Fn(&mut Self, VT) -> Result<(), std::io::Error> + Copy, - Self: Sized, - { - self.write_varint(map.len() as i32)?; - for (key, value) in map { - key_writer(self, key)?; - value_writer(self, value)?; - } - Ok(()) - } - - fn write_byte(&mut self, n: u8) -> Result<(), std::io::Error> { - WriteBytesExt::write_u8(self, n) - } - - fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), std::io::Error> { - self.extend_from_slice(bytes); - Ok(()) - } - - fn write_varint(&mut self, mut value: i32) -> Result<(), std::io::Error> { - let mut buffer = [0]; - if value == 0 { - self.write_all(&buffer).unwrap(); - } - while value != 0 { - buffer[0] = (value & 0b0111_1111) as u8; - value = (value >> 7) & (i32::max_value() >> 6); - if value != 0 { - buffer[0] |= 0b1000_0000; - } - self.write_all(&buffer)?; - } - Ok(()) - } - - fn write_utf_with_len(&mut self, string: &str, len: usize) -> Result<(), std::io::Error> { - if string.len() > len { - panic!( - "String too big (was {} bytes encoded, max {})", - string.len(), - len - ); - } - self.write_varint(string.len() as i32)?; - self.write_bytes(string.as_bytes()) - } - - fn write_utf(&mut self, string: &str) -> Result<(), std::io::Error> { - self.write_utf_with_len(string, MAX_STRING_LENGTH.into()) - } - - fn write_short(&mut self, n: i16) -> Result<(), std::io::Error> { - WriteBytesExt::write_i16::(self, n) - } - - fn write_byte_array(&mut self, bytes: &[u8]) -> Result<(), std::io::Error> { - self.write_varint(bytes.len() as i32)?; - self.write_bytes(bytes) - } - - fn write_int(&mut self, n: i32) -> Result<(), std::io::Error> { - WriteBytesExt::write_i32::(self, n) - } - - fn write_boolean(&mut self, b: bool) -> Result<(), std::io::Error> { - self.write_byte(if b { 1 } else { 0 }) - } - - fn write_nbt(&mut self, nbt: &azalea_nbt::Tag) -> Result<(), std::io::Error> { - nbt.write(self) - .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string())) - } - - fn write_long(&mut self, n: i64) -> Result<(), std::io::Error> { - WriteBytesExt::write_i64::(self, n) - } - - fn write_resource_location( - &mut self, - location: &ResourceLocation, - ) -> Result<(), std::io::Error> { - self.write_utf(&location.to_string()) - } -} - -#[async_trait] -pub trait Readable { - async fn read_int_id_list(&mut self) -> Result, String>; - async fn read_varint(&mut self) -> Result; - fn get_varint_size(&mut self, value: i32) -> u8; - fn get_varlong_size(&mut self, value: i32) -> u8; - async fn read_byte_array(&mut self) -> Result, String>; - async fn read_bytes_with_len(&mut self, n: usize) -> Result, String>; - async fn read_bytes(&mut self) -> Result, String>; - async fn read_utf(&mut self) -> Result; - async fn read_utf_with_len(&mut self, max_length: u32) -> Result; - async fn read_byte(&mut self) -> Result; - async fn read_int(&mut self) -> Result; - async fn read_boolean(&mut self) -> Result; - async fn read_nbt(&mut self) -> Result; - async fn read_long(&mut self) -> Result; - async fn read_resource_location(&mut self) -> Result; - async fn read_short(&mut self) -> Result; -} - -#[async_trait] -impl Readable for R -where - R: AsyncRead + std::marker::Unpin + std::marker::Send, -{ - async fn read_int_id_list(&mut self) -> Result, String> { - let len = self.read_varint().await?; - let mut list = Vec::with_capacity(len as usize); - for _ in 0..len { - list.push(self.read_varint().await?); - } - Ok(list) - } - - // fast varints stolen from https://github.com/luojia65/mc-varint/blob/master/src/lib.rs#L67 - /// Read a single varint from the reader and return the value, along with the number of bytes read - async fn read_varint(&mut self) -> Result { - let mut buffer = [0]; - let mut ans = 0; - for i in 0..4 { - self.read_exact(&mut buffer) - .await - .map_err(|_| "Invalid VarInt".to_string())?; - ans |= ((buffer[0] & 0b0111_1111) as i32) << (7 * i); - if buffer[0] & 0b1000_0000 == 0 { - return Ok(ans); - } - } - Ok(ans) - } - - fn get_varint_size(&mut self, value: i32) -> u8 { - for i in 1..5 { - if (value & -1 << (i * 7)) != 0 { - continue; - } - return i; - } - 5 - } - - fn get_varlong_size(&mut self, value: i32) -> u8 { - for i in 1..10 { - if (value & -1 << (i * 7)) != 0 { - continue; - } - return i; - } - 10 - } - - async fn read_byte_array(&mut self) -> Result, String> { - let length = self.read_varint().await? as usize; - Ok(self.read_bytes_with_len(length).await?) - } - - async fn read_bytes_with_len(&mut self, n: usize) -> Result, String> { - let mut bytes = vec![0; n]; - match AsyncReadExt::read_exact(self, &mut bytes).await { - Ok(_) => Ok(bytes), - Err(_) => Err("Error reading bytes".to_string()), - } - } - - async fn read_bytes(&mut self) -> Result, String> { - // read to end of the buffer - let mut bytes = vec![]; - AsyncReadExt::read_to_end(self, &mut bytes) - .await - .map_err(|_| "Error reading bytes".to_string())?; - Ok(bytes) - } - - async fn read_utf(&mut self) -> Result { - self.read_utf_with_len(MAX_STRING_LENGTH.into()).await - } - - async fn read_utf_with_len(&mut self, max_length: u32) -> Result { - let length = self.read_varint().await?; - // i don't know why it's multiplied by 4 but it's like that in mojang's code so - if length < 0 { - return Err( - "The received encoded string buffer length is less than zero! Weird string!" - .to_string(), - ); - } - if length as u32 > max_length * 4 { - return Err(format!( - "The received encoded string buffer length is longer than maximum allowed ({} > {})", - length, - max_length * 4 - )); - } - - // this is probably quite inefficient, idk how to do it better - let mut string = String::new(); - let mut buffer = vec![0; length as usize]; - self.read_exact(&mut buffer) - .await - .map_err(|_| "Invalid UTF-8".to_string())?; - - string.push_str(std::str::from_utf8(&buffer).unwrap()); - if string.len() > length as usize { - return Err(format!( - "The received string length is longer than maximum allowed ({} > {})", - length, max_length - )); - } - - Ok(string) - } - - /// Read a single byte from the reader - async fn read_byte(&mut self) -> Result { - match AsyncReadExt::read_u8(self).await { - Ok(r) => Ok(r), - Err(_) => Err("Error reading byte".to_string()), - } - } - - async fn read_int(&mut self) -> Result { - match AsyncReadExt::read_i32(self).await { - Ok(r) => Ok(r), - Err(_) => Err("Error reading int".to_string()), - } - } - - async fn read_boolean(&mut self) -> Result { - match self.read_byte().await { - Ok(0) => Ok(false), - Ok(1) => Ok(true), - _ => Err("Error reading boolean".to_string()), - } - } - - async fn read_nbt(&mut self) -> Result { - match azalea_nbt::Tag::read(self).await { - Ok(r) => Ok(r), - // Err(e) => Err(e.to_string()), - Err(e) => Err(e.to_string()).unwrap(), - } - } - - async fn read_long(&mut self) -> Result { - match AsyncReadExt::read_i64(self).await { - Ok(r) => Ok(r), - Err(_) => Err("Error reading long".to_string()), - } - } - - async fn read_resource_location(&mut self) -> Result { - // get the resource location from the string - let location_string = self.read_utf().await?; - let location = ResourceLocation::new(&location_string)?; - Ok(location) - } - - async fn read_short(&mut self) -> Result { - match AsyncReadExt::read_i16(self).await { - Ok(r) => Ok(r), - Err(_) => Err("Error reading short".to_string()), - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - use std::{collections::HashMap, io::Cursor}; - use tokio::io::BufReader; - - #[test] - fn test_write_varint() { - let mut buf = Vec::new(); - buf.write_varint(123456).unwrap(); - assert_eq!(buf, vec![192, 196, 7]); - - let mut buf = Vec::new(); - buf.write_varint(0).unwrap(); - assert_eq!(buf, vec![0]); - } - - #[tokio::test] - async fn test_read_varint() { - let mut buf = BufReader::new(Cursor::new(vec![192, 196, 7])); - assert_eq!(buf.read_varint().await.unwrap(), 123456); - assert_eq!(buf.get_varint_size(123456), 3); - - let mut buf = BufReader::new(Cursor::new(vec![0])); - assert_eq!(buf.read_varint().await.unwrap(), 0); - assert_eq!(buf.get_varint_size(0), 1); - - let mut buf = BufReader::new(Cursor::new(vec![1])); - assert_eq!(buf.read_varint().await.unwrap(), 1); - assert_eq!(buf.get_varint_size(1), 1); - } - - #[tokio::test] - async fn test_read_varint_longer() { - let mut buf = BufReader::new(Cursor::new(vec![138, 56, 0, 135, 56, 123])); - assert_eq!(buf.read_varint().await.unwrap(), 7178); - } - - #[tokio::test] - async fn test_list() { - let mut buf = Vec::new(); - buf.write_list(&vec!["a", "bc", "def"], |buf, s| buf.write_utf(s)) - .unwrap(); - - // there's no read_list because idk how to do it in rust - let mut buf = BufReader::new(Cursor::new(buf)); - - let mut result = Vec::new(); - let length = buf.read_varint().await.unwrap(); - for _ in 0..length { - result.push(buf.read_utf().await.unwrap()); - } - - assert_eq!(result, vec!["a", "bc", "def"]); - } - - #[tokio::test] - async fn test_int_id_list() { - let mut buf = Vec::new(); - buf.write_list(&vec![1, 2, 3], |buf, i| buf.write_varint(*i)) - .unwrap(); - - let mut buf = BufReader::new(Cursor::new(buf)); - - let result = buf.read_int_id_list().await.unwrap(); - assert_eq!(result, vec![1, 2, 3]); - } - - #[tokio::test] - async fn test_map() { - let mut buf = Vec::new(); - buf.write_map( - vec![("a", 1), ("bc", 23), ("def", 456)], - Vec::write_utf, - Vec::write_varint, - ) - .unwrap(); - - let mut buf = BufReader::new(Cursor::new(buf)); - - let mut result = Vec::new(); - let length = buf.read_varint().await.unwrap(); - for _ in 0..length { - result.push(( - buf.read_utf().await.unwrap(), - buf.read_varint().await.unwrap(), - )); - } - - assert_eq!( - result, - vec![ - ("a".to_string(), 1), - ("bc".to_string(), 23), - ("def".to_string(), 456) - ] - ); - } - - #[tokio::test] - async fn test_nbt() { - let mut buf = Vec::new(); - buf.write_nbt(&azalea_nbt::Tag::Compound(HashMap::from_iter(vec![( - "hello world".to_string(), - azalea_nbt::Tag::Compound(HashMap::from_iter(vec![( - "name".to_string(), - azalea_nbt::Tag::String("Bananrama".to_string()), - )])), - )]))) - .unwrap(); - - let mut buf = BufReader::new(Cursor::new(buf)); - - let result = buf.read_nbt().await.unwrap(); - assert_eq!( - result, - azalea_nbt::Tag::Compound(HashMap::from_iter(vec![( - "hello world".to_string(), - azalea_nbt::Tag::Compound(HashMap::from_iter(vec![( - "name".to_string(), - azalea_nbt::Tag::String("Bananrama".to_string()), - )])), - )])) - ); - } - - #[tokio::test] - async fn test_long() { - let mut buf = Vec::new(); - buf.write_long(123456).unwrap(); - - let mut buf = BufReader::new(Cursor::new(buf)); - - assert_eq!(buf.read_long().await.unwrap(), 123456); - } - - #[tokio::test] - async fn test_resource_location() { - let mut buf = Vec::new(); - buf.write_resource_location(&ResourceLocation::new("minecraft:dirt").unwrap()) - .unwrap(); - - let mut buf = BufReader::new(Cursor::new(buf)); - - assert_eq!( - buf.read_resource_location().await.unwrap(), - ResourceLocation::new("minecraft:dirt").unwrap() - ); - } -} diff --git a/azalea-protocol/src/mc_buf/mod.rs b/azalea-protocol/src/mc_buf/mod.rs new file mode 100644 index 00000000..a924431e --- /dev/null +++ b/azalea-protocol/src/mc_buf/mod.rs @@ -0,0 +1,163 @@ +//! Utilities for reading and writing for the Minecraft protocol + +mod read; +mod write; + +pub use read::{McBufReadable, McBufVarintReadable, Readable}; +pub use write::{McBufVarintWritable, McBufWritable, Writable}; + +// const DEFAULT_NBT_QUOTA: u32 = 2097152; +const MAX_STRING_LENGTH: u16 = 32767; +// const MAX_COMPONENT_STRING_LENGTH: u32 = 262144; + +#[cfg(test)] +mod tests { + use super::*; + use azalea_core::resource_location::ResourceLocation; + use std::{collections::HashMap, io::Cursor}; + use tokio::io::BufReader; + + #[test] + fn test_write_varint() { + let mut buf = Vec::new(); + buf.write_varint(123456).unwrap(); + assert_eq!(buf, vec![192, 196, 7]); + + let mut buf = Vec::new(); + buf.write_varint(0).unwrap(); + assert_eq!(buf, vec![0]); + } + + #[tokio::test] + async fn test_read_varint() { + let mut buf = BufReader::new(Cursor::new(vec![192, 196, 7])); + assert_eq!(buf.read_varint().await.unwrap(), 123456); + assert_eq!(buf.get_varint_size(123456), 3); + + let mut buf = BufReader::new(Cursor::new(vec![0])); + assert_eq!(buf.read_varint().await.unwrap(), 0); + assert_eq!(buf.get_varint_size(0), 1); + + let mut buf = BufReader::new(Cursor::new(vec![1])); + assert_eq!(buf.read_varint().await.unwrap(), 1); + assert_eq!(buf.get_varint_size(1), 1); + } + + #[tokio::test] + async fn test_read_varint_longer() { + let mut buf = BufReader::new(Cursor::new(vec![138, 56, 0, 135, 56, 123])); + assert_eq!(buf.read_varint().await.unwrap(), 7178); + } + + #[tokio::test] + async fn test_list() { + let mut buf = Vec::new(); + buf.write_list(&vec!["a", "bc", "def"], |buf, s| buf.write_utf(s)) + .unwrap(); + + // there's no read_list because idk how to do it in rust + let mut buf = BufReader::new(Cursor::new(buf)); + + let mut result = Vec::new(); + let length = buf.read_varint().await.unwrap(); + for _ in 0..length { + result.push(buf.read_utf().await.unwrap()); + } + + assert_eq!(result, vec!["a", "bc", "def"]); + } + + #[tokio::test] + async fn test_int_id_list() { + let mut buf = Vec::new(); + buf.write_list(&vec![1, 2, 3], |buf, i| buf.write_varint(*i)) + .unwrap(); + + let mut buf = BufReader::new(Cursor::new(buf)); + + let result = buf.read_int_id_list().await.unwrap(); + assert_eq!(result, vec![1, 2, 3]); + } + + #[tokio::test] + async fn test_map() { + let mut buf = Vec::new(); + buf.write_map( + vec![("a", 1), ("bc", 23), ("def", 456)], + Vec::write_utf, + Vec::write_varint, + ) + .unwrap(); + + let mut buf = BufReader::new(Cursor::new(buf)); + + let mut result = Vec::new(); + let length = buf.read_varint().await.unwrap(); + for _ in 0..length { + result.push(( + buf.read_utf().await.unwrap(), + buf.read_varint().await.unwrap(), + )); + } + + assert_eq!( + result, + vec![ + ("a".to_string(), 1), + ("bc".to_string(), 23), + ("def".to_string(), 456) + ] + ); + } + + #[tokio::test] + async fn test_nbt() { + let mut buf = Vec::new(); + buf.write_nbt(&azalea_nbt::Tag::Compound(HashMap::from_iter(vec![( + "hello world".to_string(), + azalea_nbt::Tag::Compound(HashMap::from_iter(vec![( + "name".to_string(), + azalea_nbt::Tag::String("Bananrama".to_string()), + )])), + )]))) + .unwrap(); + + let mut buf = BufReader::new(Cursor::new(buf)); + + let result = buf.read_nbt().await.unwrap(); + assert_eq!( + result, + azalea_nbt::Tag::Compound(HashMap::from_iter(vec![( + "hello world".to_string(), + azalea_nbt::Tag::Compound(HashMap::from_iter(vec![( + "name".to_string(), + azalea_nbt::Tag::String("Bananrama".to_string()), + )])), + )])) + ); + } + + #[tokio::test] + async fn test_long() { + let mut buf = Vec::new(); + buf.write_long(123456).unwrap(); + + let mut buf = BufReader::new(Cursor::new(buf)); + + assert_eq!(buf.read_long().await.unwrap(), 123456); + } + + #[tokio::test] + async fn test_resource_location() { + let mut buf = Vec::new(); + buf.write_resource_location(&ResourceLocation::new("minecraft:dirt").unwrap()) + .unwrap(); + + let mut buf = BufReader::new(Cursor::new(buf)); + + assert_eq!( + buf.read_resource_location().await.unwrap(), + ResourceLocation::new("minecraft:dirt").unwrap() + ); + } +} diff --git a/azalea-protocol/src/mc_buf/read.rs b/azalea-protocol/src/mc_buf/read.rs new file mode 100644 index 00000000..c429eb7f --- /dev/null +++ b/azalea-protocol/src/mc_buf/read.rs @@ -0,0 +1,230 @@ +use async_trait::async_trait; +use azalea_core::resource_location::ResourceLocation; +use tokio::io::{AsyncRead, AsyncReadExt}; + +use super::MAX_STRING_LENGTH; + +#[async_trait] +pub trait Readable { + async fn read_int_id_list(&mut self) -> Result, String>; + async fn read_varint(&mut self) -> Result; + fn get_varint_size(&mut self, value: i32) -> u8; + fn get_varlong_size(&mut self, value: i32) -> u8; + async fn read_byte_array(&mut self) -> Result, String>; + async fn read_bytes_with_len(&mut self, n: usize) -> Result, String>; + async fn read_bytes(&mut self) -> Result, String>; + async fn read_utf(&mut self) -> Result; + async fn read_utf_with_len(&mut self, max_length: u32) -> Result; + async fn read_byte(&mut self) -> Result; + async fn read_int(&mut self) -> Result; + async fn read_boolean(&mut self) -> Result; + async fn read_nbt(&mut self) -> Result; + async fn read_long(&mut self) -> Result; + async fn read_resource_location(&mut self) -> Result; + async fn read_short(&mut self) -> Result; +} + +#[async_trait] +impl Readable for R +where + R: AsyncRead + std::marker::Unpin + std::marker::Send, +{ + async fn read_int_id_list(&mut self) -> Result, String> { + let len = self.read_varint().await?; + let mut list = Vec::with_capacity(len as usize); + for _ in 0..len { + list.push(self.read_varint().await?); + } + Ok(list) + } + + // fast varints stolen from https://github.com/luojia65/mc-varint/blob/master/src/lib.rs#L67 + /// Read a single varint from the reader and return the value, along with the number of bytes read + async fn read_varint(&mut self) -> Result { + let mut buffer = [0]; + let mut ans = 0; + for i in 0..4 { + self.read_exact(&mut buffer) + .await + .map_err(|_| "Invalid VarInt".to_string())?; + ans |= ((buffer[0] & 0b0111_1111) as i32) << (7 * i); + if buffer[0] & 0b1000_0000 == 0 { + return Ok(ans); + } + } + Ok(ans) + } + + fn get_varint_size(&mut self, value: i32) -> u8 { + for i in 1..5 { + if (value & -1 << (i * 7)) != 0 { + continue; + } + return i; + } + 5 + } + + fn get_varlong_size(&mut self, value: i32) -> u8 { + for i in 1..10 { + if (value & -1 << (i * 7)) != 0 { + continue; + } + return i; + } + 10 + } + + async fn read_byte_array(&mut self) -> Result, String> { + let length = self.read_varint().await? as usize; + Ok(self.read_bytes_with_len(length).await?) + } + + async fn read_bytes_with_len(&mut self, n: usize) -> Result, String> { + let mut bytes = vec![0; n]; + match AsyncReadExt::read_exact(self, &mut bytes).await { + Ok(_) => Ok(bytes), + Err(_) => Err("Error reading bytes".to_string()), + } + } + + async fn read_bytes(&mut self) -> Result, String> { + // read to end of the buffer + let mut bytes = vec![]; + AsyncReadExt::read_to_end(self, &mut bytes) + .await + .map_err(|_| "Error reading bytes".to_string())?; + Ok(bytes) + } + + async fn read_utf(&mut self) -> Result { + self.read_utf_with_len(MAX_STRING_LENGTH.into()).await + } + + async fn read_utf_with_len(&mut self, max_length: u32) -> Result { + let length = self.read_varint().await?; + // i don't know why it's multiplied by 4 but it's like that in mojang's code so + if length < 0 { + return Err( + "The received encoded string buffer length is less than zero! Weird string!" + .to_string(), + ); + } + if length as u32 > max_length * 4 { + return Err(format!( + "The received encoded string buffer length is longer than maximum allowed ({} > {})", + length, + max_length * 4 + )); + } + + // this is probably quite inefficient, idk how to do it better + let mut string = String::new(); + let mut buffer = vec![0; length as usize]; + self.read_exact(&mut buffer) + .await + .map_err(|_| "Invalid UTF-8".to_string())?; + + string.push_str(std::str::from_utf8(&buffer).unwrap()); + if string.len() > length as usize { + return Err(format!( + "The received string length is longer than maximum allowed ({} > {})", + length, max_length + )); + } + + Ok(string) + } + + /// Read a single byte from the reader + async fn read_byte(&mut self) -> Result { + match AsyncReadExt::read_u8(self).await { + Ok(r) => Ok(r), + Err(_) => Err("Error reading byte".to_string()), + } + } + + async fn read_int(&mut self) -> Result { + match AsyncReadExt::read_i32(self).await { + Ok(r) => Ok(r), + Err(_) => Err("Error reading int".to_string()), + } + } + + async fn read_boolean(&mut self) -> Result { + match self.read_byte().await { + Ok(0) => Ok(false), + Ok(1) => Ok(true), + _ => Err("Error reading boolean".to_string()), + } + } + + async fn read_nbt(&mut self) -> Result { + match azalea_nbt::Tag::read(self).await { + Ok(r) => Ok(r), + // Err(e) => Err(e.to_string()), + Err(e) => Err(e.to_string()).unwrap(), + } + } + + async fn read_long(&mut self) -> Result { + match AsyncReadExt::read_i64(self).await { + Ok(r) => Ok(r), + Err(_) => Err("Error reading long".to_string()), + } + } + + async fn read_resource_location(&mut self) -> Result { + // get the resource location from the string + let location_string = self.read_utf().await?; + let location = ResourceLocation::new(&location_string)?; + Ok(location) + } + + async fn read_short(&mut self) -> Result { + match AsyncReadExt::read_i16(self).await { + Ok(r) => Ok(r), + Err(_) => Err("Error reading short".to_string()), + } + } +} + +#[async_trait] +pub trait McBufReadable +where + Self: Sized, +{ + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send; +} + +#[async_trait] +pub trait McBufVarintReadable +where + Self: Sized, +{ + async fn varint_read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send; +} + +#[async_trait] +impl McBufReadable for i32 { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + buf.read_int().await + } +} + +#[async_trait] +impl McBufVarintReadable for i32 { + async fn varint_read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + buf.read_varint().await + } +} diff --git a/azalea-protocol/src/mc_buf/write.rs b/azalea-protocol/src/mc_buf/write.rs new file mode 100644 index 00000000..3c3375b9 --- /dev/null +++ b/azalea-protocol/src/mc_buf/write.rs @@ -0,0 +1,180 @@ +use async_trait::async_trait; +use azalea_core::resource_location::ResourceLocation; +use byteorder::{BigEndian, WriteBytesExt}; +use std::io::Write; + +use super::MAX_STRING_LENGTH; + +#[async_trait] +pub trait Writable { + fn write_list(&mut self, list: &[T], writer: F) -> Result<(), std::io::Error> + where + F: FnOnce(&mut Self, &T) -> Result<(), std::io::Error> + Copy, + T: Sized, + Self: Sized; + fn write_int_id_list(&mut self, list: Vec) -> Result<(), std::io::Error>; + fn write_map( + &mut self, + map: Vec<(KT, VT)>, + key_writer: KF, + value_writer: VF, + ) -> Result<(), std::io::Error> + where + KF: Fn(&mut Self, KT) -> Result<(), std::io::Error> + Copy, + VF: Fn(&mut Self, VT) -> Result<(), std::io::Error> + Copy, + Self: Sized; + + fn write_byte(&mut self, n: u8) -> Result<(), std::io::Error>; + fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), std::io::Error>; + fn write_varint(&mut self, value: i32) -> Result<(), std::io::Error>; + fn write_utf_with_len(&mut self, string: &str, len: usize) -> Result<(), std::io::Error>; + fn write_utf(&mut self, string: &str) -> Result<(), std::io::Error>; + fn write_short(&mut self, n: i16) -> Result<(), std::io::Error>; + fn write_byte_array(&mut self, bytes: &[u8]) -> Result<(), std::io::Error>; + fn write_int(&mut self, n: i32) -> Result<(), std::io::Error>; + fn write_boolean(&mut self, b: bool) -> Result<(), std::io::Error>; + fn write_nbt(&mut self, nbt: &azalea_nbt::Tag) -> Result<(), std::io::Error>; + fn write_long(&mut self, n: i64) -> Result<(), std::io::Error>; + fn write_resource_location( + &mut self, + location: &ResourceLocation, + ) -> Result<(), std::io::Error>; +} + +#[async_trait] +impl Writable for Vec { + fn write_list(&mut self, list: &[T], writer: F) -> Result<(), std::io::Error> + where + F: FnOnce(&mut Self, &T) -> Result<(), std::io::Error> + Copy, + Self: Sized, + { + self.write_varint(list.len() as i32)?; + for item in list { + writer(self, item)?; + } + Ok(()) + } + + fn write_int_id_list(&mut self, list: Vec) -> Result<(), std::io::Error> { + self.write_list(&list, |buf, n| buf.write_varint(*n)) + } + + fn write_map( + &mut self, + map: Vec<(KT, VT)>, + key_writer: KF, + value_writer: VF, + ) -> Result<(), std::io::Error> + where + KF: Fn(&mut Self, KT) -> Result<(), std::io::Error> + Copy, + VF: Fn(&mut Self, VT) -> Result<(), std::io::Error> + Copy, + Self: Sized, + { + self.write_varint(map.len() as i32)?; + for (key, value) in map { + key_writer(self, key)?; + value_writer(self, value)?; + } + Ok(()) + } + + fn write_byte(&mut self, n: u8) -> Result<(), std::io::Error> { + WriteBytesExt::write_u8(self, n) + } + + fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), std::io::Error> { + self.extend_from_slice(bytes); + Ok(()) + } + + fn write_varint(&mut self, mut value: i32) -> Result<(), std::io::Error> { + let mut buffer = [0]; + if value == 0 { + self.write_all(&buffer).unwrap(); + } + while value != 0 { + buffer[0] = (value & 0b0111_1111) as u8; + value = (value >> 7) & (i32::max_value() >> 6); + if value != 0 { + buffer[0] |= 0b1000_0000; + } + self.write_all(&buffer)?; + } + Ok(()) + } + + fn write_utf_with_len(&mut self, string: &str, len: usize) -> Result<(), std::io::Error> { + if string.len() > len { + panic!( + "String too big (was {} bytes encoded, max {})", + string.len(), + len + ); + } + self.write_varint(string.len() as i32)?; + self.write_bytes(string.as_bytes()) + } + + fn write_utf(&mut self, string: &str) -> Result<(), std::io::Error> { + self.write_utf_with_len(string, MAX_STRING_LENGTH.into()) + } + + fn write_short(&mut self, n: i16) -> Result<(), std::io::Error> { + WriteBytesExt::write_i16::(self, n) + } + + fn write_byte_array(&mut self, bytes: &[u8]) -> Result<(), std::io::Error> { + self.write_varint(bytes.len() as i32)?; + self.write_bytes(bytes) + } + + fn write_int(&mut self, n: i32) -> Result<(), std::io::Error> { + WriteBytesExt::write_i32::(self, n) + } + + fn write_boolean(&mut self, b: bool) -> Result<(), std::io::Error> { + self.write_byte(if b { 1 } else { 0 }) + } + + fn write_nbt(&mut self, nbt: &azalea_nbt::Tag) -> Result<(), std::io::Error> { + nbt.write(self) + .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string())) + } + + fn write_long(&mut self, n: i64) -> Result<(), std::io::Error> { + WriteBytesExt::write_i64::(self, n) + } + + fn write_resource_location( + &mut self, + location: &ResourceLocation, + ) -> Result<(), std::io::Error> { + self.write_utf(&location.to_string()) + } +} + +pub trait McBufWritable +where + Self: Sized, +{ + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error>; +} + +pub trait McBufVarintWritable +where + Self: Sized, +{ + fn varint_write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error>; +} + +impl McBufWritable for i32 { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + Writable::write_int(buf, *self) + } +} + +impl McBufVarintWritable for i32 { + fn varint_write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_varint(*self) + } +} -- cgit v1.2.3