From a59e5fe39d7e3709038393f77ac45e81506bdb3e Mon Sep 17 00:00:00 2001 From: zp3x <90056490+zp3x@users.noreply.github.com> Date: Tue, 30 Jul 2024 18:10:02 +0200 Subject: [PATCH 1/3] Added configuration --- pumpkin/Cargo.toml | 1 + pumpkin/src/client/client_packet.rs | 1 - pumpkin/src/configuration.rs | 116 ++++++++++++++++++++++++++++ pumpkin/src/entity/player.rs | 1 - pumpkin/src/main.rs | 28 ++++--- pumpkin/src/server.rs | 4 +- 6 files changed, 139 insertions(+), 12 deletions(-) create mode 100644 pumpkin/src/configuration.rs diff --git a/pumpkin/Cargo.toml b/pumpkin/Cargo.toml index dccbd49ef..55be3b759 100644 --- a/pumpkin/Cargo.toml +++ b/pumpkin/Cargo.toml @@ -29,5 +29,6 @@ byteorder = "1" mio = { version = "1.0.1", features = ["os-poll", "net"]} crossbeam-channel = "0.5.13" uuid = "1.10" +toml = "0.8.17" diff --git a/pumpkin/src/client/client_packet.rs b/pumpkin/src/client/client_packet.rs index 0e46802b7..c75ce55e6 100644 --- a/pumpkin/src/client/client_packet.rs +++ b/pumpkin/src/client/client_packet.rs @@ -1,4 +1,3 @@ -use std::{rc::Rc, sync::Mutex}; use crate::protocol::{ client::{ diff --git a/pumpkin/src/configuration.rs b/pumpkin/src/configuration.rs new file mode 100644 index 000000000..d71fe13ae --- /dev/null +++ b/pumpkin/src/configuration.rs @@ -0,0 +1,116 @@ +use std::{path::Path}; + +use serde::{Deserialize, Serialize}; + +use crate::server::Difficulty; + + +#[derive(Deserialize, Serialize)] +pub struct AdvancedConfiguration { + + liquid_physics: bool, + encryption: bool, + +} + +impl Default for AdvancedConfiguration { + fn default() -> Self { + + + Self { + liquid_physics: true, + encryption: true, + } + } +} + +#[derive(Deserialize, Serialize)] +pub struct BasicConfiguration { + pub server_address: String, + pub server_port: u16, + pub seed: String, + pub max_plyers: i32, + pub view_distances: u8, + pub simulation_distance: u8, + pub resource_pack: String, + pub resource_pack_sha1: String, + pub default_difficulty: Difficulty, + pub allow_nether: bool, + pub hardcore: bool, + pub online_mode: bool, + pub spawn_protection: u32, + pub motd: String, +} + +impl Default for BasicConfiguration { + fn default() -> Self { + + Self { + server_address: "127.0.0.1".to_string(), + server_port: 25565, + seed: "".to_string(), + max_plyers: -1, + view_distances: 10, + simulation_distance: 10, + resource_pack: "".to_string(), + resource_pack_sha1: "".to_string(), + default_difficulty: Difficulty::Normal, + allow_nether: true, + hardcore: false, + online_mode: true, + spawn_protection: 16, + motd: "A Blazing fast Pumpkin Server!".to_string(), + } + } + +} + + + +impl AdvancedConfiguration { + + pub fn load>(path: P) -> AdvancedConfiguration { + if path.as_ref().exists() { + let toml = std::fs::read_to_string(path).expect("Couldn't read configuration"); + let configuration: AdvancedConfiguration = toml::from_str(toml.as_str()).expect("Couldn't parse"); + configuration + } else { + let config = AdvancedConfiguration::default(); + let toml = toml::to_string(&config).expect("Couldn't create toml!"); + + std::fs::write(path, toml).expect("Couldn't save configuration"); + config + } + } + + + + + +} + + + +impl BasicConfiguration { + + pub fn load>(path: P) -> BasicConfiguration { + if path.as_ref().exists() { + let toml = std::fs::read_to_string(path).expect("Couldn't read configuration"); + let configuration: BasicConfiguration = toml::from_str(toml.as_str()).expect("Couldn't parse"); + configuration + } else { + let config = BasicConfiguration::default(); + let toml = toml::to_string(&config).expect("Couldn't create toml!"); + + std::fs::write(path, toml).expect("Couldn't save configuration"); + config + } + } + + + + + +} + + diff --git a/pumpkin/src/entity/player.rs b/pumpkin/src/entity/player.rs index 2d93f09f7..4c9c89b89 100644 --- a/pumpkin/src/entity/player.rs +++ b/pumpkin/src/entity/player.rs @@ -1,4 +1,3 @@ -use std::rc::Rc; use crate::{ client::Client, diff --git a/pumpkin/src/main.rs b/pumpkin/src/main.rs index 0046cfb7a..41155aefe 100644 --- a/pumpkin/src/main.rs +++ b/pumpkin/src/main.rs @@ -1,8 +1,15 @@ use mio::net::TcpListener; use mio::{Events, Interest, Poll, Token}; -use std::collections::HashMap; use std::io::{self}; +use std::{ + sync::{Arc, Mutex} +}; + +use client::{interrupted}; +use configuration::BasicConfiguration; +use server::Server; + // Setup some tokens to allow us to identify which event is for which socket. const SERVER: Token = Token(0); @@ -11,16 +18,19 @@ pub mod entity; pub mod protocol; pub mod server; pub mod util; +pub mod configuration; #[cfg(not(target_os = "wasi"))] fn main() -> io::Result<()> { - use std::{ - rc::Rc, - sync::{Arc, Mutex}, - }; + use configuration::AdvancedConfiguration; - use client::{interrupted, Client}; - use server::Server; + + + let basic_config = BasicConfiguration::load("configuration.toml"); + + + let advanced_configuration = AdvancedConfiguration::load("features.toml"); + simple_logger::SimpleLogger::new().init().unwrap(); @@ -42,8 +52,8 @@ fn main() -> io::Result<()> { log::info!("You now can connect to the server"); - let mut server = Arc::new(Mutex::new(Server::new())); - + let server = Arc::new(Mutex::new(Server::new())); + loop { if let Err(err) = poll.poll(&mut events, None) { if interrupted(&err) { diff --git a/pumpkin/src/server.rs b/pumpkin/src/server.rs index 99b2aa537..7ada4fc85 100644 --- a/pumpkin/src/server.rs +++ b/pumpkin/src/server.rs @@ -11,6 +11,7 @@ use std::{ use base64::{engine::general_purpose, Engine}; use mio::{net::TcpStream, Token}; use rsa::{rand_core::OsRng, traits::PublicKeyParts, RsaPrivateKey, RsaPublicKey}; +use serde::{Deserialize, Serialize}; use crate::{ client::Client, @@ -163,8 +164,9 @@ impl Server { } } -#[derive(PartialEq)] +#[derive(PartialEq, Serialize, Deserialize)] pub enum Difficulty { + Peaceful, Easy, Normal, Hard, From f5d6909648ecb3f4c165eb0a724df3466bef2429 Mon Sep 17 00:00:00 2001 From: zp3x <90056490+zp3x@users.noreply.github.com> Date: Tue, 30 Jul 2024 18:10:36 +0200 Subject: [PATCH 2/3] Update Cargo.lock --- Cargo.lock | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 85b08fed8..98ab0397a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -187,6 +187,12 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + [[package]] name = "fdeflate" version = "0.3.4" @@ -227,6 +233,12 @@ dependencies = [ "wasi", ] +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + [[package]] name = "hermit-abi" version = "0.3.9" @@ -245,6 +257,16 @@ dependencies = [ "png", ] +[[package]] +name = "indexmap" +version = "2.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" +dependencies = [ + "equivalent", + "hashbrown", +] + [[package]] name = "inout" version = "0.1.3" @@ -287,6 +309,12 @@ version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + [[package]] name = "miniz_oxide" version = "0.7.4" @@ -467,6 +495,7 @@ dependencies = [ "serde", "serde_json", "simple_logger", + "toml", "uuid", ] @@ -575,6 +604,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb5b1b31579f3811bf615c144393417496f152e12ac8b7663bf664f4a815306d" +dependencies = [ + "serde", +] + [[package]] name = "signature" version = "2.2.0" @@ -707,6 +745,40 @@ dependencies = [ "time-core", ] +[[package]] +name = "toml" +version = "0.8.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a44eede9b727419af8095cb2d72fab15487a541f54647ad4414b34096ee4631" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.22.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1490595c74d930da779e944f5ba2ecdf538af67df1a9848cbd156af43c1b7cf0" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + [[package]] name = "typenum" version = "1.17.0" @@ -876,6 +948,15 @@ version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" +[[package]] +name = "winnow" +version = "0.6.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b480ae9340fc261e6be3e95a1ba86d54ae3f9171132a73ce8d4bbaf68339507c" +dependencies = [ + "memchr", +] + [[package]] name = "zeroize" version = "1.8.1" From 2c3e671201776c41fbd6d688cdf0e6bbc03d7098 Mon Sep 17 00:00:00 2001 From: zp3x <90056490+zp3x@users.noreply.github.com> Date: Tue, 30 Jul 2024 18:18:45 +0200 Subject: [PATCH 3/3] Format --- pumpkin/src/client/client_packet.rs | 3 +- pumpkin/src/configuration.rs | 75 ++++++++++------------------- pumpkin/src/entity/player.rs | 1 - pumpkin/src/main.rs | 26 +++++----- pumpkin/src/protocol/mod.rs | 2 +- 5 files changed, 40 insertions(+), 67 deletions(-) diff --git a/pumpkin/src/client/client_packet.rs b/pumpkin/src/client/client_packet.rs index c75ce55e6..cc687845c 100644 --- a/pumpkin/src/client/client_packet.rs +++ b/pumpkin/src/client/client_packet.rs @@ -1,4 +1,3 @@ - use crate::protocol::{ client::{ config::CFinishConfig, @@ -43,7 +42,7 @@ impl ClientPacketProcessor for Client { dbg!("sending status"); dbg!("test first"); - + let guard = self.server.try_lock().unwrap(); dbg!("test"); diff --git a/pumpkin/src/configuration.rs b/pumpkin/src/configuration.rs index d71fe13ae..2faa9020e 100644 --- a/pumpkin/src/configuration.rs +++ b/pumpkin/src/configuration.rs @@ -1,22 +1,17 @@ -use std::{path::Path}; +use std::path::Path; use serde::{Deserialize, Serialize}; use crate::server::Difficulty; - #[derive(Deserialize, Serialize)] pub struct AdvancedConfiguration { - liquid_physics: bool, encryption: bool, - } impl Default for AdvancedConfiguration { fn default() -> Self { - - Self { liquid_physics: true, encryption: true, @@ -27,7 +22,7 @@ impl Default for AdvancedConfiguration { #[derive(Deserialize, Serialize)] pub struct BasicConfiguration { pub server_address: String, - pub server_port: u16, + pub server_port: u16, pub seed: String, pub max_plyers: i32, pub view_distances: u8, @@ -44,7 +39,6 @@ pub struct BasicConfiguration { impl Default for BasicConfiguration { fn default() -> Self { - Self { server_address: "127.0.0.1".to_string(), server_port: 25565, @@ -62,55 +56,38 @@ impl Default for BasicConfiguration { motd: "A Blazing fast Pumpkin Server!".to_string(), } } - } - - impl AdvancedConfiguration { - pub fn load>(path: P) -> AdvancedConfiguration { - if path.as_ref().exists() { - let toml = std::fs::read_to_string(path).expect("Couldn't read configuration"); - let configuration: AdvancedConfiguration = toml::from_str(toml.as_str()).expect("Couldn't parse"); - configuration - } else { - let config = AdvancedConfiguration::default(); - let toml = toml::to_string(&config).expect("Couldn't create toml!"); - - std::fs::write(path, toml).expect("Couldn't save configuration"); - config - } + if path.as_ref().exists() { + let toml = std::fs::read_to_string(path).expect("Couldn't read configuration"); + let configuration: AdvancedConfiguration = + toml::from_str(toml.as_str()).expect("Couldn't parse"); + configuration + } else { + let config = AdvancedConfiguration::default(); + let toml = toml::to_string(&config).expect("Couldn't create toml!"); + + std::fs::write(path, toml).expect("Couldn't save configuration"); + config + } } - - - - - } - - impl BasicConfiguration { - pub fn load>(path: P) -> BasicConfiguration { - if path.as_ref().exists() { - let toml = std::fs::read_to_string(path).expect("Couldn't read configuration"); - let configuration: BasicConfiguration = toml::from_str(toml.as_str()).expect("Couldn't parse"); - configuration - } else { - let config = BasicConfiguration::default(); - let toml = toml::to_string(&config).expect("Couldn't create toml!"); - - std::fs::write(path, toml).expect("Couldn't save configuration"); - config - } + if path.as_ref().exists() { + let toml = std::fs::read_to_string(path).expect("Couldn't read configuration"); + let configuration: BasicConfiguration = + toml::from_str(toml.as_str()).expect("Couldn't parse"); + configuration + } else { + let config = BasicConfiguration::default(); + let toml = toml::to_string(&config).expect("Couldn't create toml!"); + + std::fs::write(path, toml).expect("Couldn't save configuration"); + config + } } - - - - - } - - diff --git a/pumpkin/src/entity/player.rs b/pumpkin/src/entity/player.rs index 4c9c89b89..d55cf4200 100644 --- a/pumpkin/src/entity/player.rs +++ b/pumpkin/src/entity/player.rs @@ -1,4 +1,3 @@ - use crate::{ client::Client, protocol::{ClientPacket, RawPacket, VarInt}, diff --git a/pumpkin/src/main.rs b/pumpkin/src/main.rs index 41155aefe..12c2c42da 100644 --- a/pumpkin/src/main.rs +++ b/pumpkin/src/main.rs @@ -2,11 +2,9 @@ use mio::net::TcpListener; use mio::{Events, Interest, Poll, Token}; use std::io::{self}; -use std::{ - sync::{Arc, Mutex} -}; +use std::sync::{Arc, Mutex}; -use client::{interrupted}; +use client::interrupted; use configuration::BasicConfiguration; use server::Server; @@ -14,23 +12,19 @@ use server::Server; const SERVER: Token = Token(0); pub mod client; +pub mod configuration; pub mod entity; pub mod protocol; pub mod server; pub mod util; -pub mod configuration; #[cfg(not(target_os = "wasi"))] fn main() -> io::Result<()> { use configuration::AdvancedConfiguration; - - let basic_config = BasicConfiguration::load("configuration.toml"); - - + let advanced_configuration = AdvancedConfiguration::load("features.toml"); - simple_logger::SimpleLogger::new().init().unwrap(); @@ -53,7 +47,7 @@ fn main() -> io::Result<()> { log::info!("You now can connect to the server"); let server = Arc::new(Mutex::new(Server::new())); - + loop { if let Err(err) = poll.poll(&mut events, None) { if interrupted(&err) { @@ -96,10 +90,12 @@ fn main() -> io::Result<()> { let mut guard = server.try_lock().unwrap(); guard.new_client(rc_server, connection, token); }, - + token => { // Maybe received an event for a TCP connection. - let done = if let Some(client) = server.try_lock().unwrap().connections.get_mut(&token) { + let done = if let Some(client) = + server.try_lock().unwrap().connections.get_mut(&token) + { client.poll(poll.registry(), event).unwrap(); client.closed } else { @@ -107,7 +103,9 @@ fn main() -> io::Result<()> { false }; if done { - if let Some(mut client) = server.try_lock().unwrap().connections.remove(&token) { + if let Some(mut client) = + server.try_lock().unwrap().connections.remove(&token) + { poll.registry().deregister(&mut client.connection)?; } } diff --git a/pumpkin/src/protocol/mod.rs b/pumpkin/src/protocol/mod.rs index 1021949cf..d2ea9e71f 100644 --- a/pumpkin/src/protocol/mod.rs +++ b/pumpkin/src/protocol/mod.rs @@ -91,7 +91,7 @@ pub type VarLong = i64; pub enum ConnectionState { HandShake, Status, - Login, + Login, Transfer, Config, Play,