mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-15 03:30:53 +01:00
Fixes #5592 This allows net.tickrate to be set to a max of 65535 instead of 255. I didn't raise it fully to a uint because there are many places it's cast to an int, so uint would cause various compiler errors and compat issues I don't wanna deal with.
82 lines
1.9 KiB
C#
82 lines
1.9 KiB
C#
using System.Collections.Generic;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
namespace Robust.Shared.Console;
|
|
|
|
public sealed class CommandBuffer
|
|
{
|
|
private const string DelayMarker = "-DELAY-";
|
|
|
|
private int _tickrate = 0;
|
|
private int _delay = 0;
|
|
|
|
private readonly LinkedList<string> _commandBuffer = new();
|
|
|
|
public void Append(string command)
|
|
{
|
|
_commandBuffer.AddLast(command);
|
|
}
|
|
|
|
public void Insert(string command)
|
|
{
|
|
_commandBuffer.AddFirst(command);
|
|
}
|
|
|
|
public void Tick(ushort tickRate)
|
|
{
|
|
_tickrate = tickRate;
|
|
|
|
if (_delay > 0)
|
|
{
|
|
_delay -= 1;
|
|
}
|
|
}
|
|
|
|
public bool TryGetCommand([MaybeNullWhen(false)]out string command)
|
|
{
|
|
var next = _commandBuffer.First;
|
|
|
|
if (next is null) // nothing to do here
|
|
{
|
|
command = null;
|
|
return false;
|
|
}
|
|
|
|
if (next.Value.Equals(DelayMarker))
|
|
{
|
|
if (_delay == 0) // just finished
|
|
{
|
|
_commandBuffer.RemoveFirst();
|
|
return TryGetCommand(out command);
|
|
}
|
|
else // currently counting down delay
|
|
{
|
|
command = null;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (next.Value.StartsWith("wait "))
|
|
{
|
|
var sTicks = next.Value.Substring(5);
|
|
_commandBuffer.RemoveFirst();
|
|
if (string.IsNullOrWhiteSpace(sTicks) || !int.TryParse(sTicks, out var ticks)) // messed up command
|
|
{
|
|
return TryGetCommand(out command);
|
|
}
|
|
|
|
// Setup Timing
|
|
_commandBuffer.AddFirst(DelayMarker);
|
|
_delay = ticks;
|
|
|
|
command = null;
|
|
return false;
|
|
}
|
|
|
|
// normal command
|
|
_commandBuffer.RemoveFirst();
|
|
command = next.Value;
|
|
return true;
|
|
}
|
|
}
|