feature: Add console command listener for nogui scenarios
Some checks failed
Release / Get Version (push) Successful in 8s
Release / Build and Publish Nuget (push) Successful in 3m35s
Release / Build and Publish Package (push) Failing after 4m22s

This commit is contained in:
zznty
2024-05-15 21:16:10 +07:00
parent 28e26dbf5e
commit b76af4a8b0
3 changed files with 57 additions and 0 deletions

View 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);
}
}
}

View File

@@ -59,6 +59,7 @@ namespace Torch.Server
var sessionManager = Managers.GetManager<ITorchSessionManager>(); var sessionManager = Managers.GetManager<ITorchSessionManager>();
sessionManager.AddFactory(_ => new MultiplayerManagerDedicated(this)); sessionManager.AddFactory(_ => new MultiplayerManagerDedicated(this));
sessionManager.AddFactory(_ => new ConsoleCommandManager(this));
sessionManager.SessionStateChanged += OnSessionStateChanged; sessionManager.SessionStateChanged += OnSessionStateChanged;
// Needs to be done at some point after MyVRageWindows.Init // Needs to be done at some point after MyVRageWindows.Init

View File

@@ -1,4 +1,5 @@
using System; using System;
using JetBrains.Annotations;
using Torch.API; using Torch.API;
using Torch.API.Managers; using Torch.API.Managers;
@@ -25,6 +26,7 @@ namespace Torch.Managers
/// </code> /// </code>
/// </example> /// </example>
[AttributeUsage(AttributeTargets.Field)] [AttributeUsage(AttributeTargets.Field)]
[MeansImplicitUse(ImplicitUseKindFlags.Assign)]
public class DependencyAttribute : Attribute public class DependencyAttribute : Attribute
{ {
/// <summary> /// <summary>