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

View File

@@ -131,13 +131,6 @@ namespace Content.Server.GameObjects.Components.Disposal
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(DisposalUnitUiKey.Key); [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")] [DataField("air")]
public GasMixture Air { get; set; } = new GasMixture(Atmospherics.CellVolume); public GasMixture Air { get; set; } = new GasMixture(Atmospherics.CellVolume);
@@ -328,33 +321,12 @@ namespace Content.Server.GameObjects.Components.Disposal
UpdateInterface(); UpdateInterface();
} }
private DisposalUnitBoundUserInterfaceState GetInterfaceState() private void UpdateInterface()
{ {
string stateString; string stateString;
if (_locState.State != State) stateString = Loc.GetString($"{State}");
{ var state = new DisposalUnitBoundUserInterfaceState(Owner.Name, stateString, _pressure, Powered, Engaged);
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); UserInterface?.SetState(state);
} }
@@ -488,7 +460,11 @@ namespace Content.Server.GameObjects.Components.Disposal
} }
} }
UpdateInterface(); // 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) private void PowerStateChanged(PowerChangedMessage args)
@@ -531,6 +507,7 @@ namespace Content.Server.GameObjects.Components.Disposal
} }
UpdateVisualState(); UpdateVisualState();
UpdateInterface();
} }
public override void OnRemove() public override void OnRemove()

View File

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