From 6261e7adb7fc4be6458e72cd23a09569057cb523 Mon Sep 17 00:00:00 2001 From: ORelio Date: Mon, 20 Apr 2015 17:26:16 +0200 Subject: [PATCH] More startup error handling - Pass minecraft login failure message to AutoRelog bot (suggestion by doranchak) - Fix NullReferenceException in McTcpClient caused by SocketException in ProxyHandler - Refactor error handling code in Program.InitializeClient() - More detailed error messages on network errors. --- MinecraftClient/ChatBots/AutoRelog.cs | 10 +++ MinecraftClient/McTcpClient.cs | 8 ++- MinecraftClient/Program.cs | 72 ++++++++------------- MinecraftClient/Protocol/ProtocolHandler.cs | 4 +- MinecraftClient/Proxy/ProxyHandler.cs | 12 ++-- 5 files changed, 48 insertions(+), 58 deletions(-) diff --git a/MinecraftClient/ChatBots/AutoRelog.cs b/MinecraftClient/ChatBots/AutoRelog.cs index 5f2b9e2c..19d831f5 100644 --- a/MinecraftClient/ChatBots/AutoRelog.cs +++ b/MinecraftClient/ChatBots/AutoRelog.cs @@ -62,5 +62,15 @@ namespace MinecraftClient.ChatBots } return false; } + + public static bool OnDisconnectStatic(DisconnectReason reason, string message) + { + if (Settings.AutoRelog_Enabled) + { + AutoRelog bot = new AutoRelog(Settings.AutoRelog_Delay, Settings.AutoRelog_Retries); + return bot.OnDisconnect(reason, message); + } + return false; + } } } diff --git a/MinecraftClient/McTcpClient.cs b/MinecraftClient/McTcpClient.cs index 25144ebd..01d77dd7 100644 --- a/MinecraftClient/McTcpClient.cs +++ b/MinecraftClient/McTcpClient.cs @@ -151,8 +151,9 @@ namespace MinecraftClient retry = true; } } - catch (SocketException) + catch (SocketException e) { + ConsoleIO.WriteLineFormatted("§8" + e.Message); Console.WriteLine("Failed to connect to this IP."); retry = true; } @@ -166,7 +167,7 @@ namespace MinecraftClient } else if (!singlecommand && Settings.interactiveMode) { - Program.OfflineCommandPrompt(); + Program.HandleOfflineMode(); } } } @@ -369,7 +370,8 @@ namespace MinecraftClient foreach (ChatBot bot in bots) will_restart |= bot.OnDisconnect(reason, message); - if (!will_restart) { Program.OfflineCommandPrompt(); } + if (!will_restart) + Program.HandleOfflineMode(); } /// diff --git a/MinecraftClient/Program.cs b/MinecraftClient/Program.cs index 80fa62b1..4ece8838 100644 --- a/MinecraftClient/Program.cs +++ b/MinecraftClient/Program.cs @@ -169,24 +169,8 @@ namespace MinecraftClient if (!ProtocolHandler.GetServerInfo(Settings.ServerIP, Settings.ServerPort, ref protocolversion)) { Console.WriteLine("Failed to ping this IP."); - if (Settings.AutoRelog_Enabled) - { - ChatBots.AutoRelog bot = new ChatBots.AutoRelog(Settings.AutoRelog_Delay, Settings.AutoRelog_Retries); - if (!bot.OnDisconnect(ChatBot.DisconnectReason.ConnectionLost, "Failed to ping this IP.")) { OfflineCommandPrompt(); } - } - else - { - if (Settings.interactiveMode) - { - if (MinecraftVersionPrompt()) - { - Restart(); - return; - } - OfflineCommandPrompt(); - } - } - return; + if (!ChatBots.AutoRelog.OnDisconnectStatic(ChatBot.DisconnectReason.ConnectionLost, "Failed to ping this IP.")) + HandleServerVersionFailure(); } } @@ -204,42 +188,40 @@ namespace MinecraftClient catch (NotSupportedException) { Console.WriteLine("Cannot connect to the server : This version is not supported !"); - OfflineCommandPrompt(); + HandleServerVersionFailure(); } } else { Console.WriteLine("Failed to determine server version."); - if (Settings.interactiveMode && MinecraftVersionPrompt()) - { - Restart(); - return; - } + HandleServerVersionFailure(); } } else { Console.ForegroundColor = ConsoleColor.Gray; - Console.Write("Connection failed : "); + string failureMessage = "Minecraft Login failed : "; switch (result) { - case ProtocolHandler.LoginResult.AccountMigrated: Console.WriteLine("Account migrated, use e-mail as username."); break; - case ProtocolHandler.LoginResult.ServiceUnavailable: Console.WriteLine("Login servers are unavailable. Please try again later."); break; - case ProtocolHandler.LoginResult.WrongPassword: Console.WriteLine("Incorrect password."); break; - case ProtocolHandler.LoginResult.NotPremium: Console.WriteLine("User not premium."); break; - case ProtocolHandler.LoginResult.OtherError: Console.WriteLine("Network error."); break; - case ProtocolHandler.LoginResult.SSLError: Console.WriteLine("SSL Error."); - if (isUsingMono) - { - ConsoleIO.WriteLineFormatted("§8It appears that you are using Mono to run this program." - + '\n' + "The first time, you have to import HTTPS certificates using:" - + '\n' + "mozroots --import --ask-remove"); - return; - } - break; + case ProtocolHandler.LoginResult.AccountMigrated: failureMessage += "Account migrated, use e-mail as username."; break; + case ProtocolHandler.LoginResult.ServiceUnavailable: failureMessage += "Login servers are unavailable. Please try again later."; break; + case ProtocolHandler.LoginResult.WrongPassword: failureMessage += "Incorrect password."; break; + case ProtocolHandler.LoginResult.NotPremium: failureMessage += "User not premium."; break; + case ProtocolHandler.LoginResult.OtherError: failureMessage += "Network error."; break; + 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." + + '\n' + "The first time, you have to import HTTPS certificates using:" + + '\n' + "mozroots --import --ask-remove"); + return; } while (Console.KeyAvailable) { Console.ReadKey(false); } - if (Settings.SingleCommand == "") { OfflineCommandPrompt(); } + if (!ChatBots.AutoRelog.OnDisconnectStatic(ChatBot.DisconnectReason.LoginRejected, failureMessage)) + HandleOfflineMode(); } } @@ -274,10 +256,10 @@ namespace MinecraftClient } /// - /// Pause the program, usually when an error or a kick occured, letting the user press Enter to quit OR type /reconnect + /// Pause the program, usually when an error or a kick occured, letting the user typing commands to reconnect to a server /// - public static void OfflineCommandPrompt() + public static void HandleOfflineMode() { if (Settings.interactiveMode && offlinePrompt == null) { @@ -331,7 +313,7 @@ namespace MinecraftClient /// /// TRUE if a Minecraft version has been read from prompt - public static bool MinecraftVersionPrompt() + public static void HandleServerVersionFailure() { if (Settings.interactiveMode) { @@ -340,10 +322,10 @@ namespace MinecraftClient if (Settings.ServerVersion != "") { useMcVersionOnce = true; - return true; + Restart(); } + else HandleOfflineMode(); } - return false; } /// diff --git a/MinecraftClient/Protocol/ProtocolHandler.cs b/MinecraftClient/Protocol/ProtocolHandler.cs index d33ef7e8..eb7231f0 100644 --- a/MinecraftClient/Protocol/ProtocolHandler.cs +++ b/MinecraftClient/Protocol/ProtocolHandler.cs @@ -38,9 +38,9 @@ namespace MinecraftClient.Protocol } else ConsoleIO.WriteLineFormatted("§8Unexpected answer from the server (is that a Minecraft server ?)"); } - catch + catch (Exception e) { - ConsoleIO.WriteLineFormatted("§8An error occured while attempting to connect to this IP."); + ConsoleIO.WriteLineFormatted("§8" + e.Message); } }, TimeSpan.FromSeconds(30))) { diff --git a/MinecraftClient/Proxy/ProxyHandler.cs b/MinecraftClient/Proxy/ProxyHandler.cs index 71b5faba..d18d4ea1 100644 --- a/MinecraftClient/Proxy/ProxyHandler.cs +++ b/MinecraftClient/Proxy/ProxyHandler.cs @@ -57,15 +57,11 @@ namespace MinecraftClient.Proxy } else return new TcpClient(host, port); } - catch (Exception e) + catch (ProxyException e) { - if (e is ProxyException || e is SocketException) - { - ConsoleIO.WriteLineFormatted("§8" + e.Message); - proxy = null; - return null; - } - else throw; + ConsoleIO.WriteLineFormatted("§8" + e.Message); + proxy = null; + return null; } } }