1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

add declare_state_packets to the other states

This commit is contained in:
mat 2022-04-19 19:17:36 -05:00
parent 9633874d23
commit cbdab27cb5
6 changed files with 38 additions and 189 deletions

View file

@ -222,13 +222,13 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream {
let mut clientbound_read_match_contents = quote!();
for PacketIdPair { id, module, name } in input.serverbound.packets {
enum_contents.extend(quote! {
#name(#module::#name)
#name(#module::#name),
});
id_match_contents.extend(quote! {
#state_name::#name(_packet) => #id
#state_name::#name(_packet) => #id,
});
write_match_contents.extend(quote! {
#state_name::#name(packet) => packet.write(buf)
#state_name::#name(packet) => packet.write(buf),
});
serverbound_read_match_contents.extend(quote! {
#id => #module::#name::read(buf).await?,
@ -258,8 +258,8 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream {
#enum_contents
}
#[async_trait]
impl ProtocolPacket for GamePacket {
#[async_trait::async_trait]
impl crate::packets::ProtocolPacket for #state_name {
fn id(&self) -> u32 {
match self {
#id_match_contents
@ -275,18 +275,18 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream {
/// 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,
flow: &crate::connect::PacketFlow,
buf: &mut T,
) -> Result<GamePacket, String>
) -> Result<#state_name, String>
where
Self: Sized,
{
Ok(match flow {
PacketFlow::ServerToClient => match id {
crate::connect::PacketFlow::ServerToClient => match id {
#serverbound_read_match_contents
_ => panic!("Unknown ServerToClient {} packet id: {}", #state_name_litstr, id),
},
PacketFlow::ClientToServer => match id {
crate::connect::PacketFlow::ClientToServer => match id {
#clientbound_read_match_contents
_ => return Err(format!("Unknown ClientToServer {} packet id: {}", #state_name_litstr, id)),
},

View file

@ -3,14 +3,10 @@ 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;
declare_state_packets!(
GamePacket,
// no serverbound packets implemented yet
Serverbound => {},
Clientbound => {
0x0e: clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket,

View file

@ -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,
}
declare_state_packets!(
HandshakePacket,
Serverbound => {},
Clientbound => {
0x00: client_intention_packet::ClientIntentionPacket,
}
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)),
},
}
}
}
);

View file

@ -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,
}
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,
}
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)),
},
})
}
}
);

View file

@ -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> {

View file

@ -1,65 +1,14 @@
pub mod clientbound_status_response_packet;
pub mod serverbound_status_request_packet;
use async_trait::async_trait;
use packet_macros::declare_state_packets;
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,
}
declare_state_packets!(
StatusPacket,
Serverbound => {
0x00: serverbound_status_request_packet::ServerboundStatusRequestPacket,
},
Clientbound => {
0x00: clientbound_status_response_packet::ClientboundStatusResponsePacket,
}
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)),
},
}
}
}
);