SebastianLawe

Untitled

Jun 9th, 2015
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10. using System.Net;
  11. using System.IO;
  12. using ICSharpCode.SharpZipLib.Zip;
  13. using System.Diagnostics;
  14.  
  15. namespace UnityGameUpdater {
  16.     public partial class UpdateForm : Form {
  17.         public UpdateForm() {
  18.             InitializeComponent();
  19.         }
  20.         bool doneChecking = false;
  21.         string news;
  22.         private bool downloadSucceeded;
  23.         private void UpdateForm_Load(object sender, EventArgs e) {
  24.             CheckColours();
  25.             CheckForUpdates();
  26.         }
  27.  
  28.         private void CheckForUpdates() {
  29.             Thread thread = new Thread(new ThreadStart(delegate {
  30.                 playButton.Invoke(new MethodInvoker(delegate {
  31.                     playButton.Text = "Updating...";
  32.                     progressBar1.Visible = true;
  33.                 }));
  34.                 WebClient client = new WebClient();
  35.                 string pcID = Environment.MachineName.GetHashCode().ToString()
  36.                     + Environment.UserName.GetHashCode().ToString() + Environment.OSVersion.GetHashCode().ToString();
  37.                 Stream downloadStream = IOAccess.CanSendSpecs() ? client.OpenRead(
  38.                     string.Format(
  39.                     "http://www.sebastianlawe.com/games/SmashyBall.aspx?gameVersion={0}&launcherVersion={1}&id={2}&os={3}&monitors={4}&resolutions={5}&processors={6}&is64Bit={7}",
  40.                     IOAccess.CurrentVersion(),
  41.                     Application.ProductVersion,
  42.                     pcID,
  43.                     Environment.OSVersion,
  44.                     Screen.AllScreens.Length,
  45.                     GetScreens(),
  46.                     Environment.ProcessorCount,
  47.                     Environment.Is64BitOperatingSystem.ToString())) :
  48.                     client.OpenRead(
  49.                     string.Format(
  50.                     "http://www.sebastianlawe.com/games/SmashyBall.aspx?gameVersion={0}&launcherVersion={1}",
  51.                     IOAccess.CurrentVersion(), Application.ProductVersion));
  52.                 int version;
  53.                 using (BinaryReader reader = new BinaryReader(downloadStream)) {
  54.                     version = reader.ReadInt32();
  55.                     string launcherVersion = reader.ReadString();
  56.                     int newsCount = reader.ReadInt32();
  57.                     List<string> newsList = new List<string>();
  58.                     for (int i = 0; i < newsCount; i++) {
  59.                         newsList.Add(reader.ReadString());
  60.                     }
  61.                     newsBox.Invoke(new MethodInvoker(delegate {
  62.                         for (int i = newsCount - 1; i >= 0; i--) {
  63.                             newsBox.Text += newsList[i] + "\r\n\r\n";
  64.                         }
  65.                     }));
  66.                     long fileSize = reader.ReadInt64();
  67.                     if (Application.ProductVersion == launcherVersion) {
  68.                         if (version > IOAccess.CurrentVersion()
  69.                             || !File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Smashy Ball.exe"))) {
  70.                             FastZip zipExtractor = new FastZip();
  71.                             IOAccess.SetNewVersion(version);
  72.                             using (FileStream fileStream = new FileStream(
  73.                                 Path.Combine(
  74.                                 AppDomain.CurrentDomain.BaseDirectory, "game.zip"),
  75.                                 FileMode.Create,
  76.                                 FileAccess.Write)) {
  77.                                 long totalBytesRead = 0;
  78.                                 var buffer = new byte[short.MaxValue + 1];
  79.  
  80.                                 while (totalBytesRead < fileSize) {
  81.                                     int bytesToReadThisChunk = Math.Min(buffer.Length, (int) (fileSize - totalBytesRead));
  82.                                     int bytesReadThisChunk = downloadStream.Read(buffer, 0, bytesToReadThisChunk);
  83.                                     fileStream.Write(buffer, 0, bytesReadThisChunk);
  84.                                     totalBytesRead += bytesReadThisChunk;
  85.                                     progressBar1.Invoke(new MethodInvoker(delegate {
  86.                                         progressBar1.Maximum = (int) fileSize;
  87.                                         progressBar1.Value = (int) totalBytesRead;
  88.                                     }));
  89.                                 }
  90.                                 if (fileStream.Length == fileSize) {
  91.                                     progressBar1.Invoke(new MethodInvoker(delegate {
  92.                                         downloadSucceeded = true;
  93.                                         progressBar1.Visible = false;
  94.                                     }));
  95.                                 }
  96.                             }
  97.                             if (downloadSucceeded) {
  98.                                 zipExtractor.ExtractZip(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "game.zip"),
  99.                                     AppDomain.CurrentDomain.BaseDirectory, "");
  100.                                 File.Delete(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "game.zip"));
  101.                             } else {
  102.                                 MessageBox.Show("Download failed.");
  103.                             }
  104.                         }
  105.                         doneChecking = true;
  106.                     } else {
  107.                         using (FileStream fileStream = new FileStream(
  108.                             Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "nl.exe"),
  109.                             FileMode.Create, FileAccess.Write)) {
  110.                             using (FileStream fileStream2 = new FileStream(
  111.                                 Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tl.tmp"),
  112.                                 FileMode.Create, FileAccess.Write)) {
  113.                                 long totalBytesRead = 0;
  114.                                 var buffer = new byte[short.MaxValue + 1];
  115.                                 while (totalBytesRead < fileSize) {
  116.                                     int bytesToReadThisChunk = Math.Min(buffer.Length, (int) (fileSize - totalBytesRead));
  117.                                     int bytesReadThisChunk = downloadStream.Read(buffer, 0, bytesToReadThisChunk);
  118.                                     fileStream.Write(buffer, 0, bytesReadThisChunk);
  119.                                     fileStream2.Write(buffer, 0, bytesReadThisChunk);
  120.                                     totalBytesRead += bytesReadThisChunk;
  121.                                     progressBar1.Invoke(new MethodInvoker(delegate {
  122.                                         progressBar1.Maximum = (int) fileSize;
  123.                                         progressBar1.Value = (int) totalBytesRead;
  124.                                     }));
  125.                                 }
  126.                                 if (fileStream.Length == fileSize && fileStream2.Length == fileSize) {
  127.                                     downloadSucceeded = true;
  128.                                     progressBar1.Invoke(new MethodInvoker(delegate {
  129.                                         progressBar1.Visible = false;
  130.                                     }));
  131.                                 }
  132.                             }
  133.                         }
  134.                         if (downloadSucceeded) {
  135.                             this.Invoke(new MethodInvoker(delegate {
  136.                                 Process.Start(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "nl.exe"));
  137.                                 this.Close();
  138.                             }));
  139.                         }
  140.                     }
  141.                 }
  142.             }));
  143.             thread.Start();
  144.         }
  145.  
  146.         private string GetScreens() {
  147.             string screens = "";
  148.             for (int i = 0; i < Screen.AllScreens.Length; i++) {
  149.                 screens += Screen.AllScreens[i].Bounds.Width + "x" + Screen.AllScreens[i].Bounds.Height;
  150.                 if (i > 1 && i < Screen.AllScreens.Length - 1) {
  151.                     screens += "-";
  152.                 }
  153.             }
  154.             return screens;
  155.         }
  156.  
  157.         private Color GetPositionColour(Point location, Size size) {
  158.             if (BackgroundImage != null) {
  159.                 Bitmap image = new Bitmap(BackgroundImage, this.Size);
  160.                 return image.GetPixel(location.X + size.Width / 2, (location.Y + size.Height / 2));
  161.             }
  162.             return Color.Empty;
  163.         }
  164.  
  165.         private void CheckColours() {
  166.             foreach (Control control in Controls) {
  167.                 //FormBorderStyle = FormBorderStyle.None;
  168.                 control.BackColor = GetPositionColour(control.Location, control.Size);
  169.                 control.BackgroundImage = GetPositionImage(control.Location, control.Size, this.BackgroundImage);
  170.                 //FormBorderStyle = FormBorderStyle.FixedToolWindow;
  171.             }
  172.         }
  173.  
  174.         private Bitmap GetPositionImage(Point point, Size size, Image image) {
  175.             Bitmap bitmap = new Bitmap(size.Width, size.Height);
  176.             Graphics graphics = Graphics.FromImage(bitmap);
  177.             graphics.DrawImage(image, new Rectangle(new Point(0, 0), size), new Rectangle(point, size),
  178.                 GraphicsUnit.Pixel);
  179.             return bitmap;
  180.         }
  181.  
  182.         private void playButton_Click(object sender, EventArgs e) {
  183.             if (doneChecking) {
  184.                 if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Smashy Ball.exe"))) {
  185.                     Process.Start(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Smashy Ball.exe"));
  186.                     this.Close();
  187.                 }
  188.             }
  189.         }
  190.  
  191.         private void settingsButton_Click(object sender, EventArgs e) {
  192.             new SettingsForm().ShowDialog();
  193.         }
  194.  
  195.         private void timer1_Tick(object sender, EventArgs e) {
  196.             if (doneChecking) {
  197.                 if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Smashy Ball.exe"))) {
  198.                     playButton.Text = "Play";
  199.                 } else {
  200.                     playButton.Text = "Failed";
  201.                 }
  202.             }
  203.         }
  204.  
  205.         private void settingsButton_MouseEnter(object sender, EventArgs e) {
  206.             settingsButton.BackgroundImage = GetPositionImage(settingsButton.Location, settingsButton.Size,
  207.                 Properties.Resources.SmashyBallBGHover);
  208.         }
  209.  
  210.         private void settingsButton_MouseLeave(object sender, EventArgs e) {
  211.             settingsButton.BackgroundImage = GetPositionImage(settingsButton.Location, settingsButton.Size,
  212.                 Properties.Resources.SmashyBallBG);
  213.         }
  214.  
  215.         private void playButton_MouseLeave(object sender, EventArgs e) {
  216.             playButton.BackgroundImage = GetPositionImage(playButton.Location, playButton.Size,
  217.                 Properties.Resources.SmashyBallBG);
  218.         }
  219.  
  220.         private void playButton_MouseEnter(object sender, EventArgs e) {
  221.             playButton.BackgroundImage = GetPositionImage(playButton.Location, playButton.Size,
  222.                 Properties.Resources.SmashyBallBGHover);
  223.         }
  224.     }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment