2019-09-10 07:21:51 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2019-09-19 02:58:23 +00:00
|
|
|
|
/// Array reusable logic
|
2019-09-10 07:21:51 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public static class ArrayUtil
|
|
|
|
|
{
|
2021-05-14 22:30:55 +00:00
|
|
|
|
public static bool IsRangeEmpty(this ReadOnlySpan<byte> data, byte value = 0)
|
2019-09-10 07:21:51 +00:00
|
|
|
|
{
|
2021-05-14 22:30:55 +00:00
|
|
|
|
for (int i = data.Length - 1; i >= 0; i--)
|
2019-09-10 07:21:51 +00:00
|
|
|
|
{
|
2021-05-14 22:30:55 +00:00
|
|
|
|
if (data[i] != value)
|
2019-09-10 07:21:51 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
|
public static int Count<T>(this Span<T> data, T value) where T : IEquatable<T>
|
|
|
|
|
{
|
|
|
|
|
return ((ReadOnlySpan<T>)data).Count(value);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-14 02:24:08 +00:00
|
|
|
|
public static T Find<T>(this Span<T> data, Func<T, bool> value) where T : unmanaged
|
|
|
|
|
{
|
|
|
|
|
foreach (var x in data)
|
|
|
|
|
{
|
|
|
|
|
if (value(x))
|
|
|
|
|
return x;
|
|
|
|
|
}
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
|
public static int Count<T>(this ReadOnlySpan<T> data, T value) where T : IEquatable<T>
|
|
|
|
|
{
|
|
|
|
|
int count = 0;
|
|
|
|
|
for (int i = data.Length - 1; i >= 0; i--)
|
|
|
|
|
{
|
|
|
|
|
if (data[i].Equals(value))
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
2022-02-05 01:35:15 +00:00
|
|
|
|
|
2021-05-14 22:30:55 +00:00
|
|
|
|
public static byte[] Slice(this byte[] src, int offset, int length) => src.AsSpan(offset, length).ToArray();
|
|
|
|
|
public static byte[] SliceEnd(this byte[] src, int offset) => src.AsSpan(offset).ToArray();
|
|
|
|
|
public static T[] Slice<T>(this T[] src, int offset, int length) => src.AsSpan(offset, length).ToArray();
|
|
|
|
|
public static T[] SliceEnd<T>(this T[] src, int offset) => src.AsSpan(offset).ToArray();
|
2019-10-25 01:10:23 +00:00
|
|
|
|
|
2019-09-10 07:21:51 +00:00
|
|
|
|
public static bool WithinRange(int value, int min, int max) => min <= value && value < max;
|
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
|
public static T[][] Split<T>(this ReadOnlySpan<T> data, int size)
|
2019-09-10 07:21:51 +00:00
|
|
|
|
{
|
2019-10-25 01:10:23 +00:00
|
|
|
|
var result = new T[data.Length / size][];
|
2019-09-10 07:21:51 +00:00
|
|
|
|
for (int i = 0; i < data.Length; i += size)
|
2022-01-03 05:35:59 +00:00
|
|
|
|
result[i / size] = data.Slice(i, size).ToArray();
|
2019-09-10 07:21:51 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-25 01:10:23 +00:00
|
|
|
|
public static IEnumerable<T[]> EnumerateSplit<T>(T[] bin, int size, int start = 0)
|
2019-09-10 07:21:51 +00:00
|
|
|
|
{
|
|
|
|
|
for (int i = start; i < bin.Length; i += size)
|
|
|
|
|
yield return bin.Slice(i, size);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-25 01:10:23 +00:00
|
|
|
|
public static IEnumerable<T[]> EnumerateSplit<T>(T[] bin, int size, int start, int end)
|
2019-09-10 07:21:51 +00:00
|
|
|
|
{
|
|
|
|
|
if (end < 0)
|
|
|
|
|
end = bin.Length;
|
|
|
|
|
for (int i = start; i < end; i += size)
|
|
|
|
|
yield return bin.Slice(i, size);
|
|
|
|
|
}
|
2019-09-12 01:39:49 +00:00
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
|
public static bool[] GitBitFlagArray(ReadOnlySpan<byte> data, int count)
|
2019-09-12 01:39:49 +00:00
|
|
|
|
{
|
|
|
|
|
bool[] result = new bool[count];
|
|
|
|
|
for (int i = 0; i < result.Length; i++)
|
2022-01-03 05:35:59 +00:00
|
|
|
|
result[i] = (data[i >> 3] >> (i & 7) & 0x1) == 1;
|
2019-09-12 01:39:49 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
|
public static void SetBitFlagArray(Span<byte> data, bool[] value)
|
2019-09-12 01:39:49 +00:00
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < value.Length; i++)
|
|
|
|
|
{
|
2022-01-03 05:35:59 +00:00
|
|
|
|
var ofs = i >> 3;
|
2019-09-24 02:01:37 +00:00
|
|
|
|
var mask = (1 << (i & 7));
|
2019-09-12 01:39:49 +00:00
|
|
|
|
if (value[i])
|
2019-09-24 02:01:37 +00:00
|
|
|
|
data[ofs] |= (byte)mask;
|
|
|
|
|
else
|
|
|
|
|
data[ofs] &= (byte)~mask;
|
2019-09-12 01:39:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-19 02:58:23 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Copies a <see cref="T"/> list to the destination list, with an option to copy to a starting point.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="list">Source list to copy from</param>
|
|
|
|
|
/// <param name="dest">Destination list/array</param>
|
|
|
|
|
/// <param name="skip">Criteria for skipping a slot</param>
|
|
|
|
|
/// <param name="start">Starting point to copy to</param>
|
|
|
|
|
/// <returns>Count of <see cref="T"/> copied.</returns>
|
Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately (#3222)
* Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately
Don't store within the object, track the slot origin data separately.
Batch editing now pre-filters if using Box/Slot/Identifier logic; split up mods/filters as they're starting to get pretty hefty.
- Requesting a Box Data report now shows all slots in the save file (party, misc)
- Can now exclude backup saves from database search via toggle (separate from settings preventing load entirely)
- Replace some linq usages with direct code
* Remove WasLink virtual in PKM
Inline any logic, since we now have encounter objects to indicate matching, rather than the proto-legality logic checking properties of a PKM.
* Use Fateful to directly check gen5 mysterygift origins
No other encounter types in gen5 apply Fateful
* Simplify double ball comparison
Used to be separate for deferral cases, now no longer needed to be separate.
* Grab move/relearn reference and update locally
Fix relearn move identifier
* Inline defog HM transfer preference check
HasMove is faster than getting moves & checking contains. Skips allocation by setting values directly.
* Extract more met location metadata checks: WasBredEgg
* Replace Console.Write* with Debug.Write*
There's no console output UI, so don't include them in release builds.
* Inline WasGiftEgg, WasEvent, and WasEventEgg logic
Adios legality tags that aren't entirely correct for the specific format. Just put the computations in EncounterFinder.
2021-06-23 03:23:48 +00:00
|
|
|
|
public static int CopyTo<T>(this IEnumerable<T> list, IList<T> dest, Func<int, bool> skip, int start = 0)
|
2019-09-19 02:58:23 +00:00
|
|
|
|
{
|
|
|
|
|
int ctr = start;
|
|
|
|
|
int skipped = 0;
|
|
|
|
|
foreach (var z in list)
|
|
|
|
|
{
|
|
|
|
|
// seek forward to next open slot
|
|
|
|
|
int next = FindNextValidIndex(dest, skip, ctr);
|
|
|
|
|
if (next == -1)
|
|
|
|
|
break;
|
|
|
|
|
skipped += next - ctr;
|
|
|
|
|
ctr = next;
|
|
|
|
|
dest[ctr++] = z;
|
|
|
|
|
}
|
|
|
|
|
return ctr - start - skipped;
|
|
|
|
|
}
|
|
|
|
|
|
Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately (#3222)
* Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately
Don't store within the object, track the slot origin data separately.
Batch editing now pre-filters if using Box/Slot/Identifier logic; split up mods/filters as they're starting to get pretty hefty.
- Requesting a Box Data report now shows all slots in the save file (party, misc)
- Can now exclude backup saves from database search via toggle (separate from settings preventing load entirely)
- Replace some linq usages with direct code
* Remove WasLink virtual in PKM
Inline any logic, since we now have encounter objects to indicate matching, rather than the proto-legality logic checking properties of a PKM.
* Use Fateful to directly check gen5 mysterygift origins
No other encounter types in gen5 apply Fateful
* Simplify double ball comparison
Used to be separate for deferral cases, now no longer needed to be separate.
* Grab move/relearn reference and update locally
Fix relearn move identifier
* Inline defog HM transfer preference check
HasMove is faster than getting moves & checking contains. Skips allocation by setting values directly.
* Extract more met location metadata checks: WasBredEgg
* Replace Console.Write* with Debug.Write*
There's no console output UI, so don't include them in release builds.
* Inline WasGiftEgg, WasEvent, and WasEventEgg logic
Adios legality tags that aren't entirely correct for the specific format. Just put the computations in EncounterFinder.
2021-06-23 03:23:48 +00:00
|
|
|
|
public static int FindNextValidIndex<T>(IList<T> dest, Func<int, bool> skip, int ctr)
|
2019-09-19 02:58:23 +00:00
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
if ((uint)ctr >= dest.Count)
|
|
|
|
|
return -1;
|
|
|
|
|
var exist = dest[ctr];
|
Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately (#3222)
* Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately
Don't store within the object, track the slot origin data separately.
Batch editing now pre-filters if using Box/Slot/Identifier logic; split up mods/filters as they're starting to get pretty hefty.
- Requesting a Box Data report now shows all slots in the save file (party, misc)
- Can now exclude backup saves from database search via toggle (separate from settings preventing load entirely)
- Replace some linq usages with direct code
* Remove WasLink virtual in PKM
Inline any logic, since we now have encounter objects to indicate matching, rather than the proto-legality logic checking properties of a PKM.
* Use Fateful to directly check gen5 mysterygift origins
No other encounter types in gen5 apply Fateful
* Simplify double ball comparison
Used to be separate for deferral cases, now no longer needed to be separate.
* Grab move/relearn reference and update locally
Fix relearn move identifier
* Inline defog HM transfer preference check
HasMove is faster than getting moves & checking contains. Skips allocation by setting values directly.
* Extract more met location metadata checks: WasBredEgg
* Replace Console.Write* with Debug.Write*
There's no console output UI, so don't include them in release builds.
* Inline WasGiftEgg, WasEvent, and WasEventEgg logic
Adios legality tags that aren't entirely correct for the specific format. Just put the computations in EncounterFinder.
2021-06-23 03:23:48 +00:00
|
|
|
|
if (exist == null || !skip(ctr))
|
2019-09-19 02:58:23 +00:00
|
|
|
|
return ctr;
|
|
|
|
|
ctr++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Copies an <see cref="IEnumerable{T}"/> list to the destination list, with an option to copy to a starting point.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">Typed object to copy</typeparam>
|
|
|
|
|
/// <param name="list">Source list to copy from</param>
|
|
|
|
|
/// <param name="dest">Destination list/array</param>
|
|
|
|
|
/// <param name="start">Starting point to copy to</param>
|
|
|
|
|
/// <returns>Count of <see cref="T"/> copied.</returns>
|
|
|
|
|
public static int CopyTo<T>(this IEnumerable<T> list, IList<T> dest, int start = 0)
|
|
|
|
|
{
|
|
|
|
|
int ctr = start;
|
|
|
|
|
foreach (var z in list)
|
|
|
|
|
{
|
|
|
|
|
if ((uint)ctr >= dest.Count)
|
|
|
|
|
break;
|
|
|
|
|
dest[ctr++] = z;
|
|
|
|
|
}
|
|
|
|
|
return ctr - start;
|
|
|
|
|
}
|
2019-11-16 03:50:22 +00:00
|
|
|
|
|
|
|
|
|
internal static T[] ConcatAll<T>(params T[][] arr)
|
|
|
|
|
{
|
|
|
|
|
int len = 0;
|
|
|
|
|
foreach (var a in arr)
|
|
|
|
|
len += a.Length;
|
|
|
|
|
|
|
|
|
|
var result = new T[len];
|
|
|
|
|
|
|
|
|
|
int ctr = 0;
|
2019-11-21 01:01:34 +00:00
|
|
|
|
foreach (var a in arr)
|
2019-11-16 03:50:22 +00:00
|
|
|
|
{
|
2019-11-21 01:01:34 +00:00
|
|
|
|
a.CopyTo(result, ctr);
|
|
|
|
|
ctr += a.Length;
|
2019-11-16 03:50:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2020-07-19 21:35:52 +00:00
|
|
|
|
|
|
|
|
|
internal static T[] ConcatAll<T>(T[] arr1, T[] arr2)
|
|
|
|
|
{
|
|
|
|
|
int len = arr1.Length + arr2.Length;
|
|
|
|
|
var result = new T[len];
|
|
|
|
|
arr1.CopyTo(result, 0);
|
|
|
|
|
arr2.CopyTo(result, arr1.Length);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static T[] ConcatAll<T>(T[] arr1, T[] arr2, T[] arr3)
|
|
|
|
|
{
|
|
|
|
|
int len = arr1.Length + arr2.Length + arr3.Length;
|
|
|
|
|
var result = new T[len];
|
|
|
|
|
arr1.CopyTo(result, 0);
|
|
|
|
|
arr2.CopyTo(result, arr1.Length);
|
|
|
|
|
arr3.CopyTo(result, arr1.Length + arr2.Length);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2021-05-07 06:26:38 +00:00
|
|
|
|
|
|
|
|
|
internal static T[] ConcatAll<T>(T[] arr1, T[] arr2, Span<T> arr3)
|
|
|
|
|
{
|
|
|
|
|
int len = arr1.Length + arr2.Length + arr3.Length;
|
|
|
|
|
var result = new T[len];
|
|
|
|
|
arr1.CopyTo(result, 0);
|
|
|
|
|
arr2.CopyTo(result, arr1.Length);
|
|
|
|
|
arr3.CopyTo(result.AsSpan(arr1.Length + arr2.Length));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2019-09-10 07:21:51 +00:00
|
|
|
|
}
|
2021-05-07 06:26:38 +00:00
|
|
|
|
}
|