Fixed errors from macro conversion

This commit is contained in:
Kyler Olsen 2025-09-15 22:44:23 -06:00
parent fb2c45d163
commit 6003f6b5ed
12 changed files with 88 additions and 79 deletions

View File

@ -50,7 +50,7 @@ pub fn derive_packet(input: TokenStream) -> TokenStream {
let read_fields = fields.named.iter().map(|f| {
let fname = &f.ident;
let fty = &f.ty;
quote! { #fname: <#fty as McType>::get(data)? }
quote! { #fname: <#fty as mc_types::McType>::get(data)? }
});
let write_fields = fields.named.iter().map(|f| {
@ -63,9 +63,9 @@ pub fn derive_packet(input: TokenStream) -> TokenStream {
let expanded = quote! {
impl Packet for #name {
fn packet_id() -> i32 { #packet_id }
fn packet_id() -> mc_types::VarInt { mc_types::VarInt(#packet_id) }
fn get(data: &mut Vec<u8>) -> Result<Self> {
fn get(data: &mut Vec<u8>) -> mc_types::Result<Self> {
Ok(Self {
#(#read_fields),*
})
@ -73,7 +73,7 @@ pub fn derive_packet(input: TokenStream) -> TokenStream {
fn convert(&self) -> Vec<u8> {
let mut out = Vec::new();
out.extend(convert_var_int(Self::packet_id()));
out.extend(mc_types::convert_var_int(Self::packet_id()));
#(#write_fields)*
out
}
@ -84,13 +84,13 @@ pub fn derive_packet(input: TokenStream) -> TokenStream {
}
impl #enum_name {
pub async fn read<T: ProtocolRead + Send>(conn: &mut T) -> Result<Self> {
pub async fn read<T: mc_types::ProtocolRead + Send>(conn: &mut T) -> mc_types::Result<Self> {
let mut data = conn.read_data().await?;
let packet_id = get_var_int(&mut data)?;
let packet_id = mc_types::get_var_int(&mut data)?;
if packet_id == #name::packet_id() {
Ok(Self::#enum_variant_ident(#name::get(&mut data)?))
} else {
Err(Box::new(PacketError::InvalidPacketId(packet_id)))
Err(Box::new(mc_types::PacketError::InvalidPacketId(packet_id)))
}
}
}
@ -119,7 +119,7 @@ pub fn derive_mctype_struct(input: TokenStream) -> TokenStream {
let read_fields = fields.named.iter().map(|f| {
let fname = &f.ident;
let fty = &f.ty;
quote! { #fname: <#fty as McType>::get(data)? }
quote! { #fname: <#fty as mc_types::McType>::get(data)? }
});
let write_fields = fields.named.iter().map(|f| {
@ -128,8 +128,8 @@ pub fn derive_mctype_struct(input: TokenStream) -> TokenStream {
});
let expanded = quote! {
impl McType for #name {
fn get(data: &mut Vec<u8>) -> Result<Self> {
impl mc_types::McType for #name {
fn get(data: &mut Vec<u8>) -> mc_types::Result<Self> {
Ok(Self {
#(#read_fields),*
})

View File

@ -3,8 +3,8 @@
pub mod serverbound {
use mc_derive::Packet;
use crate::mc_types::{self, Result, Packet};
use purple_cello_mc_derive::Packet;
use crate::mc_types::{self, Packet, McType};
#[derive(Packet)]
#[packet(id = 0, name = "Handshake")]

View File

@ -1,6 +1,7 @@
// Yeahbut September 2025
use crate::mc_types::{self, Result, Packet};
use purple_cello_mc_derive::{Packet, McType};
use crate::mc_types::{self, Packet, McType};
#[derive(Packet)]
#[packet(id = 0, name = "Disconnect")]

View File

@ -1,6 +1,7 @@
// Yeahbut September 2025
use crate::mc_types::{self, Result, Packet};
use purple_cello_mc_derive::Packet;
use crate::mc_types::{self, Packet, McType};
#[derive(Packet)]
#[packet(id = 0, name = "LoginStart")]

View File

@ -11,6 +11,7 @@ use serde::{Serialize, Deserialize};
pub use crate::mc_types::error::PacketError;
pub use crate::mc_types::protocol::{ProtocolRead, ProtocolConnection};
pub use crate::mc_types::packet::{Result, Packet};
pub use crate::mc_types::helpers::{convert_var_int, get_var_int};
use crate::mc_types::helpers::*;
pub const VERSION_NAME: &str = "1.21";
@ -106,23 +107,23 @@ impl UUID {
}
impl McType for VarInt {
fn get(data: &mut Vec<u8>) -> Result<Self> { Ok(get_var_int(data)) }
fn get(data: &mut Vec<u8>) -> Result<Self> { get_var_int(data) }
fn convert(&self) -> Vec<u8> { convert_var_int(*self) }
}
impl McType for VarLong {
fn get(data: &mut Vec<u8>) -> Result<Self> { Ok(get_var_long(data)) }
fn get(data: &mut Vec<u8>) -> Result<Self> { get_var_long(data) }
fn convert(&self) -> Vec<u8> { convert_var_long(*self) }
}
impl McType for String {
fn get(data: &mut Vec<u8>) -> Result<Self> { Ok(get_string(data)) }
fn convert(&self) -> Vec<u8> { convert_string(*self) }
fn get(data: &mut Vec<u8>) -> Result<Self> { get_string(data) }
fn convert(&self) -> Vec<u8> { convert_string(self) }
}
impl<T: McType> McType for Vec<T> {
fn get(data: &mut Vec<u8>) -> Result<Self> {
let length = get_var_int(data)?;
let length = get_var_int(data)?.0;
let mut out = Vec::with_capacity(length as usize);
for _ in 0..length {
out.push(T::get(data)?);
@ -131,7 +132,7 @@ impl<T: McType> McType for Vec<T> {
}
fn convert(&self) -> Vec<u8> {
let mut data = convert_var_int(self.len() as i32);
let mut data = convert_var_int(VarInt(self.len() as i32));
for el in self {
data.extend(el.convert());
}

View File

@ -3,11 +3,13 @@
use std::fmt;
use std::error::Error;
use crate::mc_types::VarInt;
#[derive(Debug)]
pub enum PacketError {
ValueTooLarge,
RanOutOfBytes,
InvalidPacketId(i32),
InvalidPacketId(VarInt),
InvalidUUIDString,
EncryptionError,
}
@ -20,7 +22,7 @@ impl fmt::Display for PacketError {
PacketError::RanOutOfBytes =>
write!(f, "Ran out of bytes while reading VarInt"),
PacketError::InvalidPacketId(packet_id) =>
write!(f, "Invalid packet id: {}", packet_id),
write!(f, "Invalid packet id: {}", packet_id.0),
PacketError::InvalidUUIDString =>
write!(f, "Invalid UUID format"),
PacketError::EncryptionError =>

View File

@ -107,45 +107,47 @@ pub fn convert_f64(value: f64) -> Vec<u8> {
convert_u64(value as u64)
}
pub fn get_uuid(data: &mut Vec<u8>) -> u128 {
((data.remove(0) as u128) << 120) |
((data.remove(0) as u128) << 112) |
((data.remove(0) as u128) << 104) |
((data.remove(0) as u128) << 96) |
((data.remove(0) as u128) << 88) |
((data.remove(0) as u128) << 80) |
((data.remove(0) as u128) << 72) |
((data.remove(0) as u128) << 64) |
((data.remove(0) as u128) << 56) |
((data.remove(0) as u128) << 48) |
((data.remove(0) as u128) << 40) |
((data.remove(0) as u128) << 32) |
((data.remove(0) as u128) << 24) |
((data.remove(0) as u128) << 16) |
((data.remove(0) as u128) << 8) |
(data.remove(0) as u128)
pub fn get_uuid(data: &mut Vec<u8>) -> UUID {
UUID(
((data.remove(0) as u128) << 120) |
((data.remove(0) as u128) << 112) |
((data.remove(0) as u128) << 104) |
((data.remove(0) as u128) << 96) |
((data.remove(0) as u128) << 88) |
((data.remove(0) as u128) << 80) |
((data.remove(0) as u128) << 72) |
((data.remove(0) as u128) << 64) |
((data.remove(0) as u128) << 56) |
((data.remove(0) as u128) << 48) |
((data.remove(0) as u128) << 40) |
((data.remove(0) as u128) << 32) |
((data.remove(0) as u128) << 24) |
((data.remove(0) as u128) << 16) |
((data.remove(0) as u128) << 8) |
(data.remove(0) as u128)
)
}
pub fn convert_uuid(value: u128) -> Vec<u8> {
pub fn convert_uuid(value: UUID) -> Vec<u8> {
vec![
((value & 0xFF000000000000000000000000000000) >> 120) as u8,
((value & 0xFF0000000000000000000000000000) >> 112) as u8,
((value & 0xFF00000000000000000000000000) >> 104) as u8,
((value & 0xFF000000000000000000000000) >> 96) as u8,
((value & 0xFF0000000000000000000000) >> 88) as u8,
((value & 0xFF00000000000000000000) >> 80) as u8,
((value & 0xFF000000000000000000) >> 72) as u8,
((value & 0xFF0000000000000000) >> 64) as u8,
((value & 0xFF00000000000000) >> 56) as u8,
((value & 0xFF000000000000) >> 48) as u8,
((value & 0xFF0000000000) >> 40) as u8,
((value & 0xFF00000000) >> 32) as u8,
((value & 0xFF000000) >> 24) as u8,
((value & 0xFF0000) >> 16) as u8,
((value & 0xFF00) >> 8) as u8,
(value & 0xFF) as u8,
((value.0 & 0xFF000000000000000000000000000000) >> 120) as u8,
((value.0 & 0xFF0000000000000000000000000000) >> 112) as u8,
((value.0 & 0xFF00000000000000000000000000) >> 104) as u8,
((value.0 & 0xFF000000000000000000000000) >> 96) as u8,
((value.0 & 0xFF0000000000000000000000) >> 88) as u8,
((value.0 & 0xFF00000000000000000000) >> 80) as u8,
((value.0 & 0xFF000000000000000000) >> 72) as u8,
((value.0 & 0xFF0000000000000000) >> 64) as u8,
((value.0 & 0xFF00000000000000) >> 56) as u8,
((value.0 & 0xFF000000000000) >> 48) as u8,
((value.0 & 0xFF0000000000) >> 40) as u8,
((value.0 & 0xFF00000000) >> 32) as u8,
((value.0 & 0xFF000000) >> 24) as u8,
((value.0 & 0xFF0000) >> 16) as u8,
((value.0 & 0xFF00) >> 8) as u8,
( value.0 & 0xFF) as u8,
]
}
pub fn uuid_u128_to_string(uuid: u128) -> String {
pub fn uuid_u128_to_string(uuid: UUID) -> String {
let uuid_bytes = convert_uuid(uuid);
format!(
"{:08x}-{:04x}-{:04x}-{:04x}-{:012x}",
@ -170,26 +172,26 @@ pub fn uuid_u128_to_string(uuid: u128) -> String {
]),
)
}
pub fn uuid_string_to_u128(uuid: &str) -> Result<u128> {
pub fn uuid_string_to_u128(uuid: &str) -> Result<UUID> {
let cleaned_uuid = uuid.replace("-", "");
if cleaned_uuid.len() != 32 {
return Err(Box::new(PacketError::InvalidUUIDString));
}
Ok(u128::from_str_radix(&cleaned_uuid, 16)?)
Ok(UUID(u128::from_str_radix(&cleaned_uuid, 16)?))
}
pub fn get_var_int(data: &mut Vec<u8>) -> Result<i32> {
Ok(get_var(data, 32)? as i32)
pub fn get_var_int(data: &mut Vec<u8>) -> Result<VarInt> {
Ok(VarInt(get_var(data, 32)? as i32))
}
pub fn convert_var_int(value: i32) -> Vec<u8> {
convert_var(value as i64)
pub fn convert_var_int(value: VarInt) -> Vec<u8> {
convert_var(value.0 as i64)
}
pub fn get_var_long(data: &mut Vec<u8>) -> Result<i64> {
get_var(data, 64)
pub fn get_var_long(data: &mut Vec<u8>) -> Result<VarLong> {
Ok(VarLong(get_var(data, 32)?))
}
pub fn convert_var_long(value: i64) -> Vec<u8> {
convert_var(value)
pub fn convert_var_long(value: VarLong) -> Vec<u8> {
convert_var(value.0)
}
fn get_var(data: &mut Vec<u8>, size: u8) -> Result<i64> {
@ -231,13 +233,13 @@ fn convert_var(mut value: i64) -> Vec<u8> {
}
pub fn get_string(data: &mut Vec<u8>) -> Result<String> {
let length = get_var_int(data)? as usize;
let length = get_var_int(data)?.0 as usize;
let buffer = data[..length].to_vec();
for _ in 0..length { data.remove(0); }
Ok(String::from_utf8_lossy(&buffer).to_string())
}
pub fn convert_string(s: &str) -> Vec<u8> {
let length = s.len() as i32;
let length = VarInt(s.len() as i32);
let mut data = convert_var_int(length);
data.append(&mut s.as_bytes().to_vec());
data

View File

@ -12,7 +12,7 @@ pub type Result<T> = std::result::Result<T, Box<dyn Error>>;
#[async_trait]
pub trait Packet: Sized {
fn packet_id() -> i32;
fn packet_id() -> VarInt;
fn get(data: &mut Vec<u8>) -> Result<Self>;
fn convert(&self) -> Vec<u8>;

View File

@ -217,7 +217,7 @@ impl<'a> ProtocolRead for ProtocolConnection<'a> {
#[async_trait]
impl<'a> ProtocolWrite for ProtocolConnection<'a> {
async fn write_data(&mut self, data: &mut Vec<u8>) -> Result<()> {
let mut out_data = convert_var_int(data.len() as i32);
let mut out_data = convert_var_int(VarInt(data.len() as i32));
out_data.append(data);
match &mut self.aes_cipher {
Some(aes_cipher) => {
@ -256,7 +256,7 @@ unsafe impl<'a> Send for WriteHaftProtocolConnection<'a> {}
#[async_trait]
impl<'a> ProtocolWrite for WriteHaftProtocolConnection<'a> {
async fn write_data(&mut self, data: &mut Vec<u8>) -> Result<()> {
let mut out_data = convert_var_int(data.len() as i32);
let mut out_data = convert_var_int(VarInt(data.len() as i32));
out_data.append(data);
match &mut self.aes_cipher {
Some(aes_cipher) => {
@ -342,7 +342,7 @@ async fn read_var_int_stream(stream: &mut OwnedReadHalf) -> Result<i32> {
}
}
let varint = get_var_int(&mut data)?;
let varint = get_var_int(&mut data)?.0;
Ok(varint)
}
@ -363,7 +363,7 @@ async fn read_var_int_stream_encrypted(
}
}
let varint = get_var_int(&mut data)?;
let varint = get_var_int(&mut data)?.0;
Ok(varint)
}

View File

@ -21,7 +21,7 @@ pub struct PlayPacket {
impl Packet for PlayPacket {
fn packet_id() -> i32 {0}
fn packet_id() -> mc_types::VarInt {mc_types::VarInt(0)}
fn get(data: &mut Vec<u8>) -> Result<Self> {
Ok(Self {

View File

@ -4,7 +4,8 @@ use serde::{Serialize, Deserialize, Serializer, Deserializer};
use serde::de::{self, Visitor, MapAccess};
use std::fmt;
use crate::mc_types::{self, Result, Packet, PacketError};
use purple_cello_mc_derive::Packet;
use crate::mc_types::{self, Packet, McType};
#[derive(Serialize, Deserialize)]
pub struct StatusVersion {
@ -106,13 +107,13 @@ pub struct Status {
impl Status {
pub fn from_json(data: StatusResponseData) -> Result<Self> {
pub fn from_json(data: StatusResponseData) -> mc_types::Result<Self> {
Ok(Self {
response: serde_json::to_string(&data)?
})
}
pub fn get_json(&self) -> Result<StatusResponseData> {
pub fn get_json(&self) -> mc_types::Result<StatusResponseData> {
Ok(serde_json::from_str(&self.response)?)
}

View File

@ -1,6 +1,7 @@
// Yeahbut September 2025
use crate::mc_types::{self, Result, Packet};
use purple_cello_mc_derive::Packet;
use crate::mc_types::{self, Packet, McType};
#[derive(Packet)]
#[packet(id = 0, name = "Status")]