diff options
Diffstat (limited to 'src/server.rs')
-rw-r--r-- | src/server.rs | 61 |
1 files changed, 41 insertions, 20 deletions
diff --git a/src/server.rs b/src/server.rs index f099f26..d9b1c1a 100644 --- a/src/server.rs +++ b/src/server.rs @@ -645,7 +645,29 @@ pub struct Server { endpoint: Option<Endpoint>, } +impl FromWorld for Server { + fn from_world(world: &mut World) -> Self { + if world.get_resource::<AsyncRuntime>().is_none() { + let async_runtime = tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .unwrap(); + world.insert_resource(AsyncRuntime(async_runtime)); + }; + + let runtime = world.resource::<AsyncRuntime>(); + Server::new(runtime.handle().clone()) + } +} + impl Server { + fn new(runtime: tokio::runtime::Handle) -> Self { + Self { + endpoint: None, + runtime, + } + } + pub fn endpoint(&self) -> &Endpoint { self.endpoint.as_ref().unwrap() } @@ -871,13 +893,6 @@ async fn client_connection_task( } } -fn create_server(mut commands: Commands, runtime: Res<AsyncRuntime>) { - commands.insert_resource(Server { - endpoint: None, - runtime: runtime.handle().clone(), - }); -} - // Receive messages from the async server tasks and update the sync server. fn update_sync_server( mut server: ResMut<Server>, @@ -924,28 +939,34 @@ fn update_sync_server( } } -pub struct QuinnetServerPlugin {} +pub struct QuinnetServerPlugin { + /// In order to have more control and only do the strict necessary, which is registering systems and events in the Bevy schedule, `initialize_later` can be set to `true`. This will prevent the plugin from initializing the `Server` Resource. + /// Server systems are scheduled to only run if the `Server` resource exists. + /// A Bevy command to create the resource `commands.init_resource::<Server>();` can be done later on, when needed. + pub initialize_later: bool, +} impl Default for QuinnetServerPlugin { fn default() -> Self { - Self {} + Self { + initialize_later: false, + } } } impl Plugin for QuinnetServerPlugin { fn build(&self, app: &mut App) { app.add_event::<ConnectionEvent>() - .add_event::<ConnectionLostEvent>() - .add_startup_system_to_stage(StartupStage::PreStartup, create_server) - .add_system_to_stage(CoreStage::PreUpdate, update_sync_server); - - if app.world.get_resource_mut::<AsyncRuntime>().is_none() { - app.insert_resource(AsyncRuntime( - tokio::runtime::Builder::new_multi_thread() - .enable_all() - .build() - .unwrap(), - )); + .add_event::<ConnectionLostEvent>(); + + if !self.initialize_later { + app.init_resource::<Server>(); } + + app.add_system( + update_sync_server + .in_base_set(CoreSet::PreUpdate) + .run_if(resource_exists::<Server>()), + ); } } |