From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Sat, 14 Oct 2023 18:02:36 +0000 (+1100) Subject: Cleanup more follower leaks (#20902) X-Git-Url: https://git.smokeofanarchy.ru/gitweb.cgi?a=commitdiff_plain;h=aac17c33210b15d144551cb677ed0bee94b55afd;p=space-station-14.git 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 --- diff --git a/Content.Shared/Follower/FollowerSystem.cs b/Content.Shared/Follower/FollowerSystem.cs index 38c0e5ada3..e4a0488dfb 100644 --- a/Content.Shared/Follower/FollowerSystem.cs +++ b/Content.Shared/Follower/FollowerSystem.cs @@ -138,7 +138,20 @@ public sealed class FollowerSystem : EntitySystem targetXform = Transform(targetXform.ParentUid); } - var followerComp = EnsureComp(follower); + // Cleanup old following. + if (TryComp(follower, out var followerComp)) + { + // Already following you goob + if (followerComp.Following == entity) + return; + + StopFollowingEntity(follower, followerComp.Following, deparent: false, removeComp: false); + } + else + { + followerComp = AddComp(follower); + } + followerComp.Following = entity; var followedComp = EnsureComp(entity); @@ -167,14 +180,14 @@ public sealed class FollowerSystem : EntitySystem RaiseLocalEvent(follower, followerEv); RaiseLocalEvent(entity, entityEv); - Dirty(followedComp); + Dirty(entity, followedComp); } /// /// Forces an entity to stop following another entity, if it is doing so. /// /// Should the entity deparent itself - 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(target); - RemComp(uid); - RemComp(uid); + if (removeComp) + { + RemComp(uid); + RemComp(uid); + } + var uidEv = new StoppedFollowingEntityEvent(target, uid); var targetEv = new EntityStoppedFollowingEvent(target, uid);