PKHeX/PKHeX.Core/Saves/Substructures/Mail/Mail2.cs
Kurt 47071b41f3
Refactoring: Span-based value writes and method signatures (#3361)
Existing `get`/`set` logic is flawed in that it doesn't work on Big Endian operating systems, and it allocates heap objects when it doesn't need to.

`System.Buffers.Binary.BinaryPrimitives` in the `System.Memory` NuGet package provides both Little Endian and Big Endian methods to read and write data; all the `get`/`set` operations have been reworked to use this new API. This removes the need for PKHeX's manual `BigEndian` class, as all functions are already covered by the BinaryPrimitives API.

The `StringConverter` has now been rewritten to accept a Span to read from & write to, no longer requiring a temporary StringBuilder.

Other Fixes included:
- The Super Training UI for Gen6 has been reworked according to the latest block structure additions.
- Cloning a Stadium2 Save File now works correctly (opening from the Folder browser list).
- Checksum & Sanity properties removed from parent PKM class, and is now implemented via interface.
2022-01-02 21:35:59 -08:00

69 lines
No EOL
2.3 KiB
C#

using System;
namespace PKHeX.Core
{
public sealed class Mail2 : Mail
{
private readonly bool US;
public Mail2(SAV2 sav, int index) : base(sav.GetData(GetMailOffset(index), 0x2F), GetMailOffset(index))
{
US = !sav.Japanese && !sav.Korean;
}
private static int GetMailOffset(int index)
{
return index < 6 ? (index * 0x2F) + 0x600 : ((index - 6) * 0x2F) + 0x835;
}
public override string GetMessage(bool isLastLine) => US ? StringConverter12.GetString(Data.AsSpan(isLastLine ? 0x11 : 0, 0x10), false) : string.Empty;
public override void SetMessage(string line1, string line2)
{
if (US)
{
StringConverter12.SetString(Data.AsSpan(0x11, 0x10), line2.AsSpan(), 0x10, false, StringConverterOption.Clear50);
bool hasLine2 = Data[0x11] != StringConverter12.G1TerminatorCode;
var padChar = !hasLine2 ? StringConverterOption.Clear50 : StringConverterOption.Clear7F; // space
StringConverter12.SetString(Data.AsSpan(0, 0x10), line1.AsSpan(), 0x10, false, padChar);
Data[0x10] = 0x4E;
}
}
public override string AuthorName
{
get => US ? StringConverter12.GetString(Data.AsSpan(0x21, 8), false) : string.Empty;
set
{
if (US)
{
StringConverter12.SetString(Data.AsSpan(0x21, 8), value.AsSpan(), 7, false, StringConverterOption.Clear50);
Data[0x29] = Data[0x2A] = 0;
}
}
}
public override ushort AuthorTID
{
get => (ushort)(Data[0x2B] << 8 | Data[0x2C]);
set
{
Data[0x2B] = (byte)(value >> 8);
Data[0x2C] = (byte)(value & 0xFF);
}
}
public override int AppearPKM { get => Data[0x2D]; set => Data[0x2D] = (byte)value; }
public override int MailType { get => Data[0x2E]; set => Data[0x2E] = (byte)value; }
public override bool? IsEmpty => MailType switch
{
0 => true,
0x9E => false,
>= 0xB5 and <= 0xBD => false,
_ => null,
};
public override void SetBlank() => (new byte[0x2F]).CopyTo(Data, 0);
}
}