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,13 +54,14 @@ namespace PKHeX.Core
else
Entries.Add(entry);
}
}
public sealed class StrategyMemoEntry
{
public readonly byte[] Data;
private readonly bool XD;
public StrategyMemoEntry(bool xd) : this(xd, new byte[SIZE_ENTRY]) { }
public StrategyMemoEntry(bool xd) : this(xd, new byte[StrategyMemo.SIZE_ENTRY]) { }
public StrategyMemoEntry(bool xd, byte[] data)
{
@ -102,7 +103,7 @@ namespace PKHeX.Core
if (XD)
Flag1 = !value;
else if (!value)
new byte[SIZE_ENTRY].CopyTo(Data, 0);
new byte[StrategyMemo.SIZE_ENTRY].CopyTo(Data, 0);
}
}
@ -125,4 +126,3 @@ namespace PKHeX.Core
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

@ -5,8 +5,6 @@ using PKHeX.Core;
using Xunit;
namespace PKHeX.Tests.PKM
{
public static class PKMTests
{
public class StringTests
{
@ -210,4 +208,3 @@ namespace PKHeX.Tests.PKM
}
}
}
}