From d75889ed750351769f174a7a601d706782406ba1 Mon Sep 17 00:00:00 2001 From: Isse Date: Sat, 9 Dec 2023 22:10:10 +0100 Subject: [PATCH] don't insert event busses on client --- client/src/lib.rs | 7 --- common/src/event.rs | 12 ++-- common/state/src/state.rs | 3 +- common/systems/src/phys.rs | 25 +++++--- server/src/events/entity_manipulation.rs | 76 ++++++++++++------------ server/src/events/group_manip.rs | 6 +- server/src/events/inventory_manip.rs | 12 ++-- server/src/events/invite.rs | 16 ++--- server/src/lib.rs | 1 + server/src/sys/msg/terrain.rs | 2 +- server/src/weather/tick.rs | 2 +- 11 files changed, 81 insertions(+), 81 deletions(-) diff --git a/client/src/lib.rs b/client/src/lib.rs index a0e9d4dc1e..cf4ea921c1 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -1982,13 +1982,6 @@ impl Client { &self.connected_server_constants, |_, _| {}, ); - // TODO: TODO - // TODO: avoid emitting these in the first place - // let _ = self - // .state - // .ecs() - // .fetch::>() - // .recv_all(); // TODO: avoid emitting these in the first place OR actually use outcomes // generated locally on the client (if they can be deduplicated from diff --git a/common/src/event.rs b/common/src/event.rs index 412f3031c4..294efc6ba1 100644 --- a/common/src/event.rs +++ b/common/src/event.rs @@ -556,35 +556,35 @@ macro_rules! event_emitters { $( #[derive(specs::SystemData)] pub struct $read_data<'a> { - $($ev_ident: specs::Read<'a, $crate::event::EventBus<$ty>>),+ + $($ev_ident: Option>>),+ } impl<'a> $read_data<'a> { #[allow(unused)] pub fn get_emitters(&self) -> $emitters { $emitters { - $($ev_ident: self.$ev_ident.emitter()),+ + $($ev_ident: self.$ev_ident.as_ref().map(|e| e.emitter())),+ } } } pub struct $emitters<'a> { - $($ev_ident: $crate::event::Emitter<'a, $ty>),+ + $($ev_ident: Option<$crate::event::Emitter<'a, $ty>>),+ } impl<'a> $emitters<'a> { #[allow(unused)] pub fn append(&mut self, mut other: Self) { $( - self.$ev_ident.append(&mut other.$ev_ident.events); + self.$ev_ident.as_mut().zip(other.$ev_ident).map(|(a, mut b)| a.append(&mut b.events)); )+ } } $( impl<'a> $crate::event::EmitExt<$ty> for $emitters<'a> { - fn emit(&mut self, event: $ty) { self.$ev_ident.emit(event) } - fn emit_many(&mut self, events: impl IntoIterator) { self.$ev_ident.emit_many(events) } + fn emit(&mut self, event: $ty) { self.$ev_ident.as_mut().map(|e| e.emit(event)); } + fn emit_many(&mut self, events: impl IntoIterator) { self.$ev_ident.as_mut().map(|e| e.emit_many(events)); } } )+ )+ diff --git a/common/state/src/state.rs b/common/state/src/state.rs index fba1c9e3be..fada52ac8c 100644 --- a/common/state/src/state.rs +++ b/common/state/src/state.rs @@ -8,7 +8,7 @@ use common::uid::IdMaps; use common::{ calendar::Calendar, comp, - event::{register_event_busses, EventBus, LocalEvent}, + event::{EventBus, LocalEvent}, link::Is, mounting::{Mount, Rider, VolumeRider, VolumeRiders}, outcome::Outcome, @@ -331,7 +331,6 @@ impl State { ecs.insert(SlowJobPool::new(slow_limit, 10_000, thread_pool)); // TODO: only register on the server - register_event_busses(&mut ecs); ecs.insert(comp::group::GroupManager::default()); ecs.insert(SysMetrics::default()); ecs.insert(PhysicsMetrics::default()); diff --git a/common/systems/src/phys.rs b/common/systems/src/phys.rs index 5413eed947..89377b3f6a 100644 --- a/common/systems/src/phys.rs +++ b/common/systems/src/phys.rs @@ -7,7 +7,8 @@ use common::{ PosVelOriDefer, PreviousPhysCache, Projectile, Scale, Stats, Sticky, Vel, }, consts::{AIR_DENSITY, FRIC_GROUND, GRAVITY}, - event::{EventBus, LandOnGroundEvent}, + event::{EmitExt, EventBus, LandOnGroundEvent}, + event_emitters, link::Is, mounting::{Rider, VolumeRider}, outcome::Outcome, @@ -123,6 +124,12 @@ fn calc_z_limit(char_state_maybe: Option<&CharacterState>, collider: &Collider) collider.get_z_limits(modifier) } +event_emitters! { + struct Events[Emitters] { + land_on_ground: LandOnGroundEvent, + } +} + /// This system applies forces and calculates new positions and velocities. #[derive(Default)] pub struct Sys; @@ -130,10 +137,10 @@ pub struct Sys; #[derive(SystemData)] pub struct PhysicsRead<'a> { entities: Entities<'a>, + events: Events<'a>, uids: ReadStorage<'a, Uid>, terrain: ReadExpect<'a, TerrainGrid>, dt: Read<'a, DeltaTime>, - land_on_ground_event: Read<'a, EventBus>, game_mode: ReadExpect<'a, GameMode>, scales: ReadStorage<'a, Scale>, stickies: ReadStorage<'a, Sticky>, @@ -1364,16 +1371,16 @@ impl<'a> PhysicsData<'a> { } drop(guard); - let mut event_emitter = read.land_on_ground_event.emitter(); - land_on_grounds - .into_iter() - .for_each(|(entity, vel, surface_normal)| { - event_emitter.emit(LandOnGroundEvent { + let mut emitters = read.events.get_emitters(); + emitters.emit_many( + land_on_grounds + .into_iter() + .map(|(entity, vel, surface_normal)| LandOnGroundEvent { entity, vel: vel.0, surface_normal, - }); - }); + }), + ); } fn update_cached_spatial_grid(&mut self) { diff --git a/server/src/events/entity_manipulation.rs b/server/src/events/entity_manipulation.rs index dfa0776771..6e1ede706c 100644 --- a/server/src/events/entity_manipulation.rs +++ b/server/src/events/entity_manipulation.rs @@ -52,8 +52,8 @@ use common_state::{AreasContainer, BlockChange, NoDurabilityArea}; use hashbrown::HashSet; use rand::Rng; use specs::{ - shred, DispatcherBuilder, Entities, Entity as EcsEntity, Entity, Join, LendJoin, ReadExpect, - ReadStorage, SystemData, Write, WriteExpect, WriteStorage, + shred, DispatcherBuilder, Entities, Entity as EcsEntity, Entity, Join, LendJoin, Read, + ReadExpect, ReadStorage, SystemData, Write, WriteExpect, WriteStorage, }; use std::{collections::HashMap, iter, sync::Arc, time::Duration}; use tracing::{debug, warn}; @@ -124,16 +124,16 @@ impl ServerEvent for PoiseChangeEvent { impl ServerEvent for HealthChangeEvent { type SystemData<'a> = ( Entities<'a>, + Read<'a, EventBus>, ReadStorage<'a, Pos>, ReadStorage<'a, Uid>, WriteStorage<'a, Agent>, WriteStorage<'a, Health>, - WriteExpect<'a, EventBus>, ); fn handle( events: impl ExactSizeIterator, - (entities, positions, uids, mut agents, mut healths, outcomes): Self::SystemData<'_>, + (entities, outcomes, positions, uids, mut agents, mut healths): Self::SystemData<'_>, ) { let mut outcomes_emitter = outcomes.emitter(); for ev in events { @@ -272,17 +272,17 @@ fn handle_exp_gain( pub struct DestroyEventData<'a> { entities: Entities<'a>, rtsim: WriteExpect<'a, RtSim>, - id_maps: ReadExpect<'a, IdMaps>, + id_maps: Read<'a, IdMaps>, msm: ReadExpect<'a, MaterialStatManifest>, ability_map: ReadExpect<'a, AbilityMap>, - time: ReadExpect<'a, Time>, + time: Read<'a, Time>, world: ReadExpect<'a, Arc>, index: ReadExpect<'a, world::IndexOwned>, - areas_container: ReadExpect<'a, AreasContainer>, - outcomes: ReadExpect<'a, EventBus>, - create_item_drop: ReadExpect<'a, EventBus>, - delete_event: ReadExpect<'a, EventBus>, - chat_events: ReadExpect<'a, EventBus>, + areas_container: Read<'a, AreasContainer>, + outcomes: Read<'a, EventBus>, + create_item_drop: Read<'a, EventBus>, + delete_event: Read<'a, EventBus>, + chat_events: Read<'a, EventBus>, melees: WriteStorage<'a, comp::Melee>, beams: WriteStorage<'a, comp::Beam>, skill_sets: WriteStorage<'a, SkillSet>, @@ -745,7 +745,7 @@ impl ServerEvent for DeleteEvent { Entities<'a>, Write<'a, comp::group::GroupManager>, Write<'a, Trades>, - ReadExpect<'a, IdMaps>, + Read<'a, IdMaps>, WriteStorage<'a, Group>, WriteStorage<'a, Agent>, ReadStorage<'a, Uid>, @@ -819,10 +819,10 @@ impl ServerEvent for DeleteEvent { impl ServerEvent for LandOnGroundEvent { type SystemData<'a> = ( - ReadExpect<'a, Time>, + Read<'a, Time>, ReadExpect<'a, MaterialStatManifest>, - ReadExpect<'a, EventBus>, - ReadExpect<'a, EventBus>, + Read<'a, EventBus>, + Read<'a, EventBus>, ReadStorage<'a, PhysicsState>, ReadStorage<'a, CharacterState>, ReadStorage<'a, comp::Mass>, @@ -933,7 +933,7 @@ impl ServerEvent for LandOnGroundEvent { impl ServerEvent for RespawnEvent { type SystemData<'a> = ( - ReadExpect<'a, SpawnPoint>, + Read<'a, SpawnPoint>, WriteStorage<'a, Health>, WriteStorage<'a, comp::Combo>, WriteStorage<'a, Pos>, @@ -995,15 +995,15 @@ event_emitters! { impl ServerEvent for ExplosionEvent { type SystemData<'a> = ( Entities<'a>, - WriteExpect<'a, BlockChange>, - ReadExpect<'a, Settings>, - ReadExpect<'a, Time>, - ReadExpect<'a, IdMaps>, - ReadExpect<'a, CachedSpatialGrid>, + Write<'a, BlockChange>, + Read<'a, Settings>, + Read<'a, Time>, + Read<'a, IdMaps>, + Read<'a, CachedSpatialGrid>, ReadExpect<'a, TerrainGrid>, ReadExpect<'a, MaterialStatManifest>, ReadExplosionEvents<'a>, - ReadExpect<'a, EventBus>, + Read<'a, EventBus>, ReadStorage<'a, Group>, ReadStorage<'a, Auras>, ReadStorage<'a, Pos>, @@ -1502,7 +1502,7 @@ impl ServerEvent for BonkEvent { type SystemData<'a> = ( Write<'a, BlockChange>, ReadExpect<'a, TerrainGrid>, - ReadExpect<'a, EventBus>, + Read<'a, EventBus>, ); fn handle( @@ -1592,7 +1592,7 @@ impl ServerEvent for AuraEvent { impl ServerEvent for BuffEvent { type SystemData<'a> = ( - ReadExpect<'a, Time>, + Read<'a, Time>, WriteStorage<'a, comp::Buffs>, ReadStorage<'a, Body>, ReadStorage<'a, Health>, @@ -1696,8 +1696,8 @@ impl ServerEvent for EnergyChangeEvent { impl ServerEvent for ComboChangeEvent { type SystemData<'a> = ( - ReadExpect<'a, Time>, - ReadExpect<'a, EventBus>, + Read<'a, Time>, + Read<'a, EventBus>, WriteStorage<'a, comp::Combo>, ReadStorage<'a, Uid>, ); @@ -1723,9 +1723,9 @@ impl ServerEvent for ComboChangeEvent { impl ServerEvent for ParryHookEvent { type SystemData<'a> = ( - ReadExpect<'a, Time>, - ReadExpect<'a, EventBus>, - ReadExpect<'a, EventBus>, + Read<'a, Time>, + Read<'a, EventBus>, + Read<'a, EventBus>, WriteStorage<'a, CharacterState>, ReadStorage<'a, Uid>, ReadStorage<'a, Stats>, @@ -1792,7 +1792,7 @@ impl ServerEvent for ParryHookEvent { impl ServerEvent for TeleportToEvent { type SystemData<'a> = ( - ReadExpect<'a, IdMaps>, + Read<'a, IdMaps>, WriteStorage<'a, Pos>, WriteStorage<'a, comp::ForceUpdate>, ); @@ -1826,12 +1826,12 @@ impl ServerEvent for EntityAttackedHookEvent { type SystemData<'a> = ( Entities<'a>, Write<'a, Trades>, - ReadExpect<'a, IdMaps>, - ReadExpect<'a, Time>, - ReadExpect<'a, EventBus>, - ReadExpect<'a, EventBus>, - ReadExpect<'a, EventBus>, - ReadExpect<'a, EventBus>, + Read<'a, IdMaps>, + Read<'a, Time>, + Read<'a, EventBus>, + Read<'a, EventBus>, + Read<'a, EventBus>, + Read<'a, EventBus>, WriteStorage<'a, CharacterState>, WriteStorage<'a, Poise>, WriteStorage<'a, Agent>, @@ -2089,7 +2089,7 @@ impl ServerEvent for RemoveLightEmitterEvent { impl ServerEvent for TeleportToPositionEvent { type SystemData<'a> = ( - ReadExpect<'a, IdMaps>, + Read<'a, IdMaps>, WriteStorage<'a, Is>, WriteStorage<'a, Pos>, WriteStorage<'a, comp::ForceUpdate>, @@ -2131,7 +2131,7 @@ impl ServerEvent for TeleportToPositionEvent { impl ServerEvent for StartTeleportingEvent { type SystemData<'a> = ( - ReadExpect<'a, Time>, + Read<'a, Time>, WriteStorage<'a, comp::Teleporting>, ReadStorage<'a, Pos>, ReadStorage<'a, comp::Object>, diff --git a/server/src/events/group_manip.rs b/server/src/events/group_manip.rs index f458cd7f89..acf79b1176 100644 --- a/server/src/events/group_manip.rs +++ b/server/src/events/group_manip.rs @@ -10,7 +10,7 @@ use common::{ uid::{IdMaps, Uid}, }; use common_net::msg::ServerGeneral; -use specs::{world::Entity, Entities, ReadExpect, ReadStorage, WriteExpect, WriteStorage}; +use specs::{world::Entity, Entities, Read, ReadStorage, Write, WriteStorage}; use super::ServerEvent; @@ -111,8 +111,8 @@ pub fn update_map_markers<'a>( impl ServerEvent for GroupManipEvent { type SystemData<'a> = ( Entities<'a>, - WriteExpect<'a, GroupManager>, - ReadExpect<'a, IdMaps>, + Write<'a, GroupManager>, + Read<'a, IdMaps>, WriteStorage<'a, Group>, ReadStorage<'a, Client>, ReadStorage<'a, Uid>, diff --git a/server/src/events/inventory_manip.rs b/server/src/events/inventory_manip.rs index 6f845d9066..e0823b843d 100644 --- a/server/src/events/inventory_manip.rs +++ b/server/src/events/inventory_manip.rs @@ -1,8 +1,8 @@ use hashbrown::HashSet; use rand::{seq::IteratorRandom, Rng}; use specs::{ - join::Join, shred, DispatcherBuilder, Entities, Entity as EcsEntity, ReadExpect, ReadStorage, - SystemData, WriteExpect, WriteStorage, + join::Join, shred, DispatcherBuilder, Entities, Entity as EcsEntity, Read, ReadExpect, + ReadStorage, SystemData, Write, WriteStorage, }; use tracing::{debug, error, warn}; use vek::{Rgb, Vec3}; @@ -77,11 +77,11 @@ event_emitters! { pub struct InventoryManipData<'a> { entities: Entities<'a>, events: Events<'a>, - block_change: WriteExpect<'a, common_state::BlockChange>, - trades: WriteExpect<'a, Trades>, + block_change: Write<'a, common_state::BlockChange>, + trades: Write<'a, Trades>, terrain: ReadExpect<'a, common::terrain::TerrainGrid>, - id_maps: ReadExpect<'a, IdMaps>, - time: ReadExpect<'a, Time>, + id_maps: Read<'a, IdMaps>, + time: Read<'a, Time>, ability_map: ReadExpect<'a, AbilityMap>, msm: ReadExpect<'a, MaterialStatManifest>, inventories: WriteStorage<'a, comp::Inventory>, diff --git a/server/src/events/invite.rs b/server/src/events/invite.rs index dcf6e395fd..c8c7101d6b 100644 --- a/server/src/events/invite.rs +++ b/server/src/events/invite.rs @@ -19,7 +19,7 @@ use common::{ }; use common_net::msg::{InviteAnswer, ServerGeneral}; use specs::{ - shred, DispatcherBuilder, Entities, Entity, ReadExpect, ReadStorage, SystemData, WriteExpect, + shred, DispatcherBuilder, Entities, Entity, Read, ReadExpect, ReadStorage, SystemData, Write, WriteStorage, }; use std::time::{Duration, Instant}; @@ -37,10 +37,10 @@ pub(super) fn register_event_systems(builder: &mut DispatcherBuilder) { impl ServerEvent for InitiateInviteEvent { type SystemData<'a> = ( - WriteExpect<'a, Trades>, - ReadExpect<'a, Settings>, - ReadExpect<'a, IdMaps>, - ReadExpect<'a, GroupManager>, + Write<'a, Trades>, + Read<'a, Settings>, + Read<'a, IdMaps>, + Read<'a, GroupManager>, WriteStorage<'a, PendingInvites>, WriteStorage<'a, Agent>, WriteStorage<'a, Invite>, @@ -215,10 +215,10 @@ impl ServerEvent for InitiateInviteEvent { #[derive(SystemData)] pub struct InviteResponseData<'a> { entities: Entities<'a>, - group_manager: WriteExpect<'a, GroupManager>, - trades: WriteExpect<'a, Trades>, + group_manager: Write<'a, GroupManager>, + trades: Write<'a, Trades>, index: ReadExpect<'a, world::IndexOwned>, - id_maps: ReadExpect<'a, IdMaps>, + id_maps: Read<'a, IdMaps>, invites: WriteStorage<'a, Invite>, pending_invites: WriteStorage<'a, PendingInvites>, groups: WriteStorage<'a, Group>, diff --git a/server/src/lib.rs b/server/src/lib.rs index d0805dfaac..f70cd28782 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -328,6 +328,7 @@ impl Server { } }, ); + register_event_busses(state.ecs_mut()); state.ecs_mut().insert(battlemode_buffer); state.ecs_mut().insert(settings.clone()); state.ecs_mut().insert(editable_settings); diff --git a/server/src/sys/msg/terrain.rs b/server/src/sys/msg/terrain.rs index 0656fafe0e..49e831231e 100644 --- a/server/src/sys/msg/terrain.rs +++ b/server/src/sys/msg/terrain.rs @@ -22,7 +22,7 @@ impl<'a> System<'a> for Sys { type SystemData = ( Entities<'a>, Read<'a, EventBus>, - ReadExpect<'a, EventBus>, + Read<'a, EventBus>, ReadExpect<'a, TerrainGrid>, ReadExpect<'a, Lod>, ReadExpect<'a, NetworkRequestMetrics>, diff --git a/server/src/weather/tick.rs b/server/src/weather/tick.rs index fe2ec70a6c..f34d0fc55a 100644 --- a/server/src/weather/tick.rs +++ b/server/src/weather/tick.rs @@ -56,7 +56,7 @@ impl<'a> System<'a> for Sys { Write<'a, Option>, WriteExpect<'a, WeatherGrid>, WriteExpect<'a, SlowJobPool>, - ReadExpect<'a, EventBus>, + Read<'a, EventBus>, ReadExpect<'a, Arc>, ReadStorage<'a, Client>, ReadStorage<'a, comp::Pos>,