diff --git a/Robust.Shared/GameObjects/Systems/SharedUserInterfaceSystem.cs b/Robust.Shared/GameObjects/Systems/SharedUserInterfaceSystem.cs index a476fa08fe4..42605e24821 100644 --- a/Robust.Shared/GameObjects/Systems/SharedUserInterfaceSystem.cs +++ b/Robust.Shared/GameObjects/Systems/SharedUserInterfaceSystem.cs @@ -48,7 +48,7 @@ private enum QueuedUpdate /// /// Defer BUIs during state handling so client doesn't spam a BUI constantly during prediction. /// - private readonly List<(BoundUserInterface bui, QueuedUpdate updateType)> _queuedBuis = new(); + private readonly List<(BoundUserInterface bui, QueuedUpdate updateType, BoundUserInterfaceState? state)> _queuedBuis = new(); public override void Initialize() { @@ -91,9 +91,9 @@ public override void Initialize() SubscribeLocalEvent(OnActorShutdown); } - private void AddQueued(BoundUserInterface bui, QueuedUpdate type) + private void AddQueued(BoundUserInterface bui, QueuedUpdate type, BoundUserInterfaceState? state = null) { - _queuedBuis.Add((bui, type)); + _queuedBuis.Add((bui, type, state)); } /// @@ -292,7 +292,8 @@ private void OnUserInterfaceStartup(Entity ent, ref Comp // PlayerAttachedEvent will catch some of these. foreach (var (key, bui) in ent.Comp.ClientOpenInterfaces) { - AddQueued(bui, QueuedUpdate.Open); + var state = ent.Comp.States.GetValueOrDefault(key); + AddQueued(bui, QueuedUpdate.Open, state); } } @@ -312,7 +313,7 @@ protected void OnUserInterfaceShutdown(Entity ent, ref C DebugTools.Assert(!ent.Comp.Actors.ContainsKey(key)); } - DebugTools.Assert(ent.Comp.ClientOpenInterfaces.Values.All(x => _queuedBuis.Contains((x, QueuedUpdate.Close)))); + DebugTools.Assert(ent.Comp.ClientOpenInterfaces.Values.All(x => _queuedBuis.Contains((x, QueuedUpdate.Close, null)))); } private void OnUserInterfaceGetState(Entity ent, ref ComponentGetState args) @@ -500,7 +501,7 @@ private void OnUserInterfaceHandleState(Entity ent, ref if (!ent.Comp.ClientOpenInterfaces.TryGetValue(key, out var cBui) || !cBui.IsOpened) continue; - AddQueued(cBui, QueuedUpdate.ApplyState); + AddQueued(cBui, QueuedUpdate.ApplyState, buiState); } } @@ -528,7 +529,7 @@ private void EnsureClientBui(Entity entity, Enum key, In // Existing BUI just keep it. if (entity.Comp.ClientOpenInterfaces.TryGetValue(key, out var existing)) { - _queuedBuis.Remove((existing, QueuedUpdate.Close)); + _queuedBuis.Remove((existing, QueuedUpdate.Close, null)); return; } @@ -764,11 +765,7 @@ public void SetUiState(Entity entity, Enum key, BoundUs // Predict the change on client if (state != null && _netManager.IsClient && entity.Comp.ClientOpenInterfaces.TryGetValue(key, out var bui)) { - if (bui.State?.Equals(state) != true) - { - bui.UpdateState(state); - bui.Update(); - } + AddQueued(bui, QueuedUpdate.ApplyState, state); } DirtyField(entity, nameof(UserInterfaceComponent.States)); @@ -1110,7 +1107,7 @@ public override void Update(float frameTime) { if (_timing.IsFirstTimePredicted) { - foreach (var (bui, updateType) in _queuedBuis) + foreach (var (bui, updateType, state) in _queuedBuis) { if (updateType == QueuedUpdate.Open || updateType == QueuedUpdate.ApplyState) { @@ -1123,26 +1120,23 @@ public override void Update(float frameTime) bui.Open(); } - if (UIQuery.TryComp(bui.Owner, out var uiComp)) + if (state != null) { - if (uiComp.States.TryGetValue(bui.UiKey, out var buiState)) - { - bui.State = buiState; - bui.UpdateState(buiState); - bui.Update(); - } + bui.State = state; + bui.UpdateState(state); + bui.Update(); } #if EXCEPTION_TOLERANCE } catch (Exception e) { + var operationType = updateType == QueuedUpdate.Open ? "create" : "update"; Log.Error( - $"Caught exception while attempting to create a BUI {bui.UiKey} with type {bui.GetType()} on entity {ToPrettyString(bui.Owner)}. Exception: {e}"); + $"Caught exception while attempting to {operationType} a BUI {bui.UiKey} with type {bui.GetType()} on entity {ToPrettyString(bui.Owner)}. Exception: {e}"); } #endif } - // Close BUI - else + else // Close BUI { if (UIQuery.TryComp(bui.Owner, out var uiComp)) {