-
Notifications
You must be signed in to change notification settings - Fork 1
/
UpdaterForm.cs
344 lines (290 loc) · 13.3 KB
/
UpdaterForm.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
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
using Ionic.Zip;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows.Forms;
namespace Updater
{
public partial class Updater : Form
{
private string version;
private WebClient webClient;
public static string destinationPath = Application.StartupPath;
public static string tempDir = Path.Combine(destinationPath, @"Pending");
private bool finished = false;
public Updater(string newVersion)
{
InitializeComponent();
version = newVersion;
label.Text = VersionCheck.modeText;
label.Location = new Point(Width / 2 - label.Width / 2, label.Location.Y);
TopMost = true;
}
private void Btn_yes_MouseClick(object sender, MouseEventArgs e)
{
try
{
if (finished)
{
Invoke(new Action(delegate
{
Close();
}));
return;
}
DownloadReleaseZip();//A decommenter
//WebClient_DownloadFileCompleted(null, null);//A supprimer
btn_yes.Visible = false;
btn_no.Visible = false;
prog_DownloadBar.Visible = true;
Process[] processes = Process.GetProcessesByName("NucleusCoop");
foreach (Process NucleusCoop in processes)
{
NucleusCoop.Kill();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Close();
}
}
private void DownloadReleaseZip()
{
DeleteTemp();
if (!Directory.Exists(tempDir))
{
Directory.CreateDirectory(tempDir);
}
if (Directory.Exists(tempDir))///Will download and extract in the previously created "Pending" folder.
{
using (webClient = new WebClient())
{
webClient.DownloadProgressChanged += Wc_DownloadProgressChanged;
webClient.DownloadFileAsync(
new System.Uri($@"https://github.com/SplitScreen-Me/splitscreenme-nucleus/releases/download/{version}/NucleusApp.zip"),
//new System.Uri($@"https://github.com/Mikou27/splitscreenme-nucleus/releases/download/{version}/NucleusApp.zip"),//custom url for testing purpose
Path.Combine(tempDir, @"NucleusApp.zip"));
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(WebClient_DownloadFileCompleted);
}
}
}
private void WebClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
System.Threading.Tasks.Task.Run(() =>
{
Invoke(new Action(delegate ///A decommenter
{
prog_DownloadBar.Value = 0;
label.Text = $"Extract and install Nucleus {version}";
label.Location = new Point(Width / 2 - label.Width / 2, label.Location.Y);
}));
bool isValidZip = ZipFile.CheckZip(Path.Combine(tempDir, @"NucleusApp.zip"));
if (isValidZip)
{
ZipFile zip = new ZipFile(Path.Combine(tempDir, @"NucleusApp.zip"));
zip.Password = "nucleus";
zip.ExtractAll(tempDir);
zip.ExtractExistingFile = ExtractExistingFileAction.OverwriteSilently;
zip.Dispose();
}
else
{
MessageBox.Show("Zip file doesn't exist or is corrupted.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
List<string> currentFiles = new List<string>(Directory.GetFileSystemEntries(destinationPath, "*", SearchOption.AllDirectories));
List<string> updateFiles = new List<string>(Directory.GetFileSystemEntries(tempDir, "*", SearchOption.AllDirectories));
List<string> currentFilesNames = new List<string>();
foreach (string current in currentFiles)
{
string[] fileName = current.Split('\\');
int index = fileName.Length - 1;
currentFilesNames.Add(fileName[index]);
}
int count = 0;
List<string> newFilesCheck = new List<string>();
foreach (string update in updateFiles)
{
if (update.Contains("NucleusApp.zip") || update.Contains("Updater.exe"))
{
continue;
}
if (File.Exists(update))//check if it's a file and not a directory
{
string[] fileName = update.Split('\\');
int index = fileName.Length - 1;
string updatefileName = fileName[index];
string updatefileNameNoRoot = update.Replace($"{tempDir}\\", null);
for (int i = 0; i < currentFiles.Count; i++)
{
if (currentFiles[i].Contains("Pending") || currentFiles[i].Contains("content") ||
currentFiles[i].Contains("debug-log.txt") || currentFiles[i].Contains("theme") ||
currentFiles[i].Contains("Updater.exe") || currentFiles[i].Contains("game profiles") || currentFiles[i].Contains("handlers"))
{
continue;
}
string currentFileName = currentFiles[i].Replace($"{destinationPath}\\", null);
newFilesCheck.Add(currentFileName);//clean paths list(no Pending/content etc)
if (updatefileNameNoRoot == currentFileName)
{
if (currentFileName == "Settings.ini")
{
UpdateSettingsInI(currentFiles[i], update);
continue;
}
try
{
File.Delete(currentFiles[i]);
File.Copy(update, currentFiles[i], true);
File.SetLastWriteTime(currentFiles[i], DateTime.Now);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("{0}: {1}", ex.ToString(), ex.Message) + " \n " + update + "\n " + currentFiles[i], "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
continue;
}
}
}
//check if the update zip contains new files
if (!newFilesCheck.Contains(updatefileNameNoRoot))
{
string newFilePath = destinationPath + update.Substring(update.IndexOf("\\Pending") + 8);//build destination path(install root + new file path)
string filePathNoFileName = newFilePath.Remove(newFilePath.IndexOf(updatefileName));//remove file name from path(keep folder path only in order to create the new required folders)
if (!Directory.Exists(filePathNoFileName))
{
Directory.CreateDirectory(filePathNoFileName);
}
File.Copy(update, newFilePath, true);
}
count++;
}
Invoke(new Action(delegate { prog_DownloadBar.Value = (count * 100) / updateFiles.Count; }));
}
Invoke(new Action(delegate
{
DeleteTemp();
prog_DownloadBar.Dispose();
finished = true;
btn_yes.Visible = true;
btn_yes.Text = "Exit";
btn_yes.Location = new Point(Width / 2 - btn_yes.Width / 2, Height / 2 - btn_yes.Height / 2);
label.Text = "Installation completed!";
label.Location = new Point(Width / 2 - label.Width / 2, label.Location.Y);
}));
});
}
private void UpdateSettingsInI(string currentIniPath, string updateIniPath)
{
//Current settings.ini
Dictionary<string, List<string>> currentIniFields = new Dictionary<string, List<string>>();
string[] CurrentIniContent = File.ReadAllLines(currentIniPath);
string currentField = "";
foreach (string field in CurrentIniContent)
{
if (field.StartsWith("["))
{
currentIniFields.Add(field, new List<string>());
currentField = field;
}
else
{
currentIniFields[currentField].Add(field);
}
}
//Update settings.ini
Dictionary<string, List<string>> updateIniFields = new Dictionary<string, List<string>>();
string[] UpdateIniContent = File.ReadAllLines(updateIniPath);
string updateField = "";
foreach (string field in UpdateIniContent)
{
if (field.StartsWith("["))
{
updateIniFields.Add(field, new List<string>());
updateField = field;
}
else
{
updateIniFields[updateField].Add(field);
}
}
//Will now compare the content of ini files
List<string> currentOptions = new List<string>();
foreach (KeyValuePair<string, List<string>> option in updateIniFields)
{
if (!currentIniFields.Keys.Contains(option.Key))
{
currentIniFields.Add(option.Key, option.Value);
}
else if (currentIniFields.Keys.Contains(option.Key))
{
foreach (string updateValue in updateIniFields[option.Key])
{
string[] updateOptionName = updateValue.Split('=');
foreach (string s in currentIniFields[option.Key])
{
string[] currentOptionName = s.Split('=');
currentOptions.Add(currentOptionName[0]);
}
if (!currentOptions.Contains(updateOptionName[0]))
{
currentIniFields[option.Key].Add(updateValue);
}
}
}
}
//Will now write the updated ini file
List<string> finalIniContent = new List<string>();
foreach (KeyValuePair<string, List<string>> option in currentIniFields)
{
finalIniContent.Add(option.Key);
foreach (string val in option.Value)
{
finalIniContent.Add(val);
}
}
File.WriteAllLines(Path.Combine(destinationPath, @"Settings.ini"), finalIniContent);
// File.WriteAllLines(Path.Combine(destinationPath, @"test.ini"), finalIniContent);
}
private void Wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
prog_DownloadBar.Value = e.ProgressPercentage;
label.Text = e.ProgressPercentage + "%";
label.Location = new Point((prog_DownloadBar.Location.X + (prog_DownloadBar.Width / 2)) - label.Width / 2, label.Location.Y);
}
private void Btn_no_MouseClick(object sender, MouseEventArgs e)
{
DeleteTemp();
Close();
}
private void DeleteTemp()
{
if (destinationPath == null || destinationPath == string.Empty)
{
return;
}
try
{
webClient?.CancelAsync();
if(Directory.Exists(tempDir))
Directory.Delete(tempDir, true);//A decommenter
}
catch /*(Exception ex)*/
{
//MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void Updater_FormClosing(object sender, FormClosingEventArgs e)
{
DeleteTemp();
}
private void Label_changelog_Click(object sender, EventArgs e)
{
Process.Start($@"https://github.com/SplitScreen-Me/splitscreenme-nucleus/releases/tag/{version}");
}
}
}