Switch-Toolbox/Switch_FileFormatsMain/FileFormats/Archives/NARC.cs

410 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 16:55:09 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Switch_Toolbox.Library;
using Switch_Toolbox.Library.IO;
using Switch_Toolbox.Library.Forms;
using System.Windows.Forms;
namespace FirstPlugin
{
class NARC : TreeNodeFile, IFileFormat
{
public bool CanSave { get; set; }
public string[] Description { get; set; } = new string[] { "Nitro Archive (NARC)" };
public string[] Extension { get; set; } = new string[] { "*.narc" };
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, "NARC");
}
}
public Type[] Types
{
get
{
List<Type> types = new List<Type>();
return types.ToArray();
}
}
public class FileEntry : TreeNodeCustom
{
public byte[] FileData { get; set; }
public NARC.FileAllocationEntry entry;
public FileImageBlock fileImage;
public FileEntry(string Name)
{
Text = Name;
if (Utils.HasExtension(Name, ".cbfmd") ||
Utils.HasExtension(Name, ".cbfa") ||
Utils.HasExtension(Name, ".cbfsa"))
{
ImageKey = "bfres";
SelectedImageKey = "bfres";
}
if (Utils.HasExtension(Name, ".cbntx"))
{
ImageKey = "bntx";
SelectedImageKey = "bntx";
}
ContextMenu = new ContextMenu();
ContextMenu.MenuItems.Add(new MenuItem("Export Raw Data", Export));
}
private void Export(object sender, EventArgs args)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.FileName = Text;
if (sfd.ShowDialog() == DialogResult.OK)
{
System.IO.File.WriteAllBytes(sfd.FileName, DecompressBlock());
}
}
public override void OnDoubleMouseClick(TreeView treeView)
{
TreeNode node = STFileLoader.GetNodeFileFormat(Text, DecompressBlock(), true, this);
if (node != null)
ReplaceNode(this.Parent, this, node);
}
public static void ReplaceNode(TreeNode node, TreeNode replaceNode, TreeNode NewNode)
{
if (NewNode == null)
return;
int index = node.Nodes.IndexOf(replaceNode);
node.Nodes.RemoveAt(index);
node.Nodes.Insert(index, NewNode);
}
public override void OnClick(TreeView treeView)
{
HexEditor editor = (HexEditor)LibraryGUI.Instance.GetActiveContent(typeof(HexEditor));
if (editor == null)
{
editor = new HexEditor();
LibraryGUI.Instance.LoadEditor(editor);
}
editor.Text = Text;
editor.Dock = DockStyle.Fill;
editor.LoadData(DecompressBlock());
}
public byte[] DecompressBlock()
{
byte[] data = GetBlock();
var reader = new FileReader(data);
reader.ByteOrder = Syroot.BinaryData.ByteOrder.BigEndian;
byte compType = reader.ReadByte();
if (compType == 0x50)
{
reader.Seek(4, System.IO.SeekOrigin.Begin);
uint decompSize = reader.ReadUInt32();
uint compSize = (uint)reader.BaseStream.Length - 8;
byte[] filedata = reader.getSection(8, (int)compSize);
// data = STLibraryCompression.Type_LZ4.Decompress(filedata, 0, (int)compSize, (int)decompSize);
// data = STLibraryCompression.GZIP.Decompress(filedata);
return data;
}
else if (compType == 0x30)
{
uint decompSize = reader.ReadUInt32();
uint compSize = (uint)reader.BaseStream.Length;
compSize -= 16;
byte[] filedata = reader.getSection(16, (int)compSize);
reader.Close();
reader.Dispose();
data = STLibraryCompression.ZLIB.Decompress(filedata, decompSize);
}
return data;
}
public byte[] GetBlock()
{
return Utils.SubArray(fileImage.dataBlock, entry.StartOffset, entry.EndOffset);
}
}
Header header;
public List<FileEntry> FileEntries = new List<FileEntry>();
public void Load(System.IO.Stream stream)
{
Text = FileName;
header = new Header(new FileReader(stream));
var names = GetNames(header.FNTB);
List<byte> Data = new List<byte>();
for (ushort i = 0; i < header.FATB.FileCount; i++)
{
FileEntries.Add(new FileEntry(names[i])
{
entry = header.FATB.FileEntries[i],
fileImage = header.FIMG,
});
Nodes.Add(FileEntries[i]);
}
}
public List<string> GetNames(FileNameTable nameTable)
{
var names = new List<string>();
foreach (var tblEntry in nameTable.entryNameTable)
{
if (tblEntry is EntryNameTableFileEntry)
names.Add(((EntryNameTableFileEntry)tblEntry).entryName);
}
return names;
}
public void Unload()
{
}
public byte[] Save()
{
return null;
}
//EFE for REing format https://github.com/Gericom/EveryFileExplorer/blob/f9f00d193c9608d71c9a23d9f3ab7e752f4ada2a/NDS/NitroSystem/FND/NARC.cs
public class Header
{
public string Signature;
public ushort ByteOrder;
public uint FileSize;
public uint Version;
public ushort HeaderSize;
public ushort DataBlocks;
public FileAllocationTableBlock FATB;
public FileNameTable FNTB;
public FileImageBlock FIMG;
public Header(FileReader reader)
{
reader.ByteOrder = Syroot.BinaryData.ByteOrder.LittleEndian;
reader.ReadSignature(4, "NARC");
ByteOrder = reader.ReadUInt16();
reader.CheckByteOrderMark(ByteOrder);
Version = reader.ReadUInt16();
FileSize = reader.ReadUInt32();
HeaderSize = reader.ReadUInt16();
DataBlocks = reader.ReadUInt16();
FATB = new FileAllocationTableBlock(reader);
FNTB = new FileNameTable(reader);
FIMG = new FileImageBlock(reader);
}
public void Write(FileWriter writer)
{
writer.WriteSignature(Signature);
writer.Write(ByteOrder);
writer.Write(Version);
writer.Write(FileSize);
writer.Write(HeaderSize);
writer.Write(DataBlocks);
}
}
public class FileAllocationTableBlock
{
public string Signature;
public uint Size;
public ushort FileCount;
public ushort Reserved;
public List<FileAllocationEntry> FileEntries = new List<FileAllocationEntry>();
public FileAllocationTableBlock(FileReader reader)
{
long startPos = reader.Position;
reader.ReadSignature(4, "BTAF");
Size = reader.ReadUInt32();
FileCount = reader.ReadUInt16();
Reserved = reader.ReadUInt16();
for (int i = 0; i < FileCount; i++)
FileEntries.Add(new FileAllocationEntry(reader));
reader.Seek(startPos + Size, System.IO.SeekOrigin.Begin);
}
}
public class FileAllocationEntry
{
public uint StartOffset;
public uint EndOffset;
public FileAllocationEntry(FileReader reader)
{
StartOffset = reader.ReadUInt32();
EndOffset = reader.ReadUInt32();
}
public void Write(FileWriter writer)
{
writer.Write(StartOffset);
writer.Write(EndOffset);
}
}
public class FileImageBlock
{
public string Signature;
public uint Size;
public byte[] dataBlock;
public FileImageBlock(FileReader reader)
{
reader.ReadSignature(4, "GMIF");
Size = reader.ReadUInt32();
dataBlock = reader.ReadBytes((int)(reader.BaseStream.Length - reader.BaseStream.Position));
}
}
public class FileNameTable
{
public string Signature;
public uint Size;
public List<DirectoryTableEntry> directoryTable = new List<DirectoryTableEntry>();
public List<EntryNameTableEntry> entryNameTable = new List<EntryNameTableEntry>();
public FileNameTable(FileReader reader)
{
long startPos = reader.BaseStream.Position;
reader.ReadSignature(4, "BTNF");
Size = reader.ReadUInt32();
directoryTable.Add(new DirectoryTableEntry(reader));
for (int i = 0; i < directoryTable[0].dirParentID - 1; i++)
{
directoryTable.Add(new DirectoryTableEntry(reader));
}
entryNameTable = new List<EntryNameTableEntry>();
int EndOfDirectory = 0;
while (EndOfDirectory < directoryTable[0].dirParentID)
{
byte entryNameLength = reader.ReadByte();
reader.BaseStream.Position--;
if (entryNameLength == 0)
{
entryNameTable.Add(new EntryNameTableEndOfDirectoryEntry(reader));
EndOfDirectory++;
}
else if (entryNameLength < 0x80) entryNameTable.Add(new EntryNameTableFileEntry(reader));
else entryNameTable.Add(new EntryNameTableDirectoryEntry(reader));
}
reader.BaseStream.Position = startPos + Size;
}
}
public class EntryNameTableEndOfDirectoryEntry : EntryNameTableEntry
{
public EntryNameTableEndOfDirectoryEntry() { }
public EntryNameTableEndOfDirectoryEntry(FileReader reader)
: base(reader) { }
public override void Write(FileWriter writer)
{
base.Write(writer);
}
}
public class EntryNameTableDirectoryEntry : EntryNameTableEntry
{
public string entryName;
public ushort directoryID;
public EntryNameTableDirectoryEntry(FileReader reader) : base(reader)
{
entryName = reader.ReadString(entryNameLength & 0x7F, Encoding.ASCII);
directoryID = reader.ReadUInt16();
}
public override void Write(FileWriter writer)
{
base.Write(writer);
writer.Write(entryName, Syroot.BinaryData.BinaryStringFormat.ZeroTerminated);
writer.Write(directoryID);
}
}
public class EntryNameTableFileEntry : EntryNameTableEntry
{
public string entryName;
public EntryNameTableFileEntry(FileReader reader) : base(reader)
{
entryName = reader.ReadString(entryNameLength, Encoding.ASCII);
}
public override void Write(FileWriter writer)
{
writer.Write(entryName, Syroot.BinaryData.BinaryStringFormat.ZeroTerminated);
}
}
public class EntryNameTableEntry
{
public byte entryNameLength;
protected EntryNameTableEntry() { }
public EntryNameTableEntry(FileReader reader)
{
entryNameLength = reader.ReadByte();
}
public virtual void Write(FileWriter writer)
{
writer.Write(entryNameLength);
}
}
public class DirectoryTableEntry
{
public uint dirEntryStart;
public ushort dirEntryFileID;
public ushort dirParentID;
public DirectoryTableEntry() { }
public DirectoryTableEntry(FileReader reader)
{
dirEntryStart = reader.ReadUInt32();
dirEntryFileID = reader.ReadUInt16();
dirParentID = reader.ReadUInt16();
}
public void Write(FileWriter writer)
{
writer.Write(dirEntryStart);
writer.Write(dirEntryFileID);
writer.Write(dirParentID);
}
}
}
}