-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMainForm.cs
335 lines (274 loc) · 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
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
using SuperMetroidRandomizer.IO;
using SuperMetroidRandomizer.Net;
using SuperMetroidRandomizer.Properties;
using SuperMetroidRandomizer.Random;
using SuperMetroidRandomizer.Rom;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
namespace SuperMetroidRandomizer
{
public partial class MainForm : Form
{
private Thread checkUpdateThread;
public MainForm()
{
InitializeSettings();
InitializeComponent();
}
private void InitializeSettings()
{
Settings.Default.OutputFileV11 = Settings.Default.OutputFileV11;
Settings.Default.ControlsShot = Settings.Default.ControlsShot;
Settings.Default.ControlsJump = Settings.Default.ControlsJump;
Settings.Default.ControlsDash = Settings.Default.ControlsDash;
Settings.Default.ControlsItemSelect = Settings.Default.ControlsItemSelect;
Settings.Default.ControlsItemCancel = Settings.Default.ControlsItemCancel;
Settings.Default.ControlsAngleUp = Settings.Default.ControlsAngleUp;
Settings.Default.ControlsAngleDown = Settings.Default.ControlsAngleDown;
Settings.Default.RandomizerDifficulty = Settings.Default.RandomizerDifficulty;
Settings.Default.RandoDiff06 = Settings.Default.RandoDiff06;
Settings.Default.CreateSpoilerLog = Settings.Default.CreateSpoilerLog;
}
private void RunCheckUpdate()
{
checkUpdateThread = new Thread(RandomizerVersion.CheckUpdate);
checkUpdateThread.SetApartmentState(ApartmentState.STA);
checkUpdateThread.Start();
}
private void MainForm_Load(object sender, EventArgs e)
{
filenameV11.Text = Settings.Default.OutputFileV11;
createSpoilerLog.Checked = Settings.Default.CreateSpoilerLog;
Text = string.Format("Super Metroid: Project Base Randomizer: v2", RandomizerVersion.CurrentDisplay);
randomizerDifficulty.SelectedItem = Settings.Default.RandomizerDifficulty;
RunCheckUpdate();
}
public void CheckBox2Checked(object sender, EventArgs e)
{
}
private void createV11_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(seedV11.Text))
{
SetSeedBasedOnDifficulty();
}
ClearOutputV11();
var difficulty = GetRandomizerDifficulty();
if (difficulty == RandomizerDifficulty.None)
{
return;
}
CreateRom(difficulty);
Settings.Default.CreateSpoilerLog = createSpoilerLog.Checked;
Settings.Default.RandomizerDifficulty = randomizerDifficulty.SelectedItem.ToString();
Settings.Default.Save();
}
private void CreateRom(RandomizerDifficulty difficulty)
{
int parsedSeed;
if (!int.TryParse(seedV11.Text, out parsedSeed))
{
MessageBox.Show("Seed must be numeric or blank.", "Seed Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
WriteOutputV11("Seed must be numeric or blank.");
}
else if(String.IsNullOrWhiteSpace(tbBaseRomPath.Text))
{
MessageBox.Show("Must select a base ROM.", "Base ROM not selected", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
var romLocations = RomLocationsFactory.GetRomLocations(difficulty);
RandomizerLog log = null;
if (createSpoilerLog.Checked)
{
log = new RandomizerLog(string.Format(romLocations.SeedFileString, parsedSeed));
}
seedV11.Text = string.Format(romLocations.SeedFileString, parsedSeed);
var randomizerV11 = new RandomizerV11(parsedSeed, romLocations, log);
randomizerV11.CreateRom(filenameV11.Text, tbBaseRomPath.Text);
var outputString = new StringBuilder();
outputString.AppendFormat("Done!{0}{0}{0}Seed: ", Environment.NewLine);
outputString.AppendFormat(romLocations.SeedFileString, parsedSeed);
outputString.AppendFormat(" ({0} Difficulty){1}{1}", romLocations.DifficultyName, Environment.NewLine);
WriteOutputV11(outputString.ToString());
}
}
private void CreateSpoilerLog(RandomizerDifficulty difficulty)
{
int parsedSeed;
if (!int.TryParse(seedV11.Text, out parsedSeed))
{
MessageBox.Show("Seed must be numeric or blank.", "Seed Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
WriteOutputV11("Seed must be numeric or blank.");
}
else if(String.IsNullOrWhiteSpace(tbBaseRomPath.Text))
{
MessageBox.Show("Must select a base ROM.", "Base ROM not selected", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
var romPlms = RomLocationsFactory.GetRomLocations(difficulty);
RandomizerLog log = new RandomizerLog(string.Format(romPlms.SeedFileString, parsedSeed));
seedV11.Text = string.Format(romPlms.SeedFileString, parsedSeed);
var randomizer = new RandomizerV11(parsedSeed, romPlms, log);
WriteOutputV11(randomizer.CreateRom(filenameV11.Text, tbBaseRomPath.Text, true));
}
}
private RandomizerDifficulty GetRandomizerDifficulty()
{
RandomizerDifficulty difficulty;
if (seedV11.Text.ToUpper().Contains("C"))
{
randomizerDifficulty.SelectedItem = "Casual";
seedV11.Text = seedV11.Text.ToUpper().Replace("C", "");
difficulty = RandomizerDifficulty.Casual;
}
else if (seedV11.Text.ToUpper().Contains("S"))
{
randomizerDifficulty.SelectedItem = "Speedrunner";
seedV11.Text = seedV11.Text.ToUpper().Replace("S", "");
difficulty = RandomizerDifficulty.Speedrunner;
}
else if (seedV11.Text.ToUpper().Contains("M"))
{
randomizerDifficulty.SelectedItem = "Masochist";
seedV11.Text = seedV11.Text.ToUpper().Replace("M", "");
difficulty = RandomizerDifficulty.Masochist;
}
//else if (seedV11.Text.ToUpper().Contains("I"))
//{
// randomizerDifficulty.SelectedItem = "Insane";
// seedV11.Text = seedV11.Text.ToUpper().Replace("I", "");
// difficulty = RandomizerDifficulty.Insane;
//}
else
{
switch (randomizerDifficulty.SelectedItem.ToString())
{
case "Casual":
difficulty = RandomizerDifficulty.Casual;
break;
case "Speedrunner":
difficulty = RandomizerDifficulty.Speedrunner;
break;
case "Masochist":
difficulty = RandomizerDifficulty.Masochist;
break;
//case "Insane":
// difficulty = RandomizerDifficulty.Insane;
// break;
default:
MessageBox.Show("Please select a difficulty.", "Select Difficulty", MessageBoxButtons.OK, MessageBoxIcon.Error);
WriteOutputV11("Please select a difficulty.");
return RandomizerDifficulty.None;
}
}
return difficulty;
}
private void SetSeedBasedOnDifficulty()
{
switch (randomizerDifficulty.SelectedItem.ToString())
{
case "Casual":
seedV11.Text = string.Format("C{0:0000000}", (new SeedRandom()).Next(10000000));
break;
case "Masochist":
seedV11.Text = string.Format("M{0:0000000}", (new SeedRandom()).Next(10000000));
break;
case "Insane":
seedV11.Text = string.Format("I{0:0000000}", (new SeedRandom()).Next(10000000));
break;
default:
seedV11.Text = string.Format("S{0:0000000}", (new SeedRandom()).Next(10000000));
break;
}
}
private void ClearOutputV11()
{
outputV11.Text = "";
}
private void WriteOutputV11(string text)
{
outputV11.Text += text;
}
private void browseV11_Click(object sender, EventArgs e)
{
var info = new FileInfo(Regex.Replace(filenameV11.Text, "<.*>", ""));
var saveFileDialog = new SaveFileDialog { Filter = "All files (*.*)|*.*", FilterIndex = 2, RestoreDirectory = true, InitialDirectory = info.DirectoryName, FileName = info.Name };
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
filenameV11.Text = saveFileDialog.FileName;
MessageBox.Show("Remember to hit \"create\" to create the rom.", "Remember to create the rom!", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
}
}
private void filenameV11_TextChanged(object sender, EventArgs e)
{
Settings.Default.OutputFileV11 = filenameV11.Text;
Settings.Default.Save();
}
private void controlsV11_Click(object sender, EventArgs e)
{
var controlsDialog = new Controls();
controlsDialog.ShowDialog();
}
private void controls_Click(object sender, EventArgs e)
{
var controlsDialog = new Controls();
controlsDialog.ShowDialog();
}
private void filename_Leave(object sender, EventArgs e)
{
var senderText = (TextBox) sender;
if (!senderText.Text.Contains("."))
{
senderText.Text += ".sfc";
}
}
private void report_Click(object sender, EventArgs e)
{
Help.ShowHelp(null, string.Format("https://docs.google.com/forms/d/e/1FAIpQLSeO3NxPYDnKhpxVoxXrgoTVrFjwvi_HqOGWzw62Vw-ziM2DTg/viewform", RandomizerVersion.CurrentDisplay));
}
private void randomSpoiler_Click(object sender, EventArgs e)
{
SetSeedBasedOnDifficulty();
ClearOutputV11();
var difficulty = GetRandomizerDifficulty();
CreateSpoilerLog(difficulty);
}
void Label3Click(object sender, EventArgs e)
{
}
private void bBrowseBaseRomPath_Click(object sender, EventArgs e)
{
var saveFileDialog = new OpenFileDialog { Filter = "All files (*.*)|*.*" };
if(saveFileDialog.ShowDialog() == DialogResult.OK)
{
tbBaseRomPath.Text = saveFileDialog.FileName;
}
}
/// <summary>
/// Open (in Explorer) the directory that the expected output will go to
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void openDirBtn_Click(object sender, EventArgs e)
{
string filePath = filenameV11.Text;
string directoryPath = ".\\";
if(filePath.Contains("\\"))
{
directoryPath = filePath.Substring(0, filePath.LastIndexOf('\\'));
}
if(!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
Process.Start(directoryPath);
}
}
}