Skip to content

Commit

Permalink
Merge pull request UnderminersTeam#1530 from Miepee/externalizeOggs
Browse files Browse the repository at this point in the history
Slightly clean up externalizeAllOGGs
  • Loading branch information
colinator27 authored Dec 22, 2023
2 parents 076d857 + d732f0f commit 0ad6b32
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions UndertaleModTool/Scripts/Community Scripts/ExternalizeAllOGGs.csx
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,32 @@ Otherwise, select 'No', and make a backup of the game before using this script.
}

//Overwrite Folder Check One
if (Directory.Exists(winFolder + "Exported_Sounds\\"))
string exportedSoundsPath = Path.Combine(winFolder, "Exported_Sounds");
if (Directory.Exists(exportedSoundsPath))
{
bool overwriteCheckOne = ScriptQuestion(@"An 'Exported_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 (overwriteCheckOne)
Directory.Delete(winFolder + "Exported_Sounds\\", true);
if (!overwriteCheckOne)
{
ScriptError("An 'Exported_Sounds' folder already exists. Please remove it.", "Error: Export already exists.");
return;
}

Directory.Delete(exportedSoundsPath, true);
}

// Group by audio group check
var groupedExport = 0;
bool groupedExport;
if (usesAGRPs)
{
bool groupedCheck = ScriptQuestion(@"Group sounds by audio group?
groupedExport = ScriptQuestion(@"Group sounds by audio group?
NOTE: Code using 'audio_group' functions will break.
You will have to check the code for these functions and change it accordingly.
");
if (groupedCheck)
groupedExport = 1;
if (!groupedCheck)
groupedExport = 0;
}

SetProgressBar(null, "Externalizing Sounds...", 0, Data.Sounds.Count);
Expand Down Expand Up @@ -93,7 +90,7 @@ void ExternalizeSound(UndertaleSound sound)
var _audioid = sound.AudioID;
var _fname = sound.File.Content;
string searchName = sound.Name.Content;
string searchFilePath = winFolder + "Exported_Sounds\\";
string searchFilePath = exportedSoundsPath;
// If it's not an external file already, setup the sound entry such that it is external.
if (flagCompressed == true || flagEmbedded == true)
{
Expand All @@ -110,15 +107,16 @@ void ExternalizeSound(UndertaleSound sound)
}
else //For sounds embedded in the external audiogroup.dat files.
{
var audioGroupReadStream = (new FileStream(winFolder + "audiogroup" + _groupid.ToString() + ".dat", FileMode.Open, FileAccess.Read)); // Load the audiogroup dat into memory
string audioGroupPath = Path.Combine(winFolder, "audiogroup" + _groupid.ToString() + ".dat");
var audioGroupReadStream = (new FileStream(audioGroupPath, FileMode.Open, FileAccess.Read)); // Load the audiogroup dat into memory
UndertaleData audioGroupDat = UndertaleIO.Read(audioGroupReadStream); // Load as UndertaleData
audioGroupReadStream.Dispose();
//make a one cell array and make it 0.
//Array.Resize(audioGroupDat.EmbeddedAudio[_audioid].Data, 1);
audioGroupDat.EmbeddedAudio[_audioid].Data = new byte[1];
audioGroupDat.EmbeddedAudio[_audioid].Data[0] = 0;

var audioGroupWriteStream = (new FileStream(winFolder + "audiogroup" + _groupid.ToString() + ".dat", FileMode.Create));
var audioGroupWriteStream = (new FileStream(audioGroupPath, FileMode.Create));
UndertaleIO.Write(audioGroupWriteStream, audioGroupDat); // Write it to the disk
audioGroupWriteStream.Dispose();
}
Expand All @@ -142,29 +140,28 @@ void ExternalizeSound(UndertaleSound sound)
IncrementProgress();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////

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

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

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 Down Expand Up @@ -214,12 +211,12 @@ void DumpSound(UndertaleSound sound)
// 3 = 101 = IsEmbedded, IsCompressed, Regular. '.ogg' type saved in win.
// 4 = 110 = Regular. '.ogg' type saved outside win.
string audioExt = ".ogg";
string soundFilePath = winFolder + "Exported_Sounds\\" + soundName;
if (groupedExport == 1)
soundFilePath = winFolder + "Exported_Sounds\\" + sound.AudioGroup.Name.Content + "\\" + soundName;
string soundFilePath = Path.Combine(exportedSoundsPath, soundName);
if (groupedExport)
soundFilePath = Path.Combine(exportedSoundsPath, sound.AudioGroup.Name.Content, 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 Down

0 comments on commit 0ad6b32

Please sign in to comment.