Skip to content

Commit

Permalink
Cleanup more follower leaks (#20902)
Browse files Browse the repository at this point in the history
* Cleanup more follower leaks

Rather than check stop everywhere we'll just check it on start and cleanup the old following. If someone were already following and followed something new the FollowedComponent would never get cleaned up and would never have its ref to the entity removed.

* Don't cause archetype changes
  • Loading branch information
metalgearsloth authored Oct 14, 2023
1 parent 8555a8e commit aac17c3
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions Content.Shared/Follower/FollowerSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,20 @@ public void StartFollowingEntity(EntityUid follower, EntityUid entity)
targetXform = Transform(targetXform.ParentUid);
}

var followerComp = EnsureComp<FollowerComponent>(follower);
// Cleanup old following.
if (TryComp<FollowerComponent>(follower, out var followerComp))
{
// Already following you goob
if (followerComp.Following == entity)
return;

StopFollowingEntity(follower, followerComp.Following, deparent: false, removeComp: false);
}
else
{
followerComp = AddComp<FollowerComponent>(follower);
}

followerComp.Following = entity;

var followedComp = EnsureComp<FollowedComponent>(entity);
Expand Down Expand Up @@ -167,14 +180,14 @@ public void StartFollowingEntity(EntityUid follower, EntityUid entity)

RaiseLocalEvent(follower, followerEv);
RaiseLocalEvent(entity, entityEv);
Dirty(followedComp);
Dirty(entity, followedComp);
}

/// <summary>
/// Forces an entity to stop following another entity, if it is doing so.
/// </summary>
/// <param name="deparent">Should the entity deparent itself</param>
public void StopFollowingEntity(EntityUid uid, EntityUid target, FollowedComponent? followed = null, bool deparent = true)
public void StopFollowingEntity(EntityUid uid, EntityUid target, FollowedComponent? followed = null, bool deparent = true, bool removeComp = true)
{
if (!Resolve(target, ref followed, false))
return;
Expand All @@ -186,8 +199,12 @@ public void StopFollowingEntity(EntityUid uid, EntityUid target, FollowedCompone
if (followed.Following.Count == 0)
RemComp<FollowedComponent>(target);

RemComp<FollowerComponent>(uid);
RemComp<OrbitVisualsComponent>(uid);
if (removeComp)
{
RemComp<FollowerComponent>(uid);
RemComp<OrbitVisualsComponent>(uid);
}

var uidEv = new StoppedFollowingEntityEvent(target, uid);
var targetEv = new EntityStoppedFollowingEvent(target, uid);

Expand Down

0 comments on commit aac17c3

Please sign in to comment.