2023-03-26 06:14:50 +00:00
|
|
|
using System;
|
2019-06-09 02:56:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
2024-03-04 05:13:16 +00:00
|
|
|
public sealed class MysteryBlock7(SAV7 sav, Memory<byte> raw) : SaveBlock<SAV7>(sav, raw), IMysteryGiftStorage, IMysteryGiftFlags
|
2019-06-09 02:56:11 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
private const int FlagStart = 0;
|
|
|
|
private const int MaxReceivedFlag = 2048;
|
|
|
|
private const int MaxCardsPresent = 48;
|
|
|
|
// private const int FlagRegionSize = (MaxReceivedFlag / 8); // 0x100
|
|
|
|
private const int CardStart = FlagStart + (MaxReceivedFlag / 8);
|
2024-03-04 05:13:16 +00:00
|
|
|
public void ClearReceivedFlags() => Data[..(MaxReceivedFlag / 8)].Clear();
|
2019-06-09 02:56:11 +00:00
|
|
|
|
2024-03-04 05:13:16 +00:00
|
|
|
private static int GetGiftOffset(int index)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2024-03-04 05:13:16 +00:00
|
|
|
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual((uint)index, (uint)MaxCardsPresent);
|
|
|
|
return CardStart + (index * WC7.Size);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-06-09 02:56:11 +00:00
|
|
|
|
2024-03-04 05:13:16 +00:00
|
|
|
private Span<byte> GetCardSpan(int index) => Data.Slice(GetGiftOffset(index), WC7.Size);
|
|
|
|
|
|
|
|
public WC7 GetMysteryGift(int index) => new(GetCardSpan(index).ToArray());
|
2019-06-09 02:56:11 +00:00
|
|
|
|
2024-03-04 05:13:16 +00:00
|
|
|
public void SetMysteryGift(int index, WC7 wc7)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if ((uint)index > MaxCardsPresent)
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(index));
|
2024-03-04 05:13:16 +00:00
|
|
|
if (wc7.Data.Length != WC7.Size)
|
|
|
|
throw new InvalidCastException(nameof(wc7));
|
2019-06-09 02:56:11 +00:00
|
|
|
|
2024-03-04 05:13:16 +00:00
|
|
|
SAV.SetData(Data[GetGiftOffset(index)..], wc7.Data);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-06-09 02:56:11 +00:00
|
|
|
|
2024-03-04 05:13:16 +00:00
|
|
|
public int MysteryGiftReceivedFlagMax => MaxReceivedFlag;
|
|
|
|
public bool GetMysteryGiftReceivedFlag(int index)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2024-03-04 05:13:16 +00:00
|
|
|
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual((uint)index, (uint)MaxReceivedFlag);
|
|
|
|
return FlagUtil.GetFlag(Data, index); // offset 0
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
2024-03-04 05:13:16 +00:00
|
|
|
public void SetMysteryGiftReceivedFlag(int index, bool value)
|
|
|
|
{
|
|
|
|
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual((uint)index, (uint)MaxReceivedFlag);
|
|
|
|
FlagUtil.SetFlag(Data, index, value); // offset 0
|
2019-06-09 02:56:11 +00:00
|
|
|
}
|
2024-03-04 05:13:16 +00:00
|
|
|
|
|
|
|
public int GiftCountMax => MaxCardsPresent;
|
|
|
|
DataMysteryGift IMysteryGiftStorage.GetMysteryGift(int index) => GetMysteryGift(index);
|
|
|
|
void IMysteryGiftStorage.SetMysteryGift(int index, DataMysteryGift gift) => SetMysteryGift(index, (WC7)gift);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|