From c8633b78d76b464323269ca04bd3b79dfc08cf13 Mon Sep 17 00:00:00 2001 From: Alexander Medvedev Date: Mon, 11 May 2026 16:59:45 +0200 Subject: [PATCH] chore: merge config files --- .gitignore | 3 +- pumpkin-config/src/lib.rs | 94 ++++++++++++++++++++++----------------- pumpkin/src/main.rs | 10 ++--- 3 files changed, 57 insertions(+), 50 deletions(-) diff --git a/.gitignore b/.gitignore index 2d8219f01..e372619b9 100644 --- a/.gitignore +++ b/.gitignore @@ -107,8 +107,7 @@ crash-reports/ data/* # project's configurations -configuration.toml -features.toml +pumpkin.toml *.local.nbt diff --git a/pumpkin-config/src/lib.rs b/pumpkin-config/src/lib.rs index 33be01e0b..279fe0d5d 100644 --- a/pumpkin-config/src/lib.rs +++ b/pumpkin-config/src/lib.rs @@ -45,6 +45,26 @@ use player_data::PlayerDataConfig; use resource_pack::ResourcePackConfig; use world::LevelConfig; +#[derive(Deserialize, Serialize, Default)] +#[serde(default)] +pub struct PumpkinConfig { + #[serde(flatten)] + pub basic: BasicConfiguration, + #[serde(flatten)] + pub advanced: AdvancedConfiguration, +} + +impl LoadConfiguration for PumpkinConfig { + fn get_path() -> &'static Path { + Path::new("pumpkin.toml") + } + + fn validate(&self) { + self.basic.validate(); + self.advanced.validate(); + } +} + /// Advanced configuration for optional and feature-specific server settings. /// /// Allows enabling/disabling features, customizing behaviour, and @@ -179,6 +199,38 @@ impl BasicConfiguration { pub fn get_world_path(&self) -> PathBuf { PathBuf::from(&self.default_level_name) } + + pub fn validate(&self) { + let min = NonZeroU8::new(2).unwrap(); + let max = NonZeroU8::new(64).unwrap(); + + assert!( + self.view_distance.ge(&min), + "View distance must be at least 2" + ); + assert!( + self.view_distance.le(&max), + "View distance must be less than 64" + ); + if self.online_mode { + assert!( + self.encryption, + "When online mode is enabled, encryption must be enabled" + ); + } + if self.allow_chat_reports { + assert!( + self.online_mode, + "When allow_chat_reports is enabled, online_mode must be enabled" + ); + } + } +} + +impl AdvancedConfiguration { + pub const fn validate(&self) { + //self.resource_pack.validate(); + } } /// Trait for loading and validating configuration from a TOML file. @@ -312,45 +364,3 @@ pub trait LoadConfiguration { /// Validates the configuration after loading or merging. fn validate(&self); } - -impl LoadConfiguration for AdvancedConfiguration { - fn get_path() -> &'static Path { - Path::new("features.toml") - } - - fn validate(&self) { - //self.resource_pack.validate(); - } -} - -impl LoadConfiguration for BasicConfiguration { - fn get_path() -> &'static Path { - Path::new("configuration.toml") - } - - fn validate(&self) { - let min = NonZeroU8::new(2).unwrap(); - let max = NonZeroU8::new(64).unwrap(); - - assert!( - self.view_distance.ge(&min), - "View distance must be at least 2" - ); - assert!( - self.view_distance.le(&max), - "View distance must be less than 64" - ); - if self.online_mode { - assert!( - self.encryption, - "When online mode is enabled, encryption must be enabled" - ); - } - if self.allow_chat_reports { - assert!( - self.online_mode, - "When allow_chat_reports is enabled, online_mode must be enabled" - ); - } - } -} diff --git a/pumpkin/src/main.rs b/pumpkin/src/main.rs index 23f15d687..b49a5d345 100644 --- a/pumpkin/src/main.rs +++ b/pumpkin/src/main.rs @@ -27,7 +27,7 @@ use pumpkin::{ }; use pumpkin::{LoggerOption, PumpkinServer, SHOULD_STOP, STOP_INTERRUPT, stop_server}; -use pumpkin_config::{AdvancedConfiguration, BasicConfiguration, LoadConfiguration}; +use pumpkin_config::{LoadConfiguration, PumpkinConfig}; use pumpkin_util::text::{ TextComponent, color::{Color, NamedColor}, @@ -75,14 +75,12 @@ async fn main() { let time = Instant::now(); let exec_dir = std::env::current_dir().unwrap(); - let config_dir = exec_dir.join("config"); - let basic_config = BasicConfiguration::load(&config_dir); - let advanced_config = AdvancedConfiguration::load(&config_dir); + let config = PumpkinConfig::load(&exec_dir); let vanilla_data = VanillaData::load(); - pumpkin::init_logger(&advanced_config); + pumpkin::init_logger(&config.advanced); info!( "{}", @@ -120,7 +118,7 @@ async fn main() { .expect("Unable to setup signal handlers"); }); - let pumpkin_server = PumpkinServer::new(basic_config, advanced_config, vanilla_data).await; + let pumpkin_server = PumpkinServer::new(config.basic, config.advanced, vanilla_data).await; let plugin_wait_time = pumpkin_server.init_plugins().await; let time_elapsed = time.elapsed().saturating_sub(plugin_wait_time);