Store server IP and server port in distinct vars

- Change "replaceVars" to "expandVars" (more explicit)
- Store server IP and server port in distinct vars in Settings class
- Add setServerIP setter in Settings which automatically split a
host:port string
- Add %serverip% variable which use the new ServerPort setting
- Fix "text%incompletevarnameatstringend" strings
This commit is contained in:
ORelio 2014-06-13 16:50:55 +02:00
parent 068b87a11a
commit 283074bb63
5 changed files with 74 additions and 60 deletions

View file

@ -37,7 +37,7 @@ namespace MinecraftClient.ChatBots
case "connect":
if (command.Length >= 9)
{
Settings.ServerIP = command.Substring(8);
Settings.setServerIP(command.Substring(8));
ReconnectToTheServer();
}
break;

View file

@ -111,7 +111,7 @@ namespace MinecraftClient.ChatBots
case "connect":
if (instruction_line.Length >= 9)
{
Settings.ServerIP = instruction_line.Substring(8);
Settings.setServerIP(instruction_line.Substring(8));
ReconnectToTheServer();
}
break;

View file

@ -44,25 +44,31 @@ namespace MinecraftClient
/// Starts the main chat client
/// </summary>
/// <param name="username">The chosen username of a premium Minecraft Account</param>
/// <param name="sessionID">A valid sessionID obtained with MinecraftCom.GetLogin()</param>
/// <param name="server_port">The server IP (serveradress or serveradress:port)</param>
/// <param name="uuid">The player's UUID for online-mode authentication</param>
/// <param name="sessionID">A valid sessionID obtained after logging in</param>
/// <param name="server_ip">The server IP</param>
/// <param name="port">The server port to use</param>
/// <param name="protocolversion">Minecraft protocol version to use</param>
public McTcpClient(string username, string uuid, string sessionID, int protocolversion, string server_port)
public McTcpClient(string username, string uuid, string sessionID, int protocolversion, string server_ip, short port)
{
StartClient(username, uuid, sessionID, server_port, protocolversion, false, "");
StartClient(username, uuid, sessionID, server_ip, port, protocolversion, false, "");
}
/// <summary>
/// Starts the main chat client in single command sending mode
/// </summary>
/// <param name="username">The chosen username of a premium Minecraft Account</param>
/// <param name="sessionID">A valid sessionID obtained with MinecraftCom.GetLogin()</param>
/// <param name="server_port">The server IP (serveradress or serveradress:port)</param>
/// <param name="uuid">The player's UUID for online-mode authentication</param>
/// <param name="sessionID">A valid sessionID obtained after logging in</param>
/// <param name="server_ip">The server IP</param>
/// <param name="port">The server port to use</param>
/// <param name="protocolversion">Minecraft protocol version to use</param>
/// <param name="command">The text or command to send.</param>
public McTcpClient(string username, string uuid, string sessionID, string server_port, int protocolversion, string command)
public McTcpClient(string username, string uuid, string sessionID, string server_ip, short port, int protocolversion, string command)
{
StartClient(username, uuid, sessionID, server_port, protocolversion, true, command);
StartClient(username, uuid, sessionID, server_ip, port, protocolversion, true, command);
}
/// <summary>
@ -70,41 +76,30 @@ namespace MinecraftClient
/// </summary>
/// <param name="user">The chosen username of a premium Minecraft Account</param>
/// <param name="sessionID">A valid sessionID obtained with MinecraftCom.GetLogin()</param>
/// <param name="server_port">The server IP (serveradress or serveradress:port)/param>
/// <param name="server_ip">The server IP</param>
/// <param name="port">The server port to use</param>
/// <param name="protocolversion">Minecraft protocol version to use</param>
/// <param name="uuid">The player's UUID for online-mode authentication</param>
/// <param name="singlecommand">If set to true, the client will send a single command and then disconnect from the server</param>
/// <param name="command">The text or command to send. Will only be sent if singlecommand is set to true.</param>
private void StartClient(string user, string uuid, string sessionID, string server_port, int protocolversion, bool singlecommand, string command)
private void StartClient(string user, string uuid, string sessionID, string server_ip, short port, int protocolversion, bool singlecommand, string command)
{
string[] sip = server_port.Split(':');
this.sessionid = sessionID;
this.uuid = uuid;
this.username = user;
this.host = sip[0];
if (sip.Length == 1)
{
port = 25565;
}
else
{
try
{
port = Convert.ToInt32(sip[1]);
}
catch (FormatException) { port = 25565; }
}
this.host = server_ip;
this.port = port;
if (!singlecommand)
{
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.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.ChatLog_Enabled) { BotLoad(new ChatBots.ChatLog(Settings.expandVars(Settings.ChatLog_File), Settings.ChatLog_Filter, Settings.ChatLog_DateTime)); }
if (Settings.PlayerLog_Enabled) { BotLoad(new ChatBots.PlayerListLogger(Settings.PlayerLog_Delay, Settings.expandVars(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.replaceVars(Settings.ScriptScheduler_TasksFile))); }
if (Settings.ScriptScheduler_Enabled) { BotLoad(new ChatBots.ScriptScheduler(Settings.expandVars(Settings.ScriptScheduler_TasksFile))); }
if (Settings.RemoteCtrl_Enabled) { BotLoad(new ChatBots.RemoteControl()); }
}
@ -191,7 +186,7 @@ namespace MinecraftClient
}
else if (text.ToLower().StartsWith("/connect "))
{
Settings.ServerIP = text.Substring(9);
Settings.setServerIP(text.Substring(9));
Program.Restart();
}
else if (text != "")

View file

@ -59,7 +59,7 @@ namespace MinecraftClient
Settings.Password = args[1];
if (args.Length >= 3)
{
Settings.ServerIP = args[2];
Settings.setServerIP(args[2]);
//Single command?
if (args.Length >= 4)
@ -73,7 +73,7 @@ namespace MinecraftClient
if (Settings.ConsoleTitle != "")
{
Settings.Username = "New Window";
Console.Title = Settings.replaceVars(Settings.ConsoleTitle);
Console.Title = Settings.expandVars(Settings.ConsoleTitle);
}
//Asking the user to type in missing data such as Username and Password
@ -127,14 +127,14 @@ namespace MinecraftClient
{
if (Settings.ConsoleTitle != "")
{
Console.Title = Settings.replaceVars(Settings.ConsoleTitle);
Console.Title = Settings.expandVars(Settings.ConsoleTitle);
}
Console.WriteLine("Success. (session ID: " + sessionID + ')');
if (Settings.ServerIP == "")
{
Console.Write("Server IP : ");
Settings.ServerIP = Console.ReadLine();
Settings.setServerIP(Console.ReadLine());
}
//Get server version
@ -147,9 +147,9 @@ namespace MinecraftClient
//Start the main TCP client
if (Settings.SingleCommand != "")
{
Client = new McTcpClient(Settings.Username, UUID, sessionID, Settings.ServerIP, 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);
else Client = new McTcpClient(Settings.Username, UUID, sessionID, protocolversion, Settings.ServerIP, Settings.ServerPort);
}
catch (NotSupportedException)
{

View file

@ -20,6 +20,7 @@ namespace MinecraftClient
public static string Username = "";
public static string Password = "";
public static string ServerIP = "";
public static short ServerPort = 25565;
public static string SingleCommand = "";
public static string ConsoleTitle = "";
@ -133,7 +134,7 @@ namespace MinecraftClient
{
case "login": Login = argValue; break;
case "password": Password = argValue; break;
case "serverip": ServerIP = argValue; break;
case "serverip": setServerIP(argValue); break;
case "singlecommand": SingleCommand = argValue; break;
case "language": Language = argValue; break;
case "consoletitle": ConsoleTitle = argValue; break;
@ -327,56 +328,74 @@ 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>
/// Parse a "serverip:port" couple and store the values in ServerIP and ServerPort variables
/// </summary>
public static void setServerIP(string serverIP)
{
string[] sip = serverIP.Split(':');
ServerIP = sip[0];
if (sip.Length == 1)
{
ServerPort = 25565;
}
else
{
try
{
ServerPort = Convert.ToInt16(sip[1]);
}
catch (FormatException) { ServerPort = 25565; }
}
}
/// <summary>
/// Replace %variables% with their value
/// </summary>
/// <param name="str">String to parse</param>
/// <returns>Modifier string</returns>
public static string replaceVars(string str)
public static string expandVars(string str)
{
StringBuilder result = new StringBuilder();
for (int i = 0; i < str.Length; i++)
{
if (str[i] == '%')
{
bool varname_ok = true;
bool varname_ok = false;
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;
}
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")
switch (varname_lower)
{
result.Append(Username);
case "username": result.Append(Username); break;
case "serverip": result.Append(ServerIP); break;
case "serverport": result.Append(ServerPort); break;
default:
if (AppVars.ContainsKey(varname_lower))
{
result.Append(AppVars[varname_lower]);
}
else result.Append("%" + varname + '%');
break;
}
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]);
}