Add default values for config

This commit is contained in:
lukas0008 2024-10-11 16:59:59 +02:00
parent 9ac4bdbd50
commit 5adaf5aa17
10 changed files with 108 additions and 17 deletions

12
Cargo.lock generated
View File

@ -1925,6 +1925,7 @@ dependencies = [
"log", "log",
"pumpkin-core", "pumpkin-core",
"serde", "serde",
"serde-inline-default",
"toml", "toml",
] ]
@ -2443,6 +2444,17 @@ dependencies = [
"serde_derive", "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]] [[package]]
name = "serde_bytes" name = "serde_bytes"
version = "0.11.15" version = "0.11.15"

View File

@ -9,3 +9,4 @@ serde.workspace = true
log.workspace = true log.workspace = true
toml = "0.8" toml = "0.8"
serde-inline-default = "0.2.1"

View File

@ -1,50 +1,78 @@
use pumpkin_core::ProfileAction; use pumpkin_core::ProfileAction;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_inline_default::serde_inline_default;
#[serde_inline_default]
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
pub struct AuthenticationConfig { pub struct AuthenticationConfig {
/// Whether to use Mojang authentication. /// Whether to use Mojang authentication.
#[serde_inline_default(true)]
pub enabled: bool, pub enabled: bool,
/// Prevent proxy connections. /// Prevent proxy connections.
#[serde_inline_default(false)]
pub prevent_proxy_connections: bool, pub prevent_proxy_connections: bool,
/// Player profile handling. /// Player profile handling.
#[serde(default)]
pub player_profile: PlayerProfileConfig, pub player_profile: PlayerProfileConfig,
/// Texture handling. /// Texture handling.
#[serde(default)]
pub textures: TextureConfig, 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)] #[derive(Deserialize, Serialize)]
#[serde(default)]
pub struct PlayerProfileConfig { pub struct PlayerProfileConfig {
/// Allow players flagged by Mojang (banned, forced name change). /// Allow players flagged by Mojang (banned, forced name change).
pub allow_banned_players: bool, pub allow_banned_players: bool,
/// Depends on the value above /// Depends on the value above
#[serde(default = "default_allowed_actions")]
pub allowed_actions: Vec<ProfileAction>, pub allowed_actions: Vec<ProfileAction>,
} }
fn default_allowed_actions() -> Vec<ProfileAction> {
vec![
ProfileAction::ForcedNameChange,
ProfileAction::UsingBannedSkin,
]
}
impl Default for PlayerProfileConfig { impl Default for PlayerProfileConfig {
fn default() -> Self { fn default() -> Self {
Self { Self {
allow_banned_players: false, allow_banned_players: false,
allowed_actions: vec![ allowed_actions: default_allowed_actions(),
ProfileAction::ForcedNameChange,
ProfileAction::UsingBannedSkin,
],
} }
} }
} }
#[serde_inline_default]
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
pub struct TextureConfig { pub struct TextureConfig {
/// Whether to use player textures. /// Whether to use player textures.
#[serde_inline_default(true)]
pub enabled: bool, pub enabled: bool,
#[serde_inline_default(vec!["http".into(), "https".into()])]
pub allowed_url_schemes: Vec<String>, pub allowed_url_schemes: Vec<String>,
#[serde_inline_default(vec![".minecraft.net".into(), ".mojang.com".into()])]
pub allowed_url_domains: Vec<String>, pub allowed_url_domains: Vec<String>,
/// Specific texture types. /// Specific texture types.
#[serde(default)]
pub types: TextureTypes, pub types: TextureTypes,
} }
@ -60,13 +88,17 @@ impl Default for TextureConfig {
} }
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
#[serde_inline_default]
pub struct TextureTypes { pub struct TextureTypes {
/// Use player skins. /// Use player skins.
#[serde_inline_default(true)]
pub skin: bool, pub skin: bool,
/// Use player capes. /// Use player capes.
#[serde_inline_default(true)]
pub cape: bool, pub cape: bool,
/// Use player elytras. /// Use player elytras.
/// (i didn't know myself that there are custom elytras) /// (i didn't know myself that there are custom elytras)
#[serde_inline_default(true)]
pub elytra: bool, 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(),
}
}
}

View File

@ -1,8 +1,11 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_inline_default::serde_inline_default;
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
#[serde_inline_default]
pub struct CommandsConfig { pub struct CommandsConfig {
/// Are commands from the Console accepted ? /// Are commands from the Console accepted ?
#[serde_inline_default(true)]
pub use_console: bool, pub use_console: bool,
// TODO: commands... // TODO: commands...
} }

View File

@ -1,22 +1,29 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_inline_default::serde_inline_default;
#[serde_inline_default]
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
/// Packet compression /// Packet compression
pub struct CompressionConfig { pub struct CompressionConfig {
/// Is compression enabled ? /// Is compression enabled ?
#[serde_inline_default(true)]
pub enabled: bool, pub enabled: bool,
#[serde(flatten)] #[serde(flatten)]
#[serde(default)]
pub compression_info: CompressionInfo, pub compression_info: CompressionInfo,
} }
#[serde_inline_default]
#[derive(Deserialize, Serialize, Clone)] #[derive(Deserialize, Serialize, Clone)]
/// We have this in a Seperate struct so we can use it outside of the Config /// We have this in a Seperate struct so we can use it outside of the Config
pub struct CompressionInfo { pub struct CompressionInfo {
/// The compression threshold used when compression is enabled /// The compression threshold used when compression is enabled
#[serde_inline_default(256)]
pub threshold: u32, pub threshold: u32,
/// A value between 0..9 /// A value between 0..9
/// 1 = Optimize for the best speed of encoding. /// 1 = Optimize for the best speed of encoding.
/// 9 = Optimize for the size of data being encoded. /// 9 = Optimize for the size of data being encoded.
#[serde_inline_default(4)]
pub level: u32, pub level: u32,
} }

View File

@ -2,6 +2,9 @@ use log::warn;
use pumpkin_core::{Difficulty, GameMode}; use pumpkin_core::{Difficulty, GameMode};
use serde::{de::DeserializeOwned, Deserialize, Serialize}; 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::{ use std::{
fs, fs,
net::{Ipv4Addr, SocketAddr}, net::{Ipv4Addr, SocketAddr},
@ -39,47 +42,71 @@ pub static BASIC_CONFIG: LazyLock<BasicConfiguration> = LazyLock::new(BasicConfi
/// Important: The Configuration should match Vanilla by default /// Important: The Configuration should match Vanilla by default
#[derive(Deserialize, Serialize, Default)] #[derive(Deserialize, Serialize, Default)]
pub struct AdvancedConfiguration { pub struct AdvancedConfiguration {
#[serde(default)]
pub proxy: ProxyConfig, pub proxy: ProxyConfig,
#[serde(default)]
pub authentication: AuthenticationConfig, pub authentication: AuthenticationConfig,
#[serde(default)]
pub packet_compression: CompressionConfig, pub packet_compression: CompressionConfig,
#[serde(default)]
pub resource_pack: ResourcePackConfig, pub resource_pack: ResourcePackConfig,
#[serde(default)]
pub commands: CommandsConfig, pub commands: CommandsConfig,
#[serde(default)]
pub rcon: RCONConfig, pub rcon: RCONConfig,
#[serde(default)]
pub pvp: PVPConfig, pub pvp: PVPConfig,
} }
#[serde_inline_default]
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct BasicConfiguration { pub struct BasicConfiguration {
/// The address to bind the server to. /// The address to bind the server to.
#[serde(default = "default_server_address")]
pub server_address: SocketAddr, pub server_address: SocketAddr,
/// The seed for world generation. /// The seed for world generation.
#[serde(default = "String::new")]
pub seed: String, pub seed: String,
/// The maximum number of players allowed on the server. /// The maximum number of players allowed on the server.
#[serde_inline_default(10000)]
pub max_players: u32, pub max_players: u32,
/// The maximum view distance for players. /// The maximum view distance for players.
#[serde_inline_default(10)]
pub view_distance: u8, pub view_distance: u8,
/// The maximum simulated view distance. /// The maximum simulated view distance.
#[serde_inline_default(10)]
pub simulation_distance: u8, pub simulation_distance: u8,
/// The default game difficulty. /// The default game difficulty.
#[serde_inline_default(Difficulty::Normal)]
pub default_difficulty: Difficulty, pub default_difficulty: Difficulty,
/// Whether the Nether dimension is enabled. /// Whether the Nether dimension is enabled.
#[serde_inline_default(true)]
pub allow_nether: bool, pub allow_nether: bool,
/// Whether the server is in hardcore mode. /// Whether the server is in hardcore mode.
#[serde_inline_default(false)]
pub hardcore: bool, pub hardcore: bool,
/// Whether online mode is enabled. Requires valid Minecraft accounts. /// Whether online mode is enabled. Requires valid Minecraft accounts.
#[serde_inline_default(true)]
pub online_mode: bool, pub online_mode: bool,
/// Whether packet encryption is enabled. Required when online mode is enabled. /// Whether packet encryption is enabled. Required when online mode is enabled.
#[serde_inline_default(true)]
pub encryption: bool, pub encryption: bool,
/// The server's description displayed on the status screen. /// The server's description displayed on the status screen.
#[serde_inline_default("A Blazing fast Pumpkin Server!".to_string())]
pub motd: String, pub motd: String,
/// The default game mode for players. /// The default game mode for players.
#[serde_inline_default(GameMode::Survival)]
pub default_gamemode: GameMode, pub default_gamemode: GameMode,
} }
fn default_server_address() -> SocketAddr {
SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), 25565)
}
impl Default for BasicConfiguration { impl Default for BasicConfiguration {
fn default() -> Self { fn default() -> Self {
Self { Self {
server_address: SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), 25565), server_address: default_server_address(),
seed: "".to_string(), seed: "".to_string(),
max_players: 100000, max_players: 100000,
view_distance: 10, view_distance: 10,

View File

@ -1,12 +1,14 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, Default)] #[derive(Deserialize, Serialize, Default)]
#[serde(default)]
pub struct ProxyConfig { pub struct ProxyConfig {
pub enabled: bool, pub enabled: bool,
pub velocity: VelocityConfig, pub velocity: VelocityConfig,
} }
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
#[serde(default)]
pub struct VelocityConfig { pub struct VelocityConfig {
pub enabled: bool, pub enabled: bool,
pub secret: String, pub secret: String,

View File

@ -1,16 +1,23 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_inline_default::serde_inline_default;
#[serde_inline_default]
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
pub struct PVPConfig { pub struct PVPConfig {
/// Is PVP enabled ? /// Is PVP enabled ?
#[serde_inline_default(true)]
pub enabled: bool, pub enabled: bool,
/// Do we want to have the Red hurt animation & fov bobbing /// Do we want to have the Red hurt animation & fov bobbing
#[serde_inline_default(true)]
pub hurt_animation: bool, pub hurt_animation: bool,
/// Should players in creative be protected against PVP /// Should players in creative be protected against PVP
#[serde_inline_default(true)]
pub protect_creative: bool, pub protect_creative: bool,
/// Has PVP Knockback? /// Has PVP Knockback?
#[serde_inline_default(true)]
pub knockback: bool, pub knockback: bool,
/// Should player swing when attacking? /// Should player swing when attacking?
#[serde_inline_default(true)]
pub swing: bool, pub swing: bool,
} }

View File

@ -1,25 +1,35 @@
use std::net::{Ipv4Addr, SocketAddr}; use std::net::{Ipv4Addr, SocketAddr};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_inline_default::serde_inline_default;
#[serde_inline_default]
#[derive(Deserialize, Serialize, Clone)] #[derive(Deserialize, Serialize, Clone)]
pub struct RCONConfig { pub struct RCONConfig {
/// Is RCON Enabled? /// Is RCON Enabled?
#[serde_inline_default(false)]
pub enabled: bool, pub enabled: bool,
/// The network address and port where the RCON server will listen for connections. /// The network address and port where the RCON server will listen for connections.
#[serde(default = "default_rcon_address")]
pub address: SocketAddr, pub address: SocketAddr,
/// The password required for RCON authentication. /// The password required for RCON authentication.
#[serde(default)]
pub password: String, pub password: String,
/// The maximum number of concurrent RCON connections allowed. /// The maximum number of concurrent RCON connections allowed.
/// If 0 there is no limit /// If 0 there is no limit
#[serde(default)]
pub max_connections: u32, pub max_connections: u32,
} }
fn default_rcon_address() -> SocketAddr {
SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), 25575)
}
impl Default for RCONConfig { impl Default for RCONConfig {
fn default() -> Self { fn default() -> Self {
Self { Self {
enabled: false, enabled: false,
address: SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), 25575), address: default_rcon_address(),
password: "".to_string(), password: "".to_string(),
max_connections: 0, max_connections: 0,
} }

View File

@ -1,6 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
#[serde(default)]
pub struct ResourcePackConfig { pub struct ResourcePackConfig {
pub enabled: bool, pub enabled: bool,
/// The path to the resource pack. /// The path to the resource pack.