Skip to content

Commit

Permalink
Merge pull request #255 from iRebbok/tmux-troubleshoot
Browse files Browse the repository at this point in the history
Implement a new console input system to fix an issue with the tmux send-keys command
  • Loading branch information
ButterscotchV authored May 24, 2021
2 parents 5c14b34 + 4ccd67a commit 8961c7c
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 21 deletions.
18 changes: 18 additions & 0 deletions MultiAdmin/Config/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using MultiAdmin.ConsoleTools;
using MultiAdmin.ServerIO;
using MultiAdmin.Utility;

namespace MultiAdmin.Config
Expand Down Expand Up @@ -241,5 +242,22 @@ public bool GetBool(string key, bool def = false)

return def;
}

public InputHandler.ConsoleInputSystem GetConsoleInputSystem(string key, InputHandler.ConsoleInputSystem def = InputHandler.ConsoleInputSystem.New)
{
try
{
string value = GetString(key);

if (!string.IsNullOrEmpty(value) && Enum.TryParse<InputHandler.ConsoleInputSystem>(value, out var consoleInputSystem))
return consoleInputSystem;
}
catch (Exception e)
{
Program.LogDebugException(nameof(GetConsoleInputSystem), e);
}

return def;
}
}
}
32 changes: 31 additions & 1 deletion MultiAdmin/Config/MultiAdminConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ public class MultiAdminConfig : InheritableConfigRegister

public ConfigEntry<bool> UseNewInputSystem { get; } =
new ConfigEntry<bool>("use_new_input_system", true,
"Use New Input System", "Whether to use the new input system, if false, the original input system will be used");
"Use New Input System", "**OBSOLETE: Use `console_input_system` instead, this config option may be removed in a future version of MultiAdmin.** Whether to use the new input system, if false, the original input system will be used");

public ConfigEntry<InputHandler.ConsoleInputSystem> ConsoleInputSystem { get; } =
new ConfigEntry<InputHandler.ConsoleInputSystem>("console_input_system", InputHandler.ConsoleInputSystem.New,
"Console Input System", "Which console input system to use");

public ConfigEntry<bool> HideInput { get; } =
new ConfigEntry<bool>("hide_input", false,
Expand Down Expand Up @@ -172,6 +176,26 @@ public class MultiAdminConfig : InheritableConfigRegister

#endregion

public InputHandler.ConsoleInputSystem ActualConsoleInputSystem
{
get
{
if (UseNewInputSystem.Value)
{
switch (ConsoleInputSystem.Value)
{
case InputHandler.ConsoleInputSystem.New:
return HideInput.Value ? InputHandler.ConsoleInputSystem.Old : InputHandler.ConsoleInputSystem.New;

case InputHandler.ConsoleInputSystem.Old:
return InputHandler.ConsoleInputSystem.Old;
}
}

return InputHandler.ConsoleInputSystem.Original;
}
}

public const string ConfigFileName = "scp_multiadmin.cfg";
public static readonly string GlobalConfigFilePath = Utils.GetFullPathSafe(ConfigFileName);

Expand Down Expand Up @@ -299,6 +323,12 @@ public override void UpdateConfigValueInheritable(ConfigEntry configEntry)
break;
}

case ConfigEntry<InputHandler.ConsoleInputSystem> config:
{
config.Value = Config.GetConsoleInputSystem(config.Key, config.Default);
break;
}

default:
{
throw new ArgumentException(
Expand Down
7 changes: 7 additions & 0 deletions MultiAdmin/Features/GithubGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using MultiAdmin.Config;
using MultiAdmin.Config.ConfigHandler;
using MultiAdmin.Features.Attributes;
using MultiAdmin.ServerIO;
using MultiAdmin.Utility;

namespace MultiAdmin.Features
Expand Down Expand Up @@ -122,6 +123,12 @@ public void OnCall(string[] args)
break;
}

case ConfigEntry<InputHandler.ConsoleInputSystem> config:
{
stringBuilder.Append($"ConsoleInputSystem{ColumnSeparator}{config.Default}");
break;
}

default:
{
stringBuilder.Append(
Expand Down
3 changes: 1 addition & 2 deletions MultiAdmin/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ public static void Write(string message, ConsoleColor color = ConsoleColor.DarkY
{
if (Headless) return;

new ColoredMessage(Utils.TimeStampMessage(message), color).WriteLine((!MultiAdminConfig.GlobalConfig?.HideInput?.Value ?? false) &&
(MultiAdminConfig.GlobalConfig?.UseNewInputSystem?.Value ?? false));
new ColoredMessage(Utils.TimeStampMessage(message), color).WriteLine(MultiAdminConfig.GlobalConfig?.ActualConsoleInputSystem == InputHandler.ConsoleInputSystem.New);
}
}

Expand Down
9 changes: 6 additions & 3 deletions MultiAdmin/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,9 @@ public void StartServer(bool restartOnCrash = true)

Write($"Starting server with the following parameters:\n{scpslExe} {startInfo.Arguments}");

if (ServerConfig.ActualConsoleInputSystem == InputHandler.ConsoleInputSystem.Original)
Write("You are using the original input system. It may prevent MultiAdmin from closing and/or cause ghost game processes", ConsoleColor.Red);

// Reset the supported mod features
supportedModFeatures = ModFeatures.None;

Expand Down Expand Up @@ -650,10 +653,10 @@ public void Write(ColoredMessage[] messages, ConsoleColor? timeStampColor = null

ColoredMessage[] timeStampedMessage = Utils.TimeStampMessage(messages, timeStampColor);

timeStampedMessage.WriteLine(!ServerConfig.HideInput.Value && ServerConfig.UseNewInputSystem.Value);
timeStampedMessage.WriteLine(ServerConfig.ActualConsoleInputSystem == InputHandler.ConsoleInputSystem.New);

if (!ServerConfig.HideInput.Value && ServerConfig.UseNewInputSystem.Value)
InputHandler.WriteInputAndSetCursor(ServerConfig);
if (ServerConfig.ActualConsoleInputSystem == InputHandler.ConsoleInputSystem.New)
InputHandler.WriteInputAndSetCursor(true);
}
}

Expand Down
40 changes: 26 additions & 14 deletions MultiAdmin/ServerIO/InputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MultiAdmin.Config;
using MultiAdmin.ConsoleTools;
using MultiAdmin.Utility;

Expand Down Expand Up @@ -61,14 +60,18 @@ public static async void Write(Server server, CancellationToken cancellationToke
}

string message;
if (!server.ServerConfig.HideInput.Value && server.ServerConfig.UseNewInputSystem.Value && SectionBufferWidth - TotalIndicatorLength > 0)
if (server.ServerConfig.ActualConsoleInputSystem == ConsoleInputSystem.New && SectionBufferWidth - TotalIndicatorLength > 0)
{
message = await GetInputLineNew(server, cancellationToken, prevMessages);
}
else
else if (server.ServerConfig.ActualConsoleInputSystem == ConsoleInputSystem.Old)
{
message = await GetInputLineOld(server, cancellationToken);
}
else
{
message = Console.ReadLine();
}

if (string.IsNullOrEmpty(message)) continue;

Expand Down Expand Up @@ -252,8 +255,7 @@ public static async Task<string> GetInputLineNew(Server server, CancellationToke

SetCurrentInput(curSection.Value.Section);
CurrentCursor = curSection.Value.GetRelativeIndex(messageCursor);

WriteInputAndSetCursor(server.ServerConfig);
WriteInputAndSetCursor(true);
}
else
{
Expand All @@ -267,7 +269,7 @@ public static async Task<string> GetInputLineNew(Server server, CancellationToke
SetCurrentInput(message);
CurrentCursor = messageCursor;

WriteInputAndSetCursor(server.ServerConfig);
WriteInputAndSetCursor(true);
}
}
else if (CurrentCursor != messageCursor)
Expand All @@ -291,7 +293,7 @@ public static async Task<string> GetInputLineNew(Server server, CancellationToke

SetCurrentInput(curSection.Value.Section);

WriteInputAndSetCursor(server.ServerConfig);
WriteInputAndSetCursor(true);
}

// Otherwise, if only the relative cursor index has changed, set only the cursor
Expand Down Expand Up @@ -400,29 +402,28 @@ public static void SetCursor()
SetCursor(CurrentCursor);
}

public static void WriteInput(ColoredMessage[] message, MultiAdminConfig config = null)
public static void WriteInput(ColoredMessage[] message, bool clearConsoleLine = false)
{
lock (ColoredConsole.WriteLock)
{
if (Program.Headless) return;

MultiAdminConfig curConfig = config ?? MultiAdminConfig.GlobalConfig;
message?.Write(!curConfig.HideInput.Value && curConfig.UseNewInputSystem.Value);
message?.Write(clearConsoleLine);

CurrentInput = message;
}
}

public static void WriteInput(MultiAdminConfig config = null)
public static void WriteInput(bool clearConsoleLine = false)
{
WriteInput(CurrentInput, config);
WriteInput(CurrentInput, clearConsoleLine);
}

public static void WriteInputAndSetCursor(MultiAdminConfig config = null)
public static void WriteInputAndSetCursor(bool clearConsoleLine = false)
{
lock (ColoredConsole.WriteLock)
{
WriteInput(config);
WriteInput(clearConsoleLine);
SetCursor();
}
}
Expand Down Expand Up @@ -450,5 +451,16 @@ public static void RandomizeInputColors()
Program.LogDebugException(nameof(RandomizeInputColors), e);
}
}

public enum ConsoleInputSystem
{
// Represents the default input system, which calls Console.ReadLine and blocks the calling context
Original,
// Represents the "old" input system, which calls non-blocking methods
Old,
// Represents the "new" input system, which also calls non-blocking methods,
// but the main difference is great display
New,
}
}
}
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Make sure that you are running Mono 5.18.0 or higher, otherwise you might have i
4. Optional: Create a file named `scp_multiadmin.cfg` within your server's folder for configuring MultiAdmin specifically for that server

## Features

- Config Generator: Generates a full default MultiAdmin config file
- Config Reload: Reloads the MultiAdmin configuration file
- Exit Command: Adds a graceful exit command
Expand Down Expand Up @@ -71,7 +72,8 @@ multiadmin_nolog | Boolean | False | Disable logging to file
multiadmin_debug_log | Boolean | True | Enables MultiAdmin debug logging, this logs to a separate file than any other logs
multiadmin_debug_log_blacklist | String List | HandleMessage, StringMatches, MessageListener | Which tags to block for MultiAdmin debug logging
multiadmin_debug_log_whitelist | String List | **Empty** | Which tags to log for MultiAdmin debug logging (Defaults to logging all if none are provided)
use_new_input_system | Boolean | True | Whether to use the new input system, if false, the original input system will be used
use_new_input_system | Boolean | True | **OBSOLETE: Use `console_input_system` instead, this config option may be removed in a future version of MultiAdmin.** Whether to use the new input system, if false, the original input system will be used
console_input_system | ConsoleInputSystem | New | Which console input system to use
hide_input | Boolean | False | Whether to hide console input, if true, typed input will not be printed
port | Unsigned Integer | 7777 | The port for the server to use
copy_from_folder_on_reload | String | **Empty** | The location of a folder to copy files from into the folder defined by `config_location` whenever the configuration file is reloaded
Expand Down Expand Up @@ -101,3 +103,12 @@ multiadmin_tick_delay | Integer | 1000 | The time in milliseconds between MultiA
servers_folder | String | servers | The location of the `servers` folder for MultiAdmin to load multiple server configurations from
set_title_bar | Boolean | True | Whether to set the console window's titlebar, if false, this feature won't be used
start_config_on_full | String | **Empty** | Start server with this config folder once the server becomes full [Requires Modding]

## ConsoleInputSystem
If you are running into issues with the `tmux send-keys` command, switch to the original input system.

String Value | Integer Value | Description
--- | :---: | :----:
Original | 0 | Represents the original input system. It may prevent MultiAdmin from closing and/or cause ghost game processes.
Old | 1 | Represents the old input system. This input system should operate similarly to the original input system but won't cause issues with MultiAdmin's functionality.
New | 2 | Represents the new input system. The main difference from the original input system is an improved display.

0 comments on commit 8961c7c

Please sign in to comment.