Skip to content

Commit

Permalink
Fixed reading TPP .lng and .ffnt files
Browse files Browse the repository at this point in the history
  • Loading branch information
Atvaark committed Sep 10, 2015
1 parent c62793e commit 7c9d98f
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 67 deletions.
33 changes: 27 additions & 6 deletions FfntTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private static byte[] ReadFontLayer(string directory, string fileName, int layer
var pixel = bitmap.GetPixel(x, y);
if (pixel.R == 255 && pixel.G == 255 && pixel.B == 255)
{
result[y*bitmap.Height + x] = layerMask;
result[y*bitmap.Width + x] = layerMask;
}
}
}
Expand Down Expand Up @@ -141,10 +141,33 @@ private static void UnpackFfnt(string path, string fileName, string outputPath)
return;

SaveFont(ffntFile, fileName, outputPath);
SaveFontLayers(fontData.Data, fileName, outputPath);
Size size = CalculateSize(fontData.Data.Length);
SaveFontLayers(fontData.Data, size, fileName, outputPath);
}
}

private static Size CalculateSize(int area)
{
int height;
int width;
if (Math.Sqrt(area)%1 == 0) // Squared (e.g. the latin font)
{
height = width = (int) Math.Sqrt(area);
}
else if (area/2%2 == 0 && Math.Sqrt(area/2)%1 == 0) // Rectangle with width = 2*height (e.g. the kanji font)
{
height = (int) Math.Sqrt(area/2);
width = 2*height;
}
else
{
// TODO: Add width and height options to specify custom dimensions.
throw new Exception("Unknown bitmap font dimensions.");
}

return new Size(width, height);
}

private static void SaveFont(FfntFile ffntFile, string fileName, string outputPath)
{
XmlSerializer serializer = new XmlSerializer(typeof (FfntFile), new[] {typeof (GlyphMap), typeof (FontData)});
Expand All @@ -156,17 +179,15 @@ private static void SaveFont(FfntFile ffntFile, string fileName, string outputPa
}
}

private static void SaveFontLayers(byte[] ffntData, string fileName, string outputPath)
private static void SaveFontLayers(byte[] ffntData, Size size, string fileName, string outputPath)
{
const int maxLayers = 8;
int dimensions = (int) Math.Sqrt(ffntData.Length); // TODO: Fix dimensions for the arabic font (1024x512)

for (int i = 0; i < maxLayers; i++)
{
byte[] layer = GetLayer(ffntData, i);
if (layer != null)
{
using (Bitmap bitmap = new Bitmap(dimensions, dimensions, PixelFormat.Format8bppIndexed))
using (Bitmap bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format8bppIndexed))
{
BitmapData bitmapData = bitmap.LockBits(
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
Expand Down
4 changes: 2 additions & 2 deletions FfntTool/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("d9af2da5-85dd-45b2-8727-34be880d1558")]
[assembly: AssemblyVersion("0.1.3.0")]
[assembly: AssemblyFileVersion("0.1.3.0")]
[assembly: AssemblyVersion("0.2.0.0")]
[assembly: AssemblyFileVersion("0.2.0.0")]
11 changes: 11 additions & 0 deletions LangTool/ExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Linq;
using System.Text;

namespace LangTool
Expand All @@ -21,5 +22,15 @@ internal static void WriteNullTerminatedString(this BinaryWriter writer, string
byte[] data = Encoding.UTF8.GetBytes(text + '\0');
writer.Write(data, 0, data.Length);
}

internal static void AlignWrite(this BinaryWriter writer, int alignment, byte data)
{
long alignmentRequired = writer.BaseStream.Position % alignment;
if (alignmentRequired > 0)
{
byte[] alignmentBytes = Enumerable.Repeat(data, (int)(alignment - alignmentRequired)).ToArray();
writer.Write(alignmentBytes, 0, alignmentBytes.Length);
}
}
}
}
7 changes: 2 additions & 5 deletions LangTool/Lang/LangEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ namespace LangTool.Lang
public class LangEntry
{
[XmlAttribute]
public string Key { get; set; }
public uint Key { get; set; }

[XmlIgnore]
public int KeyOffset { get; set; }
public int Offset { get; set; }

[XmlAttribute]
public string Value { get; set; }

[XmlIgnore]
public int ValueOffset { get; set; }
}
}
81 changes: 36 additions & 45 deletions LangTool/Lang/LangFile.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

Expand Down Expand Up @@ -27,35 +29,36 @@ public void Read(Stream inputStream)
{
BinaryReader reader = new BinaryReader(inputStream, Encoding.UTF8, true);
int magicNumber = reader.ReadInt32();
int constant2 = reader.ReadInt32();
int endianess = reader.ReadInt32();
int version = reader.ReadInt32(); // GZ 2, TPP 3
int endianess = reader.ReadInt32(); // LE, BE
int entryCount = reader.ReadInt32();
int headerSize = reader.ReadInt32();
int offsetKeys = reader.ReadInt32();
int offsetValues = reader.ReadInt32();
int padding = reader.ReadInt32();
int valuesOffset = reader.ReadInt32();
int keysOffset = reader.ReadInt32();

inputStream.Position = valuesOffset;
Dictionary<int, LangEntry> offsetEntryDictionary = new Dictionary<int, LangEntry>();
for (int i = 0; i < entryCount; i++)
{
int offsetKey = reader.ReadInt32();
int offsetValue = reader.ReadInt32();

long nextEntryPosition = inputStream.Position;

inputStream.Position = offsetKeys + offsetKey;
string key = reader.ReadNullTerminatedString();
inputStream.Position = offsetValues + offsetValue;
int valuePosition = (int)inputStream.Position - valuesOffset;
short valueConstant = reader.ReadInt16();
Debug.Assert(valueConstant == 1);
string value = reader.ReadNullTerminatedString();

Entries.Add(new LangEntry
offsetEntryDictionary.Add(valuePosition, new LangEntry
{
Key = key,
Value = value
});
}

inputStream.Position = keysOffset;
for (int i = 0; i < entryCount; i++)
{
uint key = reader.ReadUInt32();
int offset = reader.ReadInt32();

inputStream.Position = nextEntryPosition;
offsetEntryDictionary[offset].Key = key;
}

Entries = offsetEntryDictionary.Values.ToList();
}

public void Write(Stream outputStream)
Expand All @@ -64,45 +67,33 @@ public void Write(Stream outputStream)

long headerPosition = outputStream.Position;

outputStream.Position += 32;
long entriesPosition = outputStream.Position;
outputStream.Position += 8*Entries.Count;
int keysPosition = (int) outputStream.Position;

outputStream.Position += 24;
int valuesPosition = (int) outputStream.Position;
foreach (var entry in Entries)
{
entry.KeyOffset = (int) (outputStream.Position - keysPosition);
writer.WriteNullTerminatedString(entry.Key);
entry.Offset = (int) outputStream.Position - valuesPosition;
writer.Write((short) 0x0001);
writer.WriteNullTerminatedString(entry.Value);
}

writer.AlignWrite(4, 0x00);

int valuesPosition = (int) outputStream.Position;

foreach (var entry in Entries)
int keysPosition = (int) outputStream.Position;
foreach (var entry in Entries.OrderBy(e => e.Key).ThenByDescending(e => e.Offset))
{
entry.ValueOffset = (int) (outputStream.Position - valuesPosition);
writer.Write((short) 1);
writer.WriteNullTerminatedString(entry.Value);
writer.Write(entry.Key);
writer.Write(entry.Offset);
}

long endPosition = outputStream.Position;

outputStream.Position = headerPosition;

writer.Write(1196310860);
writer.Write(2);
writer.Write(17740);
writer.Write(0x474e414c); // LANG
writer.Write(3);
writer.Write(0x0000454c); // LE
writer.Write(Entries.Count);
writer.Write(32);
writer.Write((int) (keysPosition - headerPosition));
writer.Write((int) (valuesPosition - headerPosition));
writer.Write(0);

foreach (var entry in Entries)
{
writer.Write(entry.KeyOffset);
writer.Write(entry.ValueOffset);
}
writer.Write(valuesPosition);
writer.Write(keysPosition);

outputStream.Position = endPosition;
}
Expand Down
9 changes: 4 additions & 5 deletions LangTool/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using LangTool.Lang;

Expand All @@ -25,7 +24,7 @@ private static void Main(string[] args)
return;
}
string extension = Path.GetExtension(path);
if (extension == ".xml")
if (string.Equals(extension, ".xml", StringComparison.OrdinalIgnoreCase))
{
using (FileStream inputStream = new FileStream(path, FileMode.Open))
using (StreamReader xmlReader = new StreamReader(inputStream, Encoding.UTF8))
Expand All @@ -41,7 +40,7 @@ private static void Main(string[] args)
file.Write(outputStream);
}
}
else if (Regex.Match(extension, @"^\.lng#\w{3}$", RegexOptions.IgnoreCase).Success)
else if (string.Equals(extension, ".lng", StringComparison.OrdinalIgnoreCase))
{
using (FileStream inputStream = new FileStream(path, FileMode.Open))
using (FileStream outputStream = new FileStream(path + ".xml", FileMode.Create))
Expand All @@ -65,8 +64,8 @@ private static void ShowUsageInfo()
"Usage:\n" +
" LangTool file_path.lng|file_path.xml\n" +
"Examples:\n" +
" LangTool gz_cassette.lng#eng - Converts the lng file to xml\n" +
" LangTool gz_cassette.lng#eng.xml - Converts the xml file to lng");
" LangTool gz_cassette.eng.lng - Converts the lng file to xml\n" +
" LangTool gz_cassette.eng.lng.xml - Converts the xml file to lng");
}
}
}
4 changes: 2 additions & 2 deletions LangTool/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("b2857b4d-a9d2-4c25-84b8-c9f9a3612852")]
[assembly: AssemblyVersion("0.1.3.0")]
[assembly: AssemblyFileVersion("0.1.3.0")]
[assembly: AssemblyVersion("0.2.0.0")]
[assembly: AssemblyFileVersion("0.2.0.0")]
4 changes: 2 additions & 2 deletions SubpTool/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("2ab72241-1321-4bae-ba53-b12120bfc344")]
[assembly: AssemblyVersion("0.1.3.0")]
[assembly: AssemblyFileVersion("0.1.3.0")]
[assembly: AssemblyVersion("0.2.0.0")]
[assembly: AssemblyFileVersion("0.2.0.0")]

0 comments on commit 7c9d98f

Please sign in to comment.