From 068b87a11a9ed3965fa235d4c594146d35d8abc1 Mon Sep 17 00:00:00 2001 From: ORelio Date: Wed, 11 Jun 2014 20:40:25 +0200 Subject: [PATCH] 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 --- MinecraftClient/ChatBot.cs | 2 +- MinecraftClient/ConsoleIO.cs | 2 +- MinecraftClient/McTcpClient.cs | 23 ++++-- MinecraftClient/Program.cs | 9 ++- .../Protocol/Handlers/ChatParser.cs | 6 +- .../Protocol/Handlers/Protocol16.cs | 8 +- .../Protocol/Handlers/Protocol17.cs | 6 +- MinecraftClient/Protocol/ProtocolHandler.cs | 6 +- MinecraftClient/Proxy/ProxyHandler.cs | 4 +- MinecraftClient/Settings.cs | 75 ++++++++++++++++++- 10 files changed, 111 insertions(+), 30 deletions(-) diff --git a/MinecraftClient/ChatBot.cs b/MinecraftClient/ChatBot.cs index 1bf14a4a..6d8c3bb2 100644 --- a/MinecraftClient/ChatBot.cs +++ b/MinecraftClient/ChatBot.cs @@ -209,7 +209,7 @@ namespace MinecraftClient public static void LogToConsole(string text) { - ConsoleIO.WriteLineFormatted("§8[BOT] " + text, true); + ConsoleIO.WriteLineFormatted("§8[BOT] " + text); } /// diff --git a/MinecraftClient/ConsoleIO.cs b/MinecraftClient/ConsoleIO.cs index 6f08abcb..ac2ea481 100644 --- a/MinecraftClient/ConsoleIO.cs +++ b/MinecraftClient/ConsoleIO.cs @@ -249,7 +249,7 @@ namespace MinecraftClient /// String to write /// If false, space are printed instead of newlines - 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)) diff --git a/MinecraftClient/McTcpClient.cs b/MinecraftClient/McTcpClient.cs index 042cfe43..88b0fbd5 100644 --- a/MinecraftClient/McTcpClient.cs +++ b/MinecraftClient/McTcpClient.cs @@ -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()); + } + } } /// diff --git a/MinecraftClient/Program.cs b/MinecraftClient/Program.cs index 943cdd93..95a2760f 100644 --- a/MinecraftClient/Program.cs +++ b/MinecraftClient/Program.cs @@ -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; diff --git a/MinecraftClient/Protocol/Handlers/ChatParser.cs b/MinecraftClient/Protocol/Handlers/ChatParser.cs index 53664712..e6694528 100644 --- a/MinecraftClient/Protocol/Handlers/ChatParser.cs +++ b/MinecraftClient/Protocol/Handlers/ChatParser.cs @@ -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."); } } diff --git a/MinecraftClient/Protocol/Handlers/Protocol16.cs b/MinecraftClient/Protocol/Handlers/Protocol16.cs index 11cd9c25..1dc83fed 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol16.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol16.cs @@ -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; diff --git a/MinecraftClient/Protocol/Handlers/Protocol17.cs b/MinecraftClient/Protocol/Handlers/Protocol17.cs index 4c4b353f..a0eb33ab 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol17.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol17.cs @@ -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; } } diff --git a/MinecraftClient/Protocol/ProtocolHandler.cs b/MinecraftClient/Protocol/ProtocolHandler.cs index b81accaa..b715fda8 100644 --- a/MinecraftClient/Protocol/ProtocolHandler.cs +++ b/MinecraftClient/Protocol/ProtocolHandler.cs @@ -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; } } diff --git a/MinecraftClient/Proxy/ProxyHandler.cs b/MinecraftClient/Proxy/ProxyHandler.cs index 1fdba219..6ac63582 100644 --- a/MinecraftClient/Proxy/ProxyHandler.cs +++ b/MinecraftClient/Proxy/ProxyHandler.cs @@ -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; } diff --git a/MinecraftClient/Settings.cs b/MinecraftClient/Settings.cs index 3cbd5de2..41aeb198 100644 --- a/MinecraftClient/Settings.cs +++ b/MinecraftClient/Settings.cs @@ -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 AppVars = new Dictionary(); + + private enum ParseMode { Default, Main, AppVars, Proxy, AntiAFK, Hangman, Alerts, ChatLog, AutoRelog, ScriptScheduler, RemoteControl }; /// /// 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"; } + /// + /// Replace %variables% with their value + /// + /// String to parse + /// Modifier string + + 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(); + } } }