diff --git a/Cargo.toml b/Cargo.toml index f2b92861..ce3d2789 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 ==================# @@ -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 diff --git a/src/bin/Cargo.toml b/src/bin/Cargo.toml index 008d72fd..3d2957c6 100644 --- a/src/bin/Cargo.toml +++ b/src/bin/Cargo.toml @@ -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 } diff --git a/src/bin/src/main.rs b/src/bin/src/main.rs index 35f8925f..f65955e1 100644 --- a/src/bin/src/main.rs +++ b/src/bin/src/main.rs @@ -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)] diff --git a/src/bin/src/packet_handlers/handshake.rs b/src/bin/src/packet_handlers/handshake.rs index a895ab4e..5b0761b7 100644 --- a/src/bin/src/packet_handlers/handshake.rs +++ b/src/bin/src/packet_handlers/handshake.rs @@ -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; diff --git a/src/bin/src/packet_handlers/login_process.rs b/src/bin/src/packet_handlers/login_process.rs index d44638fa..89489c46 100644 --- a/src/bin/src/packet_handlers/login_process.rs +++ b/src/bin/src/packet_handlers/login_process.rs @@ -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; diff --git a/src/bin/src/packet_handlers/tick_handler.rs b/src/bin/src/packet_handlers/tick_handler.rs index bfce6253..11103c85 100644 --- a/src/bin/src/packet_handlers/tick_handler.rs +++ b/src/bin/src/packet_handlers/tick_handler.rs @@ -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 { diff --git a/src/bin/src/packet_handlers/transform/update_player_position.rs b/src/bin/src/packet_handlers/transform/update_player_position.rs index 702a1967..a5f950a4 100644 --- a/src/bin/src/packet_handlers/transform/update_player_position.rs +++ b/src/bin/src/packet_handlers/transform/update_player_position.rs @@ -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] diff --git a/src/bin/src/systems/definition.rs b/src/bin/src/systems/definition.rs index 0fa3989e..206b1fab 100644 --- a/src/bin/src/systems/definition.rs +++ b/src/bin/src/systems/definition.rs @@ -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}; diff --git a/src/bin/src/systems/keep_alive_system.rs b/src/bin/src/systems/keep_alive_system.rs index 7b4aedcd..4a594d97 100644 --- a/src/bin/src/systems/keep_alive_system.rs +++ b/src/bin/src/systems/keep_alive_system.rs @@ -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}; diff --git a/src/bin/src/systems/tcp_listener_system.rs b/src/bin/src/systems/tcp_listener_system.rs index 0803c592..7d69967e 100644 --- a/src/bin/src/systems/tcp_listener_system.rs +++ b/src/bin/src/systems/tcp_listener_system.rs @@ -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; diff --git a/src/bin/src/systems/ticking_system.rs b/src/bin/src/systems/ticking_system.rs index e4a2a511..ffe6f21f 100644 --- a/src/bin/src/systems/ticking_system.rs +++ b/src/bin/src/systems/ticking_system.rs @@ -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; diff --git a/src/lib/core/Cargo.toml b/src/lib/core/Cargo.toml index c0a9f01e..79e2e1c3 100644 --- a/src/lib/core/Cargo.toml +++ b/src/lib/core/Cargo.toml @@ -6,3 +6,5 @@ edition = "2021" [dependencies] thiserror = { workspace = true } +tokio = { workspace = true} +ferrumc-ecs = {workspace = true} diff --git a/src/lib/core/src/lib.rs b/src/lib/core/src/lib.rs index f048b6fa..09a67c3b 100644 --- a/src/lib/core/src/lib.rs +++ b/src/lib/core/src/lib.rs @@ -3,4 +3,5 @@ pub mod errors; // Core structs/types. Usually used in ECS Components. pub mod transform; -pub mod identity; \ No newline at end of file +pub mod identity; +pub mod state; \ No newline at end of file diff --git a/src/lib/core/src/state.rs b/src/lib/core/src/state.rs new file mode 100644 index 00000000..e69de29b diff --git a/src/lib/core/state/Cargo.toml b/src/lib/core/state/Cargo.toml new file mode 100644 index 00000000..214c6813 --- /dev/null +++ b/src/lib/core/state/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "ferrumc-state" +version = "0.1.0" +edition = "2021" + +[dependencies] +tokio = { workspace = true } +ferrumc-ecs = { workspace = true } diff --git a/src/lib/core/state/src/lib.rs b/src/lib/core/state/src/lib.rs new file mode 100644 index 00000000..e00f447a --- /dev/null +++ b/src/lib/core/state/src/lib.rs @@ -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; \ No newline at end of file diff --git a/src/lib/derive_macros/src/events/mod.rs b/src/lib/derive_macros/src/events/mod.rs index fffa9cd8..d46d9caf 100644 --- a/src/lib/derive_macros/src/events/mod.rs +++ b/src/lib/derive_macros/src/events/mod.rs @@ -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 { diff --git a/src/lib/derive_macros/src/net/packets/mod.rs b/src/lib/derive_macros/src/net/packets/mod.rs index b0e39c33..95499ba9 100644 --- a/src/lib/derive_macros/src/net/packets/mod.rs +++ b/src/lib/derive_macros/src/net/packets/mod.rs @@ -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(packet_id: u8, conn_id: usize, conn_state: &crate::connection::ConnectionState, cursor: &mut R, state: std::sync::Arc) -> crate::NetResult<()> { + pub async fn handle_packet(packet_id: u8, conn_id: usize, conn_state: &crate::connection::ConnectionState, cursor: &mut R, state: std::sync::Arc) -> 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()), diff --git a/src/lib/net/Cargo.toml b/src/lib/net/Cargo.toml index ffbb1238..7573a54b 100644 --- a/src/lib/net/Cargo.toml +++ b/src/lib/net/Cargo.toml @@ -27,3 +27,4 @@ serde_derive = { workspace = true } rand = { workspace = true } uuid = { workspace = true, features = ["v4"] } async-trait = { workspace = true } +ferrumc-state = { workspace = true } diff --git a/src/lib/net/src/connection.rs b/src/lib/net/src/connection.rs index c5a2453b..eac1e0c2 100644 --- a/src/lib/net/src/connection.rs +++ b/src/lib/net/src/connection.rs @@ -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; diff --git a/src/lib/net/src/lib.rs b/src/lib/net/src/lib.rs index 22bea922..311d3f27 100644 --- a/src/lib/net/src/lib.rs +++ b/src/lib/net/src/lib.rs @@ -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; @@ -12,12 +9,7 @@ pub mod server; pub mod utils; pub type NetResult = Result; -pub struct ServerState { - pub universe: Universe, - pub tcp_listener: TcpListener -} -pub type GlobalState = Arc; bake_packet_registry!("\\src\\packets\\incoming"); diff --git a/src/lib/net/src/packets/incoming/ack_finish_configuration.rs b/src/lib/net/src/packets/incoming/ack_finish_configuration.rs index 27b070f9..4ea1168f 100644 --- a/src/lib/net/src/packets/incoming/ack_finish_configuration.rs +++ b/src/lib/net/src/packets/incoming/ack_finish_configuration.rs @@ -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")] @@ -11,9 +12,9 @@ pub struct AckFinishConfigurationPacket {} impl IncomingPacket for AckFinishConfigurationPacket { async fn handle(self, conn_id: usize, state: Arc) -> NetResult<()> { let event = AckFinishConfigurationEvent::new(self, conn_id); - + tokio::spawn(AckFinishConfigurationEvent::trigger(event, state)); - + Ok(()) } } diff --git a/src/lib/net/src/packets/incoming/client_information.rs b/src/lib/net/src/packets/incoming/client_information.rs index 053a04a8..c4654ecc 100644 --- a/src/lib/net/src/packets/incoming/client_information.rs +++ b/src/lib/net/src/packets/incoming/client_information.rs @@ -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")] diff --git a/src/lib/net/src/packets/incoming/handshake.rs b/src/lib/net/src/packets/incoming/handshake.rs index 7dffb511..39d0569f 100644 --- a/src/lib/net/src/packets/incoming/handshake.rs +++ b/src/lib/net/src/packets/incoming/handshake.rs @@ -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; diff --git a/src/lib/net/src/packets/incoming/keep_alive.rs b/src/lib/net/src/packets/incoming/keep_alive.rs index a68c3e56..e3c5f36a 100644 --- a/src/lib/net/src/packets/incoming/keep_alive.rs +++ b/src/lib/net/src/packets/incoming/keep_alive.rs @@ -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; diff --git a/src/lib/net/src/packets/incoming/login_acknowledged.rs b/src/lib/net/src/packets/incoming/login_acknowledged.rs index b1cfd044..ca0e16f3 100644 --- a/src/lib/net/src/packets/incoming/login_acknowledged.rs +++ b/src/lib/net/src/packets/incoming/login_acknowledged.rs @@ -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")] diff --git a/src/lib/net/src/packets/incoming/login_start.rs b/src/lib/net/src/packets/incoming/login_start.rs index 46926442..9b189b52 100644 --- a/src/lib/net/src/packets/incoming/login_start.rs +++ b/src/lib/net/src/packets/incoming/login_start.rs @@ -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")] diff --git a/src/lib/net/src/packets/incoming/ping.rs b/src/lib/net/src/packets/incoming/ping.rs index f7c51d99..5f575041 100644 --- a/src/lib/net/src/packets/incoming/ping.rs +++ b/src/lib/net/src/packets/incoming/ping.rs @@ -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; diff --git a/src/lib/net/src/packets/incoming/server_bound_known_packs.rs b/src/lib/net/src/packets/incoming/server_bound_known_packs.rs index 8551474f..ff48f711 100644 --- a/src/lib/net/src/packets/incoming/server_bound_known_packs.rs +++ b/src/lib/net/src/packets/incoming/server_bound_known_packs.rs @@ -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")] diff --git a/src/lib/net/src/packets/incoming/server_bound_plugin_message.rs b/src/lib/net/src/packets/incoming/server_bound_plugin_message.rs index fc1dd4f2..e3c393aa 100644 --- a/src/lib/net/src/packets/incoming/server_bound_plugin_message.rs +++ b/src/lib/net/src/packets/incoming/server_bound_plugin_message.rs @@ -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")] diff --git a/src/lib/net/src/packets/incoming/set_player_position.rs b/src/lib/net/src/packets/incoming/set_player_position.rs index db46aad3..07088cc2 100644 --- a/src/lib/net/src/packets/incoming/set_player_position.rs +++ b/src/lib/net/src/packets/incoming/set_player_position.rs @@ -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")] diff --git a/src/lib/net/src/packets/incoming/status_request.rs b/src/lib/net/src/packets/incoming/status_request.rs index 4b6b2fed..0ede5ca7 100644 --- a/src/lib/net/src/packets/incoming/status_request.rs +++ b/src/lib/net/src/packets/incoming/status_request.rs @@ -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; diff --git a/src/lib/net/src/packets/mod.rs b/src/lib/net/src/packets/mod.rs index f9d49271..fd487b7a 100644 --- a/src/lib/net/src/packets/mod.rs +++ b/src/lib/net/src/packets/mod.rs @@ -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) -> NetResult<()>; + async fn handle(self, conn_id: usize, state: std::sync::Arc) -> NetResult<()>; } \ No newline at end of file diff --git a/src/lib/net/src/utils/broadcast.rs b/src/lib/net/src/utils/broadcast.rs index 8eb6b148..6cb14cdd 100644 --- a/src/lib/net/src/utils/broadcast.rs +++ b/src/lib/net/src/utils/broadcast.rs @@ -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 Pin + Send + '_>> + Send + Sync>; diff --git a/src/lib/net/src/utils/ecs_helpers.rs b/src/lib/net/src/utils/ecs_helpers.rs index 87d969a3..009e0c15 100644 --- a/src/lib/net/src/utils/ecs_helpers.rs +++ b/src/lib/net/src/utils/ecs_helpers.rs @@ -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(&self, state: GlobalState) -> ECSResult>;