diff options
| author | mat <github@matdoes.dev> | 2022-01-01 23:55:19 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-01-01 23:55:19 -0600 |
| commit | a1afbb6031527c1db5831fc8e916bc0ecce633b4 (patch) | |
| tree | 56fbccf52645cc2eefd231506ffe8ed71018dc01 /azalea-protocol/src | |
| parent | e81b85dd5bdd6d42ee84f24ed4a142f6141f170e (diff) | |
| download | azalea-drasl-a1afbb6031527c1db5831fc8e916bc0ecce633b4.tar.xz | |
start adding packet macros
Diffstat (limited to 'azalea-protocol/src')
16 files changed, 80 insertions, 86 deletions
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<u8> { self.write_utf_with_len(string, MAX_STRING_LENGTH.into()) } - fn write_short(&mut self, n: u16) -> Result<(), std::io::Error> { - WriteBytesExt::write_u16::<BigEndian>(self, n) + fn write_short(&mut self, n: i16) -> Result<(), std::io::Error> { + WriteBytesExt::write_i16::<BigEndian>(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<azalea_nbt::Tag, String>; async fn read_long(&mut self) -> Result<i64, String>; async fn read_resource_location(&mut self) -> Result<ResourceLocation, String>; + async fn read_short(&mut self) -> Result<i16, String>; } #[async_trait] @@ -334,6 +335,13 @@ where let location = ResourceLocation::new(&location_string)?; Ok(location) } + + async fn read_short(&mut self) -> Result<i16, String> { + 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<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_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<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 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<u8>) {} + fn write(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> { + Ok(()) + } /// Read a packet by its id, ConnectionProtocol, and flow async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( @@ -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<u8>) { - 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<u8>) { +// 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<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( - _buf: &mut T, - ) -> Result<HandshakePacket, String> { - Err("ClientIntentionPacket::parse not implemented".to_string()) - // Ok(ClientIntentionPacket {}.get()) - } -} +// pub async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( +// buf: &mut T, +// ) -> Result<HandshakePacket, String> { +// 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<u8>) { + fn write(&self, buf: &mut Vec<u8>) -> 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<u8>) { + pub fn write(&self, buf: &mut Vec<u8>) -> 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<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( 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<u8>) { + pub fn write(&self, buf: &mut Vec<u8>) -> 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<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( 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<u8>) { + pub fn write(&self, _buf: &mut Vec<u8>) -> 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<u8>) { + pub fn write(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> { buf.write_varint(self.compression_threshold).unwrap(); + Ok(()) } pub async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( 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<u8>) { + fn write(&self, buf: &mut Vec<u8>) -> 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<u8>) { + pub fn write(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> { buf.write_utf(&self.username).unwrap(); + Ok(()) } pub async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( 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<u8>); + fn write(&self, buf: &mut Vec<u8>) -> 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<u8>) {} + pub fn write(&self, _buf: &mut Vec<u8>) -> Result<(), std::io::Error> { + Ok(()) + } pub async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( 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<u8>) { + fn write(&self, buf: &mut Vec<u8>) -> 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<u8>) { + pub fn write(&self, _buf: &mut Vec<u8>) -> Result<(), std::io::Error> { panic!("ServerboundStatusRequestPacket::write not implemented") } |
