2013-08-06 16:11:46 +02:00
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 = "" ;
2014-11-05 02:04:33 +11:00
public static ushort ServerPort = 25565 ;
2015-05-19 15:36:20 +01:00
public static string ServerVersion = "" ;
2013-08-06 16:11:46 +02:00
public static string SingleCommand = "" ;
2014-01-17 19:54:10 +01:00
public static string ConsoleTitle = "" ;
2013-08-06 16:11:46 +02:00
2014-05-31 01:59:03 +02:00
//Proxy Settings
public static bool ProxyEnabled = false ;
public static string ProxyHost = "" ;
public static int ProxyPort = 0 ;
public static Proxy . ProxyHandler . Type proxyType = Proxy . ProxyHandler . Type . HTTP ;
public static string ProxyUsername = "" ;
public static string ProxyPassword = "" ;
2013-08-06 16:11:46 +02:00
//Other Settings
2014-01-17 20:39:55 +01:00
public static string TranslationsFile_FromMCDir = Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) + @"\.minecraft\assets\objects\9e\9e2fdc43fc1c7024ff5922b998fadb2971a64ee0" ; //MC 1.7.4 en_GB.lang
2014-01-13 12:38:01 +01:00
public static string TranslationsFile_Website_Index = "https://s3.amazonaws.com/Minecraft.Download/indexes/1.7.4.json" ;
public static string TranslationsFile_Website_Download = "http://resources.download.minecraft.net" ;
2015-05-26 19:00:37 +02:00
public static TimeSpan splitMessageDelay = TimeSpan . FromSeconds ( 2 ) ;
2014-04-03 00:17:47 +02:00
public static List < string > Bots_Owners = new List < string > ( ) ;
2014-02-01 14:57:31 +01:00
public static string Language = "en_GB" ;
2014-04-09 18:52:17 +02:00
public static bool chatTimeStamps = false ;
2015-03-25 22:50:20 +01:00
public static bool interactiveMode = true ;
2014-06-18 00:49:45 +02:00
public static char internalCmdChar = '/' ;
2014-06-30 13:55:51 +02:00
public static bool playerHeadAsIcon = false ;
2014-07-20 12:02:17 +02:00
public static string chatbotLogFile = "" ;
2013-08-06 16:11:46 +02:00
//AntiAFK Settings
public static bool AntiAFK_Enabled = false ;
public static int AntiAFK_Delay = 600 ;
2014-01-17 19:20:41 +01:00
public static string AntiAFK_Command = "/ping" ;
2013-08-06 16:11:46 +02:00
//Hangman Settings
public static bool Hangman_Enabled = false ;
public static bool Hangman_English = true ;
2013-08-12 13:06:07 +02:00
public static string Hangman_FileWords_EN = "hangman-en.txt" ;
public static string Hangman_FileWords_FR = "hangman-fr.txt" ;
2013-08-06 16:11:46 +02:00
//Alerts Settings
public static bool Alerts_Enabled = false ;
2014-05-10 21:03:03 +02:00
public static bool Alerts_Beep_Enabled = true ;
2013-08-06 16:11:46 +02:00
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" ;
2014-05-31 01:59:03 +02:00
public static ChatBots . ChatLog . MessageFilter ChatLog_Filter = ChatBots . ChatLog . MessageFilter . AllMessages ;
2013-08-06 16:11:46 +02:00
//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" ;
2014-05-06 22:41:14 +02:00
//Script Scheduler Settings
public static bool ScriptScheduler_Enabled = false ;
public static string ScriptScheduler_TasksFile = "tasks.ini" ;
2014-04-03 00:17:47 +02:00
//Remote Control
public static bool RemoteCtrl_Enabled = false ;
2014-08-28 15:12:58 +02:00
public static bool RemoteCtrl_AutoTpaccept = true ;
2015-01-02 22:10:12 +01:00
public static bool RemoteCtrl_AutoTpaccept_Everyone = false ;
2013-08-06 16:11:46 +02:00
2015-05-19 15:36:20 +01:00
//Auto Respond
public static bool Respond_Enabled = false ;
public static string Respond_MatchesFile = "detect.txt" ;
public static string Respond_RespondFile = "respond.txt" ;
2014-06-19 19:24:03 +02:00
//Custom app variables and Minecraft accounts
2014-06-11 20:40:25 +02:00
private static Dictionary < string , string > AppVars = new Dictionary < string , string > ( ) ;
2014-06-19 19:24:03 +02:00
private static Dictionary < string , KeyValuePair < string , string > > Accounts = new Dictionary < string , KeyValuePair < string , string > > ( ) ;
2014-11-05 02:04:33 +11:00
private static Dictionary < string , KeyValuePair < string , ushort > > Servers = new Dictionary < string , KeyValuePair < string , ushort > > ( ) ;
2014-06-11 20:40:25 +02:00
2015-05-19 15:36:20 +01:00
private enum ParseMode { Default , Main , AppVars , Proxy , AntiAFK , Hangman , Alerts , ChatLog , AutoRelog , ScriptScheduler , RemoteControl , Auto_Respond } ;
2013-08-06 16:11:46 +02:00
/// <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 ;
2014-05-06 22:41:14 +02:00
case "scriptscheduler" : pMode = ParseMode . ScriptScheduler ; break ;
2014-04-03 00:17:47 +02:00
case "remotecontrol" : pMode = ParseMode . RemoteControl ; break ;
2014-05-31 01:59:03 +02:00
case "proxy" : pMode = ParseMode . Proxy ; break ;
2014-06-18 00:49:45 +02:00
case "appvars" : pMode = ParseMode . AppVars ; break ;
2015-05-19 15:36:20 +01:00
case "auto respond" : pMode = ParseMode . Auto_Respond ; break ;
2013-08-06 16:11:46 +02:00
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 ;
2014-06-13 16:50:55 +02:00
case "serverip" : setServerIP ( argValue ) ; break ;
2013-08-06 16:11:46 +02:00
case "singlecommand" : SingleCommand = argValue ; break ;
2014-02-01 14:57:31 +01:00
case "language" : Language = argValue ; break ;
2014-01-16 19:33:48 +01:00
case "consoletitle" : ConsoleTitle = argValue ; break ;
2014-04-09 18:52:17 +02:00
case "timestamps" : chatTimeStamps = str2bool ( argValue ) ; break ;
2015-03-25 22:50:20 +01:00
case "exitonfailure" : interactiveMode = ! str2bool ( argValue ) ; break ;
2014-06-30 14:04:04 +02:00
case "playerheadicon" : playerHeadAsIcon = str2bool ( argValue ) ; break ;
2014-07-20 12:02:17 +02:00
case "chatbotlogfile" : chatbotLogFile = argValue ; break ;
2014-06-14 16:01:19 +02:00
case "mcversion" : ServerVersion = argValue ; break ;
2015-05-26 19:00:37 +02:00
case "splitmessagedelay" : splitMessageDelay = TimeSpan . FromSeconds ( str2int ( argValue ) ) ; break ;
2014-06-29 14:49:54 +02:00
2014-04-02 23:25:28 +02:00
case "botowners" :
Bots_Owners . Clear ( ) ;
2015-01-27 20:23:59 +01:00
foreach ( string name in argValue . ToLower ( ) . Split ( ',' ) )
Bots_Owners . Add ( name . Trim ( ) ) ;
2014-04-02 23:25:28 +02:00
break ;
2014-06-29 14:49:54 +02:00
2014-06-18 00:49:45 +02:00
case "internalcmdchar" :
switch ( argValue . ToLower ( ) )
{
case "none" : internalCmdChar = ' ' ; break ;
case "slash" : internalCmdChar = '/' ; break ;
case "backslash" : internalCmdChar = '\\' ; break ;
}
break ;
2014-06-29 14:49:54 +02:00
case "accountlist" :
2014-06-19 19:24:03 +02:00
if ( File . Exists ( argValue ) )
{
foreach ( string account_line in File . ReadAllLines ( argValue ) )
{
//Each line contains account data: 'Alias,Login,Password'
string [ ] account_data = account_line . Split ( '#' ) [ 0 ] . Trim ( ) . Split ( ',' ) ;
if ( account_data . Length = = 3 )
Accounts [ account_data [ 0 ] . ToLower ( ) ]
= new KeyValuePair < string , string > ( account_data [ 1 ] , account_data [ 2 ] ) ;
}
}
break ;
2014-06-29 14:49:54 +02:00
case "serverlist" :
if ( File . Exists ( argValue ) )
{
//Backup current server info
string server_host_temp = ServerIP ;
2014-11-05 02:04:33 +11:00
ushort server_port_temp = ServerPort ;
2014-06-29 14:49:54 +02:00
foreach ( string server_line in File . ReadAllLines ( argValue ) )
{
//Each line contains server data: 'Alias,Host:Port'
string [ ] server_data = server_line . Split ( '#' ) [ 0 ] . Trim ( ) . Split ( ',' ) ;
2014-06-29 14:53:30 +02:00
server_data [ 0 ] = server_data [ 0 ] . ToLower ( ) ;
2014-06-29 14:49:54 +02:00
if ( server_data . Length = = 2
& & server_data [ 0 ] ! = "localhost"
& & ! server_data [ 0 ] . Contains ( '.' )
& & setServerIP ( server_data [ 1 ] ) )
2014-06-29 14:53:30 +02:00
Servers [ server_data [ 0 ] ]
2014-11-05 02:04:33 +11:00
= new KeyValuePair < string , ushort > ( ServerIP , ServerPort ) ;
2014-06-29 14:49:54 +02:00
}
2015-05-19 15:36:20 +01:00
2014-06-29 14:49:54 +02:00
//Restore current server info
ServerIP = server_host_temp ;
ServerPort = server_port_temp ;
}
break ;
2013-08-06 16:11:46 +02:00
}
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 ;
2014-05-10 21:03:03 +02:00
case "beeponalert" : Alerts_Beep_Enabled = str2bool ( argValue ) ; break ;
2013-08-06 16:11:46 +02:00
}
break ;
case ParseMode . AntiAFK :
switch ( argName . ToLower ( ) )
{
case "enabled" : AntiAFK_Enabled = str2bool ( argValue ) ; break ;
case "delay" : AntiAFK_Delay = str2int ( argValue ) ; break ;
2014-01-17 19:20:41 +01:00
case "command" : AntiAFK_Command = argValue = = "" ? "/ping" : argValue ; break ;
2013-08-06 16:11:46 +02:00
}
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 ;
2014-05-31 01:59:03 +02:00
case "filter" : ChatLog_Filter = ChatBots . ChatLog . str2filter ( argValue ) ; break ;
2013-08-06 16:11:46 +02:00
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 ;
2014-05-06 22:41:14 +02:00
case ParseMode . ScriptScheduler :
2013-08-06 16:11:46 +02:00
switch ( argName . ToLower ( ) )
{
2014-05-06 22:41:14 +02:00
case "enabled" : ScriptScheduler_Enabled = str2bool ( argValue ) ; break ;
case "tasksfile" : ScriptScheduler_TasksFile = argValue ; break ;
2013-08-06 16:11:46 +02:00
}
break ;
2014-04-03 00:17:47 +02:00
case ParseMode . RemoteControl :
switch ( argName . ToLower ( ) )
{
case "enabled" : RemoteCtrl_Enabled = str2bool ( argValue ) ; break ;
2014-08-28 15:12:58 +02:00
case "autotpaccept" : RemoteCtrl_AutoTpaccept = str2bool ( argValue ) ; break ;
2015-01-02 22:10:12 +01:00
case "tpaccepteveryone" : RemoteCtrl_AutoTpaccept_Everyone = str2bool ( argValue ) ; break ;
2014-04-03 00:17:47 +02:00
}
break ;
2014-05-31 01:59:03 +02:00
case ParseMode . Proxy :
switch ( argName . ToLower ( ) )
{
case "enabled" : ProxyEnabled = str2bool ( argValue ) ; break ;
case "type" :
argValue = argValue . ToLower ( ) ;
if ( argValue = = "http" ) { proxyType = Proxy . ProxyHandler . Type . HTTP ; }
else if ( argValue = = "socks4" ) { proxyType = Proxy . ProxyHandler . Type . SOCKS4 ; }
2015-05-19 15:36:20 +01:00
else if ( argValue = = "socks4a" ) { proxyType = Proxy . ProxyHandler . Type . SOCKS4a ; }
2014-05-31 01:59:03 +02:00
else if ( argValue = = "socks5" ) { proxyType = Proxy . ProxyHandler . Type . SOCKS5 ; }
break ;
2014-08-27 17:29:58 +02:00
case "server" :
string [ ] host_splitted = argValue . Split ( ':' ) ;
if ( host_splitted . Length = = 1 )
{
ProxyHost = host_splitted [ 0 ] ;
ProxyPort = 80 ;
}
else if ( host_splitted . Length = = 2 )
{
ProxyHost = host_splitted [ 0 ] ;
ProxyPort = str2int ( host_splitted [ 1 ] ) ;
}
break ;
2014-05-31 01:59:03 +02:00
case "username" : ProxyUsername = argValue ; break ;
case "password" : ProxyPassword = argValue ; break ;
}
break ;
2014-06-11 20:40:25 +02:00
case ParseMode . AppVars :
2014-06-14 18:48:43 +02:00
setVar ( argName , argValue ) ;
2014-06-11 20:40:25 +02:00
break ;
2015-05-19 15:36:20 +01:00
case ParseMode . Auto_Respond :
switch ( argName . ToLower ( ) )
{
case "enabled" : Respond_Enabled = str2bool ( argValue ) ; break ;
case "matchfile" : Respond_MatchesFile = argValue ; break ;
case "respondfile" : Respond_RespondFile = argValue ; break ;
}
break ;
2013-08-06 16:11:46 +02:00
}
}
}
}
}
}
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 )
{
2014-01-17 19:20:41 +01:00
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"
2014-04-03 00:17:47 +02:00
+ "#leave blank to prompt user on startup\r\n"
2014-01-17 19:20:41 +01:00
+ "#Use \"-\" as password for offline mode\r\n"
+ "\r\n"
2014-05-31 01:59:03 +02:00
+ "login=\r\n"
+ "password=\r\n"
+ "serverip=\r\n"
2014-01-17 19:20:41 +01:00
+ "\r\n"
+ "#Advanced settings\r\n"
+ "\r\n"
2014-02-01 14:57:31 +01:00
+ "language=en_GB\r\n"
2014-04-02 23:25:28 +02:00
+ "botowners=Player1,Player2,Player3\r\n"
2015-03-28 13:39:56 +01:00
+ "consoletitle=%username%@%serverip% - Minecraft Console Client\r\n"
2014-06-18 00:49:45 +02:00
+ "internalcmdchar=slash #use 'none', 'slash' or 'backslash'\r\n"
2015-05-26 19:00:37 +02:00
+ "splitmessagedelay=2 #seconds between each part of a long message\r\n"
2014-06-14 16:01:19 +02:00
+ "mcversion=auto #use 'auto' or '1.X.X' values\r\n"
2014-07-20 12:02:17 +02:00
+ "chatbotlogfile= #leave empty for no logfile\r\n"
2014-06-29 14:49:54 +02:00
+ "accountlist=accounts.txt\r\n"
+ "serverlist=servers.txt\r\n"
2014-06-30 13:55:51 +02:00
+ "playerheadicon=true\r\n"
2014-05-31 12:56:54 +02:00
+ "exitonfailure=false\r\n"
2014-04-09 18:52:17 +02:00
+ "timestamps=false\r\n"
2014-01-17 19:20:41 +01:00
+ "\r\n"
2014-06-11 20:40:25 +02:00
+ "[AppVars]\r\n"
+ "#yourvar=yourvalue\r\n"
2014-06-19 19:24:03 +02:00
+ "#can be used in some other fields as %yourvar%\r\n"
+ "#%username% and %serverip% are reserved variables.\r\n"
2014-06-11 20:40:25 +02:00
+ "\r\n"
2014-05-31 01:59:03 +02:00
+ "[Proxy]\r\n"
+ "enabled=false\r\n"
+ "type=HTTP #Supported types: HTTP, SOCKS4, SOCKS4a, SOCKS5\r\n"
2014-08-27 17:29:58 +02:00
+ "server=0.0.0.0:0000\r\n"
2014-05-31 01:59:03 +02:00
+ "username=\r\n"
+ "password=\r\n"
+ "\r\n"
2014-01-17 19:20:41 +01:00
+ "#Bot Settings\r\n"
+ "\r\n"
+ "[Alerts]\r\n"
+ "enabled=false\r\n"
+ "alertsfile=alerts.txt\r\n"
+ "excludesfile=alerts-exclude.txt\r\n"
2014-05-10 21:03:03 +02:00
+ "beeponalert=true\r\n"
2014-01-17 19:20:41 +01:00
+ "\r\n"
+ "[AntiAFK]\r\n"
+ "enabled=false\r\n"
+ "delay=600 #10 = 1s\r\n"
+ "command=/ping\r\n"
+ "\r\n"
+ "[AutoRelog]\r\n"
+ "enabled=false\r\n"
+ "delay=10\r\n"
+ "retries=3 #-1 = unlimited\r\n"
+ "kickmessagesfile=kickmessages.txt\r\n"
+ "\r\n"
+ "[ChatLog]\r\n"
+ "enabled=false\r\n"
+ "timestamps=true\r\n"
+ "filter=messages\r\n"
2014-06-11 20:40:25 +02:00
+ "logfile=chatlog-%username%-%serverip%.txt\r\n"
2014-01-17 19:20:41 +01:00
+ "\r\n"
+ "[Hangman]\r\n"
+ "enabled=false\r\n"
+ "english=true\r\n"
+ "wordsfile=hangman-en.txt\r\n"
+ "fichiermots=hangman-fr.txt\r\n"
+ "\r\n"
2014-05-06 22:41:14 +02:00
+ "[ScriptScheduler]\r\n"
2014-01-17 19:20:41 +01:00
+ "enabled=false\r\n"
2014-05-06 22:41:14 +02:00
+ "tasksfile=tasks.ini\r\n"
2014-04-03 00:17:47 +02:00
+ "\r\n"
+ "[RemoteControl]\r\n"
2014-08-28 15:12:58 +02:00
+ "enabled=false\r\n"
2015-01-02 22:10:12 +01:00
+ "autotpaccept=true\r\n"
2015-05-19 15:36:20 +01:00
+ "tpaccepteveryone=false\r\n"
+ "\r\n"
+ "[Auto Respond]\r\n"
+ "enabled=false\r\n"
+ "matchfile=detect.txt\r\n"
+ "respondfile=respond.txt\r\n"
+ "#To use the bot, place the text to detect in the matchfile file and the text to respond with in the respondfile\r\n"
+ "#Each line in each file is relevant to the same line in the other document, for example if the bot detects the text in line 1 of the first file, it will respond with line 1 of the second file.\r\n" , Encoding . UTF8 ) ;
2013-08-06 16:11:46 +02:00
}
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" ; }
2014-06-13 16:50:55 +02:00
/// <summary>
2014-06-19 19:24:03 +02:00
/// Load login/password using an account alias
2014-06-13 16:50:55 +02:00
/// </summary>
2014-06-19 19:24:03 +02:00
/// <returns>True if the account was found and loaded</returns>
2014-06-13 16:50:55 +02:00
2014-06-19 19:24:03 +02:00
public static bool setAccount ( string accountAlias )
2014-06-13 16:50:55 +02:00
{
2014-06-19 19:24:03 +02:00
accountAlias = accountAlias . ToLower ( ) ;
if ( Accounts . ContainsKey ( accountAlias ) )
2014-06-13 16:50:55 +02:00
{
2014-06-19 19:24:03 +02:00
Settings . Login = Accounts [ accountAlias ] . Key ;
Settings . Password = Accounts [ accountAlias ] . Value ;
return true ;
2014-06-13 16:50:55 +02:00
}
2014-06-19 19:24:03 +02:00
else return false ;
}
/// <summary>
2014-06-29 14:49:54 +02:00
/// Load server information in ServerIP and ServerPort variables from a "serverip:port" couple or server alias
2014-06-19 19:24:03 +02:00
/// </summary>
/// <returns>True if the server IP was valid and loaded, false otherwise</returns>
2014-06-29 14:49:54 +02:00
public static bool setServerIP ( string server )
2014-06-19 19:24:03 +02:00
{
2014-09-07 15:20:58 +02:00
server = server . ToLower ( ) ;
2014-06-29 14:49:54 +02:00
string [ ] sip = server . Split ( ':' ) ;
2014-06-19 19:24:03 +02:00
string host = sip [ 0 ] ;
2014-11-05 02:04:33 +11:00
ushort port = 25565 ;
2015-05-19 15:36:20 +01:00
2014-06-19 19:24:03 +02:00
if ( sip . Length > 1 )
2014-06-13 16:50:55 +02:00
{
try
{
2014-11-05 01:59:32 +11:00
port = Convert . ToUInt16 ( sip [ 1 ] ) ;
2014-06-13 16:50:55 +02:00
}
2014-06-19 19:24:03 +02:00
catch ( FormatException ) { return false ; }
}
if ( host = = "localhost" | | host . Contains ( '.' ) )
{
ServerIP = host ;
ServerPort = port ;
return true ;
2014-06-13 16:50:55 +02:00
}
2014-06-29 14:49:54 +02:00
else if ( Servers . ContainsKey ( server ) )
{
ServerIP = Servers [ server ] . Key ;
ServerPort = Servers [ server ] . Value ;
2014-08-27 17:16:35 +02:00
return true ;
2014-06-29 14:49:54 +02:00
}
2015-05-19 15:36:20 +01:00
2014-06-19 19:24:03 +02:00
return false ;
2014-06-13 16:50:55 +02:00
}
2014-06-14 18:48:43 +02:00
/// <summary>
/// Set a custom %variable% which will be available through expandVars()
/// </summary>
/// <param name="varName">Name of the variable</param>
/// <param name="varData">Value of the variable</param>
/// <returns>True if the parameters were valid</returns>
public static bool setVar ( string varName , string varData )
{
varName = new string ( varName . TakeWhile ( char . IsLetterOrDigit ) . ToArray ( ) ) . ToLower ( ) ;
if ( varName . Length > 0 )
{
AppVars [ varName ] = varData ;
return true ;
}
else return false ;
}
2014-06-11 20:40:25 +02:00
/// <summary>
/// Replace %variables% with their value
/// </summary>
/// <param name="str">String to parse</param>
/// <returns>Modifier string</returns>
2014-06-13 16:50:55 +02:00
public static string expandVars ( string str )
2014-06-11 20:40:25 +02:00
{
StringBuilder result = new StringBuilder ( ) ;
for ( int i = 0 ; i < str . Length ; i + + )
{
if ( str [ i ] = = '%' )
{
2014-06-13 16:50:55 +02:00
bool varname_ok = false ;
2014-06-11 20:40:25 +02:00
StringBuilder var_name = new StringBuilder ( ) ;
2014-06-13 16:50:55 +02:00
2014-06-11 20:40:25 +02:00
for ( int j = i + 1 ; j < str . Length ; j + + )
{
if ( ! char . IsLetterOrDigit ( str [ j ] ) )
{
if ( str [ j ] = = '%' )
varname_ok = var_name . Length > 0 ;
2014-06-13 16:50:55 +02:00
break ;
2014-06-11 20:40:25 +02:00
}
else var_name . Append ( str [ j ] ) ;
}
2014-06-13 16:50:55 +02:00
2014-06-11 20:40:25 +02:00
if ( varname_ok )
{
string varname = var_name . ToString ( ) ;
string varname_lower = varname . ToLower ( ) ;
i = i + varname . Length + 1 ;
2014-06-13 16:50:55 +02:00
switch ( varname_lower )
2014-06-11 20:40:25 +02:00
{
2014-06-13 16:50:55 +02:00
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 ;
2014-06-11 20:40:25 +02:00
}
}
else result . Append ( str [ i ] ) ;
}
else result . Append ( str [ i ] ) ;
}
return result . ToString ( ) ;
}
2013-08-06 16:11:46 +02:00
}
}