-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
212 lines (184 loc) · 7.29 KB
/
Form1.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
using Microsoft.Win32;
using Octokit;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CSCEInstaller
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Random Random = new Random();
object steamInstallPath = "";
private void SetInputToSteamDir()
{
InstallDirTextBox.Enabled = false;
if (steamInstallPath == null)
{
MessageBox.Show("Steam not found! Cannot copy files to Steam library.");
FilesSteamCheckbox.Checked = false;
FilesManualCheckbox.Checked = true;
InstallDirTextBox.Enabled = true;
return;
}
string steamPath = steamInstallPath.ToString();
InstallDirTextBox.Text = steamPath + @"\steamapps\sourcemods\csce\";
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
// dont ask
}
string lastManualInput = "";
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
// Copy files to steam
if (!FilesSteamCheckbox.Checked) return;
lastManualInput = InstallDirTextBox.Text;
SetInputToSteamDir();
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
// I'll do it myself
if (!FilesManualCheckbox.Checked) return;
if(lastManualInput != "") InstallDirTextBox.Text = lastManualInput;
InstallDirTextBox.Enabled = true;
}
bool canContinue = true;
string BrowserDownloadUrl = "";
private void GetStuff()
{
steamInstallPath = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Valve\Steam", "InstallPath", null);
SetInputToSteamDir();
}
private async void Form1_Load(object sender, EventArgs e)
{
var client = new GitHubClient(new ProductHeaderValue("CSCEInstaller"));
var repo = "counter-strike-community-edition";//"CSCEInstaller";
Release releases;
try
{
var rels = await client.Repository.Release.GetAll("Siomek101", repo);
label7.Text = "Version: " + rels[0].Name;
releases = rels[0];
}
catch (NotFoundException)
{
GetStuff();
MessageBox.Show("(release not found) Couldn't find CS:CE files on GitHub. Make sure you have internet connection and restart the installer. Installer will not continue.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var archive = "CSCEBinary.zip";
foreach (var item in releases.Assets)
{
if(item.Name == archive)
{
BrowserDownloadUrl = item.BrowserDownloadUrl;
break;
}
}
if(BrowserDownloadUrl == "")
{
MessageBox.Show("Couldn't find CS:CE files on GitHub. Make sure you have internet connection and restart the installer. Installer will not continue.", "Error", MessageBoxButtons.OK,MessageBoxIcon.Error);
canContinue = false;
}
// MessageBox.Show(
// $"The latest release is tagged at {releases.TagName} and is named {releases.Name}, {assets}");
GetStuff();
}
private void LogBox(object msg)
{
LogTextBox.Text += msg + Environment.NewLine;
}
private void LogBoxNoLn(object msg)
{
LogTextBox.Text += msg;
}
private void button1_Click(object sender, EventArgs e)
{
if(!canContinue || BrowserDownloadUrl == "")
{
LogBox("Couldn't find CS:CE files on GitHub. Make sure you have internet connection and restart the installer. Installer will not continue.");
return;
}
LogBox("Starting Install Process");
InstallSettingsPanel.Enabled = false;
string installPath = InstallDirTextBox.Text;
if(!Directory.Exists(installPath))
{
LogBox("Directory " + installPath + " doesn't exist. Creating...");
Directory.CreateDirectory(installPath);
} else
{
if(MessageBox.Show("Directory already exists. Do you want to replace it?", "Question", MessageBoxButtons.YesNo) != DialogResult.Yes)
{
InstallSettingsPanel.Enabled = true;
LogBox("Installation aborted.");
return;
}
}
// Old approach
//LogBox("Extracting files into temporary folder...");
//var tempFolder = Path.GetTempPath() + @"CSCE_TEMP" + Random.Next(100000, (int) (int.MaxValue/100));
//Directory.CreateDirectory(tempFolder);
//LogBox("Temporary folder: " + tempFolder);
var tempFilename = Path.GetTempFileName();
LogBox($"Downloading archive from Github");
using (var client = new WebClient())
{
client.DownloadProgressChanged += (a,b) =>
{
bytesLabel.Text = $"{(b.BytesReceived) / 1000}/{(b.TotalBytesToReceive) / 1000} KB {b.ProgressPercentage}%";
downloadProgress.Value = b.ProgressPercentage;
};
client.DownloadFileCompleted += (a, b) =>
{
if (b.Error != null)
{
LogBox("Couldn't download file: ");
LogBox(b.Error.Message);
return;
}
LogBox("Extracting files using 7-zip...");
var sevenzipArgs = $"x \"{tempFilename}\" -y -o{installPath}";
Process.Start("7z.exe", sevenzipArgs).Start();
LogBox($"Deleting temp file");
File.Delete(tempFilename);
LogBox("Done!");
InstallSettingsPanel.Visible = false;
FinishLabel.Visible = true;
FinishExitButton.Visible = true;
};
client.DownloadFileAsync(new Uri(BrowserDownloadUrl), tempFilename);
}
//LogBox("Removing temporary folder...");
//Directory.Delete(tempFolder, true);
}
private void button2_Click(object sender, EventArgs e)
{
System.Windows.Forms.Application.Exit();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start("https://github.com/Siomek101/CSCEInstaller");
}
private void InstallSettingsPanel_Paint(object sender, PaintEventArgs e)
{
}
private void progressBar1_Click(object sender, EventArgs e)
{
}
}
}