33 using System ;
44 using System . Collections . Generic ;
55 using System . Linq ;
6- using System . Runtime . Remoting ;
76 using System . Threading ;
87 using EssentialsPlugin . ProcessHandlers ;
98 using EssentialsPlugin . Utility ;
109 using Sandbox . ModAPI ;
11- using Sandbox . ModAPI . Ingame ;
1210 using SEModAPIInternal . API . Common ;
1311 using VRage . ModAPI ;
1412 using VRage . ObjectBuilders ;
1513 using VRageMath ;
16- using IMyProductionBlock = Sandbox . ModAPI . Ingame . IMyProductionBlock ;
1714 using Sandbox . Game . Entities ;
1815 using Sandbox . Game . World ;
1916 using Sandbox . Game . Entities . Blocks ;
2017 using Sandbox . Game . Entities . Cube ;
18+ using Sandbox . ModAPI . Ingame ;
19+ using SpaceEngineers . Game . ModAPI ;
2120 using SpaceEngineers . Game . ModAPI . Ingame ;
2221 using VRage . Game ;
2322 using VRage . Game . Entity ;
2423 using VRage . Game . ModAPI ;
25-
24+ using IMyProductionBlock = Sandbox . ModAPI . IMyProductionBlock ;
25+
2626 public class EntityManagement
2727 {
2828 private static volatile bool _checkReveal ;
2929 private static volatile bool _checkConceal ;
3030 private static bool _oldInit ;
31- private static readonly List < long > RemovedGrids = new List < long > ( ) ;
31+ public static readonly HashSet < long > RemovedGrids = new HashSet < long > ( ) ;
3232 private static readonly List < ulong > Online = new List < ulong > ( ) ;
3333
34- public static HashSet < MyEntity > UnregisteredEntities = new HashSet < MyEntity > ( ) ;
34+ // public static HashSet<MyEntity> UnregisteredEntities = new HashSet<MyEntity>();
3535
3636
3737 public static void CheckAndConcealEntities ( )
@@ -71,7 +71,10 @@ public static void CheckAndConcealEntities( )
7171 if ( grid . Physics == null ) //projection
7272 continue ;
7373
74- if ( UnregisteredEntities . Contains ( grid ) )
74+ //if ( UnregisteredEntities.Contains( (MyEntity)grid ) )
75+ // continue;
76+
77+ if ( RemovedGrids . Contains ( grid . EntityId ) )
7578 continue ;
7679
7780 if ( grid . GridSizeEnum != MyCubeSize . Small )
@@ -222,8 +225,8 @@ private static void ConcealEntity( MyEntity entity )
222225 pos = 4 ;
223226 //else
224227 //{
225- UnregisteredEntities . Add ( entity ) ;
226- Wrapper . GameAction ( ( ) => UnregisterHierarchy ( entity ) ) ;
228+ // UnregisteredEntities.Add( entity );
229+ Wrapper . GameAction ( ( ) => UnregisterHierarchy ( entity . GetTopMostParent ( ) ) ) ;
227230 if ( PluginSettings . Instance . DynamicShowMessages )
228231 Essentials . Log . Info ( $ "Concealed - Id: { entity . EntityId } -> Display: { entity . DisplayName } OwnerId: { ownerId } OwnerName: { ownerName } " ) ;
229232
@@ -311,9 +314,11 @@ public static void CheckAndRevealEntities( )
311314 _checkReveal = true ;
312315 try
313316 {
314- foreach ( MyEntity entity in UnregisteredEntities )
317+ HashSet < MyEntity > entities = new HashSet < MyEntity > ( ) ;
318+ MyAPIGateway . Utilities . InvokeOnGameThread ( ( ) => entities = MyEntities . GetEntities ( ) ) ;
319+ foreach ( MyEntity entity in entities . ToArray ( ) )
315320 {
316- if ( entity . Closed || entity . MarkedForClose )
321+ if ( entity . Closed || entity . MarkedForClose || ! RemovedGrids . Contains ( entity . EntityId ) )
317322 continue ;
318323
319324 if ( ! ( entity is MyCubeGrid ) )
@@ -345,7 +350,7 @@ public static void CheckAndRevealEntities( )
345350
346351 if ( found )
347352 {
348- Wrapper . GameAction ( ( ) => ReregisterHierarchy ( entity ) ) ;
353+ Wrapper . GameAction ( ( ) => ReregisterHierarchy ( entity . GetTopMostParent ( ) ) ) ;
349354
350355 if ( PluginSettings . Instance . DynamicShowMessages )
351356 Essentials . Log . Info ( "Revealed - Id: {0} -> Display: {1} OwnerId: {2} OwnerName: {3} Reason: {4}" ,
@@ -563,35 +568,31 @@ public static void RevealEntityObsolete( KeyValuePair<IMyEntity, string> item )
563568
564569 static public void RevealAll ( )
565570 {
566- while ( _checkReveal )
567- {
568- }
569-
570571 _checkReveal = true ;
571572
572573 CheckAndRevealEntitiesObsolete ( ) ;
573574
574575 HashSet < MyEntity > entities = new HashSet < MyEntity > ( ) ;
575576 Wrapper . GameAction ( ( ) => entities = MyEntities . GetEntities ( ) ) ;
576577
577- foreach ( var entity in entities )
578+ foreach ( var entity in entities . ToArray ( ) )
578579 {
579580 var grid = entity as MyCubeGrid ;
580581 if ( grid == null )
581582 continue ;
582583
583- if ( PluginSettings . Instance . DynamicShowMessages && UnregisteredEntities . Contains ( entity ) )
584+ if ( PluginSettings . Instance . DynamicShowMessages && RemovedGrids . Contains ( entity . EntityId ) )
584585 Essentials . Log . Info ( "Revealed - Id: {0} -> Display: {1} OwnerId: {2} OwnerName: {3} Reason: {4}" ,
585586 entity . EntityId ,
586587 entity . DisplayName . Replace ( "\r " , "" ) . Replace ( "\n " , "" ) ,
587588 ( ( MyCubeGrid ) entity ) . BigOwners . FirstOrDefault ( ) ,
588589 PlayerMap . Instance . GetPlayerNameFromPlayerId ( ( ( MyCubeGrid ) entity ) . BigOwners . FirstOrDefault ( ) ) ,
589590 "Force reveal" ) ;
590591
591- Wrapper . GameAction ( ( ) => ReregisterHierarchy ( entity ) ) ;
592+ Wrapper . GameAction ( ( ) => ReregisterHierarchy ( entity . GetTopMostParent ( ) ) ) ;
592593 }
593594
594- UnregisteredEntities . Clear ( ) ;
595+ RemovedGrids . Clear ( ) ;
595596 _checkReveal = false ;
596597 }
597598
@@ -617,55 +618,69 @@ public static void SetOnline( ulong steamId, bool online )
617618 }
618619 }
619620
620- private static void UnregisterHierarchy ( MyEntity entity )
621+ private static void UnregisterHierarchy ( MyEntity entity , bool top = true )
621622 {
622623 if ( entity . Hierarchy == null )
623624 return ;
624625
625- // if ( UnregisteredEntities .Contains( entity ) )
626- // return;
626+ if ( RemovedGrids . Contains ( entity . EntityId ) )
627+ return ;
627628
628629 foreach ( var child in entity . Hierarchy . Children )
629630 {
630631 MyEntity childEntity = ( MyEntity ) child . Container . Entity ;
631- UnregisterHierarchy ( childEntity ) ;
632+ UnregisterHierarchy ( childEntity , false ) ;
632633 MyEntities . UnregisterForUpdate ( childEntity ) ;
633- childEntity . RemoveFromGamePruningStructure ( ) ;
634+ // childEntity.RemoveFromGamePruningStructure();
634635
635636 //child.Container.Entity.InScene = false;
636637
637- if ( ! PluginSettings . Instance . DynamicConcealPhysics )
638- continue ;
639-
640- if ( child . Container . Entity . Physics != null )
641- child . Container . Entity . Physics . Enabled = false ;
642638 }
643639
644- UnregisteredEntities . Add ( entity ) ;
640+ if ( ! top )
641+ return ;
642+
643+ RemovedGrids . Add ( entity . EntityId ) ;
644+
645+ MyEntities . UnregisterForUpdate ( entity ) ;
646+ entity . RemoveFromGamePruningStructure ( ) ;
647+ if ( ! PluginSettings . Instance . DynamicConcealPhysics )
648+ return ;
649+
650+ if ( entity . Physics != null )
651+ entity . Physics . Enabled = false ;
652+
645653 }
646654
647- private static void ReregisterHierarchy ( MyEntity entity )
655+ private static void ReregisterHierarchy ( MyEntity entity , bool top = true )
648656 {
649657 if ( entity . Hierarchy == null )
650658 return ;
651-
652- foreach ( var child in entity . Hierarchy . Children )
659+
660+ RemovedGrids . Remove ( entity . EntityId ) ;
661+
662+ foreach ( var child in entity . Hierarchy . Children )
653663 {
654664 MyEntity childEntity = ( MyEntity ) child . Container . Entity ;
655- ReregisterHierarchy ( childEntity ) ;
656- MyEntities . RegisterForUpdate ( childEntity ) ;
657- childEntity . AddToGamePruningStructure ( ) ;
665+ ReregisterHierarchy ( childEntity , false ) ;
666+ MyEntities . RegisterForUpdate ( childEntity ) ;
667+ // childEntity.AddToGamePruningStructure();
658668
659669 //child.Container.Entity.InScene = true;
660670
661- if ( ! PluginSettings . Instance . DynamicConcealPhysics )
662- continue ;
663-
664- if ( child . Container . Entity . Physics != null )
665- child . Container . Entity . Physics . Enabled = true ;
666671 }
672+ if ( ! top )
673+ return ;
674+
675+ MyEntities . RegisterForUpdate ( entity ) ;
676+ entity . AddToGamePruningStructure ( ) ;
677+
678+ if ( ! PluginSettings . Instance . DynamicConcealPhysics )
679+ return ;
667680
668- UnregisteredEntities . Remove ( entity ) ;
681+ if ( entity . Physics != null )
682+ entity . Physics . Enabled = true ;
683+ //UnregisteredEntities.Remove( entity );
669684 }
670685 }
671686}
0 commit comments