2022-08-27 06:43:36 +00:00
|
|
|
using System;
|
2020-08-18 22:39:45 +00:00
|
|
|
using System.Collections.Generic;
|
2023-12-07 08:07:55 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2022-04-10 01:12:57 +00:00
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
2016-10-20 01:19:01 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Generation 7 <see cref="SaveFile"/> object.
|
|
|
|
/// </summary>
|
|
|
|
public abstract class SAV7 : SAV_BEEF, ITrainerStatRecord, ISaveBlock7Main, IRegionOrigin, IGameSync, IEventFlag37
|
2016-10-20 01:19:01 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
// Save Data Attributes
|
|
|
|
protected internal override string ShortSummary => $"{OT} ({Version}) - {Played.LastSavedTime}";
|
|
|
|
public override string Extension => string.Empty;
|
2018-09-15 05:37:47 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public override IReadOnlyList<string> PKMExtensions => Array.FindAll(PKM.Extensions, f =>
|
|
|
|
{
|
|
|
|
int gen = f[^1] - 0x30;
|
|
|
|
return gen <= 7 && f[1] != 'b'; // ignore PB7
|
|
|
|
});
|
2017-01-05 06:22:50 +00:00
|
|
|
|
2023-12-07 08:07:55 +00:00
|
|
|
protected SAV7(byte[] data, [ConstantExpected] int biOffset) : base(data, biOffset)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
}
|
2016-10-20 01:19:01 +00:00
|
|
|
|
2023-12-07 08:07:55 +00:00
|
|
|
protected SAV7([ConstantExpected] int size, [ConstantExpected] int biOffset) : base(size, biOffset)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
}
|
2016-10-20 01:19:01 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected void ReloadBattleTeams()
|
|
|
|
{
|
2023-12-04 04:13:20 +00:00
|
|
|
var demo = this is SAV7SM && !Data.AsSpan(BoxLayout.Offset, 0x4C4).ContainsAnyExcept<byte>(0); // up to Battle Box values
|
2022-06-18 18:04:24 +00:00
|
|
|
if (demo || !State.Exportable)
|
2019-06-09 02:56:11 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
BoxLayout.ClearBattleTeams();
|
2016-10-20 01:19:01 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else // Valid slot locking info present
|
2017-11-04 22:04:21 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
BoxLayout.LoadBattleTeams();
|
2016-10-20 01:19:01 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
#region Blocks
|
|
|
|
public abstract MyItem Items { get; }
|
|
|
|
public abstract MysteryBlock7 MysteryGift { get; }
|
|
|
|
public abstract PokeFinder7 PokeFinder { get; }
|
|
|
|
public abstract JoinFesta7 Festa { get; }
|
|
|
|
public abstract Daycare7 Daycare { get; }
|
|
|
|
public abstract RecordBlock6 Records { get; }
|
|
|
|
public abstract PlayTime6 Played { get; }
|
|
|
|
public abstract MyStatus7 MyStatus { get; }
|
|
|
|
public abstract FieldMoveModelSave7 Overworld { get; }
|
|
|
|
public abstract Situation7 Situation { get; }
|
|
|
|
public abstract ConfigSave7 Config { get; }
|
|
|
|
public abstract GameTime7 GameTime { get; }
|
|
|
|
public abstract Misc7 Misc { get; }
|
|
|
|
public abstract Zukan7 Zukan { get; }
|
|
|
|
public abstract BoxLayout7 BoxLayout { get; }
|
|
|
|
public abstract BattleTree7 BattleTree { get; }
|
|
|
|
public abstract ResortSave7 ResortSave { get; }
|
|
|
|
public abstract FieldMenu7 FieldMenu { get; }
|
|
|
|
public abstract FashionBlock7 Fashion { get; }
|
|
|
|
public abstract HallOfFame7 Fame { get; }
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
// Configuration
|
|
|
|
protected override int SIZE_STORED => PokeCrypto.SIZE_6STORED;
|
|
|
|
protected override int SIZE_PARTY => PokeCrypto.SIZE_6PARTY;
|
2023-01-22 04:02:33 +00:00
|
|
|
public override PK7 BlankPKM => new();
|
2022-06-18 18:04:24 +00:00
|
|
|
public override Type PKMType => typeof(PK7);
|
|
|
|
|
|
|
|
public override int BoxCount => 32;
|
2023-09-28 05:15:59 +00:00
|
|
|
public override int MaxEV => EffortValues.Max252;
|
2024-02-23 03:20:54 +00:00
|
|
|
public override byte Generation => 7;
|
2022-06-18 18:04:24 +00:00
|
|
|
public override EntityContext Context => EntityContext.Gen7;
|
|
|
|
protected override int GiftCountMax => 48;
|
|
|
|
protected override int GiftFlagMax => 0x100 * 8;
|
|
|
|
public abstract int EventFlagCount { get; }
|
|
|
|
public int EventWorkCount => 1000;
|
|
|
|
private int EventWork => AllBlocks[05].Offset;
|
|
|
|
private int EventFlag => EventWork + (EventWorkCount * 2); // After Event Const (u16)*n
|
2022-11-25 01:42:17 +00:00
|
|
|
public override int MaxStringLengthOT => 12;
|
|
|
|
public override int MaxStringLengthNickname => 12;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
public override int MaxBallID => Legal.MaxBallID_7; // 26
|
2024-02-23 03:20:54 +00:00
|
|
|
public override GameVersion MaxGameID => Legal.MaxGameID_7;
|
2023-01-22 04:02:33 +00:00
|
|
|
protected override PK7 GetPKM(byte[] data) => new(data);
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override byte[] DecryptPKM(byte[] data) => PokeCrypto.DecryptArray6(data);
|
|
|
|
|
|
|
|
// Feature Overrides
|
|
|
|
|
|
|
|
// Blocks & Offsets
|
|
|
|
private const int MemeCryptoBlock = 36;
|
|
|
|
|
|
|
|
protected void ClearMemeCrypto()
|
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
// The MemeCrypto block is always zero -- they could have hidden a secret inside it, but they didn't.
|
|
|
|
Data.AsSpan(AllBlocks[MemeCryptoBlock].Offset + 0x100, MemeCrypto.SaveFileSignatureLength).Clear();
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2016-10-27 05:06:03 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override byte[] GetFinalData()
|
|
|
|
{
|
|
|
|
BoxLayout.SaveBattleTeams();
|
|
|
|
SetChecksums();
|
2023-01-22 04:02:33 +00:00
|
|
|
|
|
|
|
// Applying the MemeCrypto signature will invalidate the checksum for that block.
|
|
|
|
// This logic is not set up to revert that block after returning, so just return a copy of our data.
|
|
|
|
var result = (byte[])Data.Clone();
|
|
|
|
MemeCrypto.SignInPlace(result);
|
2022-06-18 18:04:24 +00:00
|
|
|
return result;
|
|
|
|
}
|
2018-05-12 15:13:39 +00:00
|
|
|
|
2024-02-23 03:20:54 +00:00
|
|
|
public override bool IsVersionValid() =>
|
|
|
|
Version is GameVersion.SN or GameVersion.MN or GameVersion.US or GameVersion.UM;
|
2018-09-15 05:37:47 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public sealed override string GetString(ReadOnlySpan<byte> data) => StringConverter7.GetString(data);
|
2016-10-20 01:19:01 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public sealed override int SetString(Span<byte> destBuffer, ReadOnlySpan<char> value, int maxLength, StringConverterOption option)
|
|
|
|
{
|
|
|
|
return StringConverter7.SetString(destBuffer, value, maxLength, Language, option);
|
|
|
|
}
|
2018-09-27 02:06:05 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Player Information
|
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; }
|
2024-02-23 03:20:54 +00:00
|
|
|
public override GameVersion Version { get => (GameVersion)MyStatus.Game; set => MyStatus.Game = (byte)value; }
|
|
|
|
public override byte Gender { get => MyStatus.Gender; set => MyStatus.Gender = value; }
|
2022-06-18 18:04:24 +00:00
|
|
|
public int GameSyncIDSize => MyStatus7.GameSyncIDSize; // 64 bits
|
|
|
|
public string GameSyncID { get => MyStatus.GameSyncID; set => MyStatus.GameSyncID = value; }
|
|
|
|
public byte Region { get => MyStatus.Region; set => MyStatus.Region = value; }
|
|
|
|
public byte Country { get => MyStatus.Country; set => MyStatus.Country = value; }
|
|
|
|
public byte ConsoleRegion { get => MyStatus.ConsoleRegion; set => MyStatus.ConsoleRegion = value; }
|
|
|
|
public override int Language { get => MyStatus.Language; set => MyStatus.Language = value; }
|
|
|
|
public override string OT { get => MyStatus.OT; set => MyStatus.OT = value; }
|
|
|
|
public int MultiplayerSpriteID { get => MyStatus.MultiplayerSpriteID; set => MyStatus.MultiplayerSpriteID = value; }
|
|
|
|
public override uint Money { get => Misc.Money; set => Misc.Money = value; }
|
|
|
|
|
|
|
|
public override int PlayedHours { get => Played.PlayedHours; set => Played.PlayedHours = value; }
|
|
|
|
public override int PlayedMinutes { get => Played.PlayedMinutes; set => Played.PlayedMinutes = value; }
|
|
|
|
public override int PlayedSeconds { get => Played.PlayedSeconds; set => Played.PlayedSeconds = value; }
|
|
|
|
public override uint SecondsToStart { get => GameTime.SecondsToStart; set => GameTime.SecondsToStart = value; }
|
|
|
|
public override uint SecondsToFame { get => GameTime.SecondsToFame; set => GameTime.SecondsToFame = value; }
|
|
|
|
|
|
|
|
// Stat Records
|
|
|
|
public int RecordCount => 200;
|
|
|
|
public int GetRecord(int recordID) => Records.GetRecord(recordID);
|
|
|
|
public void SetRecord(int recordID, int value) => Records.SetRecord(recordID, value);
|
|
|
|
public int GetRecordMax(int recordID) => Records.GetRecordMax(recordID);
|
|
|
|
public int GetRecordOffset(int recordID) => Records.GetRecordOffset(recordID);
|
|
|
|
|
|
|
|
// Inventory
|
|
|
|
public override IReadOnlyList<InventoryPouch> Inventory { get => Items.Inventory; set => Items.Inventory = value; }
|
|
|
|
|
|
|
|
// Storage
|
|
|
|
public override int GetPartyOffset(int slot) => Party + (SIZE_PARTY * slot);
|
|
|
|
public override int GetBoxOffset(int box) => Box + (SIZE_STORED * box * 30);
|
|
|
|
protected override int GetBoxWallpaperOffset(int box) => BoxLayout.GetBoxWallpaperOffset(box);
|
|
|
|
public override int GetBoxWallpaper(int box) => BoxLayout.GetBoxWallpaper(box);
|
|
|
|
public override void SetBoxWallpaper(int box, int value) => BoxLayout.SetBoxWallpaper(box, value);
|
|
|
|
public override string GetBoxName(int box) => BoxLayout[box];
|
2023-01-22 04:02:33 +00:00
|
|
|
public override void SetBoxName(int box, ReadOnlySpan<char> value) => BoxLayout.SetBoxName(box, value);
|
2022-06-18 18:04:24 +00:00
|
|
|
public override int CurrentBox { get => BoxLayout.CurrentBox; set => BoxLayout.CurrentBox = value; }
|
|
|
|
public override int BoxesUnlocked { get => BoxLayout.BoxesUnlocked; set => BoxLayout.BoxesUnlocked = value; }
|
|
|
|
public override byte[] BoxFlags { get => BoxLayout.BoxFlags; set => BoxLayout.BoxFlags = value; }
|
|
|
|
|
|
|
|
protected override void SetPKM(PKM pk, bool isParty = false)
|
|
|
|
{
|
|
|
|
PK7 pk7 = (PK7)pk;
|
|
|
|
// Apply to this Save File
|
2024-02-23 23:01:36 +00:00
|
|
|
pk7.UpdateHandler(this);
|
2018-09-15 05:37:47 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
pk7.FormArgumentElapsed = pk7.FormArgumentMaximum = 0;
|
|
|
|
pk7.FormArgumentRemain = (byte)GetFormArgument(pk);
|
2018-09-15 05:37:47 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
pk.RefreshChecksum();
|
2024-02-23 19:20:59 +00:00
|
|
|
if (SetUpdateRecords != PKMImportSetting.Skip)
|
|
|
|
AddCountAcquired(pk);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void AddCountAcquired(PKM pk)
|
|
|
|
{
|
|
|
|
Records.AddRecord(pk.WasEgg ? 008 : 006); // egg, capture
|
|
|
|
if (pk.CurrentHandler == 1)
|
|
|
|
Records.AddRecord(011); // trade
|
|
|
|
if (!pk.WasEgg)
|
2016-10-20 01:19:01 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
Records.AddRecord(004); // wild encounters
|
|
|
|
Records.AddRecord(042); // balls used
|
2016-10-20 01:19:01 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static uint GetFormArgument(PKM pk)
|
|
|
|
{
|
|
|
|
if (pk.Form == 0)
|
|
|
|
return 0;
|
|
|
|
// Gen7 allows forms to be stored in the box with the current duration & form
|
2023-12-04 04:13:20 +00:00
|
|
|
// Just cap out the form duration anyway
|
2022-06-18 18:04:24 +00:00
|
|
|
return pk.Species switch
|
2018-12-05 06:59:28 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
(int)Species.Furfrou => 5u, // Furfrou
|
|
|
|
(int)Species.Hoopa => 3u, // Hoopa
|
|
|
|
_ => 0u,
|
|
|
|
};
|
|
|
|
}
|
2016-10-29 18:32:21 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override void SetDex(PKM pk) => Zukan.SetDex(pk);
|
2022-08-27 06:43:36 +00:00
|
|
|
public override bool GetCaught(ushort species) => Zukan.GetCaught(species);
|
|
|
|
public override bool GetSeen(ushort species) => Zukan.GetSeen(species);
|
2018-09-15 05:37:47 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public override int PartyCount
|
|
|
|
{
|
|
|
|
get => Data[Party + (6 * SIZE_PARTY)];
|
|
|
|
protected set => Data[Party + (6 * SIZE_PARTY)] = (byte)value;
|
|
|
|
}
|
2017-11-13 05:28:34 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public override StorageSlotSource GetSlotFlags(int index)
|
|
|
|
{
|
|
|
|
int team = Array.IndexOf(TeamSlots, index);
|
|
|
|
if (team < 0)
|
|
|
|
return StorageSlotSource.None;
|
|
|
|
|
|
|
|
team /= 6;
|
|
|
|
var result = (StorageSlotSource)((int)StorageSlotSource.BattleTeam1 << team);
|
|
|
|
if (BoxLayout.GetIsTeamLocked(team))
|
|
|
|
result |= StorageSlotSource.Locked;
|
|
|
|
return result;
|
|
|
|
}
|
2022-04-10 01:12:57 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private int FusedCount => this is SAV7USUM ? 3 : 1;
|
|
|
|
|
|
|
|
public int GetFusedSlotOffset(int slot)
|
|
|
|
{
|
|
|
|
if ((uint)slot >= FusedCount)
|
|
|
|
return -1;
|
|
|
|
return AllBlocks[08].Offset + (PokeCrypto.SIZE_6PARTY * slot); // 0x104*slot
|
|
|
|
}
|
2022-04-10 01:12:57 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public override int DaycareSeedSize => Daycare7.DaycareSeedSize; // 128 bits
|
|
|
|
public override int GetDaycareSlotOffset(int loc, int slot) => Daycare.GetDaycareSlotOffset(slot);
|
|
|
|
public override bool? IsDaycareOccupied(int loc, int slot) => Daycare.GetIsOccupied(slot);
|
|
|
|
public override string GetDaycareRNGSeed(int loc) => Daycare.RNGSeed;
|
|
|
|
public override bool? IsDaycareHasEgg(int loc) => Daycare.HasEgg;
|
|
|
|
public override void SetDaycareOccupied(int loc, int slot, bool occupied) => Daycare.SetOccupied(slot, occupied);
|
|
|
|
public override void SetDaycareRNGSeed(int loc, string seed) => Daycare.RNGSeed = seed;
|
|
|
|
public override void SetDaycareHasEgg(int loc, bool hasEgg) => Daycare.HasEgg = hasEgg;
|
|
|
|
|
|
|
|
protected override bool[] MysteryGiftReceivedFlags { get => MysteryGift.MysteryGiftReceivedFlags; set => MysteryGift.MysteryGiftReceivedFlags = value; }
|
|
|
|
protected override DataMysteryGift[] MysteryGiftCards { get => MysteryGift.MysteryGiftCards; set => MysteryGift.MysteryGiftCards = value; }
|
|
|
|
public bool GetEventFlag(int flagNumber)
|
|
|
|
{
|
|
|
|
if ((uint)flagNumber >= EventFlagCount)
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(flagNumber), $"Event Flag to get ({flagNumber}) is greater than max ({EventFlagCount}).");
|
|
|
|
return GetFlag(EventFlag + (flagNumber >> 3), flagNumber & 7);
|
2016-10-20 01:19:01 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
public void SetEventFlag(int flagNumber, bool value)
|
|
|
|
{
|
|
|
|
if ((uint)flagNumber >= EventFlagCount)
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(flagNumber), $"Event Flag to set ({flagNumber}) is greater than max ({EventFlagCount}).");
|
|
|
|
SetFlag(EventFlag + (flagNumber >> 3), flagNumber & 7, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ushort GetWork(int index) => ReadUInt16LittleEndian(Data.AsSpan(EventWork + (index * 2)));
|
|
|
|
public void SetWork(int index, ushort value) => WriteUInt16LittleEndian(Data.AsSpan(EventWork)[(index * 2)..], value);
|
2016-10-20 01:19:01 +00:00
|
|
|
}
|