Add protocol version global mutable to merge Metadata18/19 into one

Cleans up https://github.com/iceiix/steven/pull/57 1.8.9 (47) multiprotocol support
which added too much code duplication, Metadata19 vs Metadata18, and
different packets for each, the only difference being how it was parsed.

Instead, switch back to using only one Metadata implementation, but with
parsing conditionalized on a new global mutable: SUPPORTED_PROTOCOL_VERSION.
Accessing global mutable state is unsafe but it is only set when
initializing the connection, and only read when deep enough in the code
where it is not feasible to pass otherwise. More elegant, worth it.
This commit is contained in:
ice_iix 2018-12-11 18:18:25 -08:00
parent afac493896
commit a6ea434421
3 changed files with 19 additions and 17 deletions

View File

@ -39,6 +39,8 @@ use crate::shared::Position;
pub const SUPPORTED_PROTOCOLS: [i32; 8] = [340, 316, 315, 210, 109, 107, 74, 47];
// TODO: switch to using thread_local storage?, see https://doc.rust-lang.org/std/macro.thread_local.html
pub static mut CURRENT_PROTOCOL_VERSION: i32 = SUPPORTED_PROTOCOLS[0];
/// Helper macro for defining packets
#[macro_export]
@ -805,6 +807,10 @@ pub struct Conn {
impl Conn {
pub fn new(target: &str, protocol_version: i32) -> Result<Conn, Error> {
unsafe {
CURRENT_PROTOCOL_VERSION = protocol_version;
}
// TODO SRV record support
let mut parts = target.split(':').collect::<Vec<&str>>();
let address = if parts.len() == 1 {

View File

@ -417,7 +417,7 @@ state_packets!(
field velocity_x: i16 =,
field velocity_y: i16 =,
field velocity_z: i16 =,
field metadata: types::Metadata19 =,
field metadata: types::Metadata =,
}
packet SpawnMob_u8 {
field entity_id: VarInt =,
@ -432,7 +432,7 @@ state_packets!(
field velocity_x: i16 =,
field velocity_y: i16 =,
field velocity_z: i16 =,
field metadata: types::Metadata19 =,
field metadata: types::Metadata =,
}
packet SpawnMob_u8_i32 {
field entity_id: VarInt =,
@ -447,9 +447,9 @@ state_packets!(
field velocity_x: i16 =,
field velocity_y: i16 =,
field velocity_z: i16 =,
field metadata: types::Metadata19 =,
field metadata: types::Metadata =,
}
packet SpawnMob_u8_i32_NoUUID_18 {
packet SpawnMob_u8_i32_NoUUID {
field entity_id: VarInt =,
field ty: u8 =,
field x: i32 =,
@ -461,7 +461,7 @@ state_packets!(
field velocity_x: i16 =,
field velocity_y: i16 =,
field velocity_z: i16 =,
field metadata: types::Metadata18 =,
field metadata: types::Metadata =,
}
/// SpawnPainting spawns a painting into the world when it is in range of
/// the client. The title effects the size and the texture of the painting.
@ -489,7 +489,7 @@ state_packets!(
field z: f64 =,
field yaw: i8 =,
field pitch: i8 =,
field metadata: types::Metadata19 =,
field metadata: types::Metadata =,
}
packet SpawnPlayer_i32 {
field entity_id: VarInt =,
@ -499,9 +499,9 @@ state_packets!(
field z: i32 =,
field yaw: i8 =,
field pitch: i8 =,
field metadata: types::Metadata19 =,
field metadata: types::Metadata =,
}
packet SpawnPlayer_i32_HeldItem_18 {
packet SpawnPlayer_i32_HeldItem {
field entity_id: VarInt =,
field uuid: UUID =,
field x: i32 =,
@ -510,7 +510,7 @@ state_packets!(
field yaw: i8 =,
field pitch: i8 =,
field current_item: u16 =,
field metadata: types::Metadata18 =,
field metadata: types::Metadata =,
}
/// Animation is sent by the server to play an animation on a specific entity.
packet Animation {
@ -1010,11 +1010,7 @@ state_packets!(
/// EntityMetadata updates the metadata for an entity.
packet EntityMetadata {
field entity_id: VarInt =,
field metadata: types::Metadata19 =,
}
packet EntityMetadata_18 {
field entity_id: VarInt =,
field metadata: types::Metadata18 =,
field metadata: types::Metadata =,
}
/// EntityAttach attaches to entities together, either by mounting or leashing.
/// -1 can be used at the EntityID to deattach.

View File

@ -48,10 +48,10 @@ protocol_packet_ids!(
0x09 => SetCurrentHotbarSlot
0x0a => EntityUsedBed
0x0b => Animation
0x0c => SpawnPlayer_i32_HeldItem_18
0x0c => SpawnPlayer_i32_HeldItem
0x0d => CollectItem_nocount
0x0e => SpawnObject_i32_NoUUID
0x0f => SpawnMob_u8_i32_NoUUID_18
0x0f => SpawnMob_u8_i32_NoUUID
0x10 => SpawnPainting_NoUUID
0x11 => SpawnExperienceOrb_i32
0x12 => EntityVelocity
@ -64,7 +64,7 @@ protocol_packet_ids!(
0x19 => EntityHeadLook
0x1a => EntityStatus
0x1b => EntityAttach_leashed
0x1c => EntityMetadata_18
0x1c => EntityMetadata
0x1d => EntityEffect
0x1e => EntityRemoveEffect
0x1f => SetExperience