Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved the state to it's own crate #115

Open
wants to merge 2 commits into
base: rewrite/v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@ resolver = "2"
#================= Members =================#
members = [
"src/bin",
"src/lib/adapters/anvil",
"src/lib/adapters/mca",
"src/lib/adapters/nbt",
"src/lib/core",
"src/lib/core/state",
"src/lib/derive_macros",
"src/lib/ecs",
"src/lib/events",
"src/lib/net",
"src/lib/net/crates/encryption",
"src/lib/net/crates/codec",
"src/lib/net/crates/encryption",
"src/lib/plugins",
"src/lib/storage",
"src/lib/utils", "src/lib/utils/logging", "src/lib/utils/profiling", "src/lib/utils/general_purpose",
"src/lib/utils",
"src/lib/utils/general_purpose",
"src/lib/utils/logging",
"src/lib/utils/profiling",
"src/lib/world",
"src/lib/derive_macros",
"src/lib/adapters/nbt", "src/lib/adapters/mca",
"src/tests", "src/lib/adapters/anvil",
"src/tests",
]

#================== Lints ==================#
Expand Down Expand Up @@ -88,6 +94,7 @@ ferrumc-world = { path = "src/lib/world" }
ferrumc-nbt = { path = "src/lib/adapters/nbt" }
ferrumc-anvil = { path = "src/lib/adapters/anvil" }
ferrumc-tests = { path = "src/tests" }
ferrumc-state = { path = "src/lib/core/state"}


# Asynchronous
Expand Down
1 change: 1 addition & 0 deletions src/bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ferrumc-logging = { workspace = true }
ferrumc-world = { workspace = true }
ferrumc-macros = { workspace = true }
ferrumc-nbt = { workspace = true }
ferrumc-state = { workspace = true }

ctor = { workspace = true }
parking_lot = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion src/bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ extern crate core;
use std::path::PathBuf;
use ferrumc_ecs::Universe;
use ferrumc_net::server::create_server_listener;
use ferrumc_net::ServerState;
use std::sync::Arc;
use clap::Parser;
use systems::definition;
use tracing::{error, info};
use ferrumc_config::statics::get_global_config;
use ferrumc_state::ServerState;
use ferrumc_world::World;

#[derive(clap::Parser)]
Expand Down
2 changes: 1 addition & 1 deletion src/bin/src/packet_handlers/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ferrumc_net::connection::ConnectionState;
use ferrumc_net::errors::NetError::{Packet};
use ferrumc_net::errors::{NetError, PacketError};
use ferrumc_net::packets::incoming::handshake::HandshakeEvent;
use ferrumc_net::GlobalState;
use ferrumc_state::GlobalState;
use tracing::{error, trace};
use ferrumc_ecs::errors::ECSError;
use ferrumc_net::utils::ecs_helpers::EntityExt;
Expand Down
2 changes: 1 addition & 1 deletion src/bin/src/packet_handlers/login_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use ferrumc_net::packets::outgoing::login_success::LoginSuccessPacket;
use ferrumc_net::packets::outgoing::registry_data::get_registry_packets;
use ferrumc_net::packets::outgoing::set_default_spawn_position::SetDefaultSpawnPositionPacket;
use ferrumc_net::packets::outgoing::synchronize_player_position::SynchronizePlayerPositionPacket;
use ferrumc_net::GlobalState;
use ferrumc_state::GlobalState;
use ferrumc_net_codec::encode::NetEncodeOpts;
use tracing::{debug, trace};
use ferrumc_net::packets::outgoing::finish_configuration::FinishConfigurationPacket;
Expand Down
2 changes: 1 addition & 1 deletion src/bin/src/packet_handlers/tick_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ferrumc_net::errors::NetError;
use ferrumc_net::packets::outgoing::update_time::TickEvent;
use ferrumc_net::packets::outgoing::update_time::UpdateTimePacket;
use ferrumc_net::utils::broadcast::{BroadcastOptions, BroadcastToAll};
use ferrumc_net::GlobalState;
use ferrumc_state::GlobalState;

#[event_handler]
async fn handle_tick(event: TickEvent, state: GlobalState) -> Result<TickEvent, NetError> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use ferrumc_core::transform::position::Position;
use ferrumc_macros::event_handler;
use ferrumc_net::errors::NetError;
use ferrumc_net::packets::incoming::set_player_position::SetPlayerPositionEvent;
use ferrumc_net::GlobalState;
use ferrumc_state::GlobalState;
use ferrumc_net::utils::ecs_helpers::EntityExt;

#[event_handler]
Expand Down
3 changes: 2 additions & 1 deletion src/bin/src/systems/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use crate::systems::keep_alive_system::KeepAliveSystem;
use crate::systems::tcp_listener_system::TcpListenerSystem;
use crate::systems::ticking_system::TickingSystem;
use async_trait::async_trait;
use ferrumc_net::{GlobalState, NetResult};
use ferrumc_net::{NetResult};
use ferrumc_state::GlobalState;
use futures::stream::FuturesUnordered;
use std::sync::{Arc, LazyLock};
use tracing::{debug, debug_span, info, Instrument};
Expand Down
2 changes: 1 addition & 1 deletion src/bin/src/systems/keep_alive_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use async_trait::async_trait;
use ferrumc_core::identity::player_identity::PlayerIdentity;
use ferrumc_net::connection::{ConnectionState, StreamWriter};
use ferrumc_net::packets::outgoing::keep_alive::{KeepAlive, KeepAlivePacket};
use ferrumc_net::GlobalState;
use ferrumc_state::GlobalState;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use tracing::{error, info, trace, warn};
Expand Down
2 changes: 1 addition & 1 deletion src/bin/src/systems/tcp_listener_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;
use async_trait::async_trait;
use tracing::{debug, error, info, info_span, Instrument};
use ferrumc_net::connection::handle_connection;
use ferrumc_net::GlobalState;
use ferrumc_state::GlobalState;
use crate::systems::definition::System;
use crate::Result;

Expand Down
2 changes: 1 addition & 1 deletion src/bin/src/systems/ticking_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::systems::definition::System;
use async_trait::async_trait;
use ferrumc_events::infrastructure::Event;
use ferrumc_net::packets::outgoing::update_time::TickEvent;
use ferrumc_net::GlobalState;
use ferrumc_state::GlobalState;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;
Expand Down
2 changes: 2 additions & 0 deletions src/lib/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ edition = "2021"

[dependencies]
thiserror = { workspace = true }
tokio = { workspace = true}
ferrumc-ecs = {workspace = true}
3 changes: 2 additions & 1 deletion src/lib/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pub mod errors;

// Core structs/types. Usually used in ECS Components.
pub mod transform;
pub mod identity;
pub mod identity;
pub mod state;
Empty file added src/lib/core/src/state.rs
Empty file.
8 changes: 8 additions & 0 deletions src/lib/core/state/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "ferrumc-state"
version = "0.1.0"
edition = "2021"

[dependencies]
tokio = { workspace = true }
ferrumc-ecs = { workspace = true }
10 changes: 10 additions & 0 deletions src/lib/core/state/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::sync::Arc;
use tokio::net::TcpListener;
use ferrumc_ecs::Universe;

pub struct ServerState {
pub universe: Universe,
pub tcp_listener: TcpListener
}

pub type GlobalState = Arc<ServerState>;
2 changes: 1 addition & 1 deletion src/lib/derive_macros/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub(crate) fn derive(input: TokenStream) -> TokenStream {
let output = quote! {
impl ::ferrumc_events::infrastructure::Event for #name {
type Data = Self;
type State = #net_crate::GlobalState;
type State = ferrumc_state::GlobalState;
type Error = #net_crate::errors::NetError;

fn name() -> &'static str {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/derive_macros/src/net/packets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub fn bake_registry(input: TokenStream) -> TokenStream {
let match_arms = match_arms.into_iter();

let output = quote! {
pub async fn handle_packet<R: std::io::Read>(packet_id: u8, conn_id: usize, conn_state: &crate::connection::ConnectionState, cursor: &mut R, state: std::sync::Arc<crate::ServerState>) -> crate::NetResult<()> {
pub async fn handle_packet<R: std::io::Read>(packet_id: u8, conn_id: usize, conn_state: &crate::connection::ConnectionState, cursor: &mut R, state: std::sync::Arc<ferrumc_state::ServerState>) -> crate::NetResult<()> {
match (packet_id, conn_state.as_str()) {
#(#match_arms)*
_ => tracing::debug!("No packet found for ID: 0x{:02X} in state: {}", packet_id, conn_state.as_str()),
Expand Down
1 change: 1 addition & 0 deletions src/lib/net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ serde_derive = { workspace = true }
rand = { workspace = true }
uuid = { workspace = true, features = ["v4"] }
async-trait = { workspace = true }
ferrumc-state = { workspace = true }
3 changes: 2 additions & 1 deletion src/lib/net/src/connection.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::packets::incoming::packet_skeleton::PacketSkeleton;
use crate::{handle_packet, NetResult, ServerState};
use crate::{handle_packet, NetResult};
use ferrumc_state::ServerState;
use ferrumc_net_codec::encode::NetEncode;
use ferrumc_net_codec::encode::NetEncodeOpts;
use std::sync::Arc;
Expand Down
8 changes: 0 additions & 8 deletions src/lib/net/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
extern crate core;

use tokio::net::TcpListener;
use ferrumc_ecs::Universe;
use ferrumc_macros::bake_packet_registry;
use std::sync::{Arc};

pub mod connection;
pub mod errors;
Expand All @@ -12,12 +9,7 @@ pub mod server;
pub mod utils;
pub type NetResult<T> = Result<T, errors::NetError>;

pub struct ServerState {
pub universe: Universe,
pub tcp_listener: TcpListener
}

pub type GlobalState = Arc<ServerState>;


bake_packet_registry!("\\src\\packets\\incoming");
9 changes: 5 additions & 4 deletions src/lib/net/src/packets/incoming/ack_finish_configuration.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::packets::IncomingPacket;
use crate::{NetResult, ServerState};
use crate::NetResult;
use ferrumc_state::ServerState;
use ferrumc_events::infrastructure::Event;
use ferrumc_macros::{packet, Event, NetDecode};
use std::sync::Arc;
use ferrumc_events::infrastructure::Event;

#[derive(NetDecode)]
#[packet(packet_id = 0x03, state = "configuration")]
Expand All @@ -11,9 +12,9 @@ pub struct AckFinishConfigurationPacket {}
impl IncomingPacket for AckFinishConfigurationPacket {
async fn handle(self, conn_id: usize, state: Arc<ServerState>) -> NetResult<()> {
let event = AckFinishConfigurationEvent::new(self, conn_id);

tokio::spawn(AckFinishConfigurationEvent::trigger(event, state));

Ok(())
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/net/src/packets/incoming/client_information.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::sync::Arc;
use tracing::debug;
use ferrumc_state::ServerState;
use ferrumc_macros::{packet, NetDecode};
use ferrumc_net_codec::net_types::var_int::VarInt;
use crate::packets::IncomingPacket;
use crate::{NetResult, ServerState};
use crate::NetResult;

#[derive(Debug, NetDecode)]
#[packet(packet_id = 0x00, state = "configuration")]
Expand Down
2 changes: 1 addition & 1 deletion src/lib/net/src/packets/incoming/handshake.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::packets::IncomingPacket;
use crate::{NetResult, ServerState};
use crate::NetResult; use ferrumc_state::ServerState;
use ferrumc_events::infrastructure::Event;
use ferrumc_macros::{packet, Event, NetDecode};
use ferrumc_net_codec::net_types::var_int::VarInt;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/net/src/packets/incoming/keep_alive.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::packets::outgoing::keep_alive::KeepAlive;
use crate::packets::IncomingPacket;
use crate::{NetResult, ServerState};
use crate::NetResult; use ferrumc_state::ServerState;
use ferrumc_macros::{packet, NetDecode};
use std::sync::Arc;
use tracing::debug;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/net/src/packets/incoming/login_acknowledged.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;
use ferrumc_macros::{Event, NetDecode, packet};
use ferrumc_events::infrastructure::Event;
use crate::packets::IncomingPacket;
use crate::{NetResult, ServerState};
use crate::NetResult; use ferrumc_state::ServerState;

#[derive(Debug, NetDecode)]
#[packet(packet_id = 0x03, state = "login")]
Expand Down
2 changes: 1 addition & 1 deletion src/lib/net/src/packets/incoming/login_start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;
use ferrumc_events::infrastructure::Event;
use ferrumc_macros::{packet, Event, NetDecode};
use crate::packets::IncomingPacket;
use crate::{NetResult, ServerState};
use crate::NetResult; use ferrumc_state::ServerState;

#[derive(Debug, NetDecode)]
#[packet(packet_id = 0x00, state = "login")]
Expand Down
2 changes: 1 addition & 1 deletion src/lib/net/src/packets/incoming/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;
use ferrumc_macros::{packet, NetDecode};
use ferrumc_net_codec::encode::NetEncodeOpts;
use crate::packets::IncomingPacket;
use crate::{NetResult, ServerState};
use crate::NetResult; use ferrumc_state::ServerState;
use crate::connection::StreamWriter;
use crate::packets::outgoing::ping_response::PongPacket;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ferrumc_events::infrastructure::Event;
use ferrumc_macros::{packet, Event, NetDecode};
use ferrumc_net_codec::net_types::length_prefixed_vec::LengthPrefixedVec;
use crate::packets::IncomingPacket;
use crate::{NetResult, ServerState};
use crate::NetResult; use ferrumc_state::ServerState;

#[derive(Debug, NetDecode)]
#[packet(packet_id = 0x07, state = "configuration")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tracing::debug;
use ferrumc_macros::{packet};
use ferrumc_net_codec::decode::{NetDecode, NetDecodeOpts, NetDecodeResult};
use crate::packets::IncomingPacket;
use crate::{NetResult, ServerState};
use crate::NetResult; use ferrumc_state::ServerState;

#[derive(Debug)]
#[packet(packet_id = 0x02, state = "configuration")]
Expand Down
2 changes: 1 addition & 1 deletion src/lib/net/src/packets/incoming/set_player_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;
use ferrumc_events::infrastructure::Event;
use ferrumc_macros::{packet, Event, NetDecode};
use crate::packets::IncomingPacket;
use crate::{NetResult, ServerState};
use crate::NetResult; use ferrumc_state::ServerState;

#[derive(NetDecode)]
#[packet(packet_id = 0x1A, state = "play")]
Expand Down
2 changes: 1 addition & 1 deletion src/lib/net/src/packets/incoming/status_request.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::connection::StreamWriter;
use crate::packets::outgoing::status_response::StatusResponse;
use crate::packets::IncomingPacket;
use crate::{NetResult, ServerState};
use crate::NetResult; use ferrumc_state::ServerState;
use ferrumc_config::favicon::get_favicon_base64;
use ferrumc_config::statics::get_global_config;
use ferrumc_core::identity::player_identity::PlayerIdentity;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/net/src/packets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ pub mod outgoing;

#[allow(async_fn_in_trait)]
pub trait IncomingPacket {
async fn handle(self, conn_id: usize, state: std::sync::Arc<crate::ServerState>) -> NetResult<()>;
async fn handle(self, conn_id: usize, state: std::sync::Arc<ferrumc_state::ServerState>) -> NetResult<()>;
}
3 changes: 2 additions & 1 deletion src/lib/net/src/utils/broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use futures::StreamExt;
use tracing::debug;
use ferrumc_ecs::entities::Entity;
use ferrumc_net_codec::encode::{NetEncode, NetEncodeOpts};
use crate::{GlobalState, NetResult};
use crate::NetResult;
use ferrumc_state::GlobalState;
use crate::connection::StreamWriter;

type AsyncCallbackFn = Box<dyn Fn(Entity, &GlobalState) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> + Send + Sync>;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/net/src/utils/ecs_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ferrumc_ecs::components::storage::{Component, ComponentRef, ComponentRefMut};
use ferrumc_ecs::ECSResult;
use crate::GlobalState;
use ferrumc_state::GlobalState;

pub trait EntityExt {
fn get<T: Component>(&self, state: GlobalState) -> ECSResult<ComponentRef<T>>;
Expand Down