diff --git a/server/Cargo.toml b/server/Cargo.toml index 3c6ba83..8069561 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -5,3 +5,4 @@ edition = "2021" [dependencies] polariton = { version = "0.1.0", path = "../" } +log = "0.4" diff --git a/server/src/operations/handler.rs b/server/src/operations/handler.rs index 2d4493a..d303f96 100644 --- a/server/src/operations/handler.rs +++ b/server/src/operations/handler.rs @@ -46,7 +46,7 @@ impl OperationsHandler { } pub fn with_state + OperationCode + 'static>(mut self, op: O, state: S) -> Self { - self.op_map.insert(O::op_code(), Box::new(SyncedOpWrapper { + self.insert_op(O::op_code(), Box::new(SyncedOpWrapper { state: Mutex::new(state), handler: op, })); @@ -54,12 +54,18 @@ impl OperationsHandler { } pub fn without_state + OperationCode + 'static>(mut self, op: O) -> Self { - self.op_map.insert(O::op_code(), Box::new(StatelessOpWrapper { + self.insert_op(O::op_code(), Box::new(StatelessOpWrapper { handler: op, })); self } + fn insert_op(&mut self, code: u8, handler: Box>) { + if self.op_map.insert(code, handler).is_some() { + log::warn!("Replacing operation for code {}", code); + } + } + fn try_pass_to_handler(&self, code: u8, params: ParameterTable, user:&U) -> OperationResponse { if let Some(handler) = self.op_map.get(&code) { handler.handle(params, user) diff --git a/src/operation/mod.rs b/src/operation/mod.rs index b1bcbf3..f80bd5f 100644 --- a/src/operation/mod.rs +++ b/src/operation/mod.rs @@ -24,3 +24,6 @@ pub use type_prefixed::Typed; mod typed_dict; pub use typed_dict::Dict; + +mod typed_arr; +pub use typed_arr::Arr; diff --git a/src/operation/type_prefixed.rs b/src/operation/type_prefixed.rs index 02e6e65..6b7da60 100644 --- a/src/operation/type_prefixed.rs +++ b/src/operation/type_prefixed.rs @@ -41,7 +41,7 @@ pub enum Typed { #[brw(magic = 120u8)] Bytes(super::SizePrefixedVec), #[brw(magic = 121u8)] - Arr(super::SizePrefixedVec), + Arr(super::Arr), #[brw(magic = 122u8)] ObjArr(super::SizePrefixedVec), } diff --git a/src/operation/typed_arr.rs b/src/operation/typed_arr.rs new file mode 100644 index 0000000..e5e32a5 --- /dev/null +++ b/src/operation/typed_arr.rs @@ -0,0 +1,49 @@ +use binrw::{BinRead, BinWrite}; + +use super::Typed; + +#[derive(Debug, Clone)] +pub struct Arr { + pub ty: u8, + pub items: Vec, +} + +impl BinRead for Arr { + type Args<'a> = (); + + fn read_options( + reader: &mut R, + endian: binrw::Endian, + args: Self::Args<'_>, + ) -> binrw::BinResult { + let len = i16::read_options(reader, endian, args)?; + let ty = u8::read_options(reader, endian, args)?; + let mut items = Vec::with_capacity(len as _); + for _ in 0..len { + let item = Typed::with_magic(ty, reader)?; + + items.push(item); + } + Ok(Self { + ty, items, + }) + } +} + +impl BinWrite for Arr { + type Args<'a> = (); + + fn write_options( + &self, + writer: &mut W, + endian: binrw::Endian, + args: Self::Args<'_>, + ) -> binrw::BinResult<()> { + (self.items.len() as i16).write_options(writer, endian, args)?; + self.ty.write_options(writer, endian, args)?; + for item in &self.items { + item.write_inner(writer)?; + } + Ok(()) + } +}