From 57c66c82d7f269462b73b44b7ddeaabb661fdc72 Mon Sep 17 00:00:00 2001 From: ORelio Date: Wed, 22 Apr 2015 10:27:53 +0200 Subject: [PATCH] Merge error handlers - Merge all error handling code into one method - Fix ConsoleIO not clearing the line being typed upon reset - Update console title upon logging in to the server - Pass "failed to ping this IP" to AutoRelog (thx doranchak) --- MinecraftClient/ConsoleIO.cs | 2 +- MinecraftClient/McTcpClient.cs | 4 +- MinecraftClient/Program.cs | 155 +++++++++++++++++---------------- 3 files changed, 81 insertions(+), 80 deletions(-) diff --git a/MinecraftClient/ConsoleIO.cs b/MinecraftClient/ConsoleIO.cs index 131768bd..463d309e 100644 --- a/MinecraftClient/ConsoleIO.cs +++ b/MinecraftClient/ConsoleIO.cs @@ -14,7 +14,7 @@ namespace MinecraftClient public static class ConsoleIO { - public static void Reset() { if (reading) { reading = false; Console.Write("\b \b"); } } + public static void Reset() { if (reading) { ClearLineAndBuffer(); reading = false; Console.Write("\b \b"); } } public static void SetAutoCompleteEngine(IAutoComplete engine) { autocomplete_engine = engine; } public static bool basicIO = false; private static IAutoComplete autocomplete_engine; diff --git a/MinecraftClient/McTcpClient.cs b/MinecraftClient/McTcpClient.cs index 01d77dd7..cf14046f 100644 --- a/MinecraftClient/McTcpClient.cs +++ b/MinecraftClient/McTcpClient.cs @@ -167,7 +167,7 @@ namespace MinecraftClient } else if (!singlecommand && Settings.interactiveMode) { - Program.HandleOfflineMode(); + Program.HandleFailure(); } } } @@ -371,7 +371,7 @@ namespace MinecraftClient will_restart |= bot.OnDisconnect(reason, message); if (!will_restart) - Program.HandleOfflineMode(); + Program.HandleFailure(); } /// diff --git a/MinecraftClient/Program.cs b/MinecraftClient/Program.cs index 4ece8838..399e2f14 100644 --- a/MinecraftClient/Program.cs +++ b/MinecraftClient/Program.cs @@ -167,11 +167,7 @@ namespace MinecraftClient { Console.WriteLine("Retrieving Server Info..."); if (!ProtocolHandler.GetServerInfo(Settings.ServerIP, Settings.ServerPort, ref protocolversion)) - { - Console.WriteLine("Failed to ping this IP."); - if (!ChatBots.AutoRelog.OnDisconnectStatic(ChatBot.DisconnectReason.ConnectionLost, "Failed to ping this IP.")) - HandleServerVersionFailure(); - } + HandleFailure("Failed to ping this IP.", true, ChatBots.AutoRelog.DisconnectReason.ConnectionLost); } if (protocolversion != 0) @@ -184,18 +180,14 @@ namespace MinecraftClient Client = new McTcpClient(Settings.Username, UUID, sessionID, Settings.ServerIP, Settings.ServerPort, protocolversion, Settings.SingleCommand); } else Client = new McTcpClient(Settings.Username, UUID, sessionID, protocolversion, Settings.ServerIP, Settings.ServerPort); + + //Update console title + if (Settings.ConsoleTitle != "") + Console.Title = Settings.expandVars(Settings.ConsoleTitle); } - catch (NotSupportedException) - { - Console.WriteLine("Cannot connect to the server : This version is not supported !"); - HandleServerVersionFailure(); - } - } - else - { - Console.WriteLine("Failed to determine server version."); - HandleServerVersionFailure(); + catch (NotSupportedException) { HandleFailure("Cannot connect to the server : This version is not supported !", true); } } + else HandleFailure("Failed to determine server version.", true); } else { @@ -211,7 +203,6 @@ namespace MinecraftClient case ProtocolHandler.LoginResult.SSLError: failureMessage += "SSL Error."; break; default: failureMessage += "Unknown Error."; break; } - Console.WriteLine(failureMessage); if (result == ProtocolHandler.LoginResult.SSLError && isUsingMono) { ConsoleIO.WriteLineFormatted("§8It appears that you are using Mono to run this program." @@ -220,8 +211,7 @@ namespace MinecraftClient return; } while (Console.KeyAvailable) { Console.ReadKey(false); } - if (!ChatBots.AutoRelog.OnDisconnectStatic(ChatBot.DisconnectReason.LoginRejected, failureMessage)) - HandleOfflineMode(); + HandleFailure(failureMessage, false, ChatBot.DisconnectReason.LoginRejected); } } @@ -256,75 +246,86 @@ namespace MinecraftClient } /// - /// Pause the program, usually when an error or a kick occured, letting the user typing commands to reconnect to a server + /// Handle fatal errors such as ping failure, login failure, server disconnection, and so on. + /// Allows AutoRelog to perform on fatal errors, prompt for server version, and offline commands. /// - - public static void HandleOfflineMode() + /// Error message to display and optionally pass to AutoRelog bot + /// Specify if the error is related to an incompatible or unkown server version + /// If set, the error message will be processed by the AutoRelog bot + + public static void HandleFailure(string errorMessage = null, bool versionError = false, ChatBots.AutoRelog.DisconnectReason? disconnectReason = null) { - if (Settings.interactiveMode && offlinePrompt == null) + if (!String.IsNullOrEmpty(errorMessage)) { - offlinePrompt = new Thread(new ThreadStart(delegate + ConsoleIO.Reset(); + Console.WriteLine(errorMessage); + + if (disconnectReason.HasValue) { - string command = " "; - ConsoleIO.WriteLineFormatted("Not connected to any server. Use '" + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + "help' for help."); - ConsoleIO.WriteLineFormatted("Or press Enter to exit Minecraft Console Client."); - while (command.Length > 0) - { - if (!ConsoleIO.basicIO) { ConsoleIO.Write('>'); } - command = Console.ReadLine().Trim(); - if (command.Length > 0) - { - string message = ""; - - if (Settings.internalCmdChar != ' ' - && command[0] == Settings.internalCmdChar) - command = command.Substring(1); - - if (command.StartsWith("reco")) - { - message = new Commands.Reco().Run(null, Settings.expandVars(command)); - } - else if (command.StartsWith("connect")) - { - message = new Commands.Connect().Run(null, Settings.expandVars(command)); - } - else if (command.StartsWith("exit") || command.StartsWith("quit")) - { - message = new Commands.Exit().Run(null, Settings.expandVars(command)); - } - else if (command.StartsWith("help")) - { - ConsoleIO.WriteLineFormatted("§8MCC: " + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + new Commands.Reco().CMDDesc); - ConsoleIO.WriteLineFormatted("§8MCC: " + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + new Commands.Connect().CMDDesc); - } - else ConsoleIO.WriteLineFormatted("§8Unknown command '" + command.Split(' ')[0] + "'."); - - if (message != "") - ConsoleIO.WriteLineFormatted("§8MCC: " + message); - } - } - })); - offlinePrompt.Start(); + if (ChatBots.AutoRelog.OnDisconnectStatic(disconnectReason.Value, errorMessage)) + return; //AutoRelog is triggering a restart of the client + } } - } - /// - /// Ask for server version when failed to ping server and/or determinate serveur version - /// - /// TRUE if a Minecraft version has been read from prompt - - public static void HandleServerVersionFailure() - { if (Settings.interactiveMode) { - Console.Write("Server version : "); - Settings.ServerVersion = Console.ReadLine(); - if (Settings.ServerVersion != "") + if (versionError) { - useMcVersionOnce = true; - Restart(); + Console.Write("Server version : "); + Settings.ServerVersion = Console.ReadLine(); + if (Settings.ServerVersion != "") + { + useMcVersionOnce = true; + Restart(); + return; + } + } + + if (offlinePrompt == null) + { + offlinePrompt = new Thread(new ThreadStart(delegate + { + string command = " "; + ConsoleIO.WriteLineFormatted("Not connected to any server. Use '" + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + "help' for help."); + ConsoleIO.WriteLineFormatted("Or press Enter to exit Minecraft Console Client."); + while (command.Length > 0) + { + if (!ConsoleIO.basicIO) { ConsoleIO.Write('>'); } + command = Console.ReadLine().Trim(); + if (command.Length > 0) + { + string message = ""; + + if (Settings.internalCmdChar != ' ' + && command[0] == Settings.internalCmdChar) + command = command.Substring(1); + + if (command.StartsWith("reco")) + { + message = new Commands.Reco().Run(null, Settings.expandVars(command)); + } + else if (command.StartsWith("connect")) + { + message = new Commands.Connect().Run(null, Settings.expandVars(command)); + } + else if (command.StartsWith("exit") || command.StartsWith("quit")) + { + message = new Commands.Exit().Run(null, Settings.expandVars(command)); + } + else if (command.StartsWith("help")) + { + ConsoleIO.WriteLineFormatted("§8MCC: " + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + new Commands.Reco().CMDDesc); + ConsoleIO.WriteLineFormatted("§8MCC: " + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + new Commands.Connect().CMDDesc); + } + else ConsoleIO.WriteLineFormatted("§8Unknown command '" + command.Split(' ')[0] + "'."); + + if (message != "") + ConsoleIO.WriteLineFormatted("§8MCC: " + message); + } + } + })); + offlinePrompt.Start(); } - else HandleOfflineMode(); } }