diff options
| author | mat <github@matdoes.dev> | 2021-12-14 23:03:26 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2021-12-14 23:03:26 -0600 |
| commit | 0d6e4179e6cc16594a4be3ecd62d89411115f319 (patch) | |
| tree | 019284386b7a2540efe440e0c2f3aad27801a5a6 | |
| parent | c96ae8fce4e53ea9fad111ac7f479f2fa33ef859 (diff) | |
| download | azalea-drasl-0d6e4179e6cc16594a4be3ecd62d89411115f319.tar.xz | |
progress, ?
10 files changed, 112 insertions, 77 deletions
diff --git a/minecraft-client/src/connect.rs b/minecraft-client/src/connect.rs index 1d49fb89..5329cdc0 100644 --- a/minecraft-client/src/connect.rs +++ b/minecraft-client/src/connect.rs @@ -5,7 +5,7 @@ use minecraft_protocol::{ handshake::client_intention_packet::ClientIntentionPacket, login::serverbound_hello_packet::ServerboundHelloPacket, status::clientbound_status_response_packet::ClientboundStatusResponsePacket, - ConnectionProtocol, Packet, PacketTrait, PROTOCOL_VERSION, + ConnectionProtocol, Packet, PROTOCOL_VERSION, }, resolver, ServerAddress, }; diff --git a/minecraft-client/src/ping.rs b/minecraft-client/src/ping.rs index a7a01786..05ea16da 100644 --- a/minecraft-client/src/ping.rs +++ b/minecraft-client/src/ping.rs @@ -7,7 +7,7 @@ use minecraft_protocol::{ clientbound_status_response_packet::ClientboundStatusResponsePacket, serverbound_status_request_packet::ServerboundStatusRequestPacket, }, - ConnectionProtocol, Packet, PacketTrait, PROTOCOL_VERSION, + ConnectionProtocol, Packet, PROTOCOL_VERSION, }, resolver, ServerAddress, }; diff --git a/minecraft-protocol/src/packets/handshake/client_intention_packet.rs b/minecraft-protocol/src/packets/handshake/client_intention_packet.rs index f3416f4c..76957c6f 100644 --- a/minecraft-protocol/src/packets/handshake/client_intention_packet.rs +++ b/minecraft-protocol/src/packets/handshake/client_intention_packet.rs @@ -5,7 +5,7 @@ use tokio::io::BufReader; use crate::{ mc_buf, - packets::{ConnectionProtocol, Packet, PacketTrait}, + packets::{ConnectionProtocol, Packet}, }; #[derive(Hash, Clone, Debug)] @@ -18,7 +18,7 @@ pub struct ClientIntentionPacket { } #[async_trait] -impl PacketTrait for ClientIntentionPacket { +impl ClientIntentionPacket { fn get(self) -> Packet { Packet::ClientIntentionPacket(self) } diff --git a/minecraft-protocol/src/packets/login/clientbound_hello_packet.rs b/minecraft-protocol/src/packets/login/clientbound_hello_packet.rs index 878ca456..c0ec85f3 100644 --- a/minecraft-protocol/src/packets/login/clientbound_hello_packet.rs +++ b/minecraft-protocol/src/packets/login/clientbound_hello_packet.rs @@ -2,10 +2,7 @@ use async_trait::async_trait; use std::hash::Hash; use tokio::io::BufReader; -use crate::{ - mc_buf, - packets::{Packet, PacketTrait}, -}; +use crate::{mc_buf, packets::Packet}; #[derive(Hash, Clone, Debug)] pub struct ClientboundHelloPacket { @@ -14,8 +11,7 @@ pub struct ClientboundHelloPacket { pub nonce: Vec<u8>, } -#[async_trait] -impl PacketTrait for ClientboundHelloPacket { +impl ClientboundHelloPacket { fn get(self) -> Packet { Packet::ClientboundHelloPacket(self) } diff --git a/minecraft-protocol/src/packets/login/mod.rs b/minecraft-protocol/src/packets/login/mod.rs index 47193f92..4d4f13d3 100644 --- a/minecraft-protocol/src/packets/login/mod.rs +++ b/minecraft-protocol/src/packets/login/mod.rs @@ -1,8 +1,62 @@ pub mod clientbound_hello_packet; pub mod serverbound_hello_packet; +use async_trait::async_trait; +use tokio::io::BufReader; + +use crate::connect::PacketFlow; + +use super::ProtocolPacket; + #[derive(Clone, Debug)] -pub enum LoginPacket { +pub enum LoginPacket +where + Self: Sized, +{ ServerboundHelloPacket(serverbound_hello_packet::ServerboundHelloPacket), ClientboundHelloPacket(clientbound_hello_packet::ClientboundHelloPacket), } + +#[async_trait] +impl ProtocolPacket for LoginPacket { + fn id(&self) -> u32 { + match self { + LoginPacket::ServerboundStatusRequestPacket(_packet) => 0x00, + LoginPacket::ClientboundStatusResponsePacket(_packet) => 0x00, + } + } + + fn write(&self, buf: &mut Vec<u8>) { + match self { + LoginPacket::ServerboundStatusRequestPacket(packet) => packet.write(buf), + LoginPacket::ClientboundStatusResponsePacket(packet) => packet.write(buf), + } + } + + /// Read a packet by its id, ConnectionProtocol, and flow + async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( + id: u32, + flow: &PacketFlow, + buf: &mut BufReader<T>, + ) -> Result<LoginPacket, String> + where + Self: Sized, + { + match flow { + PacketFlow::ServerToClient => match id { + 0x00 => Ok( + clientbound_status_response_packet::ClientboundStatusResponsePacket::read(buf) + .await?, + ), + _ => Err(format!("Unknown ServerToClient status packet id: {}", id)), + }, + PacketFlow::ClientToServer => match id { + 0x00 => Ok( + serverbound_status_request_packet::ServerboundStatusRequestPacket::read(buf) + .await?, + ), + _ => Err(format!("Unknown ClientToServer status packet id: {}", id)), + }, + } + } +} diff --git a/minecraft-protocol/src/packets/login/serverbound_hello_packet.rs b/minecraft-protocol/src/packets/login/serverbound_hello_packet.rs index cadf9f7b..345b11f6 100644 --- a/minecraft-protocol/src/packets/login/serverbound_hello_packet.rs +++ b/minecraft-protocol/src/packets/login/serverbound_hello_packet.rs @@ -2,10 +2,7 @@ use async_trait::async_trait; use std::hash::Hash; use tokio::io::BufReader; -use crate::{ - mc_buf, - packets::{Packet, PacketTrait}, -}; +use crate::{mc_buf, packets::Packet}; #[derive(Hash, Clone, Debug)] pub struct ServerboundHelloPacket { @@ -13,7 +10,7 @@ pub struct ServerboundHelloPacket { } #[async_trait] -impl PacketTrait for ServerboundHelloPacket { +impl ServerboundHelloPacket { fn get(self) -> Packet { Packet::ServerboundHelloPacket(self) } diff --git a/minecraft-protocol/src/packets/mod.rs b/minecraft-protocol/src/packets/mod.rs index 46caf02b..d55bd364 100644 --- a/minecraft-protocol/src/packets/mod.rs +++ b/minecraft-protocol/src/packets/mod.rs @@ -26,21 +26,20 @@ pub enum Packet { Status(status::StatusPacket), } +/// An enum of packets for a certain protocol #[async_trait] -pub trait ProtocolPacket { - fn get_inner<P: PacketTrait>(&self) -> &P; - +pub trait ProtocolPacket +where + Self: Sized, +{ fn id(&self) -> u32; /// Read a packet by its id, ConnectionProtocol, and flow - async fn read< - T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send, - P: ProtocolPacket, - >( + async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( id: u32, flow: &PacketFlow, buf: &mut BufReader<T>, - ) -> Result<P, String> + ) -> Result<Self, String> where Self: Sized; @@ -129,14 +128,19 @@ pub trait ProtocolPacket { // } // } -#[async_trait] -pub trait PacketTrait { - /// Return a version of the packet that you can actually use for stuff - fn get<P: ProtocolPacket>(self) -> P; - fn write(&self, buf: &mut Vec<u8>); - async fn read<T: AsyncRead + std::marker::Unpin + std::marker::Send, P: ProtocolPacket>( - buf: &mut BufReader<T>, - ) -> Result<P, String> - where - Self: Sized; -} +// #[async_trait] +// pub trait PacketTrait +// where +// Self: Sized, +// { +// /// Return a version of the packet that you can actually use for stuff +// fn get(self) -> dyn ProtocolPacket; + +// fn write(&self, buf: &mut Vec<u8>); + +// async fn read<T: AsyncRead + std::marker::Unpin + std::marker::Send, P: ProtocolPacket>( +// buf: &mut BufReader<T>, +// ) -> Result<P, String> +// where +// Self: Sized; +// } diff --git a/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs b/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs index 30b1403c..f19ab024 100644 --- a/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs +++ b/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs @@ -4,10 +4,7 @@ use serde::{Deserialize, Deserializer}; use serde_json::Value; use tokio::io::BufReader; -use crate::{ - mc_buf, - packets::{Packet, PacketTrait}, -}; +use crate::{mc_buf, packets::Packet}; use super::StatusPacket; @@ -39,23 +36,24 @@ pub struct ClientboundStatusResponsePacket { pub version: Version, } -#[async_trait] -impl PacketTrait for ClientboundStatusResponsePacket { - fn get(self) -> StatusPacket { +impl ClientboundStatusResponsePacket { + pub fn get(self) -> StatusPacket { StatusPacket::ClientboundStatusResponsePacket(self) } - fn write(&self, _buf: &mut Vec<u8>) {} + pub fn write(&self, _buf: &mut Vec<u8>) {} - async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( + pub async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( buf: &mut BufReader<T>, - ) -> Result<Packet, String> { + ) -> Result<StatusPacket, String> { let status_string = mc_buf::read_utf(buf).await?; let status_json: Value = serde_json::from_str(status_string.as_str()).expect("Server status isn't valid JSON"); - Ok(ClientboundStatusResponsePacket::deserialize(status_json) + let packet = ClientboundStatusResponsePacket::deserialize(status_json) .map_err(|e| e.to_string())? - .get()) + .get(); + + Ok(packet) } } diff --git a/minecraft-protocol/src/packets/status/mod.rs b/minecraft-protocol/src/packets/status/mod.rs index 7e327ca7..aa302e58 100644 --- a/minecraft-protocol/src/packets/status/mod.rs +++ b/minecraft-protocol/src/packets/status/mod.rs @@ -6,10 +6,13 @@ use tokio::io::BufReader; use crate::connect::PacketFlow; -use super::{ConnectionProtocol, PacketTrait, ProtocolPacket}; +use super::{ProtocolPacket}; #[derive(Clone, Debug)] -pub enum StatusPacket { +pub enum StatusPacket +where + Self: Sized, +{ ServerboundStatusRequestPacket( serverbound_status_request_packet::ServerboundStatusRequestPacket, ), @@ -18,22 +21,8 @@ pub enum StatusPacket { ), } -// #[async_trait] -// impl ProtocolPacket for StatusPacket { -impl StatusPacket { - fn get_inner(self) -> impl PacketTrait { - match self { - StatusPacket::ServerboundStatusRequestPacket(packet) => packet, - StatusPacket::ClientboundStatusResponsePacket(packet) => packet, - } - } - // fn get_inner(&self) -> StatusPacket { - // match self { - // StatusPacket::ServerboundStatusRequestPacket(packet) => packet, - // StatusPacket::ClientboundStatusResponsePacket(packet) => packet, - // } - // } - +#[async_trait] +impl ProtocolPacket for StatusPacket { fn id(&self) -> u32 { match self { StatusPacket::ServerboundStatusRequestPacket(_packet) => 0x00, @@ -49,14 +38,11 @@ impl StatusPacket { } /// Read a packet by its id, ConnectionProtocol, and flow - async fn read< - T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send, - P: ProtocolPacket, - >( + async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( id: u32, flow: &PacketFlow, buf: &mut BufReader<T>, - ) -> Result<P, String> + ) -> Result<StatusPacket, String> where Self: Sized, { diff --git a/minecraft-protocol/src/packets/status/serverbound_status_request_packet.rs b/minecraft-protocol/src/packets/status/serverbound_status_request_packet.rs index 066131cc..8b29391a 100644 --- a/minecraft-protocol/src/packets/status/serverbound_status_request_packet.rs +++ b/minecraft-protocol/src/packets/status/serverbound_status_request_packet.rs @@ -2,25 +2,25 @@ use async_trait::async_trait; use std::hash::Hash; use tokio::io::BufReader; -use crate::packets::{Packet, PacketTrait, ProtocolPacket}; +use crate::packets::{Packet, ProtocolPacket}; use super::StatusPacket; #[derive(Hash, Clone, Debug)] pub struct ServerboundStatusRequestPacket {} -#[async_trait] -impl PacketTrait for ServerboundStatusRequestPacket { - fn get(self) -> StatusPacket { +impl ServerboundStatusRequestPacket { + pub fn get(self) -> StatusPacket { StatusPacket::ServerboundStatusRequestPacket(self) } - fn write(&self, _buf: &mut Vec<u8>) { + + pub fn write(&self, _buf: &mut Vec<u8>) { panic!("ServerboundStatusRequestPacket::write not implemented") } - async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( + pub async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( _buf: &mut BufReader<T>, - ) -> Result<Packet, String> { + ) -> Result<StatusPacket, String> { Err("ServerboundStatusRequestPacket::read not implemented".to_string()) } } |
