feature: Add console command listener for nogui scenarios
This commit is contained in:
54
Torch.Server/Managers/ConsoleCommandManager.cs
Normal file
54
Torch.Server/Managers/ConsoleCommandManager.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using NLog;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using Torch.API;
|
||||
using Torch.API.Managers;
|
||||
using Torch.Commands;
|
||||
using Torch.Managers;
|
||||
|
||||
namespace Torch.Server.Managers
|
||||
{
|
||||
internal class ConsoleCommandManager(ITorchBase torchInstance) : Manager(torchInstance)
|
||||
{
|
||||
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
[Dependency]
|
||||
private CommandManager _commandManager;
|
||||
|
||||
public override void Attach()
|
||||
{
|
||||
if (!Torch.Config.NoGui)
|
||||
return;
|
||||
|
||||
Log.Info("Starting console command listener");
|
||||
|
||||
new Thread(CommandListener)
|
||||
{
|
||||
Name = "Console Command Listener",
|
||||
IsBackground = true,
|
||||
}.Start();
|
||||
}
|
||||
|
||||
private void CommandListener()
|
||||
{
|
||||
while (Torch.GameState < TorchGameState.Unloading)
|
||||
{
|
||||
var line = Console.ReadLine();
|
||||
|
||||
if (line == null)
|
||||
break;
|
||||
|
||||
Torch.Invoke(() =>
|
||||
{
|
||||
if (!_commandManager.HandleCommandFromServer(line, LogResponse))
|
||||
Log.Error("Invalid input '{0}'", line);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void LogResponse(TorchChatMessage message)
|
||||
{
|
||||
Log.Info(message.Message);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user