naia_shared/world/component/
replicate.rs

1use std::{any::Any, collections::HashSet};
2
3use naia_serde::{BitReader, BitWrite, SerdeErr};
4
5use crate::{
6    messages::named::Named,
7    world::{
8        component::{
9            component_kinds::{ComponentKind, ComponentKinds},
10            component_update::ComponentUpdate,
11            diff_mask::DiffMask,
12            property_mutate::PropertyMutator,
13            replica_ref::{ReplicaDynMut, ReplicaDynRef},
14        },
15        delegation::auth_channel::EntityAuthAccessor,
16        entity::entity_converters::LocalEntityAndGlobalEntityConverter,
17    },
18    ComponentFieldUpdate, LocalEntityAndGlobalEntityConverterMut, RemoteEntity,
19};
20
21pub trait ReplicateBuilder: Send + Sync + Named {
22    /// Create new Component from incoming bit stream
23    fn read(
24        &self,
25        reader: &mut BitReader,
26        converter: &dyn LocalEntityAndGlobalEntityConverter,
27    ) -> Result<Box<dyn Replicate>, SerdeErr>;
28    /// Create new Component Update from incoming bit stream
29    fn read_create_update(&self, reader: &mut BitReader) -> Result<ComponentUpdate, SerdeErr>;
30    /// Split a Component update into Waiting and Ready updates
31    fn split_update(
32        &self,
33        converter: &dyn LocalEntityAndGlobalEntityConverter,
34        update: ComponentUpdate,
35    ) -> Result<
36        (
37            Option<Vec<(RemoteEntity, ComponentFieldUpdate)>>,
38            Option<ComponentUpdate>,
39        ),
40        SerdeErr,
41    >;
42}
43
44/// A struct that implements Replicate is a Component, or otherwise,
45/// a container of Properties that can be scoped, tracked, and synced, with a
46/// remote host
47pub trait Replicate: Sync + Send + 'static + Named + Any {
48    /// Gets the ComponentKind of this type
49    fn kind(&self) -> ComponentKind;
50    fn to_any(&self) -> &dyn Any;
51    fn to_any_mut(&mut self) -> &mut dyn Any;
52    fn to_boxed_any(self: Box<Self>) -> Box<dyn Any>;
53    fn copy_to_box(&self) -> Box<dyn Replicate>;
54    fn create_builder() -> Box<dyn ReplicateBuilder>
55    where
56        Self: Sized;
57    /// Gets the number of bytes of the Component's DiffMask
58    fn diff_mask_size(&self) -> u8;
59    /// Get an immutable reference to the inner Component as a Replicate trait object
60    fn dyn_ref(&self) -> ReplicaDynRef<'_>;
61    /// Get an mutable reference to the inner Component as a Replicate trait object
62    fn dyn_mut(&mut self) -> ReplicaDynMut<'_>;
63    /// Sets the current Component to the state of another Component of the
64    /// same type
65    fn mirror(&mut self, other: &dyn Replicate);
66    /// Set the Component's PropertyMutator, which keeps track
67    /// of which Properties have been mutated, necessary to sync only the
68    /// Properties that have changed with the client
69    fn set_mutator(&mut self, mutator: &PropertyMutator);
70    /// Writes data into an outgoing byte stream, sufficient to completely
71    /// recreate the Component on the client
72    fn write(
73        &self,
74        component_kinds: &ComponentKinds,
75        writer: &mut dyn BitWrite,
76        converter: &mut dyn LocalEntityAndGlobalEntityConverterMut,
77    );
78    /// Write data into an outgoing byte stream, sufficient only to update the
79    /// mutated Properties of the Component on the client
80    fn write_update(
81        &self,
82        diff_mask: &DiffMask,
83        writer: &mut dyn BitWrite,
84        converter: &mut dyn LocalEntityAndGlobalEntityConverterMut,
85    );
86    /// Reads data from an incoming packet, sufficient to sync the in-memory
87    /// Component with it's replica on the Server
88    fn read_apply_update(
89        &mut self,
90        converter: &dyn LocalEntityAndGlobalEntityConverter,
91        update: ComponentUpdate,
92    ) -> Result<(), SerdeErr>;
93    fn read_apply_field_update(
94        &mut self,
95        converter: &dyn LocalEntityAndGlobalEntityConverter,
96        update: ComponentFieldUpdate,
97    ) -> Result<(), SerdeErr>;
98    /// Returns a list of LocalEntities contained within the Component's EntityProperty fields, which are waiting to be converted to GlobalEntities
99    fn relations_waiting(&self) -> Option<HashSet<RemoteEntity>>;
100    /// Converts any LocalEntities contained within the Component's EntityProperty fields to GlobalEntities
101    fn relations_complete(&mut self, converter: &dyn LocalEntityAndGlobalEntityConverter);
102    /// Publish Replicate
103    fn publish(&mut self, mutator: &PropertyMutator);
104    /// Unpublish Replicate
105    fn unpublish(&mut self);
106    /// Enable Delegation Replicate
107    fn enable_delegation(
108        &mut self,
109        accessor: &EntityAuthAccessor,
110        mutator_opt: Option<&PropertyMutator>,
111    );
112    /// Disable Delegation Replicate
113    fn disable_delegation(&mut self);
114    /// Convert to Local Replicate
115    fn localize(&mut self);
116}
117
118cfg_if! {
119    if #[cfg(feature = "bevy_support")]
120    {
121        // Require that Bevy Component to be implemented
122        use bevy_ecs::component::Component;
123
124        pub trait ReplicatedComponent: Replicate + Component {}
125        impl<T: Replicate + Component> ReplicatedComponent for T {}
126    }
127    else
128    {
129        pub trait ReplicatedComponent: Replicate {}
130        impl<T: Replicate> ReplicatedComponent for T {}
131    }
132}