From 5adaf5aa17b96974f243b75ee8ecb12c8239f8a8 Mon Sep 17 00:00:00 2001 From: lukas0008 Date: Fri, 11 Oct 2024 16:59:59 +0200 Subject: [PATCH 1/2] Add default values for config --- Cargo.lock | 12 +++++++ pumpkin-config/Cargo.toml | 1 + pumpkin-config/src/auth.rs | 51 ++++++++++++++++++++--------- pumpkin-config/src/commands.rs | 3 ++ pumpkin-config/src/compression.rs | 7 ++++ pumpkin-config/src/lib.rs | 29 +++++++++++++++- pumpkin-config/src/proxy.rs | 2 ++ pumpkin-config/src/pvp.rs | 7 ++++ pumpkin-config/src/rcon.rs | 12 ++++++- pumpkin-config/src/resource_pack.rs | 1 + 10 files changed, 108 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6ddf14516..73b534e16 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1925,6 +1925,7 @@ dependencies = [ "log", "pumpkin-core", "serde", + "serde-inline-default", "toml", ] @@ -2443,6 +2444,17 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde-inline-default" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "484b43bb1114a28d1a574f5682d6079fa4c20e76faaff0cfd048216650e57101" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "serde_bytes" version = "0.11.15" diff --git a/pumpkin-config/Cargo.toml b/pumpkin-config/Cargo.toml index d3ce22e43..5c2a8df09 100644 --- a/pumpkin-config/Cargo.toml +++ b/pumpkin-config/Cargo.toml @@ -9,3 +9,4 @@ serde.workspace = true log.workspace = true toml = "0.8" +serde-inline-default = "0.2.1" diff --git a/pumpkin-config/src/auth.rs b/pumpkin-config/src/auth.rs index c6606cd86..ad9d611b8 100644 --- a/pumpkin-config/src/auth.rs +++ b/pumpkin-config/src/auth.rs @@ -1,50 +1,78 @@ use pumpkin_core::ProfileAction; use serde::{Deserialize, Serialize}; +use serde_inline_default::serde_inline_default; +#[serde_inline_default] #[derive(Deserialize, Serialize)] pub struct AuthenticationConfig { /// Whether to use Mojang authentication. + #[serde_inline_default(true)] pub enabled: bool, /// Prevent proxy connections. + #[serde_inline_default(false)] pub prevent_proxy_connections: bool, /// Player profile handling. + #[serde(default)] pub player_profile: PlayerProfileConfig, /// Texture handling. + #[serde(default)] pub textures: TextureConfig, } +impl Default for AuthenticationConfig { + fn default() -> Self { + Self { + enabled: true, + prevent_proxy_connections: false, + player_profile: Default::default(), + textures: Default::default(), + } + } +} + #[derive(Deserialize, Serialize)] +#[serde(default)] pub struct PlayerProfileConfig { /// Allow players flagged by Mojang (banned, forced name change). pub allow_banned_players: bool, /// Depends on the value above + #[serde(default = "default_allowed_actions")] pub allowed_actions: Vec, } +fn default_allowed_actions() -> Vec { + vec![ + ProfileAction::ForcedNameChange, + ProfileAction::UsingBannedSkin, + ] +} + impl Default for PlayerProfileConfig { fn default() -> Self { Self { allow_banned_players: false, - allowed_actions: vec![ - ProfileAction::ForcedNameChange, - ProfileAction::UsingBannedSkin, - ], + allowed_actions: default_allowed_actions(), } } } +#[serde_inline_default] #[derive(Deserialize, Serialize)] pub struct TextureConfig { /// Whether to use player textures. + #[serde_inline_default(true)] pub enabled: bool, + #[serde_inline_default(vec!["http".into(), "https".into()])] pub allowed_url_schemes: Vec, + #[serde_inline_default(vec![".minecraft.net".into(), ".mojang.com".into()])] pub allowed_url_domains: Vec, /// Specific texture types. + #[serde(default)] pub types: TextureTypes, } @@ -60,13 +88,17 @@ impl Default for TextureConfig { } #[derive(Deserialize, Serialize)] +#[serde_inline_default] pub struct TextureTypes { /// Use player skins. + #[serde_inline_default(true)] pub skin: bool, /// Use player capes. + #[serde_inline_default(true)] pub cape: bool, /// Use player elytras. /// (i didn't know myself that there are custom elytras) + #[serde_inline_default(true)] pub elytra: bool, } @@ -79,14 +111,3 @@ impl Default for TextureTypes { } } } - -impl Default for AuthenticationConfig { - fn default() -> Self { - Self { - enabled: true, - prevent_proxy_connections: false, - player_profile: Default::default(), - textures: Default::default(), - } - } -} diff --git a/pumpkin-config/src/commands.rs b/pumpkin-config/src/commands.rs index 94ee4d8c2..cf20b064c 100644 --- a/pumpkin-config/src/commands.rs +++ b/pumpkin-config/src/commands.rs @@ -1,8 +1,11 @@ use serde::{Deserialize, Serialize}; +use serde_inline_default::serde_inline_default; #[derive(Deserialize, Serialize)] +#[serde_inline_default] pub struct CommandsConfig { /// Are commands from the Console accepted ? + #[serde_inline_default(true)] pub use_console: bool, // TODO: commands... } diff --git a/pumpkin-config/src/compression.rs b/pumpkin-config/src/compression.rs index 9bdd733d4..9b9b6b9d9 100644 --- a/pumpkin-config/src/compression.rs +++ b/pumpkin-config/src/compression.rs @@ -1,22 +1,29 @@ use serde::{Deserialize, Serialize}; +use serde_inline_default::serde_inline_default; +#[serde_inline_default] #[derive(Deserialize, Serialize)] /// Packet compression pub struct CompressionConfig { /// Is compression enabled ? + #[serde_inline_default(true)] pub enabled: bool, #[serde(flatten)] + #[serde(default)] pub compression_info: CompressionInfo, } +#[serde_inline_default] #[derive(Deserialize, Serialize, Clone)] /// We have this in a Seperate struct so we can use it outside of the Config pub struct CompressionInfo { /// The compression threshold used when compression is enabled + #[serde_inline_default(256)] pub threshold: u32, /// A value between 0..9 /// 1 = Optimize for the best speed of encoding. /// 9 = Optimize for the size of data being encoded. + #[serde_inline_default(4)] pub level: u32, } diff --git a/pumpkin-config/src/lib.rs b/pumpkin-config/src/lib.rs index a65caa444..d4ec18479 100644 --- a/pumpkin-config/src/lib.rs +++ b/pumpkin-config/src/lib.rs @@ -2,6 +2,9 @@ use log::warn; use pumpkin_core::{Difficulty, GameMode}; use serde::{de::DeserializeOwned, Deserialize, Serialize}; +// TODO: when https://github.com/rust-lang/rfcs/pull/3681 gets merged, replace serde-inline-default with native syntax +use serde_inline_default::serde_inline_default; + use std::{ fs, net::{Ipv4Addr, SocketAddr}, @@ -39,47 +42,71 @@ pub static BASIC_CONFIG: LazyLock = LazyLock::new(BasicConfi /// Important: The Configuration should match Vanilla by default #[derive(Deserialize, Serialize, Default)] pub struct AdvancedConfiguration { + #[serde(default)] pub proxy: ProxyConfig, + #[serde(default)] pub authentication: AuthenticationConfig, + #[serde(default)] pub packet_compression: CompressionConfig, + #[serde(default)] pub resource_pack: ResourcePackConfig, + #[serde(default)] pub commands: CommandsConfig, + #[serde(default)] pub rcon: RCONConfig, + #[serde(default)] pub pvp: PVPConfig, } +#[serde_inline_default] #[derive(Serialize, Deserialize)] pub struct BasicConfiguration { /// The address to bind the server to. + #[serde(default = "default_server_address")] pub server_address: SocketAddr, /// The seed for world generation. + #[serde(default = "String::new")] pub seed: String, /// The maximum number of players allowed on the server. + #[serde_inline_default(10000)] pub max_players: u32, /// The maximum view distance for players. + #[serde_inline_default(10)] pub view_distance: u8, /// The maximum simulated view distance. + #[serde_inline_default(10)] pub simulation_distance: u8, /// The default game difficulty. + #[serde_inline_default(Difficulty::Normal)] pub default_difficulty: Difficulty, /// Whether the Nether dimension is enabled. + #[serde_inline_default(true)] pub allow_nether: bool, /// Whether the server is in hardcore mode. + #[serde_inline_default(false)] pub hardcore: bool, /// Whether online mode is enabled. Requires valid Minecraft accounts. + #[serde_inline_default(true)] pub online_mode: bool, /// Whether packet encryption is enabled. Required when online mode is enabled. + #[serde_inline_default(true)] pub encryption: bool, /// The server's description displayed on the status screen. + #[serde_inline_default("A Blazing fast Pumpkin Server!".to_string())] pub motd: String, /// The default game mode for players. + #[serde_inline_default(GameMode::Survival)] pub default_gamemode: GameMode, } +fn default_server_address() -> SocketAddr { + SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), 25565) +} + impl Default for BasicConfiguration { fn default() -> Self { Self { - server_address: SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), 25565), + server_address: default_server_address(), seed: "".to_string(), max_players: 100000, view_distance: 10, diff --git a/pumpkin-config/src/proxy.rs b/pumpkin-config/src/proxy.rs index dba52f66e..1d1e44336 100644 --- a/pumpkin-config/src/proxy.rs +++ b/pumpkin-config/src/proxy.rs @@ -1,12 +1,14 @@ use serde::{Deserialize, Serialize}; #[derive(Deserialize, Serialize, Default)] +#[serde(default)] pub struct ProxyConfig { pub enabled: bool, pub velocity: VelocityConfig, } #[derive(Deserialize, Serialize)] +#[serde(default)] pub struct VelocityConfig { pub enabled: bool, pub secret: String, diff --git a/pumpkin-config/src/pvp.rs b/pumpkin-config/src/pvp.rs index cfbddc6c4..de5d9c70f 100644 --- a/pumpkin-config/src/pvp.rs +++ b/pumpkin-config/src/pvp.rs @@ -1,16 +1,23 @@ use serde::{Deserialize, Serialize}; +use serde_inline_default::serde_inline_default; +#[serde_inline_default] #[derive(Deserialize, Serialize)] pub struct PVPConfig { /// Is PVP enabled ? + #[serde_inline_default(true)] pub enabled: bool, /// Do we want to have the Red hurt animation & fov bobbing + #[serde_inline_default(true)] pub hurt_animation: bool, /// Should players in creative be protected against PVP + #[serde_inline_default(true)] pub protect_creative: bool, /// Has PVP Knockback? + #[serde_inline_default(true)] pub knockback: bool, /// Should player swing when attacking? + #[serde_inline_default(true)] pub swing: bool, } diff --git a/pumpkin-config/src/rcon.rs b/pumpkin-config/src/rcon.rs index f15fa655e..61d68a8d2 100644 --- a/pumpkin-config/src/rcon.rs +++ b/pumpkin-config/src/rcon.rs @@ -1,25 +1,35 @@ use std::net::{Ipv4Addr, SocketAddr}; use serde::{Deserialize, Serialize}; +use serde_inline_default::serde_inline_default; +#[serde_inline_default] #[derive(Deserialize, Serialize, Clone)] pub struct RCONConfig { /// Is RCON Enabled? + #[serde_inline_default(false)] pub enabled: bool, /// The network address and port where the RCON server will listen for connections. + #[serde(default = "default_rcon_address")] pub address: SocketAddr, /// The password required for RCON authentication. + #[serde(default)] pub password: String, /// The maximum number of concurrent RCON connections allowed. /// If 0 there is no limit + #[serde(default)] pub max_connections: u32, } +fn default_rcon_address() -> SocketAddr { + SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), 25575) +} + impl Default for RCONConfig { fn default() -> Self { Self { enabled: false, - address: SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), 25575), + address: default_rcon_address(), password: "".to_string(), max_connections: 0, } diff --git a/pumpkin-config/src/resource_pack.rs b/pumpkin-config/src/resource_pack.rs index 795595ada..f09b89455 100644 --- a/pumpkin-config/src/resource_pack.rs +++ b/pumpkin-config/src/resource_pack.rs @@ -1,6 +1,7 @@ use serde::{Deserialize, Serialize}; #[derive(Deserialize, Serialize)] +#[serde(default)] pub struct ResourcePackConfig { pub enabled: bool, /// The path to the resource pack. From 7db8c0ed97f17d888241b474ad1cdefd68af53be Mon Sep 17 00:00:00 2001 From: lukas0008 Date: Fri, 11 Oct 2024 17:12:35 +0200 Subject: [PATCH 2/2] Add serde defaults for logging --- pumpkin-config/src/commands.rs | 1 + pumpkin-config/src/lib.rs | 8 +------- pumpkin-config/src/logging.rs | 8 ++++++++ pumpkin-config/src/rcon.rs | 5 +++++ 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/pumpkin-config/src/commands.rs b/pumpkin-config/src/commands.rs index 2e975746d..936360818 100644 --- a/pumpkin-config/src/commands.rs +++ b/pumpkin-config/src/commands.rs @@ -8,6 +8,7 @@ pub struct CommandsConfig { #[serde_inline_default(true)] pub use_console: bool, /// Should be commands from players be logged in console? + #[serde_inline_default(true)] pub log_console: bool, // TODO: commands... } diff --git a/pumpkin-config/src/lib.rs b/pumpkin-config/src/lib.rs index 28e812c54..6062bb259 100644 --- a/pumpkin-config/src/lib.rs +++ b/pumpkin-config/src/lib.rs @@ -43,20 +43,14 @@ pub static BASIC_CONFIG: LazyLock = LazyLock::new(BasicConfi /// This also allows you get some Performance or Resource boosts. /// Important: The Configuration should match Vanilla by default #[derive(Deserialize, Serialize, Default)] +#[serde(default)] pub struct AdvancedConfiguration { - #[serde(default)] pub proxy: ProxyConfig, - #[serde(default)] pub authentication: AuthenticationConfig, - #[serde(default)] pub packet_compression: CompressionConfig, - #[serde(default)] pub resource_pack: ResourcePackConfig, - #[serde(default)] pub commands: CommandsConfig, - #[serde(default)] pub rcon: RCONConfig, - #[serde(default)] pub pvp: PVPConfig, pub logging: LoggingConfig, } diff --git a/pumpkin-config/src/logging.rs b/pumpkin-config/src/logging.rs index d6bcc9027..62991aa8c 100644 --- a/pumpkin-config/src/logging.rs +++ b/pumpkin-config/src/logging.rs @@ -1,12 +1,20 @@ use serde::{Deserialize, Serialize}; +use serde_inline_default::serde_inline_default; +#[serde_inline_default] #[derive(Deserialize, Serialize)] pub struct LoggingConfig { + #[serde_inline_default(true)] pub enabled: bool, + #[serde_inline_default(LevelFilter::Info)] pub level: LevelFilter, + #[serde_inline_default(false)] pub env: bool, + #[serde_inline_default(true)] pub threads: bool, + #[serde_inline_default(true)] pub color: bool, + #[serde_inline_default(true)] pub timestamp: bool, } diff --git a/pumpkin-config/src/rcon.rs b/pumpkin-config/src/rcon.rs index ccbb13436..bbffa53c9 100644 --- a/pumpkin-config/src/rcon.rs +++ b/pumpkin-config/src/rcon.rs @@ -23,15 +23,20 @@ pub struct RCONConfig { pub logging: RCONLogging, } +#[serde_inline_default] #[derive(Deserialize, Serialize, Clone, Debug)] pub struct RCONLogging { /// Whether successful RCON logins should be logged. + #[serde_inline_default(true)] pub log_logged_successfully: bool, /// Whether failed RCON login attempts with incorrect passwords should be logged. + #[serde_inline_default(true)] pub log_wrong_password: bool, /// Whether all RCON commands, regardless of success or failure, should be logged. + #[serde_inline_default(true)] pub log_commands: bool, /// Whether RCON quit commands should be logged. + #[serde_inline_default(true)] pub log_quit: bool, }