2022-06-18 18:04:24 +00:00
|
|
|
using System;
|
2021-03-28 06:22:56 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2022-01-03 05:35:59 +00:00
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
2021-03-28 06:22:56 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
2023-12-04 04:13:20 +00:00
|
|
|
public sealed class BV3(byte[] Data) : BattleVideo
|
2021-03-28 06:22:56 +00:00
|
|
|
{
|
2023-12-04 04:13:20 +00:00
|
|
|
public BV3() : this(new byte[SIZE]) { }
|
|
|
|
|
|
|
|
public readonly byte[] Data = (byte[])Data.Clone();
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
internal const int SIZE = 0xF80;
|
|
|
|
public override int Generation => 3;
|
2021-03-28 06:22:56 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
public override IReadOnlyList<PK3> BattlePKMs => PlayerTeams.SelectMany(z => z).ToArray();
|
2021-03-28 06:22:56 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
internal new static bool IsValid(ReadOnlySpan<byte> data)
|
|
|
|
{
|
|
|
|
if (data.Length != SIZE)
|
|
|
|
return false;
|
2023-01-22 04:02:33 +00:00
|
|
|
var chkSpan = data[^4..];
|
|
|
|
var chk = ReadUInt32LittleEndian(chkSpan);
|
2022-06-18 18:04:24 +00:00
|
|
|
if (chk > 0xF7080)
|
|
|
|
return false; // max if all are FF
|
2023-01-22 04:02:33 +00:00
|
|
|
|
|
|
|
var chkRegion = data[..^4];
|
|
|
|
var expect = GetByteSum(chkRegion);
|
2022-06-18 18:04:24 +00:00
|
|
|
return chk == expect;
|
|
|
|
}
|
2021-03-28 06:22:56 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public IReadOnlyList<PK3[]> PlayerTeams
|
|
|
|
{
|
2023-12-04 04:13:20 +00:00
|
|
|
get => [GetTeam(0), GetTeam(1)];
|
2022-06-18 18:04:24 +00:00
|
|
|
set
|
2021-03-28 06:22:56 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
SetTeam(value[0], 0);
|
|
|
|
SetTeam(value[1], 1);
|
2021-03-28 06:22:56 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public PK3[] GetTeam(int teamIndex)
|
|
|
|
{
|
|
|
|
if ((uint)teamIndex > 2)
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(teamIndex));
|
2021-03-28 06:22:56 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var ofs = 6 * PokeCrypto.SIZE_3PARTY * teamIndex;
|
|
|
|
var team = new PK3[6];
|
|
|
|
for (int p = 0; p < 6; p++)
|
2021-03-28 06:22:56 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
int offset = ofs + (PokeCrypto.SIZE_3PARTY * p);
|
2023-12-04 04:13:20 +00:00
|
|
|
var span = Data.AsSpan(offset, PokeCrypto.SIZE_3PARTY);
|
|
|
|
team[p] = new PK3(span.ToArray());
|
2021-03-28 06:22:56 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return team;
|
|
|
|
}
|
2021-03-28 06:22:56 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public void SetTeam(IReadOnlyList<PK3> team, int teamIndex)
|
|
|
|
{
|
|
|
|
var ofs = 6 * PokeCrypto.SIZE_3PARTY * teamIndex;
|
|
|
|
for (int p = 0; p < 6; p++)
|
2021-03-28 06:22:56 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
int offset = ofs + (PokeCrypto.SIZE_3PARTY * p);
|
|
|
|
team[p].EncryptedPartyData.CopyTo(Data, offset);
|
2021-03-28 06:22:56 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2021-03-28 06:22:56 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// 0x4B0 - string3[4][8] Trainer Names
|
|
|
|
// 0x4D0 - u8[4] Trainer Genders
|
|
|
|
// 0x4D4 - u32[4] Trainer IDs
|
|
|
|
// 0x4E4 - u8[4] Trainer Languages
|
2021-03-28 06:22:56 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public uint Seed
|
|
|
|
{
|
|
|
|
get => ReadUInt32LittleEndian(Data.AsSpan(0x4E8));
|
|
|
|
set => WriteUInt32LittleEndian(Data.AsSpan(0x4E8), value);
|
|
|
|
}
|
2021-03-28 06:22:56 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public uint Mode
|
|
|
|
{
|
|
|
|
get => ReadUInt32LittleEndian(Data.AsSpan(0x4EC));
|
|
|
|
set => WriteUInt32LittleEndian(Data.AsSpan(0x4EC), value);
|
|
|
|
}
|
2021-03-28 06:22:56 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// ...
|
2021-03-28 06:22:56 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public uint Checksum
|
|
|
|
{
|
|
|
|
get => ReadUInt32LittleEndian(Data.AsSpan(SIZE - 4));
|
|
|
|
set => WriteUInt32LittleEndian(Data.AsSpan(SIZE - 4), value);
|
|
|
|
}
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
public bool IsChecksumValid() => Checksum == GetByteSum(Data.AsSpan()[..^4]);
|
2022-06-18 18:04:24 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
public static uint GetByteSum(ReadOnlySpan<byte> data)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
uint result = 0;
|
2023-01-22 04:02:33 +00:00
|
|
|
foreach (byte b in data)
|
|
|
|
result += b;
|
2022-06-18 18:04:24 +00:00
|
|
|
return result;
|
2021-03-28 06:22:56 +00:00
|
|
|
}
|
|
|
|
}
|