From 912eaab4608595ea5c46909aca64662fa1736654 Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Fri, 5 Apr 2024 15:07:43 -0700 Subject: [PATCH] fix: Fixes unresponsive console input (#1720) ### Summary Fixes an issue where the call priority of `Initialize` functions causes a dead lock because the console input handler was not initialized. --- Projects/Server/Attributes.cs | 9 ++------- Projects/Server/Console/ConsoleInputHandler.cs | 9 +++++++++ Projects/Server/Main.cs | 2 +- Projects/UOContent/Misc/AccountPrompt.cs | 6 +++--- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Projects/Server/Attributes.cs b/Projects/Server/Attributes.cs index e0ea130d2..84ba2fc12 100644 --- a/Projects/Server/Attributes.cs +++ b/Projects/Server/Attributes.cs @@ -78,7 +78,7 @@ public class CallPriorityComparer : IComparer return 0; } - private int GetPriority(MethodInfo mi) + private static int GetPriority(MethodInfo mi) { var objs = mi.GetCustomAttributes(typeof(CallPriorityAttribute), true); @@ -87,12 +87,7 @@ public class CallPriorityComparer : IComparer return 50; } - if (objs[0] is not CallPriorityAttribute attr) - { - return 50; - } - - return attr.Priority; + return (objs[0] as CallPriorityAttribute)?.Priority ?? 50; } } diff --git a/Projects/Server/Console/ConsoleInputHandler.cs b/Projects/Server/Console/ConsoleInputHandler.cs index ee655dcb1..2a4cb2712 100644 --- a/Projects/Server/Console/ConsoleInputHandler.cs +++ b/Projects/Server/Console/ConsoleInputHandler.cs @@ -25,6 +25,7 @@ public static class ConsoleInputHandler { private static readonly AutoResetEvent _receivedUserInput = new(false); private static readonly AutoResetEvent _endUserInput = new(false); + private static bool _initialized; private static bool _expectUserInput; private static readonly Dictionary _inputCommands = new(); private static string[] _commandDescriptions; @@ -81,8 +82,11 @@ public static class ConsoleInputHandler RegisterCommand(["help", "?"], "Displays this help screen.", DisplayHelp); } + [CallPriority(0)] public static void Initialize() { + _initialized = true; + new Thread(ProcessConsoleInput) { IsBackground = true, @@ -179,6 +183,11 @@ public static class ConsoleInputHandler public static string ReadLine() { + if (!_initialized) + { + return Console.ReadLine(); + } + Volatile.Write(ref _expectUserInput, true); _receivedUserInput.WaitOne(); var line = _input; diff --git a/Projects/Server/Main.cs b/Projects/Server/Main.cs index 13976d423..9aa73b501 100644 --- a/Projects/Server/Main.cs +++ b/Projects/Server/Main.cs @@ -339,7 +339,7 @@ public static class Core if (!close) { Console.WriteLine("This exception is fatal, press return to exit"); - Console.ReadLine(); + ConsoleInputHandler.ReadLine(); } Kill(); diff --git a/Projects/UOContent/Misc/AccountPrompt.cs b/Projects/UOContent/Misc/AccountPrompt.cs index 8d50739ea..fc283892b 100644 --- a/Projects/UOContent/Misc/AccountPrompt.cs +++ b/Projects/UOContent/Misc/AccountPrompt.cs @@ -15,16 +15,16 @@ public static class AccountPrompt Console.WriteLine("This server has no accounts."); Console.Write("Do you want to create the owner account now? (y/n): "); - var answer = Console.ReadLine(); + var answer = ConsoleInputHandler.ReadLine(); if (answer.InsensitiveEquals("y")) { Console.WriteLine(); Console.Write("Username: "); - var username = Console.ReadLine(); + var username = ConsoleInputHandler.ReadLine(); Console.Write("Password: "); - var password = Console.ReadLine(); + var password = ConsoleInputHandler.ReadLine(); var a = new Account(username, password) {