Skip to content

Commit

Permalink
Added Bloodborne TPF support
Browse files Browse the repository at this point in the history
  • Loading branch information
Atvaark committed Jan 14, 2018
1 parent 10f99d6 commit eb46414
Show file tree
Hide file tree
Showing 20 changed files with 921 additions and 20 deletions.
10 changes: 10 additions & 0 deletions BinderTool.Core/BinderTool.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@
<Compile Include="Dcx\DcxCompression.cs" />
<Compile Include="Dcx\DcxFile.cs" />
<Compile Include="Dcx\DeflateCompression.cs" />
<Compile Include="Dds\DdsFile.cs" />
<Compile Include="Dds\DdsFileHeader.cs" />
<Compile Include="Dds\DdsFileHeaderDx10.cs" />
<Compile Include="Dds\DdsPixelFormat.cs" />
<Compile Include="Dds\Enum\D3D10ResourceDimension.cs" />
<Compile Include="Dds\Enum\DdsCaps2Flags.cs" />
<Compile Include="Dds\Enum\DdsFileHeaderFlags.cs" />
<Compile Include="Dds\Enum\DdsPixelFormatFlag.cs" />
<Compile Include="Dds\Enum\DdsSurfaceFlags.cs" />
<Compile Include="Dds\Enum\DxgiFormat.cs" />
<Compile Include="GameVersion.cs" />
<Compile Include="Enc\EncFile.cs" />
<Compile Include="Enfl\EntryFileListFile.cs" />
Expand Down
94 changes: 94 additions & 0 deletions BinderTool.Core/Dds/DdsFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.IO;
using System.Text;
using BinderTool.Core.Dds.Enum;

namespace BinderTool.Core.Dds
{
public class DdsFile
{
private const int MagicNumber = 0x20534444;

public DdsFileHeader Header { get; set; }

public DdsFileHeaderDx10 HeaderDx10 { get; set; }

public byte[] Data { get; set; }

public void Write(Stream outputStream)
{
BinaryWriter writer = new BinaryWriter(outputStream, Encoding.Default, true);
writer.Write(MagicNumber);
Header.Write(outputStream);
if (Header.IsDx10())
{
HeaderDx10.Write(outputStream);
}

writer.Write(Data);
}

public static byte[] ConvertData(byte[] sourceBuffer, int height, int width, DxgiFormat dxgiFormat)
{
if (sourceBuffer == null)
{
return null;
}

var inputStream = new MemoryStream(sourceBuffer);
byte[] targetBuffer = new byte[sourceBuffer.Length];
byte[] blockBuffer = new byte[64];
int heightBlock = height / 4;
int widthBlock = width / 4;
int bytesPerPixel = DdsPixelFormat.DxgiToBytesPerPixel(dxgiFormat) * 2;
for (int y = 0; y < heightBlock; y++)
{
for (int x = 0; x < widthBlock; x++)
{
int mx = x;
int my = y;
if (widthBlock > 1 && heightBlock > 1)
{
MapBlockPosition(x, y, widthBlock, 2, out mx, out my);
}

if (widthBlock > 2 && heightBlock > 2)
{
MapBlockPosition(mx, my, widthBlock, 4, out mx, out my);
}

if (widthBlock > 4 && heightBlock > 4)
{
MapBlockPosition(mx, my, widthBlock, 8, out mx, out my);
}

inputStream.Read(blockBuffer, 0, bytesPerPixel);
int destinationIndex = bytesPerPixel * (my * widthBlock + mx);
Array.Copy(blockBuffer, 0, targetBuffer, destinationIndex, bytesPerPixel);
}
}

return targetBuffer;
}

private static void MapBlockPosition(int x, int y, int w, int bx, out int mx, out int my)
{
int num1 = bx / 2;
int num2 = x / bx;
int num3 = y / num1;
int num4 = x % bx;
int num5 = y % num1;
int num6 = w / bx;
int num7 = 2 * num6;
int num8 = num2 + num3 * num6;
int num9 = num8 % num7;
int num10 = num9 / 2 + num9 % 2 * num6;
int num11 = num8 / num7 * num7 + num10;
int num12 = num11 % num6;
int num13 = num11 / num6;

mx = num12 * bx + num4;
my = num13 * num1 + num5;
}
}
}
71 changes: 71 additions & 0 deletions BinderTool.Core/Dds/DdsFileHeader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.IO;
using System.Text;
using BinderTool.Core.Dds.Enum;

namespace BinderTool.Core.Dds
{
public class DdsFileHeader
{
public const int DefaultHeaderSize = 124;

public int Size { get; set; } = DefaultHeaderSize;

public DdsFileHeaderFlags Flags { get; set; }

public int Height { get; set; }

public int Width { get; set; }

public int PitchOrLinearSize { get; set; }

public int Depth { get; set; }

public int MipMapCount { get; set; }

public DdsPixelFormat PixelFormat { get; set; }

public DdsSurfaceFlags Caps { get; set; }

public DdsCaps2Flags Caps2 { get; set; }

public int Caps3 { get; set; }

public int Caps4 { get; set; }

public void Write(Stream outputStream)
{
BinaryWriter writer = new BinaryWriter(outputStream, Encoding.Default, true);
writer.Write(Size);
writer.Write(Convert.ToInt32(Flags));
writer.Write(Height);
writer.Write(Width);
writer.Write(PitchOrLinearSize);
writer.Write(Depth);
writer.Write(MipMapCount);
// int Reserved1[11];
writer.WriteZeros(44);
PixelFormat.Write(outputStream);
writer.Write(Convert.ToInt32(Caps));
writer.Write(Convert.ToInt32(Caps2));
writer.Write(Caps3);
writer.Write(Caps4);
// int Reserved2
writer.WriteZeros(4);
}

public bool IsDx10()
{
return PixelFormat != null &&
PixelFormat.Flags.HasFlag(DdsPixelFormatFlag.FourCc) &&
PixelFormat.FourCc == DdsPixelFormat.Dx10FourCc;
}

public override string ToString()
{
return $"Size: {Size}, Flags: {Flags}, Height: {Height}, Width: {Width}," +
$" PitchOrLinearSize: {PitchOrLinearSize}, Depth: {Depth}, MipMapCount: {MipMapCount}," +
$" PixelFormat: {PixelFormat}, Caps: {Caps}, Caps2: {Caps2}, Caps3: {Caps3}, " + $"Caps4: {Caps4}";
}
}
}
30 changes: 30 additions & 0 deletions BinderTool.Core/Dds/DdsFileHeaderDx10.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.IO;
using System.Text;
using BinderTool.Core.Dds.Enum;

namespace BinderTool.Core.Dds
{
public class DdsFileHeaderDx10
{
public DxgiFormat Format { get; set; }

public D3D10ResourceDimension ResourceDimension { get; set; }

public uint MiscFlag { get; set; }

public uint ArraySize { get; set; }

public uint MiscFlags2 { get; set; }

public void Write(Stream outputStream)
{
BinaryWriter writer = new BinaryWriter(outputStream, Encoding.Default, true);
writer.Write(Convert.ToUInt32(Format));
writer.Write(Convert.ToInt32(ResourceDimension));
writer.Write(MiscFlag);
writer.Write(ArraySize);
writer.Write(MiscFlags2);
}
}
}
Loading

0 comments on commit eb46414

Please sign in to comment.