diff options
Diffstat (limited to 'server/protocol/src')
| -rw-r--r-- | server/protocol/src/lib.rs | 50 | 
1 files changed, 38 insertions, 12 deletions
| diff --git a/server/protocol/src/lib.rs b/server/protocol/src/lib.rs index 94bebf05..895af376 100644 --- a/server/protocol/src/lib.rs +++ b/server/protocol/src/lib.rs @@ -15,6 +15,10 @@      along with this program.  If not, see <https://www.gnu.org/licenses/>.  */ +use bincode::{ +    config::{standard, Configuration, Limit, LittleEndian, Varint}, +    Decode, Encode, +};  use glam::{IVec2, Vec2};  use serde::{Deserialize, Serialize};  use std::{ @@ -26,31 +30,44 @@ pub use glam;  pub const VERSION: (u32, u32) = (1, 0); -#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub const BINCODE_CONFIG: Configuration<LittleEndian, Varint, Limit<4096>> = +    standard().with_limit(); + +#[derive( +    Debug, Clone, Copy, Serialize, Deserialize, Encode, Decode, PartialEq, Eq, PartialOrd, Ord, Hash, +)]  #[serde(transparent)]  pub struct PlayerID(pub i64); -#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive( +    Debug, Clone, Copy, Serialize, Deserialize, Encode, Decode, PartialEq, Eq, PartialOrd, Ord, Hash, +)]  #[serde(transparent)]  pub struct ItemIndex(pub usize); -#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive( +    Debug, Clone, Copy, Serialize, Deserialize, Encode, Decode, PartialEq, Eq, PartialOrd, Ord, Hash, +)]  #[serde(transparent)]  pub struct TileIndex(pub usize); -#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive( +    Debug, Clone, Copy, Serialize, Deserialize, Encode, Decode, PartialEq, Eq, PartialOrd, Ord, Hash, +)]  #[serde(transparent)]  pub struct RecipeIndex(pub usize); -#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive( +    Debug, Clone, Copy, Serialize, Deserialize, Encode, Decode, PartialEq, Eq, PartialOrd, Ord, Hash, +)]  #[serde(transparent)]  pub struct DemandIndex(pub usize); -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]  pub struct MapMetadata {      name: String,      players: usize,      difficulty: i32,  } -#[derive(Debug, Clone, Serialize, Deserialize, Default)] +#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode,  Default)]  #[rustfmt::skip]  pub struct ClientGamedata {      pub item_names: Vec<String>, @@ -61,7 +78,7 @@ pub struct ClientGamedata {      pub maps: HashMap<String, MapMetadata>,  } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]  #[serde(rename_all = "snake_case", tag = "type")]  pub enum PacketS {      Join { @@ -70,15 +87,18 @@ pub enum PacketS {      },      Leave,      Position { +        #[bincode(with_serde)]          pos: Vec2,          rot: f32,          boosting: bool,      },      Interact { +        #[bincode(with_serde)]          pos: Option<IVec2>,      },      Collide {          player: PlayerID, +        #[bincode(with_serde)]          force: Vec2,      },      Communicate { @@ -87,6 +107,7 @@ pub enum PacketS {      },      #[serde(skip)] +    #[bincode(skip)]      /// For internal use only      ReplaceHand {          item: Option<ItemIndex>, @@ -97,7 +118,7 @@ pub enum PacketS {      },  } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]  #[serde(rename_all = "snake_case")]  pub enum Message {      Text(String), @@ -105,12 +126,13 @@ pub enum Message {      Effect(String),  } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]  #[serde(rename_all = "snake_case", tag = "type")]  pub enum PacketC {      Version {          minor: u32,          major: u32, +        supports_bincode: bool,      },      Init {          id: PlayerID, @@ -120,6 +142,7 @@ pub enum PacketC {      },      AddPlayer {          id: PlayerID, +        #[bincode(with_serde)]          position: Vec2,          character: i32,          name: String, @@ -129,6 +152,7 @@ pub enum PacketC {      },      Position {          player: PlayerID, +        #[bincode(with_serde)]          pos: Vec2,          rot: f32,          boosting: bool, @@ -147,12 +171,14 @@ pub enum PacketC {          warn: bool,      },      UpdateMap { +        #[bincode(with_serde)]          tile: IVec2,          kind: Option<TileIndex>,          neighbors: [Option<TileIndex>; 4],      },      Collide {          player: PlayerID, +        #[bincode(with_serde)]          force: Vec2,      },      Communicate { @@ -181,10 +207,10 @@ pub enum PacketC {      ReplayStart,  } -#[derive(Debug, Clone, Serialize, Deserialize, Copy, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode, Copy, PartialEq, Eq, Hash)]  #[serde(rename_all = "snake_case")]  pub enum ItemLocation { -    Tile(IVec2), +    Tile(#[bincode(with_serde)] IVec2),      Player(PlayerID),  } | 
