You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

70 lines
1.7 KiB

use std::net::SocketAddr;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
pub struct ServerProperties {
#[serde(default = "default_name")]
pub name: String,
#[serde(default = "default_addr")]
pub addr: SocketAddr,
#[serde(default = "default_colors")]
pub use_colors: bool,
}
impl ServerProperties {
pub fn load() -> Self {
serde_json::from_str(&match std::fs::read_to_string("properties.json") {
Ok(v) => v,
Err(e) => {
println!("properties.json parsing error: {}", e);
std::process::exit(1)
}
})
.unwrap()
}
}
impl Default for ServerProperties {
fn default() -> Self {
Self {
name: default_name(),
addr: default_addr(),
use_colors: default_colors(),
}
}
}
const fn default_colors() -> bool {
true
}
fn default_addr() -> SocketAddr {
"0.0.0.0:50052".parse().unwrap()
}
fn default_name() -> String {
"Spah's sappin' mah sentreh".into()
}
pub fn setup() {
if cfg!(debug_assertions) {
if let Ok(e) = std::env::var("CARGO_MANIFEST_DIR") {
std::fs::write(
AsRef::<std::path::Path>::as_ref(&e)
.join("schema")
.join("server-properties.json"),
serde_json::to_string_pretty(&schemars::schema_for!(ServerProperties)).unwrap(),
)
.unwrap()
}
}
if !std::path::PathBuf::from("properties.json").exists() {
std::fs::write("properties.json", serde_json::to_string_pretty(&ServerProperties::default()).unwrap()).unwrap();
}
if !std::path::PathBuf::from("games").exists() {
std::fs::create_dir("games").unwrap();
}
}