Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganize core #351

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- All `TestFnsEntityExt` now accept `FnsId`.
- Move replication-related modules from `core` module under `core::replication`.
- Move `Replicated` to the `replication` module.
- Split the `ctx` module and move event-related contexts under `core::events_registry::ctx` and replication-related contexts under `core::replication_registry::ctx`.

### Removed

Expand Down
14 changes: 9 additions & 5 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ use integer_encoding::VarIntReader;

use crate::core::{
channels::{ReplicationChannel, RepliconChannels},
command_markers::{CommandMarkers, EntityMarkers},
common_conditions::{client_connected, client_just_connected, client_just_disconnected},
ctx::{DespawnCtx, RemoveCtx, WriteCtx},
deferred_entity::DeferredEntity,
replication_registry::ReplicationRegistry,
replication::{
command_markers::{CommandMarkers, EntityMarkers},
deferred_entity::DeferredEntity,
replication_registry::{
ctx::{DespawnCtx, RemoveCtx, WriteCtx},
ReplicationRegistry,
},
Replicated,
},
replicon_client::RepliconClient,
replicon_tick::RepliconTick,
server_entity_map::ServerEntityMap,
Replicated,
};
use confirm_history::ConfirmHistory;

Expand Down
6 changes: 4 additions & 2 deletions src/client/events.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use super::{ClientPlugin, ClientSet, ServerInitTick};
use crate::core::{
common_conditions::*,
ctx::{ClientReceiveCtx, ClientSendCtx},
event_registry::EventRegistry,
event_registry::{
ctx::{ClientReceiveCtx, ClientSendCtx},
EventRegistry,
},
replicon_client::RepliconClient,
server_entity_map::ServerEntityMap,
};
Expand Down
22 changes: 5 additions & 17 deletions src/core.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
pub mod channels;
pub mod command_markers;
pub mod common_conditions;
pub mod connected_clients;
pub mod ctx;
pub mod deferred_entity;
pub mod event_registry;
pub mod replicated_clients;
pub mod replication_registry;
pub mod replication_rules;
pub mod replication;
pub mod replicon_client;
pub mod replicon_server;
pub mod replicon_tick;
Expand All @@ -17,10 +12,11 @@ use bevy::prelude::*;
use serde::{Deserialize, Serialize};

use channels::RepliconChannels;
use command_markers::CommandMarkers;
use event_registry::EventRegistry;
use replication_registry::ReplicationRegistry;
use replication_rules::ReplicationRules;
use replication::{
command_markers::CommandMarkers, replication_registry::ReplicationRegistry,
replication_rules::ReplicationRules, Replicated,
};

pub struct RepliconCorePlugin;

Expand All @@ -35,14 +31,6 @@ impl Plugin for RepliconCorePlugin {
}
}

#[deprecated(note = "use `Replicated` instead")]
pub type Replication = Replicated;

/// Marks entity for replication.
#[derive(Component, Clone, Copy, Default, Reflect, Debug)]
#[reflect(Component)]
pub struct Replicated;

/// Unique client ID.
///
/// Could be a client or a dual server-client.
Expand Down
2 changes: 1 addition & 1 deletion src/core/connected_clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::core::ClientId;
///
/// Inserted as resource by [`ServerPlugin`](crate::server::ServerPlugin).
///
/// See also [ReplicatedClients](super::replicated_clients::ReplicatedClients).
/// See also [ReplicatedClients](super::replication::replicated_clients::ReplicatedClients).
#[derive(Resource, Default, Deref)]
pub struct ConnectedClients(Vec<ClientId>);

Expand Down
1 change: 1 addition & 0 deletions src/core/event_registry.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub(crate) mod client_event;
pub mod ctx;
pub(crate) mod server_event;

use bevy::{ecs::component::ComponentId, prelude::*};
Expand Down
8 changes: 5 additions & 3 deletions src/core/event_registry/client_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ use bevy::{
use bincode::{DefaultOptions, Options};
use serde::{de::DeserializeOwned, Serialize};

use super::EventRegistry;
use super::{
ctx::{ClientSendCtx, ServerReceiveCtx},
EventRegistry,
};
use crate::core::{
channels::{RepliconChannel, RepliconChannels},
ctx::{ClientSendCtx, ServerReceiveCtx},
replicon_client::RepliconClient,
replicon_server::RepliconServer,
ClientId,
Expand Down Expand Up @@ -77,7 +79,7 @@ pub trait ClientEventAppExt {
reflect::serde::{ReflectSerializer, ReflectDeserializer},
};
use bevy_replicon::{
core::ctx::{ClientSendCtx, ServerReceiveCtx},
core::event_registry::ctx::{ClientSendCtx, ServerReceiveCtx},
prelude::*,
};
use bincode::{DefaultOptions, Options};
Expand Down
63 changes: 63 additions & 0 deletions src/core/event_registry/ctx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use bevy::{prelude::*, reflect::TypeRegistry};

use crate::core::server_entity_map::ServerEntityMap;

/// Event sending context for client.
#[non_exhaustive]
pub struct ClientSendCtx<'a> {
/// Registry of reflected types.
pub registry: &'a TypeRegistry,

/// Maps server entities to client entities and vice versa.
pub entity_map: &'a ServerEntityMap,
}

impl EntityMapper for ClientSendCtx<'_> {
fn map_entity(&mut self, entity: Entity) -> Entity {
*self
.entity_map
.to_server()
.get(&entity)
.unwrap_or_else(|| panic!("client {entity:?} should have a mapping"))
}
}

/// Event receiving context for server.
#[non_exhaustive]
pub struct ServerReceiveCtx<'a> {
/// Registry of reflected types.
pub registry: &'a TypeRegistry,
}

/// Event sending context for server.
#[non_exhaustive]
pub struct ServerSendCtx<'a> {
/// Registry of reflected types.
pub registry: &'a TypeRegistry,
}

/// Event receiving context for client.
#[non_exhaustive]
pub struct ClientReceiveCtx<'a> {
/// Registry of reflected types.
pub registry: &'a TypeRegistry,

/// Maps server entities to client entities and vice versa.
pub entity_map: &'a ServerEntityMap,

/// Entities that couldn't be mapped by [`EntityMapper::map_entity`].
///
/// We needed it because [`EntityMapper`] doesn't provide a way to handle errors.
pub(crate) invalid_entities: Vec<Entity>,
}

impl EntityMapper for ClientReceiveCtx<'_> {
fn map_entity(&mut self, entity: Entity) -> Entity {
if let Some(mapped_entity) = self.entity_map.to_client().get(&entity) {
*mapped_entity
} else {
self.invalid_entities.push(entity);
Entity::PLACEHOLDER
}
}
}
10 changes: 6 additions & 4 deletions src/core/event_registry/server_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ use bytes::Bytes;
use ordered_multimap::ListOrderedMultimap;
use serde::{de::DeserializeOwned, Serialize};

use super::EventRegistry;
use super::{
ctx::{ClientReceiveCtx, ServerSendCtx},
EventRegistry,
};
use crate::{
core::{
channels::{RepliconChannel, RepliconChannels},
connected_clients::ConnectedClients,
ctx::{ClientReceiveCtx, ServerSendCtx},
replicated_clients::ReplicatedClients,
replication::replicated_clients::ReplicatedClients,
replicon_client::RepliconClient,
replicon_server::RepliconServer,
replicon_tick::RepliconTick,
Expand Down Expand Up @@ -84,7 +86,7 @@ pub trait ServerEventAppExt {
reflect::serde::{ReflectSerializer, ReflectDeserializer},
};
use bevy_replicon::{
core::ctx::{ClientReceiveCtx, ServerSendCtx},
core::event_registry::ctx::{ClientReceiveCtx, ServerSendCtx},
prelude::*,
};
use bincode::{DefaultOptions, Options};
Expand Down
15 changes: 15 additions & 0 deletions src/core/replication.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pub mod command_markers;
pub mod deferred_entity;
pub mod replicated_clients;
pub mod replication_registry;
pub mod replication_rules;

use bevy::prelude::*;

#[deprecated(note = "use `Replicated` instead")]
pub type Replication = Replicated;

/// Marks entity for replication.
#[derive(Component, Clone, Copy, Default, Reflect, Debug)]
#[reflect(Component)]
pub struct Replicated;
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ use std::cmp::Reverse;

use bevy::{ecs::component::ComponentId, prelude::*};

use super::replication_registry::command_fns::{RemoveFn, WriteFn};
use crate::core::replication_registry::ReplicationRegistry;
use super::replication_registry::{
command_fns::{RemoveFn, WriteFn},
ReplicationRegistry,
};

/// Marker-based functions for [`App`].
///
Expand Down Expand Up @@ -50,10 +52,14 @@ pub trait AppMarkerExt {
use bevy::{ecs::system::EntityCommands, prelude::*, utils::HashMap};
use bevy_replicon::{
core::{
command_markers::MarkerConfig,
deferred_entity::DeferredEntity,
ctx::{RemoveCtx, WriteCtx},
replication_registry::rule_fns::RuleFns,
replication::{
command_markers::MarkerConfig,
deferred_entity::DeferredEntity,
replication_registry::{
ctx::{RemoveCtx, WriteCtx},
rule_fns::RuleFns,
},
},
replicon_tick::RepliconTick,
},
prelude::*,
Expand Down Expand Up @@ -174,7 +180,7 @@ impl CommandMarkers {
///
/// May invalidate previously returned [`CommandMarkerIndex`] due to sorting.
///
/// Use [`ReplicationFns::register_marker`] to register a slot for command functions for this marker.
/// Use [`ReplicationRegistry::register_marker`] to register a slot for command functions for this marker.
fn insert(&mut self, marker: CommandMarker) -> CommandMarkerIndex {
let key = Reverse(marker.config.priority);
let index = self
Expand Down Expand Up @@ -295,7 +301,7 @@ mod tests {
use serde::{Deserialize, Serialize};

use super::*;
use crate::core::replication_registry::{command_fns, ReplicationRegistry};
use crate::core::replication::replication_registry::command_fns;

#[test]
#[should_panic]
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use client_visibility::ClientVisibility;
///
/// Inserted as resource by [`ServerPlugin`](crate::server::ServerPlugin).
///
/// See also [ConnectedClients](super::connected_clients::ConnectedClients).
/// See also [ConnectedClients](crate::core::connected_clients::ConnectedClients).
#[derive(Resource, Default)]
pub struct ReplicatedClients {
clients: Vec<ReplicatedClient>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
pub mod command_fns;
pub mod component_fns;
pub mod ctx;
pub mod rule_fns;
pub mod test_fns;

use bevy::{ecs::component::ComponentId, prelude::*};
use serde::{Deserialize, Serialize};

use super::{command_markers::CommandMarkerIndex, ctx::DespawnCtx};
use super::command_markers::CommandMarkerIndex;
use command_fns::{RemoveFn, UntypedCommandFns, WriteFn};
use component_fns::ComponentFns;
use ctx::DespawnCtx;
use rule_fns::{RuleFns, UntypedRuleFns};

/// Stores configurable replication functions.
Expand Down Expand Up @@ -156,12 +158,12 @@ impl Default for ReplicationRegistry {
}
}

#[deprecated(note = "use `Replicated` instead")]
#[deprecated(note = "use `ReplicationRegistry` instead")]
pub type ReplicationFns = ReplicationRegistry;

/// ID of replicaton functions for a component.
///
/// Can be obtained from [`ReplicationFns::register_rule_fns`].
/// Can be obtained from [`ReplicationRegistry::register_rule_fns`].
#[derive(Clone, Copy, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct FnsId(usize);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use std::{

use bevy::prelude::*;

use super::rule_fns::RuleFns;
use crate::core::{
use super::{
ctx::{RemoveCtx, WriteCtx},
deferred_entity::DeferredEntity,
rule_fns::RuleFns,
};
use crate::core::replication::deferred_entity::DeferredEntity;

/// Writing and removal functions for a component, like [`Commands`].
#[derive(Clone, Copy)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ use std::io::Cursor;

use bevy::{prelude::*, ptr::Ptr};

use super::{command_fns::UntypedCommandFns, rule_fns::UntypedRuleFns};
use crate::core::{
command_markers::{CommandMarkerIndex, CommandMarkers, EntityMarkers},
use super::{
command_fns::UntypedCommandFns,
ctx::{RemoveCtx, SerializeCtx, WriteCtx},
rule_fns::UntypedRuleFns,
};
use crate::core::replication::{
command_markers::{CommandMarkerIndex, CommandMarkers, EntityMarkers},
deferred_entity::DeferredEntity,
};

Expand Down
Loading