Skip to content

Commit

Permalink
Try improve PVS exception tolerance a bit more (#5454)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElectroJr authored Sep 23, 2024
1 parent e714dcc commit 41ec2dc
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
10 changes: 6 additions & 4 deletions Robust.Server/GameStates/PvsChunk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,10 @@ public bool PopulateContents(EntityQuery<MetaDataComponent> meta, EntityQuery<Tr
{
// TODO ARCH multi-component queries
if (!meta.TryGetComponent(child, out var childMeta)
|| !xform.TryGetComponent(child, out var childXform))
|| !xform.TryGetComponent(child, out var childXform)
|| childMeta.EntityLifeStage >= EntityLifeStage.Terminating)
{
DebugTools.Assert($"PVS chunk contains a deleted entity: {child}");
DebugTools.Assert($"PVS chunk contains a delete or terminating entity: {child}");
MarkDirty();
return false;
}
Expand Down Expand Up @@ -188,9 +189,10 @@ public bool PopulateContents(EntityQuery<MetaDataComponent> meta, EntityQuery<Tr
{
// TODO ARCH multi-component queries
if (!meta.TryGetComponent(child, out var childMeta)
|| !xform.TryGetComponent(child, out var childXform))
|| !xform.TryGetComponent(child, out var childXform)
|| childMeta.EntityLifeStage >= EntityLifeStage.Terminating)
{
DebugTools.Assert($"PVS chunk contains a deleted entity: {child}");
DebugTools.Assert($"PVS chunk contains a delete or terminating entity: {child}");
MarkDirty();
return false;
}
Expand Down
1 change: 1 addition & 0 deletions Robust.Server/GameStates/PvsSystem.Chunks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ internal void ProcessVisibleChunksSequential()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void AddEntityToChunk(EntityUid uid, MetaDataComponent meta, PvsChunkLocation location)
{
DebugTools.Assert(meta.EntityLifeStage < EntityLifeStage.Terminating);
ref var chunk = ref CollectionsMarshal.GetValueRefOrAddDefault(_chunks, location, out var existing);
if (!existing)
{
Expand Down
3 changes: 2 additions & 1 deletion Robust.Server/GameStates/PvsSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,14 @@ public override void Initialize()

SubscribeLocalEvent<MapChangedEvent>(OnMapChanged);
SubscribeLocalEvent<GridRemovalEvent>(OnGridRemoved);
SubscribeLocalEvent<EntityTerminatingEvent>(OnEntityTerminating);
SubscribeLocalEvent<TransformComponent, TransformStartupEvent>(OnTransformStartup);

_playerManager.PlayerStatusChanged += OnPlayerStatusChanged;
_transform.OnBeforeMoveEvent += OnEntityMove;
EntityManager.EntityAdded += OnEntityAdded;
EntityManager.EntityDeleted += OnEntityDeleted;
EntityManager.AfterEntityFlush += AfterEntityFlush;
EntityManager.BeforeEntityTerminating += OnEntityTerminating;

Subs.CVar(_configManager, CVars.NetPVS, SetPvs, true);
Subs.CVar(_configManager, CVars.NetMaxUpdateRange, OnViewsizeChanged, true);
Expand All @@ -162,6 +162,7 @@ public override void Shutdown()
EntityManager.EntityAdded -= OnEntityAdded;
EntityManager.EntityDeleted -= OnEntityDeleted;
EntityManager.AfterEntityFlush -= AfterEntityFlush;
EntityManager.BeforeEntityTerminating -= OnEntityTerminating;

_parallelMgr.ParallelCountChanged -= ResetParallelism;

Expand Down
9 changes: 9 additions & 0 deletions Robust.Shared/GameObjects/EntityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ public abstract partial class EntityManager : IEntityManager
public event Action<Entity<MetaDataComponent>>? EntityAdded;
public event Action<Entity<MetaDataComponent>>? EntityInitialized;
public event Action<Entity<MetaDataComponent>>? EntityDeleted;

/// <summary>
/// Internal termination event handlers. This is mainly for exception tolerance, we want to ensure that PVS,
/// and other important engine systems can get updated before some content code throws an exception.
/// </summary>
internal event TerminatingEventHandler? BeforeEntityTerminating;
public delegate void TerminatingEventHandler(ref EntityTerminatingEvent ev);

public event Action? BeforeEntityFlush;
public event Action? AfterEntityFlush;

Expand Down Expand Up @@ -556,6 +564,7 @@ private void RecursiveFlagEntityTermination(EntityUid uid,
try
{
var ev = new EntityTerminatingEvent((uid, metadata));
BeforeEntityTerminating?.Invoke(ref ev);
EventBus.RaiseLocalEvent(uid, ref ev, true);
}
catch (Exception e)
Expand Down

0 comments on commit 41ec2dc

Please sign in to comment.