@@ -25,44 +25,28 @@ public sealed partial class ContainerSystem : SharedContainerSystem
2525
2626 private readonly HashSet < EntityUid > _updateQueue = new ( ) ;
2727
28- public readonly Dictionary < NetEntity , BaseContainer > ExpectedEntities = new ( ) ;
28+ public readonly Dictionary < NetEntity , BaseContainer > PvsDetachedEntities = new ( ) ;
2929
3030 public override void Initialize ( )
3131 {
3232 base . Initialize ( ) ;
3333
34- EntityManager . EntityInitialized += HandleEntityInitialized ;
3534 SubscribeLocalEvent < ContainerManagerComponent , ComponentHandleState > ( HandleComponentState ) ;
3635
3736 UpdatesBefore . Add ( typeof ( SpriteSystem ) ) ;
3837 }
3938
40- public override void Shutdown ( )
41- {
42- EntityManager . EntityInitialized -= HandleEntityInitialized ;
43- base . Shutdown ( ) ;
44- }
45-
4639 protected override void ValidateMissingEntity ( EntityUid uid , BaseContainer cont , EntityUid missing )
4740 {
4841 var netEntity = GetNetEntity ( missing ) ;
49- DebugTools . Assert ( ExpectedEntities . TryGetValue ( netEntity , out var expectedContainer ) && expectedContainer == cont && cont . ExpectedEntities . Contains ( netEntity ) ) ;
50- }
51-
52- private void HandleEntityInitialized ( Entity < MetaDataComponent > ent )
53- {
54- var ( uid , meta ) = ent ;
55- if ( ! RemoveExpectedEntity ( meta . NetEntity , out var container ) )
56- return ;
57-
58- Insert ( ( uid , TransformQuery . GetComponent ( uid ) , MetaQuery . GetComponent ( uid ) , null ) , container , force : true ) ;
42+ DebugTools . Assert ( PvsDetachedEntities . TryGetValue ( netEntity , out var expectedContainer ) && expectedContainer == cont && cont . PvsDetachedEntities . Contains ( netEntity ) ) ;
5943 }
6044
6145 public override void ShutdownContainer ( BaseContainer container )
6246 {
63- foreach ( var ent in container . ExpectedEntities )
47+ foreach ( var ent in container . PvsDetachedEntities )
6448 {
65- if ( ExpectedEntities . Remove ( ent , out var c ) )
49+ if ( PvsDetachedEntities . Remove ( ent , out var c ) )
6650 DebugTools . Assert ( c == container ) ;
6751 }
6852
@@ -154,17 +138,17 @@ private void HandleComponentState(EntityUid uid, ContainerManagerComponent compo
154138 DebugTools . Assert ( ! container . Contains ( entity ) ) ;
155139 }
156140
157- // Remove entities that were expected, but have been removed from the container.
158- var removedExpected = new ValueList < NetEntity > ( ) ;
159- foreach ( var netEntity in container . ExpectedEntities )
141+ // Remove PVS-detached entities that have been removed from the container.
142+ var removedDetached = new ValueList < NetEntity > ( ) ;
143+ foreach ( var netEntity in container . PvsDetachedEntities )
160144 {
161145 if ( ! stateNetEnts . Contains ( netEntity ) )
162- removedExpected . Add ( netEntity ) ;
146+ removedDetached . Add ( netEntity ) ;
163147 }
164148
165- foreach ( var entityUid in removedExpected . Span )
149+ foreach ( var entityUid in removedDetached . Span )
166150 {
167- RemoveExpectedEntity ( entityUid , out _ ) ;
151+ RemovePvsDetachedEntity ( entityUid , out _ ) ;
168152 }
169153
170154 // Add new entities.
@@ -175,7 +159,7 @@ private void HandleComponentState(EntityUid uid, ContainerManagerComponent compo
175159 if ( ! entity . IsValid ( ) )
176160 {
177161 DebugTools . Assert ( netEnt . IsValid ( ) ) ;
178- AddExpectedEntity ( netEnt , container ) ;
162+ EntityManager . EnsureEntity < ContainerManagerComponent > ( netEnt , uid ) ;
179163 continue ;
180164 }
181165
@@ -191,14 +175,14 @@ private void HandleComponentState(EntityUid uid, ContainerManagerComponent compo
191175 // containers/players.
192176 if ( ( meta . Flags & MetaDataFlags . Detached ) != 0 )
193177 {
194- AddExpectedEntity ( netEnt , container ) ;
178+ AddPvsDetachedEntity ( netEnt , container ) ;
195179 continue ;
196180 }
197181
198182 if ( container . Contains ( entity ) )
199183 continue ;
200184
201- RemoveExpectedEntity ( netEnt , out _ ) ;
185+ RemovePvsDetachedEntity ( netEnt , out _ ) ;
202186 Insert (
203187 ( entity , TransformQuery . GetComponent ( entity ) , MetaQuery . GetComponent ( entity ) , null ) ,
204188 container ,
@@ -224,7 +208,7 @@ protected override void OnParentChanged(ref EntParentChangedMessage message)
224208 if ( message . OldParent != null && message . OldParent . Value . IsValid ( ) )
225209 return ;
226210
227- if ( ! RemoveExpectedEntity ( GetNetEntity ( message . Entity ) , out var container ) )
211+ if ( ! RemovePvsDetachedEntity ( GetNetEntity ( message . Entity ) , out var container ) )
228212 return ;
229213
230214 if ( xform . ParentUid != container . Owner )
@@ -237,42 +221,42 @@ protected override void OnParentChanged(ref EntParentChangedMessage message)
237221 Insert ( message . Entity , container , force : true ) ;
238222 }
239223
240- public void AddExpectedEntity ( NetEntity netEntity , BaseContainer container )
224+ public void AddPvsDetachedEntity ( NetEntity netEntity , BaseContainer container )
241225 {
242226#if DEBUG
243227 var uid = GetEntity ( netEntity ) ;
244228
245229 if ( TryComp ( uid , out MetaDataComponent ? meta ) )
246230 {
247231 DebugTools . Assert ( ( meta . Flags & ( MetaDataFlags . Detached | MetaDataFlags . InContainer ) ) == MetaDataFlags . Detached ,
248- $ "Adding entity { ToPrettyString ( uid ) } to list of expected entities for container { container . ID } in { ToPrettyString ( container . Owner ) } , despite it already being in a container.") ;
232+ $ "Adding entity { ToPrettyString ( uid ) } to list of PVS-detached entities for container { container . ID } in { ToPrettyString ( container . Owner ) } , despite it already being in a container.") ;
249233 }
250234#endif
251235
252- if ( ! ExpectedEntities . TryAdd ( netEntity , container ) )
236+ if ( ! PvsDetachedEntities . TryAdd ( netEntity , container ) )
253237 {
254238 // It is possible that we were expecting this entity in one container, but it has now moved to another
255239 // container, and this entity's state is just being applied before the old container is getting updated.
256- var oldContainer = ExpectedEntities [ netEntity ] ;
257- ExpectedEntities [ netEntity ] = container ;
258- DebugTools . Assert ( oldContainer . ExpectedEntities . Contains ( netEntity ) ,
259- $ "Entity { netEntity } is expected , but not expected in the given container? Container: { oldContainer . ID } in { ToPrettyString ( oldContainer . Owner ) } ") ;
260- oldContainer . ExpectedEntities . Remove ( netEntity ) ;
240+ var oldContainer = PvsDetachedEntities [ netEntity ] ;
241+ PvsDetachedEntities [ netEntity ] = container ;
242+ DebugTools . Assert ( oldContainer . PvsDetachedEntities . Contains ( netEntity ) ,
243+ $ "Entity { netEntity } is PVS-detached , but not tracked in the given container? Container: { oldContainer . ID } in { ToPrettyString ( oldContainer . Owner ) } ") ;
244+ oldContainer . PvsDetachedEntities . Remove ( netEntity ) ;
261245 }
262246
263- DebugTools . Assert ( ! container . ExpectedEntities . Contains ( netEntity ) ,
264- $ "Contained entity { netEntity } was not yet expected by the system, but was already expected by the container: { container . ID } in { ToPrettyString ( container . Owner ) } ") ;
265- container . ExpectedEntities . Add ( netEntity ) ;
247+ DebugTools . Assert ( ! container . PvsDetachedEntities . Contains ( netEntity ) ,
248+ $ "Contained entity { netEntity } was not yet PVS-detached according to the system, but was already tracked by the container: { container . ID } in { ToPrettyString ( container . Owner ) } ") ;
249+ container . PvsDetachedEntities . Add ( netEntity ) ;
266250 }
267251
268- public bool RemoveExpectedEntity ( NetEntity netEntity , [ NotNullWhen ( true ) ] out BaseContainer ? container )
252+ public bool RemovePvsDetachedEntity ( NetEntity netEntity , [ NotNullWhen ( true ) ] out BaseContainer ? container )
269253 {
270- if ( ! ExpectedEntities . Remove ( netEntity , out container ) )
254+ if ( ! PvsDetachedEntities . Remove ( netEntity , out container ) )
271255 return false ;
272256
273- DebugTools . Assert ( container . ExpectedEntities . Contains ( netEntity ) ,
274- $ "While removing expected contained entity { ToPrettyString ( netEntity ) } , the entity was missing from the container expected set. Container: { container . ID } in { ToPrettyString ( container . Owner ) } ") ;
275- container . ExpectedEntities . Remove ( netEntity ) ;
257+ DebugTools . Assert ( container . PvsDetachedEntities . Contains ( netEntity ) ,
258+ $ "While removing PVS-detached contained entity { ToPrettyString ( netEntity ) } , the entity was missing from the container detached set. Container: { container . ID } in { ToPrettyString ( container . Owner ) } ") ;
259+ container . PvsDetachedEntities . Remove ( netEntity ) ;
276260 return true ;
277261 }
278262
0 commit comments