From e28946b691b241cedaf62bcb5932b6212e3ae31f Mon Sep 17 00:00:00 2001 From: ice_iix Date: Tue, 11 Dec 2018 18:18:25 -0800 Subject: [PATCH] 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. --- protocol/src/types/metadata.rs | 118 ++++++++++++++------------------- 1 file changed, 49 insertions(+), 69 deletions(-) diff --git a/protocol/src/types/metadata.rs b/protocol/src/types/metadata.rs index 498cd01..44ee0a7 100644 --- a/protocol/src/types/metadata.rs +++ b/protocol/src/types/metadata.rs @@ -38,66 +38,28 @@ impl MetadataKey { } } -pub struct Metadata18 { +pub struct Metadata { map: HashMap, } -pub struct Metadata19 { - map: HashMap, -} - -trait MetadataBase: fmt::Debug + Default { - fn map(&self) -> &HashMap; - fn map_mut(&mut self) -> &mut HashMap; - - fn get(&self, key: &MetadataKey) -> Option<&T> { - self.map().get(&key.index).map(T::unwrap) +impl Metadata { + pub fn new() -> Metadata { + Metadata { map: HashMap::new() } } - fn put(&mut self, key: &MetadataKey, val: T) { - self.map_mut().insert(key.index, val.wrap()); + pub fn get(&self, key: &MetadataKey) -> Option<&T> { + self.map.get(&key.index).map(T::unwrap) + } + + pub fn put(&mut self, key: &MetadataKey, val: T) { + self.map.insert(key.index, val.wrap()); } fn put_raw(&mut self, index: i32, val: T) { - self.map_mut().insert(index, val.wrap()); + self.map.insert(index, val.wrap()); } - fn fmt2(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Metadata[ ")?; - for (k, v) in self.map() { - write!(f, "{:?}={:?}, ", k, v)?; - } - write!(f, "]") - } -} - -impl MetadataBase for Metadata18 { - fn map(&self) -> &HashMap { &self.map } - fn map_mut(&mut self) -> &mut HashMap { &mut self.map } -} - -impl MetadataBase for Metadata19 { - fn map(&self) -> &HashMap { &self.map } - fn map_mut(&mut self) -> &mut HashMap { &mut self.map } -} - -impl Metadata18 { - pub fn new() -> Self { - Self { map: HashMap::new() } - } -} - -impl Metadata19 { - pub fn new() -> Self { - Self { map: HashMap::new() } - } -} - - - -impl Serializable for Metadata18 { - - fn read_from(buf: &mut R) -> Result { + fn read_from18(buf: &mut R) -> Result { let mut m = Self::new(); loop { let ty_index = u8::read_from(buf)? as i32; @@ -128,7 +90,7 @@ impl Serializable for Metadata18 { Ok(m) } - fn write_to(&self, buf: &mut W) -> Result<(), protocol::Error> { + fn write_to18(&self, buf: &mut W) -> Result<(), protocol::Error> { for (k, v) in &self.map { if (*k as u8) > 0x1f { panic!("write metadata index {:x} > 0x1f", *k as u8); @@ -177,9 +139,7 @@ impl Serializable for Metadata18 { val[2].write_to(buf)?; } - Value::FormatComponent(_) | Value::Bool(_) | Value::Position(_) | - Value::OptionalPosition(_) | Value::Direction(_) | Value::OptionalUUID(_) | - Value::Block(_) | Value::NBTTag(_) => { + _ => { panic!("attempted to write 1.9+ metadata to 1.8"); } } @@ -187,11 +147,8 @@ impl Serializable for Metadata18 { u8::write_to(&0x7f, buf)?; Ok(()) } -} -impl Serializable for Metadata19 { - - fn read_from(buf: &mut R) -> Result { + fn read_from19(buf: &mut R) -> Result { let mut m = Self::new(); loop { let index = u8::read_from(buf)? as i32; @@ -243,7 +200,7 @@ impl Serializable for Metadata19 { Ok(m) } - fn write_to(&self, buf: &mut W) -> Result<(), protocol::Error> { + fn write_to19(&self, buf: &mut W) -> Result<(), protocol::Error> { for (k, v) in &self.map { (*k as u8).write_to(buf)?; match *v { @@ -316,18 +273,41 @@ impl Serializable for Metadata19 { } } -// TODO: is it possible to implement these traits on MetadataBase instead? -impl fmt::Debug for Metadata19 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.fmt2(f) } } -impl fmt::Debug for Metadata18 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.fmt2(f) } } +impl Serializable for Metadata { + fn read_from(buf: &mut R) -> Result { + let protocol_version = unsafe { protocol::CURRENT_PROTOCOL_VERSION }; -impl Default for Metadata19 { - fn default() -> Self { - Self::new() + if protocol_version >= 74 { + Metadata::read_from19(buf) + } else { + Metadata::read_from18(buf) + } + } + + fn write_to(&self, buf: &mut W) -> Result<(), protocol::Error> { + let protocol_version = unsafe { protocol::CURRENT_PROTOCOL_VERSION }; + + if protocol_version >= 74 { + self.write_to19(buf) + } else { + self.write_to18(buf) + } } } -impl Default for Metadata18 { - fn default() -> Self { - Self::new() + +impl fmt::Debug for Metadata { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Metadata[ ")?; + for (k, v) in &self.map { + write!(f, "{:?}={:?}, ", k, v)?; + } + write!(f, "]") + } +} + +impl Default for Metadata { + fn default() -> Metadata { + Metadata::new() } } @@ -561,7 +541,7 @@ mod test { #[test] fn basic() { - let mut m = Metadata19::new(); + let mut m = Metadata::new(); m.put(&TEST, "Hello world".to_owned());