From 0bf0c5e987472a2cdea9f50bd52ba239ec1bb218 Mon Sep 17 00:00:00 2001 From: Ben Reeves Date: Mon, 17 Jan 2022 23:10:12 -0600 Subject: [PATCH 1/6] Bump Rust version to 1.58 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index dd581be1..55a74253 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "stevenarella" version = "0.0.1" authors = [ "Thinkofdeath ", "iceiix " ] 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" From 1f39c882b7442510177b0a81448cd32133b6e089 Mon Sep 17 00:00:00 2001 From: Ben Reeves Date: Mon, 17 Jan 2022 22:59:25 -0600 Subject: [PATCH 2/6] Derive Clone, PartialEq, Eq, and Hash where appropriate. This is really useful for anyone who wishes to use the steven_protocol crate and write tests that compare packets to expected values. --- protocol/src/format.rs | 8 ++--- protocol/src/item.rs | 2 +- protocol/src/nbt/mod.rs | 4 +-- protocol/src/protocol/forge.rs | 14 ++++----- protocol/src/protocol/mod.rs | 29 ++++++++++-------- protocol/src/protocol/mojang.rs | 2 +- protocol/src/protocol/packet.rs | 54 ++++++++++++++++----------------- protocol/src/types/bit/set.rs | 2 +- protocol/src/types/metadata.rs | 10 +++--- protocol/src/types/mod.rs | 2 +- src/chunk_builder.rs | 2 +- 11 files changed, 67 insertions(+), 62 deletions(-) diff --git a/protocol/src/format.rs b/protocol/src/format.rs index 6f433c1a..c3cc9d0d 100644 --- a/protocol/src/format.rs +++ b/protocol/src/format.rs @@ -15,7 +15,7 @@ use std::fmt; use std::mem; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum Component { Text(TextComponent), } @@ -115,7 +115,7 @@ impl Default for Component { } } -#[derive(Debug, Default, Clone)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct Modifier { pub extra: Option>, pub bold: Option, @@ -159,7 +159,7 @@ impl Modifier { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct TextComponent { pub text: String, pub modifier: Modifier, @@ -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, diff --git a/protocol/src/item.rs b/protocol/src/item.rs index 8851dd57..f48d6c14 100644 --- a/protocol/src/item.rs +++ b/protocol/src/item.rs @@ -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, diff --git a/protocol/src/nbt/mod.rs b/protocol/src/nbt/mod.rs index 7ddd5d55..7d2d638c 100644 --- a/protocol/src/nbt/mod.rs +++ b/protocol/src/nbt/mod.rs @@ -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), @@ -37,7 +37,7 @@ pub enum Tag { LongArray(Vec), } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct NamedTag(pub String, pub Tag); impl Tag { diff --git a/protocol/src/protocol/forge.rs b/protocol/src/protocol/forge.rs index 254a45cb..2409ad45 100644 --- a/protocol/src/protocol/forge.rs +++ b/protocol/src/protocol/forge.rs @@ -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, @@ -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, @@ -64,7 +64,7 @@ impl Serializable for ForgeMod { } } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct ModIdMapping { pub name: String, pub id: VarInt, @@ -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, @@ -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, @@ -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, @@ -236,7 +236,7 @@ pub mod fml2 { } } - #[derive(Debug)] + #[derive(Debug, Clone, PartialEq, Eq)] pub enum FmlHandshake { ModList { mod_names: LenPrefixed, diff --git a/protocol/src/protocol/mod.rs b/protocol/src/protocol/mod.rs index dfb4266d..1c8ad362 100644 --- a/protocol/src/protocol/mod.rs +++ b/protocol/src/protocol/mod.rs @@ -75,7 +75,7 @@ macro_rules! state_packets { use crate::protocol::*; use std::io; - #[derive(Debug)] + #[derive(Debug, Clone, PartialEq)] pub enum Packet { $( $( @@ -107,7 +107,7 @@ macro_rules! state_packets { } $( - #[derive(Default, Debug)] + #[derive(Default, Debug, Clone, PartialEq)] $(#[$attr])* pub struct $name { $($(#[$fattr])* pub $field: $field_type),+, } @@ -418,7 +418,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 {} @@ -469,6 +469,7 @@ impl Serializable for UUID { } } +#[derive(Clone, PartialEq, Eq)] pub struct Biomes3D { pub data: [i32; 1024], } @@ -511,6 +512,7 @@ pub trait Lengthable: Serializable + Copy + Default { fn from_len(_: usize) -> Self; } +#[derive(Clone, PartialEq, Eq)] pub struct LenPrefixed { len: L, pub data: Vec, @@ -566,6 +568,7 @@ impl fmt::Debug for LenPrefixed { } // Optimization +#[derive(Clone, PartialEq, Eq)] pub struct LenPrefixedBytes { len: L, pub data: Vec, @@ -662,7 +665,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); impl Serializable for FixedPoint5 { @@ -708,7 +711,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); impl Serializable for FixedPoint12 { @@ -755,7 +758,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 { @@ -818,7 +821,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 { @@ -881,7 +884,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 { @@ -963,7 +966,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, @@ -1436,7 +1439,7 @@ pub fn try_parse_packet(ibuf: Vec, protocol_version: i32) { } } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct Status { pub version: StatusVersion, pub players: StatusPlayers, @@ -1446,20 +1449,20 @@ pub struct Status { pub fml_network_version: Option, } -#[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, } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct StatusPlayer { name: String, id: String, diff --git a/protocol/src/protocol/mojang.rs b/protocol/src/protocol/mojang.rs index 8b7e50a5..0c852e65 100644 --- a/protocol/src/protocol/mojang.rs +++ b/protocol/src/protocol/mojang.rs @@ -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, diff --git a/protocol/src/protocol/packet.rs b/protocol/src/protocol/packet.rs index 31383d65..baee55f5 100644 --- a/protocol/src/protocol/packet.rs +++ b/protocol/src/protocol/packet.rs @@ -2415,7 +2415,7 @@ state_packets!( } ); -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct SpawnProperty { pub name: String, pub value: String, @@ -2438,7 +2438,7 @@ impl Serializable for SpawnProperty { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct Statistic { pub name: String, pub value: VarInt, @@ -2458,7 +2458,7 @@ impl Serializable for Statistic { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct BlockChangeRecord { pub xz: u8, pub y: u8, @@ -2481,7 +2481,7 @@ impl Serializable for BlockChangeRecord { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct ChunkMeta { pub x: i32, pub z: i32, @@ -2504,7 +2504,7 @@ impl Serializable for ChunkMeta { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct ExplosionRecord { pub x: i8, pub y: i8, @@ -2527,7 +2527,7 @@ impl Serializable for ExplosionRecord { } } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct MapIcon { pub direction_type: i8, pub x: i8, @@ -2560,7 +2560,7 @@ impl Default for MapIcon { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq)] pub struct Advancement { pub id: String, pub parent_id: Option, @@ -2613,7 +2613,7 @@ impl Serializable for Advancement { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq)] pub struct AdvancementDisplay { pub title: String, pub description: String, @@ -2666,7 +2666,7 @@ impl Serializable for AdvancementDisplay { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct AdvancementProgress { pub id: String, pub criteria: LenPrefixed, @@ -2686,7 +2686,7 @@ impl Serializable for AdvancementProgress { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct CriterionProgress { pub id: String, pub date_of_achieving: Option, @@ -2714,7 +2714,7 @@ impl Serializable for CriterionProgress { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq)] pub struct EntityEquipment { pub slot: u8, pub item: Option, @@ -2735,7 +2735,7 @@ impl Serializable for EntityEquipment { } // Top-bit terminated array of EntityEquipment -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq)] pub struct EntityEquipments { pub equipments: Vec, } @@ -2765,7 +2765,7 @@ impl Serializable for EntityEquipments { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq)] pub struct EntityProperty { pub key: String, pub value: f64, @@ -2788,7 +2788,7 @@ impl Serializable for EntityProperty { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq)] pub struct EntityProperty_i16 { pub key: String, pub value: f64, @@ -2811,7 +2811,7 @@ impl Serializable for EntityProperty_i16 { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq)] pub struct PropertyModifier { pub uuid: UUID, pub amount: f64, @@ -2834,7 +2834,7 @@ impl Serializable for PropertyModifier { } } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq)] pub struct PlayerInfoData { pub action: VarInt, pub players: Vec, @@ -2920,7 +2920,7 @@ impl Default for PlayerInfoData { } } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum PlayerDetail { Add { uuid: UUID, @@ -2947,7 +2947,7 @@ pub enum PlayerDetail { }, } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct PlayerProperty { pub name: String, pub value: String, @@ -2957,7 +2957,7 @@ pub struct PlayerProperty { use crate::item; type RecipeIngredient = LenPrefixed>; -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq)] pub enum RecipeData { Shapeless { group: String, @@ -3031,7 +3031,7 @@ impl Default for RecipeData { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq)] pub struct Recipe { pub id: String, pub ty: String, @@ -3153,7 +3153,7 @@ impl Serializable for Recipe { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct Tags { pub tag_name: String, pub entries: LenPrefixed, @@ -3172,7 +3172,7 @@ impl Serializable for Tags { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct TagsGroup { pub tag_type: String, pub tags: LenPrefixed, @@ -3191,7 +3191,7 @@ impl Serializable for TagsGroup { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq)] pub struct Trade { pub input_item_1: Option, pub output_item: Option, @@ -3234,7 +3234,7 @@ impl Serializable for Trade { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq)] pub struct CommandNode { pub flags: u8, pub children: LenPrefixed, @@ -3245,14 +3245,14 @@ pub struct CommandNode { pub suggestions_type: Option, } -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Copy, Clone, Eq, PartialEq)] enum CommandNodeType { Root, Literal, Argument, } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq)] pub enum CommandProperty { Bool, Double { @@ -3484,7 +3484,7 @@ impl Serializable for CommandNode { } } -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone, Default, PartialEq)] pub struct NumberedSlot { pub slot_number: i16, pub slot_data: Option, diff --git a/protocol/src/types/bit/set.rs b/protocol/src/types/bit/set.rs index 14cf4fc9..c5bb7242 100644 --- a/protocol/src/types/bit/set.rs +++ b/protocol/src/types/bit/set.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq, Eq)] pub struct Set { data: Vec, } diff --git a/protocol/src/types/metadata.rs b/protocol/src/types/metadata.rs index 1c8cec9f..b7601894 100644 --- a/protocol/src/types/metadata.rs +++ b/protocol/src/types/metadata.rs @@ -24,6 +24,7 @@ use std::fmt; use std::io; use std::marker::PhantomData; +#[derive(Clone, Copy, PartialEq, Eq)] pub struct MetadataKey { index: i32, ty: PhantomData, @@ -39,6 +40,7 @@ impl MetadataKey { } } +#[derive(Clone, PartialEq)] pub struct Metadata { map: HashMap, } @@ -491,7 +493,7 @@ impl Default for Metadata { } } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq)] pub enum Value { Byte(i8), Short(i16), @@ -516,7 +518,7 @@ pub enum Value { Pose(PoseData), } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq)] pub enum ParticleData { AmbientEntityEffect, AngryVillager, @@ -655,7 +657,7 @@ impl Serializable for ParticleData { } } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] #[allow(dead_code)] pub struct VillagerData { villager_type: protocol::VarInt, @@ -680,7 +682,7 @@ impl Serializable for VillagerData { } } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum PoseData { Standing, FallFlying, diff --git a/protocol/src/types/mod.rs b/protocol/src/types/mod.rs index 4eb41fd1..b6466485 100644 --- a/protocol/src/types/mod.rs +++ b/protocol/src/types/mod.rs @@ -19,7 +19,7 @@ pub mod bit; pub mod hash; pub mod nibble; -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Gamemode { Survival = 0, Creative = 1, diff --git a/src/chunk_builder.rs b/src/chunk_builder.rs index 2f4dccdd..cd17afed 100644 --- a/src/chunk_builder.rs +++ b/src/chunk_builder.rs @@ -393,7 +393,7 @@ fn flood_fill(snapshot: &world::Snapshot, visited: &mut Set, x: i32, y: i32, z: touched } -#[derive(Clone, Copy, Default)] +#[derive(Clone, Copy, Default, PartialEq, Eq)] pub struct CullInfo(u64); impl CullInfo { From dfbfeddfdff0138a7e6ae6701f04f2a33a49c029 Mon Sep 17 00:00:00 2001 From: Ben Reeves Date: Mon, 17 Jan 2022 19:42:27 -0600 Subject: [PATCH 3/6] protocol: Add set_current_protocol_version function. For users of the crate, it's important to be able to set this if they are not going through the `Conn` class. Ideally, the protocol version would be plumbed through every `Serializable` impl, but that's a much bigger refactor. --- protocol/src/protocol/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/protocol/src/protocol/mod.rs b/protocol/src/protocol/mod.rs index 1c8ad362..204e7681 100644 --- a/protocol/src/protocol/mod.rs +++ b/protocol/src/protocol/mod.rs @@ -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); } From 9fca0221db0758bf5a3bde0d3fb4b2f753f46c1d Mon Sep 17 00:00:00 2001 From: Ben Reeves Date: Mon, 17 Jan 2022 21:17:08 -0600 Subject: [PATCH 4/6] protocol: Implement PacketType for Packet. --- protocol/src/protocol/mod.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/protocol/src/protocol/mod.rs b/protocol/src/protocol/mod.rs index 204e7681..44d5017a 100644 --- a/protocol/src/protocol/mod.rs +++ b/protocol/src/protocol/mod.rs @@ -90,6 +90,32 @@ macro_rules! state_packets { )+ } + impl PacketType for Packet { + fn packet_id(&self, version: i32) -> i32 { + match self { + $( + $( + $( + Self::$name(p) => p.packet_id(version), + )* + )+ + )+ + } + } + + fn write(&self, buf: &mut W) -> Result<(), Error> { + match self { + $( + $( + $( + Self::$name(p) => p.write(buf), + )* + )+ + )+ + } + } + } + $( pub mod $state { From a79d5d8ef0b986f0b4b6ac631e71bd4eb0c1f802 Mon Sep 17 00:00:00 2001 From: Ben Reeves Date: Tue, 18 Jan 2022 00:45:21 -0600 Subject: [PATCH 5/6] Box up all Packet variants. Previously, the Packet enum was a whopping 4,264 bytes in size (over a page!). Not only is this just bad for performance (lots of memmoving), but I think it was also causing weird stack overflow segfaults on my machine. --- protocol/src/protocol/mod.rs | 4 ++-- src/server/mod.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/protocol/src/protocol/mod.rs b/protocol/src/protocol/mod.rs index 44d5017a..9c9d81b0 100644 --- a/protocol/src/protocol/mod.rs +++ b/protocol/src/protocol/mod.rs @@ -84,7 +84,7 @@ macro_rules! state_packets { $( $( $( - $name($state::$dir::$name), + $name(Box<$state::$dir::$name>), )* )+ )+ @@ -184,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) diff --git a/src/server/mod.rs b/src/server/mod.rs index 1851e9b1..1dd5d192 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -98,7 +98,7 @@ macro_rules! handle_packet { }) => ( match $pck { $( - protocol::packet::Packet::$packet(val) => $s.$func(val), + protocol::packet::Packet::$packet(val) => $s.$func(*val), )* _ => {}, } From eb4707ad6b3a7fa88841849392a293131051f08a Mon Sep 17 00:00:00 2001 From: Ben Reeves Date: Mon, 24 Jan 2022 21:28:35 -0600 Subject: [PATCH 6/6] Make protocol authentication optional behind a feature flag. Currently, the `hyper` crate (dependency of `reqwest`) causes linker errors when you attempt to compile a consumer of `steven_protocol` as a dylib. Compiling as a dylib is not uncommon for quick iteration; notably popularized by the Bevy game engine. See https://github.com/rust-lang/rust/issues/82151#issuecomment-779368017. --- protocol/Cargo.toml | 8 +++++++- protocol/src/protocol/mod.rs | 3 +++ protocol/src/protocol/mojang.rs | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/protocol/Cargo.toml b/protocol/Cargo.toml index ce52bef3..b35bef2b 100644 --- a/protocol/Cargo.toml +++ b/protocol/Cargo.toml @@ -4,6 +4,12 @@ version = "0.0.1" authors = [ "Thinkofdeath ", "iceiix " ] edition = "2021" +[features] +# Enables support for authenticating with Mojang servers. +auth = ["reqwest"] + +default = ["auth"] + [dependencies] serde = "1.0.132" serde_json = "1.0.73" @@ -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 } diff --git a/protocol/src/protocol/mod.rs b/protocol/src/protocol/mod.rs index 9c9d81b0..a2b9210b 100644 --- a/protocol/src/protocol/mod.rs +++ b/protocol/src/protocol/mod.rs @@ -1019,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), } @@ -1035,6 +1036,7 @@ impl convert::From for Error { } } +#[cfg(feature = "auth")] #[cfg(not(target_arch = "wasm32"))] impl convert::From for Error { fn from(e: reqwest::Error) -> Error { @@ -1051,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), } diff --git a/protocol/src/protocol/mojang.rs b/protocol/src/protocol/mojang.rs index 0c852e65..1f7de9bf 100644 --- a/protocol/src/protocol/mojang.rs +++ b/protocol/src/protocol/mojang.rs @@ -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 {