Misc clean

un-nest classes,
move some logic to core,
update some get-only properties that return arrays to methods
This commit is contained in:
Kurt 2019-10-26 12:58:55 -07:00
parent fcc993784b
commit 46640d48a3
15 changed files with 318 additions and 306 deletions

View file

@ -0,0 +1,34 @@
using System.Collections.Generic;
using System.Linq;
namespace PKHeX.Core
{
/// <summary>
/// Logic for generating a large amount of <see cref="PKM"/> data.
/// </summary>
public static class BulkGenerator
{
public static IList<PKM> GetLivingDex(SaveFile sav)
{
var bd = sav.BoxData;
var tr = sav;
for (int i = 1; i <= sav.MaxSpeciesID; i++) // should really get a list of valid species IDs
{
var pk = sav.BlankPKM;
pk.Species = i;
pk.Gender = pk.GetSaneGender();
if (i == (int)Species.Meowstic)
pk.AltForm = pk.Gender;
var f = EncounterMovesetGenerator.GeneratePKMs(pk, tr).FirstOrDefault();
if (f == null)
continue;
var converted = PKMConverter.ConvertToType(f, sav.PKMType, out _);
if (converted != null)
bd[i] = converted;
}
return bd;
}
}
}

View file

@ -912,7 +912,7 @@ namespace PKHeX.Core
/// </summary>
/// <remarks>
/// If a <see cref="PKM"/> originated in a generation prior to Generation 6, the <see cref="EncryptionConstant"/> is updated.
/// If a <see cref="PKM"/> is in the <see cref="_K12"/> format, it will update the <see cref="IVs"/> instead.
/// If a <see cref="PKM"/> is in the <see cref="GBPKM"/> format, it will update the <see cref="IVs"/> instead.
/// </remarks>
public virtual void SetShiny()
{

View file

@ -915,20 +915,6 @@ namespace PKHeX.Core
}
#endregion
// RTC
public sealed class RTC3
{
public readonly byte[] Data;
public const int Size = 8;
public RTC3(byte[] data) => Data = data;
public int Day { get => BitConverter.ToUInt16(Data, 0x00); set => BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x00); }
public int Hour { get => Data[2]; set => Data[2] = (byte)value; }
public int Minute { get => Data[3]; set => Data[3] = (byte)value; }
public int Second { get => Data[4]; set => Data[4] = (byte)value; }
}
public RTC3 ClockInitial
{
get
@ -1040,4 +1026,18 @@ namespace PKHeX.Core
return true;
}
}
// RTC
public sealed class RTC3
{
public readonly byte[] Data;
public const int Size = 8;
public RTC3(byte[] data) => Data = data;
public int Day { get => BitConverter.ToUInt16(Data, 0x00); set => BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x00); }
public int Hour { get => Data[2]; set => Data[2] = (byte)value; }
public int Minute { get => Data[3]; set => Data[3] = (byte)value; }
public int Second { get => Data[4]; set => Data[4] = (byte)value; }
}
}

View file

@ -112,7 +112,7 @@ namespace PKHeX.Core
private byte[] GetInnerData()
{
StrategyMemo.FinalData.CopyTo(Data, Memo);
StrategyMemo.Write().CopyTo(Data, Memo);
SetChecksums();
// Get updated save slot data

View file

@ -132,8 +132,8 @@ namespace PKHeX.Core
private byte[] GetInnerData()
{
// Set Memo Back
StrategyMemo.FinalData.CopyTo(Data, Memo);
ShadowInfo.FinalData.CopyTo(Data, Shadow);
StrategyMemo.Write().CopyTo(Data, Memo);
ShadowInfo.Write().CopyTo(Data, Shadow);
SetChecksums();
// Get updated save slot data

View file

@ -203,7 +203,7 @@ namespace PKHeX.Core
public abstract PlayerData5 PlayerData { get; }
public abstract BattleSubway5 BattleSubway { get; }
public int GetMailOffset(int index) => (index * Mail5.SIZE) + 0x1DD00;
public static int GetMailOffset(int index) => (index * Mail5.SIZE) + 0x1DD00;
public byte[] GetMailData(int offset) => GetData(offset, Mail5.SIZE);
}
}

View file

@ -39,7 +39,7 @@ namespace PKHeX.Core
public ulong RNGSeed2 { get => BitConverter.ToUInt64(Data, 0x1B0); set => BitConverter.GetBytes(value).CopyTo(Data, 0x1B0); }
public int Background { get => BitConverter.ToInt32(Data, 0x1BC); set => BitConverter.GetBytes(value).CopyTo(Data, 0x1BC); }
public int _1CE { get => BitConverter.ToUInt16(Data, 0x1CE); set => BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x1CE); }
public int Unk1CE { get => BitConverter.ToUInt16(Data, 0x1CE); set => BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x1CE); }
public int IntroID { get => BitConverter.ToUInt16(Data, 0x1E4); set => BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x1E4); }
public int MusicID { get => BitConverter.ToUInt16(Data, 0x1F0); set => BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x1F0); }

View file

@ -34,7 +34,7 @@ namespace PKHeX.Core
return entry;
}
public byte[] FinalData => Entries.SelectMany(entry => entry.Data).Take(MaxLength).ToArray();
public byte[] Write() => Entries.SelectMany(entry => entry.Data).Take(MaxLength).ToArray();
public ShadowInfoEntryXD GetEntry(int Species, uint PID)
{

View file

@ -7,7 +7,7 @@ namespace PKHeX.Core
public sealed class StrategyMemo
{
private readonly bool XD;
private const int SIZE_ENTRY = 12;
public const int SIZE_ENTRY = 12;
private readonly List<StrategyMemoEntry> Entries = new List<StrategyMemoEntry>();
public const int MAX_COUNT = 500;
public const int MAX_SIZE = MAX_COUNT * SIZE_ENTRY;
@ -38,7 +38,7 @@ namespace PKHeX.Core
return new StrategyMemoEntry(XD, data);
}
public byte[] FinalData => BigEndian.GetBytes((short)Entries.Count).Concat(_unk) // count followed by populated entries
public byte[] Write() => BigEndian.GetBytes((short)Entries.Count).Concat(_unk) // count followed by populated entries
.Concat(Entries.SelectMany(entry => entry.Data)).ToArray();
public StrategyMemoEntry GetEntry(int Species)
@ -54,75 +54,75 @@ namespace PKHeX.Core
else
Entries.Add(entry);
}
}
public sealed class StrategyMemoEntry
public sealed class StrategyMemoEntry
{
public readonly byte[] Data;
private readonly bool XD;
public StrategyMemoEntry(bool xd) : this(xd, new byte[StrategyMemo.SIZE_ENTRY]) { }
public StrategyMemoEntry(bool xd, byte[] data)
{
public readonly byte[] Data;
private readonly bool XD;
public StrategyMemoEntry(bool xd) : this(xd, new byte[SIZE_ENTRY]) { }
public StrategyMemoEntry(bool xd, byte[] data)
{
Data = data;
XD = xd;
}
public int Species
{
get
{
int val = BigEndian.ToUInt16(Data, 0) & 0x1FF;
return SpeciesConverter.GetG4Species(val);
}
set
{
value = SpeciesConverter.GetG3Species(value);
int cval = BigEndian.ToUInt16(Data, 0);
cval &= 0xE00; value &= 0x1FF; cval |= value;
BigEndian.GetBytes((ushort)cval).CopyTo(Data, 0);
}
}
private bool Flag0 { get => Data[0] >> 6 == 1; set { Data[0] &= 0xBF; if (value) Data[0] |= 0x40; } } // Unused
private bool Flag1 { get => Data[0] >> 7 == 1; set { Data[0] &= 0x7F; if (value) Data[0] |= 0x80; } } // Complete Entry
public int SID { get => BigEndian.ToUInt16(Data, 4); set => BigEndian.GetBytes((ushort)value).CopyTo(Data, 4); }
public int TID { get => BigEndian.ToUInt16(Data, 6); set => BigEndian.GetBytes((ushort)value).CopyTo(Data, 6); }
public uint PID { get => BigEndian.ToUInt32(Data, 8); set => BigEndian.GetBytes(value).CopyTo(Data, 8); }
public bool Seen
{
get
{
if (XD) return !Flag1;
return Species != 0;
}
set
{
if (XD)
Flag1 = !value;
else if (!value)
new byte[SIZE_ENTRY].CopyTo(Data, 0);
}
}
public bool Owned
{
get
{
if (XD) return false;
return Flag0 || !Flag1;
}
set
{
if (XD) return;
if (!value)
Flag1 = true;
}
}
public bool IsEmpty => Species == 0;
public bool Matches(int species, uint pid, int tid, int sid) => Species == species && PID == pid && TID == tid && SID == sid;
Data = data;
XD = xd;
}
public int Species
{
get
{
int val = BigEndian.ToUInt16(Data, 0) & 0x1FF;
return SpeciesConverter.GetG4Species(val);
}
set
{
value = SpeciesConverter.GetG3Species(value);
int cval = BigEndian.ToUInt16(Data, 0);
cval &= 0xE00; value &= 0x1FF; cval |= value;
BigEndian.GetBytes((ushort)cval).CopyTo(Data, 0);
}
}
private bool Flag0 { get => Data[0] >> 6 == 1; set { Data[0] &= 0xBF; if (value) Data[0] |= 0x40; } } // Unused
private bool Flag1 { get => Data[0] >> 7 == 1; set { Data[0] &= 0x7F; if (value) Data[0] |= 0x80; } } // Complete Entry
public int SID { get => BigEndian.ToUInt16(Data, 4); set => BigEndian.GetBytes((ushort)value).CopyTo(Data, 4); }
public int TID { get => BigEndian.ToUInt16(Data, 6); set => BigEndian.GetBytes((ushort)value).CopyTo(Data, 6); }
public uint PID { get => BigEndian.ToUInt32(Data, 8); set => BigEndian.GetBytes(value).CopyTo(Data, 8); }
public bool Seen
{
get
{
if (XD) return !Flag1;
return Species != 0;
}
set
{
if (XD)
Flag1 = !value;
else if (!value)
new byte[StrategyMemo.SIZE_ENTRY].CopyTo(Data, 0);
}
}
public bool Owned
{
get
{
if (XD) return false;
return Flag0 || !Flag1;
}
set
{
if (XD) return;
if (!value)
Flag1 = true;
}
}
public bool IsEmpty => Species == 0;
public bool Matches(int species, uint pid, int tid, int sid) => Species == species && PID == pid && TID == tid && SID == sid;
}
}

View file

@ -108,27 +108,6 @@ namespace PKHeX.WinForms
public void Dispose() => Brushes.Dispose();
public sealed class BrushSet : IDisposable
{
public Brush Text { get; set; }
public Brush BackLegal { get; set; }
public Brush BackDefault { get; set; }
public Brush TextHighlighted { get; set; }
public Brush BackHighlighted { get; set; }
public Brush GetText(bool highlight) => highlight ? TextHighlighted : Text;
public Brush GetBackground(bool legal, bool highlight) => highlight ? BackHighlighted : (legal ? BackLegal : BackDefault);
public void Dispose()
{
Text.Dispose();
BackLegal.Dispose();
BackDefault.Dispose();
TextHighlighted.Dispose();
BackHighlighted.Dispose();
}
}
public override string ToString()
{
var props = ReflectUtil.GetAllPropertyInfoCanWritePublic(typeof(DrawConfig));
@ -139,11 +118,7 @@ namespace PKHeX.WinForms
continue;
var name = p.Name;
object value;
if (p.PropertyType == typeof(Color))
value = ((Color)p.GetValue(this)).ToArgb();
else
value = p.GetValue(this);
var value = p.PropertyType == typeof(Color) ? ((Color)p.GetValue(this)).ToArgb() : p.GetValue(this);
lines.Add($"{name}\t{value}");
}
return string.Join("\n", lines);
@ -189,4 +164,25 @@ namespace PKHeX.WinForms
}
}
}
public sealed class BrushSet : IDisposable
{
public Brush Text { get; set; }
public Brush BackLegal { get; set; }
public Brush BackDefault { get; set; }
public Brush TextHighlighted { get; set; }
public Brush BackHighlighted { get; set; }
public Brush GetText(bool highlight) => highlight ? TextHighlighted : Text;
public Brush GetBackground(bool legal, bool highlight) => highlight ? BackHighlighted : (legal ? BackLegal : BackDefault);
public void Dispose()
{
Text.Dispose();
BackLegal.Dispose();
BackDefault.Dispose();
TextHighlighted.Dispose();
BackHighlighted.Dispose();
}
}
}

View file

@ -422,7 +422,14 @@ namespace PKHeX.WinForms.Controls
return;
}
if (M.Boxes.Count > 1) // subview open
{ var z = M.Boxes[1].ParentForm; z.CenterToForm(ParentForm); z.BringToFront(); return; }
{
var z = M.Boxes[1].ParentForm;
if (z == null)
return;
z.CenterToForm(ParentForm);
z.BringToFront();
return;
}
new SAV_BoxViewer(this, M).Show();
}
@ -1198,12 +1205,6 @@ namespace PKHeX.WinForms.Controls
ResetParty();
}
private void GenerateLivingDex()
{
SAV.BoxData = GetLivingDex(SAV);
ReloadSlots();
}
private static PKMImportSetting GetPKMSetOverride(bool currentSetting)
{
var yn = currentSetting ? MsgYes : MsgNo;
@ -1219,23 +1220,5 @@ namespace PKHeX.WinForms.Controls
_ => PKMImportSetting.UseDefault
};
}
private static IList<PKM> GetLivingDex(SaveFile SAV)
{
var bd = SAV.BoxData;
var tr = SAV;
for (int i = 1; i <= 807; i++)
{
var pk = SAV.BlankPKM;
pk.Species = i;
pk.Gender = pk.GetSaneGender();
if (i == 678)
pk.AltForm = pk.Gender;
var f = EncounterMovesetGenerator.GeneratePKMs(pk, tr).FirstOrDefault();
if (f != null)
bd[i] = PKMConverter.ConvertToType(f, SAV.PKMType, out _);
}
return bd;
}
}
}

View file

@ -479,8 +479,10 @@ namespace PKHeX.WinForms
private void MainMenuFolder(object sender, EventArgs e)
{
if (!this.OpenWindowExists<SAV_FolderList>())
new SAV_FolderList(s => OpenSAV(SaveUtil.GetVariantSAV(s.FilePath), s.FilePath)).Show();
if (this.OpenWindowExists<SAV_FolderList>())
return;
var form = new SAV_FolderList(s => OpenSAV(SaveUtil.GetVariantSAV(s.FilePath), s.FilePath));
form.Show();
}
// Misc Options

View file

@ -20,8 +20,8 @@ namespace PKHeX.WinForms
LoadData();
}
private readonly SAV3.RTC3 ClockInitial;
private readonly SAV3.RTC3 ClockElapsed;
private readonly RTC3 ClockInitial;
private readonly RTC3 ClockElapsed;
private void LoadData()
{

View file

@ -99,7 +99,7 @@ namespace PKHeX.WinForms
m[i] = new Mail5(((PK5)p[i]).HeldMailData);
for (int i = p.Count, j = 0; i < m.Length; i++, j++)
{
int ofs = sav5.GetMailOffset(j);
int ofs = SAV5.GetMailOffset(j);
var data = sav5.GetMailData(ofs);
m[i] = new Mail5(data, ofs);
}

View file

@ -6,17 +6,15 @@ using Xunit;
namespace PKHeX.Tests.PKM
{
public static class PKMTests
public class StringTests
{
public class StringTests
[Fact]
public void EncodesOTNameCorrectly()
{
[Fact]
public void EncodesOTNameCorrectly()
const string name_fabian = "Fabian♂";
var pkm = new PK7 { OT_Name = name_fabian };
var byte_fabian = new byte[]
{
const string name_fabian = "Fabian♂";
var pkm = new PK7 { OT_Name = name_fabian };
var byte_fabian = new byte[]
{
0x46, 0x00, // F
0x61, 0x00, // a
0x62, 0x00, // b
@ -25,189 +23,188 @@ namespace PKHeX.Tests.PKM
0x6E, 0x00, // n
0x8E, 0xE0, // ♂
0x00, 0x00, // \0 terminator
};
CheckStringGetSet(nameof(pkm.OT_Name), name_fabian, pkm.OT_Name, byte_fabian, pkm.OT_Trash);
}
};
CheckStringGetSet(nameof(pkm.OT_Name), name_fabian, pkm.OT_Name, byte_fabian, pkm.OT_Trash);
}
[Fact]
public void EncodesNicknameCorrectly()
[Fact]
public void EncodesNicknameCorrectly()
{
const string name_nidoran = "ニドラン♀";
var pkm = new PK7 { Nickname = name_nidoran };
var byte_nidoran = new byte[]
{
const string name_nidoran = "ニドラン♀";
var pkm = new PK7 { Nickname = name_nidoran };
var byte_nidoran = new byte[]
{
0xCB, 0x30, // ニ
0xC9, 0x30, // ド
0xE9, 0x30, // ラ
0xF3, 0x30, // ン
0x40, 0x26, // ♀
0x00, 0x00, // \0 terminator
};
CheckStringGetSet(nameof(pkm.Nickname), name_nidoran, pkm.Nickname, byte_nidoran, pkm.Nickname_Trash);
}
private static void CheckStringGetSet(string check, string instr, string outstr, byte[] indata,
byte[] outdata)
{
instr.Should().BeEquivalentTo(outstr);
outdata = outdata.Take(indata.Length).ToArray();
indata.SequenceEqual(outdata).Should()
.BeTrue($"expected {check} to set properly, instead got {string.Join(", ", outdata.Select(z => $"{z:X2}"))}");
}
};
CheckStringGetSet(nameof(pkm.Nickname), name_nidoran, pkm.Nickname, byte_nidoran, pkm.Nickname_Trash);
}
public class MetDateTests
private static void CheckStringGetSet(string check, string instr, string outstr, byte[] indata,
byte[] outdata)
{
[Fact]
public void MetDateNullWhenDateComponentsAreAllZero()
instr.Should().BeEquivalentTo(outstr);
outdata = outdata.Take(indata.Length).ToArray();
indata.SequenceEqual(outdata).Should()
.BeTrue($"expected {check} to set properly, instead got {string.Join(", ", outdata.Select(z => $"{z:X2}"))}");
}
}
public class MetDateTests
{
[Fact]
public void MetDateNullWhenDateComponentsAreAllZero()
{
var pk = new PK7
{
var pk = new PK7
{
Met_Day = 0,
Met_Month = 0,
Met_Year = 0
};
Met_Day = 0,
Met_Month = 0,
Met_Year = 0
};
pk.MetDate.HasValue.Should().BeFalse();
}
[Fact]
public void MetDateReturnsCorrectDate()
{
var pk = new PK7
{
Met_Day = 10,
Met_Month = 8,
Met_Year = 16
};
pk.MetDate.GetValueOrDefault().Should().Be(new DateTime(2016, 8, 10).Date);
}
[Fact]
public void MetDateCalculatesYear0Correctly()
{
var pk = new PK7
{
Met_Day = 1,
Met_Month = 1,
Met_Year = 0
};
pk.MetDate.GetValueOrDefault().Date.Year.Should().Be(2000);
}
[Fact]
public void SettingToNullZerosComponents()
{
var pk = new PK7
{
Met_Day = 12,
Met_Month = 12,
Met_Year = 12
};
pk.MetDate = null;
pk.Met_Day.Should().Be(0);
pk.Met_Month.Should().Be(0);
pk.Met_Year.Should().Be(0);
}
[Fact]
public void SettingMetDateSetsComponents()
{
var pk = new PK7
{
Met_Day = 12,
Met_Month = 12,
Met_Year = 12
};
pk.MetDate = new DateTime(2005, 5, 5);
pk.Met_Day.Should().Be(5);
pk.Met_Month.Should().Be(5);
pk.Met_Year.Should().Be(5);
}
pk.MetDate.HasValue.Should().BeFalse();
}
public class EggMetDateTests
[Fact]
public void MetDateReturnsCorrectDate()
{
[Fact]
public void EggMetDateNullWhenDateComponentsAreAllZero()
var pk = new PK7
{
var pk = new PK7
{
Egg_Day = 0,
Egg_Month = 0,
Egg_Year = 0
};
Met_Day = 10,
Met_Month = 8,
Met_Year = 16
};
pk.EggMetDate.HasValue.Should().BeFalse();
}
pk.MetDate.GetValueOrDefault().Should().Be(new DateTime(2016, 8, 10).Date);
}
[Fact]
public void EggMetDateReturnsCorrectDate()
[Fact]
public void MetDateCalculatesYear0Correctly()
{
var pk = new PK7
{
var pk = new PK7
{
Egg_Day = 10,
Egg_Month = 8,
Egg_Year = 16
};
Met_Day = 1,
Met_Month = 1,
Met_Year = 0
};
pk.EggMetDate.GetValueOrDefault().Should().Be(new DateTime(2016, 8, 10).Date);
}
pk.MetDate.GetValueOrDefault().Date.Year.Should().Be(2000);
}
[Fact]
public void EggMetDateCalculatesYear0Correctly()
[Fact]
public void SettingToNullZerosComponents()
{
var pk = new PK7
{
var pk = new PK7
{
Egg_Day = 1,
Egg_Month = 1,
Egg_Year = 0
};
Met_Day = 12,
Met_Month = 12,
Met_Year = 12
};
pk.EggMetDate.GetValueOrDefault().Date.Year.Should().Be(2000);
}
pk.MetDate = null;
[Fact]
public void SettingEggMetDateToNullZerosComponents()
pk.Met_Day.Should().Be(0);
pk.Met_Month.Should().Be(0);
pk.Met_Year.Should().Be(0);
}
[Fact]
public void SettingMetDateSetsComponents()
{
var pk = new PK7
{
var pk = new PK7
{
Egg_Day = 12,
Egg_Month = 12,
Egg_Year = 12
};
Met_Day = 12,
Met_Month = 12,
Met_Year = 12
};
pk.EggMetDate = null;
pk.MetDate = new DateTime(2005, 5, 5);
pk.Egg_Day.Should().Be(0);
pk.Egg_Month.Should().Be(0);
pk.Egg_Year.Should().Be(0);
}
pk.Met_Day.Should().Be(5);
pk.Met_Month.Should().Be(5);
pk.Met_Year.Should().Be(5);
}
}
[Fact]
public void SettingEggMetDateSetsComponents()
public class EggMetDateTests
{
[Fact]
public void EggMetDateNullWhenDateComponentsAreAllZero()
{
var pk = new PK7
{
var pk = new PK7
{
Egg_Day = 12,
Egg_Month = 12,
Egg_Year = 12
};
Egg_Day = 0,
Egg_Month = 0,
Egg_Year = 0
};
pk.EggMetDate = new DateTime(2005, 5, 5);
pk.EggMetDate.HasValue.Should().BeFalse();
}
pk.Egg_Day.Should().Be(5);
pk.Egg_Month.Should().Be(5);
pk.Egg_Year.Should().Be(5);
}
[Fact]
public void EggMetDateReturnsCorrectDate()
{
var pk = new PK7
{
Egg_Day = 10,
Egg_Month = 8,
Egg_Year = 16
};
pk.EggMetDate.GetValueOrDefault().Should().Be(new DateTime(2016, 8, 10).Date);
}
[Fact]
public void EggMetDateCalculatesYear0Correctly()
{
var pk = new PK7
{
Egg_Day = 1,
Egg_Month = 1,
Egg_Year = 0
};
pk.EggMetDate.GetValueOrDefault().Date.Year.Should().Be(2000);
}
[Fact]
public void SettingEggMetDateToNullZerosComponents()
{
var pk = new PK7
{
Egg_Day = 12,
Egg_Month = 12,
Egg_Year = 12
};
pk.EggMetDate = null;
pk.Egg_Day.Should().Be(0);
pk.Egg_Month.Should().Be(0);
pk.Egg_Year.Should().Be(0);
}
[Fact]
public void SettingEggMetDateSetsComponents()
{
var pk = new PK7
{
Egg_Day = 12,
Egg_Month = 12,
Egg_Year = 12
};
pk.EggMetDate = new DateTime(2005, 5, 5);
pk.Egg_Day.Should().Be(5);
pk.Egg_Month.Should().Be(5);
pk.Egg_Year.Should().Be(5);
}
}
}