2016-10-01 02:05:35 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
namespace PKHeX.Core
|
2016-10-01 02:05:35 +00:00
|
|
|
|
{
|
|
|
|
|
public class StrategyMemo
|
|
|
|
|
{
|
|
|
|
|
private readonly bool XD;
|
|
|
|
|
private const int SIZE_ENTRY = 12;
|
|
|
|
|
private readonly List<StrategyMemoEntry> Entries = new List<StrategyMemoEntry>();
|
|
|
|
|
private StrategyMemoEntry this[int Species] => Entries.FirstOrDefault(e => e.Species == Species);
|
2017-08-07 00:20:04 +00:00
|
|
|
|
private readonly byte[] _unk;
|
2016-10-01 02:05:35 +00:00
|
|
|
|
|
|
|
|
|
public StrategyMemo(byte[] input, int offset, bool xd)
|
|
|
|
|
{
|
|
|
|
|
XD = xd;
|
|
|
|
|
int count = BigEndian.ToInt16(input, offset);
|
|
|
|
|
if (count > 500)
|
|
|
|
|
count = 500;
|
2017-08-07 00:20:04 +00:00
|
|
|
|
_unk = input.Skip(offset + 2).Take(2).ToArray();
|
2016-10-01 02:05:35 +00:00
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
|
{
|
|
|
|
|
byte[] data = new byte[SIZE_ENTRY];
|
|
|
|
|
Array.Copy(input, 4 + offset + SIZE_ENTRY * i, data, 0, SIZE_ENTRY);
|
|
|
|
|
Entries.Add(new StrategyMemoEntry(XD, data));
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-07 00:20:04 +00:00
|
|
|
|
public byte[] FinalData => BigEndian.GetBytes((short)Entries.Count).Concat(_unk) // count followed by populated entries
|
|
|
|
|
.Concat(Entries.SelectMany(entry => entry.Data)).ToArray();
|
2016-10-01 02:05:35 +00:00
|
|
|
|
|
|
|
|
|
public StrategyMemoEntry GetEntry(int Species)
|
|
|
|
|
{
|
|
|
|
|
return this[Species] ?? new StrategyMemoEntry(XD);
|
|
|
|
|
}
|
|
|
|
|
public void SetEntry(StrategyMemoEntry entry)
|
|
|
|
|
{
|
2017-04-23 16:18:42 +00:00
|
|
|
|
int index = Entries.FindIndex(ent => ent.Species == entry.Species);
|
2016-10-01 02:05:35 +00:00
|
|
|
|
if (index > 0)
|
|
|
|
|
Entries[index] = entry;
|
|
|
|
|
else
|
|
|
|
|
Entries.Add(entry);
|
|
|
|
|
}
|
|
|
|
|
public class StrategyMemoEntry
|
|
|
|
|
{
|
|
|
|
|
public readonly byte[] Data;
|
|
|
|
|
private readonly bool XD;
|
|
|
|
|
public StrategyMemoEntry(bool xd, byte[] data = null)
|
|
|
|
|
{
|
|
|
|
|
Data = data ?? new byte[SIZE_ENTRY];
|
|
|
|
|
XD = xd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Species
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
int val = BigEndian.ToUInt16(Data, 0) & 0x1FF;
|
2017-08-01 06:03:51 +00:00
|
|
|
|
return SpeciesConverter.GetG4Species(val);
|
2016-10-01 02:05:35 +00:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2017-08-01 06:03:51 +00:00
|
|
|
|
value = SpeciesConverter.GetG3Species(value);
|
2016-10-01 02:05:35 +00:00
|
|
|
|
int cval = BigEndian.ToUInt16(Data, 0);
|
|
|
|
|
cval &= 0xE00; value &= 0x1FF; cval |= value;
|
|
|
|
|
BigEndian.GetBytes((ushort)cval).CopyTo(Data, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-13 03:32:36 +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
|
|
|
|
|
public int SID { get => BigEndian.ToUInt16(Data, 4); set => BigEndian.GetBytes((ushort)value).CopyTo(Data, 4); }
|
|
|
|
|
public int TID { get => BigEndian.ToUInt16(Data, 6); set => BigEndian.GetBytes((ushort)value).CopyTo(Data, 6); }
|
|
|
|
|
public uint PID { get => BigEndian.ToUInt32(Data, 8); set => BigEndian.GetBytes(value).CopyTo(Data, 8); }
|
2016-10-01 02:05:35 +00:00
|
|
|
|
|
|
|
|
|
public bool Seen
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (XD) return !Flag1;
|
|
|
|
|
return Species != 0;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (XD)
|
|
|
|
|
Flag1 = !value;
|
|
|
|
|
else if (!value)
|
|
|
|
|
new byte[SIZE_ENTRY].CopyTo(Data, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public bool Owned
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (XD) return false;
|
|
|
|
|
return Flag0 | Flag1 == false;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (XD) return;
|
|
|
|
|
if (!value)
|
|
|
|
|
Flag1 = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsEmpty => Species == 0;
|
|
|
|
|
public bool Matches(int species, uint pid, int tid, int sid) => Species == species && PID == pid && TID == tid && SID == sid;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|