use bevy_connect::ClientId;
use bevy_ecs::message::Message;
use bevy_ecs::prelude::Component;
use bevy_ecs::prelude::ReflectComponent;
use bevy_ecs::prelude::Resource;
use bevy_reflect::{DynamicTypePath, FromReflect, GetTypeRegistration, Reflect, TypePath};
use bevy_state::state::States;
use serde::{Deserialize, Serialize};
pub use uuid::Uuid;
pub mod prelude {
pub use super::{
ClientState, ConnectCommand, DisconnectCommand, PromoteToHostCommand, ServerState,
SyncComponent, SyncConnectionParameters, SyncEntity, SyncExclude, SyncMark, SyncPlugin,
};
}
mod metrics;
mod binreflect;
mod bundle_fix;
mod client;
mod commands;
mod full_sync;
mod handle_uuid_fix;
mod lib_priv;
mod logging;
mod networking;
mod proto;
mod server;
mod track;
use std::{collections::HashSet, marker::PhantomData, net::IpAddr};
#[derive(Component, Reflect, Default)]
#[reflect(Component)]
pub struct SyncMark;
#[derive(Component)]
pub struct SyncEntity;
#[derive(Component, Default)]
pub struct SyncExclude<T: Component> {
marker: PhantomData<T>,
}
#[derive(Serialize, Deserialize, Debug, Resource, Clone)]
pub enum SyncConnectionParameters {
Direct {
ip: IpAddr,
port: u16,
asset_port: u16,
},
}
pub struct SyncPlugin;
#[derive(Debug, Clone, Eq, PartialEq, Hash, Default, States)]
pub enum ServerState {
Connected,
#[default]
Disconnected,
}
#[derive(Debug, Clone, Eq, PartialEq, Hash, Default, States)]
pub enum ClientState {
ConnectedInitialSync,
Connected,
#[default]
Disconnected,
}
#[derive(Message)]
pub struct InitialSyncFinished;
pub trait SyncComponent {
fn sync_component<
T: Component
+ TypePath
+ DynamicTypePath
+ Reflect
+ FromReflect
+ GetTypeRegistration
+ Clone,
>(
&mut self,
) -> &mut Self;
fn sync_materials(&mut self, enable: bool);
fn sync_meshes(&mut self, enable: bool);
fn sync_audios(&mut self, enable: bool);
}
pub struct ConnectCommand {
pub is_host: bool,
pub config: SyncConnectionParameters,
}
pub struct DisconnectCommand;
pub struct PromoteToHostCommand(pub ClientId, pub Option<u16>);
pub trait SyncWorld {
fn sync_host_uuid(&self) -> Option<ClientId>;
fn sync_self_uuid(&self) -> ClientId;
fn sync_all_client_uuids(&self) -> HashSet<ClientId>;
fn events_queue_count(&self) -> usize;
}
#[derive(Clone, Debug, Default, Resource)]
pub struct SyncStats {
total_sync_net_sent_prev: usize,
total_sync_net_read_prev: usize,
pub total_sync_net_sent: usize,
pub total_sync_net_read: usize,
total_asset_net_sent_prev: usize,
total_asset_net_read_prev: usize,
pub total_asset_net_sent: usize,
pub total_asset_net_read: usize,
}