mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 23:44:38 +00:00
split mstuff
This commit is contained in:
parent
ecee5e96ca
commit
966471f740
8 changed files with 51 additions and 98 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -303,6 +303,7 @@ name = "minecraft-protocol"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-recursion",
|
"async-recursion",
|
||||||
|
"async-trait",
|
||||||
"byteorder",
|
"byteorder",
|
||||||
"bytes",
|
"bytes",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
|
|
|
@ -13,3 +13,4 @@ thiserror = "^1.0.30"
|
||||||
tokio = {version = "^1.14.0", features = ["io-util", "net", "macros"]}
|
tokio = {version = "^1.14.0", features = ["io-util", "net", "macros"]}
|
||||||
tokio-util = "^0.6.9"
|
tokio-util = "^0.6.9"
|
||||||
trust-dns-resolver = "^0.20.3"
|
trust-dns-resolver = "^0.20.3"
|
||||||
|
async-trait = "0.1.51"
|
||||||
|
|
|
@ -72,7 +72,7 @@ impl Connection {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write a packet to the server
|
/// Write a packet to the server
|
||||||
pub async fn send_packet(&mut self, packet: &impl Packet) {
|
pub async fn send_packet(&mut self, packet: Packet<'_>) {
|
||||||
// TODO: implement compression
|
// TODO: implement compression
|
||||||
|
|
||||||
// packet structure:
|
// packet structure:
|
||||||
|
|
|
@ -96,19 +96,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_utf_with_len(buf: &mut Vec<u8>, string: &String, len: usize) {
|
pub async fn read_utf_with_len<T: AsyncRead + std::marker::Unpin>(
|
||||||
if string.len() > len {
|
|
||||||
panic!(
|
|
||||||
"String too big (was {} bytes encoded, max {})",
|
|
||||||
string.len(),
|
|
||||||
len
|
|
||||||
);
|
|
||||||
}
|
|
||||||
write_varint(buf, string.len() as i32);
|
|
||||||
write_bytes(buf, string.as_bytes());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn read_utf<T: AsyncRead + std::marker::Unpin>(
|
|
||||||
buf: &mut BufReader<T>,
|
buf: &mut BufReader<T>,
|
||||||
max_length: u32,
|
max_length: u32,
|
||||||
) -> Result<String, String> {
|
) -> Result<String, String> {
|
||||||
|
@ -146,8 +134,24 @@ pub async fn read_utf<T: AsyncRead + std::marker::Unpin>(
|
||||||
Ok(string)
|
Ok(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn write_utf_with_len(buf: &mut Vec<u8>, string: &String, len: usize) {
|
||||||
|
if string.len() > len {
|
||||||
|
panic!(
|
||||||
|
"String too big (was {} bytes encoded, max {})",
|
||||||
|
string.len(),
|
||||||
|
len
|
||||||
|
);
|
||||||
|
}
|
||||||
|
write_varint(buf, string.len() as i32);
|
||||||
|
write_bytes(buf, string.as_bytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn read_utf<T: AsyncRead + std::marker::Unpin>(buf: &mut T) -> Result<String, String> {
|
||||||
|
read_utf_with_len(buf, MAX_STRING_LENGTH.into()).await
|
||||||
|
}
|
||||||
|
|
||||||
pub fn write_utf(buf: &mut Vec<u8>, string: &String) {
|
pub fn write_utf(buf: &mut Vec<u8>, string: &String) {
|
||||||
write_utf_with_len(buf, string, MAX_STRING_LENGTH as usize);
|
write_utf_with_len(buf, string, MAX_STRING_LENGTH.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_short(buf: &mut Vec<u8>, n: u16) {
|
pub fn write_short(buf: &mut Vec<u8>, n: u16) {
|
||||||
|
|
|
@ -1,31 +0,0 @@
|
||||||
use std::hash::Hash;
|
|
||||||
|
|
||||||
use crate::mc_buf;
|
|
||||||
|
|
||||||
use super::{ConnectionProtocol, Packet};
|
|
||||||
|
|
||||||
#[derive(Hash)]
|
|
||||||
pub struct ClientIntentionPacket<'a> {
|
|
||||||
pub protocol_version: u32,
|
|
||||||
pub hostname: &'a String,
|
|
||||||
pub port: u16,
|
|
||||||
/// 1 for status, 2 for login
|
|
||||||
pub intention: ConnectionProtocol,
|
|
||||||
}
|
|
||||||
|
|
||||||
// implement "Packet" for "ClientIntentionPacket"
|
|
||||||
impl<'a> Packet for ClientIntentionPacket<'a> {
|
|
||||||
fn get_id(&self) -> u32 {
|
|
||||||
0x00
|
|
||||||
}
|
|
||||||
|
|
||||||
// implement "from_reader" for "ClientIntentionPacket"
|
|
||||||
fn write(&self, buf: &mut Vec<u8>) {
|
|
||||||
mc_buf::write_varint(buf, self.protocol_version as i32);
|
|
||||||
mc_buf::write_utf(buf, &self.hostname);
|
|
||||||
mc_buf::write_short(buf, self.port);
|
|
||||||
mc_buf::write_varint(buf, self.intention.clone() as i32);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse<T: tokio::io::AsyncRead + std::marker::Unpin>(&self, buf: T) -> () {}
|
|
||||||
}
|
|
|
@ -1,23 +0,0 @@
|
||||||
use std::hash::Hash;
|
|
||||||
|
|
||||||
use super::Packet;
|
|
||||||
|
|
||||||
#[derive(Hash)]
|
|
||||||
pub struct ServerboundStatusRequestPacket {
|
|
||||||
// status: ServerStatus,
|
|
||||||
status: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
// implement "Packet" for "ClientIntentionPacket"
|
|
||||||
impl Packet for ServerboundStatusRequestPacket {
|
|
||||||
fn get_id(&self) -> u32 {
|
|
||||||
0x00
|
|
||||||
}
|
|
||||||
|
|
||||||
// implement "from_reader" for "ClientIntentionPacket"
|
|
||||||
fn write(&self, _buf: &mut Vec<u8>) {}
|
|
||||||
fn parse<T: tokio::io::AsyncRead + std::marker::Unpin>(&self, buf: T) -> () {
|
|
||||||
mc_buf::read_utf;
|
|
||||||
// this.status = GsonHelper.fromJson(GSON, friendlyByteBuf.readUtf(32767), ServerStatus.class);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,21 +1,39 @@
|
||||||
mod client_intention_packet;
|
mod game;
|
||||||
pub use client_intention_packet::ClientIntentionPacket;
|
mod handshake;
|
||||||
mod serverbound_status_request_packet;
|
mod login;
|
||||||
pub use serverbound_status_request_packet::ServerboundStatusRequestPacket;
|
mod status;
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
use tokio::io::AsyncRead;
|
use tokio::io::AsyncRead;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub enum ConnectionProtocol {
|
pub enum ConnectionProtocol {
|
||||||
Handshaking = -1,
|
Handshake = -1,
|
||||||
Play = 0,
|
Game = 0,
|
||||||
Status = 1,
|
Status = 1,
|
||||||
Login = 2,
|
Login = 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Packet {
|
pub enum Packet<'a> {
|
||||||
/// Get the id of the packet, this is always a byte.
|
// game
|
||||||
fn get_id(&self) -> u32;
|
// handshake
|
||||||
|
ClientIntentionPacket(&'a handshake::client_intention_packet::ClientIntentionPacket<'a>),
|
||||||
fn write(&self, buf: &mut Vec<u8>) -> ();
|
// login
|
||||||
fn parse<T: AsyncRead + std::marker::Unpin>(&self, buf: T) -> ();
|
// status
|
||||||
|
ServerboundStatusRequestPacket(
|
||||||
|
&'a status::serverbound_status_request_packet::ServerboundStatusRequestPacket,
|
||||||
|
),
|
||||||
|
ClientboundStatusRequestPacket(
|
||||||
|
&'a status::clientbound_status_response_packet::ClientboundStatusRequestPacket,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait PacketTrait {
|
||||||
|
/// Return a version of the packet that you can actually use for stuff
|
||||||
|
fn get(&self) -> Packet;
|
||||||
|
fn write(&self, buf: &mut Vec<u8>) -> ();
|
||||||
|
fn parse<T: AsyncRead + std::marker::Unpin>(
|
||||||
|
buf: &mut T,
|
||||||
|
// is using a static lifetime here a good idea? idk
|
||||||
|
) -> Result<Packet<'_>, String>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
use std::hash::Hash;
|
|
||||||
|
|
||||||
use super::Packet;
|
|
||||||
|
|
||||||
#[derive(Hash)]
|
|
||||||
pub struct ServerboundStatusRequestPacket {}
|
|
||||||
|
|
||||||
// implement "Packet" for "ClientIntentionPacket"
|
|
||||||
impl Packet for ServerboundStatusRequestPacket {
|
|
||||||
fn get_id(&self) -> u32 {
|
|
||||||
0x00
|
|
||||||
}
|
|
||||||
|
|
||||||
// implement "from_reader" for "ClientIntentionPacket"
|
|
||||||
fn write(&self, _buf: &mut Vec<u8>) {}
|
|
||||||
fn parse<T: tokio::io::AsyncRead + std::marker::Unpin>(&self, buf: T) -> () {}
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue