/// <summary> /// Transforms a object into a binary DBIN2 file. /// </summary> /// <param name="FileName">The output file name</param> /// <param name="Data">The object to be binarized</param> private static void Binarize(string FileName, DBIN2 Data) { using (FileStream Output = new FileStream(FileName, FileMode.Create)) { EncryptedBinary Writer = new EncryptedBinary(Output); Writer.WriteString("DBN2"); Writer.WriteUInt32(Data.Key); Writer.Key = Data.Key; Writer.WriteUInt32XOrBE((uint)Data.Entries.Length); Writer.WriteUInt32XOrBE(Data.Unknown); int DataOffset = 0x10 + Data.Entries.Length * 0x10; for (int i = 0; i < Data.Entries.Length; i++) { Output.Seek(0x10 + i * 0x10, SeekOrigin.Begin); byte[] Buffer = SDL2.GetBufferFromData(Data.Entries[i].SDL2); Writer.WriteUInt32XOrBE(Data.Entries[i].Unknown0); Writer.WriteUInt32XOrBE(Data.Entries[i].Unknown1); Writer.WriteUInt32XOrBE((uint)DataOffset); Writer.WriteUInt32XOrBE((uint)Buffer.Length); Output.Seek(DataOffset, SeekOrigin.Begin); Output.Write(Buffer, 0, Buffer.Length); DataOffset += Buffer.Length; //Align the buffer to 64-bits if needed while ((DataOffset & 7) != 0) { Writer.WriteUInt8XOr(0); DataOffset++; } } } }
/// <summary> /// Creates a buffer from the SDL2 data. /// </summary> /// <param name="Data">The data to be binarized</param> /// <returns>The data as a byte array</returns> public static byte[] GetBufferFromData(SDL2Data Data) { using (MemoryStream Output = new MemoryStream()) { EncryptedBinary Writer = new EncryptedBinary(Output); Writer.WriteString("SDL2"); Writer.WriteUInt32(Data.Key); Writer.Key = Data.Key; Writer.WriteUInt16XOrBE((ushort)Data.Unknown.Length); Writer.WriteUInt16XOrBE((ushort)Data.Dialogs.Length); int SectionALength = Data.Unknown.Length * 4; foreach (UnknownEntry Entry in Data.Unknown) { SectionALength += Entry.Values.Length * 8; } int SectionAOffset = 0xc + Data.Unknown.Length * 8 + Data.Dialogs.Length * 4; int SectionBOffset = SectionAOffset + SectionALength; int DialogsOffset = SectionBOffset + Data.Unknown.Length * 8; for (int i = 0; i < Data.Unknown.Length; i++) { Writer.WriteUInt32XOrBE((uint)SectionBOffset); Writer.WriteUInt32XOrBE((uint)SectionAOffset); long Position = Output.Position; Output.Seek(SectionAOffset, SeekOrigin.Begin); Writer.WriteUInt32XOrBE((uint)Data.Unknown[i].Values.Length); SectionAOffset += 4; foreach (SectionAEntry Entry in Data.Unknown[i].Values) { Writer.WriteUInt32XOrBE(Entry.Value0); Writer.WriteUInt32XOrBE(Entry.Value1); SectionAOffset += 8; } Output.Seek(SectionBOffset, SeekOrigin.Begin); Writer.WriteUInt32XOrBE(Data.Unknown[i].Value0); Writer.WriteUInt32XOrBE(Data.Unknown[i].Value1); SectionBOffset += 8; Output.Seek(Position, SeekOrigin.Begin); } for (int i = 0; i < Data.Dialogs.Length; i++) { Writer.WriteUInt32XOrBE((uint)DialogsOffset); long Position = Output.Position; Output.Seek(DialogsOffset, SeekOrigin.Begin); DialogsOffset += Writer.WriteUTF8(Data.Dialogs[i]); while ((DialogsOffset & 3) != 0) { //Align into 32-bits if needed Writer.WriteUInt8XOr(0); DialogsOffset++; } Output.Seek(Position, SeekOrigin.Begin); } return(Output.ToArray()); } }
/// <summary> /// Creates a buffer from the SDL2 data. /// </summary> /// <param name="Data">The data to be binarized</param> /// <returns>The data as a byte array</returns> public static byte[] GetBufferFromData(SDL2Data Data) { using (MemoryStream Output = new MemoryStream()) { EncryptedBinary Writer = new EncryptedBinary(Output); Writer.WriteString("SDL2"); Writer.WriteUInt32(Data.Key); Writer.Key = Data.Key; Writer.WriteUInt16XOrBE((ushort)Data.Unknown.Length); Writer.WriteUInt16XOrBE((ushort)Data.Dialogs.Length); int SectionALength = Data.Unknown.Length * 4; foreach (UnknownEntry Entry in Data.Unknown) SectionALength += Entry.Values.Length * 8; int SectionAOffset = 0xc + Data.Unknown.Length * 8 + Data.Dialogs.Length * 4; int SectionBOffset = SectionAOffset + SectionALength; int DialogsOffset = SectionBOffset + Data.Unknown.Length * 8; for (int i = 0; i < Data.Unknown.Length; i++) { Writer.WriteUInt32XOrBE((uint)SectionBOffset); Writer.WriteUInt32XOrBE((uint)SectionAOffset); long Position = Output.Position; Output.Seek(SectionAOffset, SeekOrigin.Begin); Writer.WriteUInt32XOrBE((uint)Data.Unknown[i].Values.Length); SectionAOffset += 4; foreach (SectionAEntry Entry in Data.Unknown[i].Values) { Writer.WriteUInt32XOrBE(Entry.Value0); Writer.WriteUInt32XOrBE(Entry.Value1); SectionAOffset += 8; } Output.Seek(SectionBOffset, SeekOrigin.Begin); Writer.WriteUInt32XOrBE(Data.Unknown[i].Value0); Writer.WriteUInt32XOrBE(Data.Unknown[i].Value1); SectionBOffset += 8; Output.Seek(Position, SeekOrigin.Begin); } for (int i = 0; i < Data.Dialogs.Length; i++) { Writer.WriteUInt32XOrBE((uint)DialogsOffset); long Position = Output.Position; Output.Seek(DialogsOffset, SeekOrigin.Begin); DialogsOffset += Writer.WriteUTF8(Data.Dialogs[i]); while ((DialogsOffset & 3) != 0) { //Align into 32-bits if needed Writer.WriteUInt8XOr(0); DialogsOffset++; } Output.Seek(Position, SeekOrigin.Begin); } return Output.ToArray(); } }