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 | |
| parent | 999116ed7c5edf113e12aae150c2e23974d539dc (diff) | |
| download | azalea-drasl-227ba5511d50af8c7c46a47e09db7f55a0ed84b7.tar.xz | |
add a few more login packets
Diffstat (limited to 'azalea-protocol/src')
6 files changed, 121 insertions, 16 deletions
diff --git a/azalea-protocol/src/mc_buf.rs b/azalea-protocol/src/mc_buf.rs index 5a1ac274..0a47f637 100644 --- a/azalea-protocol/src/mc_buf.rs +++ b/azalea-protocol/src/mc_buf.rs @@ -1,6 +1,6 @@ //! Utilities for reading and writing for the Minecraft protocol -use std::{future::Future, io::Write}; +use std::io::Write; use async_trait::async_trait; use byteorder::{BigEndian, WriteBytesExt}; @@ -35,6 +35,7 @@ pub trait Writable { fn write_utf(&mut self, string: &str) -> Result<(), std::io::Error>; fn write_short(&mut self, n: u16) -> 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>; } #[async_trait] @@ -79,7 +80,8 @@ impl Writable for Vec<u8> { } fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), std::io::Error> { - Ok(self.extend_from_slice(bytes)) + self.extend_from_slice(bytes); + Ok(()) } fn write_varint(&mut self, mut value: i32) -> Result<(), std::io::Error> { @@ -122,6 +124,10 @@ impl Writable for Vec<u8> { 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::<BigEndian>(self, n) + } } #[async_trait] @@ -133,6 +139,7 @@ pub trait Readable { async fn read_utf(&mut self) -> Result<String, String>; async fn read_utf_with_len(&mut self, max_length: u32) -> Result<String, String>; async fn read_byte(&mut self) -> Result<u8, String>; + async fn read_int(&mut self) -> Result<i32, String>; } #[async_trait] @@ -225,6 +232,13 @@ where Err(_) => Err("Error reading byte".to_string()), } } + + async fn read_int(&mut self) -> Result<i32, String> { + match AsyncReadExt::read_i32(self).await { + Ok(r) => Ok(r), + Err(_) => Err("Error reading int".to_string()), + } + } } #[cfg(test)] 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 { diff --git a/azalea-protocol/src/write.rs b/azalea-protocol/src/write.rs index 3d8540eb..bf9fd0aa 100644 --- a/azalea-protocol/src/write.rs +++ b/azalea-protocol/src/write.rs @@ -10,7 +10,9 @@ pub async fn write_packet(packet: impl ProtocolPacket, stream: &mut TcpStream) { // write the packet id let mut id_and_data_buf = vec![]; - id_and_data_buf.write_varint(packet.id() as i32); + id_and_data_buf + .write_varint(packet.id() as i32) + .expect("Writing packet id failed"); packet.write(&mut id_and_data_buf); // write the packet data @@ -18,7 +20,9 @@ pub async fn write_packet(packet: impl ProtocolPacket, stream: &mut TcpStream) { // make a new buffer that has the length at the beginning // and id+data at the end let mut complete_buf: Vec<u8> = Vec::new(); - complete_buf.write_varint(id_and_data_buf.len() as i32); + complete_buf + .write_varint(id_and_data_buf.len() as i32) + .expect("Writing packet length failed"); complete_buf.append(&mut id_and_data_buf); // finally, write and flush to the stream |
