2023-01-22 04:02:33 +00:00
|
|
|
using System;
|
2020-12-05 14:09:33 +00:00
|
|
|
using System.Collections.Generic;
|
2019-03-15 19:35:55 +00:00
|
|
|
using System.Linq;
|
2022-01-03 05:35:59 +00:00
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
2016-11-10 16:14:54 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
2024-03-07 06:34:21 +00:00
|
|
|
public sealed class BattleVideo7(byte[] data) : IBattleVideo
|
2016-11-10 16:14:54 +00:00
|
|
|
{
|
2023-10-15 02:28:46 +00:00
|
|
|
public const int SIZE = 0x2BC0;
|
2022-06-18 18:04:24 +00:00
|
|
|
private const string NPC = "NPC";
|
|
|
|
private const int PlayerCount = 4;
|
2018-09-15 05:37:47 +00:00
|
|
|
|
2024-03-07 06:34:21 +00:00
|
|
|
public byte Generation => 7;
|
2023-12-04 04:13:20 +00:00
|
|
|
private readonly byte[] Data = (byte[])data.Clone();
|
2016-11-10 16:14:54 +00:00
|
|
|
|
2024-03-07 06:34:21 +00:00
|
|
|
public IEnumerable<PKM> Contents => PlayerTeams.SelectMany(t => t);
|
|
|
|
public static bool IsValid(ReadOnlySpan<byte> data) => data.Length == SIZE;
|
2016-11-10 16:14:54 +00:00
|
|
|
|
2023-12-04 04:13:20 +00:00
|
|
|
private static ReadOnlySpan<ushort> TeamOffsets => [0xE41, 0x145E, 0x1A7B, 0x2098];
|
2016-11-10 16:14:54 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
public IReadOnlyList<PK7[]> PlayerTeams
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
get
|
2016-11-10 16:14:54 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
var Teams = new PK7[PlayerCount][];
|
2022-06-18 18:04:24 +00:00
|
|
|
for (int t = 0; t < PlayerCount; t++)
|
|
|
|
Teams[t] = GetTeam(t);
|
|
|
|
return Teams;
|
2016-11-10 16:14:54 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
set
|
2020-12-05 14:09:33 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
for (int t = 0; t < PlayerCount; t++)
|
|
|
|
SetTeam(value[t], t);
|
2020-12-05 14:09:33 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-03-15 19:35:55 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
public PK7[] GetTeam(int teamIndex)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
var team = new PK7[6];
|
2023-12-04 04:13:20 +00:00
|
|
|
var ofs = TeamOffsets[teamIndex];
|
2022-06-18 18:04:24 +00:00
|
|
|
for (int p = 0; p < 6; p++)
|
2019-03-15 19:35:55 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
int offset = ofs + (PokeCrypto.SIZE_6PARTY * p);
|
2023-12-04 04:13:20 +00:00
|
|
|
var span = Data.AsSpan(offset, PokeCrypto.SIZE_6STORED);
|
|
|
|
team[p] = new PK7(span.ToArray());
|
2020-12-05 14:09:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return team;
|
|
|
|
}
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
public void SetTeam(IReadOnlyList<PK7> team, int teamIndex)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-12-04 04:13:20 +00:00
|
|
|
var ofs = TeamOffsets[teamIndex];
|
2022-06-18 18:04:24 +00:00
|
|
|
for (int p = 0; p < 6; p++)
|
2020-12-05 14:09:33 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
int offset = ofs + (PokeCrypto.SIZE_6PARTY * p);
|
|
|
|
team[p].EncryptedPartyData.CopyTo(Data, offset);
|
2020-12-05 14:09:33 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-03-15 19:35:55 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public string[] GetPlayerNames()
|
|
|
|
{
|
|
|
|
string[] trainers = new string[PlayerCount];
|
|
|
|
for (int i = 0; i < PlayerCount; i++)
|
2020-12-05 14:09:33 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var span = Data.AsSpan(0x12C + +(0x1A * i), 0x1A);
|
|
|
|
var str = StringConverter7.GetString(span);
|
|
|
|
trainers[i] = string.IsNullOrWhiteSpace(trainers[i]) ? NPC : str;
|
|
|
|
}
|
|
|
|
return trainers;
|
|
|
|
}
|
2020-12-05 14:09:33 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
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);
|
2023-01-22 04:02:33 +00:00
|
|
|
StringConverter7.SetString(span, tr, 12, 0, StringConverterOption.ClearZero);
|
2019-03-15 19:35:55 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-03-15 19:35:55 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
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; }
|
2019-03-15 19:35:55 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public DateTime? MatchStamp
|
|
|
|
{
|
|
|
|
get
|
2019-03-15 19:35:55 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!DateUtil.IsDateValid(MatchYear, MatchMonth, MatchDay))
|
|
|
|
return null;
|
|
|
|
return new DateTime(MatchYear, MatchMonth, MatchDay, MatchHour, MatchMinute, MatchSecond);
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
2024-03-09 03:30:21 +00:00
|
|
|
if (value is { } dt)
|
2019-03-15 19:35:55 +00:00
|
|
|
{
|
2024-03-09 03:30:21 +00:00
|
|
|
MatchYear = dt.Year;
|
|
|
|
MatchDay = dt.Day;
|
|
|
|
MatchMonth = dt.Month;
|
|
|
|
MatchHour = dt.Hour;
|
|
|
|
MatchMinute = dt.Minute;
|
|
|
|
MatchSecond = dt.Second;
|
2019-03-15 19:35:55 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else
|
2019-03-15 19:35:55 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
MatchYear = MatchDay = MatchMonth = MatchHour = MatchMinute = MatchSecond = 0;
|
2019-03-15 19:35:55 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-10 16:14:54 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
public int MusicID { get => Data[0x21C]; set => Data[0x21C] = (byte)value; }
|
|
|
|
public bool SilentBGM { get => MusicID == 0xFF; set => MusicID = (byte)(value ? 0xFF : MusicID); }
|
2016-11-10 16:14:54 +00:00
|
|
|
}
|