Skip to content

Commit 5e160e2

Browse files
Prevent a potential EnsureComp exception. (#6405)
Fix potential EnsureComponent collision Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com> Co-authored-by: PJB3005 <pieterjan.briers+git@gmail.com>
1 parent 0e54fa7 commit 5e160e2

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

Robust.Shared/GameObjects/EntityManager.Components.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -838,18 +838,16 @@ public bool HasComponent([NotNullWhen(true)] EntityUid? uid, ushort netId, MetaD
838838
[MethodImpl(MethodImplOptions.AggressiveInlining)]
839839
public bool EnsureComponent<T>(ref Entity<T?> entity) where T : IComponent, new()
840840
{
841-
if (entity.Comp != null)
842-
{
843-
// Check for deferred component removal.
844-
if (entity.Comp.LifeStage <= ComponentLifeStage.Running)
845-
{
846-
DebugTools.AssertOwner(entity, entity.Comp);
847-
return true;
848-
}
841+
if (entity.Comp == null)
842+
return EnsureComponent<T>(entity.Owner, out entity.Comp);
849843

850-
RemoveComponent(entity, entity.Comp);
851-
}
844+
DebugTools.AssertOwner(entity, entity.Comp);
845+
846+
// Check for deferred component removal.
847+
if (entity.Comp.LifeStage <= ComponentLifeStage.Running)
848+
return true;
852849

850+
RemoveComponent(entity, entity.Comp);
853851
entity.Comp = AddComponent<T>(entity);
854852
return false;
855853
}

0 commit comments

Comments
 (0)