From 118b042fdc2b7f5aeed90b4301426ee4f05eecee Mon Sep 17 00:00:00 2001
From: zznty <94796179+zznty@users.noreply.github.com>
Date: Fri, 16 Feb 2024 21:36:07 +0700
Subject: [PATCH] Add console command listener for nogui scenarios
---
.../Managers/ConsoleCommandManager.cs | 62 +++++++++++++++++++
Torch.Server/Torch.Server.csproj | 3 +-
Torch.Server/TorchServer.cs | 1 +
3 files changed, 65 insertions(+), 1 deletion(-)
create mode 100644 Torch.Server/Managers/ConsoleCommandManager.cs
diff --git a/Torch.Server/Managers/ConsoleCommandManager.cs b/Torch.Server/Managers/ConsoleCommandManager.cs
new file mode 100644
index 00000000..bef3f6bd
--- /dev/null
+++ b/Torch.Server/Managers/ConsoleCommandManager.cs
@@ -0,0 +1,62 @@
+using NLog;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+using Torch.API;
+using Torch.API.Managers;
+using Torch.Commands;
+using Torch.Managers;
+
+namespace Torch.Server.Managers
+{
+ internal class ConsoleCommandManager : Manager
+ {
+ private static readonly ILogger Log = LogManager.GetCurrentClassLogger();
+
+ [Dependency]
+ private readonly CommandManager _commandManager;
+
+ public ConsoleCommandManager(ITorchBase torchInstance) : base(torchInstance)
+ {
+ }
+
+ 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);
+ }
+ }
+}
diff --git a/Torch.Server/Torch.Server.csproj b/Torch.Server/Torch.Server.csproj
index 30f0e82d..6db82faf 100644
--- a/Torch.Server/Torch.Server.csproj
+++ b/Torch.Server/Torch.Server.csproj
@@ -244,6 +244,7 @@
+
@@ -538,4 +539,4 @@
copy "$(SolutionDir)NLog.config" "$(TargetDir)" & copy "$(SolutionDir)NLog-user.config" "$(TargetDir)"
-
+
\ No newline at end of file
diff --git a/Torch.Server/TorchServer.cs b/Torch.Server/TorchServer.cs
index 8f59f3d4..070c053a 100644
--- a/Torch.Server/TorchServer.cs
+++ b/Torch.Server/TorchServer.cs
@@ -73,6 +73,7 @@ public TorchServer(TorchConfig config) : base(config)
var sessionManager = Managers.GetManager();
sessionManager.AddFactory(x => new MultiplayerManagerDedicated(this));
+ sessionManager.AddFactory(x => new ConsoleCommandManager(this));
// Needs to be done at some point after MyVRageWindows.Init
// where the debug listeners are registered