Merge branch 'master' of https://github.com/Regalis11/Barotrauma into develop

This commit is contained in:
EvilFactory
2025-07-01 17:55:43 -03:00
19 changed files with 110 additions and 34 deletions
@@ -282,6 +282,8 @@ namespace Barotrauma
private static async Task<Consent> RequestAnswerFromRemoteDatabase()
{
await Task.Yield();
static void error(string reason, Exception? exception)
{
DebugConsole.ThrowError($"Error in {nameof(GameAnalyticsManager)}.{nameof(RequestAnswerFromRemoteDatabase)}: {reason}", exception);
@@ -1,11 +1,25 @@
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace Barotrauma.Items.Components
{
class RegExFindComponent : ItemComponent
{
private static readonly TimeSpan timeout = TimeSpan.FromMilliseconds(1);
/// <summary>
/// The timeout should be a lot shorter (used to be 1 ms), but there seems to be an issue in .NET 8
/// that sometimes causes the evaluation to randomly take a significant amount of time
/// (an expression that normally takes 20 ticks might sometimes take several milliseconds every few minutes).
/// So let's use a relatively long timeout instead, and measure the actual time the expression took ourselves.
/// </summary>
private static readonly TimeSpan timeout = TimeSpan.FromMilliseconds(50);
private static readonly TimeSpan shortTimeout = TimeSpan.FromMilliseconds(1);
private readonly Stopwatch stopwatch = new Stopwatch();
private bool timedOut;
private int timeOutsInARow;
const int MaxTimeOutsInARow = 3;
private string expression;
@@ -19,6 +33,7 @@ namespace Barotrauma.Items.Components
private bool nonContinuousOutputSent;
private int maxOutputLength;
[Editable, Serialize(200, IsPropertySaveable.No, description: "The maximum length of the output string. Warning: Large values can lead to large memory usage or networking issues.")]
public int MaxOutputLength
@@ -80,7 +95,7 @@ namespace Barotrauma.Items.Components
return;
}
//reactivate the component, in case some faulty/malicious expression caused it to time out and deactivate itself
IsActive = true;
timedOut = false;
}
}
@@ -93,6 +108,12 @@ namespace Barotrauma.Items.Components
public override void Update(float deltaTime, Camera cam)
{
if (timedOut)
{
item.SendSignal("TIMEOUT", "signal_out");
return;
}
if (string.IsNullOrWhiteSpace(expression) || regex == null) { return; }
if (!ContinuousOutput && nonContinuousOutputSent) { return; }
@@ -100,7 +121,26 @@ namespace Barotrauma.Items.Components
{
try
{
stopwatch.Restart();
Match match = regex.Match(receivedSignal);
stopwatch.Stop();
//workaround to regex timeout issues in .NET 8, see comment on the timeout variable
if (stopwatch.Elapsed > shortTimeout)
{
timeOutsInARow++;
//if the regex times out just once every now and then, it's a symptom of the .NET 8 bug,
//if multiple times in a row, it's most likely a performance-intensive/malicious expression we should react to
if (timeOutsInARow >= MaxTimeOutsInARow)
{
throw new RegexMatchTimeoutException();
}
}
else
{
timeOutsInARow = 0;
}
previousResult = match.Success;
previousGroups = UseCaptureGroup && previousResult ? match.Groups : null;
previousReceivedSignal = receivedSignal;
@@ -109,9 +149,7 @@ namespace Barotrauma.Items.Components
{
if (e is RegexMatchTimeoutException)
{
item.SendSignal("TIMEOUT", "signal_out");
//deactivate the component if the expression caused it to time out
IsActive = false;
timedOut = true;
}
else
{
@@ -2336,7 +2336,6 @@ namespace Barotrauma
private void SendPendingNetworkUpdatesInternal()
{
DebugConsole.NewMessage($"Sending status event for item {Name}", Color.Gray);
CreateStatusEvent(loadingRound: false);
lastSentCondition = condition;
sendConditionUpdateTimer = NetConfig.ItemConditionUpdateInterval;
@@ -113,6 +113,7 @@ namespace Barotrauma
public override void Apply(ActionType type, float deltaTime, Entity entity, IReadOnlyList<ISerializableEntity> targets, Vector2? worldPosition = null)
{
if (this.type != type) { return; }
if (Disabled) { return; }
if (ShouldWaitForInterval(entity, deltaTime)) { return; }
if (!HasRequiredItems(entity)) { return; }
if (delayType == DelayTypes.ReachCursor && Character.Controlled == null) { return; }
@@ -855,6 +855,9 @@ namespace Barotrauma
}
}
/// <summary>
/// Disabled effects will no longer be executed. Used to make <see cref="oneShot">oneshot</> effects only execute once.
/// </summary>
public bool Disabled { get; private set; }
public static StatusEffect Load(ContentXElement element, string parentDebugName)