2022-08-04 16:53:15 +00:00
|
|
|
using System;
|
2016-10-01 02:05:35 +00:00
|
|
|
using System.Collections.Generic;
|
2022-01-03 05:35:59 +00:00
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
2016-10-01 02:05:35 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
2016-10-01 02:05:35 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public sealed class StrategyMemo
|
|
|
|
{
|
|
|
|
private readonly bool XD;
|
|
|
|
public const int SIZE_ENTRY = 12;
|
|
|
|
private readonly List<StrategyMemoEntry> Entries;
|
|
|
|
public const int MAX_COUNT = 500;
|
2023-01-22 04:02:33 +00:00
|
|
|
private StrategyMemoEntry? this[ushort Species] => Entries.Find(e => e.Species == Species);
|
2022-06-18 18:04:24 +00:00
|
|
|
private readonly ushort _unk;
|
PKHeX.Core Nullable cleanup (#2401)
* Handle some nullable cases
Refactor MysteryGift into a second abstract class (backed by a byte array, or fake data)
Make some classes have explicit constructors instead of { } initialization
* Handle bits more obviously without null
* Make SaveFile.BAK explicitly readonly again
* merge constructor methods to have readonly fields
* Inline some properties
* More nullable handling
* Rearrange box actions
define straightforward classes to not have any null properties
* Make extrabyte reference array immutable
* Move tooltip creation to designer
* Rearrange some logic to reduce nesting
* Cache generated fonts
* Split mystery gift album purpose
* Handle more tooltips
* Disallow null setters
* Don't capture RNG object, only type enum
* Unify learnset objects
Now have readonly properties which are never null
don't new() empty learnsets (>800 Learnset objects no longer created,
total of 2400 objects since we also new() a move & level array)
optimize g1/2 reader for early abort case
* Access rewrite
Initialize blocks in a separate object, and get via that object
removes a couple hundred "might be null" warnings since blocks are now readonly getters
some block references have been relocated, but interfaces should expose all that's needed
put HoF6 controls in a groupbox, and disable
* Readonly personal data
* IVs non nullable for mystery gift
* Explicitly initialize forced encounter moves
* Make shadow objects readonly & non-null
Put murkrow fix in binary data resource, instead of on startup
* Assign dex form fetch on constructor
Fixes legality parsing edge cases
also handle cxd parse for valid; exit before exception is thrown in FrameGenerator
* Remove unnecessary null checks
* Keep empty value until init
SetPouch sets the value to an actual one during load, but whatever
* Readonly team lock data
* Readonly locks
Put locked encounters at bottom (favor unlocked)
* Mail readonly data / offset
Rearrange some call flow and pass defaults
Add fake classes for SaveDataEditor mocking
Always party size, no need to check twice in stat editor
use a fake save file as initial data for savedata editor, and for
gamedata (wow i found a usage)
constrain eventwork editor to struct variable types (uint, int, etc),
thus preventing null assignment errors
2019-10-17 01:47:31 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
public StrategyMemo(bool xd = true) : this(stackalloc byte[4], xd) { }
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
public StrategyMemo(Span<byte> block, bool xd)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
XD = xd;
|
2023-01-22 04:02:33 +00:00
|
|
|
int count = ReadUInt16BigEndian(block);
|
2022-06-18 18:04:24 +00:00
|
|
|
if (count > MAX_COUNT)
|
|
|
|
count = MAX_COUNT;
|
2023-01-22 04:02:33 +00:00
|
|
|
_unk = ReadUInt16BigEndian(block[2..]);
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
Entries = new List<StrategyMemoEntry>(count);
|
|
|
|
for (int i = 0; i < count; i++)
|
2019-02-17 18:42:43 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
var entry = Read(block, i);
|
2022-06-18 18:04:24 +00:00
|
|
|
Entries.Add(entry);
|
2019-02-17 18:42:43 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-02-17 18:42:43 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
private StrategyMemoEntry Read(Span<byte> block, int index)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
var ofs = 4 + (SIZE_ENTRY * index);
|
|
|
|
var span = block.Slice(ofs, SIZE_ENTRY);
|
2022-06-18 18:04:24 +00:00
|
|
|
var data = span.ToArray();
|
|
|
|
return new StrategyMemoEntry(XD, data);
|
|
|
|
}
|
2016-10-01 02:05:35 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public byte[] Write()
|
|
|
|
{
|
|
|
|
var result = new byte[4 + (Entries.Count * SIZE_ENTRY)];
|
|
|
|
WriteInt16BigEndian(result.AsSpan(0), (short)Entries.Count);
|
|
|
|
WriteInt16BigEndian(result.AsSpan(2), (short)_unk);
|
|
|
|
|
|
|
|
var count = Math.Min(MAX_COUNT, Entries.Count);
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
Entries[i].Data.CopyTo(result, 4 + (i * SIZE_ENTRY));
|
|
|
|
return result;
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
public StrategyMemoEntry GetEntry(ushort Species)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
return this[Species] ?? new StrategyMemoEntry(XD);
|
2019-10-26 19:58:55 +00:00
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public void SetEntry(StrategyMemoEntry entry)
|
2019-10-26 19:58:55 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
int index = Entries.FindIndex(ent => ent.Species == entry.Species);
|
|
|
|
if (index >= 0)
|
|
|
|
Entries[index] = entry;
|
|
|
|
else
|
|
|
|
Entries.Add(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public sealed class StrategyMemoEntry
|
|
|
|
{
|
|
|
|
public readonly byte[] Data;
|
|
|
|
private readonly bool XD;
|
2019-10-26 19:58:55 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public StrategyMemoEntry(bool xd) : this(xd, new byte[StrategyMemo.SIZE_ENTRY]) { }
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public StrategyMemoEntry(bool xd, byte[] data)
|
|
|
|
{
|
|
|
|
Data = data;
|
|
|
|
XD = xd;
|
|
|
|
}
|
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
public ushort Species
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
get
|
2019-10-26 19:58:55 +00:00
|
|
|
{
|
2022-08-27 06:43:36 +00:00
|
|
|
var val = (ushort)(ReadUInt16BigEndian(Data.AsSpan(0)) & 0x1FF);
|
2023-01-22 04:02:33 +00:00
|
|
|
return SpeciesConverter.GetNational3(val);
|
2019-10-26 19:58:55 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
set
|
2019-10-26 19:58:55 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
var val = SpeciesConverter.GetInternal3(value);
|
2022-08-04 16:53:15 +00:00
|
|
|
var cval = ReadUInt16BigEndian(Data.AsSpan(0));
|
|
|
|
cval &= 0xE00; val &= 0x1FF; cval |= val;
|
|
|
|
WriteUInt16BigEndian(Data.AsSpan(0x00), cval);
|
2019-10-26 19:58:55 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private bool Flag0 { get => Data[0] >> 6 == 1; set { Data[0] &= 0xBF; if (value) Data[0] |= 0x40; } } // Unused
|
|
|
|
private bool Flag1 { get => Data[0] >> 7 == 1; set { Data[0] &= 0x7F; if (value) Data[0] |= 0x80; } } // Complete Entry
|
2023-01-22 04:02:33 +00:00
|
|
|
|
|
|
|
public uint ID32
|
|
|
|
{
|
|
|
|
get => ReadUInt32BigEndian(Data.AsSpan(0x04));
|
|
|
|
set => WriteUInt32BigEndian(Data.AsSpan(0x04), value);
|
|
|
|
}
|
|
|
|
public ushort SID16 { get => ReadUInt16BigEndian(Data.AsSpan(4)); set => WriteUInt16BigEndian(Data.AsSpan(4), value); }
|
|
|
|
public ushort TID16 { get => ReadUInt16BigEndian(Data.AsSpan(6)); set => WriteUInt16BigEndian(Data.AsSpan(6), value); }
|
2022-06-18 18:04:24 +00:00
|
|
|
public uint PID { get => ReadUInt32BigEndian(Data.AsSpan(8)); set => WriteUInt32BigEndian(Data.AsSpan(8), value); }
|
2016-10-01 02:05:35 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public bool Seen
|
|
|
|
{
|
|
|
|
get
|
2019-10-26 19:58:55 +00:00
|
|
|
{
|
2022-11-25 01:42:17 +00:00
|
|
|
if (XD)
|
|
|
|
return !Flag1;
|
2022-06-18 18:04:24 +00:00
|
|
|
return Species != 0;
|
2019-10-26 19:58:55 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
set
|
2019-10-26 19:58:55 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (XD)
|
|
|
|
Flag1 = !value;
|
|
|
|
else if (!value)
|
2022-11-25 01:42:17 +00:00
|
|
|
Data.AsSpan(0, StrategyMemo.SIZE_ENTRY).Clear();
|
2016-10-01 02:05:35 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-10-26 19:58:55 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public bool Owned
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2022-11-25 01:42:17 +00:00
|
|
|
if (XD)
|
|
|
|
return false;
|
2022-06-18 18:04:24 +00:00
|
|
|
return Flag0 || !Flag1;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
2022-11-25 01:42:17 +00:00
|
|
|
if (XD)
|
|
|
|
return;
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!value)
|
|
|
|
Flag1 = true;
|
|
|
|
}
|
2016-10-01 02:05:35 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
public bool IsEmpty => Species == 0;
|
2023-01-22 04:02:33 +00:00
|
|
|
public bool Matches(ushort species, uint pid, uint id32) => Species == species && PID == pid && ID32 == id32;
|
2016-10-01 02:05:35 +00:00
|
|
|
}
|