mirror of
https://github.com/zyllian/classics.git
synced 2025-01-18 11:47:14 -08:00
make all config options default
This commit is contained in:
parent
e309a46cd3
commit
8dc89d959e
6 changed files with 73 additions and 31 deletions
32
Cargo.lock
generated
32
Cargo.lock
generated
|
@ -88,6 +88,7 @@ dependencies = [
|
||||||
"flate2",
|
"flate2",
|
||||||
"half",
|
"half",
|
||||||
"internment",
|
"internment",
|
||||||
|
"optional_struct",
|
||||||
"parking_lot",
|
"parking_lot",
|
||||||
"rand",
|
"rand",
|
||||||
"safer-bytes",
|
"safer-bytes",
|
||||||
|
@ -247,6 +248,37 @@ version = "1.19.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
|
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "optional_struct"
|
||||||
|
version = "0.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4cc83044fbb5cf2dfbf5bbe011f1ce6c68ce1c7f0c41423091e2c3b21c7ebb66"
|
||||||
|
dependencies = [
|
||||||
|
"optional_struct_export",
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "optional_struct_export"
|
||||||
|
version = "0.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3aab351802d85e41a5e679e6862c7639726920c7b93fbd2a7aec925509c088ed"
|
||||||
|
dependencies = [
|
||||||
|
"optional_struct_macro_impl",
|
||||||
|
"quote",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "optional_struct_macro_impl"
|
||||||
|
version = "0.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6d8ed6022d195dd0ab66d5d214eddbe887ef5489f708ff8be131296111d475ef"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "parking_lot"
|
name = "parking_lot"
|
||||||
version = "0.12.1"
|
version = "0.12.1"
|
||||||
|
|
|
@ -4,11 +4,12 @@ name = "classics"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bytes = "1.6.0"
|
bytes = "1"
|
||||||
flate2 = "1"
|
flate2 = "1"
|
||||||
half = "2"
|
half = "2"
|
||||||
internment = { version = "0.8", features = ["serde"] }
|
internment = {version = "0.8", features = ["serde"]}
|
||||||
parking_lot = "0.12.1"
|
optional_struct = "0.4"
|
||||||
|
parking_lot = "0.12"
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
safer-bytes = "0.2"
|
safer-bytes = "0.2"
|
||||||
serde = {version = "1", features = ["derive"]}
|
serde = {version = "1", features = ["derive"]}
|
||||||
|
|
|
@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
|
||||||
use super::{block::BLOCK_STRING_ID_MAP, Level};
|
use super::{block::BLOCK_STRING_ID_MAP, Level};
|
||||||
|
|
||||||
/// enum for different kinds of level generation
|
/// enum for different kinds of level generation
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub enum LevelGeneration {
|
pub enum LevelGeneration {
|
||||||
/// an empty level
|
/// an empty level
|
||||||
|
@ -17,7 +17,7 @@ pub enum LevelGeneration {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// enum for level presents
|
/// enum for level presents
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
#[serde(tag = "flat_type")]
|
#[serde(tag = "flat_type")]
|
||||||
pub enum FlatPreset {
|
pub enum FlatPreset {
|
||||||
/// the level is mostly stone, then dirt, then a layer of grass on the top
|
/// the level is mostly stone, then dirt, then a layer of grass on the top
|
||||||
|
@ -27,7 +27,7 @@ pub enum FlatPreset {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// description of a flat world's layer
|
/// description of a flat world's layer
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
pub struct FlatLayer {
|
pub struct FlatLayer {
|
||||||
/// the block for the layer
|
/// the block for the layer
|
||||||
pub block: String,
|
pub block: String,
|
||||||
|
|
13
src/main.rs
13
src/main.rs
|
@ -2,7 +2,10 @@
|
||||||
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use server::{config::ServerConfig, Server};
|
use server::{
|
||||||
|
config::{OptionalServerConfig, ServerConfig},
|
||||||
|
Server,
|
||||||
|
};
|
||||||
|
|
||||||
mod level;
|
mod level;
|
||||||
mod packet;
|
mod packet;
|
||||||
|
@ -15,16 +18,16 @@ const CONFIG_FILE: &str = "./server-config.json";
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
let config_path = PathBuf::from(CONFIG_FILE);
|
let config_path = PathBuf::from(CONFIG_FILE);
|
||||||
let config = if config_path.exists() {
|
let config = if config_path.exists() {
|
||||||
serde_json::from_str(&std::fs::read_to_string(config_path)?)
|
serde_json::from_str::<OptionalServerConfig>(&std::fs::read_to_string(&config_path)?)
|
||||||
.expect("failed to deserialize config")
|
.expect("failed to deserialize config")
|
||||||
|
.build_default()
|
||||||
} else {
|
} else {
|
||||||
let config = ServerConfig::default();
|
ServerConfig::default()
|
||||||
|
};
|
||||||
std::fs::write(
|
std::fs::write(
|
||||||
config_path,
|
config_path,
|
||||||
serde_json::to_string_pretty(&config).expect("failed to serialize default config"),
|
serde_json::to_string_pretty(&config).expect("failed to serialize default config"),
|
||||||
)?;
|
)?;
|
||||||
config
|
|
||||||
};
|
|
||||||
|
|
||||||
println!("starting server with config: {config:#?}");
|
println!("starting server with config: {config:#?}");
|
||||||
|
|
||||||
|
|
|
@ -9,8 +9,6 @@ use crate::{level::Level, player::Player};
|
||||||
|
|
||||||
use self::config::ServerConfig;
|
use self::config::ServerConfig;
|
||||||
|
|
||||||
const DEFAULT_SERVER_SIZE: usize = 128;
|
|
||||||
|
|
||||||
/// the server
|
/// the server
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Server {
|
pub struct Server {
|
||||||
|
@ -38,16 +36,11 @@ impl Server {
|
||||||
pub async fn new(config: ServerConfig) -> std::io::Result<Self> {
|
pub async fn new(config: ServerConfig) -> std::io::Result<Self> {
|
||||||
println!("generating level");
|
println!("generating level");
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
let (level_x, level_y, level_z) = if let Some(size) = &config.level_size {
|
let mut level = Level::new(
|
||||||
(size.x, size.y, size.z)
|
config.level_size.x,
|
||||||
} else {
|
config.level_size.y,
|
||||||
(
|
config.level_size.z,
|
||||||
DEFAULT_SERVER_SIZE,
|
);
|
||||||
DEFAULT_SERVER_SIZE,
|
|
||||||
DEFAULT_SERVER_SIZE,
|
|
||||||
)
|
|
||||||
};
|
|
||||||
let mut level = Level::new(level_x, level_y, level_z);
|
|
||||||
config.generation.generate(&mut level, &mut rng);
|
config.generation.generate(&mut level, &mut rng);
|
||||||
println!("done!");
|
println!("done!");
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
|
use optional_struct::optional_struct;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::level::generation::LevelGeneration;
|
use crate::level::generation::LevelGeneration;
|
||||||
|
|
||||||
/// configuration for the server
|
/// configuration for the server
|
||||||
|
#[optional_struct]
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct ServerConfig {
|
pub struct ServerConfig {
|
||||||
/// the server's name
|
/// the server's name
|
||||||
|
@ -15,28 +17,39 @@ pub struct ServerConfig {
|
||||||
#[serde(rename = "password")]
|
#[serde(rename = "password")]
|
||||||
pub protection_mode: ServerProtectionMode,
|
pub protection_mode: ServerProtectionMode,
|
||||||
/// the level's size
|
/// the level's size
|
||||||
pub level_size: Option<ConfigCoordinates>,
|
pub level_size: ConfigCoordinates,
|
||||||
/// the level's spawn point
|
/// the level's spawn point
|
||||||
pub spawn: Option<ConfigCoordinates>,
|
pub spawn: Option<ConfigCoordinates>,
|
||||||
/// the method to generate the server's level with
|
/// the method to generate the server's level with
|
||||||
pub generation: LevelGeneration,
|
pub generation: LevelGeneration,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl OptionalServerConfig {
|
||||||
|
/// builds the server config filling with default options
|
||||||
|
pub fn build_default(self) -> ServerConfig {
|
||||||
|
self.build(Default::default())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Default for ServerConfig {
|
impl Default for ServerConfig {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
name: "classic server wowie".to_string(),
|
name: "classic server wowie".to_string(),
|
||||||
motd: "here's the default server motd".to_string(),
|
motd: "here's the default server motd".to_string(),
|
||||||
protection_mode: ServerProtectionMode::None,
|
protection_mode: ServerProtectionMode::None,
|
||||||
level_size: None,
|
level_size: ConfigCoordinates {
|
||||||
|
x: 256,
|
||||||
|
y: 64,
|
||||||
|
z: 256,
|
||||||
|
},
|
||||||
spawn: None,
|
spawn: None,
|
||||||
generation: LevelGeneration::Empty,
|
generation: LevelGeneration::Flat(crate::level::generation::FlatPreset::StoneAndGrass),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// coordinates as stored in configuration
|
/// coordinates as stored in configuration
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
pub struct ConfigCoordinates {
|
pub struct ConfigCoordinates {
|
||||||
/// the X coordinate
|
/// the X coordinate
|
||||||
pub x: usize,
|
pub x: usize,
|
||||||
|
@ -47,7 +60,7 @@ pub struct ConfigCoordinates {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// enum for the different kinds of server protection
|
/// enum for the different kinds of server protection
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
pub enum ServerProtectionMode {
|
pub enum ServerProtectionMode {
|
||||||
/// the server is unprotected and anyone can join with any username
|
/// the server is unprotected and anyone can join with any username
|
||||||
|
|
Loading…
Add table
Reference in a new issue