Repository navigation
Expand file tree
/
Copy pathUpdateChecker.cs
More file actions
369 lines (331 loc) · 15.7 KB
/
Copy pathUpdateChecker.cs
File metadata and controls
369 lines (331 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace IDAS_Save_System
{
public sealed class UpdateInfo
{
public Version Version { get; init; }
public string TagName { get; init; }
public string AssetName { get; init; }
public string DownloadUrl { get; init; }
}
public static class UpdateChecker
{
private const string RepoApiUrl = "https://api.github.com/repos/mhytee/idas_save_manager/releases/latest";
public const string ReleasesPageUrl = "https://github.com/mhytee/idas_save_manager/releases/latest";
// Version comes from <Version> in the csproj (stamped by CI from the git tag).
public static Version CurrentVersion =>
Normalize(Assembly.GetEntryAssembly()?.GetName().Version ?? new Version(0, 0, 0, 0));
private static string TempWorkDir => Path.Combine(Path.GetTempPath(), "IDAS_Update");
private static string LogFile => Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"IDAS_Save_Manager", "update_log.txt");
/// <summary>
/// Checks GitHub for a newer release. Never throws and never shows UI —
/// safe to fire in the background at startup. Returns null when up to date
/// or on any failure (offline, rate-limited, unparseable tag, no zip asset).
/// </summary>
public static async Task<UpdateInfo> CheckForUpdateAsync()
{
try
{
using HttpClient client = NewClient(TimeSpan.FromSeconds(10));
string response = await client.GetStringAsync(RepoApiUrl);
using JsonDocument doc = JsonDocument.Parse(response);
string tag = doc.RootElement.GetProperty("tag_name").GetString() ?? "";
if (!TryParseVersion(tag, out Version remote))
{
Log($"Update check skipped: could not parse release tag '{tag}'.");
return null;
}
if (remote <= CurrentVersion)
return null;
// Pick the .zip asset deliberately; a release may also carry
// checksums, readmes, or nothing at all.
foreach (JsonElement asset in doc.RootElement.GetProperty("assets").EnumerateArray())
{
string name = asset.GetProperty("name").GetString() ?? "";
if (!name.EndsWith(".zip", StringComparison.OrdinalIgnoreCase))
continue;
return new UpdateInfo
{
Version = remote,
TagName = tag,
AssetName = name,
DownloadUrl = asset.GetProperty("browser_download_url").GetString()
};
}
Log($"Update check: release {tag} has no .zip asset.");
return null;
}
catch (Exception ex)
{
Log("Update check failed", ex);
return null;
}
}
/// <summary>
/// Downloads and unpacks the update, returning the path to the new exe
/// (still in the temp folder — nothing is installed yet). Shows its own
/// error dialog and returns null on failure. Call from the UI thread.
/// </summary>
public static async Task<string> DownloadUpdateAsync(UpdateInfo update, IProgress<string> progress, IWin32Window owner)
{
try
{
if (Directory.Exists(TempWorkDir))
Directory.Delete(TempWorkDir, true);
Directory.CreateDirectory(TempWorkDir);
string zipPath = Path.Combine(TempWorkDir, update.AssetName);
// Overall time is unlimited (the zip is ~70 MB and connection speeds
// vary wildly), but 60 seconds with no data at all means the
// connection is dead — the stall guard is re-armed after every read.
using (HttpClient client = NewClient(Timeout.InfiniteTimeSpan))
using (var stallGuard = new CancellationTokenSource(TimeSpan.FromSeconds(60)))
using (HttpResponseMessage response = await client.GetAsync(update.DownloadUrl, HttpCompletionOption.ResponseHeadersRead, stallGuard.Token))
{
response.EnsureSuccessStatusCode();
long? totalBytes = response.Content.Headers.ContentLength;
await using Stream source = await response.Content.ReadAsStreamAsync(stallGuard.Token);
await using var target = new FileStream(zipPath, FileMode.Create, FileAccess.Write, FileShare.None);
byte[] buffer = new byte[81920];
long done = 0;
int read;
while ((read = await source.ReadAsync(buffer, stallGuard.Token)) > 0)
{
stallGuard.CancelAfter(TimeSpan.FromSeconds(60));
await target.WriteAsync(buffer.AsMemory(0, read), stallGuard.Token);
done += read;
// Kept short: the reporting button sits in a narrow top-right corner.
progress?.Report(totalBytes > 0
? $"{done * 100 / totalBytes.Value}%"
: $"{done / (1024 * 1024)} MB");
}
}
// A captive portal or interception proxy can return HTML with status 200.
if (!IsZipFile(zipPath))
throw new InvalidOperationException(
"The downloaded file is not a valid update package. A network sign-in page may have interfered; please try again.");
progress?.Report("Unpacking…");
string extractDir = Path.Combine(TempWorkDir, "extracted");
await Task.Run(() => ZipFile.ExtractToDirectory(zipPath, extractDir));
// Release zips have shipped both flat and inside a wrapper folder —
// search the whole tree. Largest exe wins in case a helper tool tags along.
string newExe = Directory.GetFiles(extractDir, "*.exe", SearchOption.AllDirectories)
.OrderByDescending(f => new FileInfo(f).Length)
.FirstOrDefault();
if (newExe == null)
throw new InvalidOperationException("No .exe was found inside the downloaded zip.");
return newExe;
}
catch (OperationCanceledException)
{
Log($"Download of {update.TagName} stalled.");
MessageBox.Show(owner,
"The download stalled (no data arrived for 60 seconds).\nCheck your connection and try again.",
"Update Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
catch (Exception ex)
{
Log($"Download of {update.TagName} failed", ex);
MessageBox.Show(owner,
$"The update could not be downloaded:\n{ex.Message}\n\n" +
$"You can download it manually from:\n{ReleasesPageUrl}",
"Update Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
/// <summary>
/// Swaps the running exe for the downloaded one and starts the new version.
/// Shows its own error dialogs. Returns true when the swap is committed —
/// even if the relaunch itself fails, the update is installed at that point
/// and the caller should close the application.
/// </summary>
public static bool InstallAndRestart(string newExe, IWin32Window owner)
{
string currentExe = Environment.ProcessPath;
if (currentExe == null)
{
MessageBox.Show(owner, "Could not determine the running exe's path.",
"Update Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
string stagedExe = currentExe + ".new";
string parkedExe = currentExe + ".old";
try
{
// Copy into the app folder first: proves the folder is writable
// before the running exe is touched at all.
File.Copy(newExe, stagedExe, true);
// Windows allows renaming a running exe, so swap via same-folder
// renames: park the current one, promote the staged one.
if (File.Exists(parkedExe))
File.Delete(parkedExe);
File.Move(currentExe, parkedExe);
try
{
File.Move(stagedExe, currentExe, true);
}
catch (Exception promoteError)
{
try
{
// Copy, not move: overwrite-tolerant, and .old stays behind
// as a second safety net until the next startup cleanup.
File.Copy(parkedExe, currentExe, true);
}
catch (Exception rollbackError)
{
// Worst case: nothing left at the canonical path. Tell the
// user exactly how to recover, because the app can't.
Log("Rollback after failed promote also failed", rollbackError);
Log("Original promote error", promoteError);
MessageBox.Show(owner,
"The update failed and the app could not restore itself.\n\n" +
"To fix it manually: in the app's folder, rename\n" +
$"\"{Path.GetFileName(parkedExe)}\" back to \"{Path.GetFileName(currentExe)}\"\n\n" +
$"Original error: {promoteError.Message}",
"Update Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
throw; // rethrow the promote error — the rollback restored the app
}
}
catch (Exception ex)
{
Log($"Update install failed", ex);
MessageBox.Show(owner,
$"The update could not be installed:\n{ex.Message}\n\n" +
$"You can download it manually from:\n{ReleasesPageUrl}",
"Update Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
// The swap is committed — from here on the update has succeeded even
// if the relaunch doesn't, so never report failure past this point.
try
{
Process.Start(new ProcessStartInfo
{
FileName = currentExe,
WorkingDirectory = Path.GetDirectoryName(currentExe),
UseShellExecute = true
});
Log($"Updated and restarted.");
}
catch (Exception ex)
{
Log("Relaunch after successful update failed", ex);
MessageBox.Show(owner,
"The update was installed, but the app could not restart itself.\nPlease reopen it manually.",
"Update Installed", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
return true;
}
/// <summary>
/// Removes leftovers from a completed update (the parked .old exe, temp files)
/// and debris from the old bat-based updater. Never throws — fire and forget
/// from a background thread at startup.
/// </summary>
public static void CleanupAfterUpdate()
{
string exe = Environment.ProcessPath;
if (exe != null)
{
// The replaced exe may still be shutting down; retry briefly.
TryDeleteWithRetry(exe + ".old");
TryDelete(() => File.Delete(exe + ".new"));
}
TryDelete(() => Directory.Delete(TempWorkDir, true));
// Debris from the pre-1.0.2 updater: extraction folder, bat script,
// and any downloaded release zip sitting loose in the temp root.
TryDelete(() => Directory.Delete(Path.Combine(Path.GetTempPath(), "IDAS_Extracted_Update"), true));
TryDelete(() => File.Delete(Path.Combine(Path.GetTempPath(), "IDAS_Update.bat")));
TryDelete(() =>
{
foreach (string zip in Directory.GetFiles(Path.GetTempPath(), "IDAS_Save_Manager*.zip"))
TryDelete(() => File.Delete(zip));
});
}
private static void TryDeleteWithRetry(string path)
{
for (int i = 0; i < 5 && File.Exists(path); i++)
{
try
{
File.Delete(path);
return;
}
catch (IOException) { Thread.Sleep(300); }
catch (UnauthorizedAccessException) { Thread.Sleep(300); }
}
}
private static void TryDelete(Action delete)
{
try { delete(); }
catch { }
}
private static bool IsZipFile(string path)
{
try
{
using FileStream fs = File.OpenRead(path);
return fs.ReadByte() == 'P' && fs.ReadByte() == 'K';
}
catch
{
return false;
}
}
private static HttpClient NewClient(TimeSpan timeout)
{
var client = new HttpClient { Timeout = timeout };
client.DefaultRequestHeaders.UserAgent.ParseAdd("IDAS_Save_Manager");
client.DefaultRequestHeaders.Accept.ParseAdd("application/vnd.github+json");
client.DefaultRequestHeaders.Add("X-GitHub-Api-Version", "2022-11-28");
return client;
}
private static bool TryParseVersion(string tag, out Version version)
{
version = null;
string s = (tag ?? "").Trim().TrimStart('v', 'V');
// Tolerate suffixes like "1.0.2-beta" or "1.0.2 hotfix".
int cut = s.IndexOfAny(new[] { '-', '+', ' ' });
if (cut >= 0)
s = s.Substring(0, cut);
if (!s.Contains('.'))
s += ".0"; // Version.TryParse rejects a bare major like "2"
if (!Version.TryParse(s, out Version parsed))
return false;
version = Normalize(parsed);
return true;
}
// Pad to four components so "1.0.2" and "1.0.2.0" compare as equal.
private static Version Normalize(Version v) =>
new Version(v.Major, Math.Max(v.Minor, 0), Math.Max(v.Build, 0), Math.Max(v.Revision, 0));
private static void Log(string message, Exception ex = null)
{
try
{
Directory.CreateDirectory(Path.GetDirectoryName(LogFile));
if (File.Exists(LogFile) && new FileInfo(LogFile).Length > 128 * 1024)
File.Delete(LogFile);
File.AppendAllText(LogFile,
$"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}{(ex == null ? "" : ": " + ex)}\r\n");
}
catch
{
// Logging must never take the app down.
}
}
}
}