-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSelfUpdate.cs
More file actions
250 lines (211 loc) · 9.81 KB
/
Copy pathSelfUpdate.cs
File metadata and controls
250 lines (211 loc) · 9.81 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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TrexMinerGUI
{
class SelfUpdate
{
private class JsonClass
{
public string latestVersion { get; set; }
public string latestVersionURL { get; set; }
}
private readonly System.Threading.Timer TheGUITimer;
private System.Threading.Timer TheTrexTimer;
private readonly string JsonURL;
public readonly Version TheAppVersion;
private readonly string UpdateFileName;
private readonly string UpdateFolderName;
private JsonClass JsonContents { get; set; }
public bool IsTrexUpdating { get; set; }
public string TrexUpdatingTo { get; set; }
public ProcessStartInfo UpdateScript;
public int DownloadPercentage { get; set; }
public SelfUpdate()
{
UpdateFileName = "temp_update.zip";
UpdateFolderName = "temp_update";
TheAppVersion = typeof(Program).Assembly.GetName().Version;
JsonURL = "https://raw.githubusercontent.com/forumber/TrexMinerGUI_Updater-API/main/update.json";
TheGUITimer = new System.Threading.Timer((_) => CheckForGUIUpdate(), null, dueTime: TimeSpan.FromSeconds(2), period: TimeSpan.FromMinutes(1));
TheTrexTimer = null;
IsTrexUpdating = false;
TrexUpdatingTo = "";
UpdateScript = null;
DownloadPercentage = -1;
}
private void CheckForGUIUpdate()
{
try
{
string json;
using (WebClient wc = new WebClient())
{
json = wc.DownloadString(JsonURL);
}
JsonContents = JsonConvert.DeserializeObject<JsonClass>(json);
if (new Version(JsonContents.latestVersion).CompareTo(TheAppVersion) > 0)
{
UpdateGUI();
}
}
catch { }
}
private void CheckForTrexUpdate()
{
Logging.WriteLog(MethodBase.GetCurrentMethod(), "start");
if (IsTrexUpdating)
{
Logging.WriteLog(MethodBase.GetCurrentMethod(), "an update process is already running, aborting update check");
return;
}
if (!Program.TheTrexWrapper.IsRunning)
{
Logging.WriteLog(MethodBase.GetCurrentMethod(), "miner is not running, aborting update check");
return;
}
try
{
Version CurrentTrexVersion;
try
{
CurrentTrexVersion = Program.TheTrexWrapper.CurrentTrexVersion;
if (CurrentTrexVersion < (new Version("0.20.0")))
throw new Exception();
}
catch
{
Logging.WriteLog(MethodBase.GetCurrentMethod(), "failed to get current miner version information by using miner version information from miner output, trying again by checking file metadata");
CurrentTrexVersion = new Version(ExternalMethods.GetVersionInfo(Program.ExecutionPath + "t-rex.exe", "ProductVersion")[0].Item2.Split(" ")[0]);
}
Logging.WriteLog(MethodBase.GetCurrentMethod(), "CurrentTrexVersion: " + CurrentTrexVersion.ToString());
(Version LatestTrexVersion, string LatestTrexVersionDownloadURL) = GithubAPI.GetLatestTrexRelease();
Logging.WriteLog(MethodBase.GetCurrentMethod(), "LatestTrexVersion: " + LatestTrexVersion.ToString());
if (LatestTrexVersion > CurrentTrexVersion)
{
Logging.WriteLog(MethodBase.GetCurrentMethod(), "firing up update procedure");
DownloadAndInstallTrex(LatestTrexVersionDownloadURL, LatestTrexVersion);
}
}
catch (Exception TheException)
{
Logging.WriteLog(MethodBase.GetCurrentMethod(), Environment.NewLine + TheException.ToString());
}
Logging.WriteLog(MethodBase.GetCurrentMethod(), "done");
}
public void DownloadAndInstallTrex(String DownloadURL = null, Version TrexVersion = null)
{
Logging.WriteLog(MethodBase.GetCurrentMethod(), "start");
try
{
if (DownloadURL == null)
{
Logging.WriteLog(MethodBase.GetCurrentMethod(), "there are no args provided, requesting latest version");
(TrexVersion, DownloadURL) = GithubAPI.GetLatestTrexRelease();
}
if (TrexVersion == null)
{
Logging.WriteLog(MethodBase.GetCurrentMethod(), "version information is null, using TrexUpdatingTo");
Logging.WriteLog(MethodBase.GetCurrentMethod(), "TrexUpdatingTo: " + TrexUpdatingTo);
}
else
{
Logging.WriteLog(MethodBase.GetCurrentMethod(), "TrexVersion: " + TrexVersion.ToString());
TrexUpdatingTo = TrexVersion.ToString() + " (Github)";
}
Program.TheTrexWrapper.Stop();
IsTrexUpdating = true;
Program.TheMainAppContext.trayIcon.ShowBalloonTip(0, "Updating Trex...", "to version " + TrexUpdatingTo, System.Windows.Forms.ToolTipIcon.Info);
DownloadAndExtractZip(DownloadURL);
Logging.WriteLog(MethodBase.GetCurrentMethod(), "updating...");
File.Copy(Program.ExecutionPath + UpdateFolderName + @"\" + "t-rex.exe", Program.ExecutionPath + "t-rex.exe", overwrite: true);
IsTrexUpdating = false;
Logging.WriteLog(MethodBase.GetCurrentMethod(), "update has been completed, restarting miner...");
Task.Run(() => Program.TheTrexWrapper.Start());
CleanUp();
TrexUpdatingTo = "";
//Program.TheMainAppContext.trayIcon.ShowBalloonTip(0, "Trex update completed", "Miner has been restarted", System.Windows.Forms.ToolTipIcon.Info);
Logging.WriteLog(MethodBase.GetCurrentMethod(), "done");
}
catch (Exception TheException)
{
Logging.WriteLog(MethodBase.GetCurrentMethod(), Environment.NewLine + TheException.ToString());
}
}
private void UpdateGUI()
{
Program.TheMainAppContext.trayIcon.ShowBalloonTip(0, "Updating TrexMinerGUI...", "to version " + JsonContents.latestVersion, System.Windows.Forms.ToolTipIcon.Info);
Task.Delay(2000).Wait();
DownloadAndExtractZip(JsonContents.latestVersionURL);
UpdateScript = new ProcessStartInfo();
UpdateScript.Arguments = String.Format("/C timeout /t 5 /nobreak && xcopy \"{0}\\\" \"{1}\" /E /H /Y && start \"\" \"{2}TrexMinerGUI.exe\"",
Program.ExecutionPath + UpdateFolderName,
Program.ExecutionPath, Program.ExecutionPath);
UpdateScript.WindowStyle = ProcessWindowStyle.Hidden;
UpdateScript.CreateNoWindow = true;
UpdateScript.FileName = "cmd.exe";
Application.Exit();
}
private void DownloadAndExtractZip(string URL)
{
Logging.WriteLog(MethodBase.GetCurrentMethod(), "downloading file: " + URL);
using (var webClient = new WebClient())
{
webClient.DownloadProgressChanged += (sender, e) =>
{
double bytesIn = double.Parse(e.BytesReceived.ToString());
double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
double percentage = bytesIn / totalBytes * 100;
DownloadPercentage = int.Parse(Math.Truncate(percentage).ToString());
};
webClient.DownloadFileCompleted += (sender, e) => DownloadPercentage = -2;
webClient.DownloadFileAsync(new Uri(URL), Program.ExecutionPath + UpdateFileName);
while (DownloadPercentage != -2)
{
Task.Delay(100).Wait();
}
DownloadPercentage = -1;
}
Logging.WriteLog(MethodBase.GetCurrentMethod(), "unzipping " + Program.ExecutionPath + UpdateFileName);
System.IO.Compression.ZipFile.ExtractToDirectory(Program.ExecutionPath + UpdateFileName, Program.ExecutionPath + UpdateFolderName + @"\", overwriteFiles: true);
}
public void CleanUp()
{
Logging.WriteLog(MethodBase.GetCurrentMethod(), "cleaning...");
try
{
File.Delete(Program.ExecutionPath + UpdateFileName);
}
catch { }
try
{
Directory.Delete(Program.ExecutionPath + UpdateFolderName, recursive: true);
}
catch { }
}
public void StartTrexUpdateTimer()
{
if (TheTrexTimer == null)
TheTrexTimer = new System.Threading.Timer((_) => CheckForTrexUpdate(), null, dueTime: TimeSpan.FromSeconds(10), period: TimeSpan.FromDays(1));
else
throw new InvalidOperationException("Timer is already running");
}
public void StopTrexUpdateTimer()
{
if (TheTrexTimer != null)
{
TheTrexTimer.Dispose();
TheTrexTimer = null;
}
else
throw new InvalidOperationException("Timer is not running already");
}
}
}