Optimize instruments (#7425)

This commit is contained in:
mirrorcult
2022-04-04 23:08:36 -07:00
committed by GitHub
parent 4f5864e121
commit b7ff72ba6e
3 changed files with 33 additions and 17 deletions

View File

@@ -32,3 +32,6 @@ public sealed class InstrumentComponent : SharedInstrumentComponent
[ViewVariables] public BoundUserInterface? UserInterface => Owner.GetUIOrNull(InstrumentUiKey.Key);
}
[RegisterComponent]
public sealed class ActiveInstrumentComponent : Component {}

View File

@@ -1,15 +1,12 @@
using System;
using System.Linq;
using Content.Server.Stunnable;
using Content.Server.UserInterface;
using Content.Shared.Instruments;
using Content.Shared.Popups;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.Configuration;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
namespace Content.Server.Instruments;
@@ -18,6 +15,7 @@ public sealed partial class InstrumentSystem : SharedInstrumentSystem
{
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly StunSystem _stunSystem = default!;
[Dependency] private readonly UserInterfaceSystem _userInterfaceSystem = default!;
public override void Initialize()
{
@@ -29,7 +27,8 @@ public sealed partial class InstrumentSystem : SharedInstrumentSystem
SubscribeNetworkEvent<InstrumentStartMidiEvent>(OnMidiStart);
SubscribeNetworkEvent<InstrumentStopMidiEvent>(OnMidiStop);
SubscribeLocalEvent<InstrumentComponent, ActivatableUIPlayerChangedEvent>(InstrumentNeedsClean);
SubscribeLocalEvent<InstrumentComponent, BoundUIClosedEvent>(OnBoundUIClosed);
SubscribeLocalEvent<InstrumentComponent, BoundUIOpenedEvent>(OnBoundUIOpened);
}
private void OnMidiStart(InstrumentStartMidiEvent msg, EntitySessionEventArgs args)
@@ -66,6 +65,24 @@ public sealed partial class InstrumentSystem : SharedInstrumentSystem
ShutdownCVars();
}
private void OnBoundUIClosed(EntityUid uid, InstrumentComponent component, BoundUIClosedEvent args)
{
if (HasComp<ActiveInstrumentComponent>(uid)
&& _userInterfaceSystem.TryGetUi(uid, args.UiKey, out var bui)
&& bui.SubscribedSessions.Count == 0)
{
RemComp<ActiveInstrumentComponent>(uid);
}
Clean(uid, component);
}
private void OnBoundUIOpened(EntityUid uid, InstrumentComponent component, BoundUIOpenedEvent args)
{
EnsureComp<ActiveInstrumentComponent>(uid);
Clean(uid, component);
}
public void Clean(EntityUid uid, InstrumentComponent? instrument = null)
{
if (!Resolve(uid, ref instrument))
@@ -83,11 +100,6 @@ public sealed partial class InstrumentSystem : SharedInstrumentSystem
instrument.Dirty();
}
private void InstrumentNeedsClean(EntityUid uid, InstrumentComponent component, ActivatableUIPlayerChangedEvent ev)
{
Clean(uid, component);
}
private void OnMidiEventRx(InstrumentMidiEventEvent msg, EntitySessionEventArgs args)
{
var uid = msg.Uid;
@@ -148,15 +160,15 @@ public sealed partial class InstrumentSystem : SharedInstrumentSystem
{
base.Update(frameTime);
foreach (var instrument in EntityManager.EntityQuery<InstrumentComponent>(true))
foreach (var (_, instrument) in EntityManager.EntityQuery<ActiveInstrumentComponent, InstrumentComponent>(true))
{
if (instrument.DirtyRenderer)
{
instrument.Dirty();
Dirty(instrument);
instrument.DirtyRenderer = false;
}
if (instrument.RespectMidiLimits && instrument.InstrumentPlayer != null &&
if (instrument.RespectMidiLimits &&
(instrument.BatchesDropped >= MaxMidiBatchesDropped
|| instrument.LaggedBatches >= MaxMidiLaggedBatches))
{

View File

@@ -119,12 +119,13 @@ namespace Content.Server.PAI
private void PAITurningOff(EntityUid uid)
{
UpdatePAIAppearance(uid, PAIStatus.Off);
// Close the instrument interface if it was open
// before closing
if (EntityManager.TryGetComponent<ServerUserInterfaceComponent>(uid, out var serverUi))
if (EntityManager.TryGetComponent<ActorComponent>(uid, out var actor))
if (serverUi.TryGetBoundUserInterface(InstrumentUiKey.Key,out var bui))
bui.Close(actor.PlayerSession);
if (HasComp<ActiveInstrumentComponent>(uid) && TryComp<ActorComponent>(uid, out var actor))
{
_instrumentSystem.ToggleInstrumentUi(uid, actor.PlayerSession);
}
// Stop instrument
if (EntityManager.TryGetComponent<InstrumentComponent>(uid, out var instrument)) _instrumentSystem.Clean(uid, instrument);