-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
288 lines (267 loc) · 8.68 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
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
using BinMerger.Properties;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BinMerger
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog dlg = new FolderBrowserDialog())
{
if (txtFolderBinCues.Text != "" && Directory.Exists(txtFolderBinCues.Text))
{
dlg.SelectedPath = txtFolderBinCues.Text;
}
else
{
dlg.SelectedPath = Directory.GetCurrentDirectory();
}
if (dlg.ShowDialog() != DialogResult.OK)
{
return;
}
txtFolderBinCues.Text = dlg.SelectedPath;
btnStart.Enabled = (txtFolderBinCues.Text != "");
}
}
private void btnStart_Click(object sender, EventArgs e)
{
btnClearLog.Enabled = false;
txtLog.Invoke((MethodInvoker)delegate ()
{
txtLog.AppendText("Start initiated... Please wait while scanning folder for .cue files.\r\n");
});
DateTime startTime = DateTime.Now;
// find all bin/cue files:
string[] allCueFiles = null;
string[] allBinFiles = null;
try
{
allCueFiles = Directory.GetFiles(txtFolderBinCues.Text, "*.cue", SearchOption.AllDirectories);
allBinFiles = Directory.GetFiles(txtFolderBinCues.Text, "*.bin", SearchOption.AllDirectories);
}
catch (Exception ex)
{
txtLog.Invoke((MethodInvoker)delegate ()
{
txtLog.AppendText("Could not access folder: " + txtFolderBinCues.Text + " exception: " + ex.ToString() + "\r\n");
});
btnClearLog.Enabled = true;
return;
}
if (allCueFiles == null || allCueFiles.Length == 0)
{
txtLog.Invoke((MethodInvoker)delegate ()
{
txtLog.AppendText("No .cue files were found in the folder: " + txtFolderBinCues.Text + "! Are you sure you have .cue files there?\r\n");
});
btnClearLog.Enabled = true;
return;
}
if (allBinFiles == null || allBinFiles.Length == 0)
{
txtLog.Invoke((MethodInvoker)delegate ()
{
txtLog.AppendText("No .bin files were found in the folder: " + txtFolderBinCues.Text + "! Are you sure you have .bin files there?\r\n");
if (allCueFiles != null)
{
txtLog.AppendText("You do however have " + allCueFiles.Length + ".cue files in the folder! Are they already converted to .cdm?\r\n");
}
});
btnClearLog.Enabled = true;
return;
}
var approvedCueFiles = new List<string>();
long totalFileSize = 0;
var approvedBinFiles = new List<string>();
Dictionary<string, List<string>> binFilesPerCue = new Dictionary<string, List<string>>();
foreach (string cueFile in allCueFiles)
{
// read cue file to figure out which binfile we are looking for:
// try here?
string[] lines = null;
try
{
lines = File.ReadAllLines(cueFile);
}
catch (Exception ex)
{
txtLog.Invoke((MethodInvoker)delegate ()
{
txtLog.AppendText("Could not read: " + cueFile + "! " + ex.ToString() + "\r\n");
});
}
if (lines == null)
continue;
var binFileName = "";
var hadBinFile = false;
// read all lines to figure out which bin files we should actually remove afterwards if we selected remove...
foreach (var line in lines)
{
if (line.StartsWith("FILE \"") && line.EndsWith("\" BINARY"))
{
binFileName = line.Substring("FILE \"".Length, line.LastIndexOf("\" BINARY") - "FILE \"".Length);
if (binFileName != "" && binFileName.EndsWith(".bin"))
{
foreach (string binFile in allBinFiles)
{
if (binFile.Contains(binFileName))
{
if (approvedBinFiles.Contains(binFile) == false)
{
if (!binFilesPerCue.ContainsKey(cueFile))
{
binFilesPerCue.Add(cueFile, new List<string>());
}
binFilesPerCue[cueFile].Add(binFile);
approvedBinFiles.Add(binFile);
// get file size:
totalFileSize += new FileInfo(binFile).Length;
}
hadBinFile = true;
}
}
}
}
}
if (hadBinFile)
{
approvedCueFiles.Add(cueFile);
}
}
txtLog.Invoke((MethodInvoker)delegate ()
{
txtLog.AppendText("Found: " + approvedCueFiles.Count + " .cue files with .bin files. Conversion starting in 1 seconds...\r\n");
});
Task.Delay(1000).Wait();
txtLog.Invoke((MethodInvoker)delegate ()
{
txtLog.AppendText("1 second...\r\n");
});
Task.Delay(1000).Wait();
bool canceled = false;
var binFilesRemoved = 0;
var cueFilesRemoved = 0;
var countCueFilesProcessed = 0;
// CONVERT!
foreach (string filePath in approvedCueFiles)
{
if (!File.Exists(filePath))
{
txtLog.Invoke((MethodInvoker)delegate ()
{
txtLog.AppendText("Could not find cue any longer @" + filePath + "!\r\n");
});
continue;
}
var tempPath = Path.GetDirectoryName(filePath) + "\\temp";
var outpath = Path.GetDirectoryName(filePath);
var fName = Path.GetFileNameWithoutExtension(filePath);
// must create temp folder first!
if (!Directory.Exists(tempPath))
{
Directory.CreateDirectory(tempPath);
}
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "binmerge/binmerge.exe",
Arguments = "-o \"" + tempPath + "\" \"" + filePath + "\" \"" + fName + "\"",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
string outPut = proc.StandardOutput.ReadToEnd();
if (outPut.Contains("ERROR"))
{
txtLog.Invoke((MethodInvoker)delegate ()
{
txtLog.AppendText("Error occured when merging: " + outPut + "\r\n");
});
proc.WaitForExit();
proc.Close();
return;
}
txtLog.Invoke((MethodInvoker)delegate ()
{
txtLog.AppendText("Combined .bin/.cue cd image: " + (countCueFilesProcessed + 1) + " / " + approvedCueFiles.Count + " with file: " + filePath + ". " + outPut + "\r\n");
});
proc.WaitForExit();
var exitCode = proc.ExitCode;
proc.Close();
// delete old bin files
foreach (string binFile in binFilesPerCue[filePath])
{
if (File.Exists(binFile))
{
File.Delete(binFile);
binFilesRemoved++;
}
}
// delete old cue file
if (File.Exists(filePath))
{
File.Delete(filePath);
cueFilesRemoved++;
}
// move the temp files
File.Move(tempPath + "\\" + fName + ".bin", outpath + "\\" + fName + ".bin");
File.Move(tempPath + "\\" + fName + ".cue", outpath + "\\" + fName + ".cue");
// delete the temp folder
if (Directory.Exists(tempPath))
{
Directory.Delete(tempPath);
}
countCueFilesProcessed++;
}
if (binFilesRemoved != 0)
{
txtLog.Invoke((MethodInvoker)delegate ()
{
txtLog.AppendText("Deleted " + binFilesRemoved + " old .bin files!\r\n");
});
}
TimeSpan diff = DateTime.Now - startTime;
txtLog.Invoke((MethodInvoker)delegate ()
{
txtLog.AppendText("Finished combining " + countCueFilesProcessed + " .bin/.cue files after " + Math.Round(diff.TotalSeconds, 2) + " seconds!\r\n");
});
btnClearLog.Enabled = true;
}
private void txtFolderBinCues_TextChanged(object sender, EventArgs e)
{
if (txtFolderBinCues.Text != "")
{
btnStart.Enabled = true;
} else
{
btnStart.Enabled = false;
}
}
private void btnClearLog_Click(object sender, EventArgs e)
{
txtLog.Invoke((MethodInvoker)delegate ()
{
txtLog.ResetText();
});
}
}
}