Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
cBournhonesque committed Jan 29, 2025
1 parent 2a774b4 commit 6d0c64f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
44 changes: 26 additions & 18 deletions lightyear/src/protocol/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,28 +576,29 @@ mod interpolation {
}

mod replication {
use std::alloc::Layout;
use super::*;
use crate::prelude::{
DeltaCompression, OverrideTargetComponent, ReplicateOnceComponent,
};
use crate::prelude::{DeltaCompression, OverrideTargetComponent, ReplicateOnceComponent};
use crate::serialize::reader::Reader;
use crate::serialize::ToBytes;
use crate::shared::replication::entity_map::ReceiveEntityMap;
use bevy::ptr::OwningPtr;
use bytes::Bytes;
use std::alloc::Layout;

type EntityHashMap<K, V> = hashbrown::HashMap<K, V, EntityHash>;

impl ComponentRegistry {

/// Store the component's raw bytes into a temporary buffer so that we can get an OwningPtr to it
/// This function is called for all components that will be added to an entity, so that we can
/// insert them all at once using `entity_world_mut.insert_by_ids`
///
/// SAFETY:
/// - the component C must match the `component_id `
pub(crate) unsafe fn buffer_insert_raw_ptrs<C: Component>(&mut self, mut component: C, component_id: ComponentId) {
pub(crate) unsafe fn buffer_insert_raw_ptrs<C: Component>(
&mut self,
mut component: C,
component_id: ComponentId,
) {
let layout = Layout::new::<C>();
let ptr = NonNull::new_unchecked(&mut component).cast::<u8>();
// make sure the Drop trait is not called when the `component` variable goes out of scope
Expand Down Expand Up @@ -731,7 +732,6 @@ mod replication {
Ok(*kind)
}


/// Method that buffers a pointer to the component data that will be inserted
/// in the entity inside `self.raw_bytes`
pub(crate) fn buffer_insert<C: Component + PartialEq>(
Expand Down Expand Up @@ -767,23 +767,25 @@ mod replication {
"replication::receive::component::{}::update",
std::any::type_name::<C>()
))
.increment(1);
.increment(1);
}
events.push_update_component(entity, net_id, tick);
*c = component;
}
} else {
// TODO: add safety comment
unsafe { self.buffer_insert_raw_ptrs::<C>(component, replication_metadata.component_id) };
unsafe {
self.buffer_insert_raw_ptrs::<C>(component, replication_metadata.component_id)
};
// TODO: should we send the event based on on the message type (Insert/Update) or based on whether the component was actually inserted?
#[cfg(feature = "metrics")]
{
metrics::counter!("replication::receive::component::insert").increment(1);
metrics::counter!(format!(
"replication::receive::component::{}::insert",
std::any::type_name::<C>()
))
.increment(1);
"replication::receive::component::{}::insert",
std::any::type_name::<C>()
))
.increment(1);
}
events.push_insert_component(entity, net_id, tick);
}
Expand Down Expand Up @@ -876,8 +878,10 @@ mod delta {

impl ComponentRegistry {
/// Register delta compression functions for a component
pub(crate) fn set_delta_compression<C: Component + PartialEq + Diffable>(&mut self, world: &mut World)
where
pub(crate) fn set_delta_compression<C: Component + PartialEq + Diffable>(
&mut self,
world: &mut World,
) where
C::Delta: Serialize + DeserializeOwned,
{
let kind = ComponentKind::of::<C>();
Expand Down Expand Up @@ -1115,7 +1119,12 @@ mod delta {
} else {
// TODO: add safety comment
// use the component id of C, not DeltaMessage<C>
unsafe { self.buffer_insert_raw_ptrs::<C>(new_value, replication_metadata.component_id) };
unsafe {
self.buffer_insert_raw_ptrs::<C>(
new_value,
replication_metadata.component_id,
)
};
events.push_insert_component(entity, net_id, tick);
}
// store the component value in the delta component history, so that we can compute
Expand Down Expand Up @@ -1470,11 +1479,10 @@ impl AppComponentExt for App {
where
C::Delta: Serialize + DeserializeOwned,
{

self.world_mut()
.resource_scope(|world, mut registry: Mut<ComponentRegistry>| {
registry.set_delta_compression::<C>(world);
})
})
}
}

Expand Down
1 change: 0 additions & 1 deletion lightyear/src/protocol/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ unsafe fn erased_serialize_fn<M: Message>(
}
}


/// Default serialize function using bincode
fn default_serialize<M: Message + Serialize>(
message: &M,
Expand Down
2 changes: 1 addition & 1 deletion lightyear/src/shared/replication/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ impl DeltaComponentStore {

#[cfg(test)]
mod tests {
use bevy::prelude::World;
use super::*;
use crate::tests::protocol::ComponentDeltaCompression;
use bevy::prelude::World;

#[test]
fn test_add_get_data() {
Expand Down

0 comments on commit 6d0c64f

Please sign in to comment.