From 501c8a9f6a05afc7073c337d041b7144b4bbd1d0 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Mon, 24 Aug 2020 21:16:23 +1000 Subject: [PATCH] Slight DisposalUnit optimisation (#1874) UI was being updated every tick even when not necessary. Ideally you'd just update the UI based on events but this is fine for now. Co-authored-by: Metal Gear Sloth --- .../Disposal/DisposalUnitComponent.cs | 29 +++++++++++++++++-- .../Disposal/SharedDisposalUnitComponent.cs | 13 ++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/Content.Server/GameObjects/Components/Disposal/DisposalUnitComponent.cs b/Content.Server/GameObjects/Components/Disposal/DisposalUnitComponent.cs index 24ca2e6ea5..bb16efdbdf 100644 --- a/Content.Server/GameObjects/Components/Disposal/DisposalUnitComponent.cs +++ b/Content.Server/GameObjects/Components/Disposal/DisposalUnitComponent.cs @@ -117,6 +117,13 @@ namespace Content.Server.GameObjects.Components.Disposal ? boundUi : null; + private DisposalUnitBoundUserInterfaceState? _lastUiState; + + /// + /// Store the translated state. + /// + private (PressureState State, string Localized) _locState; + public bool CanInsert(IEntity entity) { if (!Anchored) @@ -286,13 +293,31 @@ namespace Content.Server.GameObjects.Components.Disposal private DisposalUnitBoundUserInterfaceState GetInterfaceState() { - var state = Loc.GetString($"{State}"); - return new DisposalUnitBoundUserInterfaceState(Owner.Name, state, _pressure, Powered, Engaged); + string stateString; + + if (_locState.State != State) + { + stateString = Loc.GetString($"{State}"); + _locState = (State, stateString); + } + else + { + stateString = _locState.Localized; + } + + return new DisposalUnitBoundUserInterfaceState(Owner.Name, stateString, _pressure, Powered, Engaged); } private void UpdateInterface() { var state = GetInterfaceState(); + + if (_lastUiState != null && _lastUiState.Equals(state)) + { + return; + } + + _lastUiState = state; UserInterface?.SetState(state); } diff --git a/Content.Shared/GameObjects/Components/Disposal/SharedDisposalUnitComponent.cs b/Content.Shared/GameObjects/Components/Disposal/SharedDisposalUnitComponent.cs index 9bcd7edbf0..d223b1a605 100644 --- a/Content.Shared/GameObjects/Components/Disposal/SharedDisposalUnitComponent.cs +++ b/Content.Shared/GameObjects/Components/Disposal/SharedDisposalUnitComponent.cs @@ -58,7 +58,7 @@ namespace Content.Shared.GameObjects.Components.Disposal } [Serializable, NetSerializable] - public class DisposalUnitBoundUserInterfaceState : BoundUserInterfaceState + public class DisposalUnitBoundUserInterfaceState : BoundUserInterfaceState, IEquatable { public readonly string UnitName; public readonly string UnitState; @@ -75,6 +75,17 @@ namespace Content.Shared.GameObjects.Components.Disposal Powered = powered; Engaged = engaged; } + + public bool Equals(DisposalUnitBoundUserInterfaceState other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return UnitName == other.UnitName && + UnitState == other.UnitState && + Powered == other.Powered && + Engaged == other.Engaged && + Pressure.Equals(other.Pressure); + } } ///