1
0
mirror of synced 2024-11-12 02:00:50 +01:00

BRFNT: Add support for little endian.

This commit is contained in:
KillzXGaming 2020-09-18 17:03:30 -04:00
parent 1d77d99968
commit 9a0f6ebddd
5 changed files with 36 additions and 11 deletions

View File

@ -32,7 +32,8 @@ namespace FirstPlugin
{
return reader.CheckSignature(4, "FFNT") ||
reader.CheckSignature(4, "CFNT") ||
reader.CheckSignature(4, "RFNT");
reader.CheckSignature(4, "RFNT") ||
reader.CheckSignature(4, "TNFR");
}
}
@ -210,6 +211,8 @@ namespace FirstPlugin
public PlatformType Platform { get; set; } = PlatformType.Cafe;
public bool IsWiiLE => Signature == "TNFR";
public enum PlatformType
{
Wii,
@ -223,15 +226,18 @@ namespace FirstPlugin
reader.ByteOrder = Syroot.BinaryData.ByteOrder.BigEndian;
Signature = reader.ReadString(4, Encoding.ASCII);
if (Signature != "FFNT" && Signature != "CFNT" && Signature != "RFNT")
if (Signature != "FFNT" && Signature != "CFNT" && Signature != "RFNT" && Signature != "TNFR")
throw new Exception($"Invalid signature {Signature}! Expected FFNT or CFNT or RFNT.");
BOM = reader.ReadUInt16();
reader.CheckByteOrderMark(BOM);
if (Signature == "TNFR")
reader.ReverseMagic = true;
//Parse header first and check the version
//Brfnt uses a slightly different header structure
if (Signature == "RFNT") {
if (Signature == "RFNT" || Signature == "TNFR") {
Version = reader.ReadUInt16();
uint FileSize = reader.ReadUInt32();
HeaderSize = reader.ReadUInt16();
@ -259,7 +265,7 @@ namespace FirstPlugin
if (Signature == "CFNT")
Platform = PlatformType.Ctr;
if (Signature == "RFNT")
if (Signature == "RFNT" || Signature == "TNFR")
Platform = PlatformType.Wii;
Console.WriteLine($"Platform {Platform}");
@ -274,7 +280,7 @@ namespace FirstPlugin
{
long BlockStart = reader.Position;
string BlockSignature = reader.ReadString(4, Encoding.ASCII);
string BlockSignature = reader.ReadSignature(4);
uint BlockSize = reader.ReadUInt32();
switch (BlockSignature)
@ -314,6 +320,9 @@ namespace FirstPlugin
writer.Write(BOM);
writer.CheckByteOrderMark(BOM);
if (Signature == "TNFR")
writer.ReverseMagic = true;
long _ofsFileSize;
long _ofsBlockNum;
if (Platform == PlatformType.Wii)

View File

@ -50,7 +50,7 @@ namespace FirstPlugin
CharacterWidths = new List<CWDH>();
CodeMaps = new List<CMAP>();
string Signature = reader.ReadString(4, Encoding.ASCII);
string Signature = reader.ReadSignature(4);
if (Signature != "FINF")
throw new Exception($"Invalid signature {Signature}! Expected FINF.");
Size = reader.ReadUInt32();
@ -94,7 +94,6 @@ namespace FirstPlugin
}
else
{
Type = reader.ReadEnum<FontType>(true);
Height = reader.ReadByte();
Width = reader.ReadByte();

View File

@ -44,9 +44,7 @@ namespace FirstPlugin
public void Read(FileReader reader, FFNT header)
{
string Signature = reader.ReadString(4, Encoding.ASCII);
if (Signature != "TGLP")
throw new Exception($"Invalid signature {Signature}! Expected TGLP.");
string Signature = reader.ReadSignature(4, "TGLP");
SectionSize = reader.ReadUInt32();
CellWidth = reader.ReadByte();
CellHeight = reader.ReadByte();

View File

@ -13,6 +13,8 @@ namespace Toolbox.Library.IO
{
public class FileReader : BinaryDataReader
{
public bool ReverseMagic { get; set; } = false;
public FileReader(Stream stream, bool leaveOpen = false)
: base(stream, Encoding.ASCII, leaveOpen)
{
@ -193,9 +195,20 @@ namespace Toolbox.Library.IO
ByteOrder = ByteOrder.LittleEndian;
}
public string ReadSignature(int length)
{
string RealSignature = ReadString(length, Encoding.ASCII);
if (ReverseMagic)
return new string(RealSignature.Reverse().ToArray());
else
return RealSignature;
}
public string ReadSignature(int length, string ExpectedSignature, bool TrimEnd = false)
{
string RealSignature = ReadString(length, Encoding.ASCII);
if (ReverseMagic)
RealSignature = new string(RealSignature.Reverse().ToArray());
if (TrimEnd) RealSignature = RealSignature.TrimEnd(' ');

View File

@ -1,11 +1,14 @@
using Syroot.BinaryData;
using System.IO;
using System.Text;
using System.Linq;
namespace Toolbox.Library.IO
{
public class FileWriter : BinaryDataWriter
{
public bool ReverseMagic { get; set; } = false;
public void CheckByteOrderMark(uint ByteOrderMark)
{
if (ByteOrderMark == 0xFEFF)
@ -85,7 +88,10 @@ namespace Toolbox.Library.IO
public void WriteSignature(string value)
{
Write(Encoding.ASCII.GetBytes(value));
if (ReverseMagic)
Write(Encoding.ASCII.GetBytes(new string(value.Reverse().ToArray())));
else
Write(Encoding.ASCII.GetBytes(value));
}
public void WriteString(string value, Encoding encoding = null)