Skip to content

Commit e2cbf65

Browse files
authored
Merge pull request #1527 from Miepee/backslahes
Slightly clean up ExportAllSounds
2 parents 0ad6b32 + 21f05e9 commit e2cbf65

File tree

1 file changed

+42
-51
lines changed

1 file changed

+42
-51
lines changed

UndertaleModTool/Scripts/Resource Unpackers/ExportAllSounds.csx

+42-51
Original file line numberDiff line numberDiff line change
@@ -7,84 +7,72 @@ using System.Threading.Tasks;
77

88
EnsureDataLoaded();
99

10-
int maxCount;
11-
1210
// Setup root export folder.
1311
string winFolder = GetFolder(FilePath); // The folder data.win is located in.
14-
bool usesAGRP = (Data.AudioGroups.Count > 0);
12+
bool usesAGRP = (Data.AudioGroups.Count > 0);
13+
string exportedSoundsDir = Path.Combine(winFolder, "Exported_Sounds");
1514

1615
//Overwrite Folder Check One
17-
if (Directory.Exists(winFolder + "Exported_Sounds\\"))
16+
if (Directory.Exists(exportedSoundsDir))
1817
{
19-
bool overwriteCheckOne = ScriptQuestion(@"A 'Exported_Sounds' folder already exists.
20-
Would you like to remove it? This may some time.
18+
bool overwriteCheckOne = ScriptQuestion(@"An 'Exported_Sounds' folder already exists.
19+
20+
Would you like to remove it? This may take some time.
2121
2222
Note: If an error window stating that 'the directory is not empty' appears, please try again or delete the folder manually.
2323
");
24-
if (overwriteCheckOne)
25-
Directory.Delete(winFolder + "Exported_Sounds\\", true);
2624
if (!overwriteCheckOne)
2725
{
28-
ScriptError("A 'Exported_Sounds' folder already exists. Please remove it.", "Error: Export already exists.");
26+
ScriptError("An 'Exported_Sounds' folder already exists. Please remove it.", "Error: Export already exists.");
2927
return;
3028
}
29+
Directory.Delete(exportedSoundsDir, true);
3130
}
3231

33-
var externalOGG_Copy = 0;
34-
3532
// EXTERNAL OGG CHECK
36-
bool externalOGGs = ScriptQuestion(@"This script exports embedded sounds.
33+
bool externalOGG_Copy = ScriptQuestion(@"This script exports embedded sounds.
3734
However, it can also export the external OGGs to a separate folder.
3835
If you would like to export both, select 'YES'.
3936
If you just want the embedded sounds, select 'NO'.
4037
");
4138

42-
if (externalOGGs)
43-
externalOGG_Copy = 1;
44-
if (!externalOGGs)
45-
externalOGG_Copy = 0;
46-
4739
// Overwrite Folder Check Two
48-
if (Directory.Exists(winFolder + "External_Sounds\\") && externalOGG_Copy == 1)
40+
if (Directory.Exists(exportedSoundsDir) && externalOGG_Copy)
4941
{
5042
bool overwriteCheckTwo = ScriptQuestion(@"A 'External_Sounds' folder already exists.
5143
Would you like to remove it? This may some time.
5244
5345
Note: If an error window stating that 'the directory is not empty' appears, please try again or delete the folder manually.
5446
");
55-
if (overwriteCheckTwo)
56-
Directory.Delete(winFolder + "External_Sounds\\", true);
5747
if (!overwriteCheckTwo)
5848
{
5949
ScriptError("A 'External_Sounds' folder already exists. Please remove it.", "Error: Export already exists.");
6050
return;
6151
}
52+
53+
Directory.Delete(exportedSoundsDir, true);
6254
}
6355

6456
// Group by audio group check
65-
var groupedExport = 0;
57+
bool groupedExport;
6658
if (usesAGRP)
6759
{
68-
bool groupedCheck = ScriptQuestion(@"Group sounds by audio group?
69-
");
70-
if (groupedCheck)
71-
groupedExport = 1;
72-
if (!groupedCheck)
73-
groupedExport = 0;
60+
groupedExport = ScriptQuestion("Group sounds by audio group?");
61+
7462
}
7563

7664
byte[] EMPTY_WAV_FILE_BYTES = System.Convert.FromBase64String("UklGRiQAAABXQVZFZm10IBAAAAABAAIAQB8AAAB9AAAEABAAZGF0YQAAAAA=");
7765
string DEFAULT_AUDIOGROUP_NAME = "audiogroup_default";
7866

79-
maxCount = Data.Sounds.Count;
67+
int maxCount = Data.Sounds.Count;
8068
SetProgressBar(null, "Sound", 0, maxCount);
8169
StartProgressBarUpdater();
8270

8371
await Task.Run(DumpSounds); // This runs sync, because it has to load audio groups.
8472

8573
await StopProgressBarUpdater();
8674
HideProgressBar();
87-
if (Directory.Exists(winFolder + "External_Sounds\\"))
75+
if (Directory.Exists(exportedSoundsDir))
8876
ScriptMessage("Sounds exported to " + winFolder + " in the 'Exported_Sounds' and 'External_Sounds' folders.");
8977
else
9078
ScriptMessage("Sounds exported to " + winFolder + " in the 'Exported_Sounds' folder.");
@@ -95,10 +83,10 @@ void IncProgressLocal()
9583
IncrementProgress();
9684
}
9785

98-
void MakeFolder(String folderName)
86+
void MakeFolder(string folderName)
9987
{
100-
if (!Directory.Exists(winFolder + folderName + "/"))
101-
Directory.CreateDirectory(winFolder + folderName + "/");
88+
string fullPath = Path.Combine(winFolder, folderName);
89+
Directory.CreateDirectory(fullPath);
10290
}
10391

10492
string GetFolder(string path)
@@ -109,14 +97,14 @@ string GetFolder(string path)
10997
Dictionary<string, IList<UndertaleEmbeddedAudio>> loadedAudioGroups;
11098
IList<UndertaleEmbeddedAudio> GetAudioGroupData(UndertaleSound sound)
11199
{
112-
if (loadedAudioGroups == null)
100+
if (loadedAudioGroups is null)
113101
loadedAudioGroups = new Dictionary<string, IList<UndertaleEmbeddedAudio>>();
114102

115-
string audioGroupName = sound.AudioGroup != null ? sound.AudioGroup.Name.Content : DEFAULT_AUDIOGROUP_NAME;
103+
string audioGroupName = sound.AudioGroup is not null ? sound.AudioGroup.Name.Content : DEFAULT_AUDIOGROUP_NAME;
116104
if (loadedAudioGroups.ContainsKey(audioGroupName))
117105
return loadedAudioGroups[audioGroupName];
118106

119-
string groupFilePath = winFolder + "audiogroup" + sound.GroupID + ".dat";
107+
string groupFilePath = Path.Combine(winFolder, "audiogroup" + sound.GroupID + ".dat");
120108
if (!File.Exists(groupFilePath))
121109
return null; // Doesn't exist.
122110

@@ -128,7 +116,8 @@ IList<UndertaleEmbeddedAudio> GetAudioGroupData(UndertaleSound sound)
128116

129117
loadedAudioGroups[audioGroupName] = data.EmbeddedAudio;
130118
return data.EmbeddedAudio;
131-
} catch (Exception e)
119+
}
120+
catch (Exception e)
132121
{
133122
ScriptMessage("An error occured while trying to load " + audioGroupName + ":\n" + e.Message);
134123
return null;
@@ -137,13 +126,13 @@ IList<UndertaleEmbeddedAudio> GetAudioGroupData(UndertaleSound sound)
137126

138127
byte[] GetSoundData(UndertaleSound sound)
139128
{
140-
if (sound.AudioFile != null)
129+
if (sound.AudioFile is not null)
141130
return sound.AudioFile.Data;
142131

143132
if (sound.GroupID > Data.GetBuiltinSoundGroupID())
144133
{
145134
IList<UndertaleEmbeddedAudio> audioGroup = GetAudioGroupData(sound);
146-
if (audioGroup != null)
135+
if (audioGroup is not null)
147136
return audioGroup[sound.AudioID].Data;
148137
}
149138
return EMPTY_WAV_FILE_BYTES;
@@ -168,13 +157,14 @@ void DumpSound(UndertaleSound sound)
168157
// 4 = 110 = Regular. '.ogg' type saved outside win.
169158
string audioExt = ".ogg";
170159
string soundFilePath;
171-
if (groupedExport == 1)
172-
soundFilePath = winFolder + "Exported_Sounds\\" + sound.AudioGroup.Name.Content + "\\" + soundName;
160+
if (groupedExport)
161+
soundFilePath = Path.Combine(exportedSoundsDir, sound.AudioGroup.Name.Content, soundName);
173162
else
174-
soundFilePath = winFolder + "Exported_Sounds\\" + soundName;
163+
soundFilePath = Path.Combine(exportedSoundsDir, soundName);
175164
MakeFolder("Exported_Sounds");
176-
if (groupedExport == 1)
177-
MakeFolder("Exported_Sounds\\" + sound.AudioGroup.Name.Content);
165+
if (groupedExport)
166+
MakeFolder(Path.Combine("Exported_Sounds", sound.AudioGroup.Name.Content));
167+
178168
bool process = true;
179169
if (flagEmbedded && !flagCompressed) // 1.
180170
audioExt = ".wav";
@@ -186,17 +176,18 @@ void DumpSound(UndertaleSound sound)
186176
{
187177
process = false;
188178
audioExt = ".ogg";
189-
string source = winFolder + soundName + audioExt;
190-
string dest = winFolder + "External_Sounds\\" + soundName + audioExt;
191-
if (externalOGG_Copy == 1)
179+
string source = Path.Combine(winFolder, soundName + audioExt);
180+
string dest = Path.Combine(winFolder, "External_Sounds", soundName + audioExt);
181+
if (externalOGG_Copy)
192182
{
193-
if (groupedExport == 1)
183+
if (groupedExport)
194184
{
195-
dest = winFolder + "External_Sounds\\" + sound.AudioGroup.Name.Content + "\\" + soundName + audioExt;
196-
MakeFolder("External_Sounds\\" + sound.AudioGroup.Name.Content);
185+
dest = Path.Combine(winFolder, "External_Sounds", sound.AudioGroup.Name.Content, soundName + audioExt);
186+
187+
MakeFolder(Path.Combine("External_Sounds", sound.AudioGroup.Name.Content));
197188
}
198-
MakeFolder("External_Sounds\\");
199-
System.IO.File.Copy(source, dest, false);
189+
MakeFolder("External_Sounds");
190+
File.Copy(source, dest, false);
200191
}
201192
}
202193
if (process && !File.Exists(soundFilePath + audioExt))

0 commit comments

Comments
 (0)