Skip to content

Commit a403c1c

Browse files
feat(core): remove AddressModule, use observer (#737)
1 parent 92be6bc commit a403c1c

File tree

2 files changed

+28
-34
lines changed

2 files changed

+28
-34
lines changed

crates/hyperion/src/lib.rs

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use std::{
3232
cell::RefCell,
3333
fmt::Debug,
3434
io::Write,
35-
net::{SocketAddr, ToSocketAddrs},
35+
net::SocketAddr,
3636
sync::{Arc, atomic::AtomicBool},
3737
};
3838

@@ -144,15 +144,6 @@ pub fn adjust_file_descriptor_limits(recommended_min: u64) -> std::io::Result<()
144144
#[derive(Component, Debug, Clone, PartialEq, Eq, Hash, Constructor)]
145145
pub struct Address(SocketAddr);
146146

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-
156147
/// The central [`HyperionCore`] struct which owns and manages the entire server.
157148
#[derive(Component)]
158149
pub struct HyperionCore;
@@ -164,28 +155,21 @@ struct Shutdown {
164155

165156
impl Module for HyperionCore {
166157
fn module(world: &World) {
167-
let address = world.get::<&Address>(|address| address.0);
168-
Self::init_with(world, address).unwrap();
158+
Self::init_with(world).unwrap();
169159
}
170160
}
171161

172162
impl HyperionCore {
173163
/// Initializes the server with a custom handler.
174-
fn init_with(
175-
world: &World,
176-
address: impl ToSocketAddrs + Send + Sync + 'static,
177-
) -> anyhow::Result<()> {
164+
fn init_with(world: &World) -> anyhow::Result<()> {
178165
// Denormals (numbers very close to 0) are flushed to zero because doing computations on them
179166
// is slow.
180167

181-
no_denormals::no_denormals(|| Self::init_with_helper(world, address))
168+
no_denormals::no_denormals(|| Self::init_with_helper(world))
182169
}
183170

184171
/// Initialize the server.
185-
fn init_with_helper(
186-
world: &World,
187-
address: impl ToSocketAddrs + Send + Sync + 'static,
188-
) -> anyhow::Result<()> {
172+
fn init_with_helper(world: &World) -> anyhow::Result<()> {
189173
// 10k players * 2 file handles / player = 20,000. We can probably get away with 16,384 file handles
190174
#[cfg(unix)]
191175
adjust_file_descriptor_limits(32_768).context("failed to set file limits")?;
@@ -212,10 +196,7 @@ impl HyperionCore {
212196
.map_err(|_| anyhow::anyhow!("failed to create compression level"))?,
213197
});
214198

215-
let address = address
216-
.to_socket_addrs()?
217-
.next()
218-
.context("could not get first address")?;
199+
world.component::<Address>();
219200

220201
world.component::<Shutdown>();
221202
let shutdown = Arc::new(AtomicBool::new(false));
@@ -328,9 +309,18 @@ impl HyperionCore {
328309

329310
world.set(MojangClient::new(&runtime, ApiProvider::MAT_DOES_DEV));
330311

331-
let (receive_state, egress_comm) = init_proxy_comms(&runtime, address);
332-
333-
world.set(receive_state);
312+
#[rustfmt::skip]
313+
world
314+
.observer::<flecs::OnSet, (&Address, &AsyncRuntime)>()
315+
.term_at(0).singleton()
316+
.term_at(1).filter().singleton()
317+
.each_iter(|it, _, (address, runtime)| {
318+
let world = it.world();
319+
let address = address.0;
320+
let (receive_state, egress_comm) = init_proxy_comms(runtime, address);
321+
world.set(receive_state);
322+
world.set(egress_comm);
323+
});
334324

335325
let global = Global::new(shared.clone());
336326

@@ -348,8 +338,6 @@ impl HyperionCore {
348338
let events = Events::initialize(world);
349339
world.set(events);
350340

351-
world.set(egress_comm);
352-
353341
world.set(runtime);
354342
world.set(StreamLookup::default());
355343

events/tag/src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{collections::HashSet, net::SocketAddr};
77

88
use flecs_ecs::prelude::*;
99
use hyperion::{
10-
Address, AddressModule, HyperionCore,
10+
Address, HyperionCore,
1111
runtime::AsyncRuntime,
1212
simulation::{Player, blocks::Blocks},
1313
};
@@ -161,15 +161,21 @@ impl Module for TagModule {
161161
pub fn init_game(address: SocketAddr) -> anyhow::Result<()> {
162162
let world = World::new();
163163

164-
world.import::<AddressModule>();
165-
world.set(Address::new(address));
166-
167164
world.import::<HyperionCore>();
168165
world.import::<TagModule>();
169166

167+
world.set(Address::new(address));
168+
170169
// must be init last
171170
world.import::<SystemOrderModule>();
172171

172+
let mut app = world.app();
173+
174+
app.enable_rest(0)
175+
.enable_stats(true)
176+
.set_threads(i32::try_from(rayon::current_num_threads())?)
177+
.set_target_fps(20.0);
178+
173179
world.app().run();
174180

175181
Ok(())

0 commit comments

Comments
 (0)