forked from Quozul/PicoLimbo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.rs
More file actions
129 lines (104 loc) · 3.87 KB
/
config.rs
File metadata and controls
129 lines (104 loc) · 3.87 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
use crate::configuration::boss_bar::BossBarConfig;
use crate::configuration::compression::CompressionConfig;
use crate::configuration::env_placeholders::expand_env_placeholders;
use crate::configuration::forwarding::ForwardingConfig;
use crate::configuration::game_mode_config::GameModeConfig;
use crate::configuration::server_list::ServerListConfig;
use crate::configuration::tab_list::TabListConfig;
use crate::configuration::title::TitleConfig;
use crate::configuration::world_config::WorldConfig;
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::{fs, io};
use thiserror::Error;
#[derive(Error, Debug)]
pub enum ConfigError {
#[error("I/O error: {0}")]
Io(#[from] io::Error),
#[error("TOML serialization error: {0}")]
TomlSerialize(#[from] toml::ser::Error),
#[error("TOML deserialization error: {0}")]
TomlDeserialize(#[from] toml::de::Error),
#[error("Missing environment variable: {0}")]
MissingEnvVar(String),
}
/// Application configuration, serializable to/from TOML.
#[derive(Serialize, Deserialize)]
#[serde(default)]
#[serde(deny_unknown_fields)]
#[allow(clippy::struct_excessive_bools)]
pub struct Config {
/// Server listening address and port.
///
/// Specify the IP address and port the server should bind to.
/// Use 0.0.0.0 to listen on all network interfaces.
pub bind: String,
pub forwarding: ForwardingConfig,
pub world: WorldConfig,
pub server_list: ServerListConfig,
/// Message sent to the player after spawning in the world.
pub welcome_message: String,
pub action_bar: String,
/// Sets the default game mode for players
/// Valid values are: "survival", "creative", "adventure" or "spectator"
pub default_game_mode: GameModeConfig,
/// If set to true, will spawn the player in hardcode mode
pub hardcore: bool,
pub compression: CompressionConfig,
pub tab_list: TabListConfig,
pub fetch_player_skins: bool,
pub reduced_debug_info: bool,
pub allow_unsupported_versions: bool,
pub allow_flight: bool,
pub boss_bar: BossBarConfig,
pub title: TitleConfig,
}
impl Default for Config {
fn default() -> Self {
Self {
bind: "0.0.0.0:25565".into(),
server_list: ServerListConfig::default(),
welcome_message: "Welcome to PicoLimbo!".into(),
action_bar: "Welcome to PicoLimbo!".into(),
forwarding: ForwardingConfig::default(),
default_game_mode: GameModeConfig::default(),
world: WorldConfig::default(),
hardcore: false,
tab_list: TabListConfig::default(),
fetch_player_skins: false,
reduced_debug_info: false,
boss_bar: BossBarConfig::default(),
compression: CompressionConfig::default(),
title: TitleConfig::default(),
allow_unsupported_versions: false,
allow_flight: false,
}
}
}
/// Loads a `Config` from the given path.
/// If the file does not exist, it will be created (parent dirs too)
/// and populated with default values.
pub fn load_or_create<P: AsRef<Path>>(path: P) -> Result<Config, ConfigError> {
let path = path.as_ref();
if path.exists() {
let raw_toml_str = fs::read_to_string(path)?;
if raw_toml_str.trim().is_empty() {
create_default_config(path)
} else {
let expanded_toml_str = expand_env_placeholders(&raw_toml_str)?;
let cfg: Config = toml::from_str(expanded_toml_str.as_ref())?;
Ok(cfg)
}
} else {
if let Some(dir) = path.parent() {
fs::create_dir_all(dir)?;
}
create_default_config(path)
}
}
fn create_default_config<P: AsRef<Path>>(path: P) -> Result<Config, ConfigError> {
let cfg = Config::default();
let toml_str = toml::to_string_pretty(&cfg)?;
fs::write(path, toml_str)?;
Ok(cfg)
}