diff --git a/src/client.rs b/src/client.rs index 4759e27f..bec0cc7d 100644 --- a/src/client.rs +++ b/src/client.rs @@ -7,6 +7,7 @@ use bevy::{ }; use bevy_renet::{renet::Bytes, transport::client_connected}; use bevy_renet::{renet::RenetClient, transport::NetcodeClientPlugin, RenetClientPlugin}; +use bincode::{DefaultOptions, Options}; use crate::{ replicon_core::{Mapper, NetworkTick, ReplicationRules, REPLICATION_CHANNEL_ID}, @@ -98,7 +99,7 @@ impl ClientPlugin { /// /// Returns true if [`LastTick`] has been updated. fn deserialize_tick(cursor: &mut Cursor, world: &mut World) -> Result { - let tick = bincode::deserialize_from(cursor)?; + let tick = DefaultOptions::new().deserialize_from(cursor)?; let mut last_tick = world.resource_mut::(); if last_tick.0 < tick { @@ -127,7 +128,7 @@ fn deserialize_component_diffs( let mut entity = entity_map.get_by_server_or_spawn(world, entity); let components_count: u8 = bincode::deserialize_from(&mut *cursor)?; for _ in 0..components_count { - let replication_id = bincode::deserialize_from(&mut *cursor)?; + let replication_id = DefaultOptions::new().deserialize_from(&mut *cursor)?; let replication_info = replication_rules.get_info(replication_id); match diff_kind { DiffKind::Change => { @@ -161,7 +162,7 @@ fn deserialize_despawns( // The entity might have already been deleted with the last diff, // but the server might not yet have received confirmation from the // client and could include the deletion in the latest diff. - let server_entity = bincode::deserialize_from(&mut *cursor)?; + let server_entity = DefaultOptions::new().deserialize_from(&mut *cursor)?; if let Some(client_entity) = entity_map.remove_by_server(server_entity) { world.entity_mut(client_entity).despawn_recursive(); } diff --git a/src/server.rs b/src/server.rs index f5bca3db..c740fcb1 100644 --- a/src/server.rs +++ b/src/server.rs @@ -19,6 +19,7 @@ use bevy_renet::{ transport::NetcodeServerPlugin, RenetServerPlugin, }; +use bincode::{DefaultOptions, Options}; use derive_more::Constructor; use crate::replicon_core::{ @@ -145,6 +146,7 @@ impl ServerPlugin { for buffer in buffers { debug_assert_eq!(buffer.array_len, 0); debug_assert_eq!(buffer.entity_map_len, 0); + println!("{:?}", buffer.message.get_ref().len()); set.p1().send_message( buffer.client_id, @@ -451,7 +453,7 @@ impl ReplicationBuffer { current_tick: NetworkTick, ) -> Result { let mut message = Default::default(); - bincode::serialize_into(&mut message, ¤t_tick)?; + DefaultOptions::new().serialize_into(&mut message, ¤t_tick)?; Ok(Self { client_id, system_tick, @@ -477,7 +479,7 @@ impl ReplicationBuffer { self.system_tick = system_tick; self.message.set_position(0); self.message.get_mut().clear(); - bincode::serialize_into(&mut self.message, ¤t_tick)?; + DefaultOptions::new().serialize_into(&mut self.message, ¤t_tick)?; Ok(()) } @@ -496,6 +498,7 @@ impl ReplicationBuffer { /// Ends writing array by writing its length into the last remembered position. /// + /// Length is written without varint encoding. /// See also [`Self::start_array`]. fn end_array(&mut self) -> Result<(), bincode::Error> { if self.array_len != 0 { @@ -529,6 +532,7 @@ impl ReplicationBuffer { /// Ends writing array by writing its length and associated [`Entity`] into the last remembered position. /// /// If map is empty, only map length will be written. + /// [`Entity`] and length are written without varint encoding. /// See also [`Self::start_array`]. fn end_entity_map(&mut self, entity: Entity) -> Result<(), bincode::Error> { if self.entity_map_len != 0 { @@ -561,7 +565,7 @@ impl ReplicationBuffer { replication_id: ReplicationId, ptr: Ptr, ) -> Result<(), bincode::Error> { - bincode::serialize_into(&mut self.message, &replication_id)?; + DefaultOptions::new().serialize_into(&mut self.message, &replication_id)?; (replication_info.serialize)(ptr, &mut self.message)?; self.entity_map_len += 1; @@ -572,7 +576,7 @@ impl ReplicationBuffer { /// /// Increases map length by 1. fn write_removal(&mut self, replication_id: ReplicationId) -> Result<(), bincode::Error> { - bincode::serialize_into(&mut self.message, &replication_id)?; + DefaultOptions::new().serialize_into(&mut self.message, &replication_id)?; self.entity_map_len += 1; Ok(()) @@ -582,7 +586,7 @@ impl ReplicationBuffer { /// /// Increases array length by 1. fn write_despawn(&mut self, entity: Entity) -> Result<(), bincode::Error> { - bincode::serialize_into(&mut self.message, &entity)?; + DefaultOptions::new().serialize_into(&mut self.message, &entity)?; self.array_len = self .array_len .checked_add(1)