Added mc_type::packet sub-module
This commit is contained in:
parent
5f715d7688
commit
102cd9a312
|
|
@ -2,18 +2,13 @@
|
||||||
|
|
||||||
mod error;
|
mod error;
|
||||||
mod protocol;
|
mod protocol;
|
||||||
|
mod packet;
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
use async_trait::async_trait;
|
|
||||||
|
|
||||||
use crate::mc_types::protocol::ProtocolWrite;
|
|
||||||
|
|
||||||
pub use crate::mc_types::error::PacketError;
|
pub use crate::mc_types::error::PacketError;
|
||||||
pub use crate::mc_types::protocol::{ProtocolRead, ProtocolConnection};
|
pub use crate::mc_types::protocol::{ProtocolRead, ProtocolConnection};
|
||||||
|
pub use crate::mc_types::packet::{Result, Packet, PacketArray};
|
||||||
pub type Result<T> = std::result::Result<T, Box<dyn Error>>;
|
|
||||||
|
|
||||||
pub const VERSION_NAME: &str = "1.21";
|
pub const VERSION_NAME: &str = "1.21";
|
||||||
pub const VERSION_PROTOCOL: i32 = 767;
|
pub const VERSION_PROTOCOL: i32 = 767;
|
||||||
|
|
@ -26,50 +21,6 @@ pub struct Chat {
|
||||||
pub text: String,
|
pub text: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
pub trait Packet: Sized {
|
|
||||||
fn packet_id() -> i32;
|
|
||||||
fn get(data: &mut Vec<u8>) -> Result<Self>;
|
|
||||||
fn convert(&self) -> Vec<u8>;
|
|
||||||
|
|
||||||
async fn read<T: ProtocolRead + Send>(conn: &mut T) -> Result<Self> {
|
|
||||||
let mut data = conn.read_data().await?;
|
|
||||||
let packet_id = get_var_int(&mut data)?;
|
|
||||||
if packet_id == Self::packet_id() {
|
|
||||||
return Ok(Self::get(&mut data)?)
|
|
||||||
} else {
|
|
||||||
return Err(Box::new(PacketError::InvalidPacketId(packet_id)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn write<T: ProtocolWrite + Send>(&self, conn: &mut T) -> Result<()> {
|
|
||||||
conn.write_data(&mut self.convert()).await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait PacketArray: Sized {
|
|
||||||
fn get(data: &mut Vec<u8>) -> Result<Self>;
|
|
||||||
fn convert(&self) -> Vec<u8>;
|
|
||||||
|
|
||||||
fn get_array(data: &mut Vec<u8>) -> Result<Vec<Self>> {
|
|
||||||
let length = get_var_int(data)?;
|
|
||||||
let mut out_data: Vec<Self> = vec![];
|
|
||||||
for _ in 0..length {
|
|
||||||
out_data.push(Self::get(data)?);
|
|
||||||
}
|
|
||||||
Ok(out_data)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn convert_array(array: &mut Vec<Self>) -> Vec<u8> {
|
|
||||||
let length = array.len() as i32;
|
|
||||||
let mut data: Vec<u8> = convert_var_int(length);
|
|
||||||
for element in array {
|
|
||||||
data.append(&mut Self::convert(element));
|
|
||||||
}
|
|
||||||
data
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_bool(data: &mut Vec<u8>) -> bool {
|
pub fn get_bool(data: &mut Vec<u8>) -> bool {
|
||||||
data.remove(0) != 0
|
data.remove(0) != 0
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,55 @@
|
||||||
|
// Yeahbut September 2025
|
||||||
|
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
|
||||||
|
use crate::mc_types::protocol::ProtocolWrite;
|
||||||
|
|
||||||
|
use crate::mc_types::*;
|
||||||
|
|
||||||
|
pub type Result<T> = std::result::Result<T, Box<dyn Error>>;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait Packet: Sized {
|
||||||
|
fn packet_id() -> i32;
|
||||||
|
fn get(data: &mut Vec<u8>) -> Result<Self>;
|
||||||
|
fn convert(&self) -> Vec<u8>;
|
||||||
|
|
||||||
|
async fn read<T: ProtocolRead + Send>(conn: &mut T) -> Result<Self> {
|
||||||
|
let mut data = conn.read_data().await?;
|
||||||
|
let packet_id = get_var_int(&mut data)?;
|
||||||
|
if packet_id == Self::packet_id() {
|
||||||
|
return Ok(Self::get(&mut data)?)
|
||||||
|
} else {
|
||||||
|
return Err(Box::new(PacketError::InvalidPacketId(packet_id)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn write<T: ProtocolWrite + Send>(&self, conn: &mut T) -> Result<()> {
|
||||||
|
conn.write_data(&mut self.convert()).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait PacketArray: Sized {
|
||||||
|
fn get(data: &mut Vec<u8>) -> Result<Self>;
|
||||||
|
fn convert(&self) -> Vec<u8>;
|
||||||
|
|
||||||
|
fn get_array(data: &mut Vec<u8>) -> Result<Vec<Self>> {
|
||||||
|
let length = get_var_int(data)?;
|
||||||
|
let mut out_data: Vec<Self> = vec![];
|
||||||
|
for _ in 0..length {
|
||||||
|
out_data.push(Self::get(data)?);
|
||||||
|
}
|
||||||
|
Ok(out_data)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn convert_array(array: &mut Vec<Self>) -> Vec<u8> {
|
||||||
|
let length = array.len() as i32;
|
||||||
|
let mut data: Vec<u8> = convert_var_int(length);
|
||||||
|
for element in array {
|
||||||
|
data.append(&mut Self::convert(element));
|
||||||
|
}
|
||||||
|
data
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue