-
Notifications
You must be signed in to change notification settings - Fork 374
/
MultiDiskBundler.cs
292 lines (244 loc) · 7.49 KB
/
MultiDiskBundler.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using BizHawk.Emulation.Common;
using BizHawk.Client.Common;
using BizHawk.Common;
using BizHawk.Common.PathExtensions;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Cores.Sega.MasterSystem;
namespace BizHawk.Client.EmuHawk
{
public partial class MultiDiskBundler : ToolFormBase, IToolFormAutoConfig
{
private static readonly FilesystemFilterSet BundlesFSFilterSet = new(new FilesystemFilter("XML Files", new[] { "xml" }));
private XElement _currentXml;
[RequiredService]
public IEmulator Emulator { get; set; }
protected override string WindowTitleStatic => "Multi-disk Bundler";
public MultiDiskBundler()
{
InitializeComponent();
Icon = Properties.Resources.DualIcon;
SystemDropDown.Items.AddRange(new[]
{
VSystemID.Raw.AmstradCPC,
VSystemID.Raw.AppleII,
VSystemID.Raw.Arcade,
VSystemID.Raw.C64,
VSystemID.Raw.GBL,
VSystemID.Raw.GEN,
VSystemID.Raw.GGL,
VSystemID.Raw.Jaguar,
VSystemID.Raw.N64,
VSystemID.Raw.NDS,
VSystemID.Raw.PCFX,
VSystemID.Raw.PSX,
VSystemID.Raw.SAT,
VSystemID.Raw.TI83,
VSystemID.Raw.ZXSpectrum,
});
}
private void MultiGameCreator_Load(object sender, EventArgs e) => Restart();
public override void Restart()
{
FileSelectorPanel.Controls.Clear();
AddButton_Click(null, null);
AddButton_Click(null, null);
if (!Game.IsNullInstance() && !MainForm.CurrentlyOpenRom.EndsWith(".xml"))
{
if (MainForm.CurrentlyOpenRom.Contains("|"))
{
var pieces = MainForm.CurrentlyOpenRom.Split('|');
var directory = Path.GetDirectoryName(pieces[0]) ?? "";
var filename = Path.ChangeExtension(pieces[1], ".xml");
NameBox.Text = Path.Combine(directory, filename);
}
else
{
NameBox.Text = Path.ChangeExtension(MainForm.CurrentlyOpenRom, ".xml");
}
if (SystemDropDown.Items.Contains(Emulator.SystemId))
{
SystemDropDown.SelectedItem = Emulator.SystemId;
}
else if (Emulator is SMS sms && sms.IsGameGear)
{
SystemDropDown.SelectedItem = VSystemID.Raw.GGL;
}
FileSelectors.First().Path = MainForm.CurrentlyOpenRom;
Recalculate();
}
}
private void CancelBtn_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private bool DoSave(out FileInfo fileInfo)
{
fileInfo = null;
if (!Recalculate())
return false;
fileInfo = new FileInfo(NameBox.Text);
if (fileInfo.Exists)
{
var result = this.ModalMessageBox2("File already exists, overwrite?", "File exists", EMsgBoxIcon.Warning, useOKCancel: true);
if (!result)
{
return false;
}
}
File.WriteAllText(fileInfo.FullName, _currentXml.ToString());
return true;
}
private void SaveButton_Click(object sender, EventArgs e)
{
FileInfo dummy;
DoSave(out dummy);
}
private void SaveRunButton_Click(object sender, EventArgs e)
{
FileInfo fileInfo;
if (!DoSave(out fileInfo))
return;
DialogResult = DialogResult.OK;
Close();
var lra = new LoadRomArgs { OpenAdvanced = new OpenAdvanced_OpenRom { Path = fileInfo.FullName } };
_ = MainForm.LoadRom(fileInfo.FullName, lra);
}
private void AddButton_Click(object sender, EventArgs e)
{
int start = 3 + (FileSelectorPanel.Controls.Count * 43);
var groupBox = new GroupBox
{
Text = "",
Location = UIHelper.Scale(new Point(6, start)),
Size = new Size(FileSelectorPanel.ClientSize.Width - UIHelper.ScaleX(12), UIHelper.ScaleY(41)),
Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top
};
var mdf = new MultiDiskFileSelector(MainForm, Config.PathEntries,
() => MainForm.CurrentlyOpenRom, () => SystemDropDown.SelectedItem?.ToString())
{
Location = UIHelper.Scale(new Point(7, 12)),
Width = groupBox.ClientSize.Width - UIHelper.ScaleX(13),
Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top
};
mdf.NameChanged += FileSelector_NameChanged;
groupBox.Controls.Add(mdf);
FileSelectorPanel.Controls.Add(groupBox);
}
private void btnRemove_Click(object sender, EventArgs e)
{
//ToDo:
//Make this better?
//We need to have i at 1 and not zero because Controls Count doesn't start at zero (sort of)
var i = 1;
//For Each Control box we have, loop
foreach (Control ctrl in FileSelectorPanel.Controls)
{
//if we are at the last box, then remove it.
if ((i == FileSelectorPanel.Controls.Count))
{
ctrl.Dispose();
}
//One to our looper
i++;
}
Recalculate();
}
private void FileSelector_NameChanged(object sender, EventArgs e)
{
Recalculate();
}
private IEnumerable<MultiDiskFileSelector> FileSelectors =>
FileSelectorPanel.Controls
.OfType<GroupBox>()
.SelectMany(g => g.Controls.OfType<MultiDiskFileSelector>());
private bool Recalculate()
{
try
{
var names = FileSelectors.Select(f => f.Path).ToList();
if (names.Count != 0)
{
var name = NameBox.Text;
if (string.IsNullOrWhiteSpace(name))
{
throw new Exception("Xml Filename can not be blank");
}
if (names.Any(string.IsNullOrWhiteSpace))
{
throw new Exception("Rom Names can not be blank");
}
var system = SystemDropDown.SelectedItem?.ToString();
if (string.IsNullOrWhiteSpace(system))
{
throw new Exception("System Id can not be blank");
}
var basePath = Path.GetDirectoryName(name.SubstringBefore('|'));
if (string.IsNullOrEmpty(basePath))
{
var fileInfo = new FileInfo(name);
basePath = Path.GetDirectoryName(fileInfo.FullName);
}
_currentXml = new XElement("BizHawk-XMLGame",
new XAttribute("System", system),
new XAttribute("Name", Path.GetFileNameWithoutExtension(name)),
new XElement("LoadAssets",
names.Select(n => new XElement(
"Asset",
new XAttribute("FileName", PathExtensions.GetRelativePath(basePath, n))
))
)
);
SaveRunButton.Enabled = true;
SaveButton.Enabled = true;
return true;
}
}
catch (Exception)
{
//swallow exceptions, since this is just validation logic
}
_currentXml = null;
SaveRunButton.Enabled = false;
SaveButton.Enabled = false;
return false;
}
private void NameBox_TextChanged(object sender, EventArgs e)
{
Recalculate();
}
private void BrowseBtn_Click(object sender, EventArgs e)
{
string filename = "";
string initialDirectory = Config.PathEntries.UseRecentForRoms
? string.Empty
: Config.PathEntries.MultiDiskAbsolutePath();
if (!Game.IsNullInstance())
{
filename = NameBox.Text;
if (string.IsNullOrWhiteSpace(filename))
{
filename = Path.ChangeExtension(Game.FilesystemSafeName(), ".xml");
}
initialDirectory = Path.GetDirectoryName(filename) ?? string.Empty;
}
var result = this.ShowFileSaveDialog(
filter: BundlesFSFilterSet,
initDir: initialDirectory,
initFileName: filename);
if (result is not null) NameBox.Text = result;
}
private void SystemDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
Recalculate();
}
}
}