Merge pull request #1 from zp3x/master

Add configuration
This commit is contained in:
Alexander Medvedev
2024-07-30 17:26:25 +01:00
committed by GitHub
8 changed files with 199 additions and 18 deletions

81
Cargo.lock generated
View File

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

View File

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

View File

@@ -1,5 +1,3 @@
use std::{rc::Rc, sync::Mutex};
use crate::protocol::{
client::{
config::CFinishConfig,

View File

@@ -0,0 +1,93 @@
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<P: AsRef<Path>>(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<P: AsRef<Path>>(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
}
}
}

View File

@@ -1,5 +1,3 @@
use std::rc::Rc;
use crate::{
client::Client,
protocol::{ClientPacket, RawPacket, VarInt},

View File

@@ -1,12 +1,18 @@
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);
pub mod client;
pub mod configuration;
pub mod entity;
pub mod protocol;
pub mod server;
@@ -14,13 +20,11 @@ pub mod util;
#[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,7 +46,7 @@ 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) {
@@ -89,7 +93,9 @@ fn main() -> io::Result<()> {
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 {
@@ -97,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)?;
}
}

View File

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