-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from DaniD3v/master
Move config to lazy_lock and into own crate
- Loading branch information
Showing
26 changed files
with
392 additions
and
359 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "pumpkin-config" | ||
version.workspace = true | ||
edition.workspace = true | ||
|
||
[dependencies] | ||
pumpkin-core = { path = "../pumpkin-core" } | ||
|
||
serde = "1.0" | ||
toml = "0.8" | ||
|
||
log.workspace = true |
3 changes: 1 addition & 2 deletions
3
pumpkin/src/config/auth_config.rs → pumpkin-config/src/auth.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Deserialize, Serialize)] | ||
pub struct CommandsConfig { | ||
/// Are commands from the Console accepted ? | ||
pub use_console: bool, | ||
// TODO: commands... | ||
} | ||
|
||
impl Default for CommandsConfig { | ||
fn default() -> Self { | ||
Self { use_console: true } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Deserialize, Serialize)] | ||
// Packet compression | ||
pub struct CompressionConfig { | ||
/// Is compression enabled ? | ||
pub enabled: bool, | ||
/// The compression threshold used when compression is enabled | ||
pub compression_threshold: u32, | ||
/// A value between 0..9 | ||
/// 1 = Optimize for the best speed of encoding. | ||
/// 9 = Optimize for the size of data being encoded. | ||
pub compression_level: u32, | ||
} | ||
|
||
impl Default for CompressionConfig { | ||
fn default() -> Self { | ||
Self { | ||
enabled: true, | ||
compression_threshold: 256, | ||
compression_level: 4, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
use log::warn; | ||
use pumpkin_core::{Difficulty, GameMode}; | ||
use serde::{de::DeserializeOwned, Deserialize, Serialize}; | ||
|
||
use std::{ | ||
fs, | ||
net::{Ipv4Addr, SocketAddr}, | ||
path::Path, | ||
sync::LazyLock, | ||
}; | ||
|
||
pub mod auth; | ||
pub mod proxy; | ||
pub mod resource_pack; | ||
|
||
pub use auth::AuthenticationConfig; | ||
pub use commands::CommandsConfig; | ||
pub use compression::CompressionConfig; | ||
pub use pvp::PVPConfig; | ||
pub use rcon::RCONConfig; | ||
|
||
mod commands; | ||
mod compression; | ||
mod pvp; | ||
mod rcon; | ||
|
||
use proxy::ProxyConfig; | ||
use resource_pack::ResourcePackConfig; | ||
|
||
/// Current Config version of the Base Config | ||
const CURRENT_BASE_VERSION: &str = "1.0.0"; | ||
|
||
pub static ADVANCED_CONFIG: LazyLock<AdvancedConfiguration> = | ||
LazyLock::new(AdvancedConfiguration::load); | ||
|
||
pub static BASIC_CONFIG: LazyLock<BasicConfiguration> = LazyLock::new(BasicConfiguration::load); | ||
|
||
/// The idea is that Pumpkin should very customizable. | ||
/// You can Enable or Disable Features depending on your needs. | ||
/// | ||
/// This also allows you get some Performance or Resource boosts. | ||
/// Important: The Configuration should match Vanilla by default | ||
#[derive(Deserialize, Serialize, Default)] | ||
pub struct AdvancedConfiguration { | ||
pub proxy: ProxyConfig, | ||
pub authentication: AuthenticationConfig, | ||
pub packet_compression: CompressionConfig, | ||
pub resource_pack: ResourcePackConfig, | ||
pub commands: CommandsConfig, | ||
pub rcon: RCONConfig, | ||
pub pvp: PVPConfig, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct BasicConfiguration { | ||
/// A version identifier for the configuration format. | ||
pub config_version: String, | ||
/// The address to bind the server to. | ||
pub server_address: SocketAddr, | ||
/// The seed for world generation. | ||
pub seed: String, | ||
/// The maximum number of players allowed on the server. | ||
pub max_players: u32, | ||
/// The maximum view distance for players. | ||
pub view_distance: u8, | ||
/// The maximum simulated view distance. | ||
pub simulation_distance: u8, | ||
/// The default game difficulty. | ||
pub default_difficulty: Difficulty, | ||
/// Whether the Nether dimension is enabled. | ||
pub allow_nether: bool, | ||
/// Whether the server is in hardcore mode. | ||
pub hardcore: bool, | ||
/// Whether online mode is enabled. Requires valid Minecraft accounts. | ||
pub online_mode: bool, | ||
/// Whether packet encryption is enabled. Required when online mode is enabled. | ||
pub encryption: bool, | ||
/// The server's description displayed on the status screen. | ||
pub motd: String, | ||
/// The default game mode for players. | ||
pub default_gamemode: GameMode, | ||
} | ||
|
||
impl Default for BasicConfiguration { | ||
fn default() -> Self { | ||
Self { | ||
config_version: CURRENT_BASE_VERSION.to_string(), | ||
server_address: SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), 25565), | ||
seed: "".to_string(), | ||
max_players: 100000, | ||
view_distance: 10, | ||
simulation_distance: 10, | ||
default_difficulty: Difficulty::Normal, | ||
allow_nether: true, | ||
hardcore: false, | ||
online_mode: true, | ||
encryption: true, | ||
motd: "A Blazing fast Pumpkin Server!".to_string(), | ||
default_gamemode: GameMode::Survival, | ||
} | ||
} | ||
} | ||
|
||
trait LoadConfiguration { | ||
fn load() -> Self | ||
where | ||
Self: Sized + Default + Serialize + DeserializeOwned, | ||
{ | ||
let path = Self::get_path(); | ||
|
||
let config = if path.exists() { | ||
let file_content = fs::read_to_string(path) | ||
.unwrap_or_else(|_| panic!("Couldn't read configuration file at {:?}", path)); | ||
|
||
toml::from_str(&file_content).unwrap_or_else(|err| { | ||
panic!( | ||
"Couldn't parse config at {:?}. Reason: {}", | ||
path, | ||
err.message() | ||
) | ||
}) | ||
} else { | ||
let content = Self::default(); | ||
|
||
if let Err(err) = fs::write(path, toml::to_string(&content).unwrap()) { | ||
warn!( | ||
"Couldn't write default config to {:?}. Reason: {}", | ||
path, err | ||
); | ||
} | ||
|
||
content | ||
}; | ||
|
||
config.validate(); | ||
config | ||
} | ||
|
||
fn get_path() -> &'static Path; | ||
|
||
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) { | ||
assert_eq!( | ||
self.config_version, CURRENT_BASE_VERSION, | ||
"Config version does not match used Config version. Please update your config" | ||
); | ||
assert!(self.view_distance >= 2, "View distance must be at least 2"); | ||
assert!( | ||
self.view_distance <= 32, | ||
"View distance must be less than 32" | ||
); | ||
if self.online_mode { | ||
assert!( | ||
self.encryption, | ||
"When Online Mode is enabled, Encryption must be enabled" | ||
) | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Deserialize, Serialize)] | ||
pub struct PVPConfig { | ||
/// Is PVP enabled ? | ||
pub enabled: bool, | ||
/// Do we want to have the Red hurt animation & fov bobbing | ||
pub hurt_animation: bool, | ||
/// Should players in creative be protected against PVP | ||
pub protect_creative: bool, | ||
/// Has PVP Knockback? | ||
pub knockback: bool, | ||
/// Should player swing when attacking? | ||
pub swing: bool, | ||
} | ||
|
||
impl Default for PVPConfig { | ||
fn default() -> Self { | ||
Self { | ||
enabled: true, | ||
hurt_animation: true, | ||
protect_creative: true, | ||
knockback: true, | ||
swing: true, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use std::net::{Ipv4Addr, SocketAddr}; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Deserialize, Serialize, Clone)] | ||
pub struct RCONConfig { | ||
pub enabled: bool, | ||
pub address: SocketAddr, | ||
pub password: String, | ||
} | ||
|
||
impl Default for RCONConfig { | ||
fn default() -> Self { | ||
Self { | ||
enabled: false, | ||
address: SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), 25575), | ||
password: "".to_string(), | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use std::str::FromStr; | ||
|
||
use num_derive::{FromPrimitive, ToPrimitive}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
pub struct ParseGameModeError; | ||
|
||
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize, FromPrimitive, ToPrimitive)] | ||
pub enum GameMode { | ||
Undefined = -1, | ||
Survival, | ||
Creative, | ||
Adventure, | ||
Spectator, | ||
} | ||
|
||
impl FromStr for GameMode { | ||
type Err = ParseGameModeError; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
"survival" => Ok(Self::Survival), | ||
"creative" => Ok(Self::Creative), | ||
"adventure" => Ok(Self::Adventure), | ||
"spectator" => Ok(Self::Spectator), | ||
_ => Err(ParseGameModeError), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,22 @@ | ||
pub mod gamemode; | ||
pub mod random; | ||
pub mod text; | ||
|
||
pub use gamemode::GameMode; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(PartialEq, Serialize, Deserialize)] | ||
pub enum Difficulty { | ||
Peaceful, | ||
Easy, | ||
Normal, | ||
Hard, | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq)] | ||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")] | ||
pub enum ProfileAction { | ||
ForcedNameChange, | ||
UsingBannedSkin, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.