Added Settings class & Settings file

- All settings are moved in a new Settings class
- Settings can be loaded by parsing an INI file
- A default INI file is generated with default settings
- By default, loads MinecraftClient.ini if no command-line arguments are
used
- Another INI file can be loaded with MinecraftClient.exe MyFile.ini
- All the config files can be renamed or moved, just edit the INI file
- A title for the console window can be specified in the INI file
- Regular command-line arguments still works but will probably be
simplified in the future.
- Smal code optimizations and adjustments
This commit is contained in:
ORelio 2013-08-06 16:11:46 +02:00
parent 88105d30ad
commit df4a9cd7b7
6 changed files with 373 additions and 142 deletions

View file

@ -483,14 +483,14 @@ namespace MinecraftClient
private string chooseword() private string chooseword()
{ {
if (System.IO.File.Exists(English ? "config/hangman-words.txt" : "config/pendu-mots.txt")) if (System.IO.File.Exists(English ? Settings.Hangman_FileWords_EN : Settings.Hangman_FileWords_FR))
{ {
string[] dico = System.IO.File.ReadAllLines(English ? "words.txt" : "mots.txt"); string[] dico = System.IO.File.ReadAllLines(English ? Settings.Hangman_FileWords_EN : Settings.Hangman_FileWords_FR);
return dico[new Random().Next(dico.Length)]; return dico[new Random().Next(dico.Length)];
} }
else else
{ {
LogToConsole(English ? "Cannot find words.txt in config directory !" : "Fichier mots.txt introuvable dans config/hangman dossier!"); LogToConsole(English ? "File not found: " + Settings.Hangman_FileWords_EN : "Fichier introuvable : " + Settings.Hangman_FileWords_FR);
return English ? "WORDSAREMISSING" : "DICOMANQUANT"; return English ? "WORDSAREMISSING" : "DICOMANQUANT";
} }
} }
@ -499,14 +499,14 @@ namespace MinecraftClient
{ {
List<string> owners = new List<string>(); List<string> owners = new List<string>();
owners.Add("CONSOLE"); owners.Add("CONSOLE");
if (System.IO.File.Exists("config/bot-owners.txt")) if (System.IO.File.Exists(Settings.Bots_OwnersFile))
{ {
foreach (string s in System.IO.File.ReadAllLines("config/bot-owners.txt")) foreach (string s in System.IO.File.ReadAllLines(Settings.Bots_OwnersFile))
{ {
owners.Add(s.ToUpper()); owners.Add(s.ToUpper());
} }
} }
else LogToConsole(English ? "Cannot find bot-owners.txt in config folder!" : "Fichier bot-owners.txt introuvable dans config!"); else LogToConsole(English ? "File not found: " + Settings.Bots_OwnersFile : "Fichier introuvable : " + Settings.Bots_OwnersFile);
return owners.ToArray(); return owners.ToArray();
} }
@ -554,27 +554,27 @@ namespace MinecraftClient
public override void Initialize() public override void Initialize()
{ {
if (System.IO.File.Exists("config/alerts.txt")) if (System.IO.File.Exists(Settings.Alerts_MatchesFile))
{ {
dictionary = System.IO.File.ReadAllLines("config/alerts.txt"); dictionary = System.IO.File.ReadAllLines(Settings.Alerts_MatchesFile);
for (int i = 0; i < dictionary.Length; i++) for (int i = 0; i < dictionary.Length; i++)
{ {
dictionary[i] = dictionary[i].ToLower(); dictionary[i] = dictionary[i].ToLower();
} }
} }
else LogToConsole("Cannot find alerts.txt in the config folder!"); else LogToConsole("File not found: " + Settings.Alerts_MatchesFile);
if (System.IO.File.Exists("config/alerts-exclude.txt")) if (System.IO.File.Exists(Settings.Alerts_ExcludesFile))
{ {
excludelist = System.IO.File.ReadAllLines("config/alerts-exclude.txt"); excludelist = System.IO.File.ReadAllLines(Settings.Alerts_ExcludesFile);
for (int i = 0; i < excludelist.Length; i++) for (int i = 0; i < excludelist.Length; i++)
{ {
excludelist[i] = excludelist[i].ToLower(); excludelist[i] = excludelist[i].ToLower();
} }
} }
else LogToConsole("Cannot find alerts-exclude.txt in the config folder!"); else LogToConsole("File not found : " + Settings.Alerts_ExcludesFile);
} }
public override void GetText(string text) public override void GetText(string text)
@ -708,6 +708,18 @@ namespace MinecraftClient
} }
} }
public static MessageFilter str2filter(string filtername)
{
switch (filtername)
{
case "all": return MessageFilter.AllText;
case "messages": return MessageFilter.AllMessages;
case "chat": return MessageFilter.OnlyChat;
case "private": return MessageFilter.OnlyWhispers;
default: return MessageFilter.AllText;
}
}
public override void GetText(string text) public override void GetText(string text)
{ {
text = getVerbatim(text); text = getVerbatim(text);
@ -783,16 +795,16 @@ namespace MinecraftClient
public override void Initialize() public override void Initialize()
{ {
McTcpClient.AttemptsLeft = attempts; McTcpClient.AttemptsLeft = attempts;
if (System.IO.File.Exists("config/kickmessages.txt")) if (System.IO.File.Exists(Settings.AutoRelog_KickMessagesFile))
{ {
dictionary = System.IO.File.ReadAllLines("config/kickmessages.txt"); dictionary = System.IO.File.ReadAllLines(Settings.AutoRelog_KickMessagesFile);
for (int i = 0; i < dictionary.Length; i++) for (int i = 0; i < dictionary.Length; i++)
{ {
dictionary[i] = dictionary[i].ToLower(); dictionary[i] = dictionary[i].ToLower();
} }
} }
else LogToConsole("Cannot find kickmessages.txt in the config directory!"); else LogToConsole("File not found: " + Settings.AutoRelog_KickMessagesFile);
} }
public override bool OnDisconnect(DisconnectReason reason, string message) public override bool OnDisconnect(DisconnectReason reason, string message)
@ -860,14 +872,13 @@ namespace MinecraftClient
public override void Initialize() public override void Initialize()
{ {
// Loads the given file from the startup parameters // Loads the given file from the startup parameters
if (System.IO.File.Exists("config/" + file)) if (System.IO.File.Exists(file))
{ {
LogToConsole("Loading script: \"" + file + "\""); lines = System.IO.File.ReadAllLines(file); // Load the given bot text file (containing commands)
lines = System.IO.File.ReadAllLines("config/" + file); // Load the given bot text file (containing commands)
} }
else else
{ {
LogToConsole("File \"" + file + "\" not found in the config directory!"); LogToConsole("File not found: " + file);
UnloadBot(); //No need to keep the bot active UnloadBot(); //No need to keep the bot active
} }
} }
@ -885,11 +896,7 @@ namespace MinecraftClient
if (instruction_line.Length > 0) if (instruction_line.Length > 0)
{ {
if (instruction_line.Trim().StartsWith("//")) if (!instruction_line.StartsWith("//") && !instruction_line.StartsWith("#"))
{
LogToConsole(instruction_line); //Ignore comments but write them to the console
}
else
{ {
string instruction_name = instruction_line.Split(' ')[0]; string instruction_name = instruction_line.Split(' ')[0];
switch (instruction_name.ToLower()) switch (instruction_name.ToLower())
@ -904,7 +911,6 @@ namespace MinecraftClient
ticks = Convert.ToInt32(instruction_line.Substring(5, instruction_line.Length - 5)); ticks = Convert.ToInt32(instruction_line.Substring(5, instruction_line.Length - 5));
} }
catch { } catch { }
LogToConsole("Waiting " + ticks / 10 + " seconds...");
sleepticks = ticks; sleepticks = ticks;
break; break;
case "disconnect": case "disconnect":
@ -913,8 +919,12 @@ namespace MinecraftClient
case "exit": //Exit bot & stay connected to the server case "exit": //Exit bot & stay connected to the server
UnloadBot(); UnloadBot();
break; break;
default:
sleepticks = 0; Update(); //Unknown command : process next line immediately
break;
} }
} }
else { sleepticks = 0; Update(); } //Comment: process next line immediately
} }
} }
else else

View file

@ -96,9 +96,9 @@ namespace MinecraftClient
TranslationRules["commands.message.display.outgoing"] = "§7You whisper to %s: %s"; TranslationRules["commands.message.display.outgoing"] = "§7You whisper to %s: %s";
//Load an external dictionnary of translation rules //Load an external dictionnary of translation rules
if (System.IO.File.Exists("translations.lang")) if (System.IO.File.Exists(Settings.TranslationsFile))
{ {
string[] translations = System.IO.File.ReadAllLines("translations.lang"); string[] translations = System.IO.File.ReadAllLines(Settings.TranslationsFile);
foreach (string line in translations) foreach (string line in translations)
{ {
if (line.Length > 0) if (line.Length > 0)
@ -118,9 +118,9 @@ namespace MinecraftClient
else //No external dictionnary found. else //No external dictionnary found.
{ {
Console.ForegroundColor = ConsoleColor.DarkGray; Console.ForegroundColor = ConsoleColor.DarkGray;
ConsoleIO.WriteLine("MC 1.6+ warning: Translations file \"translations.lang\" not found." ConsoleIO.WriteLine("MC 1.6+ warning: Translations file \"" + Settings.TranslationsFile + "\" not found."
+ "\nYou can pick a translation file from .minecraft\\assets\\lang\\" + "\nYou can pick a translation file from .minecraft\\assets\\lang\\"
+ "\nCopy to the same folder as MinecraftClient & rename to \"translations.lang\"" + "\nCopy to the same folder as MinecraftClient & rename to \"" + Settings.TranslationsFile + "\""
+ "\nSome messages won't be properly printed without this file."); + "\nSome messages won't be properly printed without this file.");
Console.ForegroundColor = ConsoleColor.Gray; Console.ForegroundColor = ConsoleColor.Gray;
} }

View file

@ -93,6 +93,7 @@
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="McTcpClient.cs" /> <Compile Include="McTcpClient.cs" />
<Compile Include="Settings.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.0,Profile=Client"> <BootstrapperPackage Include=".NETFramework,Version=v4.0,Profile=Client">

View file

@ -813,7 +813,7 @@ namespace MinecraftClient
} }
private List<ChatBot> bots = new List<ChatBot>(); private List<ChatBot> bots = new List<ChatBot>();
public void BotLoad(ChatBot b) { b.SetHandler(this); bots.Add(b); b.Initialize(); } public void BotLoad(ChatBot b) { b.SetHandler(this); bots.Add(b); b.Initialize(); Settings.SingleCommand = ""; }
public void BotUnLoad(ChatBot b) { bots.RemoveAll(item => object.ReferenceEquals(item, b)); } public void BotUnLoad(ChatBot b) { bots.RemoveAll(item => object.ReferenceEquals(item, b)); }
public void BotClear() { bots.Clear(); } public void BotClear() { bots.Clear(); }
} }

View file

@ -14,12 +14,8 @@ namespace MinecraftClient
class Program class Program
{ {
private static McTcpClient Client; private static McTcpClient Client;
private static string loginusername = ""; public static string[] startupargs;
private static string user = ""; public const string Version = "1.6.0";
private static string pass = "";
private static string ip = "";
private static string command = "";
private static string[] startupargs;
/// <summary> /// <summary>
/// The main entry point of Minecraft Console Client /// The main entry point of Minecraft Console Client
@ -27,38 +23,134 @@ namespace MinecraftClient
static void Main(string[] args) static void Main(string[] args)
{ {
Console.WriteLine("Console Client for MC 1.4.6 to 1.6.2 - v1.5.2 - By ORelio (or3L1o@live.fr)"); Console.WriteLine("Console Client for MC 1.4.6 to 1.6.2 - v" + Version + " - By ORelio (or3L1o@live.fr)");
//Processing Command-line arguments //Processing Command-line arguments or Config File
if (args.Length >= 1) if (args.Length == 1 && System.IO.File.Exists(args[0]))
{ {
user = args[0]; Settings.LoadSettings(args[0]);
}
else if (args.Length >= 1)
{
Settings.Login = args[0];
if (args.Length >= 2) if (args.Length >= 2)
{ {
pass = args[1]; Settings.Password = args[1];
if (args.Length >= 3) if (args.Length >= 3)
{ {
ip = args[2]; Settings.ServerIP = args[2];
//Single command?
if (args.Length >= 4) if (args.Length >= 4)
{ {
command = args[3]; Settings.SingleCommand = args[3];
}
//Use bots? (will disable single command)
for (int i = 3; i < args.Length; i++)
{
if (args[i].Length > 4 && args[i].Substring(0, 4).ToLower() == "bot:")
{
Settings.SingleCommand = "";
string[] botargs = args[i].ToLower().Split(':');
switch (botargs[1])
{
#region Process bots settings
case "antiafk":
Settings.AntiAFK_Enabled = true;
if (botargs.Length > 2)
{
try { Settings.AntiAFK_Delay = Convert.ToInt32(botargs[2]); }
catch (FormatException) { }
} break;
case "pendu":
Settings.Hangman_Enabled = true;
Settings.Hangman_English = false;
break;
case "hangman":
Settings.Hangman_Enabled = true;
Settings.Hangman_English = true;
break;
case "alerts":
Settings.Alerts_Enabled = true;
break;
case "log":
Settings.ChatLog_Enabled = true;
Settings.ChatLog_DateTime = true;
Settings.ChatLog_File = "chat-" + Settings.ServerIP.Replace(':', '-') + ".log";
if (botargs.Length > 2)
{
Settings.ChatLog_DateTime = Settings.str2bool(botargs[2]);
if (botargs.Length > 3)
{
Settings.ChatLog_Filter = Bots.ChatLog.str2filter(botargs[3]);
if (botargs.Length > 4 && botargs[4] != "") { Settings.ChatLog_File = botargs[4]; }
}
} break;
case "logplayerlist":
Settings.PlayerLog_File = "connected-" + Settings.ServerIP.Replace(':', '-') + ".log";
if (botargs.Length > 2)
{
try { Settings.PlayerLog_Delay = Convert.ToInt32(botargs[2]); }
catch (FormatException) { }
} break;
case "autorelog":
if (botargs.Length > 2)
{
try { Settings.AutoRelog_Delay = Convert.ToInt32(botargs[2]); }
catch (FormatException) { }
if (botargs.Length > 3)
{
try { Settings.AutoRelog_Retries = Convert.ToInt32(botargs[3]); }
catch (FormatException) { }
}
} break;
case "xauth":
if (botargs.Length > 2)
{
Settings.xAuth_Enabled = true;
Settings.xAuth_Password = botargs[2];
} break;
case "scripting":
if (botargs.Length > 2)
{
Settings.Scripting_Enabled = true;
Settings.Scripting_ScriptFile = botargs[2];
} break;
#endregion
} }
} }
} }
} }
}
}
else if (System.IO.File.Exists("MinecraftClient.ini"))
{
Settings.LoadSettings("MinecraftClient.ini");
}
else Settings.WriteDefaultSettings("MinecraftClient.ini");
//Asking the user to type in missing data such as Username and Password //Asking the user to type in missing data such as Username and Password
if (user == "") if (Settings.Login == "")
{ {
Console.Write("Username : "); Console.Write("Username : ");
user = Console.ReadLine(); Settings.Login = Console.ReadLine();
} }
if (pass == "") if (Settings.Password == "")
{ {
Console.Write("Password : "); Console.Write("Password : ");
pass = Console.ReadLine(); Settings.Password = Console.ReadLine();
//Hide the password //Hide the password
Console.CursorTop--; Console.CursorTop--;
@ -66,11 +158,7 @@ namespace MinecraftClient
for (int i = 19; i < Console.BufferWidth; i++) { Console.Write(' '); } for (int i = 19; i < Console.BufferWidth; i++) { Console.Write(' '); }
} }
//Save the arguments
startupargs = args; startupargs = args;
loginusername = user;
//Start the Client
InitializeClient(); InitializeClient();
} }
@ -84,34 +172,34 @@ namespace MinecraftClient
MinecraftCom.LoginResult result; MinecraftCom.LoginResult result;
string logindata = ""; string logindata = "";
if (pass == "-") if (Settings.Password == "-")
{ {
Console.ForegroundColor = ConsoleColor.DarkGray; Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("You chose to run in offline mode."); Console.WriteLine("You chose to run in offline mode.");
Console.ForegroundColor = ConsoleColor.Gray; Console.ForegroundColor = ConsoleColor.Gray;
result = MinecraftCom.LoginResult.Success; result = MinecraftCom.LoginResult.Success;
logindata = "0:deprecated:" + user + ":0"; logindata = "0:deprecated:" + Settings.Login + ":0";
} }
else else
{ {
Console.WriteLine("Connecting to Minecraft.net..."); Console.WriteLine("Connecting to Minecraft.net...");
result = MinecraftCom.GetLogin(loginusername, pass, ref logindata); result = MinecraftCom.GetLogin(Settings.Login, Settings.Password, ref logindata);
} }
if (result == MinecraftCom.LoginResult.Success) if (result == MinecraftCom.LoginResult.Success)
{ {
user = logindata.Split(':')[2]; Settings.Username = logindata.Split(':')[2];
string sessionID = logindata.Split(':')[3]; string sessionID = logindata.Split(':')[3];
Console.WriteLine("Success. (session ID: " + sessionID + ')'); Console.WriteLine("Success. (session ID: " + sessionID + ')');
if (ip == "") if (Settings.ServerIP == "")
{ {
Console.Write("Server IP : "); Console.Write("Server IP : ");
ip = Console.ReadLine(); Settings.ServerIP = Console.ReadLine();
} }
//Get server version //Get server version
Console.WriteLine("Retrieving Server Info..."); Console.WriteLine("Retrieving Server Info...");
byte protocolversion = 0; string version = ""; byte protocolversion = 0; string version = "";
if (MinecraftCom.GetServerInfo(ip, ref protocolversion, ref version)) if (MinecraftCom.GetServerInfo(Settings.ServerIP, ref protocolversion, ref version))
{ {
//Supported protocol version ? //Supported protocol version ?
int[] supportedVersions = { 51, 60, 61, 72, 73, 74 }; int[] supportedVersions = { 51, 60, 61, 72, 73, 74 };
@ -127,92 +215,21 @@ namespace MinecraftClient
handler.setVersion(protocolversion); handler.setVersion(protocolversion);
//Load & initialize bots if needed //Load & initialize bots if needed
foreach (string arg in startupargs) if (Settings.AntiAFK_Enabled) { handler.BotLoad(new Bots.AntiAFK(Settings.AntiAFK_Delay)); }
{ if (Settings.Hangman_Enabled) { handler.BotLoad(new Bots.Pendu(Settings.Hangman_English)); }
if (arg.Length > 4 && arg.Substring(0, 4).ToLower() == "bot:") if (Settings.Alerts_Enabled) { handler.BotLoad(new Bots.Alerts()); }
{ if (Settings.ChatLog_Enabled) { handler.BotLoad(new Bots.ChatLog(Settings.ChatLog_File, Settings.ChatLog_Filter, Settings.ChatLog_DateTime)); }
int param; if (Settings.PlayerLog_Enabled) { handler.BotLoad(new Bots.PlayerListLogger(Settings.PlayerLog_Delay, Settings.PlayerLog_File)); }
string[] botargs = arg.ToLower().Split(':'); if (Settings.AutoRelog_Enabled) { handler.BotLoad(new Bots.AutoRelog(Settings.AutoRelog_Delay, Settings.AutoRelog_Retries)); }
switch (botargs[1]) if (Settings.xAuth_Enabled) { handler.BotLoad(new Bots.xAuth(Settings.xAuth_Password)); }
{ if (Settings.Scripting_Enabled) { handler.BotLoad(new Bots.Scripting(Settings.Scripting_ScriptFile)); }
case "antiafk":
#region Arguments for the AntiAFK bot
param = 600;
if (botargs.Length > 2)
{
try { param = Convert.ToInt32(botargs[2]); }
catch (FormatException) { }
}
#endregion
handler.BotLoad(new Bots.AntiAFK(param)); break;
case "pendu": handler.BotLoad(new Bots.Pendu(false)); break;
case "hangman": handler.BotLoad(new Bots.Pendu(true)); break;
case "alerts": handler.BotLoad(new Bots.Alerts()); break;
case "log":
#region Arguments for the ChatLog bot
bool datetime = true;
string file = "chat-" + ip + ".log";
Bots.ChatLog.MessageFilter filter = Bots.ChatLog.MessageFilter.AllMessages;
if (botargs.Length > 2)
{
datetime = (botargs[2] != "0");
if (botargs.Length > 3)
{
switch (botargs[3])
{
case "all": filter = Bots.ChatLog.MessageFilter.AllText; break;
case "messages": filter = Bots.ChatLog.MessageFilter.AllMessages; break;
case "chat": filter = Bots.ChatLog.MessageFilter.OnlyChat; break;
case "private": filter = Bots.ChatLog.MessageFilter.OnlyWhispers; break;
}
if (botargs.Length > 4 && botargs[4] != "") { file = botargs[4]; }
}
}
#endregion
handler.BotLoad(new Bots.ChatLog(file, filter, datetime)); break;
case "logplayerlist":
#region Arguments for the PlayerListLogger bot
param = 600;
if (botargs.Length > 2)
{
try { param = Convert.ToInt32(botargs[2]); }
catch (FormatException) { }
}
#endregion
handler.BotLoad(new Bots.PlayerListLogger(param, "connected-" + ip + ".log")); break;
case "autorelog":
#region Arguments for the AutoRelog bot
int delay = 10;
if (botargs.Length > 2)
{
try { delay = Convert.ToInt32(botargs[2]); }
catch (FormatException) { }
}
int retries = 3;
if (botargs.Length > 3)
{
try { retries = Convert.ToInt32(botargs[3]); }
catch (FormatException) { }
}
#endregion
handler.BotLoad(new Bots.AutoRelog(delay, retries)); break;
case "xauth": if (botargs.Length > 2) { handler.BotLoad(new Bots.xAuth(botargs[2])); } break;
case "scripting": if (botargs.Length > 2) { handler.BotLoad(new Bots.Scripting(botargs[2])); } break;
}
command = "";
}
}
//Start the main TCP client //Start the main TCP client
if (command != "") if (Settings.SingleCommand != "")
{ {
Client = new McTcpClient(user, sessionID, ip, handler, command); Client = new McTcpClient(Settings.Username, sessionID, Settings.ServerIP, handler, Settings.SingleCommand);
} }
else Client = new McTcpClient(user, sessionID, ip, handler); else Client = new McTcpClient(Settings.Username, sessionID, Settings.ServerIP, handler);
} }
else else
{ {
@ -239,7 +256,7 @@ namespace MinecraftClient
case MinecraftCom.LoginResult.Error: Console.WriteLine("Network error."); break; case MinecraftCom.LoginResult.Error: Console.WriteLine("Network error."); break;
} }
while (Console.KeyAvailable) { Console.ReadKey(false); } while (Console.KeyAvailable) { Console.ReadKey(false); }
if (command == "") { ReadLineReconnect(); } if (Settings.SingleCommand == "") { ReadLineReconnect(); }
} }
} }

203
MinecraftClient/Settings.cs Normal file
View file

@ -0,0 +1,203 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace MinecraftClient
{
/// <summary>
/// Contains main settings for Minecraft Console Client
/// Allows settings loading from an INI file
/// </summary>
public static class Settings
{
//Main Settings.
//Login: Username or email adress used as login for Minecraft/Mojang account
//Username: The actual username of the user, obtained after login to the account
public static string Login = "";
public static string Username = "";
public static string Password = "";
public static string ServerIP = "";
public static string SingleCommand = "";
//Other Settings
public static string TranslationsFile = "translations.lang";
public static string Bots_OwnersFile = "bot-owners.txt";
//AntiAFK Settings
public static bool AntiAFK_Enabled = false;
public static int AntiAFK_Delay = 600;
//Hangman Settings
public static bool Hangman_Enabled = false;
public static bool Hangman_English = true;
public static string Hangman_FileWords_EN = "words.txt";
public static string Hangman_FileWords_FR = "mots.txt";
//Alerts Settings
public static bool Alerts_Enabled = false;
public static string Alerts_MatchesFile = "alerts.txt";
public static string Alerts_ExcludesFile = "alerts-exclude.txt";
//ChatLog Settings
public static bool ChatLog_Enabled = false;
public static bool ChatLog_DateTime = true;
public static string ChatLog_File = "chatlog.txt";
public static Bots.ChatLog.MessageFilter ChatLog_Filter = Bots.ChatLog.MessageFilter.AllMessages;
//PlayerListLog Settings
public static bool PlayerLog_Enabled = false;
public static string PlayerLog_File = "playerlog.txt";
public static int PlayerLog_Delay = 600;
//AutoRelog Settings
public static bool AutoRelog_Enabled = false;
public static int AutoRelog_Delay = 10;
public static int AutoRelog_Retries = 3;
public static string AutoRelog_KickMessagesFile = "kickmessages.txt";
//xAuth Settings
public static bool xAuth_Enabled = false;
public static string xAuth_Password = "";
//Scripting Settings
public static bool Scripting_Enabled = false;
public static string Scripting_ScriptFile = "script.txt";
private enum ParseMode { Default, Main, AntiAFK, Hangman, Alerts, ChatLog, AutoRelog, Scripting };
/// <summary>
/// Load settings from the give INI file
/// </summary>
/// <param name="settingsfile">File to load</param>
public static void LoadSettings(string settingsfile)
{
if (File.Exists(settingsfile))
{
try
{
string[] Lines = File.ReadAllLines(settingsfile);
ParseMode pMode = ParseMode.Default;
foreach (string lineRAW in Lines)
{
string line = lineRAW.Split('#')[0].Trim();
if (line.Length > 0)
{
if (line[0] == '[' && line[line.Length - 1] == ']')
{
switch (line.Substring(1, line.Length - 2).ToLower())
{
case "alerts": pMode = ParseMode.Alerts; break;
case "antiafk": pMode = ParseMode.AntiAFK; break;
case "autorelog": pMode = ParseMode.AutoRelog; break;
case "chatlog": pMode = ParseMode.ChatLog; break;
case "hangman": pMode = ParseMode.Hangman; break;
case "main": pMode = ParseMode.Main; break;
case "scripting": pMode = ParseMode.Scripting; break;
default: pMode = ParseMode.Default; break;
}
}
else
{
string argName = line.Split('=')[0];
if (line.Length > (argName.Length + 1))
{
string argValue = line.Substring(argName.Length + 1);
switch (pMode)
{
case ParseMode.Main:
switch (argName.ToLower())
{
case "login": Login = argValue; break;
case "password": Password = argValue; break;
case "serverip": ServerIP = argValue; break;
case "singlecommand": SingleCommand = argValue; break;
case "translationsfile": TranslationsFile = argValue; break;
case "botownersfile": Bots_OwnersFile = argValue; break;
case "consoletitle": Console.Title = argValue; break;
}
break;
case ParseMode.Alerts:
switch (argName.ToLower())
{
case "enabled": Alerts_Enabled = str2bool(argValue); break;
case "alertsfile": Alerts_MatchesFile = argValue; break;
case "excludesfile": Alerts_ExcludesFile = argValue; break;
}
break;
case ParseMode.AntiAFK:
switch (argName.ToLower())
{
case "enabled": AntiAFK_Enabled = str2bool(argValue); break;
case "delay": AntiAFK_Delay = str2int(argValue); break;
}
break;
case ParseMode.AutoRelog:
switch (argName.ToLower())
{
case "enabled": AutoRelog_Enabled = str2bool(argValue); break;
case "delay": AutoRelog_Delay = str2int(argValue); break;
case "retries": AutoRelog_Retries = str2int(argValue); break;
case "kickmessagesfile": AutoRelog_KickMessagesFile = argValue; break;
}
break;
case ParseMode.ChatLog:
switch (argName.ToLower())
{
case "enabled": ChatLog_Enabled = str2bool(argValue); break;
case "timestamps": ChatLog_DateTime = str2bool(argValue); break;
case "filter": ChatLog_Filter = Bots.ChatLog.str2filter(argValue); break;
case "logfile": ChatLog_File = argValue; break;
}
break;
case ParseMode.Hangman:
switch (argName.ToLower())
{
case "enabled": Hangman_Enabled = str2bool(argValue); break;
case "english": Hangman_English = str2bool(argValue); break;
case "wordsfile": Hangman_FileWords_EN = argValue; break;
case "fichiermots": Hangman_FileWords_FR = argValue; break;
}
break;
case ParseMode.Scripting:
switch (argName.ToLower())
{
case "enabled": Scripting_Enabled = str2bool(argValue); break;
case "scriptfile": Scripting_ScriptFile = argValue; break;
}
break;
}
}
}
}
}
}
catch (IOException) { }
}
}
/// <summary>
/// Write an INI file with default settings
/// </summary>
/// <param name="settingsfile">File to (over)write</param>
public static void WriteDefaultSettings(string settingsfile)
{
System.IO.File.WriteAllText(settingsfile, "#Minecraft Console Client v" + Program.Version + "\r\n#Startup Config File\r\n\r\n[Main]\r\n\r\n#General settings\r\n#leave blank = prompt user on startup\r\n#Use \"-\" as password for offline mode\r\n\r\nlogin=\r\npassword=\r\nserverip=\r\n\r\n#Advanced settings\r\n\r\ntranslationsfile=translations.lang\r\nbotownersfile=bot-owners.txt\r\nconsoletitle=Minecraft Console Client\r\n\r\n#Bot Settings\r\n\r\n[Alerts]\r\nenabled=false\r\nalertsfile=alerts.txt\r\nexcludesfile=alerts-exclude.txt\r\n\r\n[AntiAFK]\r\nenabled=false\r\ndelay=600 #10 = 1s\r\n\r\n[AutoRelog]\r\nenabled=false\r\ndelay=10\r\nretries=3\r\nkickmessagesfile=kickmessages.txt\r\n\r\n[ChatLog]\r\nenabled=false\r\ntimestamps=true\r\nfilter=messages\r\nlogfile=chatlog.txt\r\n\r\n[Hangman]\r\nenabled=false\r\nenglish=true\r\nwordsfile=words.txt\r\nfichiermots=mots.txt\r\n\r\n[Scripting]\r\nenabled=false\r\nscriptfile=testscript.txt\r\n");
}
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"; }
}
}