-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainWindow.xaml.cs
460 lines (357 loc) · 16.3 KB
/
MainWindow.xaml.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
using System;
using System.Windows;
using MahApps.Metro.Controls;
using System.Net;
using Microsoft.Win32;
using System.IO;
using System.IO.Compression;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using System.Threading;
using System.Globalization;
using System.Text.RegularExpressions;
namespace CSCOInstaller
{
public partial class MainWindow : MetroWindow
{
string rawRepoUrl = "https://raw.githubusercontent.com/TheDoctor0/CSCOInstaller/master/";
string updateUrl = "";
string versionText = "";
string steamDirectory = "";
double latestVersion = 0.0;
double installedVersion = 0.0;
double localVersion = 1.51;
bool downloading = false;
bool update = false;
WebClient client;
public MainWindow()
{
InitializeComponent();
}
private void Window_Load(object sender, RoutedEventArgs e)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
if (!checkUpdates())
{
RegistryKey regKey = Registry.CurrentUser;
regKey = regKey.OpenSubKey(@"Software\Valve\Steam");
if (regKey != null)
{
string[] steamPath = regKey.GetValue("SourceModInstallPath").ToString().Split(new string[] { @"\steamapps\sourcemods" }, StringSplitOptions.RemoveEmptyEntries);
textBoxSteam.Text = steamPath[0];
}
checkVersion();
}
}
private void checkVersion()
{
latestVersion = 0.0;
installedVersion = 0.0;
button.IsEnabled = true;
string versionFile = String.Empty;
steamDirectory = textBoxSteam.Text + @"\steamapps\sourcemods";
try
{
using (WebClient client = new WebClient()) versionFile = client.DownloadString(rawRepoUrl + "version.txt");
}
catch (Exception)
{
System.Windows.MessageBox.Show("Unable to get version file.\nCheck your internet connection!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
if (versionFile != String.Empty)
{
double tempVersion = 0.0;
foreach (string line in versionFile.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries))
{
string[] version = line.Split();
tempVersion = Convert.ToDouble(version[0]);
if (remoteFileExists(version[1]) && tempVersion > latestVersion)
{
versionText = version[0];
updateUrl = version[1];
latestVersion = tempVersion;
}
}
}
if (Directory.Exists(steamDirectory + @"\csco"))
{
try
{
string localVersionFile = File.ReadAllText(steamDirectory + @"\csco\version.txt");
installedVersion = Convert.ToDouble(Regex.Match(localVersionFile, @"[0-9]+\.[0-9]+").Value);
labelInstalledVersion.Content = installedVersion.ToString();
}
catch (Exception)
{
MessageBoxResult result = System.Windows.MessageBox.Show("Classic Offensive installed, but version.txt was not found.\nDo you have the latest version?", "Version", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
File.WriteAllText(steamDirectory + @"\csco\version.txt", "" + versionText);
installedVersion = latestVersion;
labelInstalledVersion.Content = versionText;
}
else
{
labelInstalledVersion.Content = "Unknown";
installedVersion = -1.0;
}
}
}
else
{
labelInstalledVersion.Content = "None";
}
labelLatestVersion.Content = versionText == String.Empty ? "None" : versionText;
if (updateUrl != "")
{
if (!Directory.Exists(steamDirectory + @"\csco"))
{
button.Content = "Download";
}
else
{
if (installedVersion < latestVersion)
{
button.Content = "Update";
update = true;
}
else
{
button.Content = "No Update Required";
button.IsEnabled = false;
}
}
}
else
{
button.Content = "Update Not Possible!";
button.IsEnabled = false;
}
}
private void button_Click(object sender, RoutedEventArgs e)
{
if (File.Exists(textBoxSteam.Text + @"\Steam.exe"))
{
steamDirectory = textBoxSteam.Text + @"\steamapps\sourcemods";
IProgress<double> progress = new Progress<double>(b => progressBar.Value = (int)b);
deleteDir(steamDirectory + @"\Temp");
Directory.CreateDirectory(steamDirectory + @"\Temp");
try
{
client = new WebClient();
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloadProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(downloadFileCompleted);
client.DownloadFileAsync(new Uri(updateUrl), steamDirectory + "/Temp/csco.zip");
button.Content = "Downloading...";
button.IsEnabled = false;
textBoxSteam.IsEnabled = false;
downloading = true;
}
catch (Exception ee)
{
System.Windows.MessageBox.Show(ee.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
else
{
System.Windows.MessageBox.Show("Check path to your Steam directory!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void downloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
button.Content = "Downloading error!";
}
else if (e.Cancelled)
{
deleteDir(steamDirectory + "/Temp");
}
else
{
try
{
if (File.Exists(steamDirectory + "/csco/maps/workshop"))
{
File.SetAttributes(steamDirectory + "/csco/maps/workshop", FileAttributes.Normal);
File.Delete(steamDirectory + "/csco/maps/workshop");
}
if (Directory.Exists(steamDirectory + "/csco/maps/workshop")) Directory.Delete(steamDirectory + "/csco/maps/workshop");
deleteDir(steamDirectory + "/csco");
try
{
ZipFile.ExtractToDirectory(steamDirectory + "/Temp/csco.zip", steamDirectory + "/");
}
catch (Exception ee)
{
System.Windows.MessageBox.Show(ee.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
deleteDir(steamDirectory + "/Temp");
if (File.Exists(steamDirectory + "/HostMe.txt")) File.Delete(steamDirectory + "/HostMe.txt");
if (File.Exists(steamDirectory + "/ReadMe.txt")) File.Delete(steamDirectory + "/ReadMe.txt");
if (File.Exists(steamDirectory + "/FixedNotes.txt")) File.Delete(steamDirectory + "/FixedNotes.txt");
if (File.Exists(steamDirectory + "/csco/maps/workshop"))
{
File.SetAttributes(steamDirectory + "/csco/maps/workshop", FileAttributes.Normal);
File.Delete(steamDirectory + "/csco/maps/workshop");
}
File.WriteAllText(steamDirectory + "/csco/version.txt", "" + versionText);
System.IO.DirectoryInfo di = new DirectoryInfo(textBoxSteam.Text + "/userdata");
foreach (DirectoryInfo dir in di.GetDirectories())
{
try
{
System.IO.DirectoryInfo dii = new DirectoryInfo(textBoxSteam.Text + "/userdata/" + dir.Name + "/ugc/");
foreach (DirectoryInfo dirr in dii.GetDirectories()) dirr.Delete(true);
ProcessStartInfo process = new ProcessStartInfo("cmd.exe");
process.WorkingDirectory = textBoxSteam.Text + @"\userdata\" + dir.Name + @"\ugc";
process.Arguments = @"/c mklink /J workshop " + "\"" + textBoxSteam.Text + @"\steamapps\common\Counter-Strike Global Offensive\csgo\maps\workshop" + "\"";
Process.Start(process);
}
catch
{
continue;
}
}
ProcessStartInfo processs = new ProcessStartInfo("cmd.exe");
processs.WorkingDirectory = steamDirectory + @"\csco\maps";
processs.Arguments = @"/c mklink /J workshop " + "\"" + textBoxSteam.Text + @"\steamapps\common\Counter-Strike Global Offensive\csgo\maps\workshop" + "\"";
Process.Start(processs);
if (!update)
{
foreach (var process in Process.GetProcessesByName("Steam")) process.Kill();
System.Windows.MessageBox.Show("Installation complete, now Steam is launching.\nClassic Offensive will appear in Library.", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
System.Diagnostics.Process.Start(textBoxSteam.Text + "/Steam.exe");
button.Content = "Installation Complete";
}
else
{
button.Content = "Update Complete";
}
labelInstalledVersion.Content = versionText;
downloading = false;
}
catch (Exception ee)
{
System.Windows.MessageBox.Show(ee.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
private void textBox_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
folderBrowser.ShowDialog();
if (!string.IsNullOrWhiteSpace(folderBrowser.SelectedPath))
{
textBoxSteam.Text = folderBrowser.SelectedPath;
checkVersion();
}
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (downloading)
{
MessageBoxResult result = System.Windows.MessageBox.Show("Downloading in progress. You want to quit?", "Exit",
MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (result == MessageBoxResult.Yes)
{
client.CancelAsync();
deleteDir(steamDirectory + "/Temp");
}
}
}
public void deleteDir(string path)
{
if (Directory.Exists(path))
{
System.IO.DirectoryInfo di = new DirectoryInfo(path);
foreach (FileInfo file in di.GetFiles()) file.Delete();
foreach (DirectoryInfo dir in di.GetDirectories()) dir.Delete(true);
Directory.Delete(path);
}
}
private bool remoteFileExists(string url)
{
try
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "HEAD";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
response.Close();
return (response.StatusCode == HttpStatusCode.OK);
}
catch
{
return false;
}
}
private bool checkUpdates()
{
String updateFile = String.Empty;
Double updateVersion = 0.0;
if (File.Exists("updater.bat")) System.Diagnostics.Process.Start("updater.bat");
try
{
using (WebClient client = new WebClient()) updateFile = client.DownloadString(rawRepoUrl + "update.txt");
}
catch (Exception)
{
System.Windows.MessageBox.Show("Unable to get version file.\nCheck your internet connection!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
if (updateFile != String.Empty)
{
updateVersion = Convert.ToDouble(Regex.Match(updateFile, @"[0-9]+\.[0-9]+").Value);
}
else
{
return false;
}
if (updateVersion > localVersion)
{
MessageBoxResult result = System.Windows.MessageBox.Show("New Installer version is available. Do you want to update?", "Update",
MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (result == MessageBoxResult.Yes)
{
System.IO.FileInfo file = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
System.IO.File.Move(file.FullName, file.DirectoryName + "\\" + file.Name + ".old");
WebClient updateClient = new WebClient();
updateClient.DownloadFileCompleted += new AsyncCompletedEventHandler(downloadUpdateCompleted);
updateClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloadProgressChanged);
updateClient.DownloadFileAsync(new Uri(updateFile), file.DirectoryName + "\\" + file.Name);
labelLatestVersion.Content = updateVersion.ToString("0.00");
labelInstalledVersion.Content = localVersion.ToString("0.00");
button.Content = "Installer update in progress...";
button.IsEnabled = false;
textBoxSteam.IsEnabled = false;
return true;
}
}
return false;
}
private void downloadUpdateCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
System.IO.FileInfo file = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
using (StreamWriter sw = File.CreateText("updater.bat"))
{
sw.WriteLine("del " + @"""" + file.Name + ".old");
sw.WriteLine("del updater.bat");
}
System.Diagnostics.Process.Start(file.DirectoryName + "/" + file.Name);
System.Windows.Application.Current.Shutdown();
}
void downloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
double percentage = double.Parse(e.BytesReceived.ToString()) / double.Parse(e.TotalBytesToReceive.ToString()) * 100;
if (percentage >= 99.9)
{
button.Content = "Installing...";
}
else
{
button.Content = "Downloading... " + Convert.ToString(Math.Round(percentage)) + "%";
}
progressBar.Value = int.Parse(Math.Truncate(percentage).ToString());
}
}
}