Skip to content

Commit

Permalink
Fix ConsoleInput for Linux users with try/catch.
Browse files Browse the repository at this point in the history
  • Loading branch information
nerun committed May 30, 2024
1 parent 84c68bf commit f3c8162
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions Projects/Server/Console/ConsoleInputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using Server.Logging;

namespace Server;

Expand Down Expand Up @@ -147,37 +148,44 @@ private static void DisplayHelp(string arguments)
}
}

private static readonly ILogger logger = LogFactory.GetLogger(typeof(ConsoleInputHandler));

private static async void ProcessConsoleInput()
{
var token = Core.ClosingTokenSource.Token;

while (!token.IsCancellationRequested)
{
var input = Console.ReadLine()?.Trim();
try {
var input = Console.ReadLine()?.Trim();

if (Volatile.Read(ref _expectUserInput))
{
_input = input;
_receivedUserInput.Set();
_endUserInput.WaitOne();
continue;
}
if (Volatile.Read(ref _expectUserInput))
{
_input = input;
_receivedUserInput.Set();
_endUserInput.WaitOne();
continue;
}

if (string.IsNullOrEmpty(input))
{
continue;
}
if (string.IsNullOrEmpty(input))
{
continue;
}

var splitInput = input.Split(' ', 2);
var command = splitInput[0].ToLower();
var splitInput = input.Split(' ', 2);
var command = splitInput[0].ToLower();

Action<string> action;
lock (_inputCommands)
{
action = _inputCommands.GetValueOrDefault(command)?.Function;
}
Action<string> action;
lock (_inputCommands)
{
action = _inputCommands.GetValueOrDefault(command)?.Function;
}

action?.Invoke(splitInput.Length > 1 ? splitInput[1] : string.Empty);
action?.Invoke(splitInput.Length > 1 ? splitInput[1] : string.Empty);
}
catch {
logger.Warning("Failed to process console input");
}
}
}

Expand Down

0 comments on commit f3c8162

Please sign in to comment.