This commit is contained in:
KillzXGaming 2022-12-09 18:46:04 -05:00
commit 142a32f3fc
8 changed files with 540 additions and 1 deletions

Binary file not shown.

View file

@ -0,0 +1,116 @@
// <auto-generated>
// automatically generated by the FlatBuffers compiler, do not modify
// </auto-generated>
using global::System;
using global::FlatBuffers;
namespace FlatBuffers.TRPAK
{
public struct TRPAK : IFlatbufferObject
{
private Table __p;
public ByteBuffer ByteBuffer { get { return __p.bb; } }
public static TRPAK GetRootAsTRPAK(ByteBuffer _bb) { return GetRootAsTRPAK(_bb, new TRPAK()); }
public static TRPAK GetRootAsTRPAK(ByteBuffer _bb, TRPAK obj) { return (obj.__assign(_bb.GetInt(_bb.Position) + _bb.Position, _bb)); }
public void __init(int _i, ByteBuffer _bb) { __p.bb_pos = _i; __p.bb = _bb; }
public TRPAK __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public ulong Hashes(int j) { int o = __p.__offset(4); return o != 0 ? __p.bb.GetUlong(__p.__vector(o) + j * 8) : (ulong)0; }
public int HashesLength { get { int o = __p.__offset(4); return o != 0 ? __p.__vector_len(o) : 0; } }
#if ENABLE_SPAN_T
public Span<byte> GetHashesBytes() { return __p.__vector_as_span(4); }
#else
public ArraySegment<byte>? GetHashesBytes() { return __p.__vector_as_arraysegment(4); }
#endif
public ulong[] GetHashesArray() { return __p.__vector_as_array<ulong>(4); }
public File? Files(int j) { int o = __p.__offset(6); return o != 0 ? (File?)(new File()).__assign(__p.__indirect(__p.__vector(o) + j * 4), __p.bb) : null; }
public int FilesLength { get { int o = __p.__offset(6); return o != 0 ? __p.__vector_len(o) : 0; } }
public static Offset<TRPAK> CreateTRPAK(FlatBufferBuilder builder,
VectorOffset hashesOffset = default(VectorOffset),
VectorOffset filesOffset = default(VectorOffset))
{
builder.StartObject(2);
TRPAK.AddFiles(builder, filesOffset);
TRPAK.AddHashes(builder, hashesOffset);
return TRPAK.EndTRPAK(builder);
}
public static void StartTRPAK(FlatBufferBuilder builder) { builder.StartObject(2); }
public static void AddHashes(FlatBufferBuilder builder, VectorOffset hashesOffset) { builder.AddOffset(0, hashesOffset.Value, 0); }
public static VectorOffset CreateHashesVector(FlatBufferBuilder builder, ulong[] data) { builder.StartVector(8, data.Length, 8); for (int i = data.Length - 1; i >= 0; i--) builder.AddUlong(data[i]); return builder.EndVector(); }
public static VectorOffset CreateHashesVectorBlock(FlatBufferBuilder builder, ulong[] data) { builder.StartVector(8, data.Length, 8); builder.Add(data); return builder.EndVector(); }
public static void StartHashesVector(FlatBufferBuilder builder, int numElems) { builder.StartVector(8, numElems, 8); }
public static void AddFiles(FlatBufferBuilder builder, VectorOffset filesOffset) { builder.AddOffset(1, filesOffset.Value, 0); }
public static VectorOffset CreateFilesVector(FlatBufferBuilder builder, Offset<File>[] data) { builder.StartVector(4, data.Length, 4); for (int i = data.Length - 1; i >= 0; i--) builder.AddOffset(data[i].Value); return builder.EndVector(); }
public static VectorOffset CreateFilesVectorBlock(FlatBufferBuilder builder, Offset<File>[] data) { builder.StartVector(4, data.Length, 4); builder.Add(data); return builder.EndVector(); }
public static void StartFilesVector(FlatBufferBuilder builder, int numElems) { builder.StartVector(4, numElems, 4); }
public static Offset<TRPAK> EndTRPAK(FlatBufferBuilder builder)
{
int o = builder.EndObject();
return new Offset<TRPAK>(o);
}
public static void FinishTRPAKBuffer(FlatBufferBuilder builder, Offset<TRPAK> offset) { builder.Finish(offset.Value); }
public static void FinishSizePrefixedTRPAKBuffer(FlatBufferBuilder builder, Offset<TRPAK> offset) { builder.FinishSizePrefixed(offset.Value); }
};
public struct File : IFlatbufferObject
{
private Table __p;
public ByteBuffer ByteBuffer { get { return __p.bb; } }
public static File GetRootAsFile(ByteBuffer _bb) { return GetRootAsFile(_bb, new File()); }
public static File GetRootAsFile(ByteBuffer _bb, File obj) { return (obj.__assign(_bb.GetInt(_bb.Position) + _bb.Position, _bb)); }
public void __init(int _i, ByteBuffer _bb) { __p.bb_pos = _i; __p.bb = _bb; }
public File __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public byte Unused { get { int o = __p.__offset(4); return o != 0 ? __p.bb.Get(o + __p.bb_pos) : (byte)0; } }
public Compression CompressionType { get { int o = __p.__offset(6); return o != 0 ? (Compression)__p.bb.Get(o + __p.bb_pos) : Compression.NONE; } }
public byte Unk1 { get { int o = __p.__offset(8); return o != 0 ? __p.bb.Get(o + __p.bb_pos) : (byte)0; } }
public ulong DecompressedSize { get { int o = __p.__offset(10); return o != 0 ? __p.bb.GetUlong(o + __p.bb_pos) : (ulong)0; } }
public byte Data(int j) { int o = __p.__offset(12); return o != 0 ? __p.bb.Get(__p.__vector(o) + j * 1) : (byte)0; }
public int DataLength { get { int o = __p.__offset(12); return o != 0 ? __p.__vector_len(o) : 0; } }
#if ENABLE_SPAN_T
public Span<byte> GetDataBytes() { return __p.__vector_as_span(12); }
#else
public ArraySegment<byte>? GetDataBytes() { return __p.__vector_as_arraysegment(12); }
#endif
public byte[] GetDataArray() { return __p.__vector_as_array<byte>(12); }
public static Offset<File> CreateFile(FlatBufferBuilder builder,
byte unused = 0,
Compression compression_type = Compression.NONE,
byte unk1 = 0,
ulong decompressed_size = 0,
VectorOffset dataOffset = default(VectorOffset))
{
builder.StartObject(5);
File.AddDecompressedSize(builder, decompressed_size);
File.AddData(builder, dataOffset);
File.AddUnk1(builder, unk1);
File.AddCompressionType(builder, compression_type);
File.AddUnused(builder, unused);
return File.EndFile(builder);
}
public static void StartFile(FlatBufferBuilder builder) { builder.StartObject(5); }
public static void AddUnused(FlatBufferBuilder builder, byte unused) { builder.AddByte(0, unused, 0); }
public static void AddCompressionType(FlatBufferBuilder builder, Compression compressionType) { builder.AddByte(1, (byte)compressionType, 255); }
public static void AddUnk1(FlatBufferBuilder builder, byte unk1) { builder.AddByte(2, unk1, 0); }
public static void AddDecompressedSize(FlatBufferBuilder builder, ulong decompressedSize) { builder.AddUlong(3, decompressedSize, 0); }
public static void AddData(FlatBufferBuilder builder, VectorOffset dataOffset) { builder.AddOffset(4, dataOffset.Value, 0); }
public static VectorOffset CreateDataVector(FlatBufferBuilder builder, byte[] data) { builder.StartVector(1, data.Length, 1); for (int i = data.Length - 1; i >= 0; i--) builder.AddByte(data[i]); return builder.EndVector(); }
public static VectorOffset CreateDataVectorBlock(FlatBufferBuilder builder, byte[] data) { builder.StartVector(1, data.Length, 1); builder.Add(data); return builder.EndVector(); }
public static void StartDataVector(FlatBufferBuilder builder, int numElems) { builder.StartVector(1, numElems, 1); }
public static Offset<File> EndFile(FlatBufferBuilder builder)
{
int o = builder.EndObject();
return new Offset<File>(o);
}
};
public enum Compression : byte
{
OODLE = 3,
NONE = 255,
};
}

View file

@ -0,0 +1,387 @@
using FirstPlugin.FileFormats.Hashes;
using FlatBuffers.TRPAK;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using Toolbox.Library;
using Toolbox.Library.IO;
namespace FirstPlugin
{
internal class TRPAK : TreeNodeFile, IArchiveFile, IFileFormat
{
public static bool shownOodleError;
public bool CanAddFiles => false;
public bool CanDeleteFiles => false;
public bool CanRenameFiles => false;
public bool CanReplaceFiles => false;
public bool CanSave { get; set; } = false;
public Dictionary<string, string> CategoryLookup
{
get
{
return new Dictionary<string, string>()
{
{ ".bntx", "Textures" },
{ ".trmbf", "Models" },
{ ".trmdl", "Models" },
{ ".trmsh", "Models" },
{ ".trmtr", "Models" },
{ ".trskl", "Models" },
{ ".tranm", "Animations" },
};
}
}
public string[] Description { get; set; } = new string[] { "tr Package" };
public string[] Extension { get; set; } = new string[] { "*.trpak" };
public string FileName { get; set; }
public string FilePath { get; set; }
public List<ArchiveFileInfo> files { get; set; }
public IEnumerable<ArchiveFileInfo> Files => files;
public FileType FileType { get; set; } = FileType.Archive;
public IFileInfo IFileInfo { get; set; }
public Type[] Types
{
get
{
List<Type> types = new List<Type>();
return types.ToArray();
}
}
public bool AddFile(ArchiveFileInfo archiveFileInfo)
{
throw new NotImplementedException();
}
public void ClearFiles()
{
throw new NotImplementedException();
}
public bool DeleteFile(ArchiveFileInfo archiveFileInfo)
{
throw new NotImplementedException();
}
public bool Identify(Stream stream)
{
return Utils.GetExtension(FileName) == ".trpak";
}
public void Load(Stream stream)
{
GFPAKHashCache.EnsureHashCache();
files = new List<ArchiveFileInfo>();
FlatBuffers.TRPAK.TRPAK trpak = FlatBuffers.TRPAK.TRPAK.GetRootAsTRPAK(new FlatBuffers.ByteBuffer(stream.ToBytes()));
if (trpak.FilesLength != trpak.HashesLength)
{
throw new Exception("not the same amount of Hashes and File Entries in Trpak Container");
}
List<string> paths = new List<string>();
for (int i = 0; i < trpak.FilesLength; i++)
{
FlatBuffers.TRPAK.File? file = trpak.Files(i);
ulong hash = trpak.Hashes(i);
if (file.HasValue)
{
ArchiveFileInfo AFI = new ArchiveFileInfo();
byte[] FileData = file.Value.GetDataArray();
if (file.Value.CompressionType == Compression.OODLE)
{
if (!shownOodleError && !System.IO.File.Exists($"{Runtime.ExecutableDir}\\oo2core_6_win64.dll"))
{
MessageBox.Show("'oo2core_6_win64.dll' not found in the executable folder! User must provide their own copy!");
shownOodleError = true;
}
byte[] FileDatadecompressed = Toolbox.Library.Compression.Oodle.Decompress(FileData, (long)file.Value.DecompressedSize);
FileData = FileDatadecompressed;
}
AFI.FileData = FileData;
AFI.Name = GetName(hash, FileData);
if (AFI.Name.Contains("/"))
{
string tmppath = System.IO.Path.GetDirectoryName(AFI.Name);
if (!paths.Contains(tmppath)) paths.Add(tmppath);
}
AFI.FileName = AFI.Name;
files.Add(AFI);
}
}
if (paths.Count == 1)
{
string path = paths[0].Replace("\\", "/");
foreach (var f in files)
{
if (!f.Name.Contains("/"))
{
f.Name = Path.Combine(path, f.Name).Replace("\\", "/");
f.FileName = f.Name;
}
}
}
TreeNode node = new QuickAccessFolder(this, "Quick access");
Nodes.Add(node);
Dictionary<string, TreeNode> folders = new Dictionary<string, TreeNode>();
foreach (var file in files)
{
string ext = Utils.GetExtension(file.FileName);
string folderName = "Other";
if (CategoryLookup.ContainsKey(ext))
folderName = CategoryLookup[ext];
if (!folders.ContainsKey(folderName))
{
TreeNode folder = new GFPAK.QuickAccessFileFolder(folderName);
if (folderName == "Textures")
folder = new TextureFolder(this, "Textures");
if (folderName == "Models")
folder = new GFPAK.QuickAccessFileFolder("Models");
if (folderName == "Animations")
folder = new GFPAK.AnimationFolder("Animations");
node.Nodes.Add(folder);
folders.Add(folderName, folder);
}
string name = Path.GetFileName(file.FileName);
string imageKey = "fileBlank";
switch (ext)
{
case ".bntx": imageKey = "bntx"; break;
case ".trmdl": imageKey = "model"; break;
case ".trskl": imageKey = "bone"; break;
case ".trmbf": imageKey = "mesh"; break;
case ".trmsh": imageKey = "mesh"; break;
case ".trmtr": imageKey = "material"; break;
}
TreeNode fodlerNode = folders[folderName];
fodlerNode.Nodes.Add(new QuickAccessFile(name)
{
Tag = file,
ImageKey = imageKey,
SelectedImageKey = imageKey,
});
}
GFPAKHashCache.WriteCache();
}
public void Save(Stream stream)
{
throw new NotImplementedException();
}
public void Unload()
{
foreach (var file in files)
{
if (file.FileFormat != null)
file.FileFormat.Unload();
file.FileData = null;
}
files.Clear();
GC.SuppressFinalize(this);
}
private string FindMatch(byte[] f, string FileName)
{
foreach (IFileFormat fileFormat in FileManager.GetFileFormats())
{
fileFormat.FileName = FileName;
if (fileFormat.Identify(new MemoryStream(f)) && fileFormat.GetType() != typeof(GFBMDL))
{
return fileFormat.Extension[0].Replace("*", "");
}
}
return "";
}
//For BNTX, BNSH, etc
private string GetBinaryHeaderName(byte[] Data)
{
using (var reader = new FileReader(Data))
{
reader.Seek(0x10, SeekOrigin.Begin);
uint NameOffset = reader.ReadUInt32();
reader.Seek(NameOffset, SeekOrigin.Begin);
return reader.ReadString(Syroot.BinaryData.BinaryStringFormat.ZeroTerminated);
}
}
private string GetName(ulong fileHash, byte[] Data)
{
string fileHashName = GFPAKHashCache.GetHashName(fileHash) ?? "";
string ext = FindMatch(Data, fileHashName);
if ((ext == ".bntx" && fileHashName == "") || ext == ".bfres" || ext == ".bnsh" || ext == ".bfsha")
{
string fileName = GetBinaryHeaderName(Data);
//Check for matches for shaders
if (ext == ".bnsh")
{
if (FNV64A1.Calculate($"{fileName}.bnsh_fsh") == fileHash)
fileName = $"{fileName}.bnsh_fsh";
else if (FNV64A1.Calculate($"{fileName}.bnsh_vsh") == fileHash)
fileName = $"{fileName}.bnsh_vsh";
}
else
fileName = $"{fileName}{ext}";
return $"{fileName}";
}
else
{
if (fileHashName != "")
{
return $"{fileHashName}";
}
else
return $"{fileHash.ToString("X")}{ext}";
}
}
public class TextureFolder : TreeNodeCustom, IContextMenuNode
{
private IArchiveFile ArchiveFile;
private bool HasExpanded = false;
private List<STGenericTexture> Textures = new List<STGenericTexture>();
public TextureFolder(IArchiveFile archive, string text)
{
ArchiveFile = archive;
Text = text;
}
public void AddTexture(string fileName)
{
BNTX bntx = BNTX.CreateBNTXFromTexture(fileName);
var mem = new MemoryStream();
bntx.Save(mem);
string filePath = fileName;
ArchiveFile.AddFile(new ArchiveFileInfo()
{
FileData = mem.ToArray(),
FileFormat = bntx,
FileName = filePath,
});
}
public virtual ToolStripItem[] GetContextMenuItems()
{
List<ToolStripItem> Items = new List<ToolStripItem>();
Items.Add(new ToolStripMenuItem("Export All", null, ExportAllAction, Keys.Control | Keys.E));
return Items.ToArray();
}
public void LoadTextures()
{
if (HasExpanded) return;
List<TreeNode> subNodes = new List<TreeNode>();
foreach (TreeNode node in Nodes)
{
var file = (ArchiveFileInfo)node.Tag;
if (file.FileFormat == null)
file.FileFormat = file.OpenFile();
BNTX bntx = file.FileFormat as BNTX;
foreach (var tex in bntx.Textures.Values)
{
tex.OnTextureDeleted += OnTextureDeleted;
//Set tree key for deletion
tex.Name = tex.Text;
tex.Tag = file;
var texNode = new TreeNode(tex.Text);
texNode.Tag = tex;
texNode.ImageKey = tex.ImageKey;
texNode.SelectedImageKey = tex.SelectedImageKey;
subNodes.Add(texNode);
Textures.Add(tex);
}
}
Nodes.Clear();
Nodes.AddRange(subNodes.ToArray());
HasExpanded = true;
}
public override void OnExpand()
{
LoadTextures();
}
private void ExportAllAction(object sender, EventArgs args)
{
LoadTextures();
List<string> Formats = new List<string>();
Formats.Add("Microsoft DDS (.dds)");
Formats.Add("Portable Graphics Network (.png)");
Formats.Add("Joint Photographic Experts Group (.jpg)");
Formats.Add("Bitmap Image (.bmp)");
Formats.Add("Tagged Image File Format (.tiff)");
FolderSelectDialog sfd = new FolderSelectDialog();
if (sfd.ShowDialog() == DialogResult.OK)
{
string folderPath = sfd.SelectedPath;
BatchFormatExport form = new BatchFormatExport(Formats);
if (form.ShowDialog() == DialogResult.OK)
{
foreach (STGenericTexture tex in Textures)
{
if (form.Index == 0)
tex.SaveDDS(folderPath + '\\' + tex.Text + ".dds");
else if (form.Index == 1)
tex.SaveBitMap(folderPath + '\\' + tex.Text + ".png");
else if (form.Index == 2)
tex.SaveBitMap(folderPath + '\\' + tex.Text + ".jpg");
else if (form.Index == 3)
tex.SaveBitMap(folderPath + '\\' + tex.Text + ".bmp");
else if (form.Index == 4)
tex.SaveBitMap(folderPath + '\\' + tex.Text + ".tiff");
}
}
}
}
private void OnTextureDeleted(object sender, EventArgs e)
{
var tex = (TextureData)sender;
foreach (var file in ArchiveFile.Files)
{
if (file.FileFormat != null && file.FileFormat is BNTX)
{
var bntx = (BNTX)file.FileFormat;
if (bntx.Textures.ContainsKey(tex.Text))
{
bntx.RemoveTexture(tex);
bntx.Unload();
ArchiveFile.DeleteFile(file);
Nodes.RemoveByKey(tex.Text);
}
}
}
}
}
}
}

View file

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Toolbox.Library;
using Toolbox.Library.Animations;
namespace FirstPlugin
{
internal class TRANM : GFBANM, IFileFormat, IAnimationContainer, IConvertableTextFormat
{
public new bool Identify(System.IO.Stream stream)
{
return Utils.GetExtension(FileName) == ".tranm";
}
}
}

View file

@ -226,6 +226,8 @@
<Compile Include="FileFormats\Archives\QuickAccess\QuickAccessFile.cs" />
<Compile Include="FileFormats\Archives\QuickAccess\QuickAccessFolder.cs" />
<Compile Include="FileFormats\Archives\RARC\RARC_Parser.cs" />
<Compile Include="FileFormats\Archives\TRPAK\Flatbuffers\TRPAK.cs" />
<Compile Include="FileFormats\Archives\TRPAK\TRPAK.cs" />
<Compile Include="FileFormats\Archives\WTA.cs" />
<Compile Include="FileFormats\Audio\BARS\BarsFile.cs" />
<Compile Include="FileFormats\BCH\BCH.cs" />
@ -483,6 +485,7 @@
<Compile Include="FileFormats\Pokemon\GFLX\GFBMDL\GFMDLStructs.cs" />
<Compile Include="FileFormats\Pokemon\GFLX\GFBPMCATALOG\FlatBuffers\gfbpmcatalog.cs" />
<Compile Include="FileFormats\Pokemon\GFLX\PokemonTable.cs" />
<Compile Include="FileFormats\Pokemon\Trinity\TRANM\TRANM.cs" />
<Compile Include="FileFormats\Rom\3DS\NCSDStructs.cs" />
<Compile Include="FileFormats\Rom\3DS\NCSD.cs" />
<Compile Include="FileFormats\Rom\3DS\RomFS.cs" />

View file

@ -350,6 +350,7 @@ namespace FirstPlugin
Formats.Add(typeof(BTI));
Formats.Add(typeof(TXE));
Formats.Add(typeof(SARC));
Formats.Add(typeof(TRPAK));
Formats.Add(typeof(BNTX));
Formats.Add(typeof(BEA));
Formats.Add(typeof(BYAML));
@ -460,6 +461,7 @@ namespace FirstPlugin
Formats.Add(typeof(MTXT));
Formats.Add(typeof(NKN));
Formats.Add(typeof(MetroidDreadLibrary.BSMAT));
Formats.Add(typeof(TRANM));
//Formats.Add(typeof(XLINK_FILE));

View file

@ -581,6 +581,19 @@ namespace Toolbox.Library
}
reader.TemporarySeek((int)(4 + header.size + DX10HeaderSize), SeekOrigin.Begin);
var UbiExtraData = reader.ReadUInt16();
reader.TemporarySeek(-2, SeekOrigin.Current);
if (UbiExtraData == 12816 || UbiExtraData == 1331 && IsDX10) //me when ubisoft | for some reason theres some extra data on some mario rabbids textures god knows what it is
{
if (header.width == 1024 && header.height == 1024)
{
reader.TemporarySeek((int)(4 + 30 + header.size + DX10HeaderSize), SeekOrigin.Begin);
}
if (header.width == 512 && header.height == 512)
{
reader.TemporarySeek((int)(4 + 26 + header.size + DX10HeaderSize), SeekOrigin.Begin);
}
}
bdata = reader.ReadBytes((int)(reader.BaseStream.Length - reader.BaseStream.Position));
Format = GetFormat();