using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Security.Cryptography;
using System.Net;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace EuroDemons_Launcher
{
public partial class Form1 : Form
{
ArrayList Updates = new ArrayList();
public static string temppath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
string dirpath = temppath.Replace("file:\\", "");
int updatedone = 0;
string url = "http://patch.eurodemons.co.uk/";
string copy1;
string copy2;
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
public Form1()
{
InitializeComponent();
}
public void update()
{
label1.Text = "Checking For Updates Please Wait...";
label1.Visible = true;
WebClient client = new WebClient();
client.DownloadFileCompleted += new AsyncCompletedEventHandler(updatepatch);
client.DownloadFileAsync(new Uri(url + "patch.text"), dirpath + "\\patch.text");
}
void updatepatch(object sender, AsyncCompletedEventArgs e)
{
phraseupdate(url);
}
private void phraseupdate(string localURL)
{
using (StreamReader reader = new StreamReader(dirpath + "\\patch.text"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
string updateread = line;
string[] update = updateread.Split('-');// 0 path/filename - 1 md5 - 2 filename
if (!updatecheck(dirpath + "\\" + update[0] + "\\" + update[2], update[1]) == true)
{
Updates.Add(update[0] + "-" + localURL + update[2] + "-" + update[2]);
}
}
label1.Visible = false;
if (Updates.Count != 0)
{
Download();
label1.Text = "Downloading Updates...";
label1.Visible = true;
label2.Text = "Updating Files 1 / " + Updates.Count;
label2.Visible = true;
} else {
button1.Enabled = true;
label1.Text = "No Updates Press Start To Launch Eurodemons.";
label1.Visible = true;
label2.Visible = false;
button1.Image = EuroDemons_Launcher.Properties.Resources.Start;
}
if (reader != null)
reader.Close();
}
}
public Boolean updatecheck(string filename, string md5)
{
if (File.Exists(filename))
{
if (MD5(filename) == md5)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
protected string MD5(string fileName)
{
FileStream file = new FileStream(fileName, FileMode.Open);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(file);
file.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
}
public void ExecuteCommandSync(object command)
{
try
{
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
proc.Close();
Close();
}
catch (Exception)
{
MessageBox.Show("Error Launching Game");
}
Close();
}
private void Download()
{
copy1 = "";
copy2 = "";
if (!Directory.Exists(dirpath + "\\patch"))
{
Directory.CreateDirectory(dirpath + "\\patch");
}
string file = Updates[updatedone].ToString();
string[] url = file.Split('-');
copy1 = dirpath + "\\patch\\" + url[2];
copy2 = dirpath + url[0] + url[2];
if (File.Exists(copy1))
{
File.Delete(copy1);
}
if (File.Exists(copy2))
{
File.Delete(copy2);
}
progressBar1.Visible = true;
progressBar2.Visible = true;
label3.Visible = true;
label3.Visible = true;
label4.Visible = true;
label3.Text = "Downloading " + url[2] + ".";
WebClient client = new WebClient();
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadCompleted);
client.DownloadFileAsync(new Uri(url[1]), dirpath + "\\Patch\\" + url[2]);
}
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
double bytesIn = double.Parse(e.BytesReceived.ToString());
double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
double percentage = bytesIn / totalBytes * 100;
progressBar2.Value = int.Parse(Math.Truncate(percentage).ToString());
}
void DownloadCompleted(object sender, AsyncCompletedEventArgs e)
{
File.Move(copy1, copy2);
if (updatedone == Updates.Count - 1)
{
progressBar1.Value = 100;
button1.Enabled = true;
button1.Image = EuroDemons_Launcher.Properties.Resources.Start;
label1.Text = "Files Sucessfully Updated Press Start To Play EuroDemons.";
label1.Visible = true;
label2.Visible = false;
label3.Visible = false;
label4.Visible = false;
progressBar1.Visible = false;
progressBar2.Visible = false;
}
else
{
updatedone++;
Download();
int percent = (int)(Decimal.Divide(updatedone, Updates.Count) * 100);
progressBar1.Value = percent;
int updatedcounter = updatedone + 1;
label2.Text = "Updating Files " + updatedcounter + " / " + Updates.Count + ".";
}
}
private void Form1_Load(object sender, EventArgs e)
{
if (File.Exists(dirpath + "\\Replace.exe"))
{
File.Delete(dirpath + "\\Replace.exe");
}
update();
timer1.Interval = 5000;
timer1.Start();
}
public void move(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
move(sender, e);
}
private void timer1_Tick(object sender, EventArgs e)
{
webBrowser1.Left = 3;
}
private void button1_Click_1(object sender, EventArgs e)
{
Close();
button1.Enabled = false;
button1.Image = EuroDemons_Launcher.Properties.Resources.StartRed;
ExecuteCommandSync("@Eurodemons.exe Eurodemons");
Close();
}
private void button3_Click_1(object sender, EventArgs e)
{
MessageBox.Show("Designed By Steven & Kris & Timmy, Coded By Steven © 2010 - 2011 All Rights Reserved");
}
private void button2_Click_1(object sender, EventArgs e)
{
Close();
}
private void progressBar1_MouseDown(object sender, MouseEventArgs e)
{
move(sender, e);
}
private void progressBar2_MouseDown(object sender, MouseEventArgs e)
{
move(sender, e);
}
private void label1_MouseDown(object sender, MouseEventArgs e)
{
move(sender, e);
}
private void label2_MouseDown(object sender, MouseEventArgs e)
{
move(sender, e);
}
private void label4_MouseDown(object sender, MouseEventArgs e)
{
move(sender, e);
}
private void label3_MouseDown(object sender, MouseEventArgs e)
{
move(sender, e);
}
}
}