From 9e4a1f1be0685e0ba761bf7d851ff7489789d3d3 Mon Sep 17 00:00:00 2001 From: Lizzy Fleckenstein Date: Mon, 13 Feb 2023 17:13:25 +0100 Subject: [PATCH] Use improved attributes --- src/lib.rs | 2 +- src/to_clt.rs | 34 ++++++++++++++++++---------------- src/to_clt/env.rs | 36 +++++++++++++----------------------- src/to_clt/hud.rs | 4 ++-- src/to_clt/media.rs | 13 ------------- src/to_srv.rs | 37 +++++++++++-------------------------- 6 files changed, 45 insertions(+), 81 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9b410a6..644d25e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,7 @@ use std::{ }; #[cfg(any(feature = "client", feature = "server"))] -use mt_ser::{DefCfg, DeserializeError, MtCfg, MtDeserialize, MtSerialize, SerializeError}; +use mt_ser::{DefCfg, DeserializeError, MtCfg, MtDeserialize, MtSerialize, SerializeError, Utf16}; #[cfg(feature = "random")] use generate_random::GenerateRandom; diff --git a/src/to_clt.rs b/src/to_clt.rs index 871328a..00d952d 100644 --- a/src/to_clt.rs +++ b/src/to_clt.rs @@ -34,7 +34,7 @@ pub use status::*; pub enum ToCltPkt { Hello { serialize_version: u8, - #[mt(const16 = 1)] // compression + #[mt(const_before = "1u16")] // compression proto_version: u16, auth_methods: EnumSet, username: String, @@ -46,6 +46,7 @@ pub enum ToCltPkt { sudo_auth_methods: EnumSet, } = 3, AcceptSudoMode { + #[mt(const_after = "[0u8; 15]")] sudo_auth_methods: EnumSet, } = 4, DenySudoMode = 5, @@ -86,11 +87,11 @@ pub enum ToCltPkt { should_cache: bool, } = 44, ChatMsg { - #[mt(const8 = 1)] + #[mt(const_before = "1u8")] msg_type: ChatMsgType, - #[mt(utf16)] + #[mt(len = "Utf16")] sender: String, - #[mt(utf16)] + #[mt(len = "Utf16")] text: String, timestamp: i64, // unix time } = 47, @@ -99,7 +100,7 @@ pub enum ToCltPkt { add: Vec, } = 49, ObjMsgs { - #[mt(len0)] + #[mt(len = "()")] msgs: Vec, } = 50, Hp { @@ -113,7 +114,7 @@ pub enum ToCltPkt { yaw: f32, } = 52, LegacyKick { - #[mt(utf16)] + #[mt(len = "Utf16")] reason: String, } = 53, Fov { @@ -128,18 +129,19 @@ pub enum ToCltPkt { Media { n: u16, i: u16, - files: Vec, // FIXME: can we use a HashMap for this? + #[mt(len = "(u32, (DefCfg, u32))")] + files: HashMap>, // name -> payload } = 56, NodeDefs { defs: Vec, } = 58, AnnounceMedia { - files: Vec, // FIXME: can we use a HashMap for this? + files: HashMap, // name -> base64 hash url: String, } = 60, - #[mt(size32, zlib)] + #[mt(size = "u32", zlib)] ItemDefs { - #[mt(const8 = 0)] // version + #[mt(const_before = "0u8")] // version defs: Vec, aliases: HashMap, } = 61, @@ -163,18 +165,18 @@ pub enum ToCltPkt { privs: HashSet, } = 65, InvFormspec { - #[mt(size32)] + #[mt(size = "u32")] formspec: String, } = 66, DetachedInv { name: String, keep: bool, len: u16, - #[mt(len0)] + #[mt(len = "()")] inv: String, } = 67, ShowFormspec { - #[mt(len32)] + #[mt(len = "u32")] formspec: String, formname: String, } = 68, @@ -196,7 +198,7 @@ pub enum ToCltPkt { expiration_time: f32, size: f32, collide: bool, - #[mt(len32)] + #[mt(len = "u32")] texture: String, vertical: bool, collision_rm: bool, @@ -216,7 +218,7 @@ pub enum ToCltPkt { expiration_time: [f32; 2], size: [f32; 2], collide: bool, - #[mt(len32)] + #[mt(len = "u32")] texture: String, id: u32, vertical: bool, @@ -296,7 +298,7 @@ pub enum ToCltPkt { channel: String, } = 88, NodeMetasChanged { - #[mt(size32)] + #[mt(size = "u32")] changed: HashMap<[i16; 3], NodeMeta>, } = 89, SunParams { diff --git a/src/to_clt/env.rs b/src/to_clt/env.rs index cce7696..031ee96 100644 --- a/src/to_clt/env.rs +++ b/src/to_clt/env.rs @@ -2,7 +2,7 @@ use super::*; #[mt_derive(to = "clt")] pub struct ObjProps { - #[mt(const8 = 4)] // version + #[mt(const_before = "4u8")] // version pub max_hp: u16, // player only pub collide_with_nodes: bool, pub weight: f32, // deprecated @@ -93,6 +93,8 @@ pub struct ObjPhysicsOverride { pub old_sneak: bool, } +const GENERIC_CAO: u8 = 101; + #[mt_derive(to = "clt", repr = "u8", tag = "type", content = "data")] pub enum ObjMsg { Props(ObjProps) = 0, @@ -116,9 +118,8 @@ pub enum ObjMsg { Attach(ObjAttach), PhysicsOverride(ObjPhysicsOverride), SpawnInfant { + #[mt(const_after = "GENERIC_CAO")] id: u16, - #[mt(const8 = 101)] // GenericCAO - infant_type: (), } = 11, AnimSpeed { speed: f32, @@ -128,31 +129,31 @@ pub enum ObjMsg { #[mt_derive(to = "clt")] pub struct ObjIdMsg { pub id: u16, - #[mt(size16)] + #[mt(size = "u16")] pub msg: ObjMsg, } #[mt_derive(to = "clt")] -pub struct ObjInitMsg(#[mt(size32)] ObjMsg); +pub struct ObjInitMsg(#[mt(size = "u32")] pub ObjMsg); #[mt_derive(to = "clt")] pub struct ObjInitData { - #[mt(const8 = 1)] // version + #[mt(const_before = "1u8")] // version pub name: String, pub is_player: bool, pub id: u16, pub pos: [f32; 3], pub rot: [f32; 3], pub hp: u16, - #[mt(len8)] + #[mt(len = "u8")] pub msgs: Vec, } #[mt_derive(to = "clt")] pub struct ObjAdd { pub id: u16, - #[mt(const8 = 101)] // GenericCAO - #[mt(size32)] + #[mt(const_before = "GENERIC_CAO")] + #[mt(size = "u32")] pub init_data: ObjInitData, } @@ -174,25 +175,14 @@ pub const CONTENT_IGNORE: u16 = 127; pub struct MapBlock { pub flags: EnumSet, pub lit_from: u16, - - #[mt(const8 = 2)] - #[serde(skip)] - pub param0_size: (), - - #[mt(const8 = 2)] - #[serde(skip)] - pub param12_size: (), - + #[mt(const_before = "2u8")] // param0 size + #[mt(const_before = "2u8")] // param1 size + param2 size #[serde(with = "serde_arrays")] pub param_0: [u16; 4096], #[serde(with = "serde_arrays")] pub param_1: [u8; 4096], #[serde(with = "serde_arrays")] pub param_2: [u8; 4096], - + #[mt(const_after = "2u8")] // version pub node_metas: HashMap, - - #[mt(const8 = 2)] - #[serde(skip)] - pub version: (), } diff --git a/src/to_clt/hud.rs b/src/to_clt/hud.rs index cf9e337..64574f7 100644 --- a/src/to_clt/hud.rs +++ b/src/to_clt/hud.rs @@ -87,7 +87,7 @@ pub enum HudFlag { #[mt_derive(to = "clt", repr = "u16", tag = "attribute", content = "value")] pub enum HotbarParam { - Size(#[mt(const16 = 4)] u32) = 0, + Size(#[mt(const_before = "4u16")] u32) = 0, Image(String), SelectionImage(String), } @@ -134,7 +134,7 @@ impl MtDeserialize for MinimapModesPkt { fn mt_deserialize(reader: &mut impl std::io::Read) -> Result { let len = DefCfg::read_len(reader)?; let current = MtDeserialize::mt_deserialize::(reader)?; - let modes = mt_ser::mt_deserialize_sized_seq(&len, reader)?.try_collect()?; + let modes = mt_ser::mt_deserialize_sized_seq::(&len, reader)?.try_collect()?; Ok(Self { current, modes }) } diff --git a/src/to_clt/media.rs b/src/to_clt/media.rs index 0dd1f3d..a6ecd32 100644 --- a/src/to_clt/media.rs +++ b/src/to_clt/media.rs @@ -1,18 +1,5 @@ use super::*; -#[mt_derive(to = "clt")] -pub struct MediaAnnounce { - pub name: String, - pub base64_sha1: String, -} - -#[mt_derive(to = "clt")] -pub struct MediaPayload { - pub name: String, - #[mt(len32)] - pub data: Vec, -} - #[mt_derive(to = "clt")] pub struct TileAnim; // TODO diff --git a/src/to_srv.rs b/src/to_srv.rs index 321138c..704acdb 100644 --- a/src/to_srv.rs +++ b/src/to_srv.rs @@ -36,36 +36,19 @@ pub enum Interaction { } #[mt_derive(to = "srv", repr = "u8", tag = "type")] -#[mt(const8 = 0)] // version +#[mt(const_before = "0u8")] // version pub enum PointedThing { None = 0, Node { under: [i16; 3], above: [i16; 3] }, Obj { obj: u16 }, } -#[mt_derive(to = "srv")] -pub struct String32(#[mt(len32)] pub String); - -impl std::ops::Deref for String32 { - type Target = String; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl std::ops::DerefMut for String32 { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 - } -} - #[mt_derive(to = "srv", repr = "u16", tag = "type", content = "data")] pub enum ToSrvPkt { Nil = 0, Init { serialize_version: u8, - #[mt(const16 = 1)] // supported compression + #[mt(const_before = "1u16")] // supported compression min_proto_version: u16, max_proto_version: u16, player_name: String, @@ -87,19 +70,19 @@ pub enum ToSrvPkt { } = 25, PlayerPos(PlayerPos) = 35, GotBlocks { - #[mt(len8)] + #[mt(len = "u8")] blocks: Vec<[i16; 3]>, } = 36, DeletedBlocks { - #[mt(len8)] + #[mt(len = "u8")] blocks: Vec<[i16; 3]>, } = 37, InvAction { - #[mt(len0)] + #[mt(len = "()")] action: String, } = 49, ChatMsg { - #[mt(utf16)] + #[mt(len = "Utf16")] msg: String, } = 50, FallDmg { @@ -112,7 +95,7 @@ pub enum ToSrvPkt { Interact { action: Interaction, item_slot: u16, - #[mt(size32)] + #[mt(size = "u32")] pointed: PointedThing, pos: PlayerPos, } = 57, @@ -122,11 +105,13 @@ pub enum ToSrvPkt { NodeMetaFields { pos: [i16; 3], formname: String, - fields: HashMap, + #[mt(len = "(DefCfg, (DefCfg, u32))")] + fields: HashMap, } = 59, InvFields { formname: String, - fields: HashMap, + #[mt(len = "(DefCfg, (DefCfg, u32))")] + fields: HashMap, } = 60, ReqMedia { filenames: Vec, -- 2.44.0