@@ -32,16 +32,16 @@ use std::{
32
32
cell:: RefCell ,
33
33
fmt:: Debug ,
34
34
io:: Write ,
35
- net:: ToSocketAddrs ,
35
+ net:: { SocketAddr , ToSocketAddrs } ,
36
36
sync:: { Arc , atomic:: AtomicBool } ,
37
37
} ;
38
38
39
- use anyhow:: { Context , bail } ;
40
- use derive_more:: { Deref , DerefMut } ;
39
+ use anyhow:: Context ;
40
+ use derive_more:: { Constructor , Deref , DerefMut } ;
41
41
use egress:: EgressModule ;
42
42
use flecs_ecs:: prelude:: * ;
43
43
pub use glam;
44
- use glam:: IVec2 ;
44
+ use glam:: { I16Vec2 , IVec2 } ;
45
45
use ingress:: IngressModule ;
46
46
#[ cfg( unix) ]
47
47
use libc:: { RLIMIT_NOFILE , getrlimit, setrlimit} ;
@@ -69,7 +69,6 @@ use crate::{
69
69
mod common;
70
70
pub use common:: * ;
71
71
use hyperion_crafting:: CraftingRegistry ;
72
- use system_order:: SystemOrderModule ;
73
72
pub use valence_ident;
74
73
75
74
use crate :: {
@@ -142,27 +141,55 @@ pub fn adjust_file_descriptor_limits(recommended_min: u64) -> std::io::Result<()
142
141
Ok ( ( ) )
143
142
}
144
143
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 ;
147
159
148
160
#[ derive( Component ) ]
149
161
struct Shutdown {
150
162
value : Arc < AtomicBool > ,
151
163
}
152
164
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 ( ) ;
157
169
}
170
+ }
158
171
172
+ impl HyperionCore {
159
173
/// Initializes the server with a custom handler.
160
- pub fn init_with (
174
+ fn init_with (
175
+ world : & World ,
161
176
address : impl ToSocketAddrs + Send + Sync + ' static ,
162
- handlers : impl FnOnce ( & World ) + Send + Sync + ' static ,
163
177
) -> anyhow:: Result < ( ) > {
164
178
// Denormals (numbers very close to 0) are flushed to zero because doing computations on them
165
179
// 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
+
166
193
rayon:: ThreadPoolBuilder :: new ( )
167
194
. num_threads ( NUM_THREADS )
168
195
. spawn_handler ( |thread| {
@@ -179,38 +206,12 @@ impl Hyperion {
179
206
. build_global ( )
180
207
. context ( "failed to build thread pool" ) ?;
181
208
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
-
194
209
let shared = Arc :: new ( Shared {
195
210
compression_threshold : CompressionThreshold ( 256 ) ,
196
211
compression_level : CompressionLvl :: new ( 2 )
197
212
. map_err ( |_| anyhow:: anyhow!( "failed to create compression level" ) ) ?,
198
213
} ) ;
199
214
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
-
214
215
let address = address
215
216
. to_socket_addrs ( ) ?
216
217
. next ( )
@@ -234,6 +235,8 @@ impl Hyperion {
234
235
// .bit("glowing", *EntityFlags::GLOWING)
235
236
// .bit("flying_with_elytra", *EntityFlags::FLYING_WITH_ELYTRA);
236
237
238
+ component ! ( world, I16Vec2 { x: i16 , y: i16 } ) ;
239
+
237
240
component ! ( world, IVec2 { x: i32 , y: i32 } ) ;
238
241
world. component :: < PendingRemove > ( ) ;
239
242
@@ -350,6 +353,15 @@ impl Hyperion {
350
353
world. set ( runtime) ;
351
354
world. set ( StreamLookup :: default ( ) ) ;
352
355
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
+
353
365
world. import :: < SimModule > ( ) ;
354
366
world. import :: < EgressModule > ( ) ;
355
367
world. import :: < IngressModule > ( ) ;
@@ -377,14 +389,7 @@ impl Hyperion {
377
389
378
390
world. set ( IgnMap :: default ( ) ) ;
379
391
380
- handlers ( world) ;
381
-
382
- // must be init last
383
- world. import :: < SystemOrderModule > ( ) ;
384
-
385
- app. run ( ) ;
386
-
387
- bail ! ( "app exited" ) ;
392
+ Ok ( ( ) )
388
393
}
389
394
}
390
395
0 commit comments