diff options
| author | mat <github@matdoes.dev> | 2021-12-16 17:51:05 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2021-12-16 17:51:05 -0600 |
| commit | 227ba5511d50af8c7c46a47e09db7f55a0ed84b7 (patch) | |
| tree | 1067828ee2082e0f073a4d16b201b2888c55b6e8 /azalea-protocol/src/packets | |
| parent | 999116ed7c5edf113e12aae150c2e23974d539dc (diff) | |
| download | azalea-drasl-227ba5511d50af8c7c46a47e09db7f55a0ed84b7.tar.xz | |
add a few more login packets
Diffstat (limited to 'azalea-protocol/src/packets')
4 files changed, 99 insertions, 12 deletions
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 8f501fc9..2c66bfa3 100644 --- a/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs @@ -1,15 +1,13 @@ +use super::LoginPacket; +use crate::mc_buf::{Readable, Writable}; +use azalea_core::resource_location::ResourceLocation; use std::hash::Hash; use tokio::io::BufReader; -use crate::mc_buf::{Readable, Writable}; - -use super::LoginPacket; - #[derive(Hash, Clone, Debug)] pub struct ClientboundCustomQueryPacket { pub transaction_id: u32, - // TODO: this should be a resource location - pub identifier: String, + pub identifier: ResourceLocation, pub data: Vec<u8>, } @@ -20,7 +18,7 @@ impl ClientboundCustomQueryPacket { pub fn write(&self, buf: &mut Vec<u8>) { buf.write_varint(self.transaction_id as i32).unwrap(); - buf.write_utf(&self.identifier).unwrap(); + buf.write_utf(self.identifier.to_string().as_str()).unwrap(); buf.write_bytes(&self.data).unwrap(); } @@ -28,8 +26,7 @@ impl ClientboundCustomQueryPacket { buf: &mut BufReader<T>, ) -> Result<LoginPacket, String> { let transaction_id = buf.read_varint().await? as u32; - // TODO: this should be a resource location - let identifier = buf.read_utf().await?; + let identifier = ResourceLocation::new(&buf.read_utf().await?)?; let data = buf.read_bytes(1048576).await?; Ok(ClientboundCustomQueryPacket { transaction_id, diff --git a/azalea-protocol/src/packets/login/clientbound_game_profile_packet.rs b/azalea-protocol/src/packets/login/clientbound_game_profile_packet.rs new file mode 100644 index 00000000..88166c54 --- /dev/null +++ b/azalea-protocol/src/packets/login/clientbound_game_profile_packet.rs @@ -0,0 +1,39 @@ +use super::LoginPacket; +use crate::mc_buf::{Readable, Writable}; +use azalea_auth::game_profile::GameProfile; +use azalea_core::{resource_location::ResourceLocation, serializable_uuid::SerializableUuid}; +use std::hash::Hash; +use tokio::io::BufReader; + +#[derive(Hash, Clone, Debug)] +pub struct ClientboundGameProfilePacket { + pub game_profile: GameProfile, +} + +impl ClientboundGameProfilePacket { + pub fn get(self) -> LoginPacket { + LoginPacket::ClientboundGameProfilePacket(self) + } + + pub fn write(&self, buf: &mut Vec<u8>) { + 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(); + } + + pub async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( + buf: &mut BufReader<T>, + ) -> Result<LoginPacket, String> { + let uuid = SerializableUuid::from_int_array( + buf.read_int().await?, + buf.read_int().await?, + buf.read_int().await?, + buf.read_int().await?, + ); + let name = buf.read_utf(16).await?; + ClientboundGameProfilePacket { + game_profile: GameProfile::new(uuid, name), + } + } +} diff --git a/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs b/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs new file mode 100644 index 00000000..87fc6e03 --- /dev/null +++ b/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs @@ -0,0 +1,32 @@ +use std::hash::Hash; +use tokio::io::BufReader; + +use crate::mc_buf::Readable; + +use super::LoginPacket; + +#[derive(Hash, Clone, Debug)] +pub struct ClientboundLoginCompressionPacket { + pub compression_threshold: i32, +} + +impl ClientboundLoginCompressionPacket { + pub fn get(self) -> LoginPacket { + LoginPacket::ClientboundLoginCompressionPacket(self) + } + + pub fn write(&self, buf: &mut Vec<u8>) { + buf.write_varint(self.compression_threshold).unwrap(); + } + + pub async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( + buf: &mut BufReader<T>, + ) -> Result<LoginPacket, String> { + let compression_threshold = buf.read_varint().await?; + + Ok(ClientboundLoginCompressionPacket { + compression_threshold + } + .get()) + } +} diff --git a/azalea-protocol/src/packets/login/mod.rs b/azalea-protocol/src/packets/login/mod.rs index f0ed6717..7fee684a 100644 --- a/azalea-protocol/src/packets/login/mod.rs +++ b/azalea-protocol/src/packets/login/mod.rs @@ -1,5 +1,7 @@ pub mod clientbound_custom_query_packet; +pub mod clientbound_game_profile_packet; pub mod clientbound_hello_packet; +pub mod clientbound_login_compression_packet; pub mod serverbound_hello_packet; use async_trait::async_trait; @@ -15,8 +17,12 @@ where Self: Sized, { ClientboundCustomQueryPacket(clientbound_custom_query_packet::ClientboundCustomQueryPacket), - ServerboundHelloPacket(serverbound_hello_packet::ServerboundHelloPacket), + ClientboundGameProfilePacket(clientbound_game_profile_packet::ClientboundGameProfilePacket), ClientboundHelloPacket(clientbound_hello_packet::ClientboundHelloPacket), + ClientboundLoginCompressionPacket( + clientbound_login_compression_packet::ClientboundLoginCompressionPacket, + ), + ServerboundHelloPacket(serverbound_hello_packet::ServerboundHelloPacket), } #[async_trait] @@ -24,16 +30,20 @@ impl ProtocolPacket for LoginPacket { fn id(&self) -> u32 { match self { LoginPacket::ClientboundCustomQueryPacket(_packet) => 0x04, - LoginPacket::ServerboundHelloPacket(_packet) => 0x00, + LoginPacket::ClientboundGameProfilePacket(_packet) => 0x02, LoginPacket::ClientboundHelloPacket(_packet) => 0x01, + LoginPacket::ClientboundLoginCompressionPacket(_packet) => 0x03, + LoginPacket::ServerboundHelloPacket(_packet) => 0x00, } } fn write(&self, buf: &mut Vec<u8>) { match self { LoginPacket::ClientboundCustomQueryPacket(packet) => packet.write(buf), - LoginPacket::ServerboundHelloPacket(packet) => packet.write(buf), + LoginPacket::ClientboundGameProfilePacket(packet) => packet.write(buf), LoginPacket::ClientboundHelloPacket(packet) => packet.write(buf), + LoginPacket::ClientboundLoginCompressionPacket(packet) => packet.write(buf), + LoginPacket::ServerboundHelloPacket(packet) => packet.write(buf), } } @@ -49,9 +59,18 @@ impl ProtocolPacket for LoginPacket { Ok(match flow { PacketFlow::ServerToClient => match id { 0x01 => clientbound_hello_packet::ClientboundHelloPacket::read(buf).await?, + 0x02 => { + clientbound_game_profile_packet::ClientboundGameProfilePacket::read(buf).await? + } 0x04 => { clientbound_custom_query_packet::ClientboundCustomQueryPacket::read(buf).await? } + 0x03 => { + clientbound_login_compression_packet::ClientboundLoginCompressionPacket::read( + buf, + ) + .await? + } _ => return Err(format!("Unknown ServerToClient status packet id: {}", id)), }, PacketFlow::ClientToServer => match id { |
