fix: Fix ConsoleInput for Linux users with try/catch. (#1803)

This commit is contained in:
Daniel Dias Rodrigues 2024-05-30 18:03:28 -03:00 • committed by GitHub
parent 84c68bf73c
commit e85c56ec2d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -18,6 +18,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Threading; using System.Threading;
using Server.Logging;
namespace Server; namespace Server;
@ -147,37 +148,46 @@ public static class ConsoleInputHandler
} }
} }
private static readonly ILogger logger = LogFactory.GetLogger(typeof(ConsoleInputHandler));
private static async void ProcessConsoleInput() private static async void ProcessConsoleInput()
{ {
var token = Core.ClosingTokenSource.Token; var token = Core.ClosingTokenSource.Token;
while (!token.IsCancellationRequested) while (!token.IsCancellationRequested)
{ {
var input = Console.ReadLine()?.Trim(); try
if (Volatile.Read(ref _expectUserInput))
{ {
_input = input; var input = Console.ReadLine()?.Trim();
_receivedUserInput.Set();
_endUserInput.WaitOne();
continue;
}
if (string.IsNullOrEmpty(input)) if (Volatile.Read(ref _expectUserInput))
{
_input = input;
_receivedUserInput.Set();
_endUserInput.WaitOne();
continue;
}
if (string.IsNullOrEmpty(input))
{
continue;
}
var splitInput = input.Split(' ', 2);
var command = splitInput[0].ToLower();
Action<string> action;
lock (_inputCommands)
{
action = _inputCommands.GetValueOrDefault(command)?.Function;
}
action?.Invoke(splitInput.Length > 1 ? splitInput[1] : string.Empty);
}
catch (Exception e)
{ {
continue; logger.Warning(e, "Failed to process console input");
} }
var splitInput = input.Split(' ', 2);
var command = splitInput[0].ToLower();
Action<string> action;
lock (_inputCommands)
{
action = _inputCommands.GetValueOrDefault(command)?.Function;
}
action?.Invoke(splitInput.Length > 1 ? splitInput[1] : string.Empty);
} }
} }