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)
This commit is contained in:
ORelio 2015-04-22 10:27:53 +02:00
parent 8b261894c8
commit 57c66c82d7
3 changed files with 81 additions and 80 deletions

View file

@ -14,7 +14,7 @@ namespace MinecraftClient
public static class ConsoleIO 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 void SetAutoCompleteEngine(IAutoComplete engine) { autocomplete_engine = engine; }
public static bool basicIO = false; public static bool basicIO = false;
private static IAutoComplete autocomplete_engine; private static IAutoComplete autocomplete_engine;

View file

@ -167,7 +167,7 @@ namespace MinecraftClient
} }
else if (!singlecommand && Settings.interactiveMode) else if (!singlecommand && Settings.interactiveMode)
{ {
Program.HandleOfflineMode(); Program.HandleFailure();
} }
} }
} }
@ -371,7 +371,7 @@ namespace MinecraftClient
will_restart |= bot.OnDisconnect(reason, message); will_restart |= bot.OnDisconnect(reason, message);
if (!will_restart) if (!will_restart)
Program.HandleOfflineMode(); Program.HandleFailure();
} }
/// <summary> /// <summary>

View file

@ -167,11 +167,7 @@ namespace MinecraftClient
{ {
Console.WriteLine("Retrieving Server Info..."); Console.WriteLine("Retrieving Server Info...");
if (!ProtocolHandler.GetServerInfo(Settings.ServerIP, Settings.ServerPort, ref protocolversion)) if (!ProtocolHandler.GetServerInfo(Settings.ServerIP, Settings.ServerPort, ref protocolversion))
{ HandleFailure("Failed to ping this IP.", true, ChatBots.AutoRelog.DisconnectReason.ConnectionLost);
Console.WriteLine("Failed to ping this IP.");
if (!ChatBots.AutoRelog.OnDisconnectStatic(ChatBot.DisconnectReason.ConnectionLost, "Failed to ping this IP."))
HandleServerVersionFailure();
}
} }
if (protocolversion != 0) if (protocolversion != 0)
@ -184,18 +180,14 @@ namespace MinecraftClient
Client = new McTcpClient(Settings.Username, UUID, sessionID, Settings.ServerIP, Settings.ServerPort, protocolversion, Settings.SingleCommand); 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); 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) catch (NotSupportedException) { HandleFailure("Cannot connect to the server : This version is not supported !", true); }
{
Console.WriteLine("Cannot connect to the server : This version is not supported !");
HandleServerVersionFailure();
}
}
else
{
Console.WriteLine("Failed to determine server version.");
HandleServerVersionFailure();
} }
else HandleFailure("Failed to determine server version.", true);
} }
else else
{ {
@ -211,7 +203,6 @@ namespace MinecraftClient
case ProtocolHandler.LoginResult.SSLError: failureMessage += "SSL Error."; break; case ProtocolHandler.LoginResult.SSLError: failureMessage += "SSL Error."; break;
default: failureMessage += "Unknown Error."; break; default: failureMessage += "Unknown Error."; break;
} }
Console.WriteLine(failureMessage);
if (result == ProtocolHandler.LoginResult.SSLError && isUsingMono) if (result == ProtocolHandler.LoginResult.SSLError && isUsingMono)
{ {
ConsoleIO.WriteLineFormatted("§8It appears that you are using Mono to run this program." ConsoleIO.WriteLineFormatted("§8It appears that you are using Mono to run this program."
@ -220,8 +211,7 @@ namespace MinecraftClient
return; return;
} }
while (Console.KeyAvailable) { Console.ReadKey(false); } while (Console.KeyAvailable) { Console.ReadKey(false); }
if (!ChatBots.AutoRelog.OnDisconnectStatic(ChatBot.DisconnectReason.LoginRejected, failureMessage)) HandleFailure(failureMessage, false, ChatBot.DisconnectReason.LoginRejected);
HandleOfflineMode();
} }
} }
@ -256,75 +246,86 @@ namespace MinecraftClient
} }
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
/// <param name="errorMessage">Error message to display and optionally pass to AutoRelog bot</param>
public static void HandleOfflineMode() /// <param name="versionError">Specify if the error is related to an incompatible or unkown server version</param>
/// <param name="disconnectReason">If set, the error message will be processed by the AutoRelog bot</param>
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 = " "; if (ChatBots.AutoRelog.OnDisconnectStatic(disconnectReason.Value, errorMessage))
ConsoleIO.WriteLineFormatted("Not connected to any server. Use '" + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + "help' for help."); return; //AutoRelog is triggering a restart of the client
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();
} }
}
/// <summary>
/// Ask for server version when failed to ping server and/or determinate serveur version
/// </summary>
/// <returns>TRUE if a Minecraft version has been read from prompt</returns>
public static void HandleServerVersionFailure()
{
if (Settings.interactiveMode) if (Settings.interactiveMode)
{ {
Console.Write("Server version : "); if (versionError)
Settings.ServerVersion = Console.ReadLine();
if (Settings.ServerVersion != "")
{ {
useMcVersionOnce = true; Console.Write("Server version : ");
Restart(); 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();
} }
} }