From fe763c3e608afef8c7d390e4daa56d3507e38bfa Mon Sep 17 00:00:00 2001 From: Denis Drakhnia Date: Thu, 26 Sep 2024 10:35:52 +0300 Subject: [PATCH] protocol: move Os to server_info module --- protocol/src/cursor/read.rs | 6 ++++ protocol/src/cursor/write.rs | 14 ++++++++ protocol/src/net/server.rs | 70 ++---------------------------------- protocol/src/server_info.rs | 45 +++++++++++++++++++++++ 4 files changed, 68 insertions(+), 67 deletions(-) diff --git a/protocol/src/cursor/read.rs b/protocol/src/cursor/read.rs index 896c7fc..bb2fed1 100644 --- a/protocol/src/cursor/read.rs +++ b/protocol/src/cursor/read.rs @@ -77,6 +77,12 @@ impl GetKeyValue<'_> for crate::server_info::ServerType { } } +impl GetKeyValue<'_> for crate::server_info::Os { + fn get_key_value(cur: &mut Cursor) -> Result { + cur.get_key_value_raw()?.try_into() + } +} + macro_rules! impl_get_value { ($($t:ty),+ $(,)?) => { $(impl<'a> GetKeyValue<'a> for $t { diff --git a/protocol/src/cursor/write.rs b/protocol/src/cursor/write.rs index bd8e1ce..5e739b5 100644 --- a/protocol/src/cursor/write.rs +++ b/protocol/src/cursor/write.rs @@ -49,6 +49,20 @@ impl PutKeyValue for crate::server_info::ServerType { } } +impl PutKeyValue for crate::server_info::Os { + fn put_key_value<'a, 'b>( + &self, + cur: &'b mut CursorMut<'a>, + ) -> Result<&'b mut CursorMut<'a>, CursorError> { + match self { + Self::Linux => cur.put_str("l"), + Self::Windows => cur.put_str("w"), + Self::Mac => cur.put_str("m"), + Self::Unknown => cur.put_str("?"), + } + } +} + macro_rules! impl_put_key_value { ($($t:ty),+ $(,)?) => { $(impl PutKeyValue for $t { diff --git a/protocol/src/net/server.rs b/protocol/src/net/server.rs index 6b5ca1c..abf6d1e 100644 --- a/protocol/src/net/server.rs +++ b/protocol/src/net/server.rs @@ -3,8 +3,6 @@ //! Game server packets. -use core::fmt; - use crate::{ cursor::{Cursor, CursorMut, GetKeyValue, PutKeyValue}, filter::Version, @@ -21,6 +19,9 @@ pub use crate::server_info::ServerType; #[deprecated(since = "0.2.1", note = "use server_info::ServerFlags instead")] pub use crate::server_info::ServerFlags; +#[deprecated(since = "0.2.1", note = "use server_info::Os instead")] +pub use crate::server_info::Os; + /// Sended to a master server before `ServerAdd` packet. #[derive(Clone, Debug, PartialEq)] pub struct Challenge { @@ -61,71 +62,6 @@ impl Challenge { } } -/// The operating system on which the game server runs. -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -#[repr(u8)] -pub enum Os { - /// GNU/Linux. - Linux, - /// Microsoft Windows - Windows, - /// Apple macOS, OS X, Mac OS X - Mac, - /// Unknown - Unknown, -} - -impl Default for Os { - fn default() -> Os { - Os::Unknown - } -} - -impl TryFrom<&[u8]> for Os { - type Error = CursorError; - - fn try_from(value: &[u8]) -> Result { - match value { - b"l" => Ok(Os::Linux), - b"w" => Ok(Os::Windows), - b"m" => Ok(Os::Mac), - _ => Ok(Os::Unknown), - } - } -} - -impl GetKeyValue<'_> for Os { - fn get_key_value(cur: &mut Cursor) -> Result { - cur.get_key_value_raw()?.try_into() - } -} - -impl PutKeyValue for Os { - fn put_key_value<'a, 'b>( - &self, - cur: &'b mut CursorMut<'a>, - ) -> Result<&'b mut CursorMut<'a>, CursorError> { - match self { - Self::Linux => cur.put_str("l"), - Self::Windows => cur.put_str("w"), - Self::Mac => cur.put_str("m"), - Self::Unknown => cur.put_str("?"), - } - } -} - -impl fmt::Display for Os { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - let s = match self { - Os::Linux => "Linux", - Os::Windows => "Windows", - Os::Mac => "Mac", - Os::Unknown => "Unknown", - }; - write!(fmt, "{}", s) - } -} - /// Add/update game server information on the master server. #[derive(Clone, Debug, PartialEq, Default)] pub struct ServerAdd { diff --git a/protocol/src/server_info.rs b/protocol/src/server_info.rs index 54f5d09..46972d5 100644 --- a/protocol/src/server_info.rs +++ b/protocol/src/server_info.rs @@ -132,6 +132,51 @@ bitflags! { } } +/// The operating system on which the game server runs. +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[repr(u8)] +pub enum Os { + /// GNU/Linux. + Linux, + /// Microsoft Windows + Windows, + /// Apple macOS, OS X, Mac OS X + Mac, + /// Unknown + Unknown, +} + +impl Default for Os { + fn default() -> Os { + Os::Unknown + } +} + +impl TryFrom<&[u8]> for Os { + type Error = CursorError; + + fn try_from(value: &[u8]) -> Result { + match value { + b"l" => Ok(Os::Linux), + b"w" => Ok(Os::Windows), + b"m" => Ok(Os::Mac), + _ => Ok(Os::Unknown), + } + } +} + +impl fmt::Display for Os { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + let s = match self { + Os::Linux => "Linux", + Os::Windows => "Windows", + Os::Mac => "Mac", + Os::Unknown => "Unknown", + }; + write!(fmt, "{}", s) + } +} + /// Game server information. #[derive(Clone, Debug)] pub struct ServerInfo {