Variable engine done, catch bot exceptions

- %variable% variables can be declared in the INI file and used
elsewhere
- Default argument 'true' for WriteLineFormatted in ConsoleIO
- Exceptions thrown by bots no longer disconnect from the server, stack
trace is printed instead
This commit is contained in:
ORelio 2014-06-11 20:40:25 +02:00
parent 898a04a843
commit 068b87a11a
10 changed files with 111 additions and 30 deletions

View file

@ -209,7 +209,7 @@ namespace MinecraftClient
public static void LogToConsole(string text)
{
ConsoleIO.WriteLineFormatted("§8[BOT] " + text, true);
ConsoleIO.WriteLineFormatted("§8[BOT] " + text);
}
/// <summary>

View file

@ -249,7 +249,7 @@ namespace MinecraftClient
/// <param name="str">String to write</param>
/// <param name="acceptnewlines">If false, space are printed instead of newlines</param>
public static void WriteLineFormatted(string str, bool acceptnewlines)
public static void WriteLineFormatted(string str, bool acceptnewlines = true)
{
if (basicIO) { Console.WriteLine(str); return; }
if (!String.IsNullOrEmpty(str))

View file

@ -101,10 +101,10 @@ namespace MinecraftClient
if (Settings.AntiAFK_Enabled) { BotLoad(new ChatBots.AntiAFK(Settings.AntiAFK_Delay)); }
if (Settings.Hangman_Enabled) { BotLoad(new ChatBots.HangmanGame(Settings.Hangman_English)); }
if (Settings.Alerts_Enabled) { BotLoad(new ChatBots.Alerts()); }
if (Settings.ChatLog_Enabled) { BotLoad(new ChatBots.ChatLog(Settings.ChatLog_File.Replace("%username%", Settings.Username), Settings.ChatLog_Filter, Settings.ChatLog_DateTime)); }
if (Settings.PlayerLog_Enabled) { BotLoad(new ChatBots.PlayerListLogger(Settings.PlayerLog_Delay, Settings.PlayerLog_File.Replace("%username%", Settings.Username))); }
if (Settings.ChatLog_Enabled) { BotLoad(new ChatBots.ChatLog(Settings.replaceVars(Settings.ChatLog_File), Settings.ChatLog_Filter, Settings.ChatLog_DateTime)); }
if (Settings.PlayerLog_Enabled) { BotLoad(new ChatBots.PlayerListLogger(Settings.PlayerLog_Delay, Settings.replaceVars(Settings.PlayerLog_File))); }
if (Settings.AutoRelog_Enabled) { BotLoad(new ChatBots.AutoRelog(Settings.AutoRelog_Delay, Settings.AutoRelog_Retries)); }
if (Settings.ScriptScheduler_Enabled) { BotLoad(new ChatBots.ScriptScheduler(Settings.ScriptScheduler_TasksFile.Replace("%username%", Settings.Username))); }
if (Settings.ScriptScheduler_Enabled) { BotLoad(new ChatBots.ScriptScheduler(Settings.replaceVars(Settings.ScriptScheduler_TasksFile))); }
if (Settings.RemoteCtrl_Enabled) { BotLoad(new ChatBots.RemoteControl()); }
}
@ -120,7 +120,7 @@ namespace MinecraftClient
if (singlecommand)
{
handler.SendChatMessage(command);
ConsoleIO.WriteLineFormatted("§7Command §8" + command + "§7 sent.", false);
ConsoleIO.WriteLineFormatted("§7Command §8" + command + "§7 sent.");
Thread.Sleep(5000);
handler.Disconnect();
Thread.Sleep(1000);
@ -276,12 +276,12 @@ namespace MinecraftClient
case ChatBot.DisconnectReason.InGameKick:
ConsoleIO.WriteLine("Disconnected by Server :");
ConsoleIO.WriteLineFormatted(message, true);
ConsoleIO.WriteLineFormatted(message);
break;
case ChatBot.DisconnectReason.LoginRejected:
ConsoleIO.WriteLine("Login failed :");
ConsoleIO.WriteLineFormatted(message, true);
ConsoleIO.WriteLineFormatted(message);
break;
}
@ -298,7 +298,16 @@ namespace MinecraftClient
public void OnUpdate()
{
for (int i = 0; i < bots.Count; i++)
bots[i].Update();
{
try
{
bots[i].Update();
}
catch (Exception e)
{
ConsoleIO.WriteLineFormatted("§8Got error from " + bots[i].ToString() + ": " + e.ToString());
}
}
}
/// <summary>

View file

@ -72,7 +72,8 @@ namespace MinecraftClient
if (Settings.ConsoleTitle != "")
{
Console.Title = Settings.ConsoleTitle.Replace("%username%", "New Window");
Settings.Username = "New Window";
Console.Title = Settings.replaceVars(Settings.ConsoleTitle);
}
//Asking the user to type in missing data such as Username and Password
@ -112,7 +113,7 @@ namespace MinecraftClient
if (Settings.Password == "-")
{
ConsoleIO.WriteLineFormatted("§8You chose to run in offline mode.", false);
ConsoleIO.WriteLineFormatted("§8You chose to run in offline mode.");
result = ProtocolHandler.LoginResult.Success;
sessionID = "0";
}
@ -126,7 +127,7 @@ namespace MinecraftClient
{
if (Settings.ConsoleTitle != "")
{
Console.Title = Settings.ConsoleTitle.Replace("%username%", Settings.Username);
Console.Title = Settings.replaceVars(Settings.ConsoleTitle);
}
Console.WriteLine("Success. (session ID: " + sessionID + ')');
@ -183,7 +184,7 @@ namespace MinecraftClient
{
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", true);
+ '\n' + "mozroots --import --ask-remove");
return;
}
break;

View file

@ -128,7 +128,7 @@ namespace MinecraftClient.Protocol.Handlers
&& System.IO.File.Exists(Settings.TranslationsFile_FromMCDir))
{
Language_File = Settings.TranslationsFile_FromMCDir;
ConsoleIO.WriteLineFormatted("§8Defaulting to en_GB.lang from your Minecraft directory.", false);
ConsoleIO.WriteLineFormatted("§8Defaulting to en_GB.lang from your Minecraft directory.");
}
//Load the external dictionnary of translation rules or display an error message
@ -147,12 +147,12 @@ namespace MinecraftClient.Protocol.Handlers
}
}
ConsoleIO.WriteLineFormatted("§8Translations file loaded.", false);
ConsoleIO.WriteLineFormatted("§8Translations file loaded.");
}
else //No external dictionnary found.
{
ConsoleIO.WriteLineFormatted("§8Translations file not found: \"" + Language_File + "\""
+ "\nSome messages won't be properly printed without this file.", true);
+ "\nSome messages won't be properly printed without this file.");
}
}

View file

@ -434,12 +434,12 @@ namespace MinecraftClient.Protocol.Handlers
if (serverID == "-")
{
ConsoleIO.WriteLineFormatted("§8Server is in offline mode.", false);
ConsoleIO.WriteLineFormatted("§8Server is in offline mode.");
return true; //No need to check session or start encryption
}
else
{
ConsoleIO.WriteLineFormatted("§8Handshake successful. (Server ID: " + serverID + ')', false);
ConsoleIO.WriteLineFormatted("§8Handshake successful. (Server ID: " + serverID + ')');
return StartEncryption(uuid, username, sessionID, token, serverID, PublicServerkey);
}
}
@ -451,7 +451,7 @@ namespace MinecraftClient.Protocol.Handlers
System.Security.Cryptography.RSACryptoServiceProvider RSAService = CryptoHandler.DecodeRSAPublicKey(serverKey);
byte[] secretKey = CryptoHandler.GenerateAESPrivateKey();
ConsoleIO.WriteLineFormatted("§8Crypto keys & hash generated.", false);
ConsoleIO.WriteLineFormatted("§8Crypto keys & hash generated.");
if (serverIDhash != "-")
{
@ -675,7 +675,7 @@ namespace MinecraftClient.Protocol.Handlers
protocolversion = (byte)39;
version = "B1.8.1 - 1.3.2";
}
ConsoleIO.WriteLineFormatted("§8Server version : MC " + version + " (protocol v" + protocolversion + ").", false);
ConsoleIO.WriteLineFormatted("§8Server version : MC " + version + " (protocol v" + protocolversion + ").");
return true;
}
else return false;

View file

@ -328,7 +328,7 @@ namespace MinecraftClient.Protocol.Handlers
}
else if (pid == 0x02) //Login successful
{
ConsoleIO.WriteLineFormatted("§8Server is in offline mode.", false);
ConsoleIO.WriteLineFormatted("§8Server is in offline mode.");
StartUpdating();
return true; //No need to check session or start encryption
}
@ -345,7 +345,7 @@ namespace MinecraftClient.Protocol.Handlers
System.Security.Cryptography.RSACryptoServiceProvider RSAService = CryptoHandler.DecodeRSAPublicKey(serverKey);
byte[] secretKey = CryptoHandler.GenerateAESPrivateKey();
ConsoleIO.WriteLineFormatted("§8Crypto keys & hash generated.", false);
ConsoleIO.WriteLineFormatted("§8Crypto keys & hash generated.");
if (serverIDhash != "-")
{
@ -538,7 +538,7 @@ namespace MinecraftClient.Protocol.Handlers
version = "Forge " + version;
protocolversion = 0;
}
ConsoleIO.WriteLineFormatted("§8Server version : " + version + " (protocol v" + protocolversion + ").", false);
ConsoleIO.WriteLineFormatted("§8Server version : " + version + " (protocol v" + protocolversion + ").");
return true;
}
}

View file

@ -54,13 +54,13 @@ namespace MinecraftClient.Protocol
}
else
{
ConsoleIO.WriteLineFormatted("§8Unexpected answer from the server (is that a Minecraft server ?)", false);
ConsoleIO.WriteLineFormatted("§8Unexpected answer from the server (is that a Minecraft server ?)");
return false;
}
}
catch
{
ConsoleIO.WriteLineFormatted("§8An error occured while attempting to connect to this IP.", false);
ConsoleIO.WriteLineFormatted("§8An error occured while attempting to connect to this IP.");
return false;
}
}
@ -133,7 +133,7 @@ namespace MinecraftClient.Protocol
}
else
{
ConsoleIO.WriteLineFormatted("§8Got error code from server: " + code, false);
ConsoleIO.WriteLineFormatted("§8Got error code from server: " + code);
return LoginResult.OtherError;
}
}

View file

@ -49,7 +49,7 @@ namespace MinecraftClient.Proxy
if (!proxy_ok)
{
ConsoleIO.WriteLineFormatted("§8Connected to proxy " + Settings.ProxyHost + ':' + Settings.ProxyPort, false);
ConsoleIO.WriteLineFormatted("§8Connected to proxy " + Settings.ProxyHost + ':' + Settings.ProxyPort);
proxy_ok = true;
}
@ -59,7 +59,7 @@ namespace MinecraftClient.Proxy
}
catch (ProxyException e)
{
ConsoleIO.WriteLineFormatted("§8" + e.Message, false);
ConsoleIO.WriteLineFormatted("§8" + e.Message);
proxy = null;
return null;
}

View file

@ -81,7 +81,10 @@ namespace MinecraftClient
//Remote Control
public static bool RemoteCtrl_Enabled = false;
private enum ParseMode { Default, Main, Proxy, AntiAFK, Hangman, Alerts, ChatLog, AutoRelog, ScriptScheduler, RemoteControl };
//App variables
private static Dictionary<string, string> AppVars = new Dictionary<string, string>();
private enum ParseMode { Default, Main, AppVars, Proxy, AntiAFK, Hangman, Alerts, ChatLog, AutoRelog, ScriptScheduler, RemoteControl };
/// <summary>
/// Load settings from the give INI file
@ -225,6 +228,12 @@ namespace MinecraftClient
case "password": ProxyPassword = argValue; break;
}
break;
case ParseMode.AppVars:
string varName = new string(argName.TakeWhile(char.IsLetterOrDigit).ToArray()).ToLower();
if (varName.Length > 0)
AppVars[varName] = argValue;
break;
}
}
}
@ -263,6 +272,11 @@ namespace MinecraftClient
+ "exitonfailure=false\r\n"
+ "timestamps=false\r\n"
+ "\r\n"
+ "[AppVars]\r\n"
+ "#yourvar=yourvalue\r\n"
+ "#can be used in other fields as %yourvar%\r\n"
+ "#%username% and %serverip% are reserved variable names.\r\n"
+ "\r\n"
+ "[Proxy]\r\n"
+ "enabled=false\r\n"
+ "type=HTTP #Supported types: HTTP, SOCKS4, SOCKS4a, SOCKS5\r\n"
@ -294,7 +308,7 @@ namespace MinecraftClient
+ "enabled=false\r\n"
+ "timestamps=true\r\n"
+ "filter=messages\r\n"
+ "logfile=chatlog.txt\r\n"
+ "logfile=chatlog-%username%-%serverip%.txt\r\n"
+ "\r\n"
+ "[Hangman]\r\n"
+ "enabled=false\r\n"
@ -313,5 +327,62 @@ namespace MinecraftClient
public static int str2int(string str) { try { return Convert.ToInt32(str); } catch { return 0; } }
public static bool str2bool(string str) { return str == "true" || str == "1"; }
/// <summary>
/// Replace %variables% with their value
/// </summary>
/// <param name="str">String to parse</param>
/// <returns>Modifier string</returns>
public static string replaceVars(string str)
{
StringBuilder result = new StringBuilder();
for (int i = 0; i < str.Length; i++)
{
if (str[i] == '%')
{
bool varname_ok = true;
StringBuilder var_name = new StringBuilder();
for (int j = i + 1; j < str.Length; j++)
{
if (!char.IsLetterOrDigit(str[j]))
{
if (str[j] == '%')
{
varname_ok = var_name.Length > 0;
break;
}
else
{
varname_ok = false;
break;
}
}
else var_name.Append(str[j]);
}
if (varname_ok)
{
string varname = var_name.ToString();
string varname_lower = varname.ToLower();
i = i + varname.Length + 1;
if (varname_lower == "username")
{
result.Append(Username);
}
else if (varname_lower == "serverip")
{
result.Append(ServerIP.Split(':')[0]);
}
else if (AppVars.ContainsKey(varname_lower))
{
result.Append(AppVars[varname_lower]);
}
else result.Append("%" + varname + '%');
}
else result.Append(str[i]);
}
else result.Append(str[i]);
}
return result.ToString();
}
}
}