-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCheckUpdates.cs
170 lines (143 loc) · 5.53 KB
/
CheckUpdates.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
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
using Octokit;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace dmc3music
{
public partial class CheckUpdates : Form
{
public CheckUpdates()
{
InitializeComponent();
}
private void RecenterLabel1()
{
label1.Left = ((Width - label1.Width) / 2) - 10;
}
private async void CheckUpdates_Load(object sender, EventArgs e)
{
try
{
await CheckGitHubNewerVersion();
}
catch
{
label1.Text = "Could not check for updates";
}
RecenterLabel1();
}
private static readonly WebClient wc = new WebClient();
private static readonly ManualResetEvent handle = new ManualResetEvent(true);
public IReadOnlyList<ReleaseAsset> latestAsset { get; set; }
public string releaseZip { get; set; } = "updatetmp.zip";
private void ReplaceInstance()
{
try
{
string extractPath = @".\extract";
if (Directory.Exists(extractPath))
{
Directory.Delete(extractPath, true);
}
ZipFile.ExtractToDirectory(releaseZip, extractPath);
string updatePath = Path.GetFullPath("./extract/dmc3tools/");
string currentPath = System.AppDomain.CurrentDomain.BaseDirectory;
string updateBat = $@":start
tasklist /FI ""IMAGENAME eq dmc3tools.exe"" /fo csv 2>NUL | find /I ""dmc3tools.exe"">NUL
if ""%ERRORLEVEL%"" == ""0"" goto start
xcopy /e /k /h /i /Y ""{updatePath}"" ""{currentPath}""
start dmc3tools.exe
@RD /S /Q extract
del ""{releaseZip}""
(goto) 2>nul & del ""%~f0""";
File.WriteAllText("update.bat", updateBat);
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = "update.bat";
p.Start();
System.Windows.Forms.Application.Exit();
}
catch { }
}
private void WcOnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null)
{
label1.Text = "Download complete";
RecenterLabel1();
progressBar1.Visible = false;
button1.Visible = false;
button2.Visible = true;
}
handle.Set();
}
public string ReadableSize(long fileSizeInBytes)
{
string[] suffixes = { "B", "KiB", "MiB" };
double sizeResult = fileSizeInBytes * 1.0;
int suffixIndex = 0;
while (sizeResult > 1024 && suffixIndex < suffixes.Length)
{
sizeResult /= 1024;
suffixIndex++;
}
return $"{sizeResult:0.00} {suffixes[suffixIndex]}";
}
private void WcOnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
if (e.ProgressPercentage >= 0 && e.ProgressPercentage <= 100)
{
string readRecv = ReadableSize(e.BytesReceived);
string maxRecv = ReadableSize(e.TotalBytesToReceive);
label1.Text = $"Downloaded {readRecv} of {maxRecv} ({e.ProgressPercentage}%)";
RecenterLabel1();
progressBar1.Value = e.ProgressPercentage;
}
}
private async Task CheckGitHubNewerVersion()
{
GitHubClient client = new GitHubClient(new ProductHeaderValue("644"));
Release latestRelease = await client.Repository.Release.GetLatest(398084113);
Version latestGitHubVersion = new Version(latestRelease.TagName);
Assembly assembly = Assembly.GetExecutingAssembly();
Version localVersion = new Version(FileVersionInfo.GetVersionInfo(assembly.Location).ProductVersion);
latestAsset = await client.Repository.Release.GetAllAssets("644", "dmc3music", latestRelease.Id);
int versionComparison = localVersion.CompareTo(latestGitHubVersion);
if (versionComparison < 0)
{
label1.Text = $"New update available! (Version: {latestGitHubVersion})";
button1.Visible = true;
}
else if (versionComparison > 0)
{
label1.Text = "Somehow, you have a later version than the release";
}
else
{
label1.Text = "Already up to date!";
}
}
private void button1_Click(object sender, EventArgs e)
{
button1.Visible = false;
progressBar1.Visible = true;
wc.DownloadProgressChanged += WcOnDownloadProgressChanged;
wc.DownloadFileCompleted += WcOnDownloadFileCompleted;
wc.DownloadFileAsync(new Uri(latestAsset[0].BrowserDownloadUrl), releaseZip);
handle.WaitOne();
}
private void button2_Click(object sender, EventArgs e)
{
ReplaceInstance();
}
}
}