-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
269 lines (238 loc) · 8.12 KB
/
MainForm.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
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Universal_Srcds_Launcher
{
public partial class MainForm : Form
{
private bool IsGmodOrSbox = false;
public MainForm()
{
InitializeComponent();
BringToFront();
Focus();
var Settings = Properties.Settings.Default;
lancheck.Checked = Settings.Lan;
legacyCheck.Checked = Settings.Console;
maxplayers.Value = Settings.MaxPlayers;
gameselect.Text = Settings.Gamemode;
gameselect.SelectedItem = Settings.Gamemode;
mapselect.Text = Settings.Map;
mapselect.SelectedItem = Settings.Map;
passwordBox.Text = Settings.Password;
TokenEnable.Checked = Settings.TokenEnabled;
CollectionIDBox.Text = Settings.CollectionID;
launchParameters.Text = Settings.LaunchParams;
exePathLabel.Text += Settings.ExeName;
gamePathLabel.Text += Settings.GamePath;
linuxShell.Text = Settings.LinuxShell;
mapselect.Enabled = !legacyCheck.Checked;
lancheck.Enabled = !legacyCheck.Checked;
maxplayers.Enabled = !legacyCheck.Checked;
gameselect.Enabled = !legacyCheck.Checked;
passwordBox.Enabled = !legacyCheck.Checked;
CollectionIDBox.Enabled = !legacyCheck.Checked;
launchParameters.Enabled = !legacyCheck.Checked;
UpdateLists();
}
// Update gamemode and map lists when the game path changes
private void UpdateLists()
{
var Settings = Properties.Settings.Default;
gameselect.Items.Clear();
mapselect.Items.Clear();
if ( Directory.Exists( Settings.GamePath + "/gamemodes" ) )
{
string[] listgamemodes = Directory.GetDirectories( Settings.GamePath + "/gamemodes" );
foreach ( string gamemode in listgamemodes )
{
string foldername = Path.GetFileName( gamemode );
if ( foldername != "base" )
{
gameselect.Items.Add( foldername );
}
}
IsGmodOrSbox = true;
}
else if ( Path.GetFileName( Settings.ExePath ) == "sbox-server.exe" )
{
// Insert placeholder for s&box
if ( string.IsNullOrWhiteSpace( gameselect.Text ) )
{
gameselect.Text = "facepunch.sandbox";
}
IsGmodOrSbox = true;
}
else
{
if ( string.IsNullOrWhiteSpace( gameselect.Text ) )
{
gameselect.Text = Path.GetFileName( Settings.GamePath );
}
IsGmodOrSbox = false;
}
if ( Directory.Exists( Settings.GamePath + "/maps" ) )
{
string[] listmaps = Directory.GetFiles( Settings.GamePath + "/maps" );
foreach ( string map in listmaps )
{
string extension = Path.GetExtension( map );
string mapname = Path.GetFileNameWithoutExtension( map );
if ( extension == ".bsp" )
{
mapselect.Items.Add( mapname );
}
}
}
}
// Called when the lan option is changed
private void LanCheck( object sender, EventArgs e )
{
Properties.Settings.Default.Lan = lancheck.Checked;
}
// Called when the legacy launcher option is changed
private void LegacyCheck( object sender, EventArgs e )
{
if ( legacyCheck.Checked )
{
mapselect.Text = "";
gameselect.Text = "";
}
mapselect.Enabled = !legacyCheck.Checked;
lancheck.Enabled = !legacyCheck.Checked;
maxplayers.Enabled = !legacyCheck.Checked;
gameselect.Enabled = !legacyCheck.Checked;
passwordBox.Enabled = !legacyCheck.Checked;
CollectionIDBox.Enabled = !legacyCheck.Checked;
launchParameters.Enabled = !legacyCheck.Checked;
Properties.Settings.Default.Console = legacyCheck.Checked;
}
// Called when the max players option is changed
private void MaxPlayersChanged( object sender, EventArgs e )
{
Properties.Settings.Default.MaxPlayers = Convert.ToInt32( maxplayers.Value );
}
// Called when the map option is changed
private void MapChanged( object sender, EventArgs e )
{
Properties.Settings.Default.Map = mapselect.Text;
}
// Called when the gamemode option is changed
private void GamemodeChanged( object sender, EventArgs e )
{
Properties.Settings.Default.Gamemode = gameselect.Text;
}
// Called when the change exe path button is clicked
private void ChangeExePathClick( object sender, EventArgs e )
{
OpenFileDialog browse = new OpenFileDialog();
browse.Filter = "Server Executable (*.exe)|*.exe|Linux Server Script (srcds_run*)|srcds_run*";
browse.RestoreDirectory = true;
if ( browse.ShowDialog() == DialogResult.OK )
{
Properties.Settings.Default.ExeName = browse.FileName;
Properties.Settings.Default.ExePath = Path.GetDirectoryName( browse.FileName );
exePathLabel.Text = "Exe path: " + Properties.Settings.Default.ExeName;
}
}
// Called when the change game path button is clicked
private void ChangeGamePathClick( object sender, EventArgs e )
{
FolderBrowserDialog browse = new FolderBrowserDialog();
if ( browse.ShowDialog() == DialogResult.OK )
{
Properties.Settings.Default.GamePath = browse.SelectedPath;
gamePathLabel.Text = "Game path: " + browse.SelectedPath;
gameselect.Text = "";
mapselect.Text = "";
UpdateLists();
}
}
// Called when the password option is changed
private void PasswordChanged( object sender, EventArgs e )
{
Properties.Settings.Default.Password = passwordBox.Text;
}
// Called when the workshop collection option is changed
private void CollectionIDBoxChanged( object sender, EventArgs e )
{
Properties.Settings.Default.CollectionID = CollectionIDBox.Text;
}
// Called when the launch parameters option is changed
private void LaunchParamsChanged( object sender, EventArgs e )
{
Properties.Settings.Default.LaunchParams = launchParameters.Text;
}
// Called when the linux terminal program value is changed
private void LinuxShellChanged( object sender, EventArgs e )
{
Properties.Settings.Default.LinuxShell = linuxShell.Text;
}
// Called when the steam token option is changed
private void TokenEnableChanged( object sender, EventArgs e )
{
if ( TokenEnable.Checked )
{
if ( !File.Exists( Properties.Settings.Default.TokenPath ) )
{
OpenFileDialog browse = new OpenFileDialog();
browse.InitialDirectory = Properties.Settings.Default.ExePath;
browse.Filter = "Text file containing token|*.txt";
if ( browse.ShowDialog() == DialogResult.OK )
{
Properties.Settings.Default.TokenPath = browse.FileName;
TokenEnable.Checked = !string.IsNullOrEmpty( Properties.Settings.Default.TokenPath );
}
}
}
Properties.Settings.Default.TokenEnabled = TokenEnable.Checked;
}
// Called when the start button is clicked
private void StartButtonClick( object sender, EventArgs e )
{
bool isLinux = RuntimeInformation.IsOSPlatform( OSPlatform.Linux );
var Settings = Properties.Settings.Default;
string arguments = "+r_hunkalloclightmaps 0";
if ( !legacyCheck.Checked )
arguments += " -console";
if ( lancheck.Checked )
arguments += " +sv_lan 1";
if ( TokenEnable.Checked )
{
string Token = File.ReadAllText( Settings.TokenPath );
arguments += $" +sv_setsteamaccount {Token}";
}
if ( IsGmodOrSbox )
arguments += $" +gamemode {gameselect.Text}";
else
arguments += $" -game {gameselect.Text}";
if ( !string.IsNullOrWhiteSpace( passwordBox.Text ) )
arguments += $" +sv_password {passwordBox.Text}";
if ( !string.IsNullOrWhiteSpace( CollectionIDBox.Text ) )
arguments += $" +host_workshop_collection {CollectionIDBox.Text}";
arguments += $" +maxplayers {maxplayers.Value} +map {mapselect.Text} {launchParameters.Text}";
if ( isLinux )
arguments = $"-c \"{linuxShell.Text} bash -c 'cd {Path.GetDirectoryName( Settings.GamePath )}; ./{Path.GetFileName( Settings.ExeName )} {arguments}; exec bash'\"";
var proc = new ProcessStartInfo
{
UseShellExecute = false,
RedirectStandardOutput = isLinux,
WorkingDirectory = Settings.ExePath,
FileName = isLinux ? "/bin/bash" : Settings.ExeName,
Arguments = arguments
};
try
{
Process.Start( proc );
Close();
}
catch ( Exception ex )
{
DialogResult launcherror = MessageBox.Show( "Failed to launch. " + ex.Message, "Launch Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
if ( launcherror == DialogResult.OK ) Close();
}
}
}
}