Skip to content

Commit

Permalink
Merge branch 'master' into feat/commands
Browse files Browse the repository at this point in the history
  • Loading branch information
radstevee authored Dec 30, 2024
2 parents 231b8fb + cc11874 commit 0575ff3
Show file tree
Hide file tree
Showing 12 changed files with 295 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .etc/example-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ max_players = 100
network_tick_rate = 30
# World name to load
world = "world"
# Whether the server should validate players via the whitelist
whitelist = false
# Network compression threshold (can be negative). This decides how long a packet has to be before it is compressed.
# Very small packets may actually increase in size when compressed, so setting it to 0 won't be perfect in all situations.
# Set to -1 to disable compression.
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ parking_lot = "0.12.3"
rayon = "1.10.0"

# Network
reqwest = { version = "0.12.9", features = ["json"] }

# Error handling
thiserror = "2.0.3"
Expand All @@ -140,6 +141,7 @@ serde_derive = "1.0.210"
base64 = "0.22.1"
bitcode = "0.6.3"
bitcode_derive = "0.6.3"
toml = "0.8.19"

# Bit manipulation
byteorder = "1.5.0"
Expand Down Expand Up @@ -185,6 +187,7 @@ colored = "2.1.0"
deepsize = "0.2.0"
page_size = "0.6.0"
enum-ordinalize = "4.3.0"
regex = "1.11.1"

# I/O
tempfile = "3.12.0"
Expand Down
4 changes: 3 additions & 1 deletion src/bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern crate core;
use crate::errors::BinaryError;
use clap::Parser;
use ferrumc_config::statics::get_global_config;
use ferrumc_config::whitelist::create_whitelist;
use ferrumc_ecs::Universe;
use ferrumc_general_purpose::paths::get_root_path;
use ferrumc_net::server::create_server_listener;
Expand Down Expand Up @@ -59,13 +60,14 @@ async fn main() {
async fn entry() -> Result<()> {
let state = create_state().await?;
let global_state = Arc::new(state);
create_whitelist().await;

// Needed for some reason because ctor doesn't really want to do ctor things otherwise.
ferrumc_default_commands::init();

let all_system_handles = tokio::spawn(definition::start_all_systems(global_state.clone()));

// Start the systems and wait until all of them are done
//Start the systems and wait until all of them are done
all_system_handles.await??;

// Stop all systems
Expand Down
36 changes: 26 additions & 10 deletions src/bin/src/packet_handlers/login_process.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use ferrumc_commands::graph::CommandsPacket;
use ferrumc_config::statics::{get_global_config, get_whitelist};
use ferrumc_core::identity::player_identity::PlayerIdentity;
use ferrumc_core::transform::grounded::OnGround;
use ferrumc_core::transform::position::Position;
Expand All @@ -16,6 +17,7 @@ use ferrumc_net::packets::outgoing::client_bound_known_packs::ClientBoundKnownPa
use ferrumc_net::packets::outgoing::finish_configuration::FinishConfigurationPacket;
use ferrumc_net::packets::outgoing::game_event::GameEventPacket;
use ferrumc_net::packets::outgoing::keep_alive::OutgoingKeepAlivePacket;
use ferrumc_net::packets::outgoing::login_disconnect::LoginDisconnectPacket;
use ferrumc_net::packets::outgoing::login_play::LoginPlayPacket;
use ferrumc_net::packets::outgoing::login_success::LoginSuccessPacket;
use ferrumc_net::packets::outgoing::registry_data::get_registry_packets;
Expand All @@ -32,23 +34,37 @@ async fn handle_login_start(
login_start_event: LoginStartEvent,
state: GlobalState,
) -> Result<LoginStartEvent, NetError> {
debug!("Handling login start event");

let uuid = login_start_event.login_start_packet.uuid;
let username = login_start_event.login_start_packet.username.as_str();
debug!("Received login start from user with username {}", username);

// Add the player identity component to the ECS for the entity.
state.universe.add_component::<PlayerIdentity>(
login_start_event.conn_id,
PlayerIdentity::new(username.to_string(), uuid),
)?;
let player_identity = PlayerIdentity::new(username.to_string(), uuid);
debug!("Handling login start event for user: {username}, uuid: {uuid}");

//Send a Login Success Response to further the login sequence
let mut writer = state
.universe
.get_mut::<StreamWriter>(login_start_event.conn_id)?;

if get_global_config().whitelist {
let whitelist = get_whitelist();

if whitelist.get(&uuid).is_none() {
writer
.send_packet(
&LoginDisconnectPacket::new(
"{\"translate\":\"multiplayer.disconnect.not_whitelisted\"}",
),
&NetEncodeOpts::WithLength,
)
.await?;
return Ok(login_start_event);
}
}

// Add the player identity component to the ECS for the entity.
state
.universe
.add_component::<PlayerIdentity>(login_start_event.conn_id, player_identity)?;

//Send a Login Success Response to further the login sequence
writer
.send_packet(
&LoginSuccessPacket::new(uuid, username),
Expand Down
14 changes: 14 additions & 0 deletions src/lib/net/src/packets/outgoing/login_disconnect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use ferrumc_macros::{packet, NetEncode};
use std::io::Write;

#[derive(NetEncode)]
#[packet(packet_id = 0x00)]
pub struct LoginDisconnectPacket<'a> {
pub reason: &'a str,
}

impl<'a> LoginDisconnectPacket<'a> {
pub fn new(reason: &'a str) -> Self {
Self { reason }
}
}
1 change: 1 addition & 0 deletions src/lib/net/src/packets/outgoing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod disconnect;
pub mod finish_configuration;
pub mod game_event;
pub mod keep_alive;
pub mod login_disconnect;
pub mod login_play;
pub mod login_success;
pub mod ping_response;
Expand Down
10 changes: 8 additions & 2 deletions src/lib/utils/config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ edition = "2021"
description = "FerrumC configuration utilities"

[dependencies]
toml = "0.8.19"
toml = { workspace = true }
thiserror = { workspace = true }
serde = { workspace = true }
serde_derive = { workspace = true }
tracing = { workspace = true }
parking_lot = { workspace = true }
base64 = { workspace = true }
ferrumc-general-purpose = { workspace = true }
lazy_static = { workspace = true }
ferrumc-core = { workspace = true }
lazy_static = { workspace = true }
dashmap = { workspace = true }
uuid = { workspace = true }
regex = { workspace = true }
reqwest = { workspace = true }
futures = { workspace = true }
1 change: 1 addition & 0 deletions src/lib/utils/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod favicon;
pub mod server_config;
pub mod setup;
pub mod statics;
pub mod whitelist;

// Re-exports
pub use server_config::DatabaseConfig;
Expand Down
2 changes: 2 additions & 0 deletions src/lib/utils/config/src/server_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use serde_derive::{Deserialize, Serialize};
/// - `database` - [DatabaseConfig]: The configuration for the database.
/// - `world`: The name of the world that the server will load.
/// - `network_compression_threshold`: The threshold at which the server will compress network packets.
/// - `whitelist`: Whether the server whitelist is enabled or not.
#[derive(Debug, Deserialize, Serialize)]
pub struct ServerConfig {
pub host: String,
Expand All @@ -25,6 +26,7 @@ pub struct ServerConfig {
pub database: DatabaseConfig,
pub world: String,
pub network_compression_threshold: i32, // Can be negative
pub whitelist: bool,
}

/// The database configuration section from [ServerConfig].
Expand Down
4 changes: 4 additions & 0 deletions src/lib/utils/config/src/setup.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::statics::DEFAULT_CONFIG;
use crate::whitelist::create_blank_whitelist_file;
use ferrumc_general_purpose::paths::get_root_path;
use std::fs::File;
use std::io::Write;
Expand Down Expand Up @@ -26,6 +27,9 @@ impl From<std::io::Error> for SetupError {
}

pub fn setup() -> Result<(), SetupError> {
if !std::fs::exists(get_root_path().join("whitelist.txt"))? {
create_blank_whitelist_file();
}
if std::fs::exists(get_root_path().join("config.toml"))? {
return Ok(());
}
Expand Down
11 changes: 7 additions & 4 deletions src/lib/utils/config/src/statics.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
//! # Statics module.
//!
//! Contains the static global configuration and its related functions.
use crate::server_config::ServerConfig;
use dashmap::DashSet;
use ferrumc_general_purpose::paths::get_root_path;
use lazy_static::lazy_static;
use std::fs::File;
Expand All @@ -16,6 +13,8 @@ pub(crate) const DEFAULT_CONFIG: &str = include_str!("../../../../../.etc/exampl
lazy_static! {
/// The server configuration that is stored in memory.
static ref CONFIG: ServerConfig = create_config();
/// The whitelist of player uuids.
pub static ref WHITELIST: DashSet<u128> = DashSet::new();
}
fn create_config() -> ServerConfig {
let config_location = get_root_path().join("config.toml");
Expand Down Expand Up @@ -77,3 +76,7 @@ fn create_config() -> ServerConfig {
pub fn get_global_config() -> &'static ServerConfig {
&CONFIG
}

pub fn get_whitelist() -> &'static DashSet<u128> {
&WHITELIST
}
Loading

0 comments on commit 0575ff3

Please sign in to comment.