-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters