Skip to content

Commit

Permalink
Rename CTD classes according to the documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeeynamo committed Feb 28, 2020
1 parent 1272899 commit 7c8e47b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 49 deletions.
73 changes: 37 additions & 36 deletions OpenKh.Bbs/Ctd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,30 @@ private class Header
[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 short LayoutCount { get; set; }
[Data] public short MessageCount { get; set; }
[Data] public int MessageOffset { get; set; }
[Data] public int LayoutOffset { get; set; }
[Data] public int TextOffset { get; set; }
[Data] public int Unknown1c { get; set; }
}

private class Entry
private class _Message
{
[Data] public short Id { get; set; }
[Data] public short Unknown02 { get; set; }
[Data] public int Offset { get; set; }
[Data] public int Entry2Index { get; set; }
}

public class FakeEntry
public class Message
{
[Data] public short Id { get; set; }
[Data] public short Unknown02 { get; set; }
[Data] public int Entry2Index { get; set; }
public short Id { get; set; }
public short Unknown02 { get; set; }
public int Entry2Index { get; set; }

public byte[] Data { get; set; }

public string Text
{
get => CtdEncoders.International.Decode(Data);
Expand All @@ -55,7 +56,7 @@ public override string ToString() =>
$"{Id:X04} {Unknown02:X04} {Entry2Index:X08}: {Text}";
}

public class Entry2
public class Layout
{
[Data] public ushort textX { get; set; }
[Data] public ushort textY { get; set; }
Expand All @@ -78,12 +79,12 @@ public class Entry2
}

public short Unknown { get; set; }
public List<FakeEntry> Entries1 { get; set; }
public List<Entry2> Entries2 { get; set; }
public List<Message> Messages { get; set; }
public List<Layout> Layouts { get; set; }

public string GetString(int id)
{
var entry = Entries1.FirstOrDefault(x => x.Id == id);
var entry = Messages.FirstOrDefault(x => x.Id == id);
if (entry == null)
return null;

Expand All @@ -92,33 +93,33 @@ public string GetString(int id)

public void Write(Stream stream)
{
var entry1Offset = HeaderLength;
var entry2Offset = Helpers.Align(entry1Offset + Entries1.Count * Entry1Length, 16);
var textOffset = entry2Offset + Entries2.Count * Entry2Length;
var messageOffset = HeaderLength;
var layoutOffset = Helpers.Align(messageOffset + Messages.Count * Entry1Length, 16);
var textOffset = layoutOffset + Layouts.Count * Entry2Length;

BinaryMapping.WriteObject(stream, new Header
{
MagicCode = MagicCode,
Version = Version,
Unknown08 = 0,
Unknown0a = Unknown,
Entry2Count = (short)Entries2.Count,
Entry1Count = (short)Entries1.Count,
Entry1Offset = entry1Offset,
Entry2Offset = entry2Offset,
LayoutCount = (short)Layouts.Count,
MessageCount = (short)Messages.Count,
MessageOffset = messageOffset,
LayoutOffset = layoutOffset,
TextOffset = textOffset,
Unknown1c = 0,
});

stream.Position = entry1Offset;
stream.Position = messageOffset;
var textStream = new MemoryStream(4096);
var nextTextOffset = textOffset;
foreach (var item in Entries1)
foreach (var item in Messages)
{
textStream.Write(item.Data, 0, item.Data.Length);
textStream.WriteByte(0);

BinaryMapping.WriteObject(stream, new Entry
BinaryMapping.WriteObject(stream, new _Message
{
Id = item.Id,
Unknown02 = item.Unknown02,
Expand All @@ -129,12 +130,12 @@ public void Write(Stream stream)
nextTextOffset += item.Data.Length + 1;
}

stream.Position = entry2Offset;
foreach (var item in Entries2)
stream.Position = layoutOffset;
foreach (var item in Layouts)
BinaryMapping.WriteObject(stream, item);

stream.Position = textOffset;
foreach (var entry in Entries1)
foreach (var entry in Messages)
{
stream.Write(entry.Data, 0, entry.Data.Length);
stream.WriteByte(0);
Expand All @@ -144,30 +145,30 @@ public void Write(Stream stream)
public Ctd()
{
Unknown = 0;
Entries1 = new List<FakeEntry>();
Entries2 = new List<Entry2>();
Messages = new List<Message>();
Layouts = new List<Layout>();
}

private Ctd(Stream stream)
{
var header = BinaryMapping.ReadObject<Header>(stream);
Unknown = header.Unknown0a;

stream.Position = header.Entry1Offset;
var textEntries = Enumerable.Range(0, header.Entry1Count)
.Select(x => BinaryMapping.ReadObject<Entry>(stream))
stream.Position = header.MessageOffset;
var textEntries = Enumerable.Range(0, header.MessageCount)
.Select(x => BinaryMapping.ReadObject<_Message>(stream))
.ToList();

stream.Position = header.Entry2Offset;
Entries2 = Enumerable.Range(0, header.Entry2Count)
.Select(x => BinaryMapping.ReadObject<Entry2>(stream))
stream.Position = header.LayoutOffset;
Layouts = Enumerable.Range(0, header.LayoutCount)
.Select(x => BinaryMapping.ReadObject<Layout>(stream))
.ToList();

Entries1 = textEntries
Messages = textEntries
.Select(x =>
{
stream.SetPosition(x.Offset);
return new FakeEntry
return new Message
{
Id = x.Id,
Unknown02 = x.Unknown02,
Expand Down
12 changes: 6 additions & 6 deletions OpenKh.Tests/Bbs/CtdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public void IsNotValidTest()
public void ReadCorrectAmountOfEntry1() => FileOpenRead(FileName, stream =>
{
var ctd = Ctd.Read(stream);
Assert.Equal(41, ctd.Entries1.Count);
Assert.Equal(41, ctd.Messages.Count);
});

[Fact]
public void ReadCorrectAmountOfEntry2() => FileOpenRead(FileName, stream =>
{
var ctd = Ctd.Read(stream);
Assert.Equal(14, ctd.Entries2.Count);
Assert.Equal(14, ctd.Layouts.Count);
});

[Theory]
Expand Down Expand Up @@ -81,10 +81,10 @@ public void WritesBackCorrectly() => FileOpenRead(FileName, stream =>
public void CreateEmptyCtdWithoutNullValues()
{
var ctd = new Ctd();
Assert.NotNull(ctd.Entries1);
Assert.NotNull(ctd.Entries2);
Assert.Empty(ctd.Entries1);
Assert.Empty(ctd.Entries2);
Assert.NotNull(ctd.Messages);
Assert.NotNull(ctd.Layouts);
Assert.Empty(ctd.Messages);
Assert.Empty(ctd.Layouts);
Assert.Equal(0, ctd.Unknown);
}
}
Expand Down
10 changes: 5 additions & 5 deletions OpenKh.Tools.CtdEditor/ViewModels/CtdViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public Ctd Ctd
{
get
{
_ctd.Entries1.Clear();
_ctd.Entries1.AddRange(Items.Select(x => x.Message));
_ctd.Messages.Clear();
_ctd.Messages.AddRange(Items.Select(x => x.Message));
return _ctd;
}
}
Expand All @@ -36,10 +36,10 @@ public CtdViewModel() :
{ }

public CtdViewModel(Ctd ctd) :
this(ctd, ctd.Entries1)
this(ctd, ctd.Messages)
{ }

private CtdViewModel(Ctd ctd, IEnumerable<Ctd.FakeEntry> messages) :
private CtdViewModel(Ctd ctd, IEnumerable<Ctd.Message> messages) :
base(messages.Select(x => new MessageViewModel(ctd, x)))
{
_ctd = ctd;
Expand All @@ -51,7 +51,7 @@ protected override void OnSelectedItem(MessageViewModel item)
}

protected override MessageViewModel OnNewItem() =>
new MessageViewModel(_ctd, new Ctd.FakeEntry
new MessageViewModel(_ctd, new Ctd.Message
{
Id = (short)(Items.Max(x => x.Message.Id) + 1),
Data = new byte[0],
Expand Down
4 changes: 2 additions & 2 deletions OpenKh.Tools.CtdEditor/ViewModels/MessageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ public class MessageViewModel : BaseNotifyPropertyChanged
{
private readonly Ctd _ctd;

public MessageViewModel(Ctd ctd, Ctd.FakeEntry message)
public MessageViewModel(Ctd ctd, Ctd.Message message)
{
_ctd = ctd;
Message = message;
}

public Ctd.FakeEntry Message { get; }
public Ctd.Message Message { get; }

public short Id
{
Expand Down

0 comments on commit 7c8e47b

Please sign in to comment.