Skip to content

Commit

Permalink
Add debug logging, fix volume edit re-encryption
Browse files Browse the repository at this point in the history
Also cleaned up code to make it a little clearer what's happening
  • Loading branch information
ShrineFox committed Dec 14, 2024
1 parent 658e3ff commit 1e1ad0d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 54 deletions.
4 changes: 2 additions & 2 deletions PersonaVCE.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

133 changes: 81 additions & 52 deletions PersonaVCE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ private void SetupDropdowns()
private void SetupLogging()
{
Output.Logging = true;
Output.LogControl = rtb_Log;

#if DEBUG
Output.VerboseLogging = true;
Output.LogControl = rtb_Log;
#endif
}

Expand Down Expand Up @@ -98,54 +99,85 @@ private void StartEncode(string[] inputFiles)
Output.Log($"[INFO] Done encoding files to \"{comboBox_SoundFormat.SelectedItem}\".", ConsoleColor.Green);
}

private void EncodeFile(string file, string outputDir)
private void EncodeFile(string inputFile, string outputDir)
{
bool encrypted = false;
string extension = Path.GetExtension(file).ToLower();
string ogInputFile = inputFile;
string inputExtension = Path.GetExtension(inputFile).ToLower();

string vgAudioPath = Path.Combine(Exe.Directory(), "Dependencies\\VGAudio.exe");

// Check if adx is already encrypted
if (extension == ".adx")
encrypted = CheckADXEncryption(file);

string args = $"\"{file}\"";
string args = $"\"{inputFile}\"";

// If volume does not equal 1.0, convert to WAV and adjust volume
string tempAdx = Path.Combine(outputDir, Path.GetFileNameWithoutExtension(inputFile) + "_temp.adx");
string tempWav = Path.Combine(outputDir, Path.GetFileNameWithoutExtension(inputFile) + "_temp.wav");
string volAdjustedWav = Path.Combine(outputDir, Path.GetFileNameWithoutExtension(inputFile) + ".wav");

string tempWav = Path.Combine(outputDir, Path.GetFileNameWithoutExtension(file) + "_temp.wav");
string volAdjustedWav = Path.Combine(outputDir, Path.GetFileNameWithoutExtension(file) + ".wav");
if (Convert.ToSingle(num_Volume.Value) != 1.0f)
{
string tempArgs = args + $" \"{tempWav}\"";
if (inputExtension == ".adx")
{
// If ADX is encrypted, remove encryption using keycode
if (num_EncryptionKey.Value == 0 || CheckADXEncryption(inputFile))
{
args = $"\"{inputFile}\" \"{tempAdx}\" --keycode {num_EncryptionKey.Value}";

// If file is encrypted, remove encryption with key.
if (num_EncryptionKey.Value != 0 && encrypted)
tempArgs += $" --keycode {num_EncryptionKey.Value}";
encrypted = false;
Output.Log("Decrypting temporary ADX...", ConsoleColor.Yellow);
Output.VerboseLog(args);
Exe.Run(vgAudioPath, args);
using (FileSys.WaitForFile(tempAdx)) { };

// Set input file to decrypted ADX
inputFile = tempAdx;
}

Exe.Run(vgAudioPath, tempArgs);
using (FileSys.WaitForFile(tempWav)) { };
// Create temporary WAV from input ADX
args = $"\"{inputFile}\" \"{tempWav}\"";

// Change WAV volume
using (var wavReader = new AudioFileReader(tempWav))
Output.Log($"Creating temp WAV: \"{tempWav}\"", ConsoleColor.Yellow);
Output.VerboseLog(args);
Exe.Run(vgAudioPath, args);
using (FileSys.WaitForFile(tempWav)) { };

// Delete temp ADX if it was made from a separate input file
if (tempAdx != ogInputFile && File.Exists(tempAdx))
{
Output.Log($"Deleting temporary ADX: \"{tempAdx}\"", ConsoleColor.Yellow);
File.Delete(tempAdx);
}

// Use temp WAV as input file
inputFile = tempWav;
}

// Change temp WAV volume and output as new WAV
using (var wavReader = new AudioFileReader(inputFile))
{
wavReader.Volume = Convert.ToSingle(num_Volume.Value);
SampleChannel channel = new SampleChannel(wavReader);
WaveFileWriter.CreateWaveFile16(volAdjustedWav, channel);
Output.Log($"Created volume adjusted WAV: \"{volAdjustedWav}\"", ConsoleColor.Yellow);
}

using (FileSys.WaitForFile(volAdjustedWav)) { };
args = $"\"{volAdjustedWav}\"";
// Delete non-volume adjusted WAV if it was made from a separate input file
if (tempWav != ogInputFile && File.Exists(tempWav))
{
Output.Log($"Deleting temporary WAV: \"{tempWav}\"", ConsoleColor.Yellow);
File.Delete(tempWav);
}

// Set input file to volume adjusted WAV
inputFile = volAdjustedWav;
args = $"\"{inputFile}\"";
}

string outPath = Path.Combine(outputDir, Path.GetFileNameWithoutExtension(file) + settings.OutFormat);
string outPath = Path.Combine(outputDir, Path.GetFileNameWithoutExtension(ogInputFile) + settings.OutFormat);
args += $" \"{outPath}\"";

// If file is encrypted, remove encryption with key.
// Otherwise, output will be encrypted with key
if (num_EncryptionKey.Value != 0 && encrypted && settings.Decrypt ||
num_EncryptionKey.Value != 0 && !encrypted && !settings.Decrypt)
// If file is encrypted and user wants output decrypted, use keycode to decrypt.
// Otherwise, output will be encrypted using keycode as long as it's not 0.
if (num_EncryptionKey.Value != 0 && CheckADXEncryption(inputFile) && settings.Decrypt ||
num_EncryptionKey.Value != 0 && !CheckADXEncryption(inputFile) && !settings.Decrypt)
args += $" --keycode {num_EncryptionKey.Value}";

// If loops are specified, use loops
Expand All @@ -154,12 +186,12 @@ private void EncodeFile(string file, string outputDir)
string loopArgs = " -l";

if (settings.LoopAll)
loopArgs += $" 0-{GetSampleCount(file) - 1}";
loopArgs += $" 0-{GetSampleCount(ogInputFile) - 1}";
else
{
if (extension == ".adx" && settings.UseExistingLoop)
if (inputExtension == ".adx" && settings.UseExistingLoop)
{
var loopPoints = GetADXLoopPoints(file);
var loopPoints = GetADXLoopPoints(ogInputFile);
loopArgs += $" {Convert.ToInt32(loopPoints.Item1)}-{Convert.ToInt32(loopPoints.Item2)}";
}
else
Expand All @@ -169,33 +201,27 @@ private void EncodeFile(string file, string outputDir)
args += loopArgs;
}

Output.VerboseLog($"[INFO] Encoding \"{Path.GetFileName(file)}\" to \"{Path.GetFileName(outPath)}\"...");
if (!File.Exists(vgAudioPath))
{
Output.VerboseLog($"[INFO] Failed to encode, could not find executable: \"{vgAudioPath}\"", ConsoleColor.DarkRed);
return;
}

Output.Log($"[INFO] Encoding \"{Path.GetFileName(inputFile)}\" to \"{Path.GetFileName(outPath)}\"...");
Output.VerboseLog(args);
Exe.Run(vgAudioPath, args);
using (FileSys.WaitForFile(outPath)) { };
if (File.Exists(outPath))

// Delete volume adjusted WAV
if (File.Exists(volAdjustedWav) && outPath != volAdjustedWav)
{
if (settings.OutFormat == ".adx" && num_EncryptionKey.Value != 0)
{
SetADXEncryptionFlag(outPath, encrypted);
}
Output.VerboseLog($"[INFO] Encoded file successfully!", ConsoleColor.DarkGreen);
Output.Log($"Deleting volume-adjusted WAV: \"{volAdjustedWav}\"", ConsoleColor.Yellow);
File.Delete(volAdjustedWav);
}
else
Output.VerboseLog($"[INFO] Failed to encode file: \"{file}\"", ConsoleColor.DarkRed);

if (Convert.ToSingle(num_Volume.Value) != 1.0f)
// Set ADX Type 9 encryption flag if output file exists and is encrypted
if (File.Exists(outPath))
{
if (File.Exists(tempWav))
File.Delete(tempWav);
if (File.Exists(volAdjustedWav))
File.Delete(volAdjustedWav);
if (settings.OutFormat == ".adx" && !settings.Decrypt && num_EncryptionKey.Value != 0)
SetADXEncryptionFlag(outPath);
Output.Log($"[INFO] Encoded file successfully!", ConsoleColor.DarkGreen);
}
else
Output.Log($"[INFO] Failed to encode file: \"{inputFile}\"", ConsoleColor.DarkRed);
}

private Tuple<int, int> GetADXLoopPoints(string file)
Expand Down Expand Up @@ -227,6 +253,9 @@ private Tuple<int, int> GetADXLoopPoints(string file)

private bool CheckADXEncryption(string file)
{
if (Path.GetExtension(file).ToLower() == ".wav")
return false;

using (FileStream fs = new FileStream(file, FileMode.Open))
{
using (BinaryReader reader = new BinaryReader(fs))
Expand All @@ -241,7 +270,7 @@ private bool CheckADXEncryption(string file)
}
}

private void SetADXEncryptionFlag(string outPath, bool encrypted)
private void SetADXEncryptionFlag(string outPath, bool encryptType9 = true)
{
using (FileStream fs = new FileStream(outPath, FileMode.Open))
{
Expand All @@ -251,7 +280,7 @@ private void SetADXEncryptionFlag(string outPath, bool encrypted)
writer.BaseStream.Position = 19;
byte newByte = Convert.ToByte(9);
// Remove encryption flag if decrypting
if (encrypted && settings.Decrypt)
if (!encryptType9)
newByte = Convert.ToByte(0);
Output.VerboseLog($"[INFO] Setting encryption byte to: {newByte.ToString("x2")}");
writer.Write(newByte);
Expand Down

0 comments on commit 1e1ad0d

Please sign in to comment.