Fix 0.9 bfres with scene animations. Add toggle for direct x tex decoder

This commit is contained in:
KillzXGaming 2019-06-27 14:38:13 -04:00
parent 141be9ff4f
commit 47ca70a015
15 changed files with 182 additions and 42 deletions

Binary file not shown.

View file

@ -72,10 +72,12 @@ namespace FirstPlugin
public MenuExt()
{
// toolExt[0] = new STToolStripItem("Models");
// toolExt[0].DropDownItems.Add(new STToolStripItem("Batch Export (BFRES)", Export));
editExt[0] = new STToolStripItem("Use Advanced Editor As Default", AdvancedEditor);
newFileExt[0] = new STToolStripItem("BFRES (Switch)", NewSwitchBfres);
newFileExt[1] = new STToolStripItem("BFRES (Wii U)", NewWiiUBfres);
editExt[0].Checked = !PluginRuntime.UseSimpleBfresEditor;
}
@ -142,6 +144,71 @@ namespace FirstPlugin
debugInfo.Show();
debugInfo.PrintDebugInfo(ofd.FileName);
}
private void Export(object sender, EventArgs args)
{
string formats = FileFilters.FMDL_EXPORT;
string[] forms = formats.Split('|');
List<string> Formats = new List<string>();
for (int i = 0; i < forms.Length; i++)
{
if (i > 1 || i == (forms.Length - 1)) //Skip lines with all extensions
{
if (!forms[i].StartsWith("*"))
Formats.Add(forms[i]);
}
}
BatchFormatExport form = new BatchFormatExport(Formats);
if (form.ShowDialog() == DialogResult.OK)
{
string Extension = form.GetSelectedExtension();
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
ofd.Filter = Utils.GetAllFilters(new Type[] { typeof(BFRES), typeof(SARC) });
if (ofd.ShowDialog() == DialogResult.OK)
{
FolderSelectDialog folderDialog = new FolderSelectDialog();
if (folderDialog.ShowDialog() == DialogResult.OK)
{
foreach (string file in ofd.FileNames)
{
var FileFormat = STFileLoader.OpenFileFormat(file, new Type[] { typeof(BFRES), typeof(SARC) });
if (FileFormat == null)
continue;
SearchBinary(FileFormat, folderDialog.SelectedPath, Extension);
}
}
}
}
}
private void SearchBinary(IFileFormat FileFormat, string Folder, string Extension)
{
if (FileFormat is SARC)
{
foreach (var file in ((SARC)FileFormat).Files)
{
Console.WriteLine($"{FileFormat.FileName} {file.Text}");
var archiveFile = STFileLoader.OpenFileFormat(file.FullName, new Type[] { typeof(BFRES), typeof(SARC) }, file.Data);
if (archiveFile == null)
continue;
SearchBinary(archiveFile, Folder, Extension);
}
}
if (FileFormat is BFRES)
{
// ((BFRES)FileFormat).Export(Path.Combine(Folder, $"{FileFormat.FileName}{Extension}"));
}
FileFormat.Unload();
}
}
private static byte[] CreateNewBFRESSwitch(string Name)

View file

@ -231,6 +231,9 @@ namespace Switch_Toolbox.Library
case "FrameCamera":
bool.TryParse(node.InnerText, out Runtime.FrameCamera);
break;
case "UseDirectXTexDecoder":
bool.TryParse(node.InnerText, out Runtime.UseDirectXTexDecoder);
break;
}
}
}
@ -353,6 +356,7 @@ namespace Switch_Toolbox.Library
XmlNode mainSettingsNode = doc.CreateElement("MAINFORM");
parentNode.AppendChild(mainSettingsNode);
mainSettingsNode.AppendChild(createNode(doc, "UseDirectXTexDecoder", Runtime.UseDirectXTexDecoder.ToString()));
mainSettingsNode.AppendChild(createNode(doc, "DisplayViewport", Runtime.DisplayViewport.ToString()));
mainSettingsNode.AppendChild(createNode(doc, "UseOpenGL", Runtime.UseOpenGL.ToString()));
mainSettingsNode.AppendChild(createNode(doc, "UseDebugDomainExceptionHandler", Runtime.UseDebugDomainExceptionHandler.ToString()));

View file

@ -237,13 +237,11 @@ namespace Switch_Toolbox.Library
int W = (width + 3) / 4;
int H = (height + 3) / 4;
byte[] Output = new byte[W * H * 64];
for (int Y = 0; Y < H; Y++)
{
for (int X = 0; X < W; X++)
{
int IOffs = (Y * W + X) * 16;
byte[] Red = new byte[8];

View file

@ -377,10 +377,16 @@ namespace Switch_Toolbox.Library
case TEX_FORMAT.ETC1_A4:
return BitmapExtension.GetBitmap(ETC1.ETC1Decompress(data, (int)width, (int)height, true),
(int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
default:
return BitmapExtension.GetBitmap(DecodeBlock(data, width, height, Format),
(int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
}
if (Runtime.UseDirectXTexDecoder)
{
return BitmapExtension.GetBitmap(DecodeBlock(data, width, height, Format),
(int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
}
else
return DecodeNotDirectXTex(data, width, height, Format);
}
catch (Exception ex)
{
@ -388,29 +394,7 @@ namespace Switch_Toolbox.Library
try
{
if (Format == TEX_FORMAT.BC1_UNORM)
return DDSCompressor.DecompressBC1(data, (int)Width, (int)Height, false);
else if (Format == TEX_FORMAT.BC1_UNORM_SRGB)
return DDSCompressor.DecompressBC1(data, (int)Width, (int)Height, true);
else if (Format == TEX_FORMAT.BC3_UNORM_SRGB)
return DDSCompressor.DecompressBC3(data, (int)Width, (int)Height, false);
else if (Format == TEX_FORMAT.BC3_UNORM)
return DDSCompressor.DecompressBC3(data, (int)Width, (int)Height, true);
else if (Format == TEX_FORMAT.BC4_UNORM)
return DDSCompressor.DecompressBC4(data, (int)Width, (int)Height, false);
else if (Format == TEX_FORMAT.BC4_SNORM)
return DDSCompressor.DecompressBC4(data, (int)Width, (int)Height, true);
else if (Format == TEX_FORMAT.BC5_UNORM)
return DDSCompressor.DecompressBC5(data, (int)Width, (int)Height, false);
else
{
if (Runtime.UseOpenGL)
{
Runtime.OpenTKInitialized = true;
LoadOpenGLTexture();
return RenderableTex.GLTextureToBitmap(RenderableTex);
}
}
return DecodeNotDirectXTex(data, width, height, Format);
}
catch
{
@ -421,6 +405,40 @@ namespace Switch_Toolbox.Library
}
}
private Bitmap DecodeNotDirectXTex(byte[] data, uint Width, uint Height, TEX_FORMAT Format)
{
if (Format == TEX_FORMAT.BC1_UNORM)
return DDSCompressor.DecompressBC1(data, (int)Width, (int)Height, false);
else if (Format == TEX_FORMAT.BC1_UNORM_SRGB)
return DDSCompressor.DecompressBC1(data, (int)Width, (int)Height, true);
else if (Format == TEX_FORMAT.BC3_UNORM_SRGB)
return DDSCompressor.DecompressBC3(data, (int)Width, (int)Height, false);
else if (Format == TEX_FORMAT.BC3_UNORM)
return DDSCompressor.DecompressBC3(data, (int)Width, (int)Height, true);
else if (Format == TEX_FORMAT.BC4_UNORM)
return DDSCompressor.DecompressBC4(data, (int)Width, (int)Height, false);
else if (Format == TEX_FORMAT.BC4_SNORM)
return DDSCompressor.DecompressBC4(data, (int)Width, (int)Height, true);
else if (Format == TEX_FORMAT.BC5_UNORM)
return DDSCompressor.DecompressBC5(data, (int)Width, (int)Height, false);
else if (Format == TEX_FORMAT.BC7_UNORM)
return BitmapExtension.GetBitmap(CSharpImageLibrary.DDS.Dxt.DecompressBc7(data, (int)Width, (int)Height), (int)Width, (int)Height);
else if (Format == TEX_FORMAT.BC7_UNORM_SRGB)
return BitmapExtension.GetBitmap(CSharpImageLibrary.DDS.Dxt.DecompressBc7(data, (int)Width, (int)Height), (int)Width, (int)Height);
else
{
if (Runtime.UseOpenGL)
{
Runtime.OpenTKInitialized = true;
if (RenderableTex == null || !RenderableTex.GLInitialized)
LoadOpenGLTexture();
return RenderableTex.ToBitmap();
}
}
return null;
}
public static Bitmap DecodeBlockGetBitmap(byte[] data, uint Width, uint Height, TEX_FORMAT Format)
{
Bitmap bitmap = BitmapExtension.GetBitmap(DecodeBlock(data, Width, Height, Format),

View file

@ -66,17 +66,21 @@ namespace Switch_Toolbox.Library.Rendering
case TEX_FORMAT.BC4_SNORM:
//Convert to rgb to prevent red output
//While shaders could prevent this, converting is easier and works fine across all editors
data = STGenericTexture.DecodeBlock(data,
if (Runtime.UseDirectXTexDecoder)
{
data = STGenericTexture.DecodeBlock(data,
GenericTexture.Width,
GenericTexture.Height,
GenericTexture.Format,
GenericTexture.PlatformSwizzle);
pixelInternalFormat = PixelInternalFormat.Rgba;
pixelFormat = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
// pixelInternalFormat = PixelInternalFormat.CompressedRedRgtc1;
// pixelInternalFormat = PixelInternalFormat.CompressedSignedRedRgtc1;
pixelInternalFormat = PixelInternalFormat.Rgba;
pixelFormat = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
}
else
{
pixelInternalFormat = PixelInternalFormat.CompressedRedRgtc1;
pixelInternalFormat = PixelInternalFormat.CompressedSignedRedRgtc1;
}
break;
case TEX_FORMAT.BC5_SNORM:
pixelInternalFormat = PixelInternalFormat.CompressedRgRgtc2;
@ -110,14 +114,17 @@ namespace Switch_Toolbox.Library.Rendering
pixelFormat = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
break;
default:
data = STGenericTexture.DecodeBlock(data,
if (Runtime.UseDirectXTexDecoder)
{
data = STGenericTexture.DecodeBlock(data,
GenericTexture.Width,
GenericTexture.Height,
GenericTexture.Format,
GenericTexture.PlatformSwizzle);
pixelInternalFormat = PixelInternalFormat.Rgba;
pixelFormat = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
pixelInternalFormat = PixelInternalFormat.Rgba;
pixelFormat = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
}
break;
}
GLInitialized = true;

View file

@ -33,6 +33,8 @@ namespace Switch_Toolbox.Library.IO
public static IFileFormat OpenFileFormat(string FileName, Type[] FileTypes, byte[] data = null)
{
CheckCompression(FileName, data);
Stream stream;
if (data != null)
stream = new MemoryStream(data);
@ -56,6 +58,8 @@ namespace Switch_Toolbox.Library.IO
public static IFileFormat OpenFileFormat(string FileName, Type FileType, byte[] data = null)
{
CheckCompression(FileName, data);
Stream stream;
if (data != null)
stream = new MemoryStream(data);
@ -71,6 +75,46 @@ namespace Switch_Toolbox.Library.IO
return null;
}
private static void CheckCompression(string FileName, byte[] data)
{
if (data != null)
{
using (var reader = new FileReader(data))
{
DecompressData(reader, FileName, data);
}
}
else
{
using (var reader = new FileReader(FileName))
{
DecompressData(reader, FileName, data);
}
}
}
private static void DecompressData(FileReader reader, string FileName, byte[] data)
{
reader.ByteOrder = ByteOrder.BigEndian;
uint MagicHex = reader.ReadUInt32();
string Magic = reader.ReadMagic(0, 4);
reader.Position = 0;
if (Magic == "Yaz0")
{
if (data != null)
data = EveryFileExplorer.YAZ0.Decompress(data);
else
data = EveryFileExplorer.YAZ0.Decompress(FileName);
}
if (MagicHex == 0x28B52FFD || MagicHex == 0xFD2FB528)
{
if (data != null)
data = STLibraryCompression.ZSTD.Decompress(data);
else
data = STLibraryCompression.ZSTD.Decompress(File.ReadAllBytes(FileName));
}
}
/// <summary>
/// Gets the <see cref="IFileFormat"/> from a file or byte array.

View file

@ -15,6 +15,8 @@ namespace Switch_Toolbox.Library
public class Runtime
{
public static bool UseDirectXTexDecoder = true;
public static bool DEVELOPER_DEBUG_MODE = false;
public static class ResourceTables