Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slightly clean up ExportAllSounds #1527

Merged
merged 4 commits into from
Dec 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 42 additions & 51 deletions UndertaleModTool/Scripts/Resource Unpackers/ExportAllSounds.csx
Original file line number Diff line number Diff line change
Expand Up @@ -7,84 +7,72 @@ using System.Threading.Tasks;

EnsureDataLoaded();

int maxCount;

// Setup root export folder.
string winFolder = GetFolder(FilePath); // The folder data.win is located in.
bool usesAGRP = (Data.AudioGroups.Count > 0);
bool usesAGRP = (Data.AudioGroups.Count > 0);
string exportedSoundsDir = Path.Combine(winFolder, "Exported_Sounds");

//Overwrite Folder Check One
if (Directory.Exists(winFolder + "Exported_Sounds\\"))
if (Directory.Exists(exportedSoundsDir))
{
bool overwriteCheckOne = ScriptQuestion(@"A 'Exported_Sounds' folder already exists.
Would you like to remove it? This may some time.
bool overwriteCheckOne = ScriptQuestion(@"An 'Exported_Sounds' folder already exists.

Would you like to remove it? This may take some time.

Note: If an error window stating that 'the directory is not empty' appears, please try again or delete the folder manually.
");
if (overwriteCheckOne)
Directory.Delete(winFolder + "Exported_Sounds\\", true);
if (!overwriteCheckOne)
{
ScriptError("A 'Exported_Sounds' folder already exists. Please remove it.", "Error: Export already exists.");
ScriptError("An 'Exported_Sounds' folder already exists. Please remove it.", "Error: Export already exists.");
return;
}
Directory.Delete(exportedSoundsDir, true);
}

var externalOGG_Copy = 0;

// EXTERNAL OGG CHECK
bool externalOGGs = ScriptQuestion(@"This script exports embedded sounds.
bool externalOGG_Copy = ScriptQuestion(@"This script exports embedded sounds.
However, it can also export the external OGGs to a separate folder.
If you would like to export both, select 'YES'.
If you just want the embedded sounds, select 'NO'.
");

if (externalOGGs)
externalOGG_Copy = 1;
if (!externalOGGs)
externalOGG_Copy = 0;

// Overwrite Folder Check Two
if (Directory.Exists(winFolder + "External_Sounds\\") && externalOGG_Copy == 1)
if (Directory.Exists(exportedSoundsDir) && externalOGG_Copy)
{
bool overwriteCheckTwo = ScriptQuestion(@"A 'External_Sounds' folder already exists.
Would you like to remove it? This may some time.

Note: If an error window stating that 'the directory is not empty' appears, please try again or delete the folder manually.
");
if (overwriteCheckTwo)
Directory.Delete(winFolder + "External_Sounds\\", true);
if (!overwriteCheckTwo)
{
ScriptError("A 'External_Sounds' folder already exists. Please remove it.", "Error: Export already exists.");
return;
}

Directory.Delete(exportedSoundsDir, true);
}

// Group by audio group check
var groupedExport = 0;
bool groupedExport;
Miepee marked this conversation as resolved.
Show resolved Hide resolved
if (usesAGRP)
{
bool groupedCheck = ScriptQuestion(@"Group sounds by audio group?
");
if (groupedCheck)
groupedExport = 1;
if (!groupedCheck)
groupedExport = 0;
groupedExport = ScriptQuestion("Group sounds by audio group?");

}

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

maxCount = Data.Sounds.Count;
int maxCount = Data.Sounds.Count;
SetProgressBar(null, "Sound", 0, maxCount);
StartProgressBarUpdater();

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

await StopProgressBarUpdater();
HideProgressBar();
if (Directory.Exists(winFolder + "External_Sounds\\"))
if (Directory.Exists(exportedSoundsDir))
ScriptMessage("Sounds exported to " + winFolder + " in the 'Exported_Sounds' and 'External_Sounds' folders.");
else
ScriptMessage("Sounds exported to " + winFolder + " in the 'Exported_Sounds' folder.");
Expand All @@ -95,10 +83,10 @@ void IncProgressLocal()
IncrementProgress();
}

void MakeFolder(String folderName)
void MakeFolder(string folderName)
{
if (!Directory.Exists(winFolder + folderName + "/"))
Directory.CreateDirectory(winFolder + folderName + "/");
string fullPath = Path.Combine(winFolder, folderName);
Directory.CreateDirectory(fullPath);
}

string GetFolder(string path)
Expand All @@ -109,14 +97,14 @@ string GetFolder(string path)
Dictionary<string, IList<UndertaleEmbeddedAudio>> loadedAudioGroups;
IList<UndertaleEmbeddedAudio> GetAudioGroupData(UndertaleSound sound)
{
if (loadedAudioGroups == null)
if (loadedAudioGroups is null)
loadedAudioGroups = new Dictionary<string, IList<UndertaleEmbeddedAudio>>();

string audioGroupName = sound.AudioGroup != null ? sound.AudioGroup.Name.Content : DEFAULT_AUDIOGROUP_NAME;
string audioGroupName = sound.AudioGroup is not null ? sound.AudioGroup.Name.Content : DEFAULT_AUDIOGROUP_NAME;
if (loadedAudioGroups.ContainsKey(audioGroupName))
return loadedAudioGroups[audioGroupName];

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

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

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

byte[] GetSoundData(UndertaleSound sound)
{
if (sound.AudioFile != null)
if (sound.AudioFile is not null)
return sound.AudioFile.Data;

if (sound.GroupID > Data.GetBuiltinSoundGroupID())
{
IList<UndertaleEmbeddedAudio> audioGroup = GetAudioGroupData(sound);
if (audioGroup != null)
if (audioGroup is not null)
return audioGroup[sound.AudioID].Data;
}
return EMPTY_WAV_FILE_BYTES;
Expand All @@ -168,13 +157,14 @@ void DumpSound(UndertaleSound sound)
// 4 = 110 = Regular. '.ogg' type saved outside win.
string audioExt = ".ogg";
string soundFilePath;
if (groupedExport == 1)
soundFilePath = winFolder + "Exported_Sounds\\" + sound.AudioGroup.Name.Content + "\\" + soundName;
if (groupedExport)
soundFilePath = Path.Combine(exportedSoundsDir, sound.AudioGroup.Name.Content, soundName);
else
soundFilePath = winFolder + "Exported_Sounds\\" + soundName;
soundFilePath = Path.Combine(exportedSoundsDir, soundName);
MakeFolder("Exported_Sounds");
if (groupedExport == 1)
MakeFolder("Exported_Sounds\\" + sound.AudioGroup.Name.Content);
if (groupedExport)
MakeFolder(Path.Combine("Exported_Sounds", sound.AudioGroup.Name.Content));

bool process = true;
if (flagEmbedded && !flagCompressed) // 1.
audioExt = ".wav";
Expand All @@ -186,17 +176,18 @@ void DumpSound(UndertaleSound sound)
{
process = false;
audioExt = ".ogg";
string source = winFolder + soundName + audioExt;
string dest = winFolder + "External_Sounds\\" + soundName + audioExt;
if (externalOGG_Copy == 1)
string source = Path.Combine(winFolder, soundName + audioExt);
string dest = Path.Combine(winFolder, "External_Sounds", soundName + audioExt);
if (externalOGG_Copy)
{
if (groupedExport == 1)
if (groupedExport)
{
dest = winFolder + "External_Sounds\\" + sound.AudioGroup.Name.Content + "\\" + soundName + audioExt;
MakeFolder("External_Sounds\\" + sound.AudioGroup.Name.Content);
dest = Path.Combine(winFolder, "External_Sounds", sound.AudioGroup.Name.Content, soundName + audioExt);

MakeFolder(Path.Combine("External_Sounds", sound.AudioGroup.Name.Content));
}
MakeFolder("External_Sounds\\");
System.IO.File.Copy(source, dest, false);
MakeFolder("External_Sounds");
File.Copy(source, dest, false);
}
}
if (process && !File.Exists(soundFilePath + audioExt))
Expand Down