From dbaf2324f5e58d8d9b4f2b10c2606294d655d3f2 Mon Sep 17 00:00:00 2001 From: core Date: Fri, 12 Jun 2026 22:39:47 -0400 Subject: [PATCH] netcode: entity map important things # Conflicts: # crates/unified/assets/config/world.wc.toml --- crates/unified/src/server/player.rs | 18 ++++++++++++--- crates/unified/src/shared/ecs.rs | 16 +++++++++---- crates/unified/src/shared/net.rs | 6 ++++- crates/unified/src/shared/thrust.rs | 5 +++- crates/xtask/src/util.rs | 36 +++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 9 deletions(-) diff --git a/crates/unified/src/server/player.rs b/crates/unified/src/server/player.rs index 6e2ee3079fd7fe36a1236ac47eb97c6fd88647ff..d99519572de8cb6d50584210db46c06164cef018 100644 --- a/crates/unified/src/server/player.rs +++ b/crates/unified/src/server/player.rs @@ -10,7 +10,9 @@ use crate::server::system_sets::PlayerInputSet; use crate::prelude::*; use crate::shared::world_config::WorldConfigResource; use std::f64::consts::PI; +use bevy_replicon::prelude::{ClientId, FromClient}; use crate::client::components::Me; +use crate::server::ConnectedNetworkEntity; pub fn player_management_plugin(app: &mut App) { app.add_systems( @@ -209,8 +211,9 @@ fn propagate_disconnect( } fn dragging( - mut events: MessageReader, - me: Query>, + mut events: MessageReader>, + clients: Query<&ConnectedNetworkEntity>, + q_ls_me: Query>, mut parts: Query< ( &mut Transform, @@ -232,11 +235,20 @@ fn dragging( let Some(world_config) = &world_config.config else { return; }; - let Ok(player_hearty_entity) = me.single() else { return }; for event in events.read() { debug!(?event, "got drag request event"); + let player_hearty_entity = match event.client_id { + ClientId::Client(client_entity) => { + let ConnectedNetworkEntity { + game_entity: player_hearty_entity, + } = clients.get(client_entity).unwrap(); + *player_hearty_entity + }, + ClientId::Server => q_ls_me.iter().next().unwrap() + }; + let teleport_to_translation; let teleport_to_rotation; let mut new_linvel = None; diff --git a/crates/unified/src/shared/ecs.rs b/crates/unified/src/shared/ecs.rs index bb6bf428d43b0d32721cd2c4cfa8bfc095763b36..7df42fa0952d85f9bba9a837f5ccd079de80b494 100644 --- a/crates/unified/src/shared/ecs.rs +++ b/crates/unified/src/shared/ecs.rs @@ -8,6 +8,7 @@ use crate::prelude::*; use avian2d::prelude::*; use std::collections::HashMap; use std::sync::LazyLock; +use bevy::ecs::entity::MapEntities; use bevy_replicon::prelude::Replicated; #[derive(States, Debug, Clone, PartialEq, Eq, Hash)] @@ -42,16 +43,23 @@ pub struct Player { pub client: Entity, } -#[derive(Message, Debug, Clone)] +#[derive(Message, Debug, Clone, Serialize, Deserialize, MapEntities)] pub struct DragRequestEvent { + #[entities] pub drag_target: Entity, + #[entities] pub action: DragAction, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize, MapEntities)] pub enum DragAction { - Attach { snap_target: Entity, peer_snap: Entity }, - Free { position: Vec2, rotation: Quat }, + Attach { + #[entities] + snap_target: Entity, + #[entities] + peer_snap: Entity + }, + Free { position: Vec2, rotation: Quat }, } #[derive(Component, Serialize, Deserialize, Debug)] diff --git a/crates/unified/src/shared/net.rs b/crates/unified/src/shared/net.rs index ce1e75e63fe86c2511928a88fa3630db117c8890..8591a02b99861f860716c419bb344d3d051275b7 100644 --- a/crates/unified/src/shared/net.rs +++ b/crates/unified/src/shared/net.rs @@ -8,9 +8,10 @@ use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}; use url::{Host, Url}; use crate::shared::attachment::{Joint, JointOf, PartInShip, Peer, Ship, SnapOf, SnapOfJoint}; use crate::shared::config::planet::{Planet, PlanetSpring, PlanetSpringJoint}; -use crate::shared::ecs::{CanCraft, Drill, Part, Player, PlayerStorage, SingleStorage, Temperature}; +use crate::shared::ecs::{CanCraft, DragRequestEvent, Drill, Part, Player, PlayerStorage, SingleStorage, Temperature}; use crate::shared::ecs::clock_sync::{ClientTiming, ServerTiming}; use crate::shared::ecs::thruster::{Thruster, ThrusterOfPart}; +use crate::shared::thrust::ThrustSolution; pub const STARKINGDOMS_PROTOCOL_MAGIC: u64 = 0x5a5a_e37e_4aaa; @@ -20,6 +21,9 @@ pub fn register_replication(app: &mut App) { .add_client_message::(Channel::Ordered) .add_server_message::(Channel::Ordered) + .add_mapped_client_message::(Channel::Ordered) + .add_mapped_client_message::(Channel::Ordered) + .replicate_once::() .replicate_once::() diff --git a/crates/unified/src/shared/thrust.rs b/crates/unified/src/shared/thrust.rs index d6a8648669030b5f21512a955916214fd0019ac7..dc3ec62b2eda8850d61bbc44eaa80c6f584f6243 100644 --- a/crates/unified/src/shared/thrust.rs +++ b/crates/unified/src/shared/thrust.rs @@ -1,11 +1,14 @@ use std::collections::BTreeSet; +use bevy::ecs::entity::MapEntities; use bevy::prelude::{Component, Entity, Message, Resource}; +use serde::{Deserialize, Serialize}; /// A thrust solution, found by the thrust solver on the client. /// `thrusters_on` is the set of thrusters that should be on. /// Any thrusters not in this set should be off. -#[derive(Eq, PartialEq, Debug, Clone, Resource, Component, Message)] +#[derive(Eq, PartialEq, Debug, Clone, Resource, Component, Message, Serialize, Deserialize, MapEntities)] pub struct ThrustSolution { + #[entities] pub thrusters_on: BTreeSet, pub converged: bool } diff --git a/crates/xtask/src/util.rs b/crates/xtask/src/util.rs index 1bce42ec104f2b7b5ec7a5f8b324bfab413d3b67..599d5fb72c0e568b0671b1e2fdb91baf250ce7e7 100644 --- a/crates/xtask/src/util.rs +++ b/crates/xtask/src/util.rs @@ -18,6 +18,13 @@ pub fn cargo(cmd: String) { println!("{} cargo {}", "[cargo]".bold().cyan(), cmd); let mut output = std::process::Command::new(env!("CARGO")); + for (key, _) in std::env::vars_os() { + let Some(key) = key.to_str() else { continue }; + if SANITIZED_ENV_VARS.matches(key) { + output.env_remove(key); + } + } + for command in cmd.split(" ") { output.arg(command); } @@ -29,6 +36,35 @@ pub fn cargo(cmd: String) { } } +#[derive(Debug)] +struct SanitizedEnvVars { + // At the moment we only ban some prefixes, but we may also want to ban env + // vars by exact name in the future. + prefixes: &'static [&'static str], +} + +impl SanitizedEnvVars { + const fn new() -> Self { + // Remove many of the environment variables set in + // https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts. + // This is done to avoid recompilation with crates like ring between + // `cargo clippy` and `cargo xtask clippy`. (This is really a bug in + // both ring's build script and in Cargo.) + // + // The current list is informed by looking at ring's build script, so + // it's not guaranteed to be exhaustive and it may need to grow over + // time. + let prefixes = &["CARGO_PKG_", "CARGO_MANIFEST_", "CARGO_CFG_"]; + Self { prefixes } + } + + fn matches(&self, key: &str) -> bool { + self.prefixes.iter().any(|prefix| key.starts_with(prefix)) + } +} + +static SANITIZED_ENV_VARS: SanitizedEnvVars = SanitizedEnvVars::new(); + pub fn wasmopt(cmd: String) { println!("{} wasm-opt {}", "[wasm-opt]".bold().cyan(), cmd); let mut output = std::process::Command::new("wasm-opt");