Skip to content

Commit

Permalink
More reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
Veritius committed Apr 17, 2024
1 parent 3dc37bf commit 588f981
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
8 changes: 1 addition & 7 deletions stardust/src/connections/groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@ use smallvec::SmallVec;
/// A collection of network peers, used for organisational purposes.
///
/// This can be used for anything, such as teams of players, rooms for replication, or administrative permissions.
#[derive(Debug, Component, Reflect)]
#[derive(Debug, Default, Component, Reflect)]
#[reflect(Debug, Component)]
pub struct NetworkGroup(pub(crate) SmallVec<[Entity; 8]>);

impl Default for NetworkGroup {
fn default() -> Self {
Self(SmallVec::default())
}
}

impl NetworkGroup {
/// Adds the peer to the network group.
/// Does nothing if the peer is already included.
Expand Down
2 changes: 1 addition & 1 deletion stardust/src/connections/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use bevy::prelude::*;
/// - [`NetworkMessages`](crate::messages::NetworkMessages), relating to messages
/// - [`NetworkPeerUid`], relating to persistent data
/// - [`NetworkPeerLifestage`], relating to connection state
/// - [`SecurityLevel`](super::security::SecurityLevel), relating to encryption
/// - [`NetworkSecurity`](super::security::NetworkSecurity), relating to encryption
#[derive(Debug, Component, Reflect)]
#[reflect(Debug, Component)]
pub struct NetworkPeer {
Expand Down
20 changes: 18 additions & 2 deletions stardust/src/messages/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ static EMPTY_SLICE: &[Bytes] = &[];
/// A queue-like structure for storing messages, separated by channels.
///
/// The items in this queue **do not** persist across frames.
/// They are cleared in [`NetworkWrite::Clear`].
#[derive(Component)]
/// They are cleared in [`NetworkWrite::Clear`] in [`PostUpdate`].
#[derive(Component, Reflect)]
#[reflect(Debug, Component)]
pub struct NetworkMessages<D: DirectionType> {
#[reflect(ignore)]
pub(crate) queue_map: HashMap<ChannelId, Vec<Bytes>>,
#[reflect(ignore)]
phantom: PhantomData<D>
}

Expand Down Expand Up @@ -63,4 +66,17 @@ impl<D: DirectionType> NetworkMessages<D> {
.filter(|(_,v)| v.len() != 0)
.map(|(k,v)| (k.clone(), v.as_slice()))
}
}

impl<D: DirectionType> Default for NetworkMessages<D> {
#[inline]
fn default() -> Self {
Self::new()
}
}

impl<D: DirectionType> std::fmt::Debug for NetworkMessages<D> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("NetworkMessages<{}>", std::any::type_name::<D>()))
}
}
4 changes: 4 additions & 0 deletions stardust/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ impl Plugin for StardustPlugin {

// Register messaging types
app.register_type::<Direction>();
app.register_type::<Incoming>();
app.register_type::<Outgoing>();
app.register_type::<NetworkMessages<Incoming>>();
app.register_type::<NetworkMessages<Outgoing>>();

// Setup orderings
crate::scheduling::configure_scheduling(app);
Expand Down

0 comments on commit 588f981

Please sign in to comment.