Reduce disposals allocations (#3715)

Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
This commit is contained in:
metalgearsloth
2021-03-26 12:48:39 +11:00
committed by GitHub
parent d0abb85a6e
commit 1499468157
3 changed files with 21 additions and 63 deletions

View File

@@ -136,8 +136,6 @@ namespace Content.Server.GameObjects.Components.Disposal
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(DisposalMailingUnitUiKey.Key);
private DisposalMailingUnitBoundUserInterfaceState? _lastUiState;
/// <summary>
/// Store the translated state.
/// </summary>
@@ -376,34 +374,13 @@ namespace Content.Server.GameObjects.Components.Disposal
UpdateInterface();
}
private DisposalMailingUnitBoundUserInterfaceState GetInterfaceState()
private void UpdateInterface()
{
string stateString;
if (_locState.State != State)
{
stateString = Loc.GetString($"{State}");
_locState = (State, stateString);
}
else
{
stateString = _locState.Localized;
}
return new DisposalMailingUnitBoundUserInterfaceState(Owner.Name, stateString, _pressure, Powered, Engaged, _tag, _targetList, _target);
}
private void UpdateInterface(bool checkEqual = true)
{
var state = GetInterfaceState();
if (checkEqual && _lastUiState != null && _lastUiState.Equals(state))
{
return;
}
_lastUiState = state;
UserInterface?.SetState((DisposalMailingUnitBoundUserInterfaceState) state.Clone());
var state = new DisposalUnitBoundUserInterfaceState(Owner.Name, stateString, _pressure, Powered, Engaged);
UserInterface?.SetState(state);
}
private bool PlayerCanUse(IEntity? player)
@@ -547,8 +524,11 @@ namespace Content.Server.GameObjects.Components.Disposal
}
}
if (_pressure < 1.0f || oldPressure < 1.0f && _pressure >= 1.0f)
{
UpdateInterface();
}
}
private void PowerStateChanged(PowerChangedMessage args)
{
@@ -592,6 +572,7 @@ namespace Content.Server.GameObjects.Components.Disposal
UpdateTargetList();
UpdateVisualState();
UpdateInterface();
}
public override void OnRemove()
@@ -654,7 +635,7 @@ namespace Content.Server.GameObjects.Components.Disposal
if (command == NET_CMD_RESPONSE && payload.TryGetValue(NET_TAG, out var tag))
{
_targetList.Add(tag);
UpdateInterface(false);
UpdateInterface();
}
if (command == NET_CMD_REQUEST)
@@ -715,7 +696,7 @@ namespace Content.Server.GameObjects.Components.Disposal
if (IsValidInteraction(eventArgs))
{
UpdateTargetList();
UpdateInterface(false);
UpdateInterface();
UserInterface?.Open(actor.playerSession);
return true;
}

View File

@@ -131,13 +131,6 @@ namespace Content.Server.GameObjects.Components.Disposal
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(DisposalUnitUiKey.Key);
private DisposalUnitBoundUserInterfaceState? _lastUiState;
/// <summary>
/// Store the translated state.
/// </summary>
private (PressureState State, string Localized) _locState;
[DataField("air")]
public GasMixture Air { get; set; } = new GasMixture(Atmospherics.CellVolume);
@@ -328,33 +321,12 @@ namespace Content.Server.GameObjects.Components.Disposal
UpdateInterface();
}
private DisposalUnitBoundUserInterfaceState GetInterfaceState()
private void UpdateInterface()
{
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;
var state = new DisposalUnitBoundUserInterfaceState(Owner.Name, stateString, _pressure, Powered, Engaged);
UserInterface?.SetState(state);
}
@@ -488,8 +460,12 @@ namespace Content.Server.GameObjects.Components.Disposal
}
}
// TODO: Ideally we'd just send the start and end and client could lerp as the bandwidth would be way lower
if (_pressure < 1.0f || oldPressure < 1.0f && _pressure >= 1.0f)
{
UpdateInterface();
}
}
private void PowerStateChanged(PowerChangedMessage args)
{
@@ -531,6 +507,7 @@ namespace Content.Server.GameObjects.Components.Disposal
}
UpdateVisualState();
UpdateInterface();
}
public override void OnRemove()

View File

@@ -21,7 +21,7 @@ namespace Content.Shared.GameObjects.Components.Disposal
public const string NET_CMD_RESPONSE = "mailer_tag";
[Serializable, NetSerializable]
public new enum UiButton
public new enum UiButton : byte
{
Eject,
Engage,