PKHeX/PKHeX.Core/Saves/Substructures/Mail/Mail2.cs

129 lines
3.9 KiB
C#
Raw Normal View History

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.Data.AsSpan(GetMailOffset(index), 0x2F).ToArray(), 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);
}
PKHeX.Core Nullable cleanup (#2401) * Handle some nullable cases Refactor MysteryGift into a second abstract class (backed by a byte array, or fake data) Make some classes have explicit constructors instead of { } initialization * Handle bits more obviously without null * Make SaveFile.BAK explicitly readonly again * merge constructor methods to have readonly fields * Inline some properties * More nullable handling * Rearrange box actions define straightforward classes to not have any null properties * Make extrabyte reference array immutable * Move tooltip creation to designer * Rearrange some logic to reduce nesting * Cache generated fonts * Split mystery gift album purpose * Handle more tooltips * Disallow null setters * Don't capture RNG object, only type enum * Unify learnset objects Now have readonly properties which are never null don't new() empty learnsets (>800 Learnset objects no longer created, total of 2400 objects since we also new() a move & level array) optimize g1/2 reader for early abort case * Access rewrite Initialize blocks in a separate object, and get via that object removes a couple hundred "might be null" warnings since blocks are now readonly getters some block references have been relocated, but interfaces should expose all that's needed put HoF6 controls in a groupbox, and disable * Readonly personal data * IVs non nullable for mystery gift * Explicitly initialize forced encounter moves * Make shadow objects readonly & non-null Put murkrow fix in binary data resource, instead of on startup * Assign dex form fetch on constructor Fixes legality parsing edge cases also handle cxd parse for valid; exit before exception is thrown in FrameGenerator * Remove unnecessary null checks * Keep empty value until init SetPouch sets the value to an actual one during load, but whatever * Readonly team lock data * Readonly locks Put locked encounters at bottom (favor unlocked) * Mail readonly data / offset Rearrange some call flow and pass defaults Add fake classes for SaveDataEditor mocking Always party size, no need to check twice in stat editor use a fake save file as initial data for savedata editor, and for gamedata (wow i found a usage) constrain eventwork editor to struct variable types (uint, int, etc), thus preventing null assignment errors
2019-10-17 01:47:31 +00:00
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();
}