Skip to content

Commit

Permalink
Improvements to msbt
Browse files Browse the repository at this point in the history
  • Loading branch information
KillzXGaming committed Nov 18, 2023
1 parent b57cf0d commit 15487c3
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 11 deletions.
76 changes: 67 additions & 9 deletions Fushigi.Msbt/MsbtFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public struct MsbtHeader
public byte Version; //3
public ushort NumSections;
public ushort Padding2;
public uint FileSize;

public ulong Padding3;
public uint Padding4;
Expand Down Expand Up @@ -84,8 +85,17 @@ public void Read(Stream stream)
}
}

public void Write(Stream stream)
public void Save(string filePath)
{
using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write)) {
Save(fs);
}
}

public void Save(Stream stream)
{
Header.NumSections = CalculateSectionCount();

var encoding = Header.Encoding == 0 ? Encoding.UTF8 : Encoding.Unicode;

var writer = new BinaryWriter(stream, encoding);
Expand All @@ -94,17 +104,31 @@ public void Write(Stream stream)
writer.BaseStream.Seek(32, SeekOrigin.Begin);
WriteSection(writer, "LBL1", () => WriteLabel(writer, Messages.Keys.ToArray()));
WriteSection(writer, "ATR1", () => WriteAttribute(writer));
WriteSection(writer, "TXT2", () => WriteText2(writer, Messages.Values.ToArray()));
WriteSection(writer, "TXT2", () => WriteText2(writer, Messages.Values.ToArray(), encoding));

//file size
writer.WriteSize(18, (uint)writer.BaseStream.Length);
}

private ushort CalculateSectionCount()
{
return 3;
}

private void WriteSection(BinaryWriter writer, string magic, Action sectionWriter)
{
writer.Write(Encoding.ASCII.GetBytes(magic));
writer.Write(0); //size for later
writer.Write(new byte[12]); //padding for alignment
writer.Write(new byte[8]); //padding for alignment

var pos = writer.BaseStream.Position;

sectionWriter.Invoke();

var endpos = writer.BaseStream.Position;

//Write size
writer.WriteSize(pos - 12, (uint)(endpos - pos));
writer.Align(16);
}

Expand Down Expand Up @@ -165,7 +189,8 @@ private string ReadMessageText(BinaryReader reader, Encoding encoding)
while (!isNullTerimated)
{
char c = reader.ReadChar();
sb.Append(c);
if (c != 0x00)
sb.Append(c);

switch ((int)c)
{
Expand All @@ -181,7 +206,8 @@ private string ReadMessageText(BinaryReader reader, Encoding encoding)
sb.Append((char)reader.ReadByte());
}
break;
case 0x0F: //tag end
case 0xF:
//end tag
sb.Append((char)reader.ReadInt16());
sb.Append((char)reader.ReadInt16());
break;
Expand Down Expand Up @@ -211,7 +237,7 @@ private void WriteAttribute(BinaryWriter writer)
writer.Write(AttributeData);
}

private void WriteText2(BinaryWriter writer, string[] text)
private void WriteText2(BinaryWriter writer, string[] text, Encoding encoding)
{
long startPosition = writer.BaseStream.Position;

Expand All @@ -222,11 +248,43 @@ private void WriteText2(BinaryWriter writer, string[] text)
for (int i = 0; i < text.Length; i++)
{
writer.WriteOffset(startPosition + 4 + i * 4, startPosition);
for (int j = 0; j < text[i].Length; j++)
writer.Write((char)text[i][j]);
writer.Write(GetMessageBytes(text[i], encoding));
}
}

writer.Write((char)0);
private byte[] GetMessageBytes(string str, Encoding encoding)
{
var mem = new MemoryStream();
using (var bw = new BinaryWriter(mem, encoding))
{
for (var i = 0; i < str.Length; i++)
{
var c = str[i];
bw.Write(c);
if (c == 0xE)
{
//tag code
bw.Write((short)str[++i]);
bw.Write((short)str[++i]);

//tag parameters
int num_params = str[++i];
bw.Write((short)num_params);
for (var j = 0; j < num_params; j++)
{
bw.Write((byte)str[++i]);
}
}
if (c == 0xF)
{
//end tag
bw.Write((short)str[++i]);
bw.Write((short)str[++i]);
}
}
bw.Write('\0'); //null terminator
}
return mem.ToArray();
}
}
}
13 changes: 11 additions & 2 deletions Fushigi.Msbt/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public static void Align(this BinaryReader reader, uint amount)

public static void Align(this BinaryWriter writer, uint amount)
{
while (writer.BaseStream.Position % amount != 0 && writer.BaseStream.Position != writer.BaseStream.Length)
writer.Write((byte)0);
writer.Write(new byte[(int)(-writer.BaseStream.Position % amount + amount) % amount]);
}

public static void WriteOffset(this BinaryWriter writer, long pos, long startPosition)
Expand All @@ -38,5 +37,15 @@ public static void WriteOffset(this BinaryWriter writer, long pos, long startPos

writer.BaseStream.Seek(base_pos, SeekOrigin.Begin);
}

public static void WriteSize(this BinaryWriter writer, long pos, uint size)
{
var base_pos = writer.BaseStream.Position;

writer.BaseStream.Seek(pos, SeekOrigin.Begin);
writer.Write(size);

writer.BaseStream.Seek(base_pos, SeekOrigin.Begin);
}
}
}

0 comments on commit 15487c3

Please sign in to comment.