From cec722e19ee98cda7df0e6aa5b82f62475c9cbd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Aguilera=20Puerto?= <6766154+Zumorica@users.noreply.github.com> Date: Fri, 27 Nov 2020 17:12:45 +0100 Subject: [PATCH] CVars for MIDI instrument limits (#2632) * CVars for MIDI instruments limits! * Localize this * Cache CVars in instrument systems * better naming Co-authored-by: Pieter-Jan Briers --- .../Instruments/InstrumentComponent.cs | 9 ++++- .../EntitySystems/InstrumentSystem.cs | 24 +++++++++++ .../Instruments/InstrumentComponent.cs | 40 ++++++++++++------- .../EntitySystems/InstrumentSystem.cs | 40 +++++++++++++++++++ Content.Shared/CCVars.cs | 16 ++++++++ .../Instruments/SharedInstrumentComponent.cs | 7 ---- 6 files changed, 114 insertions(+), 22 deletions(-) diff --git a/Content.Client/GameObjects/Components/Instruments/InstrumentComponent.cs b/Content.Client/GameObjects/Components/Instruments/InstrumentComponent.cs index e9d3fc6f88..3896718c1a 100644 --- a/Content.Client/GameObjects/Components/Instruments/InstrumentComponent.cs +++ b/Content.Client/GameObjects/Components/Instruments/InstrumentComponent.cs @@ -2,12 +2,16 @@ using System; using System.Collections.Generic; using System.Linq; +using Content.Client.GameObjects.EntitySystems; +using Content.Shared; using Content.Shared.GameObjects.Components.Instruments; using Content.Shared.Physics; using Robust.Client.Audio.Midi; using Robust.Shared.Audio.Midi; using Robust.Shared.GameObjects; using Robust.Shared.GameObjects.Components.Timers; +using Robust.Shared.GameObjects.Systems; +using Robust.Shared.Interfaces.Configuration; using Robust.Shared.Interfaces.Network; using Robust.Shared.Interfaces.Timing; using Robust.Shared.IoC; @@ -34,6 +38,8 @@ namespace Content.Client.GameObjects.Components.Instruments private IMidiRenderer? _renderer; + private InstrumentSystem _instrumentSystem = default!; + private byte _instrumentProgram = 1; private byte _instrumentBank; @@ -158,6 +164,7 @@ namespace Content.Client.GameObjects.Components.Instruments { base.Initialize(); IoCManager.InjectDependencies(this); + _instrumentSystem = EntitySystem.Get(); } protected virtual void SetupRenderer(bool fromStateChange = false) @@ -422,7 +429,7 @@ namespace Content.Client.GameObjects.Components.Instruments if (_midiEventBuffer.Count == 0) return; - var max = Math.Min(MaxMidiEventsPerBatch, MaxMidiEventsPerSecond - _sentWithinASec); + var max = Math.Min(_instrumentSystem.MaxMidiEventsPerBatch, _instrumentSystem.MaxMidiEventsPerSecond - _sentWithinASec); if (max <= 0) { diff --git a/Content.Client/GameObjects/EntitySystems/InstrumentSystem.cs b/Content.Client/GameObjects/EntitySystems/InstrumentSystem.cs index e184d7d1eb..59c22113c1 100644 --- a/Content.Client/GameObjects/EntitySystems/InstrumentSystem.cs +++ b/Content.Client/GameObjects/EntitySystems/InstrumentSystem.cs @@ -1,6 +1,8 @@ using Content.Client.GameObjects.Components.Instruments; +using Content.Shared; using JetBrains.Annotations; using Robust.Shared.GameObjects.Systems; +using Robust.Shared.Interfaces.Configuration; using Robust.Shared.Interfaces.Timing; using Robust.Shared.IoC; @@ -10,6 +12,28 @@ namespace Content.Client.GameObjects.EntitySystems public class InstrumentSystem : EntitySystem { [Dependency] private readonly IGameTiming _gameTiming = default; + [Dependency] private readonly IConfigurationManager _cfg = default!; + + public override void Initialize() + { + base.Initialize(); + + _cfg.OnValueChanged(CCVars.MaxMidiEventsPerBatch, OnMaxMidiEventsPerBatchChanged, true); + _cfg.OnValueChanged(CCVars.MaxMidiEventsPerSecond, OnMaxMidiEventsPerSecondChanged, true); + } + + public int MaxMidiEventsPerBatch { get; private set; } + public int MaxMidiEventsPerSecond { get; private set; } + + private void OnMaxMidiEventsPerSecondChanged(int obj) + { + MaxMidiEventsPerSecond = obj; + } + + private void OnMaxMidiEventsPerBatchChanged(int obj) + { + MaxMidiEventsPerBatch = obj; + } public override void Update(float frameTime) { diff --git a/Content.Server/GameObjects/Components/Instruments/InstrumentComponent.cs b/Content.Server/GameObjects/Components/Instruments/InstrumentComponent.cs index 89d6ca6711..40269775d1 100644 --- a/Content.Server/GameObjects/Components/Instruments/InstrumentComponent.cs +++ b/Content.Server/GameObjects/Components/Instruments/InstrumentComponent.cs @@ -4,6 +4,7 @@ using System.Linq; using Content.Server.GameObjects.Components.Mobs; using Content.Server.GameObjects.EntitySystems; using Content.Server.Utility; +using Content.Shared; using Content.Shared.GameObjects.Components.Instruments; using Content.Shared.GameObjects.EntitySystems; using Content.Shared.Interfaces; @@ -16,9 +17,11 @@ using Robust.Server.Player; using Robust.Shared.Enums; using Robust.Shared.GameObjects; using Robust.Shared.GameObjects.Systems; +using Robust.Shared.Interfaces.Configuration; using Robust.Shared.Interfaces.Network; using Robust.Shared.Interfaces.Timing; using Robust.Shared.IoC; +using Robust.Shared.Localization; using Robust.Shared.Players; using Robust.Shared.Serialization; using Robust.Shared.ViewVariables; @@ -40,6 +43,7 @@ namespace Content.Server.GameObjects.Components.Instruments [Dependency] private readonly IGameTiming _gameTiming = default!; private static readonly TimeSpan OneSecAgo = TimeSpan.FromSeconds(-1); + private InstrumentSystem _instrumentSystem = default!; /// /// The client channel currently playing the instrument, or null if there's none. @@ -165,6 +169,8 @@ namespace Content.Server.GameObjects.Components.Instruments { UserInterface.OnClosed += UserInterfaceOnClosed; } + + _instrumentSystem = EntitySystem.Get(); } public override void ExposeData(ObjectSerializer serializer) @@ -186,6 +192,10 @@ namespace Content.Server.GameObjects.Components.Instruments { base.HandleNetworkMessage(message, channel, session); + var maxMidiLaggedBatches = _instrumentSystem.MaxMidiLaggedBatches; + var maxMidiEventsPerSecond = _instrumentSystem.MaxMidiEventsPerSecond; + var maxMidiEventsPerBatch = _instrumentSystem.MaxMidiEventsPerBatch; + switch (message) { case InstrumentMidiEventMessage midiEventMsg: @@ -206,26 +216,25 @@ namespace Content.Server.GameObjects.Components.Instruments } _laggedBatches++; - switch (_laggedBatches) + + if (_laggedBatches == (int) (maxMidiLaggedBatches * (1 / 3d) + 1)) { - case (int) (MaxMidiLaggedBatches * (1 / 3d)) + 1: - Owner.PopupMessage(InstrumentPlayer.AttachedEntity, - "Your fingers are beginning to a cramp a little!"); - break; - case (int) (MaxMidiLaggedBatches * (2 / 3d)) + 1: - Owner.PopupMessage(InstrumentPlayer.AttachedEntity, - "Your fingers are seriously cramping up!"); - break; + Owner.PopupMessage(InstrumentPlayer.AttachedEntity, + Loc.GetString("Your fingers are beginning to a cramp a little!")); + } else if (_laggedBatches == (int) (maxMidiLaggedBatches * (2 / 3d) + 1)) + { + Owner.PopupMessage(InstrumentPlayer.AttachedEntity, + Loc.GetString("Your fingers are seriously cramping up!")); } - if (_laggedBatches > MaxMidiLaggedBatches) + if (_laggedBatches > maxMidiLaggedBatches) { send = false; } } - if (++_midiEventCount > MaxMidiEventsPerSecond - || midiEventMsg.MidiEvent.Length > MaxMidiEventsPerBatch) + if (++_midiEventCount > maxMidiEventsPerSecond + || midiEventMsg.MidiEvent.Length > maxMidiEventsPerBatch) { var now = _gameTiming.RealTime; var oneSecAGo = now.Add(OneSecAgo); @@ -346,6 +355,9 @@ namespace Content.Server.GameObjects.Components.Instruments { base.Update(delta); + var maxMidiLaggedBatches = _instrumentSystem.MaxMidiLaggedBatches; + var maxMidiBatchDropped = _instrumentSystem.MaxMidiBatchesDropped; + if (_instrumentPlayer != null && !ActionBlockerSystem.CanInteract(_instrumentPlayer.AttachedEntity)) { InstrumentPlayer = null; @@ -353,8 +365,8 @@ namespace Content.Server.GameObjects.Components.Instruments UserInterface?.CloseAll(); } - if ((_batchesDropped >= MaxMidiBatchDropped - || _laggedBatches >= MaxMidiLaggedBatches) + if ((_batchesDropped >= maxMidiBatchDropped + || _laggedBatches >= maxMidiLaggedBatches) && InstrumentPlayer != null) { var mob = InstrumentPlayer.AttachedEntity; diff --git a/Content.Server/GameObjects/EntitySystems/InstrumentSystem.cs b/Content.Server/GameObjects/EntitySystems/InstrumentSystem.cs index e95f9016b5..80a50a93b8 100644 --- a/Content.Server/GameObjects/EntitySystems/InstrumentSystem.cs +++ b/Content.Server/GameObjects/EntitySystems/InstrumentSystem.cs @@ -1,12 +1,52 @@ using Content.Server.GameObjects.Components.Instruments; +using Content.Shared; using JetBrains.Annotations; using Robust.Shared.GameObjects.Systems; +using Robust.Shared.Interfaces.Configuration; +using Robust.Shared.IoC; namespace Content.Server.GameObjects.EntitySystems { [UsedImplicitly] internal sealed class InstrumentSystem : EntitySystem { + [Dependency] private readonly IConfigurationManager _cfg = default!; + + public override void Initialize() + { + base.Initialize(); + + _cfg.OnValueChanged(CCVars.MaxMidiEventsPerSecond, OnMaxMidiEventsPerSecondChanged, true); + _cfg.OnValueChanged(CCVars.MaxMidiEventsPerBatch, OnMaxMidiEventsPerBatchChanged, true); + _cfg.OnValueChanged(CCVars.MaxMidiBatchesDropped, OnMaxMidiBatchesDroppedChanged, true); + _cfg.OnValueChanged(CCVars.MaxMidiLaggedBatches, OnMaxMidiLaggedBatchesChanged, true); + } + + public int MaxMidiEventsPerSecond { get; private set; } + public int MaxMidiEventsPerBatch { get; private set; } + public int MaxMidiBatchesDropped { get; private set; } + public int MaxMidiLaggedBatches { get; private set; } + + private void OnMaxMidiLaggedBatchesChanged(int obj) + { + MaxMidiLaggedBatches = obj; + } + + private void OnMaxMidiBatchesDroppedChanged(int obj) + { + MaxMidiBatchesDropped = obj; + } + + private void OnMaxMidiEventsPerBatchChanged(int obj) + { + MaxMidiEventsPerBatch = obj; + } + + private void OnMaxMidiEventsPerSecondChanged(int obj) + { + MaxMidiEventsPerSecond = obj; + } + public override void Update(float frameTime) { base.Update(frameTime); diff --git a/Content.Shared/CCVars.cs b/Content.Shared/CCVars.cs index 78d6442e19..14b19f16dc 100644 --- a/Content.Shared/CCVars.cs +++ b/Content.Shared/CCVars.cs @@ -182,6 +182,22 @@ namespace Content.Shared public static readonly CVarDef ExcitedGroupsSpaceIsAllConsuming = CVarDef.Create("atmos.excited_groups_space_is_all_consuming", false, CVar.SERVERONLY); + /* + * MIDI instruments + */ + + public static readonly CVarDef MaxMidiEventsPerSecond = + CVarDef.Create("midi.max_events_per_second", 1000, CVar.REPLICATED | CVar.SERVER); + + public static readonly CVarDef MaxMidiEventsPerBatch = + CVarDef.Create("midi.max_events_per_batch", 60, CVar.REPLICATED | CVar.SERVER); + + public static readonly CVarDef MaxMidiBatchesDropped = + CVarDef.Create("midi.max_batches_dropped", 1, CVar.SERVERONLY); + + public static readonly CVarDef MaxMidiLaggedBatches = + CVarDef.Create("midi.max_lagged_batches", 8, CVar.SERVERONLY); + /* * Branding stuff */ diff --git a/Content.Shared/GameObjects/Components/Instruments/SharedInstrumentComponent.cs b/Content.Shared/GameObjects/Components/Instruments/SharedInstrumentComponent.cs index ed2292514f..17b7c5a6df 100644 --- a/Content.Shared/GameObjects/Components/Instruments/SharedInstrumentComponent.cs +++ b/Content.Shared/GameObjects/Components/Instruments/SharedInstrumentComponent.cs @@ -8,13 +8,6 @@ namespace Content.Shared.GameObjects.Components.Instruments { public class SharedInstrumentComponent : Component { - - // These 2 values are quite high for now, and this could be easily abused. Change this if people are abusing it. - public const int MaxMidiEventsPerSecond = 1000; - public const int MaxMidiEventsPerBatch = 60; - public const int MaxMidiBatchDropped = 1; - public const int MaxMidiLaggedBatches = 8; - public override string Name => "Instrument"; public override uint? NetID => ContentNetIDs.INSTRUMENTS;