Minor clean

This commit is contained in:
Kurt 2023-04-13 00:05:10 -07:00
parent b5262d69b0
commit ecef31f167
19 changed files with 78 additions and 71 deletions

View file

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
namespace PKHeX.Core;

View file

@ -47,23 +47,30 @@ public readonly record struct Moveset(ushort Move1, ushort Move2 = 0, ushort Mov
public string GetMovesetLine(IReadOnlyList<string> names, string split = " / ")
{
var sb = new StringBuilder(128);
AddMovesetLine(sb, names, split);
return sb.ToString();
}
public void AddMovesetLine(StringBuilder sb, IReadOnlyList<string> names, string split = " / ")
{
// Always has at least 1 move if calling this.
sb.Append(names[Move1]);
if (Move2 != 0)
{
if (Move2 == 0)
return;
sb.Append(split).Append(names[Move2]);
if (Move3 != 0)
{
if (Move3 == 0)
return;
sb.Append(split).Append(names[Move3]);
if (Move4 != 0)
sb.Append(split).Append(names[Move4]);
}
}
return sb.ToString();
}
/// <summary>
/// Flag each present index; having all moves will have all bitflags.
/// </summary>
/// <param name="span">Moves to check and index flags for</param>
public int BitOverlap(ReadOnlySpan<ushort> span)
{
// Flag each present index; having all moves will have all bitflags.
int flags = 0;
for (var i = 0; i < span.Length; i++)
{
@ -74,9 +81,13 @@ public readonly record struct Moveset(ushort Move1, ushort Move2 = 0, ushort Mov
return flags;
}
/// <summary>
/// Flag each present index; having all moves will have all bitflags.
/// </summary>
/// <param name="moves">Base Move source</param>
/// <param name="span">Moves to check and index flags for</param>
public static int BitOverlap(ReadOnlySpan<ushort> moves, ReadOnlySpan<ushort> span)
{
// Flag each present index; having all moves will have all bitflags.
int flags = 0;
for (var i = 0; i < span.Length; i++)
{

View file

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using static System.Buffers.Binary.BinaryPrimitives;

View file

@ -94,7 +94,7 @@ public sealed class SK2 : GBPKM, ICaughtData2
public override string Nickname
{
get => StringConverter12.GetString(Nickname_Trash, Japanese);
set => StringConverter12.SetString(Nickname_Trash, value, 12, Japanese, StringConverterOption.None);
set => StringConverter12.SetString(Nickname_Trash, value, StringLength, Japanese, StringConverterOption.None);
}
public override string OT_Name
@ -111,8 +111,8 @@ public sealed class SK2 : GBPKM, ICaughtData2
}
}
public override Span<byte> Nickname_Trash => Data.AsSpan(0x24, 12);
public override Span<byte> OT_Trash => Data.AsSpan(0x30, 12);
public override Span<byte> Nickname_Trash => Data.AsSpan(0x24, StringLength);
public override Span<byte> OT_Trash => Data.AsSpan(0x30, StringLength);
#endregion
@ -183,18 +183,21 @@ public sealed class SK2 : GBPKM, ICaughtData2
private static bool IsJapanese(ReadOnlySpan<byte> data)
{
if (!StringConverter12.GetIsG1Japanese(data.Slice(0x30, StringLength)))
const byte empty = 0;
const byte terminator = StringConverter12.G1TerminatorCode;
var ot = data.Slice(0x30, StringLength);
if (ot[6..].IndexOfAnyExcept(empty, terminator) != -1)
return false;
if (!StringConverter12.GetIsG1Japanese(data.Slice(0x24, StringLength)))
if (!StringConverter12.GetIsG1Japanese(ot))
return false;
for (int i = 6; i < 0xC; i++)
{
if (data[0x30 + i] is not (0 or StringConverter12.G1TerminatorCode))
var nick = data.Slice(0x24, StringLength);
if (nick[6..].IndexOfAnyExcept(empty, terminator) != -1)
return false;
if (data[0x24 + i] is not (0 or StringConverter12.G1TerminatorCode))
if (!StringConverter12.GetIsG1Japanese(nick))
return false;
}
return true;
}

View file

@ -70,8 +70,7 @@ public static class EntityBlank
public static PKM GetBlank(int gen)
{
var type = Type.GetType($"PKHeX.Core.PK{gen}");
if (type is null)
throw new InvalidCastException($"Unable to get the type for PK{gen}.");
ArgumentNullException.ThrowIfNull(type);
return GetBlank(type);
}

View file

@ -100,7 +100,7 @@ public static class PokeCrypto
/// <summary>
/// Positions for unshuffling.
/// </summary>
private static ReadOnlySpan<byte> blockPositionInvert => new byte[]
private static ReadOnlySpan<byte> BlockPositionInvert => new byte[]
{
0, 1, 2, 4, 3, 5, 6, 7, 12, 18, 13, 19, 8, 10, 14, 20, 16, 22, 9, 11, 15, 21, 17, 23,
0, 1, 2, 4, 3, 5, 6, 7, // duplicates of 0-7 to eliminate modulus
@ -183,7 +183,7 @@ public static class PokeCrypto
uint pv = ReadUInt32LittleEndian(pk);
uint sv = (pv >> 13) & 31;
byte[] ekm = ShuffleArray(pk, blockPositionInvert[(int)sv], SIZE_8BLOCK);
byte[] ekm = ShuffleArray(pk, BlockPositionInvert[(int)sv], SIZE_8BLOCK);
CryptPKM(ekm, pv, SIZE_8BLOCK);
return ekm;
}
@ -197,7 +197,7 @@ public static class PokeCrypto
uint pv = ReadUInt32LittleEndian(pk);
uint sv = (pv >> 13) & 31;
byte[] ekm = ShuffleArray(pk, blockPositionInvert[(int)sv], SIZE_8ABLOCK);
byte[] ekm = ShuffleArray(pk, BlockPositionInvert[(int)sv], SIZE_8ABLOCK);
CryptPKM(ekm, pv, SIZE_8ABLOCK);
return ekm;
}
@ -211,7 +211,7 @@ public static class PokeCrypto
uint pv = ReadUInt32LittleEndian(pk);
uint sv = (pv >> 13) & 31;
byte[] ekm = ShuffleArray(pk, blockPositionInvert[(int)sv], SIZE_9BLOCK);
byte[] ekm = ShuffleArray(pk, BlockPositionInvert[(int)sv], SIZE_9BLOCK);
CryptPKM(ekm, pv, SIZE_9BLOCK);
return ekm;
}
@ -240,7 +240,7 @@ public static class PokeCrypto
uint pv = ReadUInt32LittleEndian(pk);
uint sv = (pv >> 13) & 31;
byte[] ekm = ShuffleArray(pk, blockPositionInvert[(int)sv], SIZE_6BLOCK);
byte[] ekm = ShuffleArray(pk, BlockPositionInvert[(int)sv], SIZE_6BLOCK);
CryptPKM(ekm, pv, SIZE_6BLOCK);
return ekm;
}
@ -271,7 +271,7 @@ public static class PokeCrypto
uint chk = ReadUInt16LittleEndian(pk[6..]);
uint sv = (pv >> 13) & 31;
byte[] ekm = ShuffleArray(pk, blockPositionInvert[(int)sv], SIZE_4BLOCK);
byte[] ekm = ShuffleArray(pk, BlockPositionInvert[(int)sv], SIZE_4BLOCK);
CryptPKM45(ekm, pv, chk, SIZE_4BLOCK);
return ekm;
}
@ -297,7 +297,7 @@ public static class PokeCrypto
{
uint pv = ReadUInt32BigEndian(pk);
uint sv = (pv >> 13) & 31;
return ShuffleArray(pk, blockPositionInvert[(int)sv], SIZE_4BLOCK);
return ShuffleArray(pk, BlockPositionInvert[(int)sv], SIZE_4BLOCK);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -392,7 +392,7 @@ public static class PokeCrypto
uint OID = ReadUInt32LittleEndian(pk[4..]);
uint seed = PID ^ OID;
byte[] ekm = ShuffleArray3(pk, blockPositionInvert[(int)(PID % 24)]);
byte[] ekm = ShuffleArray3(pk, BlockPositionInvert[(int)(PID % 24)]);
var toEncrypt = ekm.AsSpan()[SIZE_3HEADER..SIZE_3STORED];
for (int i = 0; i < toEncrypt.Length; i += 4)

View file

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using static System.Buffers.Binary.BinaryPrimitives;
namespace PKHeX.Core;

View file

@ -46,5 +46,5 @@ public enum RanchToyType : byte
Twirler = 36,
Bound_Mat = 37,
Tree = 38,
Water = 39 // Normally unused; creates a massive plane of water in the sky
Water = 39, // Normally unused; creates a massive plane of water in the sky
}

View file

@ -7,8 +7,8 @@ namespace PKHeX.Core;
/// Stores the <see cref="Timestamp"/> to indicate the seconds since 1900 (rounded to days) that an event occurred.
/// </summary>
[TypeConverter(typeof(ExpandableObjectConverter))]
public sealed class Epoch1900Value {
public sealed class Epoch1900Value
{
// Data should be 4 bytes where we only care about the first 3 bytes i.e. 24 bits
// First 6 bits are day, next 6 bits are 0 indexed month, last 12 bits are year from 1900
private readonly Memory<byte> Data;
@ -19,14 +19,13 @@ public sealed class Epoch1900Value {
private static DateTime Epoch => new(1900, 1, 1);
public DateTime Timestamp {
get {
return Epoch
public DateTime Timestamp
{
get => Epoch
.AddDays(Span[2] >> 2)
.AddDays(-1)
.AddMonths(((Span[2] & 0b0000_0011) << 2) | ((Span[1] & 0b1111_0000) >> 4))
.AddYears(((Span[1] & 0b0000_1111) << 4) | Span[0]);
}
set {
int day = value.Day;
int month = value.Month - Epoch.Month;
@ -42,7 +41,8 @@ public sealed class Epoch1900Value {
/// <summary>
/// time_t (seconds since 1900 Epoch rounded to days)
/// </summary>
public ulong Seconds {
public ulong Seconds
{
get => (ulong)(Timestamp - Epoch).TotalSeconds;
set => Timestamp = Epoch.AddSeconds(value);
}

View file

@ -357,9 +357,8 @@ public sealed class Zukan4 : ZukanBase<SAV4>
int dpl = 1 + DPLangSpecies.IndexOf(species);
if (DP && dpl <= 0)
return false;
const int FormOffset1 = OFS_FORM1;
int PokeDexLanguageFlags = FormOffset1 + (HGSS ? 0x3C : 0x20);
int PokeDexLanguageFlags = OFS_FORM1 + (HGSS ? 0x3C : 0x20);
var ofs = PokeDexLanguageFlags + (DP ? dpl : species);
return FlagUtil.GetFlag(Data, ofs, lang & 7);
}
@ -369,9 +368,8 @@ public sealed class Zukan4 : ZukanBase<SAV4>
int dpl = 1 + DPLangSpecies.IndexOf(species);
if (DP && dpl <= 0)
return;
const int FormOffset1 = OFS_FORM1;
int PokeDexLanguageFlags = FormOffset1 + (HGSS ? 0x3C : 0x20);
int PokeDexLanguageFlags = OFS_FORM1 + (HGSS ? 0x3C : 0x20);
var ofs = PokeDexLanguageFlags + (DP ? dpl : species);
FlagUtil.SetFlag(Data, ofs, lang & 7, value);
}

View file

@ -39,7 +39,7 @@ public static partial class Util
return list;
}
public static List<ComboItem> GetCBList(IReadOnlyList<string> inStrings, ReadOnlySpan<ushort> allowed)
public static List<ComboItem> GetCBList(ReadOnlySpan<string> inStrings, ReadOnlySpan<ushort> allowed)
{
var list = new List<ComboItem>(allowed.Length + 1) { new(inStrings[0], 0) };
foreach (var index in allowed)
@ -48,14 +48,14 @@ public static partial class Util
return list;
}
public static List<ComboItem> GetCBList(IReadOnlyList<string> inStrings, int index, int offset = 0)
public static List<ComboItem> GetCBList(ReadOnlySpan<string> inStrings, int index, int offset = 0)
{
var list = new List<ComboItem>();
AddCBWithOffset(list, inStrings, offset, index);
return list;
}
public static ComboItem[] GetUnsortedCBList(IReadOnlyList<string> inStrings, ReadOnlySpan<byte> allowed)
public static ComboItem[] GetUnsortedCBList(ReadOnlySpan<string> inStrings, ReadOnlySpan<byte> allowed)
{
var count = allowed.Length;
var list = new ComboItem[count];
@ -69,13 +69,13 @@ public static partial class Util
return list;
}
public static void AddCBWithOffset(List<ComboItem> list, IReadOnlyList<string> inStrings, int offset, int index)
public static void AddCBWithOffset(List<ComboItem> list, ReadOnlySpan<string> inStrings, int offset, int index)
{
var item = new ComboItem(inStrings[index - offset], index);
list.Add(item);
}
public static void AddCBWithOffset(List<ComboItem> cbList, IReadOnlyList<string> inStrings, int offset, ReadOnlySpan<byte> allowed)
public static void AddCBWithOffset(List<ComboItem> cbList, ReadOnlySpan<string> inStrings, int offset, ReadOnlySpan<byte> allowed)
{
int beginCount = cbList.Count;
cbList.Capacity += allowed.Length;
@ -87,7 +87,7 @@ public static partial class Util
cbList.Sort(beginCount, allowed.Length, Comparer);
}
public static void AddCBWithOffset(List<ComboItem> cbList, IReadOnlyList<string> inStrings, int offset, ReadOnlySpan<ushort> allowed)
public static void AddCBWithOffset(List<ComboItem> cbList, ReadOnlySpan<string> inStrings, int offset, ReadOnlySpan<ushort> allowed)
{
int beginCount = cbList.Count;
cbList.Capacity += allowed.Length;

View file

@ -235,8 +235,7 @@ public partial class RibbonEditor : Form
rib.HasRibbon = chk.Checked;
var controlName = PrefixPB + rib.Name;
var control = FLP_Ribbons.Controls[controlName];
if (control is null)
throw new ArgumentException($"{controlName} not found in {FLP_Ribbons.Name}.");
ArgumentNullException.ThrowIfNull(control);
control.Visible = rib.HasRibbon;
ToggleNewRibbon(rib, control);

View file

@ -296,17 +296,17 @@ public partial class SAV_SimpleTrainer : Form
if (sender is ComboBox c)
{
int index = WinFormsUtil.GetIndex(c);
if (SAV is SAV4 sav4)
if (SAV is SAV4)
{
Main.SetCountrySubRegion(CB_Region, $"gen4_sr_{index:000}");
if (CB_Region.Items.Count == 0)
Main.SetCountrySubRegion(CB_Region, $"gen4_sr_default");
Main.SetCountrySubRegion(CB_Region, "gen4_sr_default");
}
else if (SAV is SAV5 s)
else if (SAV is SAV5)
{
Main.SetCountrySubRegion(CB_Region, $"gen5_sr_{index:000}");
if (CB_Region.Items.Count == 0)
Main.SetCountrySubRegion(CB_Region, $"gen5_sr_default");
Main.SetCountrySubRegion(CB_Region, "gen5_sr_default");
}
}
}

View file

@ -59,9 +59,9 @@ public class StatTest
ushort effort = 63001;
effort = (ushort)(Math.Min((ushort)255, (ushort)Math.Ceiling(Math.Sqrt(effort))) >> 2);
var iv = 15;
var level = 100;
var baseStat = 91;
const int iv = 15;
const int level = 100;
const int baseStat = 91;
var expect = (ushort)((((2 * (baseStat + iv)) + effort) * level / 100) + 5);
expect.Should().Be(279);