forked from ChrisFeline/ToNSaveManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdater.cs
127 lines (103 loc) · 5.71 KB
/
Updater.cs
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
using System.Diagnostics;
using System.Runtime.InteropServices;
using ToNSaveManager.Models;
using ICSharpCode.SharpZipLib.Zip;
using ToNSaveManager.Localization;
namespace ToNSaveManager {
internal static class Updater {
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();
static string POST_UPDATE_FILE => Program.ProgramLocationTemporary;
internal static void Start(GitHubRelease release, GitHubRelease.Asset asset) {
AllocConsole();
Console.Title = "ToNSaveManager - Updating " + release.name;
string TempFileName = release.tag_name + ".temp.zip";
string TempFileLocation = Path.Combine(Program.ProgramDirectory, TempFileName);
try {
if (File.Exists(TempFileLocation))
File.Delete(TempFileLocation);
Console.WriteLine($"Downloading '{asset.name}' . . . ");
string downloadUrl = asset.browser_download_url;
using (HttpClient client = new HttpClient()) {
using (var s = client.GetStreamAsync(downloadUrl).Result) {
using (var fs = new FileStream(TempFileLocation, FileMode.CreateNew)) {
s.CopyTo(fs);
}
}
}
// Move current executable
Logger.Info("Moving from: " + Program.ProgramLocation);
Logger.Info("Moving to: " + Program.ProgramLocationTemporary);
File.Move(Program.ProgramLocation, Program.ProgramLocationTemporary, true);
Console.WriteLine("Extracting update files . . .");
Logger.Info("Extracting: " + TempFileLocation);
FastZip zip = new FastZip();
zip.ExtractZip(TempFileLocation, Program.ProgramDirectory, null);
Console.WriteLine("Finishing update . . .");
File.Delete(TempFileLocation); // .zip file cleanup
Console.WriteLine("Update complete . . .");
Program.ReleaseMutex(); // Release mutex so downloaded app opens properly
MessageBox.Show(string.Format(LANG.S("MESSAGE.UPDATE_SUCCESS") ?? "Successfully downloaded update: {0}\nOpen 'ToNSaveManager' again to continue...", release.tag_name),
Program.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.Exit();
return;
} catch (Exception ex) {
Logger.Error("Automatic update failed.");
Logger.Error(ex);
MessageBox.Show((LANG.S("MESSAGE.UPDATE_FAILED") ?? "Automatic update has failed. Try using the file 'update.bat' instead.\nPlease report this error to on the GitHub page.") + "\n\n" + ex, "Update Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (File.Exists(Program.ProgramLocationTemporary)) {
File.Move(Program.ProgramLocationTemporary, Program.ProgramLocation, true);
}
}
const string LEGACY_POST_UPDATE_ARG = "--post-update";
internal static void CheckPostUpdate(string[] args) {
bool updateLegacy = Program.ContainsArg(LEGACY_POST_UPDATE_ARG);
bool isPostUpdate = File.Exists(POST_UPDATE_FILE) || Program.ContainsArg("--clean-update");
if (!updateLegacy && !isPostUpdate) return;
Logger.Info("Running post-update cleanup.");
try {
if (updateLegacy) {
// Run legacy cleanup, old to new transition
Logger.Info("Updated from legacy version, running legacy cleanup...");
try {
string legacyTempFiles = Path.Combine(Program.ProgramDirectory, ".temp_files");
if (Directory.Exists(legacyTempFiles)) {
Logger.Info("Deleting legacy temp files: " + legacyTempFiles);
Directory.Delete(legacyTempFiles, true);
}
} catch (Exception ex) {
Logger.Error(ex);
}
try {
Logger.Info("Deleting unused legacy files.");
var unusedFiles = Directory.GetFiles(Program.ProgramDirectory)
.Where(f => f.EndsWith(".pdb") || f.EndsWith(".dll"));
foreach (string file in unusedFiles) {
try {
Logger.Info("Deleting unused file: " + file);
File.Delete(file);
} catch { }
}
} catch (Exception ex) {
Logger.Error(ex);
}
}
if (File.Exists(Program.ProgramLocationTemporary)) {
Logger.Info("Deleting old program files.");
File.Delete(Program.ProgramLocationTemporary);
}
if (File.Exists(Program.ProgramLocationTemporaryLegacy)) {
Logger.Info("Deleting old program files again.");
File.Delete(Program.ProgramLocationTemporaryLegacy);
}
Logger.Info("Post-update success. I always knew.");
} catch (Exception ex) {
Logger.Error("Failed to run post-update.");
Logger.Error(ex);
MessageBox.Show("Failed to run post-update.\nPlease report this issue on the GitHub page.\n\n" + ex, "Update Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}