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 <pieterjan.briers+git@gmail.com>
This commit is contained in:
Víctor Aguilera Puerto
2020-11-27 17:12:45 +01:00
committed by GitHub
parent 6bb1e9fa5d
commit cec722e19e
6 changed files with 114 additions and 22 deletions

View File

@@ -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<InstrumentSystem>();
}
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)
{

View File

@@ -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)
{

View File

@@ -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!;
/// <summary>
/// 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<InstrumentSystem>();
}
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:
Loc.GetString("Your fingers are beginning to a cramp a little!"));
} else if (_laggedBatches == (int) (maxMidiLaggedBatches * (2 / 3d) + 1))
{
Owner.PopupMessage(InstrumentPlayer.AttachedEntity,
"Your fingers are seriously cramping up!");
break;
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;

View File

@@ -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);

View File

@@ -182,6 +182,22 @@ namespace Content.Shared
public static readonly CVarDef<bool> ExcitedGroupsSpaceIsAllConsuming =
CVarDef.Create("atmos.excited_groups_space_is_all_consuming", false, CVar.SERVERONLY);
/*
* MIDI instruments
*/
public static readonly CVarDef<int> MaxMidiEventsPerSecond =
CVarDef.Create("midi.max_events_per_second", 1000, CVar.REPLICATED | CVar.SERVER);
public static readonly CVarDef<int> MaxMidiEventsPerBatch =
CVarDef.Create("midi.max_events_per_batch", 60, CVar.REPLICATED | CVar.SERVER);
public static readonly CVarDef<int> MaxMidiBatchesDropped =
CVarDef.Create("midi.max_batches_dropped", 1, CVar.SERVERONLY);
public static readonly CVarDef<int> MaxMidiLaggedBatches =
CVarDef.Create("midi.max_lagged_batches", 8, CVar.SERVERONLY);
/*
* Branding stuff
*/

View File

@@ -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;