From ac67d6386a4ea06d9a87ce249c1b6251aeeeffa1 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 15 Aug 2020 13:16:04 +0100 Subject: [PATCH] much EH --- server.toml | 1 + src/bin/otter.rs | 2 +- src/bin/server.rs | 11 +++++++++++ src/global.rs | 8 +++++--- 4 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 server.toml diff --git a/server.toml b/server.toml new file mode 100644 index 00000000..628406dd --- /dev/null +++ b/server.toml @@ -0,0 +1 @@ +save_directory = "/home/ian/Rustup/Game/server/save" diff --git a/src/bin/otter.rs b/src/bin/otter.rs index dff3f85a..1eaa972d 100644 --- a/src/bin/otter.rs +++ b/src/bin/otter.rs @@ -240,7 +240,7 @@ mod create_table { f.read_to_string(&mut buf).context("read")?; let spec : TableSpec = toml::de::from_str(&buf).context("parse")?; >::Ok(spec) - })().context("game spec toml").with_context(|| args.file.to_owned())?; + })().with_context(|| args.file.to_owned()).context("read game spec")?; let _chan = connect(&ma)?; diff --git a/src/bin/server.rs b/src/bin/server.rs index 88e7b9cb..e014a764 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -68,8 +68,19 @@ fn resource(leaf : CheckedResourceLeaf) -> io::Result { NamedFile::open(format!("{}/{}", template_dir, leaf.safe)) } +const DEFAULT_CONFIG_FILENAME : &str = "server.toml"; + #[throws(StartupError)] fn main() { + { + let config_filename = env::args().nth(1) + .unwrap_or(DEFAULT_CONFIG_FILENAME.to_owned()); + let mut buf = String::new(); + File::open(config_filename)?.read_to_string(&mut buf)?; + let config = toml::de::from_str(&buf)?; + set_config(config); + }; + xxx_global_setup().expect("global setup failed"); let cl = CommandListener::new()?; diff --git a/src/global.rs b/src/global.rs index 82cd5ae1..6cd882da 100644 --- a/src/global.rs +++ b/src/global.rs @@ -811,11 +811,13 @@ pub fn config() -> Arc { GLOBAL.config.read().unwrap().clone() } +pub fn set_config(config: ServerConfig) { + *GLOBAL.config.write().unwrap() = Arc::new(config) +} + impl Default for ServerConfig { fn default() -> ServerConfig { - ServerConfig { - save_directory: DEFAULT_SAVE_DIRECTORY.to_owned(), - } + toml::de::from_str("").expect("parse empty string as ServerConfig") } } -- 2.30.2