PKHeX/PKHeX.Core/Saves/SAV8LA.cs

235 lines
9.5 KiB
C#
Raw Normal View History

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;
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
{
protected internal override string ShortSummary => $"{OT} ({Version}) - {LastSaved.LastSavedTime}";
public override string Extension => string.Empty;
public SAV8LA(byte[] data) : this(SwishCrypto.Decrypt(data)) { }
private SAV8LA(IReadOnlyList<SCBlock> blocks) : base(Array.Empty<byte>())
{
AllBlocks = blocks;
Blocks = new SaveBlockAccessor8LA(this);
2022-02-27 15:56:47 +00:00
SaveRevision = Blocks.DetectRevision();
Initialize();
}
public SAV8LA()
{
AllBlocks = Meta8.GetBlankDataLA();
Blocks = new SaveBlockAccessor8LA(this);
2022-02-27 15:56:47 +00:00
SaveRevision = Blocks.DetectRevision();
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)),
};
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++)
mine[i].CopyFrom(newB[i]);
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;
protected override PA8 GetPKM(byte[] data) => new(data);
protected override byte[] DecryptPKM(byte[] data) => PokeCrypto.DecryptArray8A(data);
public override PA8 BlankPKM => new();
public override Type PKMType => typeof(PA8);
public override int MaxEV => 252;
public override int Generation => 8;
public override EntityContext Context => EntityContext.Gen8a;
public override int MaxStringLengthOT => 12;
public override int MaxStringLengthNickname => 12;
public override bool ChecksumsValid => true;
public override string ChecksumInfo => string.Empty;
public override int BoxCount => BoxLayout8a.BoxCount; // 32
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; }
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);
public override PersonalTable8LA Personal => PersonalTable.LA;
public override ReadOnlySpan<ushort> HeldItems => Legal.HeldItems_LA;
protected override SAV8LA CloneInternal()
{
var blockCopy = new SCBlock[AllBlocks.Count];
for (int i = 0; i < AllBlocks.Count; i++)
blockCopy[i] = AllBlocks[i].Clone();
return new(blockCopy);
}
public override ushort MaxMoveID => Legal.MaxMoveID_8a;
public override ushort MaxSpeciesID => Legal.MaxSpeciesID_8a;
public override int MaxItemID => Legal.MaxItemID_8a;
public override int MaxBallID => Legal.MaxBallID_8a;
public override int MaxGameID => Legal.MaxGameID_HOME;
public override int MaxAbilityID => Legal.MaxAbilityID_8a;
#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);
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;
public Epoch1970Value AdventureStart => Blocks.AdventureStart;
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));
#endregion
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; }
protected override Span<byte> BoxBuffer => BoxInfo.Data;
protected override Span<byte> PartyBuffer => PartyInfo.Data;
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;
}
protected override void SetPKM(PKM pk, bool isParty = false)
{
var pa8 = (PA8)pk;
// Apply to this Save File
pa8.Trade(this);
pa8.RefreshChecksum();
}
// Zukan
protected override void SetDex(PKM pk)
{
// TODO: Seen in wild?
// Accessor.SetPokeSeenInWild(pk);
// 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?
// For now, if we have never obtained the poke, treat this pk as obtained-via-trade.
PokedexSave.OnPokeGet_TradeWithoutEvolution(pk);
}
public override bool GetCaught(ushort species)
{
if (species > Personal.MaxSpeciesID)
return false;
var formCount = Personal[species].FormCount;
for (byte form = 0; form < formCount; form++)
{
if (PokedexSave.HasAnyPokeObtainFlags(species, form))
return true;
}
return false;
}
public override bool GetSeen(ushort species) => PokedexSave.HasPokeEverBeenUpdated(species);
// 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
{
if (value.Length != 3)
return;
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);
}
}
public override int GetBoxOffset(int box) => Box + (SIZE_BOXSLOT * box * 30);
public override string GetBoxName(int box) => BoxLayout.GetBoxName(box);
public override void SetBoxName(int box, ReadOnlySpan<char> value) => BoxLayout.SetBoxName(box, value);
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
}