Refactoring: Move Source (Legality) (#3560)
Rewrites a good amount of legality APIs pertaining to:
* Legal moves that can be learned
* Evolution chains & cross-generation paths
* Memory validation with forgotten moves
In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data.
The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space.
The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation.
* `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game.
* `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`).
* Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 23:15:27 +00:00
|
|
|
using System;
|
2022-02-05 01:31:20 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Generation 8 <see cref="SaveFile"/> object for <see cref="GameVersion.PLA"/> games.
|
|
|
|
/// </summary>
|
2022-02-27 15:56:47 +00:00
|
|
|
public sealed class SAV8LA : SaveFile, ISaveBlock8LA, ISCBlockArray, ISaveFileRevision
|
2022-02-05 01:31:20 +00:00
|
|
|
{
|
|
|
|
protected internal override string ShortSummary => $"{OT} ({Version}) - {LastSaved.LastSavedTime}";
|
|
|
|
public override string Extension => string.Empty;
|
|
|
|
|
2022-03-06 01:15:38 +00:00
|
|
|
public SAV8LA(byte[] data) : this(SwishCrypto.Decrypt(data)) { }
|
2022-02-05 01:31:20 +00:00
|
|
|
|
2022-02-10 00:48:55 +00:00
|
|
|
private SAV8LA(IReadOnlyList<SCBlock> blocks) : base(Array.Empty<byte>())
|
2022-02-05 01:31:20 +00:00
|
|
|
{
|
|
|
|
AllBlocks = blocks;
|
|
|
|
Blocks = new SaveBlockAccessor8LA(this);
|
2022-02-27 15:56:47 +00:00
|
|
|
SaveRevision = Blocks.DetectRevision();
|
2022-02-05 01:31:20 +00:00
|
|
|
Initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
public SAV8LA()
|
|
|
|
{
|
|
|
|
AllBlocks = Meta8.GetBlankDataLA();
|
|
|
|
Blocks = new SaveBlockAccessor8LA(this);
|
2022-02-27 15:56:47 +00:00
|
|
|
SaveRevision = Blocks.DetectRevision();
|
2022-02-05 01:31:20 +00:00
|
|
|
Initialize();
|
|
|
|
ClearBoxes();
|
|
|
|
}
|
|
|
|
|
2022-02-27 15:56:47 +00:00
|
|
|
public int SaveRevision { get; }
|
|
|
|
public string SaveRevisionString => SaveRevision switch
|
|
|
|
{
|
|
|
|
0 => "-Base", // Vanilla
|
|
|
|
1 => "-DB", // DLC 1: Daybreak
|
|
|
|
_ => throw new ArgumentOutOfRangeException(nameof(SaveRevision)),
|
|
|
|
};
|
|
|
|
|
2022-02-05 01:31:20 +00:00
|
|
|
public override string GetString(ReadOnlySpan<byte> data) => StringConverter8.GetString(data);
|
|
|
|
public override int SetString(Span<byte> destBuffer, ReadOnlySpan<char> value, int maxLength, StringConverterOption option) => StringConverter8.SetString(destBuffer, value, maxLength, option);
|
|
|
|
|
|
|
|
public override void CopyChangesFrom(SaveFile sav)
|
|
|
|
{
|
|
|
|
// Absorb changes from all blocks
|
|
|
|
var z = (SAV8LA)sav;
|
|
|
|
var mine = AllBlocks;
|
|
|
|
var newB = z.AllBlocks;
|
|
|
|
for (int i = 0; i < mine.Count; i++)
|
2022-02-26 00:10:49 +00:00
|
|
|
mine[i].CopyFrom(newB[i]);
|
2022-02-05 01:31:20 +00:00
|
|
|
State.Edited = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override int SIZE_STORED => PokeCrypto.SIZE_8ASTORED;
|
|
|
|
protected override int SIZE_PARTY => PokeCrypto.SIZE_8APARTY;
|
|
|
|
public override int SIZE_BOXSLOT => PokeCrypto.SIZE_8ASTORED;
|
2023-01-22 04:02:33 +00:00
|
|
|
protected override PA8 GetPKM(byte[] data) => new(data);
|
2022-02-05 01:31:20 +00:00
|
|
|
protected override byte[] DecryptPKM(byte[] data) => PokeCrypto.DecryptArray8A(data);
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
public override PA8 BlankPKM => new();
|
2022-02-05 01:31:20 +00:00
|
|
|
public override Type PKMType => typeof(PA8);
|
2023-09-28 05:15:59 +00:00
|
|
|
public override int MaxEV => EffortValues.Max252;
|
2022-02-05 01:31:20 +00:00
|
|
|
public override int Generation => 8;
|
2022-06-04 02:08:46 +00:00
|
|
|
public override EntityContext Context => EntityContext.Gen8a;
|
2022-11-25 01:42:17 +00:00
|
|
|
public override int MaxStringLengthOT => 12;
|
|
|
|
public override int MaxStringLengthNickname => 12;
|
2022-02-05 01:31:20 +00:00
|
|
|
|
|
|
|
public override bool ChecksumsValid => true;
|
|
|
|
public override string ChecksumInfo => string.Empty;
|
|
|
|
public override int BoxCount => BoxLayout8a.BoxCount; // 32
|
2023-01-22 04:02:33 +00:00
|
|
|
public override uint ID32 { get => MyStatus.ID32; set => MyStatus.ID32 = value; }
|
|
|
|
public override ushort TID16 { get => MyStatus.TID16; set => MyStatus.TID16 = value; }
|
|
|
|
public override ushort SID16 { get => MyStatus.SID16; set => MyStatus.SID16 = value; }
|
2022-02-05 01:31:20 +00:00
|
|
|
public override int Game { get => MyStatus.Game; set => MyStatus.Game = value; }
|
|
|
|
public override int Gender { get => MyStatus.Gender; set => MyStatus.Gender = value; }
|
|
|
|
public override int Language { get => MyStatus.Language; set => MyStatus.Language = value; }
|
|
|
|
public override string OT { get => MyStatus.OT; set => MyStatus.OT = value; }
|
|
|
|
|
|
|
|
public override GameVersion Version => Game switch
|
|
|
|
{
|
|
|
|
(int)GameVersion.PLA => GameVersion.PLA,
|
|
|
|
_ => GameVersion.Invalid,
|
|
|
|
};
|
|
|
|
|
|
|
|
protected override void SetChecksums() { } // None!
|
|
|
|
protected override byte[] GetFinalData() => SwishCrypto.Encrypt(AllBlocks);
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
public override PersonalTable8LA Personal => PersonalTable.LA;
|
2023-03-26 00:55:55 +00:00
|
|
|
public override ReadOnlySpan<ushort> HeldItems => Legal.HeldItems_LA;
|
2022-02-05 01:31:20 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
protected override SAV8LA CloneInternal()
|
2022-02-05 01:31:20 +00:00
|
|
|
{
|
|
|
|
var blockCopy = new SCBlock[AllBlocks.Count];
|
|
|
|
for (int i = 0; i < AllBlocks.Count; i++)
|
|
|
|
blockCopy[i] = AllBlocks[i].Clone();
|
2023-01-22 04:02:33 +00:00
|
|
|
return new(blockCopy);
|
2022-02-05 01:31:20 +00:00
|
|
|
}
|
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
public override ushort MaxMoveID => Legal.MaxMoveID_8a;
|
|
|
|
public override ushort MaxSpeciesID => Legal.MaxSpeciesID_8a;
|
2022-02-05 01:31:20 +00:00
|
|
|
public override int MaxItemID => Legal.MaxItemID_8a;
|
|
|
|
public override int MaxBallID => Legal.MaxBallID_8a;
|
2023-06-04 01:19:16 +00:00
|
|
|
public override int MaxGameID => Legal.MaxGameID_HOME;
|
2022-02-05 01:31:20 +00:00
|
|
|
public override int MaxAbilityID => Legal.MaxAbilityID_8a;
|
|
|
|
|
2022-03-06 01:46:03 +00:00
|
|
|
#region Blocks
|
|
|
|
public SCBlockAccessor Accessor => Blocks;
|
|
|
|
public SaveBlockAccessor8LA Blocks { get; }
|
|
|
|
public IReadOnlyList<SCBlock> AllBlocks { get; }
|
|
|
|
public T GetValue<T>(uint key) where T : struct => Blocks.GetBlockValueSafe<T>(key);
|
|
|
|
public void SetValue<T>(uint key, T value) where T : struct => Blocks.SetBlockValueSafe(key, value);
|
2022-02-05 01:31:20 +00:00
|
|
|
public Box8 BoxInfo => Blocks.BoxInfo;
|
|
|
|
public Party8a PartyInfo => Blocks.PartyInfo;
|
|
|
|
public MyStatus8a MyStatus => Blocks.MyStatus;
|
|
|
|
public PokedexSave8a PokedexSave => Blocks.PokedexSave;
|
|
|
|
public BoxLayout8a BoxLayout => Blocks.BoxLayout;
|
|
|
|
public MyItem8a Items => Blocks.Items;
|
2022-11-25 01:42:17 +00:00
|
|
|
public Epoch1970Value AdventureStart => Blocks.AdventureStart;
|
2023-11-05 20:24:08 +00:00
|
|
|
public Coordinates8a Coordinates => Blocks.Coordinates;
|
2022-02-05 01:31:20 +00:00
|
|
|
public LastSaved8a LastSaved => Blocks.LastSaved;
|
|
|
|
public PlayTime8a Played => Blocks.Played;
|
2022-03-20 08:46:24 +00:00
|
|
|
public AreaSpawnerSet8a AreaSpawners => new(Blocks.GetBlock(SaveBlockAccessor8LA.KSpawners));
|
2022-03-06 01:46:03 +00:00
|
|
|
#endregion
|
2022-02-21 01:59:48 +00:00
|
|
|
|
2022-02-05 01:31:20 +00:00
|
|
|
public override uint SecondsToStart { get => (uint)AdventureStart.Seconds; set => AdventureStart.Seconds = value; }
|
|
|
|
public override uint Money { get => (uint)Blocks.GetBlockValue(SaveBlockAccessor8LA.KMoney); set => Blocks.SetBlockValue(SaveBlockAccessor8LA.KMoney, value); }
|
|
|
|
public override int MaxMoney => 9_999_999;
|
|
|
|
|
|
|
|
public override int PlayedHours { get => Played.PlayedHours; set => Played.PlayedHours = (ushort)value; }
|
|
|
|
public override int PlayedMinutes { get => Played.PlayedMinutes; set => Played.PlayedMinutes = (byte)value; }
|
|
|
|
public override int PlayedSeconds { get => Played.PlayedSeconds; set => Played.PlayedSeconds = (byte)value; }
|
|
|
|
|
2023-03-26 06:14:50 +00:00
|
|
|
protected override Span<byte> BoxBuffer => BoxInfo.Data;
|
|
|
|
protected override Span<byte> PartyBuffer => PartyInfo.Data;
|
2022-02-05 01:31:20 +00:00
|
|
|
|
|
|
|
private void Initialize()
|
|
|
|
{
|
|
|
|
Box = 0;
|
|
|
|
Party = 0;
|
|
|
|
PokeDex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override int GetPartyOffset(int slot) => Party + (SIZE_PARTY * slot);
|
|
|
|
public override int PartyCount
|
|
|
|
{
|
|
|
|
get => PartyInfo.PartyCount;
|
|
|
|
protected set => PartyInfo.PartyCount = value;
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override void SetPKM(PKM pk, bool isParty = false)
|
2022-05-31 04:43:52 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var pa8 = (PA8)pk;
|
2022-05-31 04:43:52 +00:00
|
|
|
// Apply to this Save File
|
2022-06-18 18:04:24 +00:00
|
|
|
pa8.Trade(this);
|
|
|
|
pa8.RefreshChecksum();
|
2022-05-31 04:43:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 01:31:20 +00:00
|
|
|
// Zukan
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override void SetDex(PKM pk)
|
2022-02-05 01:31:20 +00:00
|
|
|
{
|
|
|
|
// TODO: Seen in wild?
|
2022-06-18 18:04:24 +00:00
|
|
|
// Accessor.SetPokeSeenInWild(pk);
|
2022-02-05 01:31:20 +00:00
|
|
|
|
|
|
|
// TODO: Should this update research? What research should it be updating?
|
|
|
|
// TODO: Should this be passing "caught=true" to set caught flags and not just obtain flags?
|
2022-06-18 18:04:24 +00:00
|
|
|
// For now, if we have never obtained the poke, treat this pk as obtained-via-trade.
|
|
|
|
PokedexSave.OnPokeGet_TradeWithoutEvolution(pk);
|
2022-02-05 01:31:20 +00:00
|
|
|
}
|
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
public override bool GetCaught(ushort species)
|
2022-02-05 01:31:20 +00:00
|
|
|
{
|
|
|
|
if (species > Personal.MaxSpeciesID)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
var formCount = Personal[species].FormCount;
|
2022-08-27 19:53:30 +00:00
|
|
|
for (byte form = 0; form < formCount; form++)
|
2022-02-05 01:31:20 +00:00
|
|
|
{
|
|
|
|
if (PokedexSave.HasAnyPokeObtainFlags(species, form))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
public override bool GetSeen(ushort species) => PokedexSave.HasPokeEverBeenUpdated(species);
|
2022-02-05 01:31:20 +00:00
|
|
|
|
|
|
|
// Inventory
|
|
|
|
public override IReadOnlyList<InventoryPouch> Inventory { get => Items.Inventory; set => Items.Inventory = value; }
|
|
|
|
|
|
|
|
#region Boxes
|
|
|
|
public override bool HasBoxWallpapers => false;
|
|
|
|
public override bool HasNamableBoxes => true;
|
|
|
|
public override int CurrentBox { get => BoxLayout.CurrentBox; set => BoxLayout.CurrentBox = value; }
|
|
|
|
public override int BoxesUnlocked { get => (byte)Blocks.GetBlockValue(SaveBlockAccessor8LA.KBoxesUnlocked); set => Blocks.SetBlockValue(SaveBlockAccessor8LA.KBoxesUnlocked, (byte)value); }
|
|
|
|
|
|
|
|
public override byte[] BoxFlags
|
|
|
|
{
|
|
|
|
get => new[]
|
|
|
|
{
|
|
|
|
Convert.ToByte(Blocks.GetBlock(SaveBlockAccessor8LA.KUnlockedSecretBox01).Type - 1),
|
|
|
|
Convert.ToByte(Blocks.GetBlock(SaveBlockAccessor8LA.KUnlockedSecretBox02).Type - 1),
|
|
|
|
Convert.ToByte(Blocks.GetBlock(SaveBlockAccessor8LA.KUnlockedSecretBox03).Type - 1),
|
|
|
|
};
|
|
|
|
set
|
|
|
|
{
|
2022-02-25 21:29:43 +00:00
|
|
|
if (value.Length != 3)
|
2022-02-05 01:31:20 +00:00
|
|
|
return;
|
|
|
|
|
2022-02-25 21:29:43 +00:00
|
|
|
Blocks.GetBlock(SaveBlockAccessor8LA.KUnlockedSecretBox01).ChangeBooleanType((SCTypeCode)(value[0] & 1) + 1);
|
|
|
|
Blocks.GetBlock(SaveBlockAccessor8LA.KUnlockedSecretBox02).ChangeBooleanType((SCTypeCode)(value[1] & 1) + 1);
|
|
|
|
Blocks.GetBlock(SaveBlockAccessor8LA.KUnlockedSecretBox03).ChangeBooleanType((SCTypeCode)(value[2] & 1) + 1);
|
2022-02-05 01:31:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override int GetBoxOffset(int box) => Box + (SIZE_BOXSLOT * box * 30);
|
|
|
|
public override string GetBoxName(int box) => BoxLayout.GetBoxName(box);
|
2023-01-22 04:02:33 +00:00
|
|
|
public override void SetBoxName(int box, ReadOnlySpan<char> value) => BoxLayout.SetBoxName(box, value);
|
2022-02-05 01:31:20 +00:00
|
|
|
|
|
|
|
public override int GetBoxWallpaper(int box)
|
|
|
|
{
|
|
|
|
if ((uint)box >= BoxCount)
|
|
|
|
return box;
|
|
|
|
var b = Blocks.GetBlock(SaveBlockAccessor8LA.KBoxWallpapersUnused);
|
|
|
|
return b.Data[box];
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void SetBoxWallpaper(int box, int value)
|
|
|
|
{
|
|
|
|
if ((uint)box >= BoxCount)
|
|
|
|
return;
|
|
|
|
var b = Blocks.GetBlock(SaveBlockAccessor8LA.KBoxWallpapersUnused);
|
|
|
|
b.Data[box] = (byte)value;
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|