PKHeX/PKHeX.Core/Saves/Substructures/Battle Videos/BV7.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

124 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using static System.Buffers.Binary.BinaryPrimitives;
namespace PKHeX.Core
{
public sealed class BV7 : BattleVideo
{
internal const int SIZE = 0x2BC0;
private const string NPC = "NPC";
private const int PlayerCount = 4;
public override int Generation => 7;
private readonly byte[] Data;
public override IReadOnlyList<PKM> BattlePKMs => PlayerTeams.SelectMany(t => t).ToArray();
internal new static bool IsValid(ReadOnlySpan<byte> data) => data.Length == SIZE;
public BV7(byte[] data) => Data = (byte[])data.Clone();
private static readonly int[] offsets = { 0xE41, 0x145E, 0x1A7B, 0x2098 };
public IReadOnlyList<PKM[]> PlayerTeams
{
get
{
var Teams = new PKM[PlayerCount][];
for (int t = 0; t < PlayerCount; t++)
Teams[t] = GetTeam(t);
return Teams;
}
set
{
for (int t = 0; t < PlayerCount; t++)
SetTeam(value[t], t);
}
}
public PKM[] GetTeam(int teamIndex)
{
var team = new PKM[6];
var ofs = offsets[teamIndex];
for (int p = 0; p < 6; p++)
{
int offset = ofs + (PokeCrypto.SIZE_6PARTY * p);
team[p] = new PK7(Data.Slice(offset, PokeCrypto.SIZE_6STORED));
}
return team;
}
public void SetTeam(IReadOnlyList<PKM> team, int teamIndex)
{
var ofs = offsets[teamIndex];
for (int p = 0; p < 6; p++)
{
int offset = ofs + (PokeCrypto.SIZE_6PARTY * p);
team[p].EncryptedPartyData.CopyTo(Data, offset);
}
}
public string[] GetPlayerNames()
{
string[] trainers = new string[PlayerCount];
for (int i = 0; i < PlayerCount; i++)
{
var span = Data.AsSpan(0x12C + +(0x1A * i), 0x1A);
var str = StringConverter7.GetString(span);
trainers[i] = string.IsNullOrWhiteSpace(trainers[i]) ? NPC : str;
}
return trainers;
}
public void SetPlayerNames(IReadOnlyList<string> value)
{
if (value.Count != PlayerCount)
return;
for (int i = 0; i < PlayerCount; i++)
{
string tr = value[i] == NPC ? string.Empty : value[i];
var span = Data.AsSpan(0x12C + +(0x1A * i), 0x1A);
StringConverter7.SetString(span, tr.AsSpan(), 12, 0, StringConverterOption.ClearZero);
}
}
private int MatchYear { get => ReadUInt16LittleEndian(Data.AsSpan(0x2BB0)); set => WriteUInt16LittleEndian(Data.AsSpan(0x2BB0), (ushort)value); }
private int MatchDay { get => Data[0x2BB3]; set => Data[0x2BB3] = (byte)value; }
private int MatchMonth { get => Data[0x2BB2]; set => Data[0x2BB2] = (byte)value; }
private int MatchHour { get => Data[0x2BB4]; set => Data[0x2BB4] = (byte)value; }
private int MatchMinute { get => Data[0x2BB5]; set => Data[0x2BB5] = (byte)value; }
private int MatchSecond { get => Data[0x2BB6]; set => Data[0x2BB6] = (byte)value; }
public DateTime? MatchStamp
{
get
{
if (!DateUtil.IsDateValid(MatchYear, MatchMonth, MatchDay))
return null;
return new DateTime(MatchYear, MatchMonth, MatchDay, MatchHour, MatchMinute, MatchSecond);
}
set
{
if (value.HasValue)
{
MatchYear = value.Value.Year;
MatchDay = value.Value.Day;
MatchMonth = value.Value.Month;
MatchHour = value.Value.Hour;
MatchMinute = value.Value.Minute;
MatchSecond = value.Value.Second;
}
else
{
MatchYear = MatchDay = MatchMonth = MatchHour = MatchMinute = MatchSecond = 0;
}
}
}
public int MusicID { get => Data[0x21C]; set => Data[0x21C] = (byte)value; }
public bool SilentBGM { get => MusicID == 0xFF; set => MusicID = (byte)(value ? 0xFF : MusicID); }
}
}