Skip to content

Commit

Permalink
Add basics of Ctd format
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeeynamo committed Feb 28, 2020
1 parent b4b1167 commit da9862b
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
81 changes: 81 additions & 0 deletions OpenKh.Bbs/Ctd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Xe.BinaryMapper;

namespace OpenKh.Bbs
{
public class Ctd
{
private const int MagicCode = 0x44544340;

private class Header
{
[Data] public int MagicCode { get; set; }
[Data] public int Version { get; set; }
[Data] public short Unknown08 { get; set; }
[Data] public short Unknown0a { get; set; }
[Data] public short Entry2Count { get; set; }
[Data] public short Entry1Count { get; set; }
[Data] public int Entry1Offset { get; set; }
[Data] public int Entry2Offset { get; set; }
[Data] public int TextOffset { get; set; }
[Data] public int Unknown1c { get; set; }
}

public class Entry
{
public short Unknown00 { get; set; }
public short Unknown02 { get; set; }
public int Unknown04 { get; set; }
public int Unknown08 { get; set; }
}

public class Entry2
{
[Data] public ushort textX { get; set; }
[Data] public ushort textY { get; set; }
[Data] public ushort winW { get; set; }
[Data] public ushort winH { get; set; }
[Data] public byte formatType1 { get; set; }
[Data] public byte dialogType { get; set; }
[Data] public byte formatType2 { get; set; }
[Data] public byte unk1 { get; set; }
[Data] public ushort fontSize { get; set; }
[Data] public ushort unk2 { get; set; }
[Data] public ushort fontSeparation { get; set; } // NOT TESTED
[Data] public ushort unk3 { get; set; }
[Data] public ushort unk4 { get; set; }
[Data] public ushort unk5 { get; set; }
[Data] public ushort unk6 { get; set; }
[Data] public ushort color { get; set; }
[Data] public ushort unk7 { get; set; }
[Data] public ushort unk8 { get; set; }
}

private readonly Header _header;

public List<Entry> Entries1 { get; set; }
public List<Entry2> Entries2 { get; set; }

private Ctd(Stream stream)
{
_header = BinaryMapping.ReadObject<Header>(stream);

stream.Position = _header.Entry1Offset;
Entries1 = Enumerable.Range(0, _header.Entry1Count)
.Select(x => BinaryMapping.ReadObject<Entry>(stream))
.ToList();

stream.Position = _header.Entry2Offset;
Entries2 = Enumerable.Range(0, _header.Entry2Count)
.Select(x => BinaryMapping.ReadObject<Entry2>(stream))
.ToList();
}

public static Ctd Read(Stream stream) => new Ctd(stream);

public static bool IsValid(Stream stream) =>
new BinaryReader(stream).ReadInt32() == MagicCode;
}
}
53 changes: 53 additions & 0 deletions OpenKh.Tests/Bbs/CtdTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using OpenKh.Bbs;
using System.IO;
using Xunit;

namespace OpenKh.Tests.Bbs
{
public class CtdTests : Common
{
private static readonly string FileName = "Bbs/res/ctdtest.ctd";

[Fact]
public void IsValidTest()
{
using (var stream = new MemoryStream())
{
stream.WriteByte(0x40);
stream.WriteByte(0x43);
stream.WriteByte(0x54);
stream.WriteByte(0x44);
stream.Position = 0;
Assert.True(Ctd.IsValid(stream));
}
}

[Fact]
public void IsNotValidTest()
{
using (var stream = new MemoryStream())
{
stream.WriteByte(1);
stream.WriteByte(2);
stream.WriteByte(3);
stream.WriteByte(4);
stream.Position = 0;
Assert.False(Ctd.IsValid(stream));
}
}

[Fact]
public void ReadCorrectAmountOfEntry1() => FileOpenRead(FileName, stream =>
{
var ctd = Ctd.Read(stream);
Assert.Equal(41, ctd.Entries1.Count);
});

[Fact]
public void ReadCorrectAmountOfEntry2() => FileOpenRead(FileName, stream =>
{
var ctd = Ctd.Read(stream);
Assert.Equal(14, ctd.Entries2.Count);
});
}
}
Binary file added OpenKh.Tests/Bbs/res/ctdtest.ctd
Binary file not shown.
3 changes: 3 additions & 0 deletions OpenKh.Tests/OpenKh.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<None Update="Imaging\res\image-8bit-512-272.tm2">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Bbs\res\ctdtest.ctd">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="kh2\res\00objentry.bin">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down

0 comments on commit da9862b

Please sign in to comment.