Seamlessly convert the .bin files to .wav sounds for the user.

This commit is contained in:
UnknownShadow200 2016-04-16 20:56:59 +10:00
parent 3a8d699c2b
commit 97128d295a
3 changed files with 82 additions and 0 deletions

View File

@ -81,6 +81,7 @@
<Compile Include="LauncherWindow.Background.cs" />
<Compile Include="LauncherWindow.cs" />
<Compile Include="Patcher\Animations.cs" />
<Compile Include="Patcher\BinUnpacker.cs" />
<Compile Include="Patcher\ResourceFetcher.cs" />
<Compile Include="Patcher\ResourcePatcher.cs" />
<Compile Include="Patcher\SoundPatcher.cs" />

View File

@ -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();

View File

@ -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] );
}
}
}