diff options
| author | mat <github@matdoes.dev> | 2022-01-01 19:44:51 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-01-01 19:44:51 -0600 |
| commit | e81b85dd5bdd6d42ee84f24ed4a142f6141f170e (patch) | |
| tree | 16d950954429b403e2adcc582feb64da73da42df /azalea-protocol/src | |
| parent | 1a961d968b80b720ef2d3900c0b95e1c16a0089e (diff) | |
| download | azalea-drasl-e81b85dd5bdd6d42ee84f24ed4a142f6141f170e.tar.xz | |
add a couple more packets
Diffstat (limited to 'azalea-protocol/src')
6 files changed, 87 insertions, 23 deletions
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<Vec<u8>, String>; - async fn read_bytes(&mut self, n: usize) -> Result<Vec<u8>, String>; + async fn read_bytes_with_len(&mut self, n: usize) -> Result<Vec<u8>, String>; + async fn read_bytes(&mut self) -> Result<Vec<u8>, String>; 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>; @@ -230,10 +231,10 @@ where async fn read_byte_array(&mut self) -> Result<Vec<u8>, 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<Vec<u8>, String> { + async fn read_bytes_with_len(&mut self, n: usize) -> Result<Vec<u8>, 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<Vec<u8>, 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<String, String> { self.read_utf_with_len(MAX_STRING_LENGTH.into()).await } diff --git a/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs b/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs new file mode 100644 index 00000000..63047801 --- /dev/null +++ b/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs @@ -0,0 +1,30 @@ +use super::GamePacket; +use crate::mc_buf::{Readable, Writable}; +use azalea_core::{game_type::GameType, resource_location::ResourceLocation}; + +#[derive(Clone, Debug)] +pub struct ClientboundCustomPayloadPacket { + pub identifier: ResourceLocation, + pub data: Vec<u8>, +} + +impl ClientboundCustomPayloadPacket { + pub fn get(self) -> GamePacket { + GamePacket::ClientboundCustomPayloadPacket(self) + } + + pub fn write(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> { + buf.write_resource_location(&self.identifier)?; + buf.write_bytes(&self.data)?; + Ok(()) + } + + pub async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( + buf: &mut T, + ) -> Result<GamePacket, String> { + 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_login_packet.rs b/azalea-protocol/src/packets/game/clientbound_login_packet.rs index 9043fa1a..0286fce4 100644 --- a/azalea-protocol/src/packets/game/clientbound_login_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_login_packet.rs @@ -4,23 +4,6 @@ use azalea_core::{game_type::GameType, resource_location::ResourceLocation}; #[derive(Clone, Debug)] pub struct ClientboundLoginPacket { - // private final int playerId; - // private final boolean hardcore; - // private final GameType gameType; - // @Nullable - // private final GameType previousGameType; - // private final Set<ResourceKey<Level>> levels; - // private final RegistryAccess.RegistryHolder registryHolder; - // private final DimensionType dimensionType; - // private final ResourceKey<Level> dimension; - // private final long seed; - // private final int maxPlayers; - // private final int chunkRadius; - // private final int simulationDistance; - // private final boolean reducedDebugInfo; - // private final boolean showDeathScreen; - // private final boolean isDebug; - // private final boolean isFlat; pub player_id: i32, pub hardcore: bool, pub game_type: GameType, 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 new file mode 100644 index 00000000..562f8fc2 --- /dev/null +++ b/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs @@ -0,0 +1,28 @@ +// i don't know the actual name of this packet, i couldn't find it in the source code! + +use super::GamePacket; +use crate::mc_buf::{Readable, Writable}; + +#[derive(Clone, Debug)] +pub struct ClientboundUpdateViewDistancePacket { + pub view_distance: i32, +} + +impl ClientboundUpdateViewDistancePacket { + pub fn get(self) -> GamePacket { + GamePacket::ClientboundUpdateViewDistancePacket(self) + } + + pub fn write(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> { + buf.write_varint(self.view_distance)?; + Ok(()) + } + + pub async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( + buf: &mut T, + ) -> Result<GamePacket, String> { + 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 00fa1d75..ab5ca7e8 100644 --- a/azalea-protocol/src/packets/game/mod.rs +++ b/azalea-protocol/src/packets/game/mod.rs @@ -1,4 +1,6 @@ +pub mod clientbound_custom_payload_packet; pub mod clientbound_login_packet; +pub mod clientbound_update_view_distance_packet; use super::ProtocolPacket; use crate::connect::PacketFlow; @@ -10,13 +12,21 @@ where Self: Sized, { ClientboundLoginPacket(clientbound_login_packet::ClientboundLoginPacket), + ClientboundUpdateViewDistancePacket( + clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket, + ), + ClientboundCustomPayloadPacket( + clientbound_custom_payload_packet::ClientboundCustomPayloadPacket, + ), } #[async_trait] impl ProtocolPacket for GamePacket { fn id(&self) -> u32 { match self { - GamePacket::ClientboundLoginPacket(_packet) => 0x00, + GamePacket::ClientboundCustomPayloadPacket(_packet) => 0x18, + GamePacket::ClientboundLoginPacket(_packet) => 0x26, + GamePacket::ClientboundUpdateViewDistancePacket(_packet) => 0x4a, } } @@ -33,8 +43,11 @@ impl ProtocolPacket for GamePacket { { Ok(match flow { PacketFlow::ServerToClient => match id { + 0x18 => clientbound_custom_payload_packet::ClientboundCustomPayloadPacket::read(buf).await?, 0x26 => clientbound_login_packet::ClientboundLoginPacket::read(buf).await?, - + 0x4a => clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket + ::read(buf) + .await?, _ => return Err(format!("Unknown ServerToClient game packet id: {}", id)), }, PacketFlow::ClientToServer => match id { 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 2bc1fc1e..048fa53f 100644 --- a/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs @@ -26,7 +26,7 @@ impl ClientboundCustomQueryPacket { ) -> Result<LoginPacket, String> { let transaction_id = buf.read_varint().await? as u32; let identifier = ResourceLocation::new(&buf.read_utf().await?)?; - let data = buf.read_bytes(1048576).await?; + let data = buf.read_bytes_with_len(1048576).await?; Ok(ClientboundCustomQueryPacket { transaction_id, identifier, |
