using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Net;
using System.Security.Principal;
using System.Threading;
using System.Windows.Forms;
namespace OnTeraLauncher
{
internal class OnTeraLauncher
{
protected static string IpAddress;
protected static List<string> Hosts;
protected static NotifyIcon NotifyIcon;
static OnTeraLauncher()
{
OnTeraLauncher.IpAddress = "159.253.18.161";
List<string> list = new List<string>();
list.Add("account.tera-europe.com");
list.Add("download.frogster-online.com");
OnTeraLauncher.Hosts = list;
}
public OnTeraLauncher()
{
base.\u002Ector();
}
[STAThread]
private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
WindowsIdentity current = WindowsIdentity.GetCurrent();
if (current == null)
{
int num1 = (int) MessageBox.Show((IWin32Window) null, "Unknown error.\nНеизвестная ошибка.", (string) null, MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
else if (!new WindowsPrincipal(current).IsInRole(WindowsBuiltInRole.Administrator))
{
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.Verb = "runas";
processStartInfo.FileName = Application.ExecutablePath;
ProcessStartInfo startInfo = processStartInfo;
try
{
Process.Start(startInfo);
}
catch
{
int num2 = (int) MessageBox.Show((IWin32Window) null, "Access denied! Run by Administrator...\nДоступ запрещен! Запустите от Администратора...", (string) null, MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
}
else
{
Icon associatedIcon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
NotifyIcon notifyIcon = new NotifyIcon((IContainer) new Container());
notifyIcon.Text = "OnTeraLauncher";
notifyIcon.Icon = associatedIcon;
notifyIcon.Visible = true;
using (OnTeraLauncher.NotifyIcon = notifyIcon)
{
if (!OnTeraLauncher.SetHosts())
{
Thread.Sleep(2500);
}
else
{
Thread.Sleep(1000);
if (!OnTeraLauncher.Check())
{
Thread.Sleep(2500);
}
else
{
Thread.Sleep(1000);
if (OnTeraLauncher.RunOffLauncher())
return;
Thread.Sleep(2500);
}
}
}
}
}
protected static bool SetHosts()
{
OnTeraLauncher.NotifyIcon.ShowBalloonTip(250, "OnTeraLauncher", "Rewrite hosts...", ToolTipIcon.Info);
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "drivers/etc/hosts");
List<string> list = new List<string>((IEnumerable<string>) OnTeraLauncher.Hosts);
try
{
string[] strArray = System.IO.File.Exists(path) ? System.IO.File.ReadAllLines(path) : new string[0];
using (StreamWriter streamWriter = new StreamWriter(path))
{
foreach (string str1 in strArray)
{
bool flag = false;
foreach (string str2 in list)
{
if (str1.IndexOf(str2) != -1)
{
streamWriter.WriteLine("{0}\t{1}", (object) OnTeraLauncher.IpAddress, (object) str2);
list.Remove(str2);
flag = true;
break;
}
}
if (!flag)
streamWriter.WriteLine(str1);
}
foreach (string str in list)
streamWriter.WriteLine("{0}\t{1}", (object) OnTeraLauncher.IpAddress, (object) str);
}
}
catch
{
OnTeraLauncher.NotifyIcon.ShowBalloonTip(500, "OnTeraLauncher", "Can't rewrite hosts...", ToolTipIcon.Error);
return false;
}
return true;
}
protected static bool Check()
{
OnTeraLauncher.NotifyIcon.ShowBalloonTip(250, "OnTeraLauncher", "Check the connection...", ToolTipIcon.Info);
try
{
IPAddress ipAddress = IPAddress.Parse(OnTeraLauncher.IpAddress);
foreach (string hostNameOrAddress in OnTeraLauncher.Hosts)
{
if (Array.IndexOf<IPAddress>(Dns.GetHostAddresses(hostNameOrAddress), ipAddress) == -1)
{
OnTeraLauncher.NotifyIcon.ShowBalloonTip(500, "Invalid rewrite finded!", "Try shutdown antivirus.", ToolTipIcon.Error);
return false;
}
}
}
catch
{
OnTeraLauncher.NotifyIcon.ShowBalloonTip(500, "OnTeraLauncher", "Check failed...", ToolTipIcon.Error);
return false;
}
return true;
}
protected static bool RunOffLauncher()
{
OnTeraLauncher.NotifyIcon.ShowBalloonTip(250, "OnTeraLauncher", "Run launcher...", ToolTipIcon.Info);
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.Verb = "runas";
processStartInfo.FileName = Path.Combine(Application.StartupPath, "TERA-Launcher.exe");
ProcessStartInfo startInfo = processStartInfo;
try
{
Process.Start(startInfo);
}
catch
{
OnTeraLauncher.NotifyIcon.ShowBalloonTip(500, "OnTeraLauncher", "Can't start TERA-Launcher.exe", ToolTipIcon.Error);
return false;
}
return true;
}
}
}