2021-03-16 06:51:58 +00:00
using System ;
2021-08-21 03:33:53 +00:00
using System.Collections.Generic ;
2022-01-03 05:35:59 +00:00
using static System . Buffers . Binary . BinaryPrimitives ;
2021-03-16 06:51:58 +00:00
namespace PKHeX.Core
{
2021-06-06 18:56:54 +00:00
/// <summary>
/// Generation 3 <see cref="SaveFile"/> object for <see cref="GameVersion.E"/>.
/// </summary>
/// <inheritdoc cref="SAV3" />
2021-03-28 01:58:51 +00:00
public sealed class SAV3E : SAV3 , IGen3Hoenn , IGen3Joyful , IGen3Wonder
2021-03-16 06:51:58 +00:00
{
// Configuration
protected override SaveFile CloneInternal ( ) = > new SAV3E ( Write ( ) ) ;
public override GameVersion Version { get = > GameVersion . E ; protected set { } }
public override PersonalTable Personal = > PersonalTable . E ;
protected override int EventFlagMax = > 8 * 300 ;
protected override int EventConstMax = > 0x100 ;
protected override int DaycareSlotSize = > SIZE_STORED + 0x3C ; // 0x38 mail + 4 exp
public override int DaycareSeedSize = > 8 ; // 32bit
protected override int EggEventFlag = > 0x86 ;
protected override int BadgeFlagStart = > 0x867 ;
public SAV3E ( byte [ ] data ) : base ( data ) = > Initialize ( ) ;
public SAV3E ( bool japanese = false ) : base ( japanese ) = > Initialize ( ) ;
private void Initialize ( )
{
// small
PokeDex = 0x18 ;
// large
EventFlag = 0x1270 ;
EventConst = 0x139C ;
DaycareOffset = 0x3030 ;
// storage
Box = 0 ;
}
#region Small
public override bool NationalDex
{
get = > PokedexNationalMagicRSE = = PokedexNationalUnlockRSE ;
set
{
2021-03-29 07:14:44 +00:00
PokedexMode = value ? ( byte ) 1 : ( byte ) 0 ; // mode
PokedexNationalMagicRSE = value ? PokedexNationalUnlockRSE : ( byte ) 0 ; // magic
2021-03-16 06:51:58 +00:00
SetEventFlag ( 0x896 , value ) ;
SetEventConst ( 0x46 , PokedexNationalUnlockWorkRSE ) ;
}
}
public override uint SecurityKey
{
2022-01-03 05:35:59 +00:00
get = > ReadUInt32LittleEndian ( Small . AsSpan ( 0xAC ) ) ;
set = > WriteUInt32LittleEndian ( Small . AsSpan ( 0xAC ) , value ) ;
2021-03-16 06:51:58 +00:00
}
public RTC3 ClockInitial
{
get = > new ( GetData ( Small , 0x98 , RTC3 . Size ) ) ;
set = > SetData ( Small , value . Data , 0x98 ) ;
}
public RTC3 ClockElapsed
{
get = > new ( GetData ( Small , 0xA0 , RTC3 . Size ) ) ;
set = > SetData ( Small , value . Data , 0xA0 ) ;
}
2022-01-03 05:35:59 +00:00
public ushort JoyfulJumpInRow { get = > ReadUInt16LittleEndian ( Small . AsSpan ( 0x1FC ) ) ; set = > WriteUInt16LittleEndian ( Small . AsSpan ( 0x1FC ) , Math . Min ( ( ushort ) 9999 , value ) ) ; }
2021-03-16 06:51:58 +00:00
// u16 field2;
2022-01-03 05:35:59 +00:00
public ushort JoyfulJump5InRow { get = > ReadUInt16LittleEndian ( Small . AsSpan ( 0x200 ) ) ; set = > WriteUInt16LittleEndian ( Small . AsSpan ( 0x200 ) , Math . Min ( ( ushort ) 9999 , value ) ) ; }
public ushort JoyfulJumpGamesMaxPlayers { get = > ReadUInt16LittleEndian ( Small . AsSpan ( 0x202 ) ) ; set = > WriteUInt16LittleEndian ( Small . AsSpan ( 0x202 ) , Math . Min ( ( ushort ) 9999 , value ) ) ; }
2021-03-16 06:51:58 +00:00
// u32 field8;
2022-01-03 05:35:59 +00:00
public uint JoyfulJumpScore { get = > ReadUInt16LittleEndian ( Small . AsSpan ( 0x208 ) ) ; set = > WriteUInt32LittleEndian ( Small . AsSpan ( 0x208 ) , Math . Min ( 9999 , value ) ) ; }
2021-03-17 01:48:33 +00:00
2022-01-03 05:35:59 +00:00
public uint JoyfulBerriesScore { get = > ReadUInt16LittleEndian ( Small . AsSpan ( 0x20C ) ) ; set = > WriteUInt32LittleEndian ( Small . AsSpan ( 0x20C ) , Math . Min ( 9999 , value ) ) ; }
public ushort JoyfulBerriesInRow { get = > ReadUInt16LittleEndian ( Small . AsSpan ( 0x210 ) ) ; set = > WriteUInt16LittleEndian ( Small . AsSpan ( 0x210 ) , Math . Min ( ( ushort ) 9999 , value ) ) ; }
public ushort JoyfulBerries5InRow { get = > ReadUInt16LittleEndian ( Small . AsSpan ( 0x212 ) ) ; set = > WriteUInt16LittleEndian ( Small . AsSpan ( 0x212 ) , Math . Min ( ( ushort ) 9999 , value ) ) ; }
2021-03-16 06:51:58 +00:00
public uint BP
{
2022-01-03 05:35:59 +00:00
get = > ReadUInt16LittleEndian ( Small . AsSpan ( 0xEB8 ) ) ;
2021-03-16 06:51:58 +00:00
set
{
if ( value > 9999 )
value = 9999 ;
2022-01-03 05:35:59 +00:00
WriteUInt16LittleEndian ( Small . AsSpan ( 0xEB8 ) , ( ushort ) value ) ;
2021-03-16 06:51:58 +00:00
}
}
public uint BPEarned
{
2022-01-03 05:35:59 +00:00
get = > ReadUInt16LittleEndian ( Small . AsSpan ( 0xEBA ) ) ;
2021-03-16 06:51:58 +00:00
set
{
if ( value > 65535 )
value = 65535 ;
2022-01-03 05:35:59 +00:00
WriteUInt16LittleEndian ( Small . AsSpan ( 0xEBA ) , ( ushort ) value ) ;
2021-03-16 06:51:58 +00:00
}
}
#endregion
#region Large
2021-03-17 01:48:33 +00:00
public override int PartyCount { get = > Large [ 0x234 ] ; protected set = > Large [ 0x234 ] = ( byte ) value ; }
public override int GetPartyOffset ( int slot ) = > 0x238 + ( SIZE_PARTY * slot ) ;
2021-03-16 06:51:58 +00:00
public override uint Money
{
2022-01-03 05:35:59 +00:00
get = > ReadUInt32LittleEndian ( Large . AsSpan ( 0x0490 ) ) ^ SecurityKey ;
set = > WriteUInt32LittleEndian ( Large . AsSpan ( 0x0490 ) , value ^ SecurityKey ) ;
2021-03-16 06:51:58 +00:00
}
public override uint Coin
{
2022-01-03 05:35:59 +00:00
get = > ( ushort ) ( ReadUInt16LittleEndian ( Large . AsSpan ( 0x0494 ) ) ^ SecurityKey ) ;
set = > WriteUInt16LittleEndian ( Large . AsSpan ( 0x0494 ) , ( ushort ) ( value ^ SecurityKey ) ) ;
2021-03-16 06:51:58 +00:00
}
private const int OFS_PCItem = 0x0498 ;
private const int OFS_PouchHeldItem = 0x0560 ;
private const int OFS_PouchKeyItem = 0x05D8 ;
private const int OFS_PouchBalls = 0x0650 ;
private const int OFS_PouchTMHM = 0x0690 ;
private const int OFS_PouchBerry = 0x0790 ;
protected override InventoryPouch3 [ ] GetItems ( )
{
const int max = 99 ;
2021-04-01 21:14:47 +00:00
var PCItems = ArrayUtil . ConcatAll ( Legal . Pouch_Items_RS , Legal . Pouch_Key_E , Legal . Pouch_Ball_RS , Legal . Pouch_TMHM_RS , Legal . Pouch_Berries_RS ) ;
2021-03-16 06:51:58 +00:00
return new InventoryPouch3 [ ]
{
new ( InventoryType . Items , Legal . Pouch_Items_RS , max , OFS_PouchHeldItem , ( OFS_PouchKeyItem - OFS_PouchHeldItem ) / 4 ) ,
new ( InventoryType . KeyItems , Legal . Pouch_Key_E , 1 , OFS_PouchKeyItem , ( OFS_PouchBalls - OFS_PouchKeyItem ) / 4 ) ,
new ( InventoryType . Balls , Legal . Pouch_Ball_RS , max , OFS_PouchBalls , ( OFS_PouchTMHM - OFS_PouchBalls ) / 4 ) ,
2021-04-01 21:14:47 +00:00
new ( InventoryType . TMHMs , Legal . Pouch_TMHM_RS , max , OFS_PouchTMHM , ( OFS_PouchBerry - OFS_PouchTMHM ) / 4 ) ,
2021-03-16 06:51:58 +00:00
new ( InventoryType . Berries , Legal . Pouch_Berries_RS , 999 , OFS_PouchBerry , 46 ) ,
new ( InventoryType . PCItems , PCItems , 999 , OFS_PCItem , ( OFS_PouchHeldItem - OFS_PCItem ) / 4 ) ,
} ;
}
public PokeBlock3Case PokeBlocks
{
get = > new ( Large , 0x848 ) ;
set = > SetData ( Large , value . Write ( ) , 0x848 ) ;
}
protected override int SeenOffset2 = > 0x988 ;
public DecorationInventory3 Decorations
{
get = > Large . Slice ( 0x2734 , DecorationInventory3 . SIZE ) . ToStructure < DecorationInventory3 > ( ) ;
set = > SetData ( Large , value . ToBytes ( ) , 0x2734 ) ;
}
2021-08-21 02:52:31 +00:00
public Swarm3 Swarm
{
get = > Large . Slice ( 0x2B90 , Swarm3 . SIZE ) . ToClass < Swarm3 > ( ) ;
set = > SetData ( Large , value . ToBytesClass ( ) , 0x2B90 ) ;
}
2021-08-21 03:33:53 +00:00
public IReadOnlyList < Swarm3 > DefaultSwarms = > Swarm3Details . Swarms_E ;
public int SwarmIndex
{
get = > Array . FindIndex ( Swarm3Details . Swarms_E , z = > z . MapNum = = Swarm . MapNum ) ;
set
{
var arr = DefaultSwarms ;
Swarm = ( uint ) value > = arr . Count ? new Swarm3 ( ) : arr [ value ] ;
}
}
2021-03-16 06:51:58 +00:00
protected override int MailOffset = > 0x2BE0 ;
protected override int GetDaycareEXPOffset ( int slot ) = > GetDaycareSlotOffset ( 0 , slot + 1 ) - 4 ; // @ end of each pkm slot
2022-01-03 05:35:59 +00:00
public override string GetDaycareRNGSeed ( int loc ) = > ReadUInt32LittleEndian ( Large . AsSpan ( GetDaycareSlotOffset ( 0 , 2 ) ) ) . ToString ( "X8" ) ; // after the 2 slots, before the step counter
public override void SetDaycareRNGSeed ( int loc , string seed ) = > WriteUInt32LittleEndian ( Large . AsSpan ( GetDaycareEXPOffset ( 2 ) ) , Util . GetHexValue ( seed ) ) ;
2021-03-16 06:51:58 +00:00
2021-08-23 00:15:08 +00:00
protected override int ExternalEventData = > 0x31B3 ;
2021-03-16 06:51:58 +00:00
#region eBerry
private const int OFFSET_EBERRY = 0x31F8 ;
private const int SIZE_EBERRY = 0x134 ;
public byte [ ] GetEReaderBerry ( ) = > Large . Slice ( OFFSET_EBERRY , SIZE_EBERRY ) ;
2022-01-03 05:35:59 +00:00
public void SetEReaderBerry ( ReadOnlySpan < byte > data ) = > data . CopyTo ( Large . AsSpan ( OFFSET_EBERRY ) ) ;
2021-03-16 06:51:58 +00:00
2022-01-03 05:35:59 +00:00
public override string EBerryName = > GetString ( Large . AsSpan ( OFFSET_EBERRY , 7 ) ) ;
2021-03-16 06:51:58 +00:00
public override bool IsEBerryEngima = > Large [ OFFSET_EBERRY ] is 0 or 0xFF ;
#endregion
2021-03-28 01:58:51 +00:00
public int WonderOffset = > WonderNewsOffset ;
private const int WonderNewsOffset = 0x322C ;
private const int WonderCardOffset = WonderNewsOffset + WonderNews3 . SIZE ;
private const int WonderCardExtraOffset = WonderCardOffset + WonderCard3 . SIZE ;
public WonderNews3 WonderNews { get = > new ( Large . Slice ( WonderNewsOffset , WonderNews3 . SIZE ) ) ; set = > SetData ( Large , value . Data , WonderOffset ) ; }
public WonderCard3 WonderCard { get = > new ( Large . Slice ( WonderCardOffset , WonderCard3 . SIZE ) ) ; set = > SetData ( Large , value . Data , WonderCardOffset ) ; }
public WonderCard3Extra WonderCardExtra { get = > new ( Large . Slice ( WonderCardExtraOffset , WonderCard3Extra . SIZE ) ) ; set = > SetData ( Large , value . Data , WonderCardExtraOffset ) ; }
// 0x338: 4 easy chat words
// 0x340: news MENewsJisanStruct
// 0x344: uint[5], uint[5] tracking?
public override MysteryEvent3 MysteryEvent
{
get = > new ( Large . Slice ( 0x3728 , MysteryEvent3 . SIZE ) ) ;
set = > SetData ( Large , value . Data , 0x3728 ) ;
}
2021-03-16 06:51:58 +00:00
protected override int SeenOffset3 = > 0x3B24 ;
2021-09-07 03:46:41 +00:00
private const int Walda = 0x3D70 ;
2022-01-03 05:35:59 +00:00
public ushort WaldaBackgroundColor { get = > ReadUInt16LittleEndian ( Large . AsSpan ( Walda + 0 ) ) ; set = > WriteUInt16LittleEndian ( Large . AsSpan ( Walda + 0 ) , value ) ; }
public ushort WaldaForegroundColor { get = > ReadUInt16LittleEndian ( Large . AsSpan ( Walda + 2 ) ) ; set = > WriteUInt16LittleEndian ( Large . AsSpan ( Walda + 2 ) , value ) ; }
2021-09-07 03:46:41 +00:00
public byte WaldaIconID { get = > Large [ Walda + 0x14 ] ; set = > Large [ Walda + 0x14 ] = value ; }
public byte WaldaPatternID { get = > Large [ Walda + 0x15 ] ; set = > Large [ Walda + 0x15 ] = value ; }
public bool WaldaUnlocked { get = > Large [ Walda + 0x16 ] ! = 0 ; set = > Large [ Walda + 0x16 ] = ( byte ) ( value ? 1 : 0 ) ; }
2021-03-16 06:51:58 +00:00
#endregion
2021-03-28 06:22:56 +00:00
2021-03-29 07:14:44 +00:00
private const uint EXTRADATA_SENTINEL = 0x0000B39D ;
2021-03-28 06:22:56 +00:00
private const int OFS_BV = 31 * 0x1000 ; // last sector of the save
2022-01-03 05:35:59 +00:00
public bool HasBattleVideo = > Data . Length > SaveUtil . SIZE_G3RAWHALF & & ReadUInt32LittleEndian ( Data . AsSpan ( OFS_BV ) ) = = EXTRADATA_SENTINEL ;
2021-03-28 06:22:56 +00:00
public BV3 BattleVideo
{
2021-06-03 19:04:19 +00:00
get = > ! HasBattleVideo ? new BV3 ( ) : new BV3 ( Data . Slice ( OFS_BV + 4 , BV3 . SIZE ) ) ;
2021-03-28 06:22:56 +00:00
set = > SetData ( Data , value . Data , OFS_BV + 4 ) ;
}
2021-03-16 06:51:58 +00:00
}
}