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",
"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"

View File

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

View File

@@ -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,
}
@@ -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(),
}
}
}

View File

@@ -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...
}

View File

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

View File

@@ -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<BasicConfiguration> = 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,

View File

@@ -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,

View File

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

View File

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

View File

@@ -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.