-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathinit.rs
More file actions
162 lines (136 loc) · 4.67 KB
/
Copy pathinit.rs
File metadata and controls
162 lines (136 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::{cell::RefCell, rc::Rc, sync::Arc};
use anyhow::{anyhow, bail, Context};
use quill::world::{WorldDescriptor, WorldSettings};
use quill::{Game as _, WorldId};
use tokio::runtime::Runtime;
use crate::{config::Config, logging, Server};
use common::{Game, TickLoop};
use libcraft::biome::BiomeList;
use vane::SystemExecutor;
const CONFIG_PATH: &str = "config.toml";
pub fn create_game(runtime: Runtime) -> anyhow::Result<Game> {
let crate::config::ConfigContainer {
config,
was_config_created,
} = crate::config::load(CONFIG_PATH).context("failed to load configuration file")?;
logging::init(config.log.level);
if was_config_created {
log::info!("Created default config");
}
log::info!("Loaded config");
log::info!("Creating server");
let options = config.to_options();
let server = runtime.block_on(async move { Server::bind(options).await })?;
let mut game = init_game(server, &config, runtime)?;
game.insert_resource(config);
Ok(game)
}
pub fn run(game: Game) -> anyhow::Result<()> {
launch(game)
}
fn init_game(server: Server, _config: &Config, runtime: Runtime) -> anyhow::Result<Game> {
let mut game = Game::new(runtime);
init_systems(&mut game, server);
init_biomes(&mut game);
Ok(game)
}
fn init_systems(game: &mut Game, server: Server) {
let mut systems = SystemExecutor::new();
// Register common before server code, so
// that packet broadcasting happens after
// gameplay actions.
common::register(game, &mut systems);
server.link_with_game(game, &mut systems);
game.system_executor = Rc::new(RefCell::new(systems));
}
fn init_biomes(game: &mut Game) {
let biomes = Arc::new(BiomeList::vanilla());
game.insert_resource(biomes);
}
fn launch(mut game: Game) -> anyhow::Result<()> {
// World initialization must happen after plugin initialization
// so plugin world sources can be referenced in the `config.toml`.
init_worlds(&mut game)?;
let tick_loop = create_tick_loop(game);
log::debug!("Launching the game loop");
tick_loop.run();
Ok(())
}
fn init_worlds(game: &mut Game) -> anyhow::Result<()> {
let config = game.resources.get::<Config>()?.clone();
let dimension_types = libcraft::dimension::vanilla_dimensions();
let mut default_world_set = false;
for (world_name, world) in config.worlds {
let dimension_info = dimension_types
.iter()
.find(|dim| dim.r#type == world.dimension_type)
.ok_or_else(|| {
anyhow!(
"world '{}' has unknown dimension type '{}'",
world_name,
world.dimension_type
)
})?
.clone();
let id = WorldId::new_random(); // TODO persist
let source_factory = game
.world_source_factory(&world.source.typ)
.with_context(|| format!("unknown world source in world '{}'", world_name))?;
let source = source_factory
.create_world_source(
game,
&toml::Value::Table(world.source.params),
&dimension_info,
id,
)
.with_context(|| {
format!(
"failed to initialize world source for world '{}'",
world_name
)
})?;
let desc = WorldDescriptor {
id,
source,
name: Some(world_name.clone()),
dimension_info,
flat: world.flat,
settings: WorldSettings {
save_strategy: world.save_strategy.into(),
..Default::default()
},
};
game.create_world(desc);
if world_name == config.server.default_world {
game.set_default_world(id);
default_world_set = true;
}
}
if !default_world_set {
bail!(
"default world '{}' is not configured",
config.server.default_world
);
}
Ok(())
}
fn create_tick_loop(mut game: Game) -> TickLoop {
let (shutdown_sender, shutdown) = flume::bounded(1);
ctrlc::set_handler(move || {
shutdown_sender.send(()).ok();
})
.expect("failed to set shutdown handler");
TickLoop::new(move || {
let systems = Rc::clone(&game.system_executor);
systems.borrow_mut().run(&mut game);
game.tick_count += 1;
let should_shutdown = shutdown.try_recv().is_ok();
if should_shutdown {
log::info!("Server shutting down");
for mut world in game.worlds_mut() {
world.shutdown();
}
}
should_shutdown
})
}