1
0
mirror of synced 2024-12-05 12:28:02 +01:00
Switch-Toolbox/Switch_FileFormatsMain/FileFormats/Archives/SDF.cs

395 lines
14 KiB
C#
Raw Normal View History

Add files for the new one. 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
2019-03-23 17:55:09 +01:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Switch_Toolbox;
using System.Windows.Forms;
using Switch_Toolbox.Library;
using Switch_Toolbox.Library.IO;
namespace FirstPlugin
{
public class SDF : TreeNodeFile, IFileFormat
{
public bool CanSave { get; set; }
public string[] Description { get; set; } = new string[] { "Snow Engine Data Table Of Contents" };
public string[] Extension { get; set; } = new string[] { "*.sdftoc" };
public string FileName { get; set; }
public string FilePath { get; set; }
public IFileInfo IFileInfo { get; set; }
public bool Identify(System.IO.Stream stream)
{
using (var reader = new Switch_Toolbox.Library.IO.FileReader(stream, true))
{
return reader.CheckSignature(4, "WEST");
}
}
public Type[] Types
{
get
{
List<Type> types = new List<Type>();
return types.ToArray();
}
}
SDFTOC_Header Header;
SDFTOC_ID startId;
int[] block1;
SDFTOC_ID[] blockIds;
SDFTOC_Block2[] block2Array;
byte[] DecompressedBlock;
SDFTOC_ID endId;
List<string> FilePaths = new List<string>();
//Thanks to https://github.com/GoldFarmer/rouge_sdf/blob/master/main.cpp for docs/structs
public void Load(System.IO.Stream stream)
{
using (var reader = new FileReader(stream))
{
reader.ByteOrder = Syroot.BinaryData.ByteOrder.LittleEndian;
//Read header
Header = new SDFTOC_Header();
Header.Read(reader);
//Read first id
startId = new SDFTOC_ID(reader);
//Check this flag
byte Flag1 = reader.ReadByte();
if (Flag1 != 0)
{
byte[] unk = reader.ReadBytes(0x140);
}
//Read first block
block1 = reader.ReadInt32s((int)Header.Block1Count);
//Read ID blocks
blockIds = new SDFTOC_ID[Header.Block1Count];
for (int i = 0; i < Header.Block1Count; i++)
{
blockIds[i] = new SDFTOC_ID(reader);
}
//Read block 2 (DDS headers)
block2Array = new SDFTOC_Block2[Header.Block2Count];
for (int i = 0; i < Header.Block2Count; i++)
{
block2Array[i] = new SDFTOC_Block2(reader, Header);
}
//Here is the compressed block. Check the magic first
uint magic = reader.ReadUInt32();
reader.Seek(-4, SeekOrigin.Current);
//Read and decompress the compressed block
//Contains file names and block info
DecompressNameBlock(magic, reader.ReadBytes((int)Header.CompressedSize), Header);
//Read last id
endId = new SDFTOC_ID(reader);
Text = FileName;
LoadTree();
}
}
private void LoadTree()
{
// Get a list of everything under the users' temp folder as an example
string[] fileList;
fileList = FilePaths.ToArray();
// Parse the file list into a TreeNode collection
// TreeNode node = GetNodes(new TreeNode(), fileList);
// Nodes.Add(node); // Add the new nodes
// Copy the new nodes to an array
// int nodeCount = node.Nodes.Count;
// TreeNode[] nodes = new TreeNode[nodeCount];
// node.Nodes.CopyTo(nodes, 0);
// Nodes.AddRange(nodes); // Add the new nodes
}
private TreeNode GetNodes(TreeNode parent, string[] fileList)
{
// build a TreeNode collection from the file list
foreach (string strPath in fileList)
{
// Every time we parse a new file path, we start at the top level again
TreeNode thisParent = parent;
// split the file path into pieces at every backslash
foreach (string pathPart in strPath.Split('\\'))
{
// check if we already have a node for this
TreeNode[] tn = thisParent.Nodes.Find(pathPart, false);
if (tn.Length == 0)
{
// no node found, so add one
thisParent = thisParent.Nodes.Add(pathPart, pathPart);
}
else
{
// we already have this node, so use it as the parent of the next part of the path
thisParent = tn[0];
}
}
}
return parent;
}
void FillTreeNodes(TreeNode root, List<string> files)
{
var rootText = root.Text;
var rootTextLength = rootText.Length;
var nodeStrings = files;
foreach (var node in nodeStrings)
{
string nodeString = node;
nodeString = nodeString.Replace(@"\", "/");
var roots = nodeString.Split(new char[] { '/' },
StringSplitOptions.RemoveEmptyEntries);
// The initial parent is the root node
var parentNode = root;
var sb = new StringBuilder(rootText, nodeString.Length + rootTextLength);
for (int rootIndex = 0; rootIndex < roots.Length; rootIndex++)
{
// Build the node name
var parentName = roots[rootIndex];
sb.Append("/");
sb.Append(parentName);
var nodeName = sb.ToString();
// Search for the node
var index = parentNode.Nodes.IndexOfKey(nodeName);
if (index == -1)
{
// Node was not found, add it
var temp = new TreeNode(parentName, 0, 0);
if (rootIndex == roots.Length - 1)
temp = new TreeNode(parentName); //File entry
temp.Name = nodeName;
parentNode.Nodes.Add(temp);
parentNode = temp;
}
else
{
// Node was found, set that as parent and continue
parentNode = parentNode.Nodes[index];
}
}
}
}
public void DecompressNameBlock(uint magic, byte[] CompressedBlock, SDFTOC_Header header)
{
byte[] decomp = null;
if (magic == 0xDFF25B82 || magic == 0xFD2FB528)
decomp = STLibraryCompression.ZSTD.Decompress(CompressedBlock);
else if (header.Version > 22)
decomp = STLibraryCompression.Type_LZ4.Decompress(CompressedBlock);
else
decomp = STLibraryCompression.ZLIB.Decompress(CompressedBlock, Header.DecompressedSize);
//Now it's decompressed lets parse!
using (var reader = new FileReader(decomp))
{
ParseNames(reader);
}
}
private ulong readVariadicInteger(int Count, FileReader reader)
{
ulong result = 0;
for (int i = 0; i < Count; i++)
{
result |= (ulong)(reader.ReadByte()) << (i * 8);
}
return result;
}
public void ParseNames(FileReader reader, string Name = "")
{
if (!Name.Contains("dummy"))
FilePaths.Add(Name);
char ch = reader.ReadChar();
if (ch == 0)
throw new Exception("Unexcepted byte in file tree");
if (ch >= 1 && ch <= 0x1f) //string part
{
while (ch-- != 0)
{
Name += reader.ReadChar();
}
ParseNames(reader, Name);
}
else if (ch >= 'A' && ch <= 'Z') //file entry
{
int var = Convert.ToInt32(ch - 'A');
ch = Convert.ToChar(var);
int count1 = ch >> 7;
int flag1 = (ch >> 3) & 1;
if (count1 != 0)
{
uint strangeId = reader.ReadUInt32();
byte chr2 = reader.ReadByte();
int byteCount = chr2 & 3;
int byteValue = chr2 >> 2;
ulong DdsType = readVariadicInteger(byteCount, reader);
for (int chunkIndex = 0; chunkIndex < count1; chunkIndex++)
{
byte ch3 = reader.ReadByte();
int compressedSizeByteCount = (ch3 & 3) + 1;
int packageOffsetByteCount = (ch3 >> 2) & 7;
int hasCompression = (ch3 >> 5) & 1;
ulong decompressedSize = readVariadicInteger(compressedSizeByteCount, reader);
ulong compressedSize = 0;
ulong packageOffset = 0;
if (hasCompression != 0)
{
compressedSize = readVariadicInteger(compressedSizeByteCount, reader);
}
if (packageOffsetByteCount != 0)
{
packageOffset = readVariadicInteger(packageOffsetByteCount, reader);
}
ulong packageId = readVariadicInteger(2, reader);
List<ulong> compSizeArray = new List<ulong>();
if (hasCompression != 0)
{
ulong pageCount = (decompressedSize + 0xffff) >> 16;
if (pageCount > 1)
{
for (ulong page = 0; page < pageCount; page++)
{
ulong compSize = readVariadicInteger(2, reader);
compSizeArray.Add(compSize);
}
}
}
ulong fileId = readVariadicInteger(4, reader);
if (compSizeArray.Count == 0 && hasCompression != 0)
compSizeArray.Add(compressedSize);
DumpFile(Name, packageId, packageOffset, decompressedSize, compSizeArray, DdsType, chunkIndex != 0, byteCount != 0 && chunkIndex == 0);
}
}
if (flag1 != 0)
{
byte ch3 = reader.ReadByte();
while (ch3-- != 0)
{
byte ch3_1 = reader.ReadByte();
byte ch3_2 = reader.ReadByte();
}
}
}
else
{
uint offset = reader.ReadUInt32();
ParseNames(reader, Name);
reader.Seek(offset, SeekOrigin.Begin);
ParseNames(reader, Name);
}
}
public void DumpFile(string Name, ulong packageId, ulong packageOffset, ulong decompresedSize,
List<ulong> compressedSize, ulong ddsType, bool Append, bool UseDDS)
{
string layer;
}
public void Unload()
{
}
public byte[] Save()
{
return null;
}
public class SDFTOC_Header
{
public uint Version { get; set; }
public uint DecompressedSize { get; set; }
public uint CompressedSize { get; set; }
public uint Zero { get; set; }
public uint Block1Count { get; set; }
public uint Block2Count { get; set; }
public void Read(FileReader reader)
{
reader.CheckSignature(4, "WEST");
reader.Seek(4, System.IO.SeekOrigin.Begin);
Version = reader.ReadUInt32();
DecompressedSize = reader.ReadUInt32();
CompressedSize = reader.ReadUInt32();
Zero = reader.ReadUInt32();
Block1Count = reader.ReadUInt32();
Block2Count = reader.ReadUInt32();
}
}
public class SDFTOC_ID
{
public ulong ubisoft { get; set; }
public byte[] Data { get; set; }
public ulong massive { get; set; }
public SDFTOC_ID(FileReader reader)
{
ubisoft = reader.ReadUInt64();
Data = reader.ReadBytes(0x20);
massive = reader.ReadUInt64();
}
}
public class SDFTOC_Block2 //Seems to be for DDS headers
{
public uint UsedBytes { get; set; }
public byte[] Data { get; set; }
public SDFTOC_Block2(FileReader reader, SDFTOC_Header header)
{
if (header.Version == 22)
{
UsedBytes = reader.ReadUInt32();
Data = reader.ReadBytes(0xC8);
}
else
{
UsedBytes = reader.ReadUInt32();
Data = reader.ReadBytes(0x94);
}
}
}
}
}