From ceede68861efff6257dfef5e719d0427709e0108 Mon Sep 17 00:00:00 2001 From: Kurt Date: Fri, 7 May 2021 22:11:10 -0700 Subject: [PATCH] Minor tweaks --- PKHeX.Core/Editing/Applicators/BallApplicator.cs | 2 +- PKHeX.Core/MysteryGifts/PCD.cs | 2 +- PKHeX.Core/PKM/Shared/PokeSize.cs | 2 +- .../Saves/Encryption/MemeCrypto/MemeCrypto.cs | 6 +++--- PKHeX.Core/Saves/Encryption/MemeCrypto/MemeKey.cs | 4 ++-- PKHeX.Core/Saves/SaveFile.cs | 12 ++++++++++-- PKHeX.Core/Saves/SaveFileMetadata.cs | 7 +++---- PKHeX.Core/Saves/Substructures/Gen7/MyStatus7b.cs | 3 ++- .../Saves/Substructures/Gen8/RentalTeam8.cs | 4 ++-- Tests/PKHeX.Core.Tests/PKM/StatTest.cs | 15 ++++++++++++++- 10 files changed, 39 insertions(+), 18 deletions(-) diff --git a/PKHeX.Core/Editing/Applicators/BallApplicator.cs b/PKHeX.Core/Editing/Applicators/BallApplicator.cs index 670bdc26b..1ecac547c 100644 --- a/PKHeX.Core/Editing/Applicators/BallApplicator.cs +++ b/PKHeX.Core/Editing/Applicators/BallApplicator.cs @@ -119,7 +119,7 @@ static BallApplicator() 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); } } diff --git a/PKHeX.Core/MysteryGifts/PCD.cs b/PKHeX.Core/MysteryGifts/PCD.cs index 662d334ff..0cb847256 100644 --- a/PKHeX.Core/MysteryGifts/PCD.cs +++ b/PKHeX.Core/MysteryGifts/PCD.cs @@ -49,7 +49,7 @@ public PGT Gift private PGT? _gift; - public byte[] GetMetadata() => Data.SliceEnd(PGT.Size); + public Span 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; } diff --git a/PKHeX.Core/PKM/Shared/PokeSize.cs b/PKHeX.Core/PKM/Shared/PokeSize.cs index eb866c29c..81aec8924 100644 --- a/PKHeX.Core/PKM/Shared/PokeSize.cs +++ b/PKHeX.Core/PKM/Shared/PokeSize.cs @@ -44,4 +44,4 @@ public static int GetRandomScalar() return rnd.Next(0x81) + rnd.Next(0x80); } } -} \ No newline at end of file +} diff --git a/PKHeX.Core/Saves/Encryption/MemeCrypto/MemeCrypto.cs b/PKHeX.Core/Saves/Encryption/MemeCrypto/MemeCrypto.cs index 8dbe36707..9292554e6 100644 --- a/PKHeX.Core/Saves/Encryption/MemeCrypto/MemeCrypto.cs +++ b/PKHeX.Core/Saves/Encryption/MemeCrypto/MemeCrypto.cs @@ -71,7 +71,7 @@ public static bool VerifyMemeData(byte[] input, out byte[] output, MemeKeyIndex 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 @@ public static bool VerifyMemeData(byte[] input, out byte[] output, MemeKeyIndex return false; } - private static bool DecryptCompare(byte[] output, byte[] sigBuffer, MemeKey key, SHA1 sha1) + private static bool DecryptCompare(byte[] output, ReadOnlySpan 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); diff --git a/PKHeX.Core/Saves/Encryption/MemeCrypto/MemeKey.cs b/PKHeX.Core/Saves/Encryption/MemeCrypto/MemeKey.cs index f38258376..4e520f272 100644 --- a/PKHeX.Core/Saves/Encryption/MemeCrypto/MemeKey.cs +++ b/PKHeX.Core/Saves/Encryption/MemeCrypto/MemeKey.cs @@ -208,10 +208,10 @@ internal byte[] RsaPrivate(byte[] data) /// /// Perform Rsa Encryption /// - internal byte[] RsaPublic(byte[] data) + internal byte[] RsaPublic(ReadOnlySpan 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); diff --git a/PKHeX.Core/Saves/SaveFile.cs b/PKHeX.Core/Saves/SaveFile.cs index b8028ff51..2b18293dc 100644 --- a/PKHeX.Core/Saves/SaveFile.cs +++ b/PKHeX.Core/Saves/SaveFile.cs @@ -668,8 +668,16 @@ private void MoveBox(int box, int insertBeforeBox, byte[] storage) 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(Func 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; diff --git a/PKHeX.Core/Saves/SaveFileMetadata.cs b/PKHeX.Core/Saves/SaveFileMetadata.cs index b8cac2b35..11d3ec7fd 100644 --- a/PKHeX.Core/Saves/SaveFileMetadata.cs +++ b/PKHeX.Core/Saves/SaveFileMetadata.cs @@ -1,6 +1,5 @@ using System; using System.IO; -using System.Linq; namespace PKHeX.Core { @@ -52,13 +51,13 @@ public class SaveFileMetadata /// /// Finalized save file data (with fixed checksums) to be written to a file /// Toggle flags - /// + /// Final save file data. 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; } diff --git a/PKHeX.Core/Saves/Substructures/Gen7/MyStatus7b.cs b/PKHeX.Core/Saves/Substructures/Gen7/MyStatus7b.cs index d296b0507..4e31d6eec 100644 --- a/PKHeX.Core/Saves/Substructures/Gen7/MyStatus7b.cs +++ b/PKHeX.Core/Saves/Substructures/Gen7/MyStatus7b.cs @@ -41,7 +41,8 @@ public string GameSyncID { 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 diff --git a/PKHeX.Core/Saves/Substructures/Gen8/RentalTeam8.cs b/PKHeX.Core/Saves/Substructures/Gen8/RentalTeam8.cs index 6a16333f6..5e8658176 100644 --- a/PKHeX.Core/Saves/Substructures/Gen8/RentalTeam8.cs +++ b/PKHeX.Core/Saves/Substructures/Gen8/RentalTeam8.cs @@ -69,8 +69,8 @@ public static int GetSlotOffset(int slot) return OFS_1 + (LEN_POKE * slot); } - public byte[] GetMetadataStart() => Data.Slice(OFS_META, LEN_META); - public byte[] GetMetadataEnd() => Data.SliceEnd(POST_META); + public Span GetMetadataStart() => Data.AsSpan(OFS_META, LEN_META); + public Span GetMetadataEnd() => Data.AsSpan(POST_META); public IEnumerable Contents => GetTeam(); diff --git a/Tests/PKHeX.Core.Tests/PKM/StatTest.cs b/Tests/PKHeX.Core.Tests/PKM/StatTest.cs index d2e18f51a..6658c5e7b 100644 --- a/Tests/PKHeX.Core.Tests/PKM/StatTest.cs +++ b/Tests/PKHeX.Core.Tests/PKM/StatTest.cs @@ -38,4 +38,17 @@ public void CalcStatsGB() pk.Move3_PP.Should().Be(61, "pp calc oddity"); } } -} \ No newline at end of file + + 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); + } + } +}