-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
518 lines (421 loc) · 16.3 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
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace DoomLauncher
{
public partial class MainForm : Form
{
private readonly string saveFile = @"\_sobad_doom_launcher.cfg";
private string savePath;
private string sourceportPath;
private string demoDirectory;
private string wadDirectory;
public MainForm()
{
InitializeComponent();
SetDirectories();
TryLoadSettings();
}
#region Launcher Logic
private bool ChangeDirectory(string message, out string newDirectory)
{
using (FolderBrowserDialog fbd = new FolderBrowserDialog())
{
fbd.Description = message;
fbd.ShowNewFolderButton = false;
DialogResult result = fbd.ShowDialog();
if (result == DialogResult.OK)
{
newDirectory = fbd.SelectedPath;
return true;
}
newDirectory = null;
return false;
}
}
private void ChangeMode(bool demoTime)
{
btnDemoMode.Enabled = !demoTime;
btnDemoMode.Visible = !demoTime;
btnPlayMode.Enabled = demoTime;
btnPlayMode.Visible = demoTime;
btnDemoDirectory.Enabled = demoTime;
btnDemoDirectory.Visible = demoTime;
lblDemo.Visible = demoTime;
lstDemos.Enabled = demoTime;
lstDemos.Visible = demoTime;
pnlPlaySettings.Enabled = !demoTime;
pnlPlaySettings.Visible = !demoTime;
}
private void ChangeSourcePort()
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.InitialDirectory = Environment.CurrentDirectory;
openFileDialog.Filter = "Source Ports (*.exe)|*.exe|All files (*.*)|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string[] splitPath = openFileDialog.FileName.Split('\\');
string executable = splitPath[splitPath.Length - 1];
lblSourcePort.Text = executable;
sourceportPath = openFileDialog.FileName;
}
}
}
private bool CheckIfValidIWad(string wadName)
{
byte[] identifier = new byte[4];
FileStream stream = File.OpenRead(wadName);
stream.Read(identifier, 0, 4);
return System.Text.Encoding.UTF8.GetString(identifier, 0, identifier.Length) == "IWAD";
}
private void DefaultComboBoxes()
{
cboCompLevel.SelectedIndex = 0;
cboSkill.SelectedIndex = 0;
cboWarp.SelectedIndex = 0;
cboWarpDoom1.SelectedIndex = 0;
}
private void DisplayError(string message, string header)
{
MessageBox.Show(message, header, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void EnableProperWarpComboBox()
{
bool doom1Selected = lstIWads.Items.Count > 0 ? lstIWads.SelectedItem.ToString() == "doom.wad" : false;
cboWarp.Enabled = !doom1Selected;
cboWarp.Visible = !doom1Selected;
cboWarpDoom1.Enabled = doom1Selected;
cboWarpDoom1.Visible = doom1Selected;
}
private void LoadFiles(string fileDirectory, string fileType)
{
if (!Directory.Exists(fileDirectory))
return;
string[] splitPath;
string fileToLoad;
string[] allFiles = Directory.GetFiles(fileDirectory);
foreach (string file in allFiles)
{
if (!file.ToLower().EndsWith(fileType))
continue;
splitPath = file.Split('\\');
fileToLoad = splitPath[splitPath.Length - 1].ToLower();
if (fileType == ".wad")
{
if (CheckIfValidIWad(file))
{
lstIWads.Items.Add(fileToLoad);
}
else
{
lstPWads.Items.Add(fileToLoad);
}
}
else if (fileType == ".deh")
{
lstPWads.Items.Add(fileToLoad);
}
else // Therefore must be a demo.
{
lstDemos.Items.Add(fileToLoad);
}
}
}
private void LoadDemos()
{
LoadFiles(demoDirectory, ".lmp");
}
private void LoadWads()
{
LoadFiles(wadDirectory, ".wad");
LoadFiles(wadDirectory, ".deh");
lstPWads.Sorted = true; // Puts same-named .deh next to .wad files.
}
private string PrepareLaunchCommand()
{
StringBuilder launchCommand = new StringBuilder();
launchCommand.Append($"-iwad \"{wadDirectory + @"\" + lstIWads.SelectedItem}\" ");
if (lstPWads.SelectedIndices.Count > 0)
{
launchCommand.Append($"-file ");
foreach (object wad in lstPWads.SelectedItems)
{
launchCommand.Append($"\"{wadDirectory + @"\" + wad}\" ");
}
}
if (!pnlPlaySettings.Enabled) // Early exit if only playing demo file.
{
launchCommand.Append($"-playdemo \"{demoDirectory + @"\" + lstDemos.SelectedItem}\" ");
return launchCommand.ToString();
}
if (cboCompLevel.SelectedIndex > 0) // Allows for default comp level.
{
launchCommand.Append($"-complevel {cboCompLevel.SelectedItem} ");
}
if (cboWarp.Enabled && cboWarp.SelectedIndex > 0) // Allows for main menu access.
{
launchCommand.Append($"-warp {cboWarp.SelectedIndex} ");
}
else if (cboWarpDoom1.Enabled && cboWarpDoom1.SelectedIndex > 0)
{
string mapCode = cboWarpDoom1.SelectedItem.ToString();
launchCommand.Append($"-warp {mapCode[1]} {mapCode[3]} ");
}
if (cboSkill.SelectedIndex > 0) // Allows for main menu access.
{
launchCommand.Append($"-skill {cboSkill.SelectedIndex} ");
}
if (chkNoMonsters.Checked)
{
launchCommand.Append($"-nomonsters 1 ");
}
if (chkFast.Checked)
{
launchCommand.Append($"-fast 1 ");
}
if (chkRespawn.Checked)
{
launchCommand.Append($"-respawn 1 ");
}
if (chkNoMusic.Checked)
{
launchCommand.Append($"-nomusic 1 ");
}
if (chkShortTics.Checked)
{
launchCommand.Append($"-shorttics 1 ");
}
if (chkSoloNet.Checked)
{
launchCommand.Append($"-solo-net 1 ");
}
if (chkRecordDemo.Checked)
{
if (string.IsNullOrWhiteSpace(txtDemoName.Text))
{
DisplayError($"Please choose a name for your demo.", "No demo filename.");
return null; // Checked for in TryLaunchDoom, which aborts process.
}
if (txtDemoName.Text.Contains(" "))
{
DisplayError($"Please remove whitespace from your demo name.", "Whitespace in filename.");
return null; // Checked for in TryLaunchDoom, which aborts process.
}
launchCommand.Append($"-record {txtDemoName.Text}");
}
return launchCommand.ToString();
}
private void SaveSettings()
{
StringBuilder settings = new StringBuilder();
// Launcher Parameters
settings.Append(lblSourcePort.Text + "|");
settings.Append(lstIWads.SelectedItem.ToString() + "|");
settings.Append(lstPWads.SelectedIndices.Count > 0 ? SaveMultiplePWads() + "|" : "no_pwad|");
settings.Append(cboCompLevel.SelectedIndex.ToString() + "|");
settings.Append(cboSkill.SelectedIndex.ToString() + "|");
settings.Append(cboWarp.SelectedIndex.ToString() + "|");
settings.Append(cboWarpDoom1.SelectedIndex.ToString() + "|"); // Special Doom1 Map names added.
settings.Append(chkNoMonsters.Checked.ToString() + "|");
settings.Append(chkFast.Checked.ToString() + "|");
settings.Append(chkRespawn.Checked.ToString() + "|");
settings.Append(chkNoMusic.Checked.ToString() + "|");
settings.Append(chkSoloNet.Checked.ToString() + "|");
settings.Append(chkShortTics.Checked.ToString() + "|");
settings.Append(chkRecordDemo.Checked.ToString() + "|");
settings.Append(txtDemoName.Text + "|");
// Demo Info
settings.Append(lstDemos.SelectedIndex > -1 ? lstDemos.SelectedItem.ToString() + "|" : "no_demo|");
settings.Append(pnlPlaySettings.Enabled + "|");
// Directories
settings.Append(sourceportPath + "|");
settings.Append(demoDirectory + "|");
settings.Append(wadDirectory);
using (StreamWriter writer = File.CreateText(savePath))
{
writer.Write(settings.ToString());
}
// Local helper just for cleanliness.
string SaveMultiplePWads()
{
StringBuilder builder = new StringBuilder();
foreach (object wad in lstPWads.SelectedItems)
{
builder.Append($"{wad}&"); // Can't use '|' as delimiter here.
}
string result = builder.ToString();
return result.Remove(result.Length - 1); // Don't need extra '&' at end of string.
}
}
private void SetDirectories()
{
try
{
savePath = Environment.CurrentDirectory + saveFile;
}
catch
{
DisplayError($"Can't find current directory or don't have permission to access it. " +
$"Please move me somewhere else.", "Directory Lookup Error.");
}
}
private void TryLaunchDoom(string launchCommand)
{
if (launchCommand == null) // Null in case of unnamed demo during recording attempt.
return;
try
{
System.Diagnostics.Process.Start(sourceportPath, launchCommand);
}
catch (FileNotFoundException)
{
DisplayError($"Your {lblSourcePort.Text} can't be found. " +
$"Have you moved or deleted it since your last session?", "Whoa hold on.");
}
catch
{
DisplayError($"Something went wrong. You can try again or inform me of the error by reaching " +
$"out on itch or YouTube.", "Unknown error.");
}
}
private void TryLoadSettings()
{
if (!File.Exists(savePath))
{
DefaultComboBoxes();
return;
}
string[] loadData = File.ReadAllText(savePath).Split('|');
if (loadData.Length != 20) // In case user manually altered .cfg file, or old version.
return;
wadDirectory = loadData[loadData.Length - 1];
demoDirectory = loadData[loadData.Length - 2];
sourceportPath = loadData[loadData.Length - 3];
if (wadDirectory == "")
{
wadDirectory = null;
}
if (demoDirectory == "")
{
demoDirectory = null;
}
LoadWads();
LoadDemos();
lblSourcePort.Text = loadData[0];
lstIWads.SelectedIndex = lstIWads.FindString(loadData[1]);
string[] pwads = loadData[2].Split('&');
foreach (string pwad in pwads)
{
int index = lstPWads.FindString(pwad);
if (index == -1)
continue;
lstPWads.SetSelected(index, true);
}
cboCompLevel.SelectedIndex = int.Parse(loadData[3]);
cboSkill.SelectedIndex = int.Parse(loadData[4]);
cboWarp.SelectedIndex = int.Parse(loadData[5]);
cboWarpDoom1.SelectedIndex = int.Parse(loadData[6]);
chkNoMonsters.Checked = bool.Parse(loadData[7]);
chkFast.Checked = bool.Parse(loadData[8]);
chkRespawn.Checked = bool.Parse(loadData[9]);
chkNoMusic.Checked = bool.Parse(loadData[10]);
chkSoloNet.Checked = bool.Parse(loadData[11]);
chkShortTics.Checked = bool.Parse(loadData[12]);
chkRecordDemo.Checked = bool.Parse(loadData[13]);
txtDemoName.Text = loadData[14];
lstDemos.SelectedIndex = lstDemos.FindString(loadData[15]);
if (chkRecordDemo.Checked)
{
txtDemoName.Enabled = true;
}
if (chkNoMonsters.Checked)
{
chkFast.Enabled = false;
chkRespawn.Enabled = false;
}
EnableProperWarpComboBox();
ChangeMode(!bool.Parse(loadData[16]));
}
#endregion
#region WinForms Controls Events
private void btnDemoDirectory_Click(object sender, EventArgs e)
{
if (!ChangeDirectory("Please select your demos folder.", out string newDirectory))
return;
demoDirectory = newDirectory;
lstDemos.Items.Clear();
LoadDemos();
}
private void btnDemoMode_Click(object sender, EventArgs e)
{
ChangeMode(true);
}
private void btnDeselectPWad_Click(object sender, EventArgs e)
{
lstPWads.SelectedIndex = -1;
}
private void btnPlay_Click(object sender, EventArgs e)
{
if (!lblSourcePort.Text.EndsWith(".exe"))
{
DisplayError("No source port selected.", "You goofed!");
return;
}
if (lstIWads.SelectedIndex == -1)
{
DisplayError("No iwad selected.", "You goofed!");
return;
}
if (!pnlPlaySettings.Enabled && lstDemos.SelectedIndex == -1)
{
DisplayError("No demo selected.", "You goofed!");
return;
}
SaveSettings();
TryLaunchDoom(PrepareLaunchCommand());
}
private void btnPlayMode_Click(object sender, EventArgs e)
{
ChangeMode(false);
}
private void btnSourcePort_Click(object sender, EventArgs e)
{
ChangeSourcePort();
}
private void btnWadDirectory_Click(object sender, EventArgs e)
{
if (!ChangeDirectory("Please select your wads folder.", out string newDirectory))
return;
wadDirectory = newDirectory;
lstIWads.Items.Clear();
lstPWads.Items.Clear();
LoadWads();
}
private void chkNoMonsters_CheckedChanged(object sender, EventArgs e)
{
if (chkNoMonsters.Checked)
{
chkFast.Checked = false;
chkFast.Enabled = false;
chkRespawn.Checked = false;
chkRespawn.Enabled = false;
}
else
{
chkFast.Enabled = true;
chkRespawn.Enabled = true;
}
}
private void chkRecordDemo_CheckedChanged(object sender, EventArgs e)
{
txtDemoName.Enabled = ((CheckBox)sender).Checked;
}
private void lstIWads_SelectedIndexChanged(object sender, EventArgs e)
{
EnableProperWarpComboBox();
}
#endregion
}
}