Close UIs on disconnect (#5071)

Engine handles it fine but content does not as the state gets handled before all comps are initialized.
This commit is contained in:
metalgearsloth
2024-04-27 12:21:46 +10:00
committed by GitHub
parent 123d0ae6ac
commit 16bab1bc03

View File

@@ -59,6 +59,25 @@ public abstract class SharedUserInterfaceSystem : EntitySystem
SubscribeLocalEvent<UserInterfaceUserComponent, ComponentGetStateAttemptEvent>(OnGetStateAttempt);
SubscribeLocalEvent<UserInterfaceUserComponent, ComponentGetState>(OnActorGetState);
SubscribeLocalEvent<UserInterfaceUserComponent, ComponentHandleState>(OnActorHandleState);
_player.PlayerStatusChanged += OnStatusChange;
}
private void OnStatusChange(object? sender, SessionStatusEventArgs e)
{
var attachedEnt = e.Session.AttachedEntity;
if (attachedEnt == null)
return;
// Content can't handle it yet sadly :(
CloseUserUis(attachedEnt.Value);
}
public override void Shutdown()
{
base.Shutdown();
_player.PlayerStatusChanged -= OnStatusChange;
}
/// <summary>
@@ -727,6 +746,32 @@ public abstract class SharedUserInterfaceSystem : EntitySystem
RaiseNetworkEvent(new BoundUIWrapMessage(GetNetEntity(entity.Owner), message, key));
}
/// <summary>
/// Closes all Uis for the actor.
/// </summary>
public void CloseUserUis(Entity<UserInterfaceUserComponent?> actor)
{
if (!_userQuery.Resolve(actor.Owner, ref actor.Comp, false))
return;
if (actor.Comp.OpenInterfaces.Count == 0)
return;
var copied = new Dictionary<EntityUid, List<Enum>>(actor.Comp.OpenInterfaces);
var enumCopy = new ValueList<Enum>();
foreach (var (uid, enums) in copied)
{
enumCopy.Clear();
enumCopy.AddRange(enums);
foreach (var key in enumCopy)
{
CloseUi(uid, key, actor.Owner);
}
}
}
/// <summary>
/// Closes all Uis for the entity.
/// </summary>