|
2 | 2 | using System.Windows.Forms; |
3 | 3 | using System.Net; |
4 | 4 | using System.Diagnostics; |
| 5 | +using Newtonsoft.Json; |
| 6 | +using System; |
5 | 7 |
|
6 | 8 | namespace AssistantComputerControl { |
7 | 9 | class ACC_Updater { |
8 | | - public void Check() { |
9 | | - string latest_version; |
10 | | - using (WebClient client = new WebClient()) { |
11 | | - latest_version = client.DownloadString("http://gh.albe.pw/acc/releases/latest/version.txt"); |
| 10 | + private const string releaseJsonUrl = "http://acc.albe.pw/versions/release/latest_version.json"; |
| 11 | + private const string betaJsonUrl = "http://acc.albe.pw/versions/beta/latest_version.json"; |
| 12 | + |
| 13 | + public bool Check() { |
| 14 | + MainProgram.DoDebug("Checking for updates..."); |
| 15 | + |
| 16 | + string latestReleaseJson = null; |
| 17 | + string latestBetaJson = null; |
| 18 | + |
| 19 | + //Check and get latest |
| 20 | + if (RemoteFileExists(releaseJsonUrl)) { |
| 21 | + using (WebClient client = new WebClient()) { |
| 22 | + latestReleaseJson = client.DownloadString(releaseJsonUrl); |
| 23 | + } |
12 | 24 | } |
13 | | - if (latest_version != MainProgram.softwareVersion) { |
14 | | - //New version available |
15 | | - MainProgram.DoDebug("New software version found (" + latest_version + ")"); |
16 | | - DialogResult dialogResult = MessageBox.Show("A new version of " + MainProgram.messageBoxTitle + " is available (v" + latest_version + "), do you wish to install it?", "New update found | " + MainProgram.messageBoxTitle, MessageBoxButtons.YesNo); |
17 | | - if (dialogResult == DialogResult.Yes) { |
18 | | - MainProgram.DoDebug("User chose \"yes\" to install update"); |
19 | | - Install(); |
20 | | - } else if(dialogResult == DialogResult.No) { |
21 | | - MainProgram.DoDebug("User did not want to install update"); |
| 25 | + |
| 26 | + //Check and get beta |
| 27 | + if (Properties.Settings.Default.BetaProgram && RemoteFileExists(betaJsonUrl)) { |
| 28 | + using (WebClient client = new WebClient()) { |
| 29 | + latestBetaJson = client.DownloadString(betaJsonUrl); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + if (latestReleaseJson != null || latestBetaJson != null) { |
| 34 | + Version newVersion = null |
| 35 | + , latestRelease |
| 36 | + , latestBeta; |
| 37 | + |
| 38 | + if (latestReleaseJson != null && latestBetaJson != null) { |
| 39 | + //Beta program enabled; check both release and beta for newest update |
| 40 | + latestRelease = JsonConvert.DeserializeObject<Version>(latestReleaseJson); |
| 41 | + latestBeta = JsonConvert.DeserializeObject<Version>(latestBetaJson); |
| 42 | + |
| 43 | + if (DateTime.Parse(latestRelease.datetime) > DateTime.Parse(MainProgram.releaseDate) || DateTime.Parse(latestBeta.datetime) > DateTime.Parse(MainProgram.releaseDate)) { |
| 44 | + //Both latest release and beta is ahead of this current build |
| 45 | + if (DateTime.Parse(latestRelease.datetime) > DateTime.Parse(latestBeta.datetime)) { |
| 46 | + //Release is newest |
| 47 | + newVersion = latestRelease; |
| 48 | + } else { |
| 49 | + //Beta is newest |
| 50 | + newVersion = latestBeta; |
| 51 | + } |
| 52 | + } else { |
| 53 | + //None of them are newer. Nothing new |
| 54 | + MainProgram.DoDebug("Software up to date (beta program enabled)"); |
| 55 | + return false; |
| 56 | + } |
| 57 | + } else if (latestReleaseJson != null && latestBetaJson == null) { |
| 58 | + //Only check latest |
| 59 | + latestRelease = JsonConvert.DeserializeObject<Version>(latestReleaseJson); |
| 60 | + |
| 61 | + if (DateTime.Parse(latestRelease.datetime) > DateTime.Parse(MainProgram.releaseDate)) { |
| 62 | + //Newer build |
| 63 | + newVersion = latestRelease; |
| 64 | + } else { |
| 65 | + //Not new, move on |
| 66 | + MainProgram.DoDebug("Software up to date"); |
| 67 | + return false; |
| 68 | + } |
| 69 | + } else if (latestReleaseJson == null && latestBetaJson != null) { |
| 70 | + //Couldn't reach "latest" update, but beta-updates are enabled |
| 71 | + latestBeta = JsonConvert.DeserializeObject<Version>(latestBetaJson); |
| 72 | + |
| 73 | + if (DateTime.Parse(latestBeta.datetime) > DateTime.Parse(MainProgram.releaseDate)) { |
| 74 | + //Newer build |
| 75 | + newVersion = latestBeta; |
| 76 | + } else { |
| 77 | + //Not new, move on |
| 78 | + MainProgram.DoDebug("Software up to date (beta program enabled)"); |
| 79 | + return false; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if (newVersion != null) { |
| 84 | + //New version available |
| 85 | + MainProgram.DoDebug("New software version found (" + newVersion.version + ") [" + newVersion.type + "], current; " + MainProgram.softwareVersion); |
| 86 | + DialogResult dialogResult = MessageBox.Show("A new version of " + MainProgram.messageBoxTitle + " is available (v" + newVersion.version + " [" + newVersion.type + "]), do you wish to install it?", "New update found | " + MainProgram.messageBoxTitle, MessageBoxButtons.YesNo); |
| 87 | + if (dialogResult == DialogResult.Yes) { |
| 88 | + MainProgram.DoDebug("User chose \"yes\" to install update"); |
| 89 | + Install(newVersion.installpath); |
| 90 | + } else if (dialogResult == DialogResult.No) { |
| 91 | + MainProgram.DoDebug("User did not want to install update"); |
| 92 | + } |
| 93 | + |
| 94 | + if (File.Exists(Path.Combine(MainProgram.dataFolderLocation, "updated.txt"))) { |
| 95 | + File.Delete("updated.txt"); |
| 96 | + } |
| 97 | + return true; |
22 | 98 | } |
23 | 99 | } else { |
24 | | - MainProgram.DoDebug("Software up to date"); |
| 100 | + MainProgram.DoDebug("Could not reach the webserver (both 'release' and 'beta' json files couldn't be reached)"); |
25 | 101 | } |
26 | | - if (File.Exists(Path.Combine(MainProgram.dataFolderLocation, "updated.txt"))) { |
27 | | - File.Delete("updated.txt"); |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + private bool RemoteFileExists(string url) { |
| 106 | + try { |
| 107 | + //Creating the HttpWebRequest |
| 108 | + HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; |
| 109 | + //Setting the Request method HEAD, you can also use GET too. |
| 110 | + request.Method = "HEAD"; |
| 111 | + //Getting the Web Response. |
| 112 | + HttpWebResponse response = request.GetResponse() as HttpWebResponse; |
| 113 | + //Returns TRUE if the Status code == 200 |
| 114 | + response.Close(); |
| 115 | + return (response.StatusCode == HttpStatusCode.OK); |
| 116 | + } catch { |
| 117 | + //Any exception will returns false. |
| 118 | + return false; |
28 | 119 | } |
29 | 120 | } |
30 | 121 |
|
31 | | - public void Install() { |
| 122 | + public void Install(string installPath) { |
32 | 123 | MainProgram.DoDebug("Installing new version..."); |
33 | | - //Create file for the updater, containing the name (and full path) of the ACC-exe which is being updated |
34 | 124 | if (File.Exists(Path.Combine(MainProgram.dataFolderLocation, "updated.txt"))) { |
35 | 125 | File.Delete("updated.txt"); |
36 | 126 | } |
37 | | - if (File.Exists(Path.Combine(MainProgram.dataFolderLocation, "acc_location.txt"))) { |
38 | | - File.WriteAllText(Path.Combine(MainProgram.dataFolderLocation, "acc_location.txt"), string.Empty); |
39 | | - } |
40 | | - using (var tw = new StreamWriter(Path.Combine(MainProgram.dataFolderLocation, "acc_location.txt"), true)) { |
41 | | - tw.WriteLine(MainProgram.currentLocationFull); |
42 | | - tw.Close(); |
| 127 | + |
| 128 | + HttpWebResponse response = null; |
| 129 | + var request = (HttpWebRequest)WebRequest.Create(installPath); |
| 130 | + request.Method = "HEAD"; |
| 131 | + bool wentThrough = true; |
| 132 | + |
| 133 | + try { |
| 134 | + response = (HttpWebResponse)request.GetResponse(); |
| 135 | + } catch (WebException ex) { |
| 136 | + //A WebException will be thrown if the status of the response is not `200 OK` |
| 137 | + MainProgram.DoDebug("Failed to update, installation URL does not exist (" + installPath + "). Error;"); |
| 138 | + MainProgram.DoDebug(ex.Message); |
| 139 | + MessageBox.Show("Couldn't find the new version online. Please try again later.", "Error | " + MainProgram.messageBoxTitle); |
| 140 | + wentThrough = false; |
| 141 | + } finally { |
| 142 | + if (response != null) { |
| 143 | + response.Close(); |
| 144 | + } |
43 | 145 | } |
44 | | - string downloadLocation = Path.Combine(MainProgram.currentLocation, "ACC_installer.exe"); |
| 146 | + |
| 147 | + if (!wentThrough) |
| 148 | + return; |
| 149 | + |
| 150 | + string downloadLocation = Path.Combine(Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "Downloads"), "AssistantComputerControl installer.exe"); |
45 | 151 | using (var client = new WebClient()) { |
46 | | - client.DownloadFile("https://gh.albe.pw/acc/installer/ACC_installer.exe", downloadLocation); |
| 152 | + client.DownloadFile(installPath, downloadLocation); |
47 | 153 | } |
48 | 154 | Process.Start(downloadLocation); |
49 | 155 | MainProgram.Exit(); |
|
0 commit comments