From 08e1d3cd66cb09bd2c6a99a4a4aceb8ef71b8da0 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Thu, 9 Jun 2022 04:01:37 +1000 Subject: [PATCH] Fix mind unvisit crash (#8682) --- Content.Server/Mind/Mind.cs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/Content.Server/Mind/Mind.cs b/Content.Server/Mind/Mind.cs index 61fb22b986..9f51860411 100644 --- a/Content.Server/Mind/Mind.cs +++ b/Content.Server/Mind/Mind.cs @@ -279,7 +279,7 @@ namespace Content.Server.Mind if (entity != null) { - if (!entMan.TryGetComponent(entity.Value, out component)) + if (!entMan.TryGetComponent(entity.Value, out component)) { component = entMan.AddComponent(entity.Value); } @@ -309,10 +309,18 @@ namespace Content.Server.Mind if(OwnedComponent != null) mindSystem.InternalAssignMind(OwnedComponent.Owner, this, OwnedComponent); - if (VisitingEntity != null - && (ghostCheckOverride // to force mind transfer, for example from ControlMobVerb - || !entMan.TryGetComponent(VisitingEntity!, out GhostComponent? ghostComponent) // visiting entity is not a Ghost - || !ghostComponent.CanReturnToBody)) // it is a ghost, but cannot return to body anyway, so it's okay + // Don't do the full deletion cleanup if we're transferring to our visitingentity + if (alreadyAttached) + { + // Set VisitingEntity null first so the removal of VisitingMind doesn't get through Unvisit() and delete what we're visiting. + // Yes this control flow sucks. + VisitingEntity = null; + IoCManager.Resolve().RemoveComponent(entity!.Value); + } + else if (VisitingEntity != null + && (ghostCheckOverride // to force mind transfer, for example from ControlMobVerb + || !entMan.TryGetComponent(VisitingEntity!, out GhostComponent? ghostComponent) // visiting entity is not a Ghost + || !ghostComponent.CanReturnToBody)) // it is a ghost, but cannot return to body anyway, so it's okay { RemoveVisitingEntity(); } @@ -402,11 +410,7 @@ namespace Content.Server.Mind DebugTools.AssertNotNull(oldVisitingEnt); var entities = IoCManager.Resolve(); - if (entities.HasComponent(oldVisitingEnt)) - { - entities.RemoveComponent(oldVisitingEnt); - } - + entities.RemoveComponent(oldVisitingEnt); entities.EventBus.RaiseLocalEvent(oldVisitingEnt, new MindUnvisitedMessage()); }