aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol/src
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2022-04-20 01:33:55 +0000
committerGitHub <noreply@github.com>2022-04-20 01:33:55 +0000
commitd09762f5d38ab1200fb08ca3b1178813b4e47081 (patch)
tree2a5349db5fdbd80ef362dfefa8303ffbd901b916 /azalea-protocol/src
parent747bfa568cb1da8afdeb29005bdd2836963ed613 (diff)
parentbc22faf3b0ba6cc8cc8de6276ff998690ba87bab (diff)
downloadazalea-drasl-d09762f5d38ab1200fb08ca3b1178813b4e47081.tar.xz
Merge pull request #2 from mat-1/state-packets-macro
State packets macro
Diffstat (limited to 'azalea-protocol/src')
-rw-r--r--azalea-protocol/src/mc_buf/read.rs1
-rw-r--r--azalea-protocol/src/mc_buf/write.rs1
-rw-r--r--azalea-protocol/src/packets/game/mod.rs78
-rw-r--r--azalea-protocol/src/packets/handshake/mod.rs51
-rw-r--r--azalea-protocol/src/packets/login/mod.rs83
-rw-r--r--azalea-protocol/src/packets/status/clientbound_status_response_packet.rs2
-rw-r--r--azalea-protocol/src/packets/status/mod.rs71
7 files changed, 40 insertions, 247 deletions
diff --git a/azalea-protocol/src/mc_buf/read.rs b/azalea-protocol/src/mc_buf/read.rs
index 0fa1d099..20b69238 100644
--- a/azalea-protocol/src/mc_buf/read.rs
+++ b/azalea-protocol/src/mc_buf/read.rs
@@ -2,7 +2,6 @@ use async_trait::async_trait;
use azalea_core::{
difficulty::Difficulty, game_type::GameType, resource_location::ResourceLocation,
};
-use num_traits::FromPrimitive;
use tokio::io::{AsyncRead, AsyncReadExt};
use super::MAX_STRING_LENGTH;
diff --git a/azalea-protocol/src/mc_buf/write.rs b/azalea-protocol/src/mc_buf/write.rs
index fd9faeb4..26c0ef35 100644
--- a/azalea-protocol/src/mc_buf/write.rs
+++ b/azalea-protocol/src/mc_buf/write.rs
@@ -3,7 +3,6 @@ use azalea_core::{
difficulty::Difficulty, game_type::GameType, resource_location::ResourceLocation,
};
use byteorder::{BigEndian, WriteBytesExt};
-use num_traits::FromPrimitive;
use std::io::Write;
use super::MAX_STRING_LENGTH;
diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs
index 4efe72fb..a4304d9f 100644
--- a/azalea-protocol/src/packets/game/mod.rs
+++ b/azalea-protocol/src/packets/game/mod.rs
@@ -3,73 +3,15 @@ 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;
-use async_trait::async_trait;
+use packet_macros::declare_state_packets;
-#[derive(Clone, Debug)]
-pub enum GamePacket
-where
- Self: Sized,
-{
- ClientboundLoginPacket(clientbound_login_packet::ClientboundLoginPacket),
- ClientboundUpdateViewDistancePacket(
- clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket,
- ),
- ClientboundCustomPayloadPacket(
- clientbound_custom_payload_packet::ClientboundCustomPayloadPacket,
- ),
- ClientboundChangeDifficultyPacket(
- clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket,
- ),
-}
-
-#[async_trait]
-impl ProtocolPacket for GamePacket {
- fn id(&self) -> u32 {
- match self {
- GamePacket::ClientboundChangeDifficultyPacket(_packet) => 0x0e,
- GamePacket::ClientboundCustomPayloadPacket(_packet) => 0x18,
- GamePacket::ClientboundLoginPacket(_packet) => 0x26,
- GamePacket::ClientboundUpdateViewDistancePacket(_packet) => 0x4a,
- }
- }
-
- fn write(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
- match self {
- GamePacket::ClientboundChangeDifficultyPacket(packet) => packet.write(buf),
- GamePacket::ClientboundCustomPayloadPacket(packet) => packet.write(buf),
- GamePacket::ClientboundLoginPacket(packet) => packet.write(buf),
- GamePacket::ClientboundUpdateViewDistancePacket(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 T,
- ) -> Result<GamePacket, String>
- where
- Self: Sized,
- {
- Ok(match flow {
- PacketFlow::ServerToClient => match id {
- 0x0e => clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket
- ::read(buf)
- .await?,
- 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)),
- _ => panic!("Unknown ServerToClient game packet id: {}", id),
- },
- PacketFlow::ClientToServer => match id {
- // 0x00 => serverbound_hello_packet::ServerboundHelloPacket::read(buf).await?,
- _ => return Err(format!("Unknown ClientToServer game packet id: {}", id)),
- },
- })
+declare_state_packets!(
+ GamePacket,
+ Serverbound => {},
+ Clientbound => {
+ 0x0e: clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket,
+ 0x18: clientbound_custom_payload_packet::ClientboundCustomPayloadPacket,
+ 0x26: clientbound_login_packet::ClientboundLoginPacket,
+ 0x4a: clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket,
}
-}
+);
diff --git a/azalea-protocol/src/packets/handshake/mod.rs b/azalea-protocol/src/packets/handshake/mod.rs
index 17465fca..88d9939b 100644
--- a/azalea-protocol/src/packets/handshake/mod.rs
+++ b/azalea-protocol/src/packets/handshake/mod.rs
@@ -1,48 +1,11 @@
pub mod client_intention_packet;
-use async_trait::async_trait;
+use packet_macros::declare_state_packets;
-use crate::connect::PacketFlow;
-
-use super::ProtocolPacket;
-
-#[derive(Clone, Debug)]
-pub enum HandshakePacket
-where
- Self: Sized,
-{
- ClientIntentionPacket(client_intention_packet::ClientIntentionPacket),
-}
-
-#[async_trait]
-impl ProtocolPacket for HandshakePacket {
- fn id(&self) -> u32 {
- match self {
- HandshakePacket::ClientIntentionPacket(_packet) => 0x00,
- }
- }
-
- fn write(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
- match self {
- HandshakePacket::ClientIntentionPacket(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 T,
- ) -> Result<HandshakePacket, String>
- where
- Self: Sized,
- {
- match flow {
- PacketFlow::ServerToClient => Err("HandshakePacket::read not implemented".to_string()),
- PacketFlow::ClientToServer => match id {
- 0x00 => Ok(client_intention_packet::ClientIntentionPacket::read(buf).await?),
- _ => Err(format!("Unknown ClientToServer status packet id: {}", id)),
- },
- }
+declare_state_packets!(
+ HandshakePacket,
+ Serverbound => {},
+ Clientbound => {
+ 0x00: client_intention_packet::ClientIntentionPacket,
}
-}
+);
diff --git a/azalea-protocol/src/packets/login/mod.rs b/azalea-protocol/src/packets/login/mod.rs
index b1f61746..ef5f15c1 100644
--- a/azalea-protocol/src/packets/login/mod.rs
+++ b/azalea-protocol/src/packets/login/mod.rs
@@ -4,76 +4,17 @@ pub mod clientbound_hello_packet;
pub mod clientbound_login_compression_packet;
pub mod serverbound_hello_packet;
-use super::ProtocolPacket;
-use crate::connect::PacketFlow;
-use async_trait::async_trait;
+use packet_macros::declare_state_packets;
-#[derive(Clone, Debug)]
-pub enum LoginPacket
-where
- Self: Sized,
-{
- ClientboundCustomQueryPacket(clientbound_custom_query_packet::ClientboundCustomQueryPacket),
- ClientboundGameProfilePacket(clientbound_game_profile_packet::ClientboundGameProfilePacket),
- ClientboundHelloPacket(clientbound_hello_packet::ClientboundHelloPacket),
- ClientboundLoginCompressionPacket(
- clientbound_login_compression_packet::ClientboundLoginCompressionPacket,
- ),
- ServerboundHelloPacket(serverbound_hello_packet::ServerboundHelloPacket),
-}
-
-#[async_trait]
-impl ProtocolPacket for LoginPacket {
- fn id(&self) -> u32 {
- match self {
- LoginPacket::ClientboundCustomQueryPacket(_packet) => 0x04,
- LoginPacket::ClientboundGameProfilePacket(_packet) => 0x02,
- LoginPacket::ClientboundHelloPacket(_packet) => 0x01,
- LoginPacket::ClientboundLoginCompressionPacket(_packet) => 0x03,
- LoginPacket::ServerboundHelloPacket(_packet) => 0x00,
- }
- }
-
- 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),
- LoginPacket::ClientboundHelloPacket(packet) => packet.write(buf),
- LoginPacket::ClientboundLoginCompressionPacket(packet) => packet.write(buf),
- LoginPacket::ServerboundHelloPacket(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 T,
- ) -> Result<LoginPacket, String>
- where
- Self: Sized,
- {
- 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 login packet id: {}", id)),
- },
- PacketFlow::ClientToServer => match id {
- 0x00 => serverbound_hello_packet::ServerboundHelloPacket::read(buf).await?,
- _ => return Err(format!("Unknown ClientToServer login packet id: {}", id)),
- },
- })
+declare_state_packets!(
+ LoginPacket,
+ Serverbound => {
+ 0x00: serverbound_hello_packet::ServerboundHelloPacket,
+ },
+ Clientbound => {
+ 0x00: clientbound_hello_packet::ClientboundHelloPacket,
+ 0x02: clientbound_game_profile_packet::ClientboundGameProfilePacket,
+ 0x03: clientbound_login_compression_packet::ClientboundLoginCompressionPacket,
+ 0x04: clientbound_custom_query_packet::ClientboundCustomQueryPacket,
}
-}
+);
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 58f5b701..884cf408 100644
--- a/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs
+++ b/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs
@@ -36,7 +36,7 @@ pub struct ClientboundStatusResponsePacket {
impl ClientboundStatusResponsePacket {
pub fn get(self) -> StatusPacket {
- StatusPacket::ClientboundStatusResponsePacket(Box::new(self))
+ StatusPacket::ClientboundStatusResponsePacket(self)
}
pub fn write(&self, _buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
diff --git a/azalea-protocol/src/packets/status/mod.rs b/azalea-protocol/src/packets/status/mod.rs
index 31fedfb9..56aa577e 100644
--- a/azalea-protocol/src/packets/status/mod.rs
+++ b/azalea-protocol/src/packets/status/mod.rs
@@ -1,65 +1,14 @@
pub mod clientbound_status_response_packet;
pub mod serverbound_status_request_packet;
-use async_trait::async_trait;
-
-use crate::connect::PacketFlow;
-
-use super::ProtocolPacket;
-
-#[derive(Clone, Debug)]
-pub enum StatusPacket
-where
- Self: Sized,
-{
- ServerboundStatusRequestPacket(
- serverbound_status_request_packet::ServerboundStatusRequestPacket,
- ),
- ClientboundStatusResponsePacket(
- Box<clientbound_status_response_packet::ClientboundStatusResponsePacket>,
- ),
-}
-
-#[async_trait]
-impl ProtocolPacket for StatusPacket {
- fn id(&self) -> u32 {
- match self {
- StatusPacket::ServerboundStatusRequestPacket(_packet) => 0x00,
- StatusPacket::ClientboundStatusResponsePacket(_packet) => 0x00,
- }
- }
-
- 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),
- }
- }
-
- /// 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 T,
- ) -> Result<StatusPacket, 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)),
- },
- }
+use packet_macros::declare_state_packets;
+
+declare_state_packets!(
+ StatusPacket,
+ Serverbound => {
+ 0x00: serverbound_status_request_packet::ServerboundStatusRequestPacket,
+ },
+ Clientbound => {
+ 0x00: clientbound_status_response_packet::ClientboundStatusResponsePacket,
}
-}
+);