Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.Threading;
- using System.Net;
- using System.IO;
- using ICSharpCode.SharpZipLib.Zip;
- using System.Diagnostics;
- namespace UnityGameUpdater {
- public partial class UpdateForm : Form {
- public UpdateForm() {
- InitializeComponent();
- }
- bool doneChecking = false;
- string news;
- private bool downloadSucceeded;
- private void UpdateForm_Load(object sender, EventArgs e) {
- CheckColours();
- CheckForUpdates();
- }
- private void CheckForUpdates() {
- Thread thread = new Thread(new ThreadStart(delegate {
- playButton.Invoke(new MethodInvoker(delegate {
- playButton.Text = "Updating...";
- progressBar1.Visible = true;
- }));
- WebClient client = new WebClient();
- string pcID = Environment.MachineName.GetHashCode().ToString()
- + Environment.UserName.GetHashCode().ToString() + Environment.OSVersion.GetHashCode().ToString();
- Stream downloadStream = IOAccess.CanSendSpecs() ? client.OpenRead(
- string.Format(
- "http://www.sebastianlawe.com/games/SmashyBall.aspx?gameVersion={0}&launcherVersion={1}&id={2}&os={3}&monitors={4}&resolutions={5}&processors={6}&is64Bit={7}",
- IOAccess.CurrentVersion(),
- Application.ProductVersion,
- pcID,
- Environment.OSVersion,
- Screen.AllScreens.Length,
- GetScreens(),
- Environment.ProcessorCount,
- Environment.Is64BitOperatingSystem.ToString())) :
- client.OpenRead(
- string.Format(
- "http://www.sebastianlawe.com/games/SmashyBall.aspx?gameVersion={0}&launcherVersion={1}",
- IOAccess.CurrentVersion(), Application.ProductVersion));
- int version;
- using (BinaryReader reader = new BinaryReader(downloadStream)) {
- version = reader.ReadInt32();
- string launcherVersion = reader.ReadString();
- int newsCount = reader.ReadInt32();
- List<string> newsList = new List<string>();
- for (int i = 0; i < newsCount; i++) {
- newsList.Add(reader.ReadString());
- }
- newsBox.Invoke(new MethodInvoker(delegate {
- for (int i = newsCount - 1; i >= 0; i--) {
- newsBox.Text += newsList[i] + "\r\n\r\n";
- }
- }));
- long fileSize = reader.ReadInt64();
- if (Application.ProductVersion == launcherVersion) {
- if (version > IOAccess.CurrentVersion()
- || !File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Smashy Ball.exe"))) {
- FastZip zipExtractor = new FastZip();
- IOAccess.SetNewVersion(version);
- using (FileStream fileStream = new FileStream(
- Path.Combine(
- AppDomain.CurrentDomain.BaseDirectory, "game.zip"),
- FileMode.Create,
- FileAccess.Write)) {
- long totalBytesRead = 0;
- var buffer = new byte[short.MaxValue + 1];
- while (totalBytesRead < fileSize) {
- int bytesToReadThisChunk = Math.Min(buffer.Length, (int) (fileSize - totalBytesRead));
- int bytesReadThisChunk = downloadStream.Read(buffer, 0, bytesToReadThisChunk);
- fileStream.Write(buffer, 0, bytesReadThisChunk);
- totalBytesRead += bytesReadThisChunk;
- progressBar1.Invoke(new MethodInvoker(delegate {
- progressBar1.Maximum = (int) fileSize;
- progressBar1.Value = (int) totalBytesRead;
- }));
- }
- if (fileStream.Length == fileSize) {
- progressBar1.Invoke(new MethodInvoker(delegate {
- downloadSucceeded = true;
- progressBar1.Visible = false;
- }));
- }
- }
- if (downloadSucceeded) {
- zipExtractor.ExtractZip(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "game.zip"),
- AppDomain.CurrentDomain.BaseDirectory, "");
- File.Delete(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "game.zip"));
- } else {
- MessageBox.Show("Download failed.");
- }
- }
- doneChecking = true;
- } else {
- using (FileStream fileStream = new FileStream(
- Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "nl.exe"),
- FileMode.Create, FileAccess.Write)) {
- using (FileStream fileStream2 = new FileStream(
- Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tl.tmp"),
- FileMode.Create, FileAccess.Write)) {
- long totalBytesRead = 0;
- var buffer = new byte[short.MaxValue + 1];
- while (totalBytesRead < fileSize) {
- int bytesToReadThisChunk = Math.Min(buffer.Length, (int) (fileSize - totalBytesRead));
- int bytesReadThisChunk = downloadStream.Read(buffer, 0, bytesToReadThisChunk);
- fileStream.Write(buffer, 0, bytesReadThisChunk);
- fileStream2.Write(buffer, 0, bytesReadThisChunk);
- totalBytesRead += bytesReadThisChunk;
- progressBar1.Invoke(new MethodInvoker(delegate {
- progressBar1.Maximum = (int) fileSize;
- progressBar1.Value = (int) totalBytesRead;
- }));
- }
- if (fileStream.Length == fileSize && fileStream2.Length == fileSize) {
- downloadSucceeded = true;
- progressBar1.Invoke(new MethodInvoker(delegate {
- progressBar1.Visible = false;
- }));
- }
- }
- }
- if (downloadSucceeded) {
- this.Invoke(new MethodInvoker(delegate {
- Process.Start(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "nl.exe"));
- this.Close();
- }));
- }
- }
- }
- }));
- thread.Start();
- }
- private string GetScreens() {
- string screens = "";
- for (int i = 0; i < Screen.AllScreens.Length; i++) {
- screens += Screen.AllScreens[i].Bounds.Width + "x" + Screen.AllScreens[i].Bounds.Height;
- if (i > 1 && i < Screen.AllScreens.Length - 1) {
- screens += "-";
- }
- }
- return screens;
- }
- private Color GetPositionColour(Point location, Size size) {
- if (BackgroundImage != null) {
- Bitmap image = new Bitmap(BackgroundImage, this.Size);
- return image.GetPixel(location.X + size.Width / 2, (location.Y + size.Height / 2));
- }
- return Color.Empty;
- }
- private void CheckColours() {
- foreach (Control control in Controls) {
- //FormBorderStyle = FormBorderStyle.None;
- control.BackColor = GetPositionColour(control.Location, control.Size);
- control.BackgroundImage = GetPositionImage(control.Location, control.Size, this.BackgroundImage);
- //FormBorderStyle = FormBorderStyle.FixedToolWindow;
- }
- }
- private Bitmap GetPositionImage(Point point, Size size, Image image) {
- Bitmap bitmap = new Bitmap(size.Width, size.Height);
- Graphics graphics = Graphics.FromImage(bitmap);
- graphics.DrawImage(image, new Rectangle(new Point(0, 0), size), new Rectangle(point, size),
- GraphicsUnit.Pixel);
- return bitmap;
- }
- private void playButton_Click(object sender, EventArgs e) {
- if (doneChecking) {
- if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Smashy Ball.exe"))) {
- Process.Start(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Smashy Ball.exe"));
- this.Close();
- }
- }
- }
- private void settingsButton_Click(object sender, EventArgs e) {
- new SettingsForm().ShowDialog();
- }
- private void timer1_Tick(object sender, EventArgs e) {
- if (doneChecking) {
- if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Smashy Ball.exe"))) {
- playButton.Text = "Play";
- } else {
- playButton.Text = "Failed";
- }
- }
- }
- private void settingsButton_MouseEnter(object sender, EventArgs e) {
- settingsButton.BackgroundImage = GetPositionImage(settingsButton.Location, settingsButton.Size,
- Properties.Resources.SmashyBallBGHover);
- }
- private void settingsButton_MouseLeave(object sender, EventArgs e) {
- settingsButton.BackgroundImage = GetPositionImage(settingsButton.Location, settingsButton.Size,
- Properties.Resources.SmashyBallBG);
- }
- private void playButton_MouseLeave(object sender, EventArgs e) {
- playButton.BackgroundImage = GetPositionImage(playButton.Location, playButton.Size,
- Properties.Resources.SmashyBallBG);
- }
- private void playButton_MouseEnter(object sender, EventArgs e) {
- playButton.BackgroundImage = GetPositionImage(playButton.Location, playButton.Size,
- Properties.Resources.SmashyBallBGHover);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment