Skip to content

Commit

Permalink
Reflect a lot of things
Browse files Browse the repository at this point in the history
  • Loading branch information
Veritius committed Apr 17, 2024
1 parent a069a43 commit 3dc37bf
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 38 deletions.
11 changes: 8 additions & 3 deletions stardust/src/channels/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
//!
//! All settings are not definitive, but hints to transport layers as how to treat channels.
use bevy::reflect::Reflect;

#[cfg(feature="hashing")]
use {std::hash::Hasher, crate::hashing::StableHash};

/// Configuration for a channel.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Hash, Reflect)]
#[reflect(Debug, Hash)]
pub struct ChannelConfiguration {
/// Whether messages should be resent if they're missed.
pub reliable: ReliabilityGuarantee,
Expand Down Expand Up @@ -34,7 +37,8 @@ impl StableHash for &ChannelConfiguration {
}

/// The reliability guarantee of a channel.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect)]
#[reflect(Debug, PartialEq, Hash)]
pub enum ReliabilityGuarantee {
/// Messages are not guaranteed to arrive.
Unreliable,
Expand All @@ -54,7 +58,8 @@ impl StableHash for ReliabilityGuarantee {
}

/// The ordering guarantee of a channel.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect)]
#[reflect(Debug, PartialEq, Hash)]
pub enum OrderingGuarantee {
/// Messages will be available in the order they are received.
/// This is not necessarily the order they were sent. If that matters, use a different variant.
Expand Down
3 changes: 1 addition & 2 deletions stardust/src/channels/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ impl<C: Channel> Default for ChannelMarker<C> {
///
/// Channel identifiers are generated by the `ChannelRegistry` and are unique to the `World` they originated from.
/// Attempting to use a `ChannelId` in another `World` will probably panic, or give you unintended results.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature="reflect", derive(bevy::reflect::Reflect))]
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Reflect)]
#[repr(transparent)]
pub struct ChannelId(u32);

Expand Down
3 changes: 2 additions & 1 deletion stardust/src/connections/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use bevy::prelude::*;

/// Used to intentionally reduce the performance of peers for testing purposes.
/// If applied to a `NetworkPeer` entity, reduces performance for that peer specifically.
#[derive(Debug, Component)]
#[derive(Debug, Clone, Component, Reflect)]
#[reflect(Debug, Component)]
pub struct NetworkPerformanceReduction {
/// Chance to drop a packet when sending, if the transport is packet-based.
/// This chance is from `0.0` (never) to `1.0` (always), with `0.5` dropping 50% of the time.
Expand Down
3 changes: 2 additions & 1 deletion stardust/src/connections/groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ 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)]
#[derive(Debug, Component, Reflect)]
#[reflect(Debug, Component)]
pub struct NetworkGroup(pub(crate) SmallVec<[Entity; 8]>);

impl Default for NetworkGroup {
Expand Down
12 changes: 6 additions & 6 deletions stardust/src/connections/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use bevy::prelude::*;
/// - [`NetworkPeerUid`], relating to persistent data
/// - [`NetworkPeerLifestage`], relating to connection state
/// - [`SecurityLevel`](super::security::SecurityLevel), relating to encryption
#[derive(Debug, Component)]
#[cfg_attr(feature="reflect", derive(bevy::reflect::Reflect))]
#[derive(Debug, Component, Reflect)]
#[reflect(Debug, Component)]
pub struct NetworkPeer {
/// The point in time this peer was added to the `World`.
pub joined: Instant,
Expand Down Expand Up @@ -62,8 +62,8 @@ impl NetworkPeer {
///
/// This exists to model the average lifecycle of a connection, from an initial handshake to being disconnected.
/// An `Ord` implementation is provided, with variants being 'greater' if they're later in the model lifecycle.
#[derive(Debug, Component, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature="reflect", derive(bevy::reflect::Reflect))]
#[derive(Debug, Component, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Reflect)]
#[reflect(Debug, Component, PartialEq)]
#[non_exhaustive]
pub enum NetworkPeerLifestage {
/// Midway through a [handshake].
Expand All @@ -89,8 +89,8 @@ pub enum NetworkPeerLifestage {
///
/// If you're working with another ID namespace, like UUIDs and Steam IDs, you should
/// map the ids from that space into a unique value here through some kind of associative array.
#[derive(Component, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature="reflect", derive(bevy::reflect::Reflect))]
#[derive(Component, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Reflect)]
#[reflect(Debug, Component, PartialEq, Hash)]
pub struct NetworkPeerUid(pub u64);

impl std::fmt::Debug for NetworkPeerUid {
Expand Down
5 changes: 3 additions & 2 deletions stardust/src/connections/security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use bevy::prelude::*;
///
/// This value is set by the transport layer managing this peer.
/// It's up to it to provide an appropriate value here.
#[derive(Debug, Component, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature="reflect", derive(bevy::reflect::Reflect))]
#[derive(Debug, Component, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Reflect)]
#[reflect(Debug, Component, PartialEq)]
#[non_exhaustive]
pub enum NetworkSecurity {
/// Communication is encrypted but not authenticated, or is fully plain text.
///
Expand Down
29 changes: 9 additions & 20 deletions stardust/src/messages/direction.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::{any::Any, fmt::Debug};
use std::fmt::Debug;
use bevy::reflect::Reflect;

/// The direction a message is going, as an enum for dynamic use.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature="reflect", derive(bevy::reflect::Reflect))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Reflect)]
#[reflect(Debug, PartialEq, Hash)]
pub enum Direction {
/// Messages being sent to a remote peer.
Outgoing,
Expand All @@ -15,26 +16,14 @@ pub enum Direction {
/// Implemented by:
/// - [`Outgoing`], corresponding to [`Direction::Outgoing`]
/// - [`Incoming`], corresponding to [`Direction::Incoming`]
#[cfg(not(feature="reflect"))]
pub trait DirectionType: Debug + Send + Sync + Any + sealed::Sealed {
/// Returns the corresponding [`Direction`].
fn as_enum() -> Direction;
}

/// The direction a message is going, as a trait for use in the type system.
///
/// Implemented by:
/// - [`Outgoing`], corresponding to [`Direction::Outgoing`]
/// - [`Incoming`], corresponding to [`Direction::Incoming`]
#[cfg(feature="reflect")]
pub trait DirectionType: Debug + Send + Sync + Any + bevy::reflect::Reflect + sealed::Sealed {
pub trait DirectionType: Debug + Send + Sync + Reflect + sealed::Sealed {
/// Returns the corresponding [`Direction`].
fn as_enum() -> Direction;
}

/// Messages being sent to a remote peer. Counterpart to [`Incoming`].
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature="reflect", derive(bevy::reflect::Reflect))]
#[derive(Debug, Clone, Copy, Reflect)]
#[reflect(Debug)]
pub struct Outgoing;
impl DirectionType for Outgoing {
fn as_enum() -> Direction {
Expand All @@ -43,8 +32,8 @@ impl DirectionType for Outgoing {
}

/// Messages being received from a remote peer. Counterpart to [`Outgoing`].
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature="reflect", derive(bevy::reflect::Reflect))]
#[derive(Debug, Clone, Copy, Reflect)]
#[reflect(Debug)]
pub struct Incoming;
impl DirectionType for Incoming {
fn as_enum() -> Direction {
Expand Down
23 changes: 20 additions & 3 deletions stardust/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@ pub struct StardustPlugin;

impl Plugin for StardustPlugin {
fn build(&self, app: &mut App) {
// Register connection types
app.register_type::<NetworkPeer>();
app.register_type::<NetworkPeerUid>();
app.register_type::<NetworkGroup>();
app.register_type::<NetworkPeerLifestage>();
app.register_type::<NetworkSecurity>();
app.register_type::<NetworkPerformanceReduction>();

// Register channel types
app.register_type::<ChannelId>();
app.register_type::<ChannelConfiguration>();
app.register_type::<ReliabilityGuarantee>();
app.register_type::<OrderingGuarantee>();

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

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

// Add ChannelRegistryMut
app.insert_resource(ChannelRegistryMut(Box::new(ChannelRegistryInner::new())));

Expand All @@ -20,9 +40,6 @@ impl Plugin for StardustPlugin {
crate::messages::systems::clear_message_queue_system::<Incoming>,
).in_set(NetworkWrite::Clear));

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

// Hashing-related functionality
#[cfg(feature="hashing")] {
use crate::hashing::*;
Expand Down

0 comments on commit 3dc37bf

Please sign in to comment.