-
Notifications
You must be signed in to change notification settings - Fork 8
/
ExportProgressForm.cs
96 lines (84 loc) · 3.84 KB
/
ExportProgressForm.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
using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using BlackOps2SoundStudio.Format;
namespace BlackOps2SoundStudio
{
public partial class ExportProgressForm : Form
{
private SndAliasBank _sndAliasBank;
private bool _useOriginalTreeStructure;
private string _outputDirectory;
public ExportProgressForm()
{
InitializeComponent();
}
private void SetSndAliasBank(SndAliasBank sndAliasBank, bool useOriginalTreeStructure, string outputDirectory)
{
_sndAliasBank = sndAliasBank;
_useOriginalTreeStructure = useOriginalTreeStructure;
_outputDirectory = outputDirectory;
}
internal static void Export(SndAliasBank sndAliasBank, bool useOriginalTreeStructure, string outputDirectory)
{
var form = new ExportProgressForm();
form.SetSndAliasBank(sndAliasBank, useOriginalTreeStructure, outputDirectory);
form.ShowDialog();
}
private void exportBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < _sndAliasBank.Entries.Count; i++)
{
if (exportBackgroundWorker.CancellationPending)
return;
var entry = _sndAliasBank.Entries[i];
string relativePath;
if (!_useOriginalTreeStructure || !SndAliasNameDatabase.Names.TryGetValue(entry.Identifier, out relativePath))
relativePath = "Sound " + (i + 1) + SndAliasBankHelper.GetExtensionFromFormat(entry.Format);
else
relativePath += SndAliasBankHelper.GetExtensionFromFormat(entry.Format);
using (var audioStream = SndAliasBankHelper.GetAudioStreamFromEntry(entry))
{
try
{
var fullPath = Path.Combine(_outputDirectory, relativePath);
string directoryPath;
if (relativePath.IndexOf('\\') != -1 && !Directory.Exists((directoryPath = Path.GetDirectoryName(fullPath))))
Directory.CreateDirectory(directoryPath);
using (var fs = new FileStream(fullPath, FileMode.Create,
FileAccess.Write, FileShare.Read))
audioStream.CopyTo(fs);
exportBackgroundWorker.ReportProgress((i + 1) * 100 / _sndAliasBank.Entries.Count, Path.GetFileName(fullPath));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Black Ops II Sound Studio",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
}
}
MessageBox.Show("All audio entries have been exported successfully.",
"Black Ops II Sound Studio", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void ExportProgressForm_Load(object sender, EventArgs e)
{
exportBackgroundWorker.RunWorkerAsync();
}
private void exportBackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
exportProgressBar.Value = e.ProgressPercentage;
exportLabel.Text = "Exporting " + e.UserState + "... " + e.ProgressPercentage + "%";
}
private void ExportProgressForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
exportBackgroundWorker.CancelAsync();
}
private void exportBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Close();
}
}
}