Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 30 additions & 48 deletions Robust.Shared/GameObjects/Systems/SharedUserInterfaceSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,10 @@ public abstract partial class SharedUserInterfaceSystem : EntitySystem

private ActorRangeCheckJob _rangeJob;

/// Update type to apply
private enum QueuedUpdate
{
/// Upen a UI
Open,
/// Apply new state to the UI
ApplyState,
/// Close the UI
Close
};

/// <summary>
/// Defer BUIs during state handling so client doesn't spam a BUI constantly during prediction.
/// </summary>
private readonly List<(BoundUserInterface bui, QueuedUpdate updateType, BoundUserInterfaceState? state)> _queuedBuis = new();
private readonly List<(BoundUserInterface Bui, bool value)> _queuedBuis = new();

public override void Initialize()
{
Expand Down Expand Up @@ -91,22 +80,9 @@ public override void Initialize()
SubscribeLocalEvent<UserInterfaceUserComponent, ComponentShutdown>(OnActorShutdown);
}

/// <summary>
/// Enqueues a BUI command to be processed in the next Update call,
/// getting the last available state for the given UserInterfaceComponent at the last state.
/// </summary>
private void AddQueued(BoundUserInterface bui, QueuedUpdate type, Entity<UserInterfaceComponent> ent, Enum key)
{
var state = ent.Comp.States.GetValueOrDefault(key);
_queuedBuis.Add((bui, type, state));
}

/// <summary>
/// Enqueues a BUI command to be processed in the next Update call.
/// </summary>
private void AddQueued(BoundUserInterface bui, QueuedUpdate type, BoundUserInterfaceState? state = null)
private void AddQueued(BoundUserInterface bui, bool value)
{
_queuedBuis.Add((bui, type, state));
_queuedBuis.Add((bui, value));
}

/// <summary>
Expand Down Expand Up @@ -263,7 +239,7 @@ private void CloseUiInternal(Entity<UserInterfaceComponent?> ent, Enum key, Enti

if (ent.Comp.ClientOpenInterfaces.TryGetValue(key, out var cBui))
{
AddQueued(cBui, QueuedUpdate.Close);
AddQueued(cBui, false);
}

if (ent.Comp.Actors.Count == 0)
Expand Down Expand Up @@ -305,7 +281,7 @@ private void OnUserInterfaceStartup(Entity<UserInterfaceComponent> ent, ref Comp
// PlayerAttachedEvent will catch some of these.
foreach (var (key, bui) in ent.Comp.ClientOpenInterfaces)
{
AddQueued(bui, QueuedUpdate.Open, ent, key);
AddQueued(bui, true);
}
}

Expand All @@ -325,7 +301,7 @@ protected void OnUserInterfaceShutdown(Entity<UserInterfaceComponent> ent, ref C
DebugTools.Assert(!ent.Comp.Actors.ContainsKey(key));
}

DebugTools.Assert(ent.Comp.ClientOpenInterfaces.Values.All(x => _queuedBuis.Contains((x, QueuedUpdate.Close, null))));
DebugTools.Assert(ent.Comp.ClientOpenInterfaces.Values.All(x => _queuedBuis.Contains((x, false))));
}

private void OnUserInterfaceGetState(Entity<UserInterfaceComponent> ent, ref ComponentGetState args)
Expand Down Expand Up @@ -486,7 +462,7 @@ private void OnUserInterfaceHandleState(Entity<UserInterfaceComponent> ent, ref
}

var bui = ent.Comp.ClientOpenInterfaces[key];
AddQueued(bui, QueuedUpdate.Close);
AddQueued(bui, false);
}
}

Expand All @@ -513,7 +489,9 @@ private void OnUserInterfaceHandleState(Entity<UserInterfaceComponent> ent, ref
if (!ent.Comp.ClientOpenInterfaces.TryGetValue(key, out var cBui) || !cBui.IsOpened)
continue;

AddQueued(cBui, QueuedUpdate.ApplyState, buiState);
cBui.State = buiState;
cBui.UpdateState(buiState);
cBui.Update();
}
}

Expand Down Expand Up @@ -541,7 +519,7 @@ private void EnsureClientBui(Entity<UserInterfaceComponent> entity, Enum key, In
// Existing BUI just keep it.
if (entity.Comp.ClientOpenInterfaces.TryGetValue(key, out var existing))
{
_queuedBuis.Remove((existing, QueuedUpdate.Close, null));
_queuedBuis.Remove((existing, false));
return;
}

Expand All @@ -566,7 +544,7 @@ private void EnsureClientBui(Entity<UserInterfaceComponent> entity, Enum key, In
if (!open)
return;

AddQueued(boundUserInterface, QueuedUpdate.Open, entity, key);
AddQueued(boundUserInterface, true);
}

/// <summary>
Expand Down Expand Up @@ -777,7 +755,11 @@ public void SetUiState(Entity<UserInterfaceComponent?> entity, Enum key, BoundUs
// Predict the change on client
if (state != null && _netManager.IsClient && entity.Comp.ClientOpenInterfaces.TryGetValue(key, out var bui))
{
AddQueued(bui, QueuedUpdate.ApplyState, state);
if (bui.State?.Equals(state) != true)
{
bui.UpdateState(state);
bui.Update();
}
}

DirtyField(entity, nameof(UserInterfaceComponent.States));
Expand Down Expand Up @@ -1119,36 +1101,36 @@ public override void Update(float frameTime)
{
if (_timing.IsFirstTimePredicted)
{
foreach (var (bui, updateType, state) in _queuedBuis)
foreach (var (bui, open) in _queuedBuis)
{
if (updateType == QueuedUpdate.Open || updateType == QueuedUpdate.ApplyState)
if (open)
{
#if EXCEPTION_TOLERANCE
try
{
#endif
if (updateType == QueuedUpdate.Open)
{
bui.Open();
}
bui.Open();

if (state != null)
if (UIQuery.TryComp(bui.Owner, out var uiComp))
{
bui.State = state;
bui.UpdateState(state);
bui.Update();
if (uiComp.States.TryGetValue(bui.UiKey, out var buiState))
{
bui.State = buiState;
bui.UpdateState(buiState);
bui.Update();
}
}
#if EXCEPTION_TOLERANCE
}
catch (Exception e)
{
var operationType = updateType == QueuedUpdate.Open ? "create" : "update";
Log.Error(
$"Caught exception while attempting to {operationType} a BUI {bui.UiKey} with type {bui.GetType()} on entity {ToPrettyString(bui.Owner)}. Exception: {e}");
$"Caught exception while attempting to create a BUI {bui.UiKey} with type {bui.GetType()} on entity {ToPrettyString(bui.Owner)}. Exception: {e}");
}
#endif
}
else // Close BUI
// Close BUI
else
{
if (UIQuery.TryComp(bui.Owner, out var uiComp))
{
Expand Down
Loading