Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various improvements to the protocol crate [WIP-ish] #651

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "stevenarella"
version = "0.0.1"
authors = [ "Thinkofdeath <[email protected]>", "iceiix <[email protected]>" ]
edition = "2021"
rust_version = "1.57"
rust_version = "1.58"
resolver = "2"
description = "Multi-protocol multi-platform Minecraft-compatible client"
repository = "https://github.com/iceiix/stevenarella"
Expand Down
8 changes: 7 additions & 1 deletion protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ version = "0.0.1"
authors = [ "Thinkofdeath <[email protected]>", "iceiix <[email protected]>" ]
edition = "2021"

[features]
# Enables support for authenticating with Mojang servers.
auth = ["reqwest"]

default = ["auth"]

[dependencies]
serde = "1.0.132"
serde_json = "1.0.73"
Expand All @@ -26,4 +32,4 @@ path = "../std_or_web"
version = "0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
reqwest = { version = "0.11.8", features = [ "blocking" ]}
reqwest = { version = "0.11.8", features = [ "blocking" ], optional = true }
8 changes: 4 additions & 4 deletions protocol/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use std::fmt;
use std::mem;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Component {
Text(TextComponent),
}
Expand Down Expand Up @@ -115,7 +115,7 @@ impl Default for Component {
}
}

#[derive(Debug, Default, Clone)]
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Modifier {
pub extra: Option<Vec<Component>>,
pub bold: Option<bool>,
Expand Down Expand Up @@ -159,7 +159,7 @@ impl Modifier {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TextComponent {
pub text: String,
pub modifier: Modifier,
Expand Down Expand Up @@ -199,7 +199,7 @@ impl fmt::Display for TextComponent {
}
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Color {
Black,
DarkBlue,
Expand Down
2 changes: 1 addition & 1 deletion protocol/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::protocol::{self, Serializable};
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use std::io;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct Stack {
pub id: isize,
pub count: isize,
Expand Down
4 changes: 2 additions & 2 deletions protocol/src/nbt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use super::protocol;
use super::protocol::Serializable;
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum Tag {
End,
Byte(i8),
Expand All @@ -37,7 +37,7 @@ pub enum Tag {
LongArray(Vec<i64>),
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct NamedTag(pub String, pub Tag);

impl Tag {
Expand Down
14 changes: 7 additions & 7 deletions protocol/src/protocol/forge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::io;

use super::{Error, LenPrefixed, Serializable, VarInt};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Phase {
// Client handshake states (written)
Start,
Expand Down Expand Up @@ -44,7 +44,7 @@ impl Serializable for Phase {
}
}

#[derive(Clone, Debug, Default)]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ForgeMod {
pub modid: String,
pub version: String,
Expand All @@ -64,7 +64,7 @@ impl Serializable for ForgeMod {
}
}

#[derive(Debug)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModIdMapping {
pub name: String,
pub id: VarInt,
Expand All @@ -87,7 +87,7 @@ impl Serializable for ModIdMapping {
pub static BLOCK_NAMESPACE: &str = "\u{1}";
pub static ITEM_NAMESPACE: &str = "\u{2}";

#[derive(Debug)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FmlHs {
ServerHello {
fml_protocol_version: i8,
Expand Down Expand Up @@ -196,7 +196,7 @@ pub mod fml2 {
// https://wiki.vg/Minecraft_Forge_Handshake#FML2_protocol_.281.13_-_Current.29
use super::*;

#[derive(Clone, Default, Debug)]
#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub struct Channel {
pub name: String,
pub version: String,
Expand All @@ -216,7 +216,7 @@ pub mod fml2 {
}
}

#[derive(Clone, Default, Debug)]
#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub struct Registry {
pub name: String,
pub marker: String,
Expand All @@ -236,7 +236,7 @@ pub mod fml2 {
}
}

#[derive(Debug)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FmlHandshake {
ModList {
mod_names: LenPrefixed<VarInt, String>,
Expand Down
66 changes: 51 additions & 15 deletions protocol/src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ pub fn current_protocol_version() -> i32 {
CURRENT_PROTOCOL_VERSION.load(Ordering::Relaxed)
}

pub fn set_current_protocol_version(version: i32) {
CURRENT_PROTOCOL_VERSION.store(version, Ordering::Relaxed);
}

pub fn enable_network_debug() {
NETWORK_DEBUG.store(true, Ordering::Relaxed);
}
Expand All @@ -75,17 +79,43 @@ macro_rules! state_packets {
use crate::protocol::*;
use std::io;

#[derive(Debug)]
#[derive(Debug, Clone, PartialEq)]
pub enum Packet {
$(
$(
$(
$name($state::$dir::$name),
$name(Box<$state::$dir::$name>),
)*
)+
)+
}

impl PacketType for Packet {
fn packet_id(&self, version: i32) -> i32 {
match self {
$(
$(
$(
Self::$name(p) => p.packet_id(version),
)*
)+
)+
}
}

fn write<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
match self {
$(
$(
$(
Self::$name(p) => p.write(buf),
)*
)+
)+
}
}
}

$(
pub mod $state {

Expand All @@ -107,7 +137,7 @@ macro_rules! state_packets {
}

$(
#[derive(Default, Debug)]
#[derive(Default, Debug, Clone, PartialEq)]
$(#[$attr])* pub struct $name {
$($(#[$fattr])* pub $field: $field_type),+,
}
Expand Down Expand Up @@ -154,7 +184,7 @@ macro_rules! state_packets {
packet.$field = Serializable::read_from(&mut buf)?;
}
)+
Result::Ok(Option::Some(Packet::$name(packet)))
Result::Ok(Option::Some(Packet::$name(Box::new(packet))))
},
)*
_ => Result::Ok(Option::None)
Expand Down Expand Up @@ -418,7 +448,7 @@ impl Serializable for f64 {
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct UUID(u64, u64);

#[derive(Debug)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UUIDParseError;
impl std::error::Error for UUIDParseError {}

Expand Down Expand Up @@ -469,6 +499,7 @@ impl Serializable for UUID {
}
}

#[derive(Clone, PartialEq, Eq)]
pub struct Biomes3D {
pub data: [i32; 1024],
}
Expand Down Expand Up @@ -511,6 +542,7 @@ pub trait Lengthable: Serializable + Copy + Default {
fn from_len(_: usize) -> Self;
}

#[derive(Clone, PartialEq, Eq)]
pub struct LenPrefixed<L: Lengthable, V> {
len: L,
pub data: Vec<V>,
Expand Down Expand Up @@ -566,6 +598,7 @@ impl<L: Lengthable, V: fmt::Debug> fmt::Debug for LenPrefixed<L, V> {
}

// Optimization
#[derive(Clone, PartialEq, Eq)]
pub struct LenPrefixedBytes<L: Lengthable> {
len: L,
pub data: Vec<u8>,
Expand Down Expand Up @@ -662,7 +695,7 @@ impl Lengthable for i32 {
use num_traits::cast::{cast, NumCast};
/// `FixedPoint5` has the 5 least-significant bits for the fractional
/// part, upper for integer part: https://wiki.vg/Data_types#Fixed-point_numbers
#[derive(Clone, Copy)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct FixedPoint5<T>(T);

impl<T: Serializable> Serializable for FixedPoint5<T> {
Expand Down Expand Up @@ -708,7 +741,7 @@ where
}

/// `FixedPoint12` is like `FixedPoint5` but the fractional part is 12-bit
#[derive(Clone, Copy)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct FixedPoint12<T>(T);

impl<T: Serializable> Serializable for FixedPoint12<T> {
Expand Down Expand Up @@ -755,7 +788,7 @@ where

/// `VarInt` have a variable size (between 1 and 5 bytes) when encoded based
/// on the size of the number
#[derive(Clone, Copy)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct VarInt(pub i32);

impl Lengthable for VarInt {
Expand Down Expand Up @@ -818,7 +851,7 @@ impl fmt::Debug for VarInt {

/// `VarShort` have a variable size (2 or 3 bytes) and are backwards-compatible
/// with vanilla shorts, used for Forge custom payloads
#[derive(Clone, Copy)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct VarShort(pub i32);

impl Lengthable for VarShort {
Expand Down Expand Up @@ -881,7 +914,7 @@ impl fmt::Debug for VarShort {

/// `VarLong` have a variable size (between 1 and 10 bytes) when encoded based
/// on the size of the number
#[derive(Clone, Copy)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct VarLong(pub i64);

impl Lengthable for VarLong {
Expand Down Expand Up @@ -963,7 +996,7 @@ impl Serializable for Position {

/// Direction is used to define whether packets are going to the
/// server or the client.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Direction {
Serverbound,
Clientbound,
Expand All @@ -986,6 +1019,7 @@ pub enum Error {
Disconnect(format::Component),
IOError(io::Error),
Json(serde_json::Error),
#[cfg(feature = "auth")]
#[cfg(not(target_arch = "wasm32"))]
Reqwest(reqwest::Error),
}
Expand All @@ -1002,6 +1036,7 @@ impl convert::From<serde_json::Error> for Error {
}
}

#[cfg(feature = "auth")]
#[cfg(not(target_arch = "wasm32"))]
impl convert::From<reqwest::Error> for Error {
fn from(e: reqwest::Error) -> Error {
Expand All @@ -1018,6 +1053,7 @@ impl ::std::fmt::Display for Error {
Error::Disconnect(ref val) => write!(f, "{}", val),
Error::IOError(ref e) => e.fmt(f),
Error::Json(ref e) => e.fmt(f),
#[cfg(feature = "auth")]
#[cfg(not(target_arch = "wasm32"))]
Error::Reqwest(ref e) => e.fmt(f),
}
Expand Down Expand Up @@ -1436,7 +1472,7 @@ pub fn try_parse_packet(ibuf: Vec<u8>, protocol_version: i32) {
}
}

#[derive(Debug)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Status {
pub version: StatusVersion,
pub players: StatusPlayers,
Expand All @@ -1446,20 +1482,20 @@ pub struct Status {
pub fml_network_version: Option<i64>,
}

#[derive(Debug)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StatusVersion {
pub name: String,
pub protocol: i32,
}

#[derive(Debug)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StatusPlayers {
pub max: i32,
pub online: i32,
pub sample: Vec<StatusPlayer>,
}

#[derive(Debug)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StatusPlayer {
name: String,
id: String,
Expand Down
3 changes: 2 additions & 1 deletion protocol/src/protocol/mojang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use serde_json::json;
use sha1::{self, Digest};

#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Profile {
pub username: String,
pub id: String,
Expand All @@ -28,6 +28,7 @@ const LOGIN_URL: &str = "https://authserver.mojang.com/authenticate";
const REFRESH_URL: &str = "https://authserver.mojang.com/refresh";
const VALIDATE_URL: &str = "https://authserver.mojang.com/validate";

#[cfg(feature = "auth")]
#[cfg(not(target_arch = "wasm32"))]
impl Profile {
pub fn login(username: &str, password: &str, token: &str) -> Result<Profile, super::Error> {
Expand Down
Loading