From 97128d295aafb4889509d61cc85b4834471be6bc Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 16 Apr 2016 20:56:59 +1000 Subject: [PATCH] Seamlessly convert the .bin files to .wav sounds for the user. --- Launcher2/Launcher2.csproj | 1 + Launcher2/LauncherWindow.cs | 5 +++ Launcher2/Patcher/BinUnpacker.cs | 76 ++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 Launcher2/Patcher/BinUnpacker.cs diff --git a/Launcher2/Launcher2.csproj b/Launcher2/Launcher2.csproj index 2f2b484b8..0826fd9b4 100644 --- a/Launcher2/Launcher2.csproj +++ b/Launcher2/Launcher2.csproj @@ -81,6 +81,7 @@ + diff --git a/Launcher2/LauncherWindow.cs b/Launcher2/LauncherWindow.cs index 5accff8ad..689b91c33 100644 --- a/Launcher2/LauncherWindow.cs +++ b/Launcher2/LauncherWindow.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Drawing; +using System.IO; using System.Net; using System.Reflection; using System.Threading; @@ -149,6 +150,10 @@ namespace Launcher { TryLoadTexturePack(); platformDrawer.Init( Window.WindowInfo ); + string audioPath = Path.Combine( Program.AppDirectory, "audio" ); + BinUnpacker.Unpack( audioPath, "dig" ); + BinUnpacker.Unpack( audioPath, "step" ); + fetcher = new ResourceFetcher(); fetcher.CheckResourceExistence(); checkTask = new UpdateCheckTask(); diff --git a/Launcher2/Patcher/BinUnpacker.cs b/Launcher2/Patcher/BinUnpacker.cs new file mode 100644 index 000000000..3e01587bd --- /dev/null +++ b/Launcher2/Patcher/BinUnpacker.cs @@ -0,0 +1,76 @@ +// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT +using System; +using System.Drawing; +using System.Drawing.Imaging; +using System.IO; +using ClassicalSharp; +using ClassicalSharp.TexturePack; + +namespace Launcher { + + public static class BinUnpacker { + + public static void Unpack( string dir, string group ) { + string binFile = Path.Combine( dir, group + ".bin" ); + if( !File.Exists( binFile ) ) return; + + byte[] data = File.ReadAllBytes( binFile ); + UnpackBin( data, dir, group ); + File.Delete( binFile ); + } + + static void UnpackBin( byte[] data, string dir, string group ) { + string propsFile = Path.Combine( dir, group + ".txt" ); + using( StreamReader reader = new StreamReader( propsFile ) ) { + string line; + while( (line = reader.ReadLine()) != null ) { + if( line.Length == 0 || line[0] == '#' ) continue; + string[] parts = line.Split( ',' ); + if( parts.Length < 6 ) continue; + string name = parts[0].ToLower(); + int sampleRate, bitsPerSample, channels; + int offset, length; + + if( !Int32.TryParse( parts[1], out sampleRate ) || + !Int32.TryParse( parts[2], out bitsPerSample ) || + !Int32.TryParse( parts[3], out channels ) || + !Int32.TryParse( parts[4], out offset ) || + !Int32.TryParse( parts[5], out length ) ) + continue; + + string file = Path.Combine( dir, group + "_" + name + ".wav" ); + using( FileStream fs = File.Create( file ) ) + WriteWave( fs, data, offset, length, + channels, sampleRate, bitsPerSample ); + } + } + File.Delete( propsFile ); + } + + static void WriteWave( Stream stream, byte[] data, int dataOffset, int dataLen, + int channels, int frequency, int bitsPerSample ) { + BinaryWriter w = new BinaryWriter( stream ); + WriteFourCC( w, "RIFF" ); + w.Write( 4 + (8 + 16) + (8 + dataLen) ); + WriteFourCC( w, "WAVE" ); + + WriteFourCC( w, "fmt " ); + w.Write( 16 ); + w.Write( (ushort)1 ); // audio format, PCM + w.Write( (ushort)channels ); + w.Write( frequency ); + w.Write((frequency * channels * bitsPerSample) / 8 ); // byte rate + w.Write( (ushort)((channels * bitsPerSample) / 8) ); // block align + w.Write( (ushort)bitsPerSample ); + + WriteFourCC( w, "data" ); + w.Write( dataLen ); + w.Write( data, dataOffset, dataLen ); + } + + static void WriteFourCC( BinaryWriter w, string fourCC ) { + w.Write( (byte)fourCC[0] ); w.Write( (byte)fourCC[1] ); + w.Write( (byte)fourCC[2] ); w.Write( (byte)fourCC[3] ); + } + } +}