Skip to content

Commit

Permalink
Merge pull request #112 from Snowiiii/config-serde-defaults
Browse files Browse the repository at this point in the history
Add default values for config
  • Loading branch information
Snowiiii authored Oct 11, 2024
2 parents e0180ef + 7db8c0e commit 5d86ba5
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 17 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pumpkin-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ serde.workspace = true
log.workspace = true

toml = "0.8"
serde-inline-default = "0.2.1"
51 changes: 36 additions & 15 deletions pumpkin-config/src/auth.rs
Original file line number Diff line number Diff line change
@@ -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<ProfileAction>,
}

fn default_allowed_actions() -> Vec<ProfileAction> {
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<String>,
#[serde_inline_default(vec![".minecraft.net".into(), ".mojang.com".into()])]
pub allowed_url_domains: Vec<String>,

/// Specific texture types.
#[serde(default)]
pub types: TextureTypes,
}

Expand All @@ -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,
}

Expand All @@ -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(),
}
}
}
4 changes: 4 additions & 0 deletions pumpkin-config/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
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,
/// Should be commands from players be logged in console?
#[serde_inline_default(true)]
pub log_console: bool, // TODO: commands...
}

Expand Down
7 changes: 7 additions & 0 deletions pumpkin-config/src/compression.rs
Original file line number Diff line number Diff line change
@@ -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,
}

Expand Down
23 changes: 22 additions & 1 deletion pumpkin-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use logging::LoggingConfig;
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},
Expand Down Expand Up @@ -40,6 +43,7 @@ pub static BASIC_CONFIG: LazyLock<BasicConfiguration> = 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 {
pub proxy: ProxyConfig,
pub authentication: AuthenticationConfig,
Expand All @@ -51,38 +55,55 @@ pub struct AdvancedConfiguration {
pub logging: LoggingConfig,
}

#[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,
Expand Down
8 changes: 8 additions & 0 deletions pumpkin-config/src/logging.rs
Original file line number Diff line number Diff line change
@@ -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,
}

Expand Down
2 changes: 2 additions & 0 deletions pumpkin-config/src/proxy.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
7 changes: 7 additions & 0 deletions pumpkin-config/src/pvp.rs
Original file line number Diff line number Diff line change
@@ -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,
}

Expand Down
17 changes: 16 additions & 1 deletion pumpkin-config/src/rcon.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
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,
/// RCON Logging
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,
}

Expand All @@ -40,11 +51,15 @@ impl Default for RCONLogging {
}
}

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,
logging: Default::default(),
Expand Down
1 change: 1 addition & 0 deletions pumpkin-config/src/resource_pack.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit 5d86ba5

Please sign in to comment.