2016-06-20 04:22:43 +00:00
|
|
|
|
using System;
|
2016-08-27 11:33:21 +00:00
|
|
|
|
using System.Linq;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
namespace PKHeX.Core
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
|
|
|
|
public enum InventoryType
|
|
|
|
|
{
|
|
|
|
|
Items,
|
|
|
|
|
KeyItems,
|
|
|
|
|
TMHMs,
|
|
|
|
|
Medicine,
|
|
|
|
|
Berries,
|
|
|
|
|
Balls,
|
|
|
|
|
BattleItems,
|
|
|
|
|
MailItems,
|
2016-10-28 02:53:19 +00:00
|
|
|
|
PCItems,
|
|
|
|
|
FreeSpace,
|
2016-10-20 01:19:01 +00:00
|
|
|
|
ZCrystals,
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
public class InventoryItem
|
|
|
|
|
{
|
2016-10-27 05:06:03 +00:00
|
|
|
|
public bool New;
|
2016-11-30 06:22:01 +00:00
|
|
|
|
public bool FreeSpace;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
public int Index, Count;
|
2017-12-28 00:36:24 +00:00
|
|
|
|
public InventoryItem Clone() => (InventoryItem) MemberwiseClone();
|
2016-12-10 04:12:37 +00:00
|
|
|
|
|
|
|
|
|
// Check Pouch Compatibility
|
|
|
|
|
public bool Valid(ushort[] LegalItems, bool HaX, int MaxItemID)
|
|
|
|
|
{
|
|
|
|
|
if (Index == 0)
|
|
|
|
|
return true;
|
|
|
|
|
if (Index <= MaxItemID)
|
|
|
|
|
return HaX || LegalItems.Contains((ushort)Index);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class InventoryPouch
|
|
|
|
|
{
|
|
|
|
|
public readonly InventoryType Type;
|
|
|
|
|
public readonly ushort[] LegalItems;
|
|
|
|
|
public readonly int MaxCount;
|
2016-08-27 11:33:21 +00:00
|
|
|
|
public int Count => Items.Count(it => it.Count > 0);
|
2016-07-01 04:53:24 +00:00
|
|
|
|
public uint SecurityKey { private get; set; } // = 0 // Gen3 Only
|
2016-06-20 04:22:43 +00:00
|
|
|
|
public InventoryItem[] Items;
|
2016-10-27 05:06:03 +00:00
|
|
|
|
|
|
|
|
|
private readonly int Offset;
|
|
|
|
|
private readonly int PouchDataSize;
|
|
|
|
|
private InventoryItem[] OriginalItems;
|
2018-05-12 15:13:39 +00:00
|
|
|
|
|
2016-06-20 04:22:43 +00:00
|
|
|
|
public InventoryPouch(InventoryType type, ushort[] legal, int maxcount, int offset, int size = -1)
|
|
|
|
|
{
|
|
|
|
|
Type = type;
|
|
|
|
|
LegalItems = legal;
|
|
|
|
|
MaxCount = maxcount;
|
|
|
|
|
Offset = offset;
|
|
|
|
|
PouchDataSize = size > -1 ? size : legal.Length;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-28 00:36:24 +00:00
|
|
|
|
public void GetPouch(byte[] Data)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
|
|
|
|
InventoryItem[] items = new InventoryItem[PouchDataSize];
|
|
|
|
|
for (int i = 0; i < items.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
items[i] = new InventoryItem
|
|
|
|
|
{
|
|
|
|
|
Index = BitConverter.ToUInt16(Data, Offset + i*4),
|
2016-07-01 04:53:24 +00:00
|
|
|
|
Count = BitConverter.ToUInt16(Data, Offset + i*4 + 2) ^ (ushort)SecurityKey
|
2016-06-20 04:22:43 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
Items = items;
|
|
|
|
|
}
|
2017-12-28 00:36:24 +00:00
|
|
|
|
public void SetPouch(byte[] Data)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
|
|
|
|
if (Items.Length != PouchDataSize)
|
|
|
|
|
throw new ArgumentException("Item array length does not match original pouch size.");
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < Items.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
BitConverter.GetBytes((ushort)Items[i].Index).CopyTo(Data, Offset + i*4);
|
2016-07-02 04:21:15 +00:00
|
|
|
|
BitConverter.GetBytes((ushort)((ushort)Items[i].Count ^ (ushort)SecurityKey)).CopyTo(Data, Offset + i*4 + 2);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-28 00:36:24 +00:00
|
|
|
|
public void GetPouch7(byte[] Data)
|
2016-10-27 05:06:03 +00:00
|
|
|
|
{
|
|
|
|
|
InventoryItem[] items = new InventoryItem[PouchDataSize];
|
|
|
|
|
for (int i = 0; i < items.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
// 10bit itemID
|
|
|
|
|
// 10bit count
|
|
|
|
|
// 12bit flags/reserved
|
|
|
|
|
uint val = BitConverter.ToUInt32(Data, Offset + i*4);
|
|
|
|
|
items[i] = new InventoryItem
|
|
|
|
|
{
|
|
|
|
|
Index = (int)(val & 0x3FF),
|
|
|
|
|
Count = (int)(val >> 10 & 0x3FF),
|
2016-11-30 06:30:14 +00:00
|
|
|
|
New = (val & 0x40000000) != 0, // 30th bit is "NEW"
|
2017-02-27 02:50:23 +00:00
|
|
|
|
FreeSpace = (val >> 20 & 0x3FF) != 0, // "FREE SPACE" sortIndex
|
2016-10-27 05:06:03 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
Items = items;
|
|
|
|
|
OriginalItems = Items.Select(i => i.Clone()).ToArray();
|
|
|
|
|
}
|
2017-12-28 00:36:24 +00:00
|
|
|
|
public void SetPouch7(byte[] Data, bool setNEW = false)
|
2016-10-27 05:06:03 +00:00
|
|
|
|
{
|
|
|
|
|
if (Items.Length != PouchDataSize)
|
|
|
|
|
throw new ArgumentException("Item array length does not match original pouch size.");
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < Items.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
// Build Item Value
|
|
|
|
|
uint val = 0;
|
|
|
|
|
val |= (uint)(Items[i].Index & 0x3FF);
|
2016-11-01 22:16:21 +00:00
|
|
|
|
val |= (uint)(Items[i].Count & 0x3FF) << 10;
|
2016-12-26 21:57:54 +00:00
|
|
|
|
if (setNEW)
|
|
|
|
|
Items[i].New |= OriginalItems.All(z => z.Index != Items[i].Index);
|
2016-10-27 05:06:03 +00:00
|
|
|
|
if (Items[i].New)
|
|
|
|
|
val |= 0x40000000;
|
2016-11-30 06:22:01 +00:00
|
|
|
|
if (Items[i].FreeSpace)
|
|
|
|
|
val |= 0x100000;
|
2016-11-09 15:33:59 +00:00
|
|
|
|
BitConverter.GetBytes(val).CopyTo(Data, Offset + i * 4);
|
2016-10-27 05:06:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public void GetPouchBigEndian(ref byte[] Data)
|
2016-09-26 23:15:40 +00:00
|
|
|
|
{
|
|
|
|
|
InventoryItem[] items = new InventoryItem[PouchDataSize];
|
|
|
|
|
for (int i = 0; i < items.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
items[i] = new InventoryItem
|
|
|
|
|
{
|
|
|
|
|
Index = BigEndian.ToUInt16(Data, Offset + i * 4),
|
|
|
|
|
Count = BigEndian.ToUInt16(Data, Offset + i * 4 + 2) ^ (ushort)SecurityKey
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
Items = items;
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public void SetPouchBigEndian(ref byte[] Data)
|
2016-09-26 23:15:40 +00:00
|
|
|
|
{
|
|
|
|
|
if (Items.Length != PouchDataSize)
|
|
|
|
|
throw new ArgumentException("Item array length does not match original pouch size.");
|
2016-08-28 10:18:22 +00:00
|
|
|
|
|
2016-09-26 23:15:40 +00:00
|
|
|
|
for (int i = 0; i < Items.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
BigEndian.GetBytes((ushort)Items[i].Index).CopyTo(Data, Offset + i * 4);
|
|
|
|
|
BigEndian.GetBytes((ushort)((ushort)Items[i].Count ^ (ushort)SecurityKey)).CopyTo(Data, Offset + i * 4 + 2);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-27 05:06:03 +00:00
|
|
|
|
|
2017-12-28 00:36:24 +00:00
|
|
|
|
public void GetPouchG1(byte[] Data)
|
2016-08-28 10:18:22 +00:00
|
|
|
|
{
|
|
|
|
|
InventoryItem[] items = new InventoryItem[PouchDataSize];
|
2016-09-04 01:57:54 +00:00
|
|
|
|
if (Type == InventoryType.TMHMs)
|
2016-08-28 10:18:22 +00:00
|
|
|
|
{
|
2016-09-04 01:57:54 +00:00
|
|
|
|
int slot = 0;
|
|
|
|
|
for (int i = 0; i < items.Length; i++)
|
2016-08-28 10:18:22 +00:00
|
|
|
|
{
|
2016-09-04 01:57:54 +00:00
|
|
|
|
if (Data[Offset + i] != 0)
|
|
|
|
|
items[slot++] = new InventoryItem
|
|
|
|
|
{
|
|
|
|
|
Index = LegalItems[i],
|
|
|
|
|
Count = Data[Offset+i]
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
while (slot < items.Length)
|
|
|
|
|
items[slot++] = new InventoryItem
|
|
|
|
|
{
|
|
|
|
|
Index = 0,
|
|
|
|
|
Count = 0
|
|
|
|
|
};
|
2016-08-28 10:18:22 +00:00
|
|
|
|
}
|
2016-09-04 01:57:54 +00:00
|
|
|
|
else
|
2016-08-28 10:18:22 +00:00
|
|
|
|
{
|
2016-09-04 01:57:54 +00:00
|
|
|
|
int numStored = Data[Offset];
|
2017-02-22 03:45:08 +00:00
|
|
|
|
if (numStored > PouchDataSize) // uninitialized yellow (0xFF), sanity check for out-of-bounds values
|
|
|
|
|
numStored = 0;
|
2016-09-04 01:57:54 +00:00
|
|
|
|
for (int i = 0; i < numStored; i++)
|
2016-08-28 10:18:22 +00:00
|
|
|
|
{
|
2016-09-04 01:57:54 +00:00
|
|
|
|
switch (Type)
|
|
|
|
|
{
|
|
|
|
|
case InventoryType.KeyItems:
|
|
|
|
|
items[i] = new InventoryItem
|
|
|
|
|
{
|
|
|
|
|
Index = Data[Offset + i + 1],
|
|
|
|
|
Count = 1
|
|
|
|
|
};
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
items[i] = new InventoryItem
|
|
|
|
|
{
|
|
|
|
|
Index = Data[Offset + i * 2 + 1],
|
|
|
|
|
Count = Data[Offset + i * 2 + 2]
|
|
|
|
|
};
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (int i = numStored; i < items.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
items[i] = new InventoryItem
|
|
|
|
|
{
|
|
|
|
|
Index = 0,
|
|
|
|
|
Count = 0
|
|
|
|
|
};
|
|
|
|
|
}
|
2016-08-28 10:18:22 +00:00
|
|
|
|
}
|
|
|
|
|
Items = items;
|
|
|
|
|
}
|
2017-12-28 00:36:24 +00:00
|
|
|
|
public void SetPouchG1(byte[] Data)
|
2016-08-28 10:18:22 +00:00
|
|
|
|
{
|
|
|
|
|
if (Items.Length != PouchDataSize)
|
|
|
|
|
throw new ArgumentException("Item array length does not match original pouch size.");
|
2017-06-18 01:37:19 +00:00
|
|
|
|
switch (Type)
|
2016-09-04 01:57:54 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
case InventoryType.TMHMs:
|
|
|
|
|
foreach (InventoryItem t in Items)
|
|
|
|
|
{
|
|
|
|
|
if (!LegalItems.Any(it => it == t.Index))
|
|
|
|
|
continue;
|
|
|
|
|
int index = Offset + Array.FindIndex(LegalItems, it => t.Index == it);
|
|
|
|
|
Data[index] = (byte)t.Count;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case InventoryType.KeyItems:
|
|
|
|
|
for (int i = 0; i < Items.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
Data[Offset + i + 1] = (byte)Items[i].Index;
|
|
|
|
|
}
|
|
|
|
|
Data[Offset] = (byte)Count;
|
|
|
|
|
Data[Offset + 1 + Count] = 0xFF;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
for (int i = 0; i < Items.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
Data[Offset + i * 2 + 1] = (byte)Items[i].Index;
|
|
|
|
|
Data[Offset + i * 2 + 2] = (byte)Items[i].Count;
|
|
|
|
|
}
|
|
|
|
|
Data[Offset] = (byte)Count;
|
|
|
|
|
Data[Offset + 1 + 2 * Count] = 0xFF;
|
|
|
|
|
break;
|
2016-08-28 10:18:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-24 20:29:15 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public void SortByCount(bool reverse = false)
|
2016-11-24 20:29:15 +00:00
|
|
|
|
{
|
2017-11-30 03:47:27 +00:00
|
|
|
|
var list = Items.Where(item => item.Index != 0).OrderBy(item => item.Count == 0);
|
|
|
|
|
list = reverse
|
|
|
|
|
? list.ThenByDescending(item => item.Count)
|
|
|
|
|
: list.ThenBy(item => item.Count);
|
|
|
|
|
Items = list.Concat(Items.Where(item => item.Index == 0)).ToArray();
|
2016-11-24 20:29:15 +00:00
|
|
|
|
}
|
2017-07-07 00:24:40 +00:00
|
|
|
|
public void SortByIndex(bool reverse = false)
|
|
|
|
|
{
|
2017-11-30 03:47:27 +00:00
|
|
|
|
var list = Items.Where(item => item.Index != 0).OrderBy(item => item.Count == 0);
|
|
|
|
|
list = reverse
|
|
|
|
|
? list.ThenByDescending(item => item.Index)
|
|
|
|
|
: list.ThenBy(item => item.Index);
|
|
|
|
|
Items = list.Concat(Items.Where(item => item.Index == 0)).ToArray();
|
2017-07-07 00:24:40 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public void SortByName(string[] names, bool reverse = false)
|
2016-11-24 20:29:15 +00:00
|
|
|
|
{
|
2017-11-30 03:47:27 +00:00
|
|
|
|
var list = Items.Where(item => item.Index != 0 && item.Index < names.Length).OrderBy(item => item.Count == 0);
|
2018-05-12 15:13:39 +00:00
|
|
|
|
list = reverse
|
|
|
|
|
? list.ThenByDescending(item => names[item.Index])
|
2017-11-30 03:47:27 +00:00
|
|
|
|
: list.ThenBy(item => names[item.Index]);
|
|
|
|
|
Items = list.Concat(Items.Where(item => item.Index == 0 || item.Index >= names.Length)).ToArray();
|
2016-11-24 20:29:15 +00:00
|
|
|
|
}
|
2016-12-10 04:12:37 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public void Sanitize(bool HaX, int MaxItemID)
|
2016-12-10 04:12:37 +00:00
|
|
|
|
{
|
|
|
|
|
var x = Items.Where(item => item.Valid(LegalItems, HaX, MaxItemID)).ToArray();
|
2018-05-12 19:28:48 +00:00
|
|
|
|
Items = x.Concat(new byte[PouchDataSize - x.Length].Select(_ => new InventoryItem())).ToArray();
|
2016-12-10 04:12:37 +00:00
|
|
|
|
}
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|