namespace CryOnline_Launcher
{
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.IO;
    using System.Net;
    using System.Security.Cryptography;
    using System.Threading;
    using System.Windows.Forms;
    using System.Xml;

    public class Form2 : Form
    {
        private IContainer components = null;
        private MemoryStream FilesecStream;
        private bool IsDownloaded = false;
        private Label label1;
        private Label label2;
        private string Password;
        private ProgressBar progressBar1;
        private ProgressBar progressBar2;
        private string Username;

        public Form2()
        {
            this.InitializeComponent();
        }

        private bool CheckMD5File(string hash, string filename)
        {
            if (!System.IO.File.Exists(filename))
            {
                return false;
            }
            return (string.Compare(hash, this.FileToMD5(filename)) == 0);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void DoFileSec()
        {
            MethodInvoker method = null;
            WebClient client = new WebClient();
            try
            {
                this.FilesecStream = new MemoryStream(client.DownloadData("http://cry-community.de/patcher/Filesecurity/filesec.xml"));
                XmlDocument document = new XmlDocument();
                document.Load(this.FilesecStream);
                XmlNodeList FilesToCheck = document.DocumentElement.SelectNodes("/securedfiles/fileinfo");
                this.progressBar2.Invoke(() => this.progressBar2.Maximum = FilesToCheck.Count);
                foreach (XmlNode node2 in FilesToCheck)
                {
                    string innerText = node2.Attributes["fmd5"].InnerText;
                    string filename = @".\ressystem\" + node2.Attributes["fname"].InnerText;
                    if (!this.CheckMD5File(innerText, filename))
                    {
                        if (System.IO.File.Exists(filename))
                        {
                            System.IO.File.Delete(filename);
                        }
                        WebClient client2 = new WebClient();
                        client2.DownloadProgressChanged += new DownloadProgressChangedEventHandler(this.wc2_DownloadProgressChanged);
                        client2.DownloadFileCompleted += new AsyncCompletedEventHandler(this.wc2_DownloadFileCompleted);
                        client2.DownloadFileAsync(new Uri("http://cry-community.de/patcher/Filesecurity/FileSecData/" + node2.Attributes["fname"].InnerText.Replace(@"\", "/")), filename);
                        while (!this.IsDownloaded)
                        {
                        }
                        this.IsDownloaded = false;
                    }
                    if (method == null)
                    {
                        method = () => this.progressBar2.PerformStep();
                    }
                    this.progressBar2.Invoke(method);
                }
                ProcessStartInfo startInfo = new ProcessStartInfo("CryOnline.exe", "-k " + this.Username + "#" + this.Password + " -i 80.241.209.139");
                Process.Start(startInfo);
                Application.Exit();
            }
            catch (Exception)
            {
                new Form3().ShowDialog();
            }
        }

        private string FileToMD5(string Filename)
        {
            FileStream inputStream = System.IO.File.OpenRead(Filename);
            byte[] buffer = new MD5CryptoServiceProvider().ComputeHash(inputStream);
            inputStream.Close();
            return BitConverter.ToString(buffer).Replace("-", "").ToLower();
        }

        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            Form1 owner = (Form1) base.Owner;
            this.Username = owner.Username;
            this.Password = owner.Password;
        }

        private void Form2_Shown(object sender, EventArgs e)
        {
            new Thread(new ThreadStart(this.DoFileSec)).Start();
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(Form2));
            this.progressBar1 = new ProgressBar();
            this.progressBar2 = new ProgressBar();
            this.label1 = new Label();
            this.label2 = new Label();
            base.SuspendLayout();
            this.progressBar1.Location = new Point(12, 70);
            this.progressBar1.Name = "progressBar1";
            this.progressBar1.Size = new Size(310, 10);
            this.progressBar1.Step = 1;
            this.progressBar1.TabIndex = 0;
            this.progressBar1.Tag = "";
            this.progressBar2.Location = new Point(12, 0x2f);
            this.progressBar2.Name = "progressBar2";
            this.progressBar2.Size = new Size(310, 10);
            this.progressBar2.Step = 1;
            this.progressBar2.TabIndex = 1;
            this.label1.AutoSize = true;
            this.label1.BackColor = Color.Transparent;
            this.label1.Font = new Font("Microsoft Sans Serif", 6f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.label1.ForeColor = Color.White;
            this.label1.Location = new Point(250, 0x3a);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x48, 9);
            this.label1.TabIndex = 2;
            this.label1.Text = "Update FileSecurity";
            this.label2.AutoSize = true;
            this.label2.BackColor = Color.Transparent;
            this.label2.Font = new Font("Microsoft Sans Serif", 6f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.label2.ForeColor = Color.White;
            this.label2.Location = new Point(12, 60);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x2e, 9);
            this.label2.TabIndex = 3;
            this.label2.Text = "Check Files";
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            this.BackgroundImage = (Image) manager.GetObject("$this.BackgroundImage");
            base.ClientSize = new Size(0x14e, 0x52);
            base.Controls.Add(this.label2);
            base.Controls.Add(this.label1);
            base.Controls.Add(this.progressBar2);
            base.Controls.Add(this.progressBar1);
            base.FormBorderStyle = FormBorderStyle.None;
            base.Name = "Form2";
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "CryOnline File Security";
            base.FormClosed += new FormClosedEventHandler(this.Form2_FormClosed);
            base.Load += new EventHandler(this.Form2_Load);
            base.Shown += new EventHandler(this.Form2_Shown);
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void wc2_DownloadFileCompleted(object sender, EventArgs e)
        {
            this.IsDownloaded = true;
        }

        private void wc2_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            this.progressBar1.Invoke(() => this.progressBar1.Value = e.ProgressPercentage);
        }
    }
}

