Skip to content

Commit

Permalink
Impl more TotK tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ArchLeaders committed Mar 26, 2024
1 parent 7279edf commit 326cecf
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/Models/MessageStudio/BinaryText/ModuleSystemTagResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,39 @@ public class ModuleSystemTagResolver : ITagResolver
public string? GetName(ushort group, ushort type)
{
return (group, type) switch {
(0, 0) => RubyTag.NAME,
(0, 2) => FontSizeTag.NAME,
(0, 3) => FontColorTag.NAME,
(0, 4) => PageBreakTag.NAME,
(2, 1) => FlagTag.STRING_NAME,
(2, 2) => FlagTag.INT_NAME,
(2, 9) => FlagTag.FLOAT_NAME,
_ => null
};
}

public (ushort, ushort)? GetGroupAndType(ReadOnlySpan<char> name)
{
return name switch {
RubyTag.NAME => (0, 0),
FontSizeTag.NAME => (0, 2),
FontColorTag.NAME => (0, 3),
PageBreakTag.NAME => (0, 4),
FlagTag.STRING_NAME => (2, 1),
FlagTag.INT_NAME => (2, 2),
FlagTag.FLOAT_NAME => (2, 9),
_ => null
};
}

public bool WriteBinaryUtf16(RevrsWriter writer, ushort group, ushort type, in TagParams @params)
{
return (group, type) switch {
(0, 0) => RubyTag.WriteUtf16(writer, @params),
(0, 2) => FontSizeTag.WriteUtf16(writer, @params),
(0, 3) => FontColorTag.WriteUtf16(writer, @params),
(0, 4) => PageBreakTag.WriteUtf16(writer),
(2, 1) or (2, 2) or (2, 9) => FlagTag.WriteUtf16(writer, @params),
_ => false
};
}
Expand All @@ -45,7 +61,11 @@ public bool WriteBinaryUtf8(RevrsWriter writer, ushort group, ushort type, in Ta
public bool WriteText(StringBuilder sb, ushort group, ushort type, Span<byte> data)
{
return (group, type) switch {
(0, 0) => RubyTag.WriteText(sb, data),
(0, 2) => FontSizeTag.WriteText(sb, data),
(0, 3) => FontColorTag.WriteText(sb, data),
(0, 4) => true,
(2, 1) or (2, 2) or (2, 9) => FlagTag.WriteText(sb, data),
_ => false
};
}
Expand Down
55 changes: 55 additions & 0 deletions src/Models/MessageStudio/BinaryText/Tags/FlagTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using MessageStudio.Formats.BinaryText.Components;
using MessageStudio.Formats.BinaryText.Extensions;
using Revrs;
using System.Runtime.InteropServices;
using System.Text;

namespace NxEditor.EpdPlugin.Models.MessageStudio.BinaryText.Tags;

public class FlagTag
{
public const string FLOAT_NAME = "IntFlag";
public const string INT_NAME = "FloatFlag";
public const string STRING_NAME = "StringFlag";

private const string NAME_PARAM = "Name";
private const string UNKNOWN_PARAM_1 = "Unknown1";
private const string UNKNOWN_PARAM_2 = "Unknown2";

public static bool WriteUtf16(RevrsWriter writer, in TagParams @params)
{
ReadOnlySpan<char> name = @params.Get<string>(NAME_PARAM);
ReadOnlySpan<byte> nameRawBytes = MemoryMarshal.Cast<char, byte>(name);

writer.Write((ushort)(
sizeof(ushort) + nameRawBytes.Length +
sizeof(ushort) + sizeof(ushort)));

writer.Write((ushort)nameRawBytes.Length);
writer.Write(nameRawBytes);

writer.Write(@params.Get<ushort>(UNKNOWN_PARAM_2));
writer.Write(@params.Get<ushort>(UNKNOWN_PARAM_1));
return true;
}

public static bool WriteText(StringBuilder sb, Span<byte> data)
{
RevrsReader reader = RevrsReader.Native(data);

ushort rawNameLength = reader.Read<ushort>();

sb.OpenParam(NAME_PARAM);
sb.Append(MemoryMarshal.Cast<byte, char>(reader.ReadSpan<byte>(rawNameLength)));
sb.CloseParam();

sb.OpenParam(UNKNOWN_PARAM_1);
sb.Append(reader.Read<ushort>());
sb.CloseParam();

sb.OpenParam(UNKNOWN_PARAM_2);
sb.Append(reader.Read<ushort>());
sb.CloseParam();
return true;
}
}
4 changes: 2 additions & 2 deletions src/Models/MessageStudio/BinaryText/Tags/FontColorTag.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using MessageStudio.Formats.BinaryText.Components;
using Revrs.Extensions;
using MessageStudio.Formats.BinaryText.Extensions;
using Revrs;
using Revrs.Extensions;
using System.Text;
using MessageStudio.Formats.BinaryText.Extensions;

namespace NxEditor.EpdPlugin.Models.MessageStudio.BinaryText.Tags;

Expand Down
29 changes: 29 additions & 0 deletions src/Models/MessageStudio/BinaryText/Tags/FontSizeTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using MessageStudio.Formats.BinaryText.Components;
using MessageStudio.Formats.BinaryText.Extensions;
using Revrs;
using Revrs.Extensions;
using System.Text;

namespace NxEditor.EpdPlugin.Models.MessageStudio.BinaryText.Tags;

public class FontSizeTag
{
public const string NAME = "FontSize";

private const string FONT_SIZE_PARAM = "Scale";

public static bool WriteUtf16(RevrsWriter writer, in TagParams @params)
{
writer.Write<ushort>(sizeof(ushort));
writer.Write(@params.Get<ushort>(FONT_SIZE_PARAM));
return true;
}

public static bool WriteText(StringBuilder sb, Span<byte> data)
{
sb.OpenParam(FONT_SIZE_PARAM);
sb.Append(data.Read<ushort>());
sb.CloseParam();
return true;
}
}
14 changes: 14 additions & 0 deletions src/Models/MessageStudio/BinaryText/Tags/PageBreakTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Revrs;

namespace NxEditor.EpdPlugin.Models.MessageStudio.BinaryText.Tags;

public class PageBreakTag
{
public const string NAME = "Break";

public static bool WriteUtf16(RevrsWriter writer)
{
writer.Write<ushort>(0);
return true;
}
}
45 changes: 45 additions & 0 deletions src/Models/MessageStudio/BinaryText/Tags/RubyTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using MessageStudio.Formats.BinaryText.Components;
using MessageStudio.Formats.BinaryText.Extensions;
using Revrs;
using System.Runtime.InteropServices;
using System.Text;

namespace NxEditor.EpdPlugin.Models.MessageStudio.BinaryText.Tags;

public class RubyTag
{
public const string NAME = "Ruby";

private const string CHAR_SPAN_PARAM = "SpanChars";
private const string TEXT_PARAM = "Text";

public static bool WriteUtf16(RevrsWriter writer, in TagParams @params)
{
ReadOnlySpan<char> name = @params.Get<string>(TEXT_PARAM);
ReadOnlySpan<byte> nameRawBytes = MemoryMarshal.Cast<char, byte>(name);

writer.Write((ushort)(sizeof(ushort) + sizeof(ushort) + nameRawBytes.Length));
writer.Write(@params.Get<ushort>(CHAR_SPAN_PARAM));

writer.Write((ushort)nameRawBytes.Length);
writer.Write(nameRawBytes);

return true;
}

public static bool WriteText(StringBuilder sb, Span<byte> data)
{
RevrsReader reader = RevrsReader.Native(data);

sb.OpenParam(CHAR_SPAN_PARAM);
sb.Append(reader.Read<ushort>());
sb.CloseParam();

ushort rawNameLength = reader.Read<ushort>();
sb.OpenParam(TEXT_PARAM);
sb.Append(MemoryMarshal.Cast<byte, char>(reader.ReadSpan<byte>(rawNameLength)));
sb.CloseParam();

return true;
}
}

0 comments on commit 326cecf

Please sign in to comment.