Cleanup more follower leaks (#20902)

* 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
This commit is contained in:
metalgearsloth
2023-10-15 05:02:36 +11:00
committed by Debug
parent 4f17e038ff
commit b2e81d5fc8

View File

@@ -138,7 +138,20 @@ public sealed class FollowerSystem : EntitySystem
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);
@@ -167,14 +180,14 @@ public sealed class FollowerSystem : EntitySystem
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;
@@ -186,8 +199,12 @@ public sealed class FollowerSystem : EntitySystem
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);