-
Notifications
You must be signed in to change notification settings - Fork 7
/
Helper.cs
313 lines (286 loc) · 12.1 KB
/
Helper.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
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
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Windows.Forms;
namespace XeniaUpdater_C
{
class Helper
{
string currentExecutableFullPathName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; //Full path of the Xenia Updater executable
string currentExecutableName = System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName; //The current name of the Updater. (For the case that the user renames it)
string newExeLoc = $"C:\\Users\\{Environment.UserName}\\AppData\\Local\\Temp\\XeniaUpdater.Latest.exe"; //Full path in which you will find the newly downloaded version of the updater
string newBatLoc = $"C:\\Users\\{Environment.UserName}\\AppData\\Local\\Temp\\UpdateDownloaded.bat"; //Full path in which you will find the generated batch file used for the self updater
public Helper()
{
}
//Creates the folders needed for a given Xenia build
public void CreateFolders(string folderName)
{
try
{
Directory.CreateDirectory(folderName);
Directory.CreateDirectory($"{folderName}/LastUpdate");
}
catch (Exception e)
{
LogError(e.ToString(),"XeniaUpdaterLog.txt");
}
}
//Open the folder in which the current running instance of Xenia Updater is located.
public void OpenInstallFolder()
{
Process.Start(currentExecutableFullPathName.Replace(currentExecutableName, ""));
}
//Extracts the zip
public void ExtractBuild(string folderName, string zipName)
{
//Deletes LICENSE file because it isn't needed and also causes issues for some reason
try
{
File.Delete($"{folderName}/LICENSE");
ZipFile.ExtractToDirectory($"{folderName}/{zipName}", folderName);
File.Delete($"{folderName}/LICENSE");
}
catch (Exception e)
{
LogError(e.ToString(),"XeniaUpdaterLog.txt");
}
}
//Logs the last error the program had in a file named XeniaUpdaterLog
//This file will not infintely be written to.
void LogError(string e, string fileName)
{
if (!File.Exists(fileName))
{
File.Create(fileName);
}
double length = new System.IO.FileInfo(fileName).Length;
length = length / 1000;
byte limit = 200;
if(length > limit)
{
File.Delete(fileName);
File.AppendAllText(fileName, $"Your previous log file exceeded {limit}KB and was deleted.\n");
}
File.AppendAllText(fileName, e);
}
//Cleanup tasks to do before download and extraction of a new version
public void PreUpdateTask(string folderName, string zipName, string ExecutableName)
{
string PDBName = ExecutableName.Remove(ExecutableName.Length - 4) + ".pdb";
try
{
File.Delete($"{folderName}/LastUpdate/{zipName}");
File.Move($"{folderName}/{zipName}", $"{folderName}/LastUpdate/{zipName}");
File.Delete($"{folderName}/{ExecutableName}");
File.Delete($"{folderName}/LICENSE");
File.Delete($"{folderName}/xenia.log");
File.Delete($"{folderName}/{ExecutableName}");
File.Delete($"{folderName}/{PDBName}");
File.Delete($"{folderName}/{PDBName}");
}
catch (Exception e)
{
LogError(e.ToString(),"XeniaUpdaterLog.txt");
}
}
//Start a process
public void StartProcess(string ExecutableName, string ExeLocation)
{
try
{
Process.Start($"{ExeLocation}\\{ExecutableName}");
}
catch (Exception e)
{
MessageBox.Show($"\"{ExecutableName}.exe\" could not be started.\nThe file must be present and executable.", "Error");
LogError(e.ToString(),"XeniaUpdaterLog.txt");
}
}
//Updates XeniaUpdater
public void UpdateXeniaUpdater(string branch)
{
//If we have an internet connection
if (InternetAvailable() == true)
{
//Create instance of form 2 to pull build date from self.
Form2 f2 = new Form2();
//Pull latest version from github
System.Net.WebClient wc1 = new System.Net.WebClient();
string webData = wc1.DownloadString($"https://raw.githubusercontent.com/Chopper1337/XeniaUpdater/main/version-{branch}.txt");
wc1.Dispose();
//If current version is equal to version pulled from github
if (webData.Equals(f2.buildDate))
{
MessageBox.Show("You are on the latest version! :)\n\nYour build date matches that on GitHub","Better luck next time!");
return;
}
else
{
//Usual update process
WebClient wc = new WebClient();
wc.DownloadFileAsync(new Uri($"https://raw.githubusercontent.com/Chopper1337/XeniaUpdater/main/bin/{branch}/XeniaUpdater.exe"), newExeLoc);
wc.Dispose();
if (File.Exists(newExeLoc))
{
using (FileStream strm = File.Create(newBatLoc))
using (StreamWriter sw = new StreamWriter(strm))
{
//This is supposed to look like this.
sw.WriteLine(
$@"@echo off
rem File generated by {currentExecutableName} (XeniaUpdater), do not modify or delete :)
title Updating Xenia Updater
echo Killing {currentExecutableName}
taskkill /im {currentExecutableName}
timeout 1
echo Deleting {currentExecutableName}
del {currentExecutableFullPathName}
move {newExeLoc} {currentExecutableFullPathName}
echo XeniaUpdater has been updated under the name {currentExecutableName}.
echo This CMD window will not appear on next start and can be closed :)
{currentExecutableName}
del {newBatLoc}");
}
MessageBox.Show($"Latest version downloaded, please restart!", "Latest version downloaded :)");
}
}
}
else
{
MessageBox.Show("Internet connection not available.");
}
//This is needed for backward compatibility with previous versions of Xenia Updater
if (File.Exists("XeniaUpdater.Latest.exe"))
{
MessageBox.Show($"Latest version downloaded, please restart!", "Latest version downloaded :)");
using (FileStream strm = File.Create("UpdateDownloaded.bat"))
using (StreamWriter sw = new StreamWriter(strm))
{
//This is supposed to look like this.
sw.WriteLine(
$@"@echo off
rem File generated by {currentExecutableName} (XeniaUpdater), do not modify or delete :)
title Updating Xenia Updater
echo Killing {currentExecutableName}
taskkill /im {currentExecutableName}
timeout 1
echo Deleting {currentExecutableName}
del {currentExecutableFullPathName}
move XeniaUpdater.Latest.exe {currentExecutableFullPathName}
echo XeniaUpdater has been updated under the name {currentExecutableName}.
echo This CMD window will not appear on next start and can be closed :)
{currentExecutableName}
del UpdateDownloaded.bat");
}
}
}
//Things to do upon launch of the program
public void StartupTasks()
{
//Booleans to check for the existance of the update batch file, a newly downloaded version of the updater from GitHub and a safety switch to prevent infinte cmd.exe's being started
bool updateBatExists = File.Exists(newBatLoc);
bool updateEXEExists = File.Exists(newExeLoc);
bool updateBatExists_c = File.Exists("UpdateDownloaded.bat");
bool updateEXEExists_c = File.Exists("XeniaUpdater.Latest.exe");
bool safetySwitch = true;
if (safetySwitch)
{
//If the batch file exists and the new executable exists, start the batch file
if (updateBatExists && updateEXEExists)
{
Process.Start(newBatLoc);
safetySwitch = false;
}
//Else, if the batch file does not exist but the updated executable does, delete the executable.
//Reason behind this being that this executable is useless without the batch file.
else if (!updateBatExists && updateEXEExists)
{
File.Delete(newExeLoc);
}
else if (updateBatExists && !updateEXEExists)
{
File.Delete(newBatLoc);
}
// For people using previous builds:
//If the batch file exists and the new executable exists, start the batch file
if (updateBatExists_c && updateEXEExists_c)
{
Process.Start("UpdateDownloaded.bat");
safetySwitch = false;
}
//Else, if the batch file does not exist but the updated executable does, delete the executable.
//Reason behind this being that this executable is useless without the batch file.
else if (!updateBatExists_c && updateEXEExists_c)
{
File.Delete("XeniaUpdater.Latest.exe");
}
else if (updateBatExists_c && !updateEXEExists_c)
{
File.Delete("UpdateDownloaded.bat");
}
}
//Delete log uploader, it isn't necessary to keep
if (File.Exists("UploadFile.bat"))
{
File.Delete("UploadFile.bat");
File.Delete("response.txt");
}
}
//Bool to check for internet connection. Pings AppVeyor as you will need to connect to their servers eventually to use this application properly.
public bool InternetAvailable()
{
try
{
using (var client = new WebClient())
using (var stream = client.OpenRead("http://appveyor.com"))
{
return true;
}
}
catch
{
return false;
}
}
//Generates batch script to upload a file to tmp.ninja using CURL.
public void UploadFile(string filename)
{
string text = $@"
@echo off
rem File generated by {currentExecutableName} (XeniaUpdater), safe to delete :)
echo Uploading log :)
curl -i -F files[]=@{filename} https://tmp.ninja/upload.php?output=text > response.txt
cls
echo Your {filename} is uploaded here:
echo.
type response.txt | findstr http
echo.
echo Highlight the URL with your mouse and right click to copy to clipboard.
echo.
echo If there is no URL displayed above, you will need to install CURL. Download it here: https://curl.se/windows/
echo.
echo Files are hosted on https://tmp.ninja/. Neither Xenia nor Xenia Updater are associated with tmp.ninja.
echo.
echo Read more here: https://tmp.ninja/faq.html
echo.
echo You can close this window.
pause";
if (File.Exists(filename))
{
using (FileStream strm = File.Create("UploadFile.bat"))
using (StreamWriter sw = new StreamWriter(strm))
{
sw.WriteLine(text);
}
Process.Start("UploadFile.bat");
}
else
{
MessageBox.Show($"{filename} not found");
}
}
}
}