PKHeX/PKHeX.Core/Saves/Substructures/Mail/Mail2.cs
Kurt 88830e0d00
Update from .NET Framework 4.6 to .NET 7 (#3729)
Updates from net46->net7, dropping support for mono in favor of using the latest runtime (along with the performance/API improvements). Releases will be posted as 64bit only for now.

Refactors a good amount of internal API methods to be more performant and more customizable for future updates & fixes.

Adds functionality for Batch Editor commands to `>`, `<` and <=/>=

TID/SID properties renamed to TID16/SID16 for clarity; other properties exposed for Gen7 / display variants.

Main window has a new layout to account for DPI scaling (8 point grid)

Fixed: Tatsugiri and Paldean Tauros now output Showdown form names as Showdown expects
Changed: Gen9 species now interact based on the confirmed National Dex IDs (closes #3724)
Fixed: Pokedex set all no longer clears species with unavailable non-base forms (closes #3720)
Changed: Hyper Training suggestions now apply for level 50 in SV. (closes #3714)
Fixed: B2/W2 hatched egg met locations exclusive to specific versions are now explicitly checked (closes #3691)
Added: Properties for ribbon/mark count (closes #3659)
Fixed: Traded SV eggs are now checked correctly (closes #3692)
2023-01-21 20:02:33 -08:00

128 lines
3.9 KiB
C#

using System;
using static System.Buffers.Binary.BinaryPrimitives;
namespace PKHeX.Core;
// warning: international only
public sealed class Mail2 : MailDetail
{
private readonly bool US;
private bool Japanese => !US;
private bool Korean => !US && !Japanese;
// structure:
private const int LINE_LENGTH = 0x10;
private const int MESSAGE_LENGTH = LINE_LENGTH + LINE_LENGTH + 1; // each line has a single char at end
private const int OFS_AUTHOR = MESSAGE_LENGTH;
private const int OFS_AUTHOR_NATION = OFS_AUTHOR + AUTHOR_LENGTH + 1;
private const int OFS_AUTHOR_ID = OFS_AUTHOR_NATION + 2;
private const int OFS_APPEAR = OFS_AUTHOR_ID + 2;
private const int OFS_TYPE = OFS_APPEAR + 1;
private const int SIZE = OFS_TYPE + 1;
private const int COUNT_PARTY = 6;
private const int COUNT_MAILBOX = 10;
private const int AUTHOR_LENGTH = 7;
public Mail2(SAV2 sav, int index) : base(sav.GetData(GetMailOffset(index), 0x2F), GetMailOffset(index))
{
US = sav is { Japanese: false, Korean: false };
}
private static int GetMailOffset(int index)
{
if (index < COUNT_PARTY)
return GetPartyMailOffset(index);
return GetMailboxMailOffset(index - COUNT_PARTY);
}
private static int GetPartyMailOffset(int index)
{
if ((uint)index >= COUNT_PARTY)
throw new ArgumentOutOfRangeException(nameof(index));
return (index * SIZE) + 0x600;
}
private static int GetMailboxMailOffset(int index)
{
if ((uint)index >= COUNT_MAILBOX)
throw new ArgumentOutOfRangeException(nameof(index));
return (index * SIZE) + 0x835;
}
private string GetString(Span<byte> asSpan)
{
if (Korean)
return StringConverter2KOR.GetString(asSpan);
return StringConverter12.GetString(asSpan, Japanese);
}
private void SetString(Span<byte> asSpan, ReadOnlySpan<char> value, int maxLength)
{
if (Korean)
StringConverter2KOR.SetString(asSpan, value, maxLength);
else
StringConverter12.SetString(asSpan, value, maxLength, Japanese);
}
public string Line1
{
get => GetString(Data.AsSpan(0, LINE_LENGTH - 1));
set
{
var span = Data.AsSpan(0, LINE_LENGTH);
SetString(span[..^1], value, LINE_LENGTH - 1);
span[^1] = 0x4E;
}
}
public string Line2
{
get => GetString(Data.AsSpan(LINE_LENGTH, LINE_LENGTH - 1));
set
{
var span = Data.AsSpan(LINE_LENGTH, LINE_LENGTH);
SetString(span[..^1], value, LINE_LENGTH - 1);
span[^1] = 0x4E;
}
}
public override string GetMessage(bool isLastLine) => isLastLine ? Line2 : Line1;
public override void SetMessage(string line1, string line2) => (Line1, Line2) = (line1, line2);
public override string AuthorName
{
get => GetString(Data.AsSpan(OFS_AUTHOR, AUTHOR_LENGTH + 1));
set
{
SetString(Data.AsSpan(OFS_AUTHOR, 8), value, AUTHOR_LENGTH);
Nationality = 0; // ??
}
}
public ushort Nationality
{
get => ReadUInt16BigEndian(Data.AsSpan(OFS_AUTHOR_NATION, 2));
set => WriteUInt16LittleEndian(Data.AsSpan(OFS_AUTHOR_NATION, 2), value);
}
public override ushort AuthorTID
{
get => ReadUInt16BigEndian(Data.AsSpan(OFS_AUTHOR_ID + 2));
set => WriteUInt16BigEndian(Data.AsSpan(OFS_AUTHOR_ID, 2), value);
}
public override ushort AppearPKM { get => Data[OFS_APPEAR]; set => Data[OFS_APPEAR] = (byte)value; }
public override int MailType { get => Data[OFS_TYPE]; set => Data[OFS_TYPE] = (byte)value; }
public override bool? IsEmpty => MailType switch
{
0 => true,
0x9E => false,
>= 0xB5 and <= 0xBD => false,
_ => null,
};
public override void SetBlank() => Data.AsSpan(0, SIZE).Clear();
}