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 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;

View file

@ -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();
}
/// <summary>

View file

@ -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,12 +246,42 @@ namespace MinecraftClient
}
/// <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>
/// <param name="errorMessage">Error message to display and optionally pass to AutoRelog bot</param>
/// <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 HandleOfflineMode()
public static void HandleFailure(string errorMessage = null, bool versionError = false, ChatBots.AutoRelog.DisconnectReason? disconnectReason = null)
{
if (Settings.interactiveMode && offlinePrompt == null)
if (!String.IsNullOrEmpty(errorMessage))
{
ConsoleIO.Reset();
Console.WriteLine(errorMessage);
if (disconnectReason.HasValue)
{
if (ChatBots.AutoRelog.OnDisconnectStatic(disconnectReason.Value, errorMessage))
return; //AutoRelog is triggering a restart of the client
}
}
if (Settings.interactiveMode)
{
if (versionError)
{
Console.Write("Server version : ");
Settings.ServerVersion = Console.ReadLine();
if (Settings.ServerVersion != "")
{
useMcVersionOnce = true;
Restart();
return;
}
}
if (offlinePrompt == null)
{
offlinePrompt = new Thread(new ThreadStart(delegate
{
@ -307,25 +327,6 @@ namespace MinecraftClient
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)
{
Console.Write("Server version : ");
Settings.ServerVersion = Console.ReadLine();
if (Settings.ServerVersion != "")
{
useMcVersionOnce = true;
Restart();
}
else HandleOfflineMode();
}
}
/// <summary>