diff --git a/Form1.cs b/Form1.cs index d68fade..b380254 100644 --- a/Form1.cs +++ b/Form1.cs @@ -3,19 +3,21 @@ using System.Diagnostics; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; +using System.IO.Compression; using System.Net; using Newtonsoft.Json.Linq; using System.Security.Cryptography; +using Microsoft.Win32; namespace TadahLauncher { public partial class Form1 : Form { static string baseUrl = "http://tadah.rocks"; - static string version = "1.0.5"; + static string version = "1.0.6"; static string client = "2010"; - static string path = Path.GetDirectoryName(Application.ExecutablePath); static string installPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Tadah\\" + client; + static bool doSha256Check = false; public Form1() { @@ -70,40 +72,107 @@ namespace TadahLauncher // Assuming we're in the temp folder or the user knows what they're doing, start replacing files. label1.Text = "Downloading latest client..."; - WebClient client = new WebClient(); + WebClient webClient = new WebClient(); string tempZipArchivePath = Path.GetTempPath() + "Tadah" + client + ".zip"; - client.DownloadProgressChanged += (s, e) => + webClient.DownloadProgressChanged += (s, e) => { progressBar1.Value = e.ProgressPercentage; }; - client.DownloadFileCompleted += (s, e) => + webClient.DownloadFileCompleted += (s, e) => { - label1.Text = "Verifying downloaded files..."; - - SHA256 cSha256 = SHA256.Create(); - - byte[] zipArchiveSha256Bytes; - using (FileStream stream = File.OpenRead(tempZipArchivePath)) + if (doSha256Check) { - zipArchiveSha256Bytes = cSha256.ComputeHash(stream); + label1.Text = "Verifying downloaded files..."; + + SHA256 cSha256 = SHA256.Create(); + + byte[] zipArchiveSha256Bytes; + using (FileStream stream = File.OpenRead(tempZipArchivePath)) + { + zipArchiveSha256Bytes = cSha256.ComputeHash(stream); + } + + string sha256result = ""; + foreach (byte b in zipArchiveSha256Bytes) sha256result += b.ToString("x2"); + + if (sha256result != sha256) + { + MessageBox.Show("SHA256 mismatch.\nWebsite reported: " + sha256 + "\nLocal file: " + sha256result, "Update Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + Close(); + return; + } } - string sha256result = ""; - foreach (byte b in zipArchiveSha256Bytes) sha256result += b.ToString("x2"); + label1.Text = "Extracting files..."; + progressBar1.Value = 50; - if (sha256result != sha256) + try { - MessageBox.Show("SHA256 mismatch.\nWebsite reported: " + sha256 + "\nLocal file: " + sha256result, "Update Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + if (Directory.Exists(installPath)) + { + Directory.Delete(installPath, true); + } + + ZipFile.ExtractToDirectory(tempZipArchivePath, installPath); + } + catch (Exception exc) + { + MessageBox.Show("Error occurred while attempting to extract the client to its proper directory. (" + exc.Message + ") \n\n" + installPath, "Update Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Close(); return; } + + label1.Text = "Setting up URI..."; + try + { + var classesKey = Registry.CurrentUser.OpenSubKey(@"Software\Classes", true); + + var key = classesKey.CreateSubKey("tadahten"); + key.CreateSubKey("DefaultIcon").SetValue("", installPath + "\\TadahLauncher.exe,1"); + key.SetValue("", "tadahten:Protocol"); + key.SetValue("URL Protocol", ""); + key.CreateSubKey(@"shell\open\command").SetValue("", installPath + "\\TadahLauncher.exe -token %1"); + key.Close(); + } + catch + { + MessageBox.Show("Could not set up Tadah's URI. This usually happens on machines running Windows 7 or older. Tadah may not launch at all.", "URI Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + + label1.Text = "Clean up..."; + if (Directory.Exists("C:\\Tadah")) + { + DialogResult dialogResult = MessageBox.Show("Old Tadah installation folder detected. Would you like to delete the old Tadah installation folder (found at C:\\Tadah)?\n\nPlease make sure you aren't leaving anything important behind, because that data cannot be restored when you confirm you press \"Yes.\"", "Clean Up", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); + if (dialogResult == DialogResult.Yes) + { + try + { + Directory.Delete("C:\\Tadah", true); + + MessageBox.Show("Old Tadah folder deleted.", "Clean Up", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + catch + { + MessageBox.Show("Could not delete the entirety of the old installation folder. There may be files remaining, so please go clean those up yourself.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + + if (File.Exists(tempZipArchivePath)) + { + File.Delete(tempZipArchivePath); + } + + MessageBox.Show("Tadah successfully updated. Go ahead and play.", "Update Complete", MessageBoxButtons.OK, MessageBoxIcon.Information); + Close(); + return; }; try { - client.DownloadFileAsync(new Uri(baseUrl + "/client/download/" + client), tempZipArchivePath); + webClient.DownloadFileAsync(new Uri(baseUrl + "/client/download/" + client), tempZipArchivePath); } catch { @@ -158,9 +227,9 @@ namespace TadahLauncher return; } - if (!File.Exists(@path + @"\TadahApp.exe")) + if (!File.Exists(installPath + "\\TadahApp.exe")) { - MessageBox.Show("The client couldn't be found, so we are going to install it.\nCurrent Directory: " + path, "Client Missing", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show("The client couldn't be found, so we are going to install it.\nInstall Path: " + installPath, "Client Missing", MessageBoxButtons.OK, MessageBoxIcon.Error); UpdateClient(false); return; } @@ -208,7 +277,7 @@ namespace TadahLauncher { StartInfo = { - FileName = path + "\\TadahApp.exe", + FileName = installPath + "\\TadahApp.exe", Arguments = "-script dofile('" + baseUrl + "/client/join/" + args[2].Split(':')[1] + "')" } }; @@ -222,6 +291,8 @@ namespace TadahLauncher } else { + label1.Text = "New version found, updating: " + version + " -> " + versionString; + await Task.Delay(5000); UpdateClient(false); return; }