Minor tweaks

This commit is contained in:
Kurt 2021-05-07 22:11:10 -07:00
parent 31142ee297
commit ceede68861
10 changed files with 39 additions and 18 deletions

View file

@ -119,7 +119,7 @@ namespace PKHeX.Core
var matchingColors = BallColors[c];
var extra = allBalls.Except(matchingColors).ToArray();
Util.Shuffle(extra);
BallColors[c] = matchingColors.Concat(extra).Concat(end).ToArray();
BallColors[c] = ArrayUtil.ConcatAll(matchingColors, extra, end);
}
}

View file

@ -49,7 +49,7 @@ namespace PKHeX.Core
private PGT? _gift;
public byte[] GetMetadata() => Data.SliceEnd(PGT.Size);
public Span<byte> GetMetadata() => Data.AsSpan(PGT.Size);
public void SetMetadata(byte[] data) => data.CopyTo(Data, Data.Length - PGT.Size);
public override bool GiftUsed { get => Gift.GiftUsed; set => Gift.GiftUsed = value; }

View file

@ -44,4 +44,4 @@
return rnd.Next(0x81) + rnd.Next(0x80);
}
}
}
}

View file

@ -71,7 +71,7 @@ namespace PKHeX.Core
var key = new MemeKey(keyIndex);
output = (byte[])input.Clone();
var sigBuffer = key.RsaPublic(input.SliceEnd(input.Length - 0x60));
var sigBuffer = key.RsaPublic(input.AsSpan(input.Length - 0x60));
using var sha1 = SHA1.Create();
if (DecryptCompare(output, sigBuffer, key, sha1))
return true;
@ -83,9 +83,9 @@ namespace PKHeX.Core
return false;
}
private static bool DecryptCompare(byte[] output, byte[] sigBuffer, MemeKey key, SHA1 sha1)
private static bool DecryptCompare(byte[] output, ReadOnlySpan<byte> sigBuffer, MemeKey key, SHA1 sha1)
{
sigBuffer.CopyTo(output, output.Length - 0x60);
sigBuffer.CopyTo(output.AsSpan(output.Length - 0x60));
key.AesDecrypt(output).CopyTo(output, 0);
// Check for 8-byte equality.
var hash = sha1.ComputeHash(output, 0, output.Length - 0x8);

View file

@ -208,10 +208,10 @@ namespace PKHeX.Core
/// <summary>
/// Perform Rsa Encryption
/// </summary>
internal byte[] RsaPublic(byte[] data)
internal byte[] RsaPublic(ReadOnlySpan<byte> data)
{
var _M = new byte[data.Length + 1];
data.CopyTo(_M, 1);
data.CopyTo(_M.AsSpan(1));
Array.Reverse(_M);
var M = new BigInteger(_M);

View file

@ -668,8 +668,16 @@ namespace PKHeX.Core
int len = BoxSlotCount * SIZE_BOXSLOT;
byte[] boxdata = storage.Slice(GetBoxOffset(0), len * BoxCount); // get all boxes
string[] boxNames = new int[BoxCount].Select((_, i) => GetBoxName(i)).ToArray();
int[] boxWallpapers = new int[BoxCount].Select((_, i) => GetBoxWallpaper(i)).ToArray();
string[] boxNames = Get(GetBoxName, BoxCount);
int[] boxWallpapers = Get(GetBoxWallpaper, BoxCount);
static T[] Get<T>(Func<int, T> act, int count)
{
T[] result = new T[count];
for (int i = 0; i < result.Length; i++)
result[i] = act(i);
return result;
}
min /= BoxSlotCount;
max /= BoxSlotCount;

View file

@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Linq;
namespace PKHeX.Core
{
@ -52,13 +51,13 @@ namespace PKHeX.Core
/// </summary>
/// <param name="data">Finalized save file data (with fixed checksums) to be written to a file</param>
/// <param name="flags">Toggle flags </param>
/// <returns></returns>
/// <returns>Final save file data.</returns>
public byte[] Finalize(byte[] data, ExportFlags flags)
{
if (Footer.Length > 0 && flags.HasFlagFast(ExportFlags.IncludeFooter))
return data.Concat(Footer).ToArray();
return ArrayUtil.ConcatAll(data, Footer);
if (Header.Length > 0 && flags.HasFlagFast(ExportFlags.IncludeHeader))
return Header.Concat(data).ToArray();
return ArrayUtil.ConcatAll(Header, data);
return data;
}

View file

@ -41,7 +41,8 @@ namespace PKHeX.Core
{
get
{
var data = Data.Skip(Offset + 0x10).Take(GameSyncIDSize / 2).Reverse().ToArray();
var data = Data.AsSpan(Offset + 0x10, GameSyncIDSize / 2).ToArray();
Array.Reverse(data);
return BitConverter.ToString(data).Replace("-", string.Empty);
}
set

View file

@ -69,8 +69,8 @@ namespace PKHeX.Core
return OFS_1 + (LEN_POKE * slot);
}
public byte[] GetMetadataStart() => Data.Slice(OFS_META, LEN_META);
public byte[] GetMetadataEnd() => Data.SliceEnd(POST_META);
public Span<byte> GetMetadataStart() => Data.AsSpan(OFS_META, LEN_META);
public Span<byte> GetMetadataEnd() => Data.AsSpan(POST_META);
public IEnumerable<PKM> Contents => GetTeam();

View file

@ -38,4 +38,17 @@ namespace PKHeX.Tests.PKM
pk.Move3_PP.Should().Be(61, "pp calc oddity");
}
}
}
public class BelugaTests
{
[Theory]
[InlineData(41, 25, 91)]
public void CalculateCP(int level, int statSum, int expect)
{
var result1 = (((level * 4.0f / 100.0f) + 2.0f) * (statSum & 0xFFFF));
var result2 = (int)result1;
result2.Should().Be(expect);
}
}
}