|
9 | 9 | using System.Runtime.InteropServices;
|
10 | 10 | using System.Text;
|
11 | 11 | using System.Text.RegularExpressions;
|
| 12 | +using Task = System.Threading.Tasks.Task; |
12 | 13 |
|
13 | 14 | namespace OpenTimerResolution
|
14 | 15 | {
|
@@ -126,14 +127,18 @@ public MainWindow()
|
126 | 127 | return;
|
127 | 128 | }
|
128 | 129 |
|
| 130 | +#if !DEBUG |
129 | 131 | CheckForUpdates();
|
| 132 | +#endif |
130 | 133 | }
|
131 | 134 |
|
132 | 135 | private static string GetRequest(string uri, bool jsonaccept)
|
133 | 136 | {
|
134 |
| - HttpClient client = new(); |
135 |
| - client.BaseAddress = new(uri); |
136 |
| - client.Timeout = new(0, 0, 4); |
| 137 | + HttpClient client = new() |
| 138 | + { |
| 139 | + BaseAddress = new(uri), |
| 140 | + Timeout = new(0, 0, 4) |
| 141 | + }; |
137 | 142 |
|
138 | 143 | if (jsonaccept)
|
139 | 144 | client.DefaultRequestHeaders
|
@@ -175,18 +180,52 @@ private void CheckForUpdates()
|
175 | 180 |
|
176 | 181 | if (ver != programVersion)
|
177 | 182 | {
|
178 |
| - DialogResult res = MessageBox.Show("Found a new update! Would you like to be redirected to the GitHub page?", "OpenTimerResolution", MessageBoxButtons.YesNo, MessageBoxIcon.Question); |
| 183 | + DialogResult res = MessageBox.Show("Found a new update! Would you like to install it?", "OpenTimerResolution", MessageBoxButtons.YesNo, MessageBoxIcon.Question); |
| 184 | + |
179 | 185 | switch (res)
|
180 | 186 | {
|
181 | 187 | case DialogResult.Yes:
|
182 |
| - Process.Start(new ProcessStartInfo("cmd", "/c start https://github.com/TorniX0/OpenTimerResolution/releases/latest") { CreateNoWindow = true }); |
| 188 | + |
| 189 | + DialogResult type = MessageBox.Show("Framework dependent version?", "OpenTimerResolution", MessageBoxButtons.YesNo, MessageBoxIcon.Question); |
| 190 | + |
| 191 | + string url = (type == DialogResult.Yes) ? |
| 192 | + $"https://github.com/TorniX0/OpenTimerResolution/releases/download/{obj["tag_name"]}/OpenTimerResolution.exe" : |
| 193 | + $"https://github.com/TorniX0/OpenTimerResolution/releases/download/{obj["tag_name"]}/OpenTimerResolution_s.exe"; |
| 194 | + |
| 195 | + UpdateProgram(new Uri(url)); |
183 | 196 | break;
|
184 | 197 | case DialogResult.No:
|
185 | 198 | break;
|
186 | 199 | }
|
187 | 200 | }
|
188 | 201 | }
|
189 | 202 |
|
| 203 | + private void UpdateProgram(Uri url) |
| 204 | + { |
| 205 | + FileInfo file = new(Application.ExecutablePath); |
| 206 | + var oldFile = file.DirectoryName + "\\" + file.Name.Replace(file.Extension, ".old"); |
| 207 | + File.Move(file.FullName, oldFile); |
| 208 | + |
| 209 | + var task = Task.Run(async () => await Utilities.DownloadAsync(url, file.FullName)); |
| 210 | + task.ContinueWith(x => |
| 211 | + { |
| 212 | + Process.Start(new ProcessStartInfo("cmd", $"/c taskkill /f /im {file.Name} && start {file.FullName} && takeown.exe /f \"{oldFile}\" && del \"{oldFile}\" /f /q") { CreateNoWindow = true, Verb = "runas" }); |
| 213 | + }); |
| 214 | + |
| 215 | + updateText.Visible = true; |
| 216 | + timerResolutionBox.ReadOnly = true; |
| 217 | + timerResolutionBox.Enabled = false; |
| 218 | + startButton.Enabled = false; |
| 219 | + purgeCacheButton.Enabled = false; |
| 220 | + updateConfigButton.Enabled = false; |
| 221 | + darkModeBox.Enabled = false; |
| 222 | + automaticCacheCleanBox.Enabled = false; |
| 223 | + intervalComboBox.Enabled = false; |
| 224 | + installScheduleButton.Enabled = false; |
| 225 | + logButton.Enabled = false; |
| 226 | + stopButton.Enabled = false; |
| 227 | + } |
| 228 | + |
190 | 229 | private void timerResolutionBox_TextChanged(object sender, EventArgs e)
|
191 | 230 | {
|
192 | 231 | if (timerResolutionBox.Text != string.Empty && timerResolutionBox.Text.Last() != '.')
|
@@ -476,6 +515,6 @@ private void updateConfigButton_Click(object sender, EventArgs e)
|
476 | 515 | MessageBox.Show("Config was updated successfully!", "OpenTimerResolution", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
477 | 516 | }
|
478 | 517 |
|
479 |
| - #endregion |
| 518 | +#endregion |
480 | 519 | }
|
481 | 520 | }
|
0 commit comments