diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b803dcb --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.vs +/bin +/obj \ No newline at end of file diff --git a/AssemblyInfo.cs b/AssemblyInfo.cs index e6fb5e9..670ef23 100644 --- a/AssemblyInfo.cs +++ b/AssemblyInfo.cs @@ -3,14 +3,14 @@ using System.Resources; using System.Runtime.InteropServices; using System.Security.Permissions; -[assembly: AssemblyTitle("libWiiSharp_ModMii")] -[assembly: AssemblyDescription("a wii related .NET library modified for ModMii")] +[assembly: AssemblyTitle("libWiiSharp_Modified")] +[assembly: AssemblyDescription("a wii related .NET library modified to add features for othe programs.")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("Leathl,SC reproductions")] -[assembly: AssemblyProduct("libWiiSharp_ModMii")] -[assembly: AssemblyCopyright("Copyright © Leathl 2011")] +[assembly: AssemblyProduct("libWiiSharp_Modified")] +[assembly: AssemblyCopyright("Copyright © Leathl 2011, Copyright © Github Contributors 2020")] [assembly: AssemblyTrademark("")] -[assembly: ComVisible(false)] +[assembly: ComVisible(true)] [assembly: Guid("af701263-5875-4866-9c09-d7f62e9f0ff0")] [assembly: AssemblyFileVersion("0.4.0.0")] [assembly: NeutralResourcesLanguage("en")] diff --git a/BNS.cs b/BNS.cs index 14a6838..0f0718a 100644 --- a/BNS.cs +++ b/BNS.cs @@ -1,8 +1,20 @@ -// Decompiled with JetBrains decompiler -// Type: libWiiSharp.BNS -// Assembly: libWiiSharp, Version=0.4.0.0, Culture=neutral, PublicKeyToken=null -// MVID: FBF36F3D-B5D6-481F-B5F5-1BD3C19E13B2 -// Assembly location: C:\Users\theso\Downloads\NCPatcher\pack\libWiiSharp.dll +/* This file is part of libWiiSharp + * Copyright (C) 2009 Leathl + * Copyright (C) 2020 Github Contributors + * + * libWiiSharp is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * libWiiSharp is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ using System; using System.Collections.Generic; @@ -11,15 +23,16 @@ using System.IO; namespace libWiiSharp { - public class BNS : IDisposable - { - private BNS_Header bnsHeader = new BNS_Header(); - private BNS_Info bnsInfo = new BNS_Info(); - private BNS_Data bnsData = new BNS_Data(); - private int[,] lSamples = new int[2, 2]; - private int[,] rlSamples = new int[2, 2]; - private int[] tlSamples = new int[2]; - private int[] hbcDefTbl = new int[16] + public class BNS : IDisposable + { + + private BNS_Header bnsHeader = new BNS_Header(); + private BNS_Info bnsInfo = new BNS_Info(); + private BNS_Data bnsData = new BNS_Data(); + private int[,] lSamples = new int[2, 2]; + private int[,] rlSamples = new int[2, 2]; + private int[] tlSamples = new int[2]; + private int[] hbcDefTbl = new int[16] { 674, 1040, @@ -38,7 +51,7 @@ namespace libWiiSharp 3795, -1759 }; - private int[] defTbl = new int[16] + private int[] defTbl = new int[16] { 1820, -856, @@ -57,458 +70,527 @@ namespace libWiiSharp 3420, -1398 }; - private int[] pHist1 = new int[2]; - private int[] pHist2 = new int[2]; - private int tempSampleCount; - private byte[] waveFile; - private bool loopFromWave; - private bool converted; - private bool toMono; - private bool isDisposed; + private int[] pHist1 = new int[2]; + private int[] pHist2 = new int[2]; + private int tempSampleCount; + private byte[] waveFile; + private bool loopFromWave; + private bool converted; + private bool toMono; + private bool isDisposed; - public bool HasLoop - { - get => this.bnsInfo.HasLoop == (byte) 1; - set => this.bnsInfo.HasLoop = value ? (byte) 1 : (byte) 0; - } - - public uint LoopStart - { - get => this.bnsInfo.LoopStart; - set => this.bnsInfo.LoopStart = value; - } - - public uint TotalSampleCount - { - get => this.bnsInfo.LoopEnd; - set => this.bnsInfo.LoopEnd = value; - } - - public bool StereoToMono - { - get => this.toMono; - set => this.toMono = value; - } - - public event EventHandler Progress; - - protected BNS() - { - } - - public BNS(string waveFile) => this.waveFile = File.ReadAllBytes(waveFile); - - public BNS(string waveFile, bool loopFromWave) - { - this.waveFile = File.ReadAllBytes(waveFile); - this.loopFromWave = loopFromWave; - } - - public BNS(byte[] waveFile) => this.waveFile = waveFile; - - public BNS(byte[] waveFile, bool loopFromWave) - { - this.waveFile = waveFile; - this.loopFromWave = loopFromWave; - } - - ~BNS() => this.Dispose(false); - - public void Dispose() - { - this.Dispose(true); - GC.SuppressFinalize((object) this); - } - - protected virtual void Dispose(bool disposing) - { - if (disposing && !this.isDisposed) - { - this.bnsHeader = (BNS_Header) null; - this.bnsInfo = (BNS_Info) null; - this.bnsData = (BNS_Data) null; - this.lSamples = (int[,]) null; - this.rlSamples = (int[,]) null; - this.tlSamples = (int[]) null; - this.hbcDefTbl = (int[]) null; - this.pHist1 = (int[]) null; - this.pHist2 = (int[]) null; - this.waveFile = (byte[]) null; - } - this.isDisposed = true; - } - - public static int GetBnsLength(byte[] bnsFile) - { - uint num = (uint) Shared.Swap(BitConverter.ToUInt16(bnsFile, 44)); - return (int) (Shared.Swap(BitConverter.ToUInt32(bnsFile, 52)) / num); - } - - public void Convert() => this.convert(this.waveFile, this.loopFromWave); - - public byte[] ToByteArray() => this.ToMemoryStream().ToArray(); - - public MemoryStream ToMemoryStream() - { - if (!this.converted) - this.convert(this.waveFile, this.loopFromWave); - MemoryStream memoryStream = new MemoryStream(); - try - { - this.bnsHeader.Write((Stream) memoryStream); - this.bnsInfo.Write((Stream) memoryStream); - this.bnsData.Write((Stream) memoryStream); - return memoryStream; - } - catch - { - memoryStream.Dispose(); - throw; - } - } - - public void Save(string destionationFile) - { - if (File.Exists(destionationFile)) - File.Delete(destionationFile); - using (FileStream fileStream = new FileStream(destionationFile, FileMode.Create)) - { - byte[] array = this.ToMemoryStream().ToArray(); - fileStream.Write(array, 0, array.Length); - } - } - - public void SetLoop(int loopStartSample) - { - this.bnsInfo.HasLoop = (byte) 1; - this.bnsInfo.LoopStart = (uint) loopStartSample; - } - - private void convert(byte[] waveFile, bool loopFromWave) - { - Wave wave = new Wave(waveFile); - int numLoops = wave.NumLoops; - int loopStart = wave.LoopStart; - this.bnsInfo.ChannelCount = (byte) wave.NumChannels; - this.bnsInfo.SampleRate = (ushort) wave.SampleRate; - if (this.bnsInfo.ChannelCount > (byte) 2 || this.bnsInfo.ChannelCount < (byte) 1) - throw new Exception("Unsupported Amount of Channels!"); - if (wave.BitDepth != 16) - throw new Exception("Only 16bit Wave files are supported!"); - this.bnsData.Data = wave.DataFormat == 1 ? this.Encode(wave.SampleData) : throw new Exception("The format of this Wave file is not supported!"); - if (this.bnsInfo.ChannelCount == (byte) 1) - { - this.bnsHeader.InfoLength = 96U; - this.bnsHeader.DataOffset = 128U; - this.bnsInfo.Size = 96U; - this.bnsInfo.Channel1StartOffset = 28U; - this.bnsInfo.Channel2StartOffset = 0U; - this.bnsInfo.Channel1Start = 40U; - this.bnsInfo.Coefficients1Offset = 0U; - } - this.bnsData.Size = (uint) (this.bnsData.Data.Length + 8); - this.bnsHeader.DataLength = this.bnsData.Size; - this.bnsHeader.FileSize = (uint) this.bnsHeader.Size + this.bnsInfo.Size + this.bnsData.Size; - if (loopFromWave && numLoops == 1 && loopStart != -1) - { - this.bnsInfo.LoopStart = (uint) loopStart; - this.bnsInfo.HasLoop = (byte) 1; - } - this.bnsInfo.LoopEnd = (uint) this.tempSampleCount; - for (int index = 0; index < 16; ++index) - { - this.bnsInfo.Coefficients1[index] = this.defTbl[index]; - if (this.bnsInfo.ChannelCount == (byte) 2) - this.bnsInfo.Coefficients2[index] = this.defTbl[index]; - } - this.converted = true; - } - - private byte[] Encode(byte[] inputFrames) - { - int[] inputBuffer = new int[14]; - this.tempSampleCount = inputFrames.Length / (this.bnsInfo.ChannelCount == (byte) 2 ? 4 : 2); - int num1 = inputFrames.Length / (this.bnsInfo.ChannelCount == (byte) 2 ? 4 : 2) % 14; - Array.Resize(ref inputFrames, inputFrames.Length + (14 - num1) * (this.bnsInfo.ChannelCount == (byte) 2 ? 4 : 2)); - int num2 = inputFrames.Length / (this.bnsInfo.ChannelCount == (byte) 2 ? 4 : 2); - int num3 = (num2 + 13) / 14; - List intList1 = new List(); - List intList2 = new List(); - int startIndex = 0; - if (this.toMono && this.bnsInfo.ChannelCount == (byte) 2) - this.bnsInfo.ChannelCount = (byte) 1; - else if (this.toMono) - this.toMono = false; - for (int index = 0; index < num2; ++index) - { - intList1.Add((int) BitConverter.ToInt16(inputFrames, startIndex)); - startIndex += 2; - if (this.bnsInfo.ChannelCount == (byte) 2 || this.toMono) + /// + /// 0x00 (0) = No Loop, 0x01 (1) = Loop + /// + public bool HasLoop { - intList2.Add((int) BitConverter.ToInt16(inputFrames, startIndex)); - startIndex += 2; + get => this.bnsInfo.HasLoop == (byte) 1; + set => this.bnsInfo.HasLoop = value ? (byte) 1 : (byte) 0; } - } - byte[] numArray1 = new byte[this.bnsInfo.ChannelCount == (byte) 2 ? num3 * 16 : num3 * 8]; - int num4 = 0; - int num5 = num3 * 8; - this.bnsInfo.Channel2Start = this.bnsInfo.ChannelCount == (byte) 2 ? (uint) num5 : 0U; - int[] array1 = intList1.ToArray(); - int[] array2 = intList2.ToArray(); - for (int index1 = 0; index1 < num3; ++index1) - { - try + + /// + /// The start sample of the Loop + /// + public uint LoopStart { - if (index1 % (num3 / 100) != 0) - { - if (index1 + 1 != num3) - goto label_14; - } - this.ChangeProgress((index1 + 1) * 100 / num3); + get => this.bnsInfo.LoopStart; + set => this.bnsInfo.LoopStart = value; } - catch + + /// + /// The total number of samples in this file + /// + public uint TotalSampleCount { + get => this.bnsInfo.LoopEnd; + set => this.bnsInfo.LoopEnd = value; } -label_14: - for (int index2 = 0; index2 < 14; ++index2) - inputBuffer[index2] = array1[index1 * 14 + index2]; - byte[] numArray2 = this.RepackAdpcm(0, this.defTbl, inputBuffer); - for (int index2 = 0; index2 < 8; ++index2) - numArray1[num4 + index2] = numArray2[index2]; - num4 += 8; - if (this.bnsInfo.ChannelCount == (byte) 2) + + /// + /// If true and the input Wave file is stereo, the BNS will be converted to Mono. + /// Be sure to set this before you call Convert()! + /// + public bool StereoToMono { - for (int index2 = 0; index2 < 14; ++index2) - inputBuffer[index2] = array2[index1 * 14 + index2]; - byte[] numArray3 = this.RepackAdpcm(1, this.defTbl, inputBuffer); - for (int index2 = 0; index2 < 8; ++index2) - numArray1[num5 + index2] = numArray3[index2]; - num5 += 8; + get => this.toMono; + set => this.toMono = value; } - } - this.bnsInfo.LoopEnd = (uint) (num3 * 7); - return numArray1; - } - private byte[] RepackAdpcm(int index, int[] table, int[] inputBuffer) - { - byte[] numArray1 = new byte[8]; - int[] numArray2 = new int[2]; - double num1 = 999999999.0; - for (int tableIndex = 0; tableIndex < 8; ++tableIndex) - { - double outError; - byte[] numArray3 = this.CompressAdpcm(index, table, tableIndex, inputBuffer, out outError); - if (outError < num1) + public event EventHandler Progress; + + protected BNS() { - num1 = outError; - for (int index1 = 0; index1 < 8; ++index1) - numArray1[index1] = numArray3[index1]; - for (int index1 = 0; index1 < 2; ++index1) - numArray2[index1] = this.tlSamples[index1]; - } - } - for (int index1 = 0; index1 < 2; ++index1) - { - int[,] rlSamples = this.rlSamples; - int num2 = index1; - int index2 = index; - int index3 = num2; - int num3 = numArray2[index1]; - rlSamples[index2, index3] = num3; - } - return numArray1; - } - private byte[] CompressAdpcm( - int index, - int[] table, - int tableIndex, - int[] inputBuffer, - out double outError) - { - byte[] numArray = new byte[8]; - int num1 = 0; - int num2 = table[2 * tableIndex]; - int num3 = table[2 * tableIndex + 1]; - int stdExponent = this.DetermineStdExponent(index, table, tableIndex, inputBuffer); - while (stdExponent <= 15) - { - bool flag = false; - num1 = 0; - numArray[0] = (byte) (stdExponent | tableIndex << 4); - for (int index1 = 0; index1 < 2; ++index1) - this.tlSamples[index1] = this.rlSamples[index, index1]; - int num4 = 0; - for (int index1 = 0; index1 < 14; ++index1) + } + + public BNS(string waveFile) => this.waveFile = File.ReadAllBytes(waveFile); + + public BNS(string waveFile, bool loopFromWave) { - int num5 = this.tlSamples[1] * num2 + this.tlSamples[0] * num3 >> 11; - int input1 = inputBuffer[index1] - num5 >> stdExponent; - if (input1 <= 7 && input1 >= -8) - { - int num6 = this.Clamp(input1, -8, 7); - numArray[index1 / 2 + 1] = (index1 & 1) == 0 ? (byte) (num6 << 4) : (byte) ((uint) numArray[index1 / 2 + 1] | (uint) (num6 & 15)); - int input2 = num5 + (num6 << stdExponent); - this.tlSamples[0] = this.tlSamples[1]; - this.tlSamples[1] = this.Clamp(input2, (int) short.MinValue, (int) short.MaxValue); - num1 += (int) Math.Pow((double) (this.tlSamples[1] - inputBuffer[index1]), 2.0); - } - else - { - ++stdExponent; - flag = true; - break; - } + this.waveFile = File.ReadAllBytes(waveFile); + this.loopFromWave = loopFromWave; } - if (!flag) - num4 = 14; - if (num4 == 14) - break; - } - outError = (double) num1; - return numArray; - } - private int DetermineStdExponent(int index, int[] table, int tableIndex, int[] inputBuffer) - { - int[] numArray = new int[2]; - int num1 = 0; - int num2 = table[2 * tableIndex]; - int num3 = table[2 * tableIndex + 1]; - for (int index1 = 0; index1 < 2; ++index1) - numArray[index1] = this.rlSamples[index, index1]; - for (int index1 = 0; index1 < 14; ++index1) - { - int num4 = numArray[1] * num2 + numArray[0] * num3 >> 11; - int num5 = inputBuffer[index1] - num4; - if (num5 > num1) - num1 = num5; - numArray[0] = numArray[1]; - numArray[1] = inputBuffer[index1]; - } - return this.FindExponent((double) num1); - } + public BNS(byte[] waveFile) => this.waveFile = waveFile; - private int FindExponent(double residual) - { - int num = 0; - for (; residual > 7.5 || residual < -8.5; residual /= 2.0) - ++num; - return num; - } - - private int Clamp(int input, int min, int max) - { - if (input < min) - return min; - return input > max ? max : input; - } - - private void ChangeProgress(int progressPercentage) - { - EventHandler progress = this.Progress; - if (progress == null) - return; - progress(new object(), new ProgressChangedEventArgs(progressPercentage, new object())); - } - - public static Wave BnsToWave(Stream inputFile) - { - BNS bns = new BNS(); - byte[] samples = bns.Read(inputFile); - Wave wave = new Wave((int) bns.bnsInfo.ChannelCount, 16, (int) bns.bnsInfo.SampleRate, samples); - if (bns.bnsInfo.HasLoop == (byte) 1) - wave.AddLoop((int) bns.bnsInfo.LoopStart); - return wave; - } - - public static Wave BnsToWave(string pathToFile) - { - BNS bns = new BNS(); - byte[] samples = (byte[]) null; - using (FileStream fileStream = new FileStream(pathToFile, FileMode.Open)) - samples = bns.Read((Stream) fileStream); - Wave wave = new Wave((int) bns.bnsInfo.ChannelCount, 16, (int) bns.bnsInfo.SampleRate, samples); - if (bns.bnsInfo.HasLoop == (byte) 1) - wave.AddLoop((int) bns.bnsInfo.LoopStart); - return wave; - } - - public static Wave BnsToWave(byte[] bnsFile) - { - BNS bns = new BNS(); - byte[] samples = (byte[]) null; - using (MemoryStream memoryStream = new MemoryStream(bnsFile)) - samples = bns.Read((Stream) memoryStream); - Wave wave = new Wave((int) bns.bnsInfo.ChannelCount, 16, (int) bns.bnsInfo.SampleRate, samples); - if (bns.bnsInfo.HasLoop == (byte) 1) - wave.AddLoop((int) bns.bnsInfo.LoopStart); - return wave; - } - - private byte[] Read(Stream input) - { - input.Seek(0L, SeekOrigin.Begin); - this.bnsHeader.Read(input); - this.bnsInfo.Read(input); - this.bnsData.Read(input); - return this.Decode(); - } - - private byte[] Decode() - { - List byteList = new List(); - int num = this.bnsData.Data.Length / (this.bnsInfo.ChannelCount == (byte) 2 ? 16 : 8); - int dataOffset1 = 0; - int dataOffset2 = num * 8; - byte[] numArray1 = new byte[0]; - byte[] numArray2 = new byte[0]; - for (int index1 = 0; index1 < num; ++index1) - { - byte[] numArray3 = this.DecodeAdpcm(0, dataOffset1); - if (this.bnsInfo.ChannelCount == (byte) 2) - numArray2 = this.DecodeAdpcm(1, dataOffset2); - for (int index2 = 0; index2 < 14; ++index2) + public BNS(byte[] waveFile, bool loopFromWave) { - byteList.Add(numArray3[index2 * 2]); - byteList.Add(numArray3[index2 * 2 + 1]); - if (this.bnsInfo.ChannelCount == (byte) 2) - { - byteList.Add(numArray2[index2 * 2]); - byteList.Add(numArray2[index2 * 2 + 1]); - } + this.waveFile = waveFile; + this.loopFromWave = loopFromWave; } - dataOffset1 += 8; - if (this.bnsInfo.ChannelCount == (byte) 2) - dataOffset2 += 8; - } - return byteList.ToArray(); - } - private byte[] DecodeAdpcm(int channel, int dataOffset) - { - byte[] numArray = new byte[28]; - int num1 = (int) this.bnsData.Data[dataOffset] >> 4 & 15; - int num2 = 1 << ((int) this.bnsData.Data[dataOffset] & 15); - int num3 = this.pHist1[channel]; - int num4 = this.pHist2[channel]; - int num5 = channel == 0 ? this.bnsInfo.Coefficients1[num1 * 2] : this.bnsInfo.Coefficients2[num1 * 2]; - int num6 = channel == 0 ? this.bnsInfo.Coefficients1[num1 * 2 + 1] : this.bnsInfo.Coefficients2[num1 * 2 + 1]; - for (int index = 0; index < 14; ++index) - { - short num7 = (short) this.bnsData.Data[dataOffset + (index / 2 + 1)]; - int num8 = (index & 1) != 0 ? (int) num7 & 15 : (int) num7 >> 4; - if (num8 >= 8) - num8 -= 16; - int num9 = this.Clamp((num2 * num8 << 11) + (num5 * num3 + num6 * num4) + 1024 >> 11, (int) short.MinValue, (int) short.MaxValue); - numArray[index * 2] = (byte) ((uint) (short) num9 & (uint) byte.MaxValue); - numArray[index * 2 + 1] = (byte) ((uint) (short) num9 >> 8); - num4 = num3; - num3 = num9; - } - this.pHist1[channel] = num3; - this.pHist2[channel] = num4; - return numArray; + #region IDisposable Members + + ~BNS() => this.Dispose(false); + + public void Dispose() + { + this.Dispose(true); + GC.SuppressFinalize((object) this); + } + + protected virtual void Dispose(bool disposing) + { + if (disposing && !this.isDisposed) + { + this.bnsHeader = (BNS_Header) null; + this.bnsInfo = (BNS_Info) null; + this.bnsData = (BNS_Data) null; + this.lSamples = (int[,]) null; + this.rlSamples = (int[,]) null; + this.tlSamples = (int[]) null; + this.hbcDefTbl = (int[]) null; + this.pHist1 = (int[]) null; + this.pHist2 = (int[]) null; + this.waveFile = (byte[]) null; + } + + this.isDisposed = true; + + } + #endregion + + #region Public Functions + + /// + /// Returns the length of the BNS audio file in seconds + /// + /// + /// + public static int GetBnsLength(byte[] bnsFile) + { + uint sampleRate = (uint) Shared.Swap(BitConverter.ToUInt16(bnsFile, 44)); + uint sampleCount = Shared.Swap(BitConverter.ToUInt32(bnsFile, 52)); + + return (int)(sampleCount / sampleRate); + } + + /// + /// Converts the Wave file to BNS + /// + public void Convert() => this.convert(this.waveFile, this.loopFromWave); + + /// + /// Returns the BNS file as a Byte Array. If not already converted, it will be done first. + /// + /// + public byte[] ToByteArray() => this.ToMemoryStream().ToArray(); + + /// + /// Returns the BNS file as a Memory Stream. If not already converted, it will be done first. + /// + /// + public MemoryStream ToMemoryStream() + { + if (!this.converted) + this.convert(this.waveFile, this.loopFromWave); + MemoryStream memoryStream = new MemoryStream(); + try + { + this.bnsHeader.Write((Stream) memoryStream); + this.bnsInfo.Write((Stream) memoryStream); + this.bnsData.Write((Stream) memoryStream); + return memoryStream; + } + catch + { + memoryStream.Dispose(); + throw; + } + } + + /// + /// Saves the BNS file to the given path. If not already converted, it will be done first. + /// + /// + public void Save(string destinationFile) + { + if (File.Exists(destinationFile)) + File.Delete(destinationFile); + using (FileStream fileStream = new FileStream(destinationFile, FileMode.Create)) + { + byte[] array = this.ToMemoryStream().ToArray(); + fileStream.Write(array, 0, array.Length); + } + } + + /// + /// Sets the Loop to the given Start Sample. Be sure that you call Convert() first! + /// + /// + public void SetLoop(int loopStartSample) + { + this.bnsInfo.HasLoop = (byte) 1; + this.bnsInfo.LoopStart = (uint) loopStartSample; + } + + #endregion + + #region Private Functions + + private void convert(byte[] waveFile, bool loopFromWave) + { + Wave wave = new Wave(waveFile); + int numLoops = wave.NumLoops; + int loopStart = wave.LoopStart; + this.bnsInfo.ChannelCount = (byte) wave.NumChannels; + this.bnsInfo.SampleRate = (ushort) wave.SampleRate; + if (this.bnsInfo.ChannelCount > (byte) 2 || this.bnsInfo.ChannelCount < (byte) 1) + throw new Exception("Unsupported Amount of Channels!"); + if (wave.BitDepth != 16) + throw new Exception("Only 16bit Wave files are supported!"); + this.bnsData.Data = wave.DataFormat == 1 ? this.Encode(wave.SampleData) : throw new Exception("The format of this Wave file is not supported!"); + if (this.bnsInfo.ChannelCount == (byte) 1) + { + this.bnsHeader.InfoLength = 96U; + this.bnsHeader.DataOffset = 128U; + this.bnsInfo.Size = 96U; + this.bnsInfo.Channel1StartOffset = 28U; + this.bnsInfo.Channel2StartOffset = 0U; + this.bnsInfo.Channel1Start = 40U; + this.bnsInfo.Coefficients1Offset = 0U; + } + this.bnsData.Size = (uint) (this.bnsData.Data.Length + 8); + this.bnsHeader.DataLength = this.bnsData.Size; + this.bnsHeader.FileSize = (uint) this.bnsHeader.Size + this.bnsInfo.Size + this.bnsData.Size; + if (loopFromWave && numLoops == 1 && loopStart != -1) + { + this.bnsInfo.LoopStart = (uint) loopStart; + this.bnsInfo.HasLoop = (byte) 1; + } + this.bnsInfo.LoopEnd = (uint) this.tempSampleCount; + for (int index = 0; index < 16; ++index) + { + this.bnsInfo.Coefficients1[index] = this.defTbl[index]; + if (this.bnsInfo.ChannelCount == (byte) 2) + this.bnsInfo.Coefficients2[index] = this.defTbl[index]; + } + this.converted = true; + } + + private byte[] Encode(byte[] inputFrames) + { + int[] inputBuffer = new int[14]; + this.tempSampleCount = inputFrames.Length / (this.bnsInfo.ChannelCount == (byte) 2 ? 4 : 2); + int num1 = inputFrames.Length / (this.bnsInfo.ChannelCount == (byte) 2 ? 4 : 2) % 14; + Array.Resize(ref inputFrames, inputFrames.Length + (14 - num1) * (this.bnsInfo.ChannelCount == (byte) 2 ? 4 : 2)); + int num2 = inputFrames.Length / (this.bnsInfo.ChannelCount == (byte) 2 ? 4 : 2); + int num3 = (num2 + 13) / 14; + List intList1 = new List(); + List intList2 = new List(); + int startIndex = 0; + if (this.toMono && this.bnsInfo.ChannelCount == (byte) 2) + this.bnsInfo.ChannelCount = (byte) 1; + else if (this.toMono) + this.toMono = false; + for (int index = 0; index < num2; ++index) + { + intList1.Add((int) BitConverter.ToInt16(inputFrames, startIndex)); + startIndex += 2; + if (this.bnsInfo.ChannelCount == (byte) 2 || this.toMono) + { + intList2.Add((int) BitConverter.ToInt16(inputFrames, startIndex)); + startIndex += 2; + } + } + byte[] numArray1 = new byte[this.bnsInfo.ChannelCount == (byte) 2 ? num3 * 16 : num3 * 8]; + int num4 = 0; + int num5 = num3 * 8; + this.bnsInfo.Channel2Start = this.bnsInfo.ChannelCount == (byte) 2 ? (uint) num5 : 0U; + int[] array1 = intList1.ToArray(); + int[] array2 = intList2.ToArray(); + for (int index1 = 0; index1 < num3; ++index1) + { + try + { + if (index1 % (num3 / 100) != 0) + { + if (index1 + 1 != num3) + goto label_14; + } + this.ChangeProgress((index1 + 1) * 100 / num3); + } + catch + { + } + label_14: + for (int index2 = 0; index2 < 14; ++index2) + inputBuffer[index2] = array1[index1 * 14 + index2]; + byte[] numArray2 = this.RepackAdpcm(0, this.defTbl, inputBuffer); + for (int index2 = 0; index2 < 8; ++index2) + numArray1[num4 + index2] = numArray2[index2]; + num4 += 8; + if (this.bnsInfo.ChannelCount == (byte) 2) + { + for (int index2 = 0; index2 < 14; ++index2) + inputBuffer[index2] = array2[index1 * 14 + index2]; + byte[] numArray3 = this.RepackAdpcm(1, this.defTbl, inputBuffer); + for (int index2 = 0; index2 < 8; ++index2) + numArray1[num5 + index2] = numArray3[index2]; + num5 += 8; + } + } + this.bnsInfo.LoopEnd = (uint) (num3 * 7); + return numArray1; + } + + private byte[] RepackAdpcm(int index, int[] table, int[] inputBuffer) + { + byte[] numArray1 = new byte[8]; + int[] numArray2 = new int[2]; + double num1 = 999999999.0; + for (int tableIndex = 0; tableIndex < 8; ++tableIndex) + { + double outError; + byte[] numArray3 = this.CompressAdpcm(index, table, tableIndex, inputBuffer, out outError); + if (outError < num1) + { + num1 = outError; + for (int index1 = 0; index1 < 8; ++index1) + numArray1[index1] = numArray3[index1]; + for (int index1 = 0; index1 < 2; ++index1) + numArray2[index1] = this.tlSamples[index1]; + } + } + for (int index1 = 0; index1 < 2; ++index1) + { + int[,] rlSamples = this.rlSamples; + int num2 = index1; + int index2 = index; + int index3 = num2; + int num3 = numArray2[index1]; + rlSamples[index2, index3] = num3; + } + return numArray1; + } + + private byte[] CompressAdpcm( + int index, + int[] table, + int tableIndex, + int[] inputBuffer, + out double outError) + { + byte[] numArray = new byte[8]; + int num1 = 0; + int num2 = table[2 * tableIndex]; + int num3 = table[2 * tableIndex + 1]; + int stdExponent = this.DetermineStdExponent(index, table, tableIndex, inputBuffer); + while (stdExponent <= 15) + { + bool flag = false; + num1 = 0; + numArray[0] = (byte) (stdExponent | tableIndex << 4); + for (int index1 = 0; index1 < 2; ++index1) + this.tlSamples[index1] = this.rlSamples[index, index1]; + int num4 = 0; + for (int index1 = 0; index1 < 14; ++index1) + { + int num5 = this.tlSamples[1] * num2 + this.tlSamples[0] * num3 >> 11; + int input1 = inputBuffer[index1] - num5 >> stdExponent; + if (input1 <= 7 && input1 >= -8) + { + int num6 = this.Clamp(input1, -8, 7); + numArray[index1 / 2 + 1] = (index1 & 1) == 0 ? (byte) (num6 << 4) : (byte) ((uint) numArray[index1 / 2 + 1] | (uint) (num6 & 15)); + int input2 = num5 + (num6 << stdExponent); + this.tlSamples[0] = this.tlSamples[1]; + this.tlSamples[1] = this.Clamp(input2, (int) short.MinValue, (int) short.MaxValue); + num1 += (int) Math.Pow((double) (this.tlSamples[1] - inputBuffer[index1]), 2.0); + } + else + { + ++stdExponent; + flag = true; + break; + } + } + if (!flag) + num4 = 14; + if (num4 == 14) + break; + } + outError = (double) num1; + return numArray; + } + + private int DetermineStdExponent(int index, int[] table, int tableIndex, int[] inputBuffer) + { + int[] numArray = new int[2]; + int num1 = 0; + int num2 = table[2 * tableIndex]; + int num3 = table[2 * tableIndex + 1]; + for (int index1 = 0; index1 < 2; ++index1) + numArray[index1] = this.rlSamples[index, index1]; + for (int index1 = 0; index1 < 14; ++index1) + { + int num4 = numArray[1] * num2 + numArray[0] * num3 >> 11; + int num5 = inputBuffer[index1] - num4; + if (num5 > num1) + num1 = num5; + numArray[0] = numArray[1]; + numArray[1] = inputBuffer[index1]; + } + return this.FindExponent((double) num1); + } + + private int FindExponent(double residual) + { + int num = 0; + for (; residual > 7.5 || residual < -8.5; residual /= 2.0) + ++num; + return num; + } + + private int Clamp(int input, int min, int max) + { + if (input < min) + return min; + return input > max ? max : input; + } + + private void ChangeProgress(int progressPercentage) + { + EventHandler progress = this.Progress; + if (progress == null) + return; + progress(new object(), new ProgressChangedEventArgs(progressPercentage, new object())); + } + + #endregion + + #region BNS to Wave + + #region Public Functions + + /// + /// Converts a BNS audio file to Wave format. + /// + /// + /// + /// + public static Wave BnsToWave(Stream inputFile) + { + BNS bns = new BNS(); + byte[] samples = bns.Read(inputFile); + Wave wave = new Wave((int) bns.bnsInfo.ChannelCount, 16, (int) bns.bnsInfo.SampleRate, samples); + if (bns.bnsInfo.HasLoop == (byte) 1) + wave.AddLoop((int) bns.bnsInfo.LoopStart); + return wave; + } + + public static Wave BnsToWave(string pathToFile) + { + BNS bns = new BNS(); + byte[] samples = (byte[]) null; + using (FileStream fileStream = new FileStream(pathToFile, FileMode.Open)) + samples = bns.Read((Stream) fileStream); + Wave wave = new Wave((int) bns.bnsInfo.ChannelCount, 16, (int) bns.bnsInfo.SampleRate, samples); + if (bns.bnsInfo.HasLoop == (byte) 1) + wave.AddLoop((int) bns.bnsInfo.LoopStart); + return wave; + } + + public static Wave BnsToWave(byte[] bnsFile) + { + BNS bns = new BNS(); + byte[] samples = (byte[]) null; + using (MemoryStream memoryStream = new MemoryStream(bnsFile)) + samples = bns.Read((Stream) memoryStream); + Wave wave = new Wave((int) bns.bnsInfo.ChannelCount, 16, (int) bns.bnsInfo.SampleRate, samples); + if (bns.bnsInfo.HasLoop == (byte) 1) + wave.AddLoop((int) bns.bnsInfo.LoopStart); + return wave; + } + + #endregion + + #region Private Functions + + private byte[] Read(Stream input) + { + input.Seek(0L, SeekOrigin.Begin); + this.bnsHeader.Read(input); + this.bnsInfo.Read(input); + this.bnsData.Read(input); + return this.Decode(); + } + + private byte[] Decode() + { + List byteList = new List(); + int num = this.bnsData.Data.Length / (this.bnsInfo.ChannelCount == (byte) 2 ? 16 : 8); + int dataOffset1 = 0; + int dataOffset2 = num * 8; + byte[] numArray1 = new byte[0]; + byte[] numArray2 = new byte[0]; + for (int index1 = 0; index1 < num; ++index1) + { + byte[] numArray3 = this.DecodeAdpcm(0, dataOffset1); + if (this.bnsInfo.ChannelCount == (byte) 2) + numArray2 = this.DecodeAdpcm(1, dataOffset2); + for (int index2 = 0; index2 < 14; ++index2) + { + byteList.Add(numArray3[index2 * 2]); + byteList.Add(numArray3[index2 * 2 + 1]); + if (this.bnsInfo.ChannelCount == (byte) 2) + { + byteList.Add(numArray2[index2 * 2]); + byteList.Add(numArray2[index2 * 2 + 1]); + } + } + dataOffset1 += 8; + if (this.bnsInfo.ChannelCount == (byte) 2) + dataOffset2 += 8; + } + return byteList.ToArray(); + } + + private byte[] DecodeAdpcm(int channel, int dataOffset) + { + byte[] numArray = new byte[28]; + int num1 = (int) this.bnsData.Data[dataOffset] >> 4 & 15; + int num2 = 1 << ((int) this.bnsData.Data[dataOffset] & 15); + int num3 = this.pHist1[channel]; + int num4 = this.pHist2[channel]; + int num5 = channel == 0 ? this.bnsInfo.Coefficients1[num1 * 2] : this.bnsInfo.Coefficients2[num1 * 2]; + int num6 = channel == 0 ? this.bnsInfo.Coefficients1[num1 * 2 + 1] : this.bnsInfo.Coefficients2[num1 * 2 + 1]; + for (int index = 0; index < 14; ++index) + { + short num7 = (short) this.bnsData.Data[dataOffset + (index / 2 + 1)]; + int num8 = (index & 1) != 0 ? (int) num7 & 15 : (int) num7 >> 4; + if (num8 >= 8) + num8 -= 16; + int num9 = this.Clamp((num2 * num8 << 11) + (num5 * num3 + num6 * num4) + 1024 >> 11, (int) short.MinValue, (int) short.MaxValue); + numArray[index * 2] = (byte) ((uint) (short) num9 & (uint) byte.MaxValue); + numArray[index * 2 + 1] = (byte) ((uint) (short) num9 >> 8); + num4 = num3; + num3 = num9; + } + this.pHist1[channel] = num3; + this.pHist2[channel] = num4; + return numArray; + } + #endregion + #endregion } - } -} +} \ No newline at end of file diff --git a/BNS_Data.cs b/BNS_Data.cs index 5f68ebb..f8a1580 100644 --- a/BNS_Data.cs +++ b/BNS_Data.cs @@ -1,51 +1,63 @@ -// Decompiled with JetBrains decompiler -// Type: libWiiSharp.BNS_Data -// Assembly: libWiiSharp, Version=0.4.0.0, Culture=neutral, PublicKeyToken=null -// MVID: FBF36F3D-B5D6-481F-B5F5-1BD3C19E13B2 -// Assembly location: C:\Users\theso\Downloads\NCPatcher\pack\libWiiSharp.dll +/* This file is part of libWiiSharp + * Copyright (C) 2009 Leathl + * Copyright (C) 2020 Github Contributors + * + * libWiiSharp is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * libWiiSharp is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ using System; using System.IO; namespace libWiiSharp { - internal class BNS_Data - { - private byte[] magic = new byte[4] + internal class BNS_Data { - (byte) 68, - (byte) 65, - (byte) 84, - (byte) 65 - }; - private uint size = 315392; - private byte[] data; + private byte[] magic = new byte[4] + { + (byte) 68, + (byte) 65, + (byte) 84, + (byte) 65 + }; + private uint size = 315392; + private byte[] data; - public uint Size - { - get => this.size; - set => this.size = value; - } + public uint Size + { + get => this.size; + set => this.size = value; + } - public byte[] Data - { - get => this.data; - set => this.data = value; - } + public byte[] Data + { + get => this.data; + set => this.data = value; + } - public void Write(Stream outStream) - { - byte[] bytes = BitConverter.GetBytes(Shared.Swap(this.size)); - outStream.Write(this.magic, 0, this.magic.Length); - outStream.Write(bytes, 0, bytes.Length); - outStream.Write(this.data, 0, this.data.Length); - } + public void Write(Stream outStream) + { + byte[] bytes = BitConverter.GetBytes(Shared.Swap(this.size)); + outStream.Write(this.magic, 0, this.magic.Length); + outStream.Write(bytes, 0, bytes.Length); + outStream.Write(this.data, 0, this.data.Length); + } - public void Read(Stream input) - { - BinaryReader binaryReader = new BinaryReader(input); - this.size = Shared.CompareByteArrays(this.magic, binaryReader.ReadBytes(4)) ? Shared.Swap(binaryReader.ReadUInt32()) : throw new Exception("This is not a valid BNS audfo file!"); - this.data = binaryReader.ReadBytes((int) this.size - 8); + public void Read(Stream input) + { + BinaryReader binaryReader = new BinaryReader(input); + this.size = Shared.CompareByteArrays(this.magic, binaryReader.ReadBytes(4)) ? Shared.Swap(binaryReader.ReadUInt32()) : throw new Exception("This is not a valid BNS audfo file!"); + this.data = binaryReader.ReadBytes((int) this.size - 8); + } } - } } diff --git a/BNS_Header.cs b/BNS_Header.cs index 84db688..df463dc 100644 --- a/BNS_Header.cs +++ b/BNS_Header.cs @@ -1,108 +1,120 @@ -// Decompiled with JetBrains decompiler -// Type: libWiiSharp.BNS_Header -// Assembly: libWiiSharp, Version=0.4.0.0, Culture=neutral, PublicKeyToken=null -// MVID: FBF36F3D-B5D6-481F-B5F5-1BD3C19E13B2 -// Assembly location: C:\Users\theso\Downloads\NCPatcher\pack\libWiiSharp.dll +/* This file is part of libWiiSharp + * Copyright (C) 2009 Leathl + * Copyright (C) 2020 Github Contributors + * + * libWiiSharp is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * libWiiSharp is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ using System; using System.IO; namespace libWiiSharp { - internal class BNS_Header - { - private byte[] magic = new byte[4] + internal class BNS_Header { - (byte) 66, - (byte) 78, - (byte) 83, - (byte) 32 - }; - private uint flags = 4278124800; - private uint fileSize = 315584; - private ushort size = 32; - private ushort chunkCount = 2; - private uint infoOffset = 32; - private uint infoLength = 160; - private uint dataOffset = 192; - private uint dataLength = 315392; + private byte[] magic = new byte[4] + { + (byte) 66, + (byte) 78, + (byte) 83, + (byte) 32 + }; + private uint flags = 4278124800; + private uint fileSize = 315584; + private ushort size = 32; + private ushort chunkCount = 2; + private uint infoOffset = 32; + private uint infoLength = 160; + private uint dataOffset = 192; + private uint dataLength = 315392; - public uint DataOffset - { - get => this.dataOffset; - set => this.dataOffset = value; - } + public uint DataOffset + { + get => this.dataOffset; + set => this.dataOffset = value; + } - public uint InfoLength - { - get => this.infoLength; - set => this.infoLength = value; - } + public uint InfoLength + { + get => this.infoLength; + set => this.infoLength = value; + } - public ushort Size - { - get => this.size; - set => this.size = value; - } + public ushort Size + { + get => this.size; + set => this.size = value; + } - public uint DataLength - { - get => this.dataLength; - set => this.dataLength = value; - } + public uint DataLength + { + get => this.dataLength; + set => this.dataLength = value; + } - public uint FileSize - { - get => this.fileSize; - set => this.fileSize = value; - } + public uint FileSize + { + get => this.fileSize; + set => this.fileSize = value; + } - public void Write(Stream outStream) - { - outStream.Write(this.magic, 0, this.magic.Length); - byte[] bytes1 = BitConverter.GetBytes(this.flags); - Array.Reverse((Array) bytes1); - outStream.Write(bytes1, 0, bytes1.Length); - byte[] bytes2 = BitConverter.GetBytes(this.fileSize); - Array.Reverse((Array) bytes2); - outStream.Write(bytes2, 0, bytes2.Length); - byte[] bytes3 = BitConverter.GetBytes(this.size); - Array.Reverse((Array) bytes3); - outStream.Write(bytes3, 0, bytes3.Length); - byte[] bytes4 = BitConverter.GetBytes(this.chunkCount); - Array.Reverse((Array) bytes4); - outStream.Write(bytes4, 0, bytes4.Length); - byte[] bytes5 = BitConverter.GetBytes(this.infoOffset); - Array.Reverse((Array) bytes5); - outStream.Write(bytes5, 0, bytes5.Length); - byte[] bytes6 = BitConverter.GetBytes(this.infoLength); - Array.Reverse((Array) bytes6); - outStream.Write(bytes6, 0, bytes6.Length); - byte[] bytes7 = BitConverter.GetBytes(this.dataOffset); - Array.Reverse((Array) bytes7); - outStream.Write(bytes7, 0, bytes7.Length); - byte[] bytes8 = BitConverter.GetBytes(this.dataLength); - Array.Reverse((Array) bytes8); - outStream.Write(bytes8, 0, bytes8.Length); - } + public void Write(Stream outStream) + { + outStream.Write(this.magic, 0, this.magic.Length); + byte[] bytes1 = BitConverter.GetBytes(this.flags); + Array.Reverse((Array) bytes1); + outStream.Write(bytes1, 0, bytes1.Length); + byte[] bytes2 = BitConverter.GetBytes(this.fileSize); + Array.Reverse((Array) bytes2); + outStream.Write(bytes2, 0, bytes2.Length); + byte[] bytes3 = BitConverter.GetBytes(this.size); + Array.Reverse((Array) bytes3); + outStream.Write(bytes3, 0, bytes3.Length); + byte[] bytes4 = BitConverter.GetBytes(this.chunkCount); + Array.Reverse((Array) bytes4); + outStream.Write(bytes4, 0, bytes4.Length); + byte[] bytes5 = BitConverter.GetBytes(this.infoOffset); + Array.Reverse((Array) bytes5); + outStream.Write(bytes5, 0, bytes5.Length); + byte[] bytes6 = BitConverter.GetBytes(this.infoLength); + Array.Reverse((Array) bytes6); + outStream.Write(bytes6, 0, bytes6.Length); + byte[] bytes7 = BitConverter.GetBytes(this.dataOffset); + Array.Reverse((Array) bytes7); + outStream.Write(bytes7, 0, bytes7.Length); + byte[] bytes8 = BitConverter.GetBytes(this.dataLength); + Array.Reverse((Array) bytes8); + outStream.Write(bytes8, 0, bytes8.Length); + } - public void Read(Stream input) - { - BinaryReader binaryReader = new BinaryReader(input); - if (!Shared.CompareByteArrays(this.magic, binaryReader.ReadBytes(4))) - { - binaryReader.BaseStream.Seek(28L, SeekOrigin.Current); - if (!Shared.CompareByteArrays(this.magic, binaryReader.ReadBytes(4))) - throw new Exception("This is not a valid BNS audfo file!"); - } - this.flags = Shared.Swap(binaryReader.ReadUInt32()); - this.fileSize = Shared.Swap(binaryReader.ReadUInt32()); - this.size = Shared.Swap(binaryReader.ReadUInt16()); - this.chunkCount = Shared.Swap(binaryReader.ReadUInt16()); - this.infoOffset = Shared.Swap(binaryReader.ReadUInt32()); - this.infoLength = Shared.Swap(binaryReader.ReadUInt32()); - this.dataOffset = Shared.Swap(binaryReader.ReadUInt32()); - this.dataLength = Shared.Swap(binaryReader.ReadUInt32()); + public void Read(Stream input) + { + BinaryReader binaryReader = new BinaryReader(input); + if (!Shared.CompareByteArrays(this.magic, binaryReader.ReadBytes(4))) + { + binaryReader.BaseStream.Seek(28L, SeekOrigin.Current); + if (!Shared.CompareByteArrays(this.magic, binaryReader.ReadBytes(4))) + throw new Exception("This is not a valid BNS audio file!"); + } + this.flags = Shared.Swap(binaryReader.ReadUInt32()); + this.fileSize = Shared.Swap(binaryReader.ReadUInt32()); + this.size = Shared.Swap(binaryReader.ReadUInt16()); + this.chunkCount = Shared.Swap(binaryReader.ReadUInt16()); + this.infoOffset = Shared.Swap(binaryReader.ReadUInt32()); + this.infoLength = Shared.Swap(binaryReader.ReadUInt32()); + this.dataOffset = Shared.Swap(binaryReader.ReadUInt32()); + this.dataLength = Shared.Swap(binaryReader.ReadUInt32()); + } } - } } diff --git a/BNS_Info.cs b/BNS_Info.cs index caa54b8..01cf279 100644 --- a/BNS_Info.cs +++ b/BNS_Info.cs @@ -1,351 +1,365 @@ -// Decompiled with JetBrains decompiler -// Type: libWiiSharp.BNS_Info -// Assembly: libWiiSharp, Version=0.4.0.0, Culture=neutral, PublicKeyToken=null -// MVID: FBF36F3D-B5D6-481F-B5F5-1BD3C19E13B2 -// Assembly location: C:\Users\theso\Downloads\NCPatcher\pack\libWiiSharp.dll +/* This file is part of libWiiSharp + * Copyright (C) 2009 Leathl + * Copyright (C) 2020 Github Contributors + * + * libWiiSharp is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * libWiiSharp is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ using System; using System.IO; namespace libWiiSharp { - internal class BNS_Info - { - private byte[] magic = new byte[4] + internal class BNS_Info { - (byte) 73, - (byte) 78, - (byte) 70, - (byte) 79 - }; - private uint size = 160; - private byte codec; - private byte hasLoop; - private byte channelCount = 2; - private byte zero; - private ushort sampleRate = 44100; - private ushort pad0; - private uint loopStart; - private uint loopEnd; - private uint offsetToChannelStart = 24; - private uint pad1; - private uint channel1StartOffset = 32; - private uint channel2StartOffset = 44; - private uint channel1Start; - private uint coefficients1Offset = 56; - private uint pad2; - private uint channel2Start; - private uint coefficients2Offset = 104; - private uint pad3; - private int[] coefficients1 = new int[16]; - private ushort channel1Gain; - private ushort channel1PredictiveScale; - private ushort channel1PreviousValue; - private ushort channel1NextPreviousValue; - private ushort channel1LoopPredictiveScale; - private ushort channel1LoopPreviousValue; - private ushort channel1LoopNextPreviousValue; - private ushort channel1LoopPadding; - private int[] coefficients2 = new int[16]; - private ushort channel2Gain; - private ushort channel2PredictiveScale; - private ushort channel2PreviousValue; - private ushort channel2NextPreviousValue; - private ushort channel2LoopPredictiveScale; - private ushort channel2LoopPreviousValue; - private ushort channel2LoopNextPreviousValue; - private ushort channel2LoopPadding; - - public byte HasLoop - { - get => this.hasLoop; - set => this.hasLoop = value; - } - - public uint Coefficients1Offset - { - get => this.coefficients1Offset; - set => this.coefficients1Offset = value; - } - - public uint Channel1StartOffset - { - get => this.channel1StartOffset; - set => this.channel1StartOffset = value; - } - - public uint Channel2StartOffset - { - get => this.channel2StartOffset; - set => this.channel2StartOffset = value; - } - - public uint Size - { - get => this.size; - set => this.size = value; - } - - public ushort SampleRate - { - get => this.sampleRate; - set => this.sampleRate = value; - } - - public byte ChannelCount - { - get => this.channelCount; - set => this.channelCount = value; - } - - public uint Channel1Start - { - get => this.channel1Start; - set => this.channel1Start = value; - } - - public uint Channel2Start - { - get => this.channel2Start; - set => this.channel2Start = value; - } - - public uint LoopStart - { - get => this.loopStart; - set => this.loopStart = value; - } - - public uint LoopEnd - { - get => this.loopEnd; - set => this.loopEnd = value; - } - - public int[] Coefficients1 - { - get => this.coefficients1; - set => this.coefficients1 = value; - } - - public int[] Coefficients2 - { - get => this.coefficients2; - set => this.coefficients2 = value; - } - - public void Write(Stream outStream) - { - outStream.Write(this.magic, 0, this.magic.Length); - byte[] bytes1 = BitConverter.GetBytes(this.size); - Array.Reverse((Array) bytes1); - outStream.Write(bytes1, 0, bytes1.Length); - outStream.WriteByte(this.codec); - outStream.WriteByte(this.hasLoop); - outStream.WriteByte(this.channelCount); - outStream.WriteByte(this.zero); - byte[] bytes2 = BitConverter.GetBytes(this.sampleRate); - Array.Reverse((Array) bytes2); - outStream.Write(bytes2, 0, bytes2.Length); - byte[] bytes3 = BitConverter.GetBytes(this.pad0); - Array.Reverse((Array) bytes3); - outStream.Write(bytes3, 0, bytes3.Length); - byte[] bytes4 = BitConverter.GetBytes(this.loopStart); - Array.Reverse((Array) bytes4); - outStream.Write(bytes4, 0, bytes4.Length); - byte[] bytes5 = BitConverter.GetBytes(this.loopEnd); - Array.Reverse((Array) bytes5); - outStream.Write(bytes5, 0, bytes5.Length); - byte[] bytes6 = BitConverter.GetBytes(this.offsetToChannelStart); - Array.Reverse((Array) bytes6); - outStream.Write(bytes6, 0, bytes6.Length); - byte[] bytes7 = BitConverter.GetBytes(this.pad1); - Array.Reverse((Array) bytes7); - outStream.Write(bytes7, 0, bytes7.Length); - byte[] bytes8 = BitConverter.GetBytes(this.channel1StartOffset); - Array.Reverse((Array) bytes8); - outStream.Write(bytes8, 0, bytes8.Length); - byte[] bytes9 = BitConverter.GetBytes(this.channel2StartOffset); - Array.Reverse((Array) bytes9); - outStream.Write(bytes9, 0, bytes9.Length); - byte[] bytes10 = BitConverter.GetBytes(this.channel1Start); - Array.Reverse((Array) bytes10); - outStream.Write(bytes10, 0, bytes10.Length); - byte[] bytes11 = BitConverter.GetBytes(this.coefficients1Offset); - Array.Reverse((Array) bytes11); - outStream.Write(bytes11, 0, bytes11.Length); - if (this.channelCount == (byte) 2) - { - byte[] bytes12 = BitConverter.GetBytes(this.pad2); - Array.Reverse((Array) bytes12); - outStream.Write(bytes12, 0, bytes12.Length); - byte[] bytes13 = BitConverter.GetBytes(this.channel2Start); - Array.Reverse((Array) bytes13); - outStream.Write(bytes13, 0, bytes13.Length); - byte[] bytes14 = BitConverter.GetBytes(this.coefficients2Offset); - Array.Reverse((Array) bytes14); - outStream.Write(bytes14, 0, bytes14.Length); - byte[] bytes15 = BitConverter.GetBytes(this.pad3); - Array.Reverse((Array) bytes15); - outStream.Write(bytes15, 0, bytes15.Length); - foreach (int num in this.coefficients1) + //Private Variables + private byte[] magic = new byte[4] { - byte[] bytes16 = BitConverter.GetBytes(num); - Array.Reverse((Array) bytes16); - outStream.Write(bytes16, 2, bytes16.Length - 2); - } - byte[] bytes17 = BitConverter.GetBytes(this.channel1Gain); - Array.Reverse((Array) bytes17); - outStream.Write(bytes17, 0, bytes17.Length); - byte[] bytes18 = BitConverter.GetBytes(this.channel1PredictiveScale); - Array.Reverse((Array) bytes18); - outStream.Write(bytes18, 0, bytes18.Length); - byte[] bytes19 = BitConverter.GetBytes(this.channel1PreviousValue); - Array.Reverse((Array) bytes19); - outStream.Write(bytes19, 0, bytes19.Length); - byte[] bytes20 = BitConverter.GetBytes(this.channel1NextPreviousValue); - Array.Reverse((Array) bytes20); - outStream.Write(bytes20, 0, bytes20.Length); - byte[] bytes21 = BitConverter.GetBytes(this.channel1LoopPredictiveScale); - Array.Reverse((Array) bytes21); - outStream.Write(bytes21, 0, bytes21.Length); - byte[] bytes22 = BitConverter.GetBytes(this.channel1LoopPreviousValue); - Array.Reverse((Array) bytes22); - outStream.Write(bytes22, 0, bytes22.Length); - byte[] bytes23 = BitConverter.GetBytes(this.channel1LoopNextPreviousValue); - Array.Reverse((Array) bytes23); - outStream.Write(bytes23, 0, bytes23.Length); - byte[] bytes24 = BitConverter.GetBytes(this.channel1LoopPadding); - Array.Reverse((Array) bytes24); - outStream.Write(bytes24, 0, bytes24.Length); - foreach (int num in this.coefficients2) - { - byte[] bytes16 = BitConverter.GetBytes(num); - Array.Reverse((Array) bytes16); - outStream.Write(bytes16, 2, bytes16.Length - 2); - } - byte[] bytes25 = BitConverter.GetBytes(this.channel2Gain); - Array.Reverse((Array) bytes25); - outStream.Write(bytes25, 0, bytes25.Length); - byte[] bytes26 = BitConverter.GetBytes(this.channel2PredictiveScale); - Array.Reverse((Array) bytes26); - outStream.Write(bytes26, 0, bytes26.Length); - byte[] bytes27 = BitConverter.GetBytes(this.channel2PreviousValue); - Array.Reverse((Array) bytes27); - outStream.Write(bytes27, 0, bytes27.Length); - byte[] bytes28 = BitConverter.GetBytes(this.channel2NextPreviousValue); - Array.Reverse((Array) bytes28); - outStream.Write(bytes28, 0, bytes28.Length); - byte[] bytes29 = BitConverter.GetBytes(this.channel2LoopPredictiveScale); - Array.Reverse((Array) bytes29); - outStream.Write(bytes29, 0, bytes29.Length); - byte[] bytes30 = BitConverter.GetBytes(this.channel2LoopPreviousValue); - Array.Reverse((Array) bytes30); - outStream.Write(bytes30, 0, bytes30.Length); - byte[] bytes31 = BitConverter.GetBytes(this.channel2LoopNextPreviousValue); - Array.Reverse((Array) bytes31); - outStream.Write(bytes31, 0, bytes31.Length); - byte[] bytes32 = BitConverter.GetBytes(this.channel2LoopPadding); - Array.Reverse((Array) bytes32); - outStream.Write(bytes32, 0, bytes32.Length); - } - else - { - if (this.channelCount != (byte) 1) - return; - foreach (int num in this.coefficients1) - { - byte[] bytes12 = BitConverter.GetBytes(num); - Array.Reverse((Array) bytes12); - outStream.Write(bytes12, 2, bytes12.Length - 2); - } - byte[] bytes13 = BitConverter.GetBytes(this.channel1Gain); - Array.Reverse((Array) bytes13); - outStream.Write(bytes13, 0, bytes13.Length); - byte[] bytes14 = BitConverter.GetBytes(this.channel1PredictiveScale); - Array.Reverse((Array) bytes14); - outStream.Write(bytes14, 0, bytes14.Length); - byte[] bytes15 = BitConverter.GetBytes(this.channel1PreviousValue); - Array.Reverse((Array) bytes15); - outStream.Write(bytes15, 0, bytes15.Length); - byte[] bytes16 = BitConverter.GetBytes(this.channel1NextPreviousValue); - Array.Reverse((Array) bytes16); - outStream.Write(bytes16, 0, bytes16.Length); - byte[] bytes17 = BitConverter.GetBytes(this.channel1LoopPredictiveScale); - Array.Reverse((Array) bytes17); - outStream.Write(bytes17, 0, bytes17.Length); - byte[] bytes18 = BitConverter.GetBytes(this.channel1LoopPreviousValue); - Array.Reverse((Array) bytes18); - outStream.Write(bytes18, 0, bytes18.Length); - byte[] bytes19 = BitConverter.GetBytes(this.channel1LoopNextPreviousValue); - Array.Reverse((Array) bytes19); - outStream.Write(bytes19, 0, bytes19.Length); - byte[] bytes20 = BitConverter.GetBytes(this.channel1LoopPadding); - Array.Reverse((Array) bytes20); - outStream.Write(bytes20, 0, bytes20.Length); - } - } + (byte) 73, + (byte) 78, + (byte) 70, + (byte) 79 + }; + private uint size = 160; + private byte codec; + private byte hasLoop; + private byte channelCount = 2; + private byte zero; + private ushort sampleRate = 44100; + private ushort pad0; + private uint loopStart; + private uint loopEnd; //Or total sample count + private uint offsetToChannelStart = 24; + private uint pad1; + private uint channel1StartOffset = 32; + private uint channel2StartOffset = 44; + private uint channel1Start; + private uint coefficients1Offset = 56; + private uint pad2; + private uint channel2Start; + private uint coefficients2Offset = 104; + private uint pad3; + private int[] coefficients1 = new int[16]; + private ushort channel1Gain; + private ushort channel1PredictiveScale; + private ushort channel1PreviousValue; + private ushort channel1NextPreviousValue; + private ushort channel1LoopPredictiveScale; + private ushort channel1LoopPreviousValue; + private ushort channel1LoopNextPreviousValue; + private ushort channel1LoopPadding; + private int[] coefficients2 = new int[16]; + private ushort channel2Gain; + private ushort channel2PredictiveScale; + private ushort channel2PreviousValue; + private ushort channel2NextPreviousValue; + private ushort channel2LoopPredictiveScale; + private ushort channel2LoopPreviousValue; + private ushort channel2LoopNextPreviousValue; + private ushort channel2LoopPadding; - public void Read(Stream input) - { - BinaryReader binaryReader = new BinaryReader(input); - this.size = Shared.CompareByteArrays(this.magic, binaryReader.ReadBytes(4)) ? Shared.Swap(binaryReader.ReadUInt32()) : throw new Exception("This is not a valid BNS audfo file!"); - this.codec = binaryReader.ReadByte(); - this.hasLoop = binaryReader.ReadByte(); - this.channelCount = binaryReader.ReadByte(); - this.zero = binaryReader.ReadByte(); - this.sampleRate = Shared.Swap(binaryReader.ReadUInt16()); - this.pad0 = Shared.Swap(binaryReader.ReadUInt16()); - this.loopStart = Shared.Swap(binaryReader.ReadUInt32()); - this.loopEnd = Shared.Swap(binaryReader.ReadUInt32()); - this.offsetToChannelStart = Shared.Swap(binaryReader.ReadUInt32()); - this.pad1 = Shared.Swap(binaryReader.ReadUInt32()); - this.channel1StartOffset = Shared.Swap(binaryReader.ReadUInt32()); - this.channel2StartOffset = Shared.Swap(binaryReader.ReadUInt32()); - this.channel1Start = Shared.Swap(binaryReader.ReadUInt32()); - this.coefficients1Offset = Shared.Swap(binaryReader.ReadUInt32()); - if (this.channelCount == (byte) 2) - { - this.pad2 = Shared.Swap(binaryReader.ReadUInt32()); - this.channel2Start = Shared.Swap(binaryReader.ReadUInt32()); - this.coefficients2Offset = Shared.Swap(binaryReader.ReadUInt32()); - this.pad3 = Shared.Swap(binaryReader.ReadUInt32()); - for (int index = 0; index < 16; ++index) - this.coefficients1[index] = (int) (short) Shared.Swap(binaryReader.ReadUInt16()); - this.channel1Gain = Shared.Swap(binaryReader.ReadUInt16()); - this.channel1PredictiveScale = Shared.Swap(binaryReader.ReadUInt16()); - this.channel1PreviousValue = Shared.Swap(binaryReader.ReadUInt16()); - this.channel1NextPreviousValue = Shared.Swap(binaryReader.ReadUInt16()); - this.channel1LoopPredictiveScale = Shared.Swap(binaryReader.ReadUInt16()); - this.channel1LoopPreviousValue = Shared.Swap(binaryReader.ReadUInt16()); - this.channel1LoopNextPreviousValue = Shared.Swap(binaryReader.ReadUInt16()); - this.channel1LoopPadding = Shared.Swap(binaryReader.ReadUInt16()); - for (int index = 0; index < 16; ++index) - this.coefficients2[index] = (int) (short) Shared.Swap(binaryReader.ReadUInt16()); - this.channel2Gain = Shared.Swap(binaryReader.ReadUInt16()); - this.channel2PredictiveScale = Shared.Swap(binaryReader.ReadUInt16()); - this.channel2PreviousValue = Shared.Swap(binaryReader.ReadUInt16()); - this.channel2NextPreviousValue = Shared.Swap(binaryReader.ReadUInt16()); - this.channel2LoopPredictiveScale = Shared.Swap(binaryReader.ReadUInt16()); - this.channel2LoopPreviousValue = Shared.Swap(binaryReader.ReadUInt16()); - this.channel2LoopNextPreviousValue = Shared.Swap(binaryReader.ReadUInt16()); - this.channel2LoopPadding = Shared.Swap(binaryReader.ReadUInt16()); - } - else - { - if (this.channelCount != (byte) 1) - return; - for (int index = 0; index < 16; ++index) - this.coefficients1[index] = (int) (short) Shared.Swap(binaryReader.ReadUInt16()); - this.channel1Gain = Shared.Swap(binaryReader.ReadUInt16()); - this.channel1PredictiveScale = Shared.Swap(binaryReader.ReadUInt16()); - this.channel1PreviousValue = Shared.Swap(binaryReader.ReadUInt16()); - this.channel1NextPreviousValue = Shared.Swap(binaryReader.ReadUInt16()); - this.channel1LoopPredictiveScale = Shared.Swap(binaryReader.ReadUInt16()); - this.channel1LoopPreviousValue = Shared.Swap(binaryReader.ReadUInt16()); - this.channel1LoopNextPreviousValue = Shared.Swap(binaryReader.ReadUInt16()); - this.channel1LoopPadding = Shared.Swap(binaryReader.ReadUInt16()); - } + //Public Variables + public byte HasLoop + { + get => this.hasLoop; + set => this.hasLoop = value; + } + + public uint Coefficients1Offset + { + get => this.coefficients1Offset; + set => this.coefficients1Offset = value; + } + + public uint Channel1StartOffset + { + get => this.channel1StartOffset; + set => this.channel1StartOffset = value; + } + + public uint Channel2StartOffset + { + get => this.channel2StartOffset; + set => this.channel2StartOffset = value; + } + + public uint Size + { + get => this.size; + set => this.size = value; + } + + public ushort SampleRate + { + get => this.sampleRate; + set => this.sampleRate = value; + } + + public byte ChannelCount + { + get => this.channelCount; + set => this.channelCount = value; + } + + public uint Channel1Start + { + get => this.channel1Start; + set => this.channel1Start = value; + } + + public uint Channel2Start + { + get => this.channel2Start; + set => this.channel2Start = value; + } + + public uint LoopStart + { + get => this.loopStart; + set => this.loopStart = value; + } + + public uint LoopEnd + { + get => this.loopEnd; + set => this.loopEnd = value; + } + + public int[] Coefficients1 + { + get => this.coefficients1; + set => this.coefficients1 = value; + } + + public int[] Coefficients2 + { + get => this.coefficients2; + set => this.coefficients2 = value; + } + + public void Write(Stream outStream) + { + outStream.Write(this.magic, 0, this.magic.Length); + byte[] bytes1 = BitConverter.GetBytes(this.size); + Array.Reverse((Array) bytes1); + outStream.Write(bytes1, 0, bytes1.Length); + outStream.WriteByte(this.codec); + outStream.WriteByte(this.hasLoop); + outStream.WriteByte(this.channelCount); + outStream.WriteByte(this.zero); + byte[] bytes2 = BitConverter.GetBytes(this.sampleRate); + Array.Reverse((Array) bytes2); + outStream.Write(bytes2, 0, bytes2.Length); + byte[] bytes3 = BitConverter.GetBytes(this.pad0); + Array.Reverse((Array) bytes3); + outStream.Write(bytes3, 0, bytes3.Length); + byte[] bytes4 = BitConverter.GetBytes(this.loopStart); + Array.Reverse((Array) bytes4); + outStream.Write(bytes4, 0, bytes4.Length); + byte[] bytes5 = BitConverter.GetBytes(this.loopEnd); + Array.Reverse((Array) bytes5); + outStream.Write(bytes5, 0, bytes5.Length); + byte[] bytes6 = BitConverter.GetBytes(this.offsetToChannelStart); + Array.Reverse((Array) bytes6); + outStream.Write(bytes6, 0, bytes6.Length); + byte[] bytes7 = BitConverter.GetBytes(this.pad1); + Array.Reverse((Array) bytes7); + outStream.Write(bytes7, 0, bytes7.Length); + byte[] bytes8 = BitConverter.GetBytes(this.channel1StartOffset); + Array.Reverse((Array) bytes8); + outStream.Write(bytes8, 0, bytes8.Length); + byte[] bytes9 = BitConverter.GetBytes(this.channel2StartOffset); + Array.Reverse((Array) bytes9); + outStream.Write(bytes9, 0, bytes9.Length); + byte[] bytes10 = BitConverter.GetBytes(this.channel1Start); + Array.Reverse((Array) bytes10); + outStream.Write(bytes10, 0, bytes10.Length); + byte[] bytes11 = BitConverter.GetBytes(this.coefficients1Offset); + Array.Reverse((Array) bytes11); + outStream.Write(bytes11, 0, bytes11.Length); + if (this.channelCount == (byte) 2) + { + byte[] bytes12 = BitConverter.GetBytes(this.pad2); + Array.Reverse((Array) bytes12); + outStream.Write(bytes12, 0, bytes12.Length); + byte[] bytes13 = BitConverter.GetBytes(this.channel2Start); + Array.Reverse((Array) bytes13); + outStream.Write(bytes13, 0, bytes13.Length); + byte[] bytes14 = BitConverter.GetBytes(this.coefficients2Offset); + Array.Reverse((Array) bytes14); + outStream.Write(bytes14, 0, bytes14.Length); + byte[] bytes15 = BitConverter.GetBytes(this.pad3); + Array.Reverse((Array) bytes15); + outStream.Write(bytes15, 0, bytes15.Length); + foreach (int num in this.coefficients1) + { + byte[] bytes16 = BitConverter.GetBytes(num); + Array.Reverse((Array) bytes16); + outStream.Write(bytes16, 2, bytes16.Length - 2); + } + byte[] bytes17 = BitConverter.GetBytes(this.channel1Gain); + Array.Reverse((Array) bytes17); + outStream.Write(bytes17, 0, bytes17.Length); + byte[] bytes18 = BitConverter.GetBytes(this.channel1PredictiveScale); + Array.Reverse((Array) bytes18); + outStream.Write(bytes18, 0, bytes18.Length); + byte[] bytes19 = BitConverter.GetBytes(this.channel1PreviousValue); + Array.Reverse((Array) bytes19); + outStream.Write(bytes19, 0, bytes19.Length); + byte[] bytes20 = BitConverter.GetBytes(this.channel1NextPreviousValue); + Array.Reverse((Array) bytes20); + outStream.Write(bytes20, 0, bytes20.Length); + byte[] bytes21 = BitConverter.GetBytes(this.channel1LoopPredictiveScale); + Array.Reverse((Array) bytes21); + outStream.Write(bytes21, 0, bytes21.Length); + byte[] bytes22 = BitConverter.GetBytes(this.channel1LoopPreviousValue); + Array.Reverse((Array) bytes22); + outStream.Write(bytes22, 0, bytes22.Length); + byte[] bytes23 = BitConverter.GetBytes(this.channel1LoopNextPreviousValue); + Array.Reverse((Array) bytes23); + outStream.Write(bytes23, 0, bytes23.Length); + byte[] bytes24 = BitConverter.GetBytes(this.channel1LoopPadding); + Array.Reverse((Array) bytes24); + outStream.Write(bytes24, 0, bytes24.Length); + foreach (int num in this.coefficients2) + { + byte[] bytes16 = BitConverter.GetBytes(num); + Array.Reverse((Array) bytes16); + outStream.Write(bytes16, 2, bytes16.Length - 2); + } + byte[] bytes25 = BitConverter.GetBytes(this.channel2Gain); + Array.Reverse((Array) bytes25); + outStream.Write(bytes25, 0, bytes25.Length); + byte[] bytes26 = BitConverter.GetBytes(this.channel2PredictiveScale); + Array.Reverse((Array) bytes26); + outStream.Write(bytes26, 0, bytes26.Length); + byte[] bytes27 = BitConverter.GetBytes(this.channel2PreviousValue); + Array.Reverse((Array) bytes27); + outStream.Write(bytes27, 0, bytes27.Length); + byte[] bytes28 = BitConverter.GetBytes(this.channel2NextPreviousValue); + Array.Reverse((Array) bytes28); + outStream.Write(bytes28, 0, bytes28.Length); + byte[] bytes29 = BitConverter.GetBytes(this.channel2LoopPredictiveScale); + Array.Reverse((Array) bytes29); + outStream.Write(bytes29, 0, bytes29.Length); + byte[] bytes30 = BitConverter.GetBytes(this.channel2LoopPreviousValue); + Array.Reverse((Array) bytes30); + outStream.Write(bytes30, 0, bytes30.Length); + byte[] bytes31 = BitConverter.GetBytes(this.channel2LoopNextPreviousValue); + Array.Reverse((Array) bytes31); + outStream.Write(bytes31, 0, bytes31.Length); + byte[] bytes32 = BitConverter.GetBytes(this.channel2LoopPadding); + Array.Reverse((Array) bytes32); + outStream.Write(bytes32, 0, bytes32.Length); + } + else + { + if (this.channelCount != (byte) 1) + return; + foreach (int num in this.coefficients1) + { + byte[] bytes12 = BitConverter.GetBytes(num); + Array.Reverse((Array) bytes12); + outStream.Write(bytes12, 2, bytes12.Length - 2); + } + byte[] bytes13 = BitConverter.GetBytes(this.channel1Gain); + Array.Reverse((Array) bytes13); + outStream.Write(bytes13, 0, bytes13.Length); + byte[] bytes14 = BitConverter.GetBytes(this.channel1PredictiveScale); + Array.Reverse((Array) bytes14); + outStream.Write(bytes14, 0, bytes14.Length); + byte[] bytes15 = BitConverter.GetBytes(this.channel1PreviousValue); + Array.Reverse((Array) bytes15); + outStream.Write(bytes15, 0, bytes15.Length); + byte[] bytes16 = BitConverter.GetBytes(this.channel1NextPreviousValue); + Array.Reverse((Array) bytes16); + outStream.Write(bytes16, 0, bytes16.Length); + byte[] bytes17 = BitConverter.GetBytes(this.channel1LoopPredictiveScale); + Array.Reverse((Array) bytes17); + outStream.Write(bytes17, 0, bytes17.Length); + byte[] bytes18 = BitConverter.GetBytes(this.channel1LoopPreviousValue); + Array.Reverse((Array) bytes18); + outStream.Write(bytes18, 0, bytes18.Length); + byte[] bytes19 = BitConverter.GetBytes(this.channel1LoopNextPreviousValue); + Array.Reverse((Array) bytes19); + outStream.Write(bytes19, 0, bytes19.Length); + byte[] bytes20 = BitConverter.GetBytes(this.channel1LoopPadding); + Array.Reverse((Array) bytes20); + outStream.Write(bytes20, 0, bytes20.Length); + } + } + + public void Read(Stream input) + { + BinaryReader binaryReader = new BinaryReader(input); + this.size = Shared.CompareByteArrays(this.magic, binaryReader.ReadBytes(4)) ? Shared.Swap(binaryReader.ReadUInt32()) : throw new Exception("This is not a valid BNS audfo file!"); + this.codec = binaryReader.ReadByte(); + this.hasLoop = binaryReader.ReadByte(); + this.channelCount = binaryReader.ReadByte(); + this.zero = binaryReader.ReadByte(); + this.sampleRate = Shared.Swap(binaryReader.ReadUInt16()); + this.pad0 = Shared.Swap(binaryReader.ReadUInt16()); + this.loopStart = Shared.Swap(binaryReader.ReadUInt32()); + this.loopEnd = Shared.Swap(binaryReader.ReadUInt32()); + this.offsetToChannelStart = Shared.Swap(binaryReader.ReadUInt32()); + this.pad1 = Shared.Swap(binaryReader.ReadUInt32()); + this.channel1StartOffset = Shared.Swap(binaryReader.ReadUInt32()); + this.channel2StartOffset = Shared.Swap(binaryReader.ReadUInt32()); + this.channel1Start = Shared.Swap(binaryReader.ReadUInt32()); + this.coefficients1Offset = Shared.Swap(binaryReader.ReadUInt32()); + if (this.channelCount == (byte) 2) + { + this.pad2 = Shared.Swap(binaryReader.ReadUInt32()); + this.channel2Start = Shared.Swap(binaryReader.ReadUInt32()); + this.coefficients2Offset = Shared.Swap(binaryReader.ReadUInt32()); + this.pad3 = Shared.Swap(binaryReader.ReadUInt32()); + for (int index = 0; index < 16; ++index) + this.coefficients1[index] = (int) (short) Shared.Swap(binaryReader.ReadUInt16()); + this.channel1Gain = Shared.Swap(binaryReader.ReadUInt16()); + this.channel1PredictiveScale = Shared.Swap(binaryReader.ReadUInt16()); + this.channel1PreviousValue = Shared.Swap(binaryReader.ReadUInt16()); + this.channel1NextPreviousValue = Shared.Swap(binaryReader.ReadUInt16()); + this.channel1LoopPredictiveScale = Shared.Swap(binaryReader.ReadUInt16()); + this.channel1LoopPreviousValue = Shared.Swap(binaryReader.ReadUInt16()); + this.channel1LoopNextPreviousValue = Shared.Swap(binaryReader.ReadUInt16()); + this.channel1LoopPadding = Shared.Swap(binaryReader.ReadUInt16()); + for (int index = 0; index < 16; ++index) + this.coefficients2[index] = (int) (short) Shared.Swap(binaryReader.ReadUInt16()); + this.channel2Gain = Shared.Swap(binaryReader.ReadUInt16()); + this.channel2PredictiveScale = Shared.Swap(binaryReader.ReadUInt16()); + this.channel2PreviousValue = Shared.Swap(binaryReader.ReadUInt16()); + this.channel2NextPreviousValue = Shared.Swap(binaryReader.ReadUInt16()); + this.channel2LoopPredictiveScale = Shared.Swap(binaryReader.ReadUInt16()); + this.channel2LoopPreviousValue = Shared.Swap(binaryReader.ReadUInt16()); + this.channel2LoopNextPreviousValue = Shared.Swap(binaryReader.ReadUInt16()); + this.channel2LoopPadding = Shared.Swap(binaryReader.ReadUInt16()); + } + else + { + if (this.channelCount != (byte) 1) + return; + for (int index = 0; index < 16; ++index) + this.coefficients1[index] = (int) (short) Shared.Swap(binaryReader.ReadUInt16()); + this.channel1Gain = Shared.Swap(binaryReader.ReadUInt16()); + this.channel1PredictiveScale = Shared.Swap(binaryReader.ReadUInt16()); + this.channel1PreviousValue = Shared.Swap(binaryReader.ReadUInt16()); + this.channel1NextPreviousValue = Shared.Swap(binaryReader.ReadUInt16()); + this.channel1LoopPredictiveScale = Shared.Swap(binaryReader.ReadUInt16()); + this.channel1LoopPreviousValue = Shared.Swap(binaryReader.ReadUInt16()); + this.channel1LoopNextPreviousValue = Shared.Swap(binaryReader.ReadUInt16()); + this.channel1LoopPadding = Shared.Swap(binaryReader.ReadUInt16()); + } + } } - } } diff --git a/Brlan.cs b/Brlan.cs index 7b0a37d..275e280 100644 --- a/Brlan.cs +++ b/Brlan.cs @@ -1,8 +1,20 @@ -// Decompiled with JetBrains decompiler -// Type: libWiiSharp.Brlan -// Assembly: libWiiSharp, Version=0.4.0.0, Culture=neutral, PublicKeyToken=null -// MVID: FBF36F3D-B5D6-481F-B5F5-1BD3C19E13B2 -// Assembly location: C:\Users\theso\Downloads\NCPatcher\pack\libWiiSharp.dll +/* This file is part of libWiiSharp + * Copyright (C) 2009 Leathl + * Copyright (C) 2020 Github Contributors + * + * libWiiSharp is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * libWiiSharp is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ using System; using System.Collections.Generic; @@ -10,57 +22,57 @@ using System.IO; namespace libWiiSharp { - public class Brlan - { - public static string[] GetBrlanTpls(string pathTobrlan) => Brlan.getBrlanTpls(File.ReadAllBytes(pathTobrlan)); - - public static string[] GetBrlanTpls(byte[] brlanFile) => Brlan.getBrlanTpls(brlanFile); - - public static string[] GetBrlanTpls(WAD wad, bool banner) + public class Brlan { - if (!wad.HasBanner) - return new string[0]; - string str = nameof (banner); - if (!banner) - str = "icon"; - for (int index1 = 0; index1 < wad.BannerApp.Nodes.Count; ++index1) - { - if (wad.BannerApp.StringTable[index1].ToLower() == str + ".bin") + public static string[] GetBrlanTpls(string pathTobrlan) => Brlan.getBrlanTpls(File.ReadAllBytes(pathTobrlan)); + + public static string[] GetBrlanTpls(byte[] brlanFile) => Brlan.getBrlanTpls(brlanFile); + + public static string[] GetBrlanTpls(WAD wad, bool banner) { - U8 u8 = U8.Load(wad.BannerApp.Data[index1]); - string[] a = new string[0]; - for (int index2 = 0; index2 < u8.Nodes.Count; ++index2) - { - if (u8.StringTable[index2].ToLower() == str + "_start.brlan" || u8.StringTable[index2].ToLower() == str + "_loop.brlan" || u8.StringTable[index2].ToLower() == str + ".brlan") - a = Shared.MergeStringArrays(a, Brlan.getBrlanTpls(u8.Data[index2])); - } - return a; + if (!wad.HasBanner) + return new string[0]; + string str = nameof (banner); + if (!banner) + str = "icon"; + for (int index1 = 0; index1 < wad.BannerApp.Nodes.Count; ++index1) + { + if (wad.BannerApp.StringTable[index1].ToLower() == str + ".bin") + { + U8 u8 = U8.Load(wad.BannerApp.Data[index1]); + string[] a = new string[0]; + for (int index2 = 0; index2 < u8.Nodes.Count; ++index2) + { + if (u8.StringTable[index2].ToLower() == str + "_start.brlan" || u8.StringTable[index2].ToLower() == str + "_loop.brlan" || u8.StringTable[index2].ToLower() == str + ".brlan") + a = Shared.MergeStringArrays(a, Brlan.getBrlanTpls(u8.Data[index2])); + } + return a; + } + } + return new string[0]; } - } - return new string[0]; - } - private static string[] getBrlanTpls(byte[] brlanFile) - { - List stringList = new List(); - int numOfTpls = Brlan.getNumOfTpls(brlanFile); - int index1 = 36 + numOfTpls * 4; - for (int index2 = 0; index2 < numOfTpls; ++index2) - { - string empty = string.Empty; - while (brlanFile[index1] != (byte) 0) - empty += Convert.ToChar(brlanFile[index1++]).ToString(); - stringList.Add(empty); - ++index1; - } - for (int index2 = stringList.Count - 1; index2 >= 0; --index2) - { - if (!stringList[index2].ToLower().EndsWith(".tpl")) - stringList.RemoveAt(index2); - } - return stringList.ToArray(); - } + private static string[] getBrlanTpls(byte[] brlanFile) + { + List stringList = new List(); + int numOfTpls = Brlan.getNumOfTpls(brlanFile); + int index1 = 36 + numOfTpls * 4; + for (int index2 = 0; index2 < numOfTpls; ++index2) + { + string empty = string.Empty; + while (brlanFile[index1] != (byte) 0) + empty += Convert.ToChar(brlanFile[index1++]).ToString(); + stringList.Add(empty); + ++index1; + } + for (int index2 = stringList.Count - 1; index2 >= 0; --index2) + { + if (!stringList[index2].ToLower().EndsWith(".tpl")) + stringList.RemoveAt(index2); + } + return stringList.ToArray(); + } - private static int getNumOfTpls(byte[] brlanFile) => (int) Shared.Swap(BitConverter.ToUInt16(brlanFile, 28)); - } + private static int getNumOfTpls(byte[] brlanFile) => (int) Shared.Swap(BitConverter.ToUInt16(brlanFile, 28)); + } } diff --git a/Brlyt.cs b/Brlyt.cs index 5bd937f..631e6b6 100644 --- a/Brlyt.cs +++ b/Brlyt.cs @@ -1,8 +1,20 @@ -// Decompiled with JetBrains decompiler -// Type: libWiiSharp.Brlyt -// Assembly: libWiiSharp, Version=0.4.0.0, Culture=neutral, PublicKeyToken=null -// MVID: FBF36F3D-B5D6-481F-B5F5-1BD3C19E13B2 -// Assembly location: C:\Users\theso\Downloads\NCPatcher\pack\libWiiSharp.dll +/* This file is part of libWiiSharp + * Copyright (C) 2009 Leathl + * Copyright (C) 2020 Github Contributors + * + * libWiiSharp is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * libWiiSharp is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ using System; using System.Collections.Generic; @@ -10,57 +22,57 @@ using System.IO; namespace libWiiSharp { - public class Brlyt - { - public static string[] GetBrlytTpls(string pathToBrlyt) => Brlyt.getBrlytTpls(File.ReadAllBytes(pathToBrlyt)); - - public static string[] GetBrlytTpls(byte[] brlytFile) => Brlyt.getBrlytTpls(brlytFile); - - public static string[] GetBrlytTpls(WAD wad, bool banner) + public class Brlyt { - if (!wad.HasBanner) - return new string[0]; - string str = nameof (banner); - if (!banner) - str = "icon"; - for (int index1 = 0; index1 < wad.BannerApp.Nodes.Count; ++index1) - { - if (wad.BannerApp.StringTable[index1].ToLower() == str + ".bin") + public static string[] GetBrlytTpls(string pathToBrlyt) => Brlyt.getBrlytTpls(File.ReadAllBytes(pathToBrlyt)); + + public static string[] GetBrlytTpls(byte[] brlytFile) => Brlyt.getBrlytTpls(brlytFile); + + public static string[] GetBrlytTpls(WAD wad, bool banner) { - U8 u8 = U8.Load(wad.BannerApp.Data[index1]); - string[] a = new string[0]; - for (int index2 = 0; index2 < u8.Nodes.Count; ++index2) - { - if (u8.StringTable[index2].ToLower() == str + ".brlyt") - a = Shared.MergeStringArrays(a, Brlyt.getBrlytTpls(u8.Data[index2])); - } - return a; + if (!wad.HasBanner) + return new string[0]; + string str = nameof (banner); + if (!banner) + str = "icon"; + for (int index1 = 0; index1 < wad.BannerApp.Nodes.Count; ++index1) + { + if (wad.BannerApp.StringTable[index1].ToLower() == str + ".bin") + { + U8 u8 = U8.Load(wad.BannerApp.Data[index1]); + string[] a = new string[0]; + for (int index2 = 0; index2 < u8.Nodes.Count; ++index2) + { + if (u8.StringTable[index2].ToLower() == str + ".brlyt") + a = Shared.MergeStringArrays(a, Brlyt.getBrlytTpls(u8.Data[index2])); + } + return a; + } + } + return new string[0]; } - } - return new string[0]; - } - private static string[] getBrlytTpls(byte[] brlytFile) - { - List stringList = new List(); - int numOfTpls = Brlyt.getNumOfTpls(brlytFile); - int index1 = 48 + numOfTpls * 8; - for (int index2 = 0; index2 < numOfTpls; ++index2) - { - string empty = string.Empty; - while (brlytFile[index1] != (byte) 0) - empty += Convert.ToChar(brlytFile[index1++]).ToString(); - stringList.Add(empty); - ++index1; - } - for (int index2 = stringList.Count - 1; index2 >= 0; --index2) - { - if (!stringList[index2].ToLower().EndsWith(".tpl")) - stringList.RemoveAt(index2); - } - return stringList.ToArray(); - } + private static string[] getBrlytTpls(byte[] brlytFile) + { + List stringList = new List(); + int numOfTpls = Brlyt.getNumOfTpls(brlytFile); + int index1 = 48 + numOfTpls * 8; + for (int index2 = 0; index2 < numOfTpls; ++index2) + { + string empty = string.Empty; + while (brlytFile[index1] != (byte) 0) + empty += Convert.ToChar(brlytFile[index1++]).ToString(); + stringList.Add(empty); + ++index1; + } + for (int index2 = stringList.Count - 1; index2 >= 0; --index2) + { + if (!stringList[index2].ToLower().EndsWith(".tpl")) + stringList.RemoveAt(index2); + } + return stringList.ToArray(); + } - private static int getNumOfTpls(byte[] brlytFile) => (int) Shared.Swap(BitConverter.ToUInt16(brlytFile, 44)); - } + private static int getNumOfTpls(byte[] brlytFile) => (int) Shared.Swap(BitConverter.ToUInt16(brlytFile, 44)); + } } diff --git a/CertificateChain.cs b/CertificateChain.cs index 2ba228a..7705afa 100644 --- a/CertificateChain.cs +++ b/CertificateChain.cs @@ -1,8 +1,20 @@ -// Decompiled with JetBrains decompiler -// Type: libWiiSharp.CertificateChain -// Assembly: libWiiSharp, Version=0.4.0.0, Culture=neutral, PublicKeyToken=null -// MVID: FBF36F3D-B5D6-481F-B5F5-1BD3C19E13B2 -// Assembly location: C:\Users\theso\Downloads\NCPatcher\pack\libWiiSharp.dll +/* This file is part of libWiiSharp + * Copyright (C) 2009 Leathl + * Copyright (C) 2020 Github Contributors + * + * libWiiSharp is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * libWiiSharp is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ using System; using System.IO; @@ -10,429 +22,429 @@ using System.Security.Cryptography; namespace libWiiSharp { - public class CertificateChain : IDisposable - { - private const string certCaHash = "5B7D3EE28706AD8DA2CBD5A6B75C15D0F9B6F318"; - private const string certCpHash = "6824D6DA4C25184F0D6DAF6EDB9C0FC57522A41C"; - private const string certXsHash = "09787045037121477824BC6A3E5E076156573F8A"; - private SHA1 sha = SHA1.Create(); - private bool[] certsComplete = new bool[3]; - private byte[] certCa = new byte[1024]; - private byte[] certCp = new byte[768]; - private byte[] certXs = new byte[768]; - private bool isDisposed; - - public bool CertsComplete => this.certsComplete[0] && this.certsComplete[1] && this.certsComplete[2]; - - public event EventHandler Debug; - - ~CertificateChain() => this.Dispose(false); - - public void Dispose() + public class CertificateChain : IDisposable { - this.Dispose(true); - GC.SuppressFinalize((object) this); - } + private const string certCaHash = "5B7D3EE28706AD8DA2CBD5A6B75C15D0F9B6F318"; + private const string certCpHash = "6824D6DA4C25184F0D6DAF6EDB9C0FC57522A41C"; + private const string certXsHash = "09787045037121477824BC6A3E5E076156573F8A"; + private SHA1 sha = SHA1.Create(); + private bool[] certsComplete = new bool[3]; + private byte[] certCa = new byte[1024]; + private byte[] certCp = new byte[768]; + private byte[] certXs = new byte[768]; + private bool isDisposed; - protected virtual void Dispose(bool disposing) - { - if (disposing && !this.isDisposed) - { - this.sha.Clear(); - this.sha = (SHA1) null; - this.certsComplete = (bool[]) null; - this.certCa = (byte[]) null; - this.certCp = (byte[]) null; - this.certXs = (byte[]) null; - } - this.isDisposed = true; - } + public bool CertsComplete => this.certsComplete[0] && this.certsComplete[1] && this.certsComplete[2]; - public static CertificateChain Load(string pathToCert) => CertificateChain.Load(File.ReadAllBytes(pathToCert)); + public event EventHandler Debug; - public static CertificateChain Load(byte[] certFile) - { - CertificateChain certificateChain = new CertificateChain(); - MemoryStream memoryStream = new MemoryStream(certFile); - try - { - certificateChain.parseCert((Stream) memoryStream); - } - catch - { - memoryStream.Dispose(); - throw; - } - memoryStream.Dispose(); - return certificateChain; - } + ~CertificateChain() => this.Dispose(false); - public static CertificateChain Load(Stream cert) - { - CertificateChain certificateChain = new CertificateChain(); - certificateChain.parseCert(cert); - return certificateChain; - } - - public static CertificateChain FromTikTmd(string pathToTik, string pathToTmd) => CertificateChain.FromTikTmd(File.ReadAllBytes(pathToTik), File.ReadAllBytes(pathToTmd)); - - public static CertificateChain FromTikTmd(byte[] tikFile, byte[] tmdFile) - { - CertificateChain certificateChain = new CertificateChain(); - MemoryStream memoryStream1 = new MemoryStream(tikFile); - try - { - certificateChain.grabFromTik((Stream) memoryStream1); - } - catch - { - memoryStream1.Dispose(); - throw; - } - MemoryStream memoryStream2 = new MemoryStream(tmdFile); - try - { - certificateChain.grabFromTmd((Stream) memoryStream2); - } - catch - { - memoryStream2.Dispose(); - throw; - } - memoryStream2.Dispose(); - return certificateChain.CertsComplete ? certificateChain : throw new Exception("Couldn't locate all certs!"); - } - - public static CertificateChain FromTikTmd(Stream tik, Stream tmd) - { - CertificateChain certificateChain = new CertificateChain(); - certificateChain.grabFromTik(tik); - certificateChain.grabFromTmd(tmd); - return certificateChain; - } - - public void LoadFile(string pathToCert) => this.LoadFile(File.ReadAllBytes(pathToCert)); - - public void LoadFile(byte[] certFile) - { - MemoryStream memoryStream = new MemoryStream(certFile); - try - { - this.parseCert((Stream) memoryStream); - } - catch - { - memoryStream.Dispose(); - throw; - } - memoryStream.Dispose(); - } - - public void LoadFile(Stream cert) => this.parseCert(cert); - - public void LoadFromTikTmd(string pathToTik, string pathToTmd) => this.LoadFromTikTmd(File.ReadAllBytes(pathToTik), File.ReadAllBytes(pathToTmd)); - - public void LoadFromTikTmd(byte[] tikFile, byte[] tmdFile) - { - MemoryStream memoryStream1 = new MemoryStream(tikFile); - try - { - this.grabFromTik((Stream) memoryStream1); - } - catch - { - memoryStream1.Dispose(); - throw; - } - MemoryStream memoryStream2 = new MemoryStream(tmdFile); - try - { - this.grabFromTmd((Stream) memoryStream2); - } - catch - { - memoryStream2.Dispose(); - throw; - } - memoryStream2.Dispose(); - if (!this.CertsComplete) - throw new Exception("Couldn't locate all certs!"); - } - - public void LoadFromTikTmd(Stream tik, Stream tmd) - { - this.grabFromTik(tik); - this.grabFromTmd(tmd); - } - - public void Save(string savePath) - { - if (File.Exists(savePath)) - File.Delete(savePath); - using (FileStream fileStream = new FileStream(savePath, FileMode.Create)) - this.writeToStream((Stream) fileStream); - } - - public MemoryStream ToMemoryStream() - { - MemoryStream memoryStream = new MemoryStream(); - try - { - this.writeToStream((Stream) memoryStream); - return memoryStream; - } - catch - { - memoryStream.Dispose(); - throw; - } - } - - public byte[] ToByteArray() - { - MemoryStream memoryStream = new MemoryStream(); - try - { - this.writeToStream((Stream) memoryStream); - } - catch - { - memoryStream.Dispose(); - throw; - } - byte[] array = memoryStream.ToArray(); - memoryStream.Dispose(); - return array; - } - - private void writeToStream(Stream writeStream) - { - this.fireDebug("Writing Certificate Chain..."); - if (!this.CertsComplete) - { - this.fireDebug(" Certificate Chain incomplete..."); - throw new Exception("At least one certificate is missing!"); - } - writeStream.Seek(0L, SeekOrigin.Begin); - object[] objArray1 = new object[1]; - long position = writeStream.Position; - objArray1[0] = (object) position.ToString("x8"); - this.fireDebug(" Writing Certificate CA... (Offset: 0x{0})", objArray1); - writeStream.Write(this.certCa, 0, this.certCa.Length); - object[] objArray2 = new object[1]; - position = writeStream.Position; - objArray2[0] = (object) position.ToString("x8"); - this.fireDebug(" Writing Certificate CP... (Offset: 0x{0})", objArray2); - writeStream.Write(this.certCp, 0, this.certCp.Length); - object[] objArray3 = new object[1]; - position = writeStream.Position; - objArray3[0] = (object) position.ToString("x8"); - this.fireDebug(" Writing Certificate XS... (Offset: 0x{0})", objArray3); - writeStream.Write(this.certXs, 0, this.certXs.Length); - this.fireDebug("Writing Certificate Chain Finished..."); - } - - private void parseCert(Stream certFile) - { - this.fireDebug("Parsing Certificate Chain..."); - int num = 0; - for (int index = 0; index < 3; ++index) - { - this.fireDebug(" Scanning at Offset 0x{0}:", (object) num.ToString("x8")); - try + public void Dispose() { - certFile.Seek((long) num, SeekOrigin.Begin); - byte[] array = new byte[1024]; - certFile.Read(array, 0, array.Length); - this.fireDebug(" Checking for Certificate CA..."); - if (this.isCertCa(array) && !this.certsComplete[1]) - { - this.fireDebug(" Certificate CA detected..."); - this.certCa = array; - this.certsComplete[1] = true; - num += 1024; - continue; - } - this.fireDebug(" Checking for Certificate CP..."); - if (this.isCertCp(array) && !this.certsComplete[2]) - { - this.fireDebug(" Certificate CP detected..."); - Array.Resize(ref array, 768); - this.certCp = array; - this.certsComplete[2] = true; - num += 768; - continue; - } - this.fireDebug(" Checking for Certificate XS..."); - if (this.isCertXs(array)) - { - if (!this.certsComplete[0]) + this.Dispose(true); + GC.SuppressFinalize((object) this); + } + + protected virtual void Dispose(bool disposing) + { + if (disposing && !this.isDisposed) { - this.fireDebug(" Certificate XS detected..."); - Array.Resize(ref array, 768); - this.certXs = array; - this.certsComplete[0] = true; - num += 768; - continue; + this.sha.Clear(); + this.sha = (SHA1) null; + this.certsComplete = (bool[]) null; + this.certCa = (byte[]) null; + this.certCp = (byte[]) null; + this.certXs = (byte[]) null; } - } + this.isDisposed = true; } - catch (Exception ex) - { - this.fireDebug("Error: {0}", (object) ex.Message); - } - num += 768; - } - if (!this.CertsComplete) - { - this.fireDebug(" Couldn't locate all Certificates..."); - throw new Exception("Couldn't locate all certs!"); - } - this.fireDebug("Parsing Certificate Chain Finished..."); - } - private void grabFromTik(Stream tik) - { - this.fireDebug("Scanning Ticket for Certificates..."); - int num = 676; - for (int index = 0; index < 3; ++index) - { - this.fireDebug(" Scanning at Offset 0x{0}:", (object) num.ToString("x8")); - try + public static CertificateChain Load(string pathToCert) => CertificateChain.Load(File.ReadAllBytes(pathToCert)); + + public static CertificateChain Load(byte[] certFile) { - tik.Seek((long) num, SeekOrigin.Begin); - byte[] array = new byte[1024]; - tik.Read(array, 0, array.Length); - this.fireDebug(" Checking for Certificate CA..."); - if (this.isCertCa(array) && !this.certsComplete[1]) - { - this.fireDebug(" Certificate CA detected..."); - this.certCa = array; - this.certsComplete[1] = true; - num += 1024; - continue; - } - this.fireDebug(" Checking for Certificate CP..."); - if (this.isCertCp(array) && !this.certsComplete[2]) - { - this.fireDebug(" Certificate CP detected..."); - Array.Resize(ref array, 768); - this.certCp = array; - this.certsComplete[2] = true; - num += 768; - continue; - } - this.fireDebug(" Checking for Certificate XS..."); - if (this.isCertXs(array)) - { - if (!this.certsComplete[0]) + CertificateChain certificateChain = new CertificateChain(); + MemoryStream memoryStream = new MemoryStream(certFile); + try { - this.fireDebug(" Certificate XS detected..."); - Array.Resize(ref array, 768); - this.certXs = array; - this.certsComplete[0] = true; - num += 768; - continue; + certificateChain.parseCert((Stream) memoryStream); } - } - } - catch - { - } - num += 768; - } - this.fireDebug("Scanning Ticket for Certificates Finished..."); - } - - private void grabFromTmd(Stream tmd) - { - this.fireDebug("Scanning TMD for Certificates..."); - byte[] buffer = new byte[2]; - tmd.Seek(478L, SeekOrigin.Begin); - tmd.Read(buffer, 0, 2); - int num = 484 + (int) Shared.Swap(BitConverter.ToUInt16(buffer, 0)) * 36; - for (int index = 0; index < 3; ++index) - { - this.fireDebug(" Scanning at Offset 0x{0}:", (object) num.ToString("x8")); - try - { - tmd.Seek((long) num, SeekOrigin.Begin); - byte[] array = new byte[1024]; - tmd.Read(array, 0, array.Length); - this.fireDebug(" Checking for Certificate CA..."); - if (this.isCertCa(array) && !this.certsComplete[1]) - { - this.fireDebug(" Certificate CA detected..."); - this.certCa = array; - this.certsComplete[1] = true; - num += 1024; - continue; - } - this.fireDebug(" Checking for Certificate CP..."); - if (this.isCertCp(array) && !this.certsComplete[2]) - { - this.fireDebug(" Certificate CP detected..."); - Array.Resize(ref array, 768); - this.certCp = array; - this.certsComplete[2] = true; - num += 768; - continue; - } - this.fireDebug(" Checking for Certificate XS..."); - if (this.isCertXs(array)) - { - if (!this.certsComplete[0]) + catch { - this.fireDebug(" Certificate XS detected..."); - Array.Resize(ref array, 768); - this.certXs = array; - this.certsComplete[0] = true; - num += 768; - continue; + memoryStream.Dispose(); + throw; } - } + memoryStream.Dispose(); + return certificateChain; } - catch + + public static CertificateChain Load(Stream cert) { + CertificateChain certificateChain = new CertificateChain(); + certificateChain.parseCert(cert); + return certificateChain; } - num += 768; - } - this.fireDebug("Scanning TMD for Certificates Finished..."); - } - private bool isCertXs(byte[] part) - { - if (part.Length < 768) - return false; - if (part.Length > 768) - Array.Resize(ref part, 768); - return part[388] == (byte) 88 && part[389] == (byte) 83 && Shared.CompareByteArrays(this.sha.ComputeHash(part), Shared.HexStringToByteArray("09787045037121477824BC6A3E5E076156573F8A")); - } + public static CertificateChain FromTikTmd(string pathToTik, string pathToTmd) => CertificateChain.FromTikTmd(File.ReadAllBytes(pathToTik), File.ReadAllBytes(pathToTmd)); - private bool isCertCa(byte[] part) - { - if (part.Length < 1024) - return false; - if (part.Length > 1024) - Array.Resize(ref part, 1024); - return part[644] == (byte) 67 && part[645] == (byte) 65 && Shared.CompareByteArrays(this.sha.ComputeHash(part), Shared.HexStringToByteArray("5B7D3EE28706AD8DA2CBD5A6B75C15D0F9B6F318")); - } + public static CertificateChain FromTikTmd(byte[] tikFile, byte[] tmdFile) + { + CertificateChain certificateChain = new CertificateChain(); + MemoryStream memoryStream1 = new MemoryStream(tikFile); + try + { + certificateChain.grabFromTik((Stream) memoryStream1); + } + catch + { + memoryStream1.Dispose(); + throw; + } + MemoryStream memoryStream2 = new MemoryStream(tmdFile); + try + { + certificateChain.grabFromTmd((Stream) memoryStream2); + } + catch + { + memoryStream2.Dispose(); + throw; + } + memoryStream2.Dispose(); + return certificateChain.CertsComplete ? certificateChain : throw new Exception("Couldn't locate all certs!"); + } - private bool isCertCp(byte[] part) - { - if (part.Length < 768) - return false; - if (part.Length > 768) - Array.Resize(ref part, 768); - return part[388] == (byte) 67 && part[389] == (byte) 80 && Shared.CompareByteArrays(this.sha.ComputeHash(part), Shared.HexStringToByteArray("6824D6DA4C25184F0D6DAF6EDB9C0FC57522A41C")); - } + public static CertificateChain FromTikTmd(Stream tik, Stream tmd) + { + CertificateChain certificateChain = new CertificateChain(); + certificateChain.grabFromTik(tik); + certificateChain.grabFromTmd(tmd); + return certificateChain; + } - private void fireDebug(string debugMessage, params object[] args) - { - EventHandler debug = this.Debug; - if (debug == null) - return; - debug(new object(), new MessageEventArgs(string.Format(debugMessage, args))); + public void LoadFile(string pathToCert) => this.LoadFile(File.ReadAllBytes(pathToCert)); + + public void LoadFile(byte[] certFile) + { + MemoryStream memoryStream = new MemoryStream(certFile); + try + { + this.parseCert((Stream) memoryStream); + } + catch + { + memoryStream.Dispose(); + throw; + } + memoryStream.Dispose(); + } + + public void LoadFile(Stream cert) => this.parseCert(cert); + + public void LoadFromTikTmd(string pathToTik, string pathToTmd) => this.LoadFromTikTmd(File.ReadAllBytes(pathToTik), File.ReadAllBytes(pathToTmd)); + + public void LoadFromTikTmd(byte[] tikFile, byte[] tmdFile) + { + MemoryStream memoryStream1 = new MemoryStream(tikFile); + try + { + this.grabFromTik((Stream) memoryStream1); + } + catch + { + memoryStream1.Dispose(); + throw; + } + MemoryStream memoryStream2 = new MemoryStream(tmdFile); + try + { + this.grabFromTmd((Stream) memoryStream2); + } + catch + { + memoryStream2.Dispose(); + throw; + } + memoryStream2.Dispose(); + if (!this.CertsComplete) + throw new Exception("Couldn't locate all certs!"); + } + + public void LoadFromTikTmd(Stream tik, Stream tmd) + { + this.grabFromTik(tik); + this.grabFromTmd(tmd); + } + + public void Save(string savePath) + { + if (File.Exists(savePath)) + File.Delete(savePath); + using (FileStream fileStream = new FileStream(savePath, FileMode.Create)) + this.writeToStream((Stream) fileStream); + } + + public MemoryStream ToMemoryStream() + { + MemoryStream memoryStream = new MemoryStream(); + try + { + this.writeToStream((Stream) memoryStream); + return memoryStream; + } + catch + { + memoryStream.Dispose(); + throw; + } + } + + public byte[] ToByteArray() + { + MemoryStream memoryStream = new MemoryStream(); + try + { + this.writeToStream((Stream) memoryStream); + } + catch + { + memoryStream.Dispose(); + throw; + } + byte[] array = memoryStream.ToArray(); + memoryStream.Dispose(); + return array; + } + + private void writeToStream(Stream writeStream) + { + this.fireDebug("Writing Certificate Chain..."); + if (!this.CertsComplete) + { + this.fireDebug(" Certificate Chain incomplete..."); + throw new Exception("At least one certificate is missing!"); + } + writeStream.Seek(0L, SeekOrigin.Begin); + object[] objArray1 = new object[1]; + long position = writeStream.Position; + objArray1[0] = (object) position.ToString("x8"); + this.fireDebug(" Writing Certificate CA... (Offset: 0x{0})", objArray1); + writeStream.Write(this.certCa, 0, this.certCa.Length); + object[] objArray2 = new object[1]; + position = writeStream.Position; + objArray2[0] = (object) position.ToString("x8"); + this.fireDebug(" Writing Certificate CP... (Offset: 0x{0})", objArray2); + writeStream.Write(this.certCp, 0, this.certCp.Length); + object[] objArray3 = new object[1]; + position = writeStream.Position; + objArray3[0] = (object) position.ToString("x8"); + this.fireDebug(" Writing Certificate XS... (Offset: 0x{0})", objArray3); + writeStream.Write(this.certXs, 0, this.certXs.Length); + this.fireDebug("Writing Certificate Chain Finished..."); + } + + private void parseCert(Stream certFile) + { + this.fireDebug("Parsing Certificate Chain..."); + int num = 0; + for (int index = 0; index < 3; ++index) + { + this.fireDebug(" Scanning at Offset 0x{0}:", (object) num.ToString("x8")); + try + { + certFile.Seek((long) num, SeekOrigin.Begin); + byte[] array = new byte[1024]; + certFile.Read(array, 0, array.Length); + this.fireDebug(" Checking for Certificate CA..."); + if (this.isCertCa(array) && !this.certsComplete[1]) + { + this.fireDebug(" Certificate CA detected..."); + this.certCa = array; + this.certsComplete[1] = true; + num += 1024; + continue; + } + this.fireDebug(" Checking for Certificate CP..."); + if (this.isCertCp(array) && !this.certsComplete[2]) + { + this.fireDebug(" Certificate CP detected..."); + Array.Resize(ref array, 768); + this.certCp = array; + this.certsComplete[2] = true; + num += 768; + continue; + } + this.fireDebug(" Checking for Certificate XS..."); + if (this.isCertXs(array)) + { + if (!this.certsComplete[0]) + { + this.fireDebug(" Certificate XS detected..."); + Array.Resize(ref array, 768); + this.certXs = array; + this.certsComplete[0] = true; + num += 768; + continue; + } + } + } + catch (Exception ex) + { + this.fireDebug("Error: {0}", (object) ex.Message); + } + num += 768; + } + if (!this.CertsComplete) + { + this.fireDebug(" Couldn't locate all Certificates..."); + throw new Exception("Couldn't locate all certs!"); + } + this.fireDebug("Parsing Certificate Chain Finished..."); + } + + private void grabFromTik(Stream tik) + { + this.fireDebug("Scanning Ticket for Certificates..."); + int num = 676; + for (int index = 0; index < 3; ++index) + { + this.fireDebug(" Scanning at Offset 0x{0}:", (object) num.ToString("x8")); + try + { + tik.Seek((long) num, SeekOrigin.Begin); + byte[] array = new byte[1024]; + tik.Read(array, 0, array.Length); + this.fireDebug(" Checking for Certificate CA..."); + if (this.isCertCa(array) && !this.certsComplete[1]) + { + this.fireDebug(" Certificate CA detected..."); + this.certCa = array; + this.certsComplete[1] = true; + num += 1024; + continue; + } + this.fireDebug(" Checking for Certificate CP..."); + if (this.isCertCp(array) && !this.certsComplete[2]) + { + this.fireDebug(" Certificate CP detected..."); + Array.Resize(ref array, 768); + this.certCp = array; + this.certsComplete[2] = true; + num += 768; + continue; + } + this.fireDebug(" Checking for Certificate XS..."); + if (this.isCertXs(array)) + { + if (!this.certsComplete[0]) + { + this.fireDebug(" Certificate XS detected..."); + Array.Resize(ref array, 768); + this.certXs = array; + this.certsComplete[0] = true; + num += 768; + continue; + } + } + } + catch + { + } + num += 768; + } + this.fireDebug("Scanning Ticket for Certificates Finished..."); + } + + private void grabFromTmd(Stream tmd) + { + this.fireDebug("Scanning TMD for Certificates..."); + byte[] buffer = new byte[2]; + tmd.Seek(478L, SeekOrigin.Begin); + tmd.Read(buffer, 0, 2); + int num = 484 + (int) Shared.Swap(BitConverter.ToUInt16(buffer, 0)) * 36; + for (int index = 0; index < 3; ++index) + { + this.fireDebug(" Scanning at Offset 0x{0}:", (object) num.ToString("x8")); + try + { + tmd.Seek((long) num, SeekOrigin.Begin); + byte[] array = new byte[1024]; + tmd.Read(array, 0, array.Length); + this.fireDebug(" Checking for Certificate CA..."); + if (this.isCertCa(array) && !this.certsComplete[1]) + { + this.fireDebug(" Certificate CA detected..."); + this.certCa = array; + this.certsComplete[1] = true; + num += 1024; + continue; + } + this.fireDebug(" Checking for Certificate CP..."); + if (this.isCertCp(array) && !this.certsComplete[2]) + { + this.fireDebug(" Certificate CP detected..."); + Array.Resize(ref array, 768); + this.certCp = array; + this.certsComplete[2] = true; + num += 768; + continue; + } + this.fireDebug(" Checking for Certificate XS..."); + if (this.isCertXs(array)) + { + if (!this.certsComplete[0]) + { + this.fireDebug(" Certificate XS detected..."); + Array.Resize(ref array, 768); + this.certXs = array; + this.certsComplete[0] = true; + num += 768; + continue; + } + } + } + catch + { + } + num += 768; + } + this.fireDebug("Scanning TMD for Certificates Finished..."); + } + + private bool isCertXs(byte[] part) + { + if (part.Length < 768) + return false; + if (part.Length > 768) + Array.Resize(ref part, 768); + return part[388] == (byte) 88 && part[389] == (byte) 83 && Shared.CompareByteArrays(this.sha.ComputeHash(part), Shared.HexStringToByteArray("09787045037121477824BC6A3E5E076156573F8A")); + } + + private bool isCertCa(byte[] part) + { + if (part.Length < 1024) + return false; + if (part.Length > 1024) + Array.Resize(ref part, 1024); + return part[644] == (byte) 67 && part[645] == (byte) 65 && Shared.CompareByteArrays(this.sha.ComputeHash(part), Shared.HexStringToByteArray("5B7D3EE28706AD8DA2CBD5A6B75C15D0F9B6F318")); + } + + private bool isCertCp(byte[] part) + { + if (part.Length < 768) + return false; + if (part.Length > 768) + Array.Resize(ref part, 768); + return part[388] == (byte) 67 && part[389] == (byte) 80 && Shared.CompareByteArrays(this.sha.ComputeHash(part), Shared.HexStringToByteArray("6824D6DA4C25184F0D6DAF6EDB9C0FC57522A41C")); + } + + private void fireDebug(string debugMessage, params object[] args) + { + EventHandler debug = this.Debug; + if (debug == null) + return; + debug(new object(), new MessageEventArgs(string.Format(debugMessage, args))); + } } - } } diff --git a/ColorIndexConverter.cs b/ColorIndexConverter.cs index 414ddec..fefb44d 100644 --- a/ColorIndexConverter.cs +++ b/ColorIndexConverter.cs @@ -1,279 +1,291 @@ -// Decompiled with JetBrains decompiler -// Type: libWiiSharp.ColorIndexConverter -// Assembly: libWiiSharp, Version=0.4.0.0, Culture=neutral, PublicKeyToken=null -// MVID: FBF36F3D-B5D6-481F-B5F5-1BD3C19E13B2 -// Assembly location: C:\Users\theso\Downloads\NCPatcher\pack\libWiiSharp.dll +/* This file is part of libWiiSharp + * Copyright (C) 2009 Leathl + * Copyright (C) 2020 Github Contributors + * + * libWiiSharp is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * libWiiSharp is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ using System; using System.Collections.Generic; namespace libWiiSharp { - internal class ColorIndexConverter - { - private uint[] rgbaPalette; - private byte[] tplPalette; - private uint[] rgbaData; - private byte[] tplData; - private TPL_TextureFormat tplFormat; - private TPL_PaletteFormat paletteFormat; - private int width; - private int height; - - public byte[] Palette => this.tplPalette; - - public byte[] Data => this.tplData; - - public ColorIndexConverter( - uint[] rgbaData, - int width, - int height, - TPL_TextureFormat tplFormat, - TPL_PaletteFormat paletteFormat) + internal class ColorIndexConverter { - if (tplFormat != TPL_TextureFormat.CI4 && tplFormat != TPL_TextureFormat.CI8) - throw new Exception("Texture format must be either CI4 or CI8"); - if (paletteFormat != TPL_PaletteFormat.IA8 && paletteFormat != TPL_PaletteFormat.RGB565 && paletteFormat != TPL_PaletteFormat.RGB5A3) - throw new Exception("Palette format must be either IA8, RGB565 or RGB5A3!"); - this.rgbaData = rgbaData; - this.width = width; - this.height = height; - this.tplFormat = tplFormat; - this.paletteFormat = paletteFormat; - this.buildPalette(); - if (tplFormat != TPL_TextureFormat.CI4) - { - if (tplFormat == TPL_TextureFormat.CI8) - this.toCI8(); - else - this.toCI14X2(); - } - else - this.toCI4(); - } + private uint[] rgbaPalette; + private byte[] tplPalette; + private uint[] rgbaData; + private byte[] tplData; + private TPL_TextureFormat tplFormat; + private TPL_PaletteFormat paletteFormat; + private int width; + private int height; - private void toCI4() - { - byte[] numArray = new byte[Shared.AddPadding(this.width, 8) * Shared.AddPadding(this.height, 8) / 2]; - int num = 0; - for (int index1 = 0; index1 < this.height; index1 += 8) - { - for (int index2 = 0; index2 < this.width; index2 += 8) + public byte[] Palette => this.tplPalette; + + public byte[] Data => this.tplData; + + public ColorIndexConverter( + uint[] rgbaData, + int width, + int height, + TPL_TextureFormat tplFormat, + TPL_PaletteFormat paletteFormat) { - for (int index3 = index1; index3 < index1 + 8; ++index3) - { - for (int index4 = index2; index4 < index2 + 8; index4 += 2) + if (tplFormat != TPL_TextureFormat.CI4 && tplFormat != TPL_TextureFormat.CI8) + throw new Exception("Texture format must be either CI4 or CI8"); + if (paletteFormat != TPL_PaletteFormat.IA8 && paletteFormat != TPL_PaletteFormat.RGB565 && paletteFormat != TPL_PaletteFormat.RGB5A3) + throw new Exception("Palette format must be either IA8, RGB565 or RGB5A3!"); + this.rgbaData = rgbaData; + this.width = width; + this.height = height; + this.tplFormat = tplFormat; + this.paletteFormat = paletteFormat; + this.buildPalette(); + if (tplFormat != TPL_TextureFormat.CI4) { - uint colorIndex1 = this.getColorIndex(index3 >= this.height || index4 >= this.width ? 0U : this.rgbaData[index3 * this.width + index4]); - uint colorIndex2 = this.getColorIndex(index3 >= this.height || index4 >= this.width ? 0U : (index3 * this.width + index4 + 1 < this.rgbaData.Length ? this.rgbaData[index3 * this.width + index4 + 1] : 0U)); - numArray[num++] = (byte) ((uint) (byte) colorIndex1 << 4 | (uint) (byte) colorIndex2); + if (tplFormat == TPL_TextureFormat.CI8) + this.toCI8(); + else + this.toCI14X2(); } - } + else + this.toCI4(); } - } - this.tplData = numArray; - } - private void toCI8() - { - byte[] numArray = new byte[Shared.AddPadding(this.width, 8) * Shared.AddPadding(this.height, 4)]; - int num1 = 0; - for (int index1 = 0; index1 < this.height; index1 += 4) - { - for (int index2 = 0; index2 < this.width; index2 += 8) + private void toCI4() { - for (int index3 = index1; index3 < index1 + 4; ++index3) - { - for (int index4 = index2; index4 < index2 + 8; ++index4) + byte[] numArray = new byte[Shared.AddPadding(this.width, 8) * Shared.AddPadding(this.height, 8) / 2]; + int num = 0; + for (int index1 = 0; index1 < this.height; index1 += 8) { - uint num2 = index3 >= this.height || index4 >= this.width ? 0U : this.rgbaData[index3 * this.width + index4]; - numArray[num1++] = (byte) this.getColorIndex(num2); + for (int index2 = 0; index2 < this.width; index2 += 8) + { + for (int index3 = index1; index3 < index1 + 8; ++index3) + { + for (int index4 = index2; index4 < index2 + 8; index4 += 2) + { + uint colorIndex1 = this.getColorIndex(index3 >= this.height || index4 >= this.width ? 0U : this.rgbaData[index3 * this.width + index4]); + uint colorIndex2 = this.getColorIndex(index3 >= this.height || index4 >= this.width ? 0U : (index3 * this.width + index4 + 1 < this.rgbaData.Length ? this.rgbaData[index3 * this.width + index4 + 1] : 0U)); + numArray[num++] = (byte) ((uint) (byte) colorIndex1 << 4 | (uint) (byte) colorIndex2); + } + } + } } - } + this.tplData = numArray; } - } - this.tplData = numArray; - } - private void toCI14X2() - { - byte[] numArray1 = new byte[Shared.AddPadding(this.width, 4) * Shared.AddPadding(this.height, 4) * 2]; - int num1 = 0; - for (int index1 = 0; index1 < this.height; index1 += 4) - { - for (int index2 = 0; index2 < this.width; index2 += 4) + private void toCI8() { - for (int index3 = index1; index3 < index1 + 4; ++index3) - { - for (int index4 = index2; index4 < index2 + 4; ++index4) + byte[] numArray = new byte[Shared.AddPadding(this.width, 8) * Shared.AddPadding(this.height, 4)]; + int num1 = 0; + for (int index1 = 0; index1 < this.height; index1 += 4) { - byte[] bytes = BitConverter.GetBytes((ushort) this.getColorIndex(index3 >= this.height || index4 >= this.width ? 0U : this.rgbaData[index3 * this.width + index4])); - byte[] numArray2 = numArray1; - int index5 = num1; - int num2 = index5 + 1; - int num3 = (int) bytes[1]; - numArray2[index5] = (byte) num3; - byte[] numArray3 = numArray1; - int index6 = num2; - num1 = index6 + 1; - int num4 = (int) bytes[0]; - numArray3[index6] = (byte) num4; + for (int index2 = 0; index2 < this.width; index2 += 8) + { + for (int index3 = index1; index3 < index1 + 4; ++index3) + { + for (int index4 = index2; index4 < index2 + 8; ++index4) + { + uint num2 = index3 >= this.height || index4 >= this.width ? 0U : this.rgbaData[index3 * this.width + index4]; + numArray[num1++] = (byte) this.getColorIndex(num2); + } + } + } } - } + this.tplData = numArray; } - } - this.tplData = numArray1; - } - private void buildPalette() - { - int num1 = 256; - if (this.tplFormat == TPL_TextureFormat.CI4) - num1 = 16; - else if (this.tplFormat == TPL_TextureFormat.CI14X2) - num1 = 16384; - List uintList = new List(); - List ushortList = new List(); - uintList.Add(0U); - ushortList.Add((ushort) 0); - for (int index = 1; index < this.rgbaData.Length && uintList.Count != num1; ++index) - { - if ((long) (this.rgbaData[index] >> 24 & (uint) byte.MaxValue) >= (this.tplFormat == TPL_TextureFormat.CI14X2 ? 1L : 25L)) + private void toCI14X2() { - ushort num2 = Shared.Swap(this.convertToPaletteValue((int) this.rgbaData[index])); - if (!uintList.Contains(this.rgbaData[index]) && !ushortList.Contains(num2)) - { - uintList.Add(this.rgbaData[index]); - ushortList.Add(num2); - } + byte[] numArray1 = new byte[Shared.AddPadding(this.width, 4) * Shared.AddPadding(this.height, 4) * 2]; + int num1 = 0; + for (int index1 = 0; index1 < this.height; index1 += 4) + { + for (int index2 = 0; index2 < this.width; index2 += 4) + { + for (int index3 = index1; index3 < index1 + 4; ++index3) + { + for (int index4 = index2; index4 < index2 + 4; ++index4) + { + byte[] bytes = BitConverter.GetBytes((ushort) this.getColorIndex(index3 >= this.height || index4 >= this.width ? 0U : this.rgbaData[index3 * this.width + index4])); + byte[] numArray2 = numArray1; + int index5 = num1; + int num2 = index5 + 1; + int num3 = (int) bytes[1]; + numArray2[index5] = (byte) num3; + byte[] numArray3 = numArray1; + int index6 = num2; + num1 = index6 + 1; + int num4 = (int) bytes[0]; + numArray3[index6] = (byte) num4; + } + } + } + } + this.tplData = numArray1; } - } - while (uintList.Count % 16 != 0) - { - uintList.Add(uint.MaxValue); - ushortList.Add(ushort.MaxValue); - } - this.tplPalette = Shared.UShortArrayToByteArray(ushortList.ToArray()); - this.rgbaPalette = uintList.ToArray(); - } - private ushort convertToPaletteValue(int rgba) - { - int num1 = 0; - int num2; - if (this.paletteFormat == TPL_PaletteFormat.IA8) - { - int num3 = ((rgba & (int) byte.MaxValue) + (rgba >> 8 & (int) byte.MaxValue) + (rgba >> 16 & (int) byte.MaxValue)) / 3 & (int) byte.MaxValue; - num2 = (int) (ushort) ((rgba >> 24 & (int) byte.MaxValue) << 8 | num3); - } - else if (this.paletteFormat == TPL_PaletteFormat.RGB565) - { - num2 = (int) (ushort) ((rgba >> 16 & (int) byte.MaxValue) >> 3 << 11 | (rgba >> 8 & (int) byte.MaxValue) >> 2 << 5 | (rgba & (int) byte.MaxValue) >> 3); - } - else - { - int num3 = rgba >> 16 & (int) byte.MaxValue; - int num4 = rgba >> 8 & (int) byte.MaxValue; - int num5 = rgba & (int) byte.MaxValue; - int num6 = rgba >> 24 & (int) byte.MaxValue; - if (num6 <= 218) + private void buildPalette() { - int num7 = num1 & -32769; - int num8 = num3 * 15 / (int) byte.MaxValue & 15; - int num9 = num4 * 15 / (int) byte.MaxValue & 15; - int num10 = num5 * 15 / (int) byte.MaxValue & 15; - int num11 = num6 * 7 / (int) byte.MaxValue & 7; - num2 = num7 | num11 << 12 | num10 | num9 << 4 | num8 << 8; + int num1 = 256; + if (this.tplFormat == TPL_TextureFormat.CI4) + num1 = 16; + else if (this.tplFormat == TPL_TextureFormat.CI14X2) + num1 = 16384; + List uintList = new List(); + List ushortList = new List(); + uintList.Add(0U); + ushortList.Add((ushort) 0); + for (int index = 1; index < this.rgbaData.Length && uintList.Count != num1; ++index) + { + if ((long) (this.rgbaData[index] >> 24 & (uint) byte.MaxValue) >= (this.tplFormat == TPL_TextureFormat.CI14X2 ? 1L : 25L)) + { + ushort num2 = Shared.Swap(this.convertToPaletteValue((int) this.rgbaData[index])); + if (!uintList.Contains(this.rgbaData[index]) && !ushortList.Contains(num2)) + { + uintList.Add(this.rgbaData[index]); + ushortList.Add(num2); + } + } + } + while (uintList.Count % 16 != 0) + { + uintList.Add(uint.MaxValue); + ushortList.Add(ushort.MaxValue); + } + this.tplPalette = Shared.UShortArrayToByteArray(ushortList.ToArray()); + this.rgbaPalette = uintList.ToArray(); } - else - { - int num7 = num1 | 32768; - int num8 = num3 * 31 / (int) byte.MaxValue & 31; - int num9 = num4 * 31 / (int) byte.MaxValue & 31; - int num10 = num5 * 31 / (int) byte.MaxValue & 31; - num2 = num7 | num10 | num9 << 5 | num8 << 10; - } - } - return (ushort) num2; - } - private uint getColorIndex(uint value) - { - uint num1 = (uint) int.MaxValue; - uint num2 = 0; - if ((long) (value >> 24 & (uint) byte.MaxValue) < (this.tplFormat == TPL_TextureFormat.CI14X2 ? 1L : 25L)) - return 0; - ushort paletteValue1 = this.convertToPaletteValue((int) value); - for (int index = 0; index < this.rgbaPalette.Length; ++index) - { - ushort paletteValue2 = this.convertToPaletteValue((int) this.rgbaPalette[index]); - if ((int) paletteValue1 == (int) paletteValue2) - return (uint) index; - uint distance = this.getDistance(paletteValue1, paletteValue2); - if (distance < num1) + private ushort convertToPaletteValue(int rgba) { - num1 = distance; - num2 = (uint) index; + int num1 = 0; + int num2; + if (this.paletteFormat == TPL_PaletteFormat.IA8) + { + int num3 = ((rgba & (int) byte.MaxValue) + (rgba >> 8 & (int) byte.MaxValue) + (rgba >> 16 & (int) byte.MaxValue)) / 3 & (int) byte.MaxValue; + num2 = (int) (ushort) ((rgba >> 24 & (int) byte.MaxValue) << 8 | num3); + } + else if (this.paletteFormat == TPL_PaletteFormat.RGB565) + { + num2 = (int) (ushort) ((rgba >> 16 & (int) byte.MaxValue) >> 3 << 11 | (rgba >> 8 & (int) byte.MaxValue) >> 2 << 5 | (rgba & (int) byte.MaxValue) >> 3); + } + else + { + int num3 = rgba >> 16 & (int) byte.MaxValue; + int num4 = rgba >> 8 & (int) byte.MaxValue; + int num5 = rgba & (int) byte.MaxValue; + int num6 = rgba >> 24 & (int) byte.MaxValue; + if (num6 <= 218) + { + int num7 = num1 & -32769; + int num8 = num3 * 15 / (int) byte.MaxValue & 15; + int num9 = num4 * 15 / (int) byte.MaxValue & 15; + int num10 = num5 * 15 / (int) byte.MaxValue & 15; + int num11 = num6 * 7 / (int) byte.MaxValue & 7; + num2 = num7 | num11 << 12 | num10 | num9 << 4 | num8 << 8; + } + else + { + int num7 = num1 | 32768; + int num8 = num3 * 31 / (int) byte.MaxValue & 31; + int num9 = num4 * 31 / (int) byte.MaxValue & 31; + int num10 = num5 * 31 / (int) byte.MaxValue & 31; + num2 = num7 | num10 | num9 << 5 | num8 << 10; + } + } + return (ushort) num2; } - } + + private uint getColorIndex(uint value) + { + uint num1 = (uint) int.MaxValue; + uint num2 = 0; + if ((long) (value >> 24 & (uint) byte.MaxValue) < (this.tplFormat == TPL_TextureFormat.CI14X2 ? 1L : 25L)) + return 0; + ushort paletteValue1 = this.convertToPaletteValue((int) value); + for (int index = 0; index < this.rgbaPalette.Length; ++index) + { + ushort paletteValue2 = this.convertToPaletteValue((int) this.rgbaPalette[index]); + if ((int) paletteValue1 == (int) paletteValue2) + return (uint) index; + uint distance = this.getDistance(paletteValue1, paletteValue2); + if (distance < num1) + { + num1 = distance; + num2 = (uint) index; + } + } return num2; - } + } - private uint getDistance(ushort color, ushort paletteColor) - { - int rgbaValue1 = (int) this.convertToRgbaValue(color); - uint rgbaValue2 = this.convertToRgbaValue(paletteColor); - uint val1_1 = (uint) rgbaValue1 >> 24 & (uint) byte.MaxValue; - uint val1_2 = (uint) rgbaValue1 >> 16 & (uint) byte.MaxValue; - uint val1_3 = (uint) rgbaValue1 >> 8 & (uint) byte.MaxValue; - uint val1_4 = (uint) (rgbaValue1 & (int) byte.MaxValue); - uint val2_1 = rgbaValue2 >> 24 & (uint) byte.MaxValue; - uint val2_2 = rgbaValue2 >> 16 & (uint) byte.MaxValue; - uint val2_3 = rgbaValue2 >> 8 & (uint) byte.MaxValue; - uint val2_4 = rgbaValue2 & (uint) byte.MaxValue; - int num1 = (int) Math.Max(val1_1, val2_1) - (int) Math.Min(val1_1, val2_1); - uint num2 = Math.Max(val1_2, val2_2) - Math.Min(val1_2, val2_2); - uint num3 = Math.Max(val1_3, val2_3) - Math.Min(val1_3, val2_3); - uint num4 = Math.Max(val1_4, val2_4) - Math.Min(val1_4, val2_4); - int num5 = (int) num2; - return (uint) (num1 + num5) + num3 + num4; - } + private uint getDistance(ushort color, ushort paletteColor) + { + int rgbaValue1 = (int) this.convertToRgbaValue(color); + uint rgbaValue2 = this.convertToRgbaValue(paletteColor); + uint val1_1 = (uint) rgbaValue1 >> 24 & (uint) byte.MaxValue; + uint val1_2 = (uint) rgbaValue1 >> 16 & (uint) byte.MaxValue; + uint val1_3 = (uint) rgbaValue1 >> 8 & (uint) byte.MaxValue; + uint val1_4 = (uint) (rgbaValue1 & (int) byte.MaxValue); + uint val2_1 = rgbaValue2 >> 24 & (uint) byte.MaxValue; + uint val2_2 = rgbaValue2 >> 16 & (uint) byte.MaxValue; + uint val2_3 = rgbaValue2 >> 8 & (uint) byte.MaxValue; + uint val2_4 = rgbaValue2 & (uint) byte.MaxValue; + int num1 = (int) Math.Max(val1_1, val2_1) - (int) Math.Min(val1_1, val2_1); + uint num2 = Math.Max(val1_2, val2_2) - Math.Min(val1_2, val2_2); + uint num3 = Math.Max(val1_3, val2_3) - Math.Min(val1_3, val2_3); + uint num4 = Math.Max(val1_4, val2_4) - Math.Min(val1_4, val2_4); + int num5 = (int) num2; + return (uint) (num1 + num5) + num3 + num4; + } - private uint convertToRgbaValue(ushort pixel) - { - if (this.paletteFormat == TPL_PaletteFormat.IA8) - { - int num1 = (int) pixel >> 8; - int num2 = (int) pixel & (int) byte.MaxValue; - return (uint) (num1 | num1 << 8 | num1 << 16 | num2 << 24); - } - if (this.paletteFormat == TPL_PaletteFormat.RGB565) - { - int num1 = ((int) pixel >> 11 & 31) << 3 & (int) byte.MaxValue; - int num2 = ((int) pixel >> 5 & 63) << 2 & (int) byte.MaxValue; - int num3 = ((int) pixel & 31) << 3 & (int) byte.MaxValue; - int maxValue = (int) byte.MaxValue; - return (uint) (num3 | num2 << 8 | num1 << 16 | maxValue << 24); - } - int num4; - int num5; - int num6; - int num7; - if (((int) pixel & 32768) != 0) - { - num4 = ((int) pixel >> 10 & 31) * (int) byte.MaxValue / 31; - num5 = ((int) pixel >> 5 & 31) * (int) byte.MaxValue / 31; - num6 = ((int) pixel & 31) * (int) byte.MaxValue / 31; - num7 = (int) byte.MaxValue; - } - else - { - num7 = ((int) pixel >> 12 & 7) * (int) byte.MaxValue / 7; - num4 = ((int) pixel >> 8 & 15) * (int) byte.MaxValue / 15; - num5 = ((int) pixel >> 4 & 15) * (int) byte.MaxValue / 15; - num6 = ((int) pixel & 15) * (int) byte.MaxValue / 15; - } - return (uint) (num6 | num5 << 8 | num4 << 16 | num7 << 24); + private uint convertToRgbaValue(ushort pixel) + { + if (this.paletteFormat == TPL_PaletteFormat.IA8) + { + int num1 = (int) pixel >> 8; + int num2 = (int) pixel & (int) byte.MaxValue; + return (uint) (num1 | num1 << 8 | num1 << 16 | num2 << 24); + } + if (this.paletteFormat == TPL_PaletteFormat.RGB565) + { + int num1 = ((int) pixel >> 11 & 31) << 3 & (int) byte.MaxValue; + int num2 = ((int) pixel >> 5 & 63) << 2 & (int) byte.MaxValue; + int num3 = ((int) pixel & 31) << 3 & (int) byte.MaxValue; + int maxValue = (int) byte.MaxValue; + return (uint) (num3 | num2 << 8 | num1 << 16 | maxValue << 24); + } + int num4; + int num5; + int num6; + int num7; + if (((int) pixel & 32768) != 0) + { + num4 = ((int) pixel >> 10 & 31) * (int) byte.MaxValue / 31; + num5 = ((int) pixel >> 5 & 31) * (int) byte.MaxValue / 31; + num6 = ((int) pixel & 31) * (int) byte.MaxValue / 31; + num7 = (int) byte.MaxValue; + } + else + { + num7 = ((int) pixel >> 12 & 7) * (int) byte.MaxValue / 7; + num4 = ((int) pixel >> 8 & 15) * (int) byte.MaxValue / 15; + num5 = ((int) pixel >> 4 & 15) * (int) byte.MaxValue / 15; + num6 = ((int) pixel & 15) * (int) byte.MaxValue / 15; + } + return (uint) (num6 | num5 << 8 | num4 << 16 | num7 << 24); + } } - } } diff --git a/libWiiSharp.csproj b/libWiiSharp.csproj index 57c0a2e..bd7f885 100644 --- a/libWiiSharp.csproj +++ b/libWiiSharp.csproj @@ -24,6 +24,7 @@ prompt 4 false + true AnyCPU @@ -34,6 +35,7 @@ prompt 4 false + false false @@ -93,5 +95,8 @@ + + + \ No newline at end of file