mirror of
https://github.com/KillzXGaming/Switch-Toolbox
synced 2024-12-25 20:33:07 +00:00
d1f03b161f
Rework UI from scratch with proper themes and custom controls. MDI windows are now used for workspaces, comparing docs, and multiple usages. Tabs organise multiple workspaces and you can keep mdi windows maximized if you want to only use tabs. Themes currently include dark and white theme but plan to have XML files with list of color and styles Alot of things optimized. UI is very fast and snappy now Dae rigging fixed. Dae bones can be imported. Dae with textures can be imported and exported to a folder Custom sampler editor for sampler data. Texture refs, shader options, params, render info, and basically all material data can be added/removed and edited User data editor Update opengl framework by JuPaHe64 to the newest. Includes an origintation cube, multiple models in a scene, and many improvements Skeleton can be viewed GFPAK with some fixes in saving NUTEXB has proper mip map viewing PTCL Editor (Wii U and Switch). Can edit colors ( Wii U) and view textures. Also EFFN files in smash ultimate can be previewed Files can be associated with the program and opened with on clicking them ASTC textures can be viewed UVs can be viewed. Includes wrap modes and also translating and scaling for some basic edits Textures use a new editor. It includes channel viewing and some new editing options Fixed black textures on some wii u bfres Fixed saving sarcs in sarcs Shortcut keys have been added in. CTRL + S can save the active file in the currently used window Fix more issues with bfres crashing File - New includes BNTX for creating new bntx files from scatch Raw shader binaries can be extracted from bnsh and bfsha. Yuzu and Ryujinx can decompile these Sharc files can have source data previewed and shader programs in XML Aamp v1 and v2 data can be previewed. v1 can be edited and saved atm, v2 will be at a later update Byaml uses it's own editor instead of a seperate window for easy saving within sarcs Archives have a hex viewer Dae exporting greatly improved and can export rigged meshes Scene, shader param, srt, color, and texture pattern animations can all be previewed (in a list) Memory usage is greatly improved Narc (Nitro Archives) can be viewed and extracted. Fixed importing TGA images Support importing ASTC textures for bntx Added in PBR lighting for bfres from my implimentaion in forge Added gradient background for viewport. This can be edited in the settings Added skybox background option for viewport. Can load cubemaps Added grid with customizable cells for viewport. DDS decompression no longer requires Direct X tex. Zlib decompression has been improved for opening files that use it Rigid bones are properly ordered on importing a mesh. May fix some exploding issues. Endianness for KCL can be toggled for saving. Will be set to what it was using orignally Tangents can be filled with a constant value. Will allow them to not cause seams nor flat lighting however normal maps may not work as good Vertex buffers can be added and removed. Also re encoded Parameters now use drop down panels with values for easier editing Reworked the bone editor. Everything for a bone can be fully edited now besides the index, billboard index and parent index which get set automatically Fixed animation scaling for skeletal animations finally! Textures can be loaded in a tab now with thumbnail displaying for easy real time edits while previewing in the viewport Fixed support for audio files to be big endian in BARS Textures for switch now use their own folder. You can easily add textures to this and add textures to bfres that have no bntx. If there are no textures then the bfres will automatically not have one on save. Animations are split into multiple sub sections for switch's material animation for easier access Bfres for wii u has better binary exporting and is fully compatiable with Wexos Toolbox (to and from) Every section can be added in as new for both wii u and switch. Every section can be renamed properly and mostly everything can be edited. (Key frame editing and a more in depth curve editor later) Added option to copy UV channel Bone weights can be previewed Tons of fixes for the switch bfres library with more games working. Splatoon 2 (more work now), BOTW, Kirby Star Allies, and more! Fixed 3.3 Wii U bfres from not opening Wii U Sharcfb files can have shader program data previewed (XML) And possibly alot more things i missed! All this is still experimental but will improve over the next few weeks
382 lines
14 KiB
C#
382 lines
14 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Drawing;
|
|
using System.Threading.Tasks;
|
|
using Switch_Toolbox;
|
|
using System.Windows.Forms;
|
|
using Switch_Toolbox.Library;
|
|
using Switch_Toolbox.Library.IO;
|
|
using Switch_Toolbox.Library.Forms;
|
|
|
|
namespace FirstPlugin
|
|
{
|
|
public class XTX : TreeNodeFile, IFileFormat
|
|
{
|
|
public bool CanSave { get; set; }
|
|
public string[] Description { get; set; } = new string[] { "XTX" };
|
|
public string[] Extension { get; set; } = new string[] { "*.xtx", "*.z" };
|
|
public string FileName { get; set; }
|
|
public string FilePath { get; set; }
|
|
public IFileInfo IFileInfo { get; set; }
|
|
|
|
public bool Identify(Stream stream)
|
|
{
|
|
using (var reader = new FileReader(stream, true))
|
|
{
|
|
return reader.CheckSignature(4, "DFvN");
|
|
}
|
|
}
|
|
|
|
public Type[] Types
|
|
{
|
|
get
|
|
{
|
|
List<Type> types = new List<Type>();
|
|
return types.ToArray();
|
|
}
|
|
}
|
|
|
|
public void Load(System.IO.Stream stream)
|
|
{
|
|
Text = FileName;
|
|
LoadFile(stream);
|
|
}
|
|
public void Unload()
|
|
{
|
|
|
|
}
|
|
public byte[] Save()
|
|
{
|
|
return null;
|
|
}
|
|
|
|
public class XTXFormats
|
|
{
|
|
public enum XTXImageFormat : uint
|
|
{
|
|
NVN_FORMAT_RGBA8 = 0x00000025,
|
|
NVN_FORMAT_RGBA8_SRGB = 0x00000038,
|
|
NVN_FORMAT_RGB10A2 = 0x0000003d,
|
|
NVN_FORMAT_RGB565 = 0x0000003c,
|
|
NVN_FORMAT_RGB5A1 = 0x0000003b,
|
|
NVN_FORMAT_RGBA4 = 0x00000039,
|
|
NVN_FORMAT_R8 = 0x00000001,
|
|
NVN_FORMAT_RG8 = 0x0000000d,
|
|
DXT1 = 0x00000042,
|
|
DXT3 = 0x00000043,
|
|
DXT5 = 0x00000044,
|
|
BC4U = 0x00000049,
|
|
BC4S = 0x0000004a,
|
|
BC5U = 0x0000004b,
|
|
BC5S = 0x0000004c,
|
|
};
|
|
|
|
public static uint blk_dims(uint format)
|
|
{
|
|
switch (format)
|
|
{
|
|
case (uint)XTXImageFormat.DXT1:
|
|
case (uint)XTXImageFormat.DXT3:
|
|
case (uint)XTXImageFormat.DXT5:
|
|
case (uint)XTXImageFormat.BC4U:
|
|
case (uint)XTXImageFormat.BC4S:
|
|
case (uint)XTXImageFormat.BC5U:
|
|
case (uint)XTXImageFormat.BC5S:
|
|
case 0x2d:
|
|
return 0x44;
|
|
|
|
default: return 0x11;
|
|
}
|
|
}
|
|
|
|
public static uint bpps(uint format)
|
|
{
|
|
switch (format)
|
|
{
|
|
case (uint)XTXImageFormat.NVN_FORMAT_R8:
|
|
return 1;
|
|
|
|
case (uint)XTXImageFormat.NVN_FORMAT_RGBA8:
|
|
case (uint)XTXImageFormat.NVN_FORMAT_RGBA8_SRGB:
|
|
case (uint)XTXImageFormat.NVN_FORMAT_RGB10A2:
|
|
return 4;
|
|
|
|
case (uint)XTXImageFormat.NVN_FORMAT_RGB565:
|
|
case (uint)XTXImageFormat.NVN_FORMAT_RGB5A1:
|
|
case (uint)XTXImageFormat.NVN_FORMAT_RGBA4:
|
|
case (uint)XTXImageFormat.NVN_FORMAT_RG8:
|
|
return 2;
|
|
|
|
case (uint)XTXImageFormat.DXT1:
|
|
case (uint)XTXImageFormat.BC4S:
|
|
case (uint)XTXImageFormat.BC4U:
|
|
return 8;
|
|
|
|
case (uint)XTXImageFormat.DXT3:
|
|
case (uint)XTXImageFormat.DXT5:
|
|
case (uint)XTXImageFormat.BC5U:
|
|
case (uint)XTXImageFormat.BC5S:
|
|
return 16;
|
|
default: return 0x00;
|
|
}
|
|
}
|
|
}
|
|
|
|
public uint HeaderSize { get; set; }
|
|
public uint MajorVersion { get; set; }
|
|
public uint MinorVersion { get; set; }
|
|
public BlockHeader blockHeader { get; set; }
|
|
private const int texHeadBlkType = 2;
|
|
private const int dataBlkType = 3;
|
|
|
|
public void LoadFile(Stream data)
|
|
{
|
|
FileReader reader = new FileReader(data);
|
|
string Signature = reader.ReadString(4, Encoding.ASCII);
|
|
if (Signature != "DFvN")
|
|
throw new Exception($"Invalid signature {Signature}! Expected DFvN.");
|
|
|
|
HeaderSize = reader.ReadUInt32();
|
|
MajorVersion = reader.ReadUInt32();
|
|
MinorVersion = reader.ReadUInt32();
|
|
|
|
blockHeader = new BlockHeader();
|
|
blockHeader.Read(reader);
|
|
Nodes.Add(blockHeader.textureInfo);
|
|
|
|
reader.Close();
|
|
reader.Dispose();
|
|
}
|
|
|
|
public class BlockHeader
|
|
{
|
|
public uint BlockSize { get; set; }
|
|
public UInt64 DataSize { get; set; }
|
|
public uint BlockType { get; set; }
|
|
public uint GlobalBlockIndex { get; set; }
|
|
public uint IncBlockTypeIndex { get; set; }
|
|
public TextureInfo textureInfo { get; set; }
|
|
public long DataOffset;
|
|
|
|
public void Read(FileReader reader)
|
|
{
|
|
string Signature = reader.ReadString(4, Encoding.ASCII);
|
|
if (Signature != "HBvN")
|
|
throw new Exception($"Invalid signature {Signature}! Expected HBvN.");
|
|
|
|
BlockSize = reader.ReadUInt32();
|
|
DataSize = reader.ReadUInt64();
|
|
DataOffset = reader.ReadInt64();
|
|
BlockType = reader.ReadUInt32();
|
|
GlobalBlockIndex = reader.ReadUInt32();
|
|
IncBlockTypeIndex = reader.ReadUInt32();
|
|
|
|
int indx = 0;
|
|
if (BlockType == texHeadBlkType)
|
|
{
|
|
textureInfo = new TextureInfo();
|
|
textureInfo.Read(reader);
|
|
textureInfo.Text = "Texture " + indx++;
|
|
}
|
|
if (BlockType == dataBlkType)
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|
|
public class TextureInfo : STGenericTexture
|
|
{
|
|
public override TEX_FORMAT[] SupportedFormats
|
|
{
|
|
get
|
|
{
|
|
return new TEX_FORMAT[]
|
|
{
|
|
TEX_FORMAT.BC1_UNORM,
|
|
TEX_FORMAT.BC2_UNORM,
|
|
TEX_FORMAT.BC3_UNORM,
|
|
TEX_FORMAT.BC4_UNORM,
|
|
TEX_FORMAT.BC5_UNORM,
|
|
TEX_FORMAT.R8_UNORM,
|
|
TEX_FORMAT.R8G8_UNORM,
|
|
TEX_FORMAT.R8G8_UNORM,
|
|
TEX_FORMAT.R10G10B10A2_UNORM,
|
|
TEX_FORMAT.B5G6R5_UNORM,
|
|
TEX_FORMAT.B5G5R5A1_UNORM,
|
|
TEX_FORMAT.B4G4R4A4_UNORM,
|
|
TEX_FORMAT.R8G8B8A8_UNORM,
|
|
TEX_FORMAT.R8G8B8A8_UNORM_SRGB,
|
|
};
|
|
}
|
|
}
|
|
|
|
public override bool CanEdit { get; set; } = false;
|
|
|
|
public UInt64 DataSize { get; set; }
|
|
public uint Alignment { get; set; }
|
|
public uint Depth { get; set; }
|
|
public uint Target { get; set; }
|
|
public XTXFormats.XTXImageFormat XTXFormat { get; set; }
|
|
public uint MipCount { get; set; }
|
|
public uint SliceSize { get; set; }
|
|
public uint[] MipOffsets { get; set; }
|
|
public BlockHeader DataBlockHeader { get; set; }
|
|
public byte[] ImageData;
|
|
|
|
public void Read(FileReader reader)
|
|
{
|
|
DataSize = reader.ReadUInt64();
|
|
Alignment = reader.ReadUInt32();
|
|
Width = reader.ReadUInt32();
|
|
Height = reader.ReadUInt32();
|
|
Depth = reader.ReadUInt32();
|
|
Target = reader.ReadUInt32();
|
|
XTXFormat = reader.ReadEnum<XTXFormats.XTXImageFormat>(true);
|
|
Format = ConvertFormat(XTXFormat);
|
|
|
|
MipCount = reader.ReadUInt32();
|
|
ArrayCount = 1;
|
|
|
|
SliceSize = reader.ReadUInt32();
|
|
long offPos = reader.Position;
|
|
MipOffsets = reader.ReadUInt32s((int)MipCount);
|
|
|
|
reader.Seek(offPos + 68, SeekOrigin.Begin);
|
|
byte[] Layout = reader.ReadBytes(8);
|
|
byte Sparse = reader.ReadByte();
|
|
reader.Seek(3);
|
|
long DataBlockOff = reader.Position;
|
|
|
|
DataBlockHeader = new BlockHeader();
|
|
DataBlockHeader.Read(reader);
|
|
|
|
reader.Seek(DataBlockOff + DataBlockHeader.DataOffset, SeekOrigin.Begin);
|
|
long datastart = reader.Position;
|
|
ImageData = reader.ReadBytes((int)DataSize);
|
|
|
|
if (ImageData.Length == 0)
|
|
throw new System.Exception("Empty data size!");
|
|
|
|
reader.Seek(DataBlockOff + DataBlockHeader.DataOffset + (long)DataBlockHeader.DataSize, SeekOrigin.Begin);
|
|
BlockHeader EndBlockHeader = new BlockHeader();
|
|
EndBlockHeader.Read(reader); }
|
|
|
|
public override void OnClick(TreeView treeview)
|
|
{
|
|
UpdateEditor();
|
|
}
|
|
public void UpdateEditor()
|
|
{
|
|
ImageEditorBase editor = (ImageEditorBase)LibraryGUI.Instance.GetActiveContent(typeof(ImageEditorBase));
|
|
if (editor == null)
|
|
{
|
|
editor = new ImageEditorBase();
|
|
editor.Dock = DockStyle.Fill;
|
|
|
|
LibraryGUI.Instance.LoadEditor(editor);
|
|
}
|
|
editor.Text = Text;
|
|
editor.LoadImage(this);
|
|
}
|
|
|
|
private static TEX_FORMAT ConvertFormat(XTXFormats.XTXImageFormat Format)
|
|
{
|
|
switch (Format)
|
|
{
|
|
case XTXFormats.XTXImageFormat.DXT1: return TEX_FORMAT.BC1_UNORM;
|
|
case XTXFormats.XTXImageFormat.DXT3: return TEX_FORMAT.BC2_UNORM;
|
|
case XTXFormats.XTXImageFormat.DXT5: return TEX_FORMAT.BC3_UNORM;
|
|
case XTXFormats.XTXImageFormat.BC4U: return TEX_FORMAT.BC4_UNORM;
|
|
case XTXFormats.XTXImageFormat.BC4S: return TEX_FORMAT.BC4_SNORM;
|
|
case XTXFormats.XTXImageFormat.BC5U: return TEX_FORMAT.BC5_UNORM;
|
|
case XTXFormats.XTXImageFormat.BC5S: return TEX_FORMAT.BC5_SNORM;
|
|
case XTXFormats.XTXImageFormat.NVN_FORMAT_R8: return TEX_FORMAT.R8_UNORM;
|
|
case XTXFormats.XTXImageFormat.NVN_FORMAT_RG8: return TEX_FORMAT.R8G8_UNORM;
|
|
case XTXFormats.XTXImageFormat.NVN_FORMAT_RGB10A2: return TEX_FORMAT.R10G10B10A2_UNORM;
|
|
case XTXFormats.XTXImageFormat.NVN_FORMAT_RGB565: return TEX_FORMAT.B5G6R5_UNORM;
|
|
case XTXFormats.XTXImageFormat.NVN_FORMAT_RGB5A1: return TEX_FORMAT.B5G5R5A1_UNORM;
|
|
case XTXFormats.XTXImageFormat.NVN_FORMAT_RGBA4: return TEX_FORMAT.B4G4R4A4_UNORM;
|
|
case XTXFormats.XTXImageFormat.NVN_FORMAT_RGBA8: return TEX_FORMAT.R8G8B8A8_UNORM;
|
|
case XTXFormats.XTXImageFormat.NVN_FORMAT_RGBA8_SRGB: return TEX_FORMAT.R8G8B8A8_UNORM_SRGB;
|
|
default:
|
|
throw new Exception($"Cannot convert format {Format}");
|
|
}
|
|
}
|
|
|
|
public override void SetImageData(Bitmap bitmap, int ArrayLevel)
|
|
{
|
|
throw new NotImplementedException("Cannot set image data! Operation not implemented!");
|
|
}
|
|
|
|
public override byte[] GetImageData(int ArrayLevel = 0, int MipLevel = 0)
|
|
{
|
|
uint bpp = GetBytesPerPixel(Format);
|
|
uint blkWidth = GetBlockHeight(Format);
|
|
uint blkHeight = GetBlockWidth(Format);
|
|
uint blkDepth = GetBlockDepth(Format);
|
|
|
|
uint blockHeight = TegraX1Swizzle.GetBlockHeight(TegraX1Swizzle.DIV_ROUND_UP(Height, blkHeight));
|
|
uint BlockHeightLog2 = (uint)Convert.ToString(blockHeight, 2).Length - 1;
|
|
|
|
int linesPerBlockHeight = (1 << (int)BlockHeightLog2) * 8;
|
|
|
|
int TileMode = 0;
|
|
|
|
|
|
List<byte[]> mips = new List<byte[]>();
|
|
|
|
int blockHeightShift = 0;
|
|
for (int mipLevel = 0; mipLevel < MipOffsets.Length; mipLevel++)
|
|
{
|
|
|
|
uint width = (uint)Math.Max(1, Width >> mipLevel);
|
|
uint height = (uint)Math.Max(1, Height >> mipLevel);
|
|
uint depth = (uint)Math.Max(1, Depth >> mipLevel);
|
|
|
|
// uint size = width * height * bpp;
|
|
uint size = TegraX1Swizzle.DIV_ROUND_UP(width, blkWidth) * TegraX1Swizzle.DIV_ROUND_UP(height, blkHeight) * bpp;
|
|
|
|
byte[] Output = new byte[size];
|
|
|
|
uint mipOffset;
|
|
if (mipLevel != 0)
|
|
{
|
|
mipOffset = (MipOffsets[mipLevel - 1]);
|
|
if (mipLevel == 1)
|
|
mipOffset -= (uint)size;
|
|
|
|
Array.Copy(ImageData, mipOffset, Output, 0, size);
|
|
}
|
|
else
|
|
Output = ImageData;
|
|
|
|
byte[] output = new byte[size];
|
|
Console.WriteLine(mipLevel + " " + size);
|
|
|
|
if (TegraX1Swizzle.pow2_round_up(TegraX1Swizzle.DIV_ROUND_UP(height, blkWidth)) < linesPerBlockHeight)
|
|
blockHeightShift += 1;
|
|
|
|
byte[] result = TegraX1Swizzle.deswizzle(width, height, depth, blkWidth, blkHeight, blkDepth, (int)Target, bpp, (uint)TileMode, (int)Math.Max(0, BlockHeightLog2 - blockHeightShift), Output);
|
|
//Create a copy and use that to remove uneeded data
|
|
byte[] result_ = new byte[size];
|
|
Array.Copy(result, 0, result_, 0, size);
|
|
|
|
result = null;
|
|
|
|
if (MipLevel == mipLevel)
|
|
return result_;
|
|
|
|
Console.WriteLine("bpp " + bpp);
|
|
Console.WriteLine("result_ " + size);
|
|
Console.WriteLine("width " + width);
|
|
Console.WriteLine("height " + height);
|
|
|
|
break;
|
|
}
|
|
return new byte[0];
|
|
}
|
|
}
|
|
}
|
|
}
|