@@ -32,16 +32,16 @@ use std::{
3232 cell:: RefCell ,
3333 fmt:: Debug ,
3434 io:: Write ,
35- net:: ToSocketAddrs ,
35+ net:: { SocketAddr , ToSocketAddrs } ,
3636 sync:: { Arc , atomic:: AtomicBool } ,
3737} ;
3838
39- use anyhow:: { Context , bail } ;
40- use derive_more:: { Deref , DerefMut } ;
39+ use anyhow:: Context ;
40+ use derive_more:: { Constructor , Deref , DerefMut } ;
4141use egress:: EgressModule ;
4242use flecs_ecs:: prelude:: * ;
4343pub use glam;
44- use glam:: IVec2 ;
44+ use glam:: { I16Vec2 , IVec2 } ;
4545use ingress:: IngressModule ;
4646#[ cfg( unix) ]
4747use libc:: { RLIMIT_NOFILE , getrlimit, setrlimit} ;
@@ -69,7 +69,6 @@ use crate::{
6969mod common;
7070pub use common:: * ;
7171use hyperion_crafting:: CraftingRegistry ;
72- use system_order:: SystemOrderModule ;
7372pub use valence_ident;
7473
7574use crate :: {
@@ -142,27 +141,55 @@ pub fn adjust_file_descriptor_limits(recommended_min: u64) -> std::io::Result<()
142141 Ok ( ( ) )
143142}
144143
145- /// The central [`Hyperion`] struct which owns and manages the entire server.
146- pub struct Hyperion ;
144+ #[ derive( Component , Debug , Clone , PartialEq , Eq , Hash , Constructor ) ]
145+ pub struct Address ( SocketAddr ) ;
146+
147+ #[ derive( Component ) ]
148+ pub struct AddressModule ;
149+
150+ impl Module for AddressModule {
151+ fn module ( world : & World ) {
152+ world. component :: < Address > ( ) ;
153+ }
154+ }
155+
156+ /// The central [`HyperionCore`] struct which owns and manages the entire server.
157+ #[ derive( Component ) ]
158+ pub struct HyperionCore ;
147159
148160#[ derive( Component ) ]
149161struct Shutdown {
150162 value : Arc < AtomicBool > ,
151163}
152164
153- impl Hyperion {
154- /// Initializes the server.
155- pub fn init ( address : impl ToSocketAddrs + Send + Sync + ' static ) -> anyhow :: Result < ( ) > {
156- Self :: init_with ( address , |_| { } )
165+ impl Module for HyperionCore {
166+ fn module ( world : & World ) {
167+ let address = world . get :: < & Address > ( |address| address . 0 ) ;
168+ Self :: init_with ( world , address ) . unwrap ( ) ;
157169 }
170+ }
158171
172+ impl HyperionCore {
159173 /// Initializes the server with a custom handler.
160- pub fn init_with (
174+ fn init_with (
175+ world : & World ,
161176 address : impl ToSocketAddrs + Send + Sync + ' static ,
162- handlers : impl FnOnce ( & World ) + Send + Sync + ' static ,
163177 ) -> anyhow:: Result < ( ) > {
164178 // Denormals (numbers very close to 0) are flushed to zero because doing computations on them
165179 // is slow.
180+
181+ no_denormals:: no_denormals ( || Self :: init_with_helper ( world, address) )
182+ }
183+
184+ /// Initialize the server.
185+ fn init_with_helper (
186+ world : & World ,
187+ address : impl ToSocketAddrs + Send + Sync + ' static ,
188+ ) -> anyhow:: Result < ( ) > {
189+ // 10k players * 2 file handles / player = 20,000. We can probably get away with 16,384 file handles
190+ #[ cfg( unix) ]
191+ adjust_file_descriptor_limits ( 32_768 ) . context ( "failed to set file limits" ) ?;
192+
166193 rayon:: ThreadPoolBuilder :: new ( )
167194 . num_threads ( NUM_THREADS )
168195 . spawn_handler ( |thread| {
@@ -179,38 +206,12 @@ impl Hyperion {
179206 . build_global ( )
180207 . context ( "failed to build thread pool" ) ?;
181208
182- no_denormals:: no_denormals ( || Self :: init_with_helper ( address, handlers) )
183- }
184-
185- /// Initialize the server.
186- fn init_with_helper (
187- address : impl ToSocketAddrs + Send + Sync + ' static ,
188- handlers : impl FnOnce ( & World ) + Send + Sync + ' static ,
189- ) -> anyhow:: Result < ( ) > {
190- // 10k players * 2 file handles / player = 20,000. We can probably get away with 16,384 file handles
191- #[ cfg( unix) ]
192- adjust_file_descriptor_limits ( 32_768 ) . context ( "failed to set file limits" ) ?;
193-
194209 let shared = Arc :: new ( Shared {
195210 compression_threshold : CompressionThreshold ( 256 ) ,
196211 compression_level : CompressionLvl :: new ( 2 )
197212 . map_err ( |_| anyhow:: anyhow!( "failed to create compression level" ) ) ?,
198213 } ) ;
199214
200- let world = World :: new ( ) ;
201-
202- let world = Box :: new ( world) ;
203- let world: & World = Box :: leak ( world) ;
204-
205- let mut app = world. app ( ) ;
206-
207- app. enable_rest ( 0 )
208- . enable_stats ( true )
209- . set_threads ( i32:: try_from ( rayon:: current_num_threads ( ) ) ?)
210- . set_target_fps ( 20.0 ) ;
211-
212- world. set_threads ( i32:: try_from ( rayon:: current_num_threads ( ) ) ?) ;
213-
214215 let address = address
215216 . to_socket_addrs ( ) ?
216217 . next ( )
@@ -234,6 +235,8 @@ impl Hyperion {
234235 // .bit("glowing", *EntityFlags::GLOWING)
235236 // .bit("flying_with_elytra", *EntityFlags::FLYING_WITH_ELYTRA);
236237
238+ component ! ( world, I16Vec2 { x: i16 , y: i16 } ) ;
239+
237240 component ! ( world, IVec2 { x: i32 , y: i32 } ) ;
238241 world. component :: < PendingRemove > ( ) ;
239242
@@ -350,6 +353,15 @@ impl Hyperion {
350353 world. set ( runtime) ;
351354 world. set ( StreamLookup :: default ( ) ) ;
352355
356+ world. set_threads ( i32:: try_from ( rayon:: current_num_threads ( ) ) ?) ;
357+
358+ let mut app = world. app ( ) ;
359+
360+ app. enable_rest ( 0 )
361+ . enable_stats ( true )
362+ . set_threads ( i32:: try_from ( rayon:: current_num_threads ( ) ) ?)
363+ . set_target_fps ( 20.0 ) ;
364+
353365 world. import :: < SimModule > ( ) ;
354366 world. import :: < EgressModule > ( ) ;
355367 world. import :: < IngressModule > ( ) ;
@@ -377,14 +389,7 @@ impl Hyperion {
377389
378390 world. set ( IgnMap :: default ( ) ) ;
379391
380- handlers ( world) ;
381-
382- // must be init last
383- world. import :: < SystemOrderModule > ( ) ;
384-
385- app. run ( ) ;
386-
387- bail ! ( "app exited" ) ;
392+ Ok ( ( ) )
388393 }
389394}
390395
0 commit comments