Skip to content
This repository has been archived by the owner on Dec 31, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
KillzXGaming committed Nov 18, 2023
2 parents 9a47c36 + 05dd784 commit 2fa935c
Show file tree
Hide file tree
Showing 11 changed files with 473 additions and 46 deletions.
Binary file modified BrawlboxHelper/BrawlboxHelper.dll
Binary file not shown.
43 changes: 38 additions & 5 deletions File_Format_Library/FileFormats/Archives/U8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Windows.Forms;
using Toolbox.Library;
using Toolbox.Library.IO;
using System.IO;

namespace FirstPlugin
{
Expand All @@ -16,7 +17,7 @@ public class U8 : IArchiveFile, IFileFormat

public bool CanSave { get; set; }
public string[] Description { get; set; } = new string[] { "U8" };
public string[] Extension { get; set; } = new string[] { "*.u8"};
public string[] Extension { get; set; } = new string[] { "*.u8", "*.arc", "*.cmparc"};
public string FileName { get; set; }
public string FilePath { get; set; }
public IFileInfo IFileInfo { get; set; }
Expand All @@ -28,14 +29,30 @@ public class U8 : IArchiveFile, IFileFormat

private readonly uint BEMagic = 0x55AA382D;
private readonly uint LEMagic = 0x2D38AA55;
private int LZType = 0x0, LZSize = 0;

public bool Identify(System.IO.Stream stream)
public bool Identify(Stream stream)
{
using (var reader = new Toolbox.Library.IO.FileReader(stream, true))
using (var reader = new FileReader(stream, true))
{
reader.ByteOrder = Syroot.BinaryData.ByteOrder.BigEndian;
byte LZCheck = reader.ReadByte();
if (LZCheck == 0x11 || LZCheck == 0x10)
{
LZType = LZCheck;

// For the Wii's U8 ARC files compressed with LZ77 Type 10 or Type 11, the decompiled file size is written in little endian.
reader.ByteOrder = Syroot.BinaryData.ByteOrder.LittleEndian;
LZSize = reader.ReadInt32();
reader.ByteOrder = Syroot.BinaryData.ByteOrder.BigEndian;
}
else
{
reader.Position = 0;
}

uint signature = reader.ReadUInt32();

reader.Position = 0;
return signature == BEMagic || signature == LEMagic;
}
Expand Down Expand Up @@ -65,9 +82,25 @@ public string Name

private bool IsBigEndian = false;

public void Load(System.IO.Stream stream)
public void Load(Stream stream)
{
using (var reader = new FileReader(stream))
SubStream sub;
if (LZType == 0x11 || LZType == 0x10)
{
sub = new SubStream(stream, 4);
}
else
{
sub = null;
}

Stream dataStream =
LZType == 0x11 ? new MemoryStream(LZ77_WII.Decompress11(sub.ToArray(), LZSize)) :
LZType == 0x10 ? new MemoryStream(LZ77_WII.Decompress10LZ(sub.ToArray(), LZSize)) : stream;

dataStream.Position = 0;

using (var reader = new FileReader(dataStream))
{
reader.ByteOrder = Syroot.BinaryData.ByteOrder.BigEndian;

Expand Down
86 changes: 51 additions & 35 deletions File_Format_Library/FileFormats/Font/BXFNT/BXFNT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class BXFNT : IFileFormat, IEditor<BffntEditor>, IConvertableTextFormat
public FileType FileType { get; set; } = FileType.Font;

public bool CanSave { get; set; }
public string[] Description { get; set; } = new string[] { "Cafe Font", "CTR Font", "Revolution Font" };
public string[] Extension { get; set; } = new string[] { "*.bffnt", "*.bcfnt", "*.brfnt", };
public string[] Description { get; set; } = new string[] { "Cafe Font", "CTR Font", "Revolution Font", "Revolution Archived Font" };
public string[] Extension { get; set; } = new string[] { "*.bffnt", "*.bcfnt", "*.brfnt", "*.brfna" };
public string FileName { get; set; }
public string FilePath { get; set; }
public IFileInfo IFileInfo { get; set; }
Expand All @@ -33,7 +33,8 @@ public bool Identify(Stream stream)
return reader.CheckSignature(4, "FFNT") ||
reader.CheckSignature(4, "CFNT") ||
reader.CheckSignature(4, "RFNT") ||
reader.CheckSignature(4, "TNFR");
reader.CheckSignature(4, "TNFR") ||
reader.CheckSignature(4, "RFNA");
}
}

Expand Down Expand Up @@ -101,7 +102,8 @@ public void Load(System.IO.Stream stream)
}
else if (bffnt.Platform == FFNT.PlatformType.Cafe)
{
for (int s = 0; s < tglp.SheetDataList.Count; s++) {
for (int s = 0; s < tglp.SheetDataList.Count; s++)
{
var surface = new Gx2ImageBlock();
surface.Text = $"Sheet_{s}";
surface.Load(tglp, s);
Expand Down Expand Up @@ -206,6 +208,7 @@ public class FFNT
public ushort HeaderSize;
public uint Version { get; set; }

public GLGR GlyphGroup { get; set; }
public FINF FontSection { get; set; }
public FontKerningTable KerningTable { get; set; }

Expand All @@ -226,7 +229,7 @@ public void Read(FileReader reader)
reader.ByteOrder = Syroot.BinaryData.ByteOrder.BigEndian;

Signature = reader.ReadString(4, Encoding.ASCII);
if (Signature != "FFNT" && Signature != "CFNT" && Signature != "RFNT" && Signature != "TNFR")
if (Signature != "FFNT" && Signature != "CFNT" && Signature != "RFNT" && Signature != "TNFR" && Signature != "RFNA")
throw new Exception($"Invalid signature {Signature}! Expected FFNT or CFNT or RFNT.");

BOM = reader.ReadUInt16();
Expand All @@ -237,7 +240,8 @@ public void Read(FileReader reader)

//Parse header first and check the version
//Brfnt uses a slightly different header structure
if (Signature == "RFNT" || Signature == "TNFR") {
if (Signature == "RFNT" || Signature == "TNFR" || Signature == "RFNA")
{
Version = reader.ReadUInt16();
uint FileSize = reader.ReadUInt32();
HeaderSize = reader.ReadUInt16();
Expand Down Expand Up @@ -265,13 +269,24 @@ public void Read(FileReader reader)

if (Signature == "CFNT")
Platform = PlatformType.Ctr;
if (Signature == "RFNT" || Signature == "TNFR")
if (Signature == "RFNT" || Signature == "TNFR" || Signature == "RFNA")
Platform = PlatformType.Wii;

Console.WriteLine($"Platform {Platform}");

reader.Seek(HeaderSize, SeekOrigin.Begin);
if (Signature == "RFNA")
{
GlyphGroup = new GLGR();
GlyphGroup.Read(reader);
// It's needed to take off 22 because of the included header length in SectionSize.
reader.Seek(GlyphGroup.SectionSize - 0x16, SeekOrigin.Current);
}
FontSection = new FINF();
if (GlyphGroup != null)
{
FontSection.GlyphGroup = GlyphGroup;
}
FontSection.Read(reader, this);

//Check for any unread blocks
Expand Down Expand Up @@ -347,7 +362,8 @@ public void Write(FileWriter writer)

writer.SeekBegin(HeaderSize);
FontSection.Write(writer, this);
if (KerningTable != null) {
if (KerningTable != null)
{
BlockCounter++;
KerningTable.Write(writer, this);
}
Expand Down Expand Up @@ -392,33 +408,33 @@ public Bitmap GetBitmap(string text, bool reversewh, LayoutBXLYT.BasePane pane)
float YScale = (fontHeight / TextureGlyph.CellWidth);
float height = (TextureGlyph.SheetHeight - 2) / TextureGlyph.ColumnCount;

/* int pos = 0;
for (int i = 0; i < text.Length; i++)
{
char character = text[i];
int charWidth = (int)FontInfo.DefaultCharWidth;
int glyphWidth = (int)FontInfo.DefaultGlyphWidth;
int leftWidth = (int)FontInfo.DefaultLeftWidth;
if (FontInfo.CodeMapDictionary.ContainsKey(character))
{
var idx = FontInfo.CodeMapDictionary[character];
if (idx == 0xFFFF) continue;
var charWidthInfo = GetCharWidthInfoByIndex(FontInfo, (ushort)idx);
charWidth = charWidthInfo.CharWidth;
glyphWidth = charWidthInfo.GlyphWidth;
leftWidth = charWidthInfo.Left;
}
/* Bitmap b = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(b))
{
g.DrawImage();
}
}*/
/* int pos = 0;
for (int i = 0; i < text.Length; i++)
{
char character = text[i];
int charWidth = (int)FontInfo.DefaultCharWidth;
int glyphWidth = (int)FontInfo.DefaultGlyphWidth;
int leftWidth = (int)FontInfo.DefaultLeftWidth;
if (FontInfo.CodeMapDictionary.ContainsKey(character))
{
var idx = FontInfo.CodeMapDictionary[character];
if (idx == 0xFFFF) continue;
var charWidthInfo = GetCharWidthInfoByIndex(FontInfo, (ushort)idx);
charWidth = charWidthInfo.CharWidth;
glyphWidth = charWidthInfo.GlyphWidth;
leftWidth = charWidthInfo.Left;
}
/* Bitmap b = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(b))
{
g.DrawImage();
}
}*/

if (bitmapFont == null)
bitmapFont = GetBitmapFont(true);
Expand Down
1 change: 1 addition & 0 deletions File_Format_Library/FileFormats/Font/BXFNT/FINF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class FINF
public byte DefaultGlyphWidth { get; set; }
public byte DefaultCharWidth { get; set; }
public CharacterCode CharEncoding { get; set; }
public GLGR GlyphGroup;
public TGLP TextureGlyph;
public CMAP CodeMap;
public CWDH CharacterWidth;
Expand Down
37 changes: 37 additions & 0 deletions File_Format_Library/FileFormats/Font/BXFNT/GLGR.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Toolbox.Library.IO;

namespace FirstPlugin
{
public class GLGR
{
public uint SectionSize;
public uint SheetSize;
public uint HeaderSize = 0x20;
public ushort GlyphsPerSheet;
public ushort SetCount;
public ushort SheetCount;
public ushort CWDHCount;
public ushort CMAPCount;

public void Read(FileReader reader)
{
string Signature = reader.ReadString(4, Encoding.ASCII);
if (Signature != "GLGR")
{
throw new Exception($"Invalid signature {Signature}! Expected GLGR.");
}
SectionSize = reader.ReadUInt32();
SheetSize = reader.ReadUInt32();
GlyphsPerSheet = reader.ReadUInt16();
SetCount = reader.ReadUInt16();
SheetCount = reader.ReadUInt16();
CWDHCount = reader.ReadUInt16();
CMAPCount = reader.ReadUInt16();
}
}
}
28 changes: 24 additions & 4 deletions File_Format_Library/FileFormats/Font/BXFNT/TGLP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Bitmap GetBitmap(int index)
public byte CellWidth { get; set; }
public byte CellHeight { get; set; }
public byte MaxCharWidth { get; set; }
public byte SheetCount { get; private set; }
public ushort SheetCount { get; private set; }
public uint SheetSize { get; set; }
public ushort BaseLinePos { get; set; }
public ushort Format { get; set; }
Expand All @@ -53,17 +53,26 @@ public void Read(FileReader reader, FFNT header)
BaseLinePos = reader.ReadByte();
MaxCharWidth = reader.ReadByte();
SheetSize = reader.ReadUInt32();
SheetCount = (byte)reader.ReadUInt16();
SheetCount = reader.ReadUInt16();
if (header.Signature == "RFNA")
{
reader.ReadByte(); // No clue what the value is for
Format = reader.ReadByte();
}
else
{
Format = reader.ReadUInt16();
}
}
else
{
SheetCount = reader.ReadByte();
MaxCharWidth = reader.ReadByte();
SheetSize = reader.ReadUInt32();
BaseLinePos = reader.ReadUInt16();
Format = reader.ReadUInt16();
}

Format = reader.ReadUInt16();
RowCount = reader.ReadUInt16();
ColumnCount = reader.ReadUInt16();
SheetWidth = reader.ReadUInt16();
Expand All @@ -74,7 +83,18 @@ public void Read(FileReader reader, FFNT header)
{
for (int i = 0; i < SheetCount; i++)
{
SheetDataList.Add(reader.ReadBytes((int)SheetSize));
byte[] decompedData;
uint compSheetSize = 0;
if (header.Signature == "RFNA") // .brfna files have their texture sheets compressed with Huffman.
{
compSheetSize = reader.ReadUInt32();
decompedData = Huffman_WII.DecompressHuffman(reader.ReadBytes((int)compSheetSize), (int)SheetSize);
}
else
{
decompedData = reader.ReadBytes((int)SheetSize);
}
SheetDataList.Add(decompedData);
if (SheetDataList[i].Length != SheetSize)
throw new Exception("SheetSize mis match!");
}
Expand Down
4 changes: 2 additions & 2 deletions File_Format_Library/FileFormats/NLG/NLG_NLOC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class NLG_NLOC : IEditor<TextEditor>, IFileFormat, IContextMenuNode, ICon

public bool CanSave { get; set; }
public string[] Description { get; set; } = new string[] { "NLG Localization Text" };
public string[] Extension { get; set; } = new string[] { "*.nloc" };
public string[] Extension { get; set; } = new string[] { "*.nloc", "*.loc" };
public string FileName { get; set; }
public string FilePath { get; set; }
public IFileInfo IFileInfo { get; set; }
Expand Down Expand Up @@ -208,4 +208,4 @@ public class TextEntry
public uint ID;
}
}
}
}
1 change: 1 addition & 0 deletions File_Format_Library/File_Format_Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@
<Compile Include="FileFormats\Font\BXFNT\CMAP.cs" />
<Compile Include="FileFormats\Font\BXFNT\FINF.cs" />
<Compile Include="FileFormats\Font\BXFNT\FontKerningTable.cs" />
<Compile Include="FileFormats\Font\BXFNT\GLGR.cs" />
<Compile Include="FileFormats\Font\BXFNT\Images\CtrImageBlock.cs" />
<Compile Include="FileFormats\Font\BXFNT\CWDH.cs" />
<Compile Include="FileFormats\Font\BXFNT\Images\Gx2ImageBlock.cs" />
Expand Down
Loading

0 comments on commit 2fa935c

Please sign in to comment.