Relocate some logic to interfaces

This commit is contained in:
Kurt
2018-12-18 17:15:35 -08:00
parent ff649009ca
commit d7a501c168
13 changed files with 80 additions and 97 deletions

View File

@@ -240,7 +240,6 @@ protected override byte[] Write(bool DSV)
// Configuration
public override SaveFile Clone() { return new SAV3(Write(DSV:false), Version) {Japanese = Japanese}; }
public override bool IndeterminateSubVersion => Version == GameVersion.FRLG;
public override int SIZE_STORED => PKX.SIZE_3STORED;
protected override int SIZE_PARTY => PKX.SIZE_3PARTY;
@@ -794,7 +793,7 @@ public override byte[] SetString(string value, int maxLength, int PadToSize = 0,
private uint EBerryChecksum => BitConverter.ToUInt32(Data, BlockOfs[4] + OFFSET_EBERRY + SIZE_EBERRY - 4);
private bool IsEBerryChecksumValid { get; set; }
public override string EBerryName
public string EBerryName
{
get
{
@@ -804,7 +803,7 @@ public override string EBerryName
}
}
public override bool IsEBerryIsEnigma => string.IsNullOrEmpty(EBerryName.Trim());
public bool IsEBerryIsEnigma => string.IsNullOrEmpty(EBerryName.Trim());
private void LoadEReaderBerryData()
{

View File

@@ -7,7 +7,7 @@ namespace PKHeX.Core
/// <summary>
/// Generation 6 <see cref="SaveFile"/> object.
/// </summary>
public sealed class SAV6 : SaveFile, ITrainerStatRecord
public sealed class SAV6 : SaveFile, ITrainerStatRecord, IPokePuff, ISecureValueStorage
{
// Save Data Attributes
protected override string BAKText => $"{OT} ({Version}) - {LastSavedTime}";
@@ -56,6 +56,8 @@ public SAV6(byte[] data = null)
public override int MaxGameID => Legal.MaxGameID_6; // OR
// Feature Overrides
public int Puff { get; set; } = -1;
public bool HasPuffData => Puff >= 0;
// Blocks & Offsets
private readonly int BlockInfoOffset;
@@ -64,16 +66,16 @@ public SAV6(byte[] data = null)
public override bool ChecksumsValid => Blocks.GetChecksumsValid(Data);
public override string ChecksumInfo => Blocks.GetChecksumInfo(Data);
public override ulong? Secure1
public ulong TimeStampCurrent
{
get => BitConverter.ToUInt64(Data, BlockInfoOffset - 0x14);
set => BitConverter.GetBytes(value ?? 0).CopyTo(Data, BlockInfoOffset - 0x14);
set => BitConverter.GetBytes(value).CopyTo(Data, BlockInfoOffset - 0x14);
}
public override ulong? Secure2
public ulong TimeStampPrevious
{
get => BitConverter.ToUInt64(Data, BlockInfoOffset - 0xC);
set => BitConverter.GetBytes(value ?? 0).CopyTo(Data, BlockInfoOffset - 0xC);
set => BitConverter.GetBytes(value).CopyTo(Data, BlockInfoOffset - 0xC);
}
private void GetSAVOffsets()
@@ -632,7 +634,7 @@ public override void SetDaycareHasEgg(int loc, bool hasEgg)
Data[ofs + 0x1E0] = (byte)(hasEgg ? 1 : 0);
}
public byte[] Puffs { get => GetData(Puff, 100); set => value.CopyTo(Data, Puff); }
public byte[] Puffs { get => GetData(Puff, 100); set => SetData(value, Puff); }
public int PuffCount { get => BitConverter.ToInt32(Data, Puff + 100); set => BitConverter.GetBytes(value).CopyTo(Data, Puff + 100); }
public int[] SelectItems

View File

@@ -8,7 +8,7 @@ namespace PKHeX.Core
/// <summary>
/// Generation 7 <see cref="SaveFile"/> object.
/// </summary>
public sealed class SAV7 : SaveFile, ITrainerStatRecord
public sealed class SAV7 : SaveFile, ITrainerStatRecord, ISecureValueStorage
{
// Save Data Attributes
protected override string BAKText => $"{OT} ({Version}) - {LastSavedTime}";
@@ -109,16 +109,16 @@ protected override void SetChecksums()
IsMemeCryptoApplied = true;
}
public override ulong? Secure1
public ulong TimeStampCurrent
{
get => BitConverter.ToUInt64(Data, BlockInfoOffset - 0x14);
set => BitConverter.GetBytes(value ?? 0).CopyTo(Data, BlockInfoOffset - 0x14);
set => BitConverter.GetBytes(value).CopyTo(Data, BlockInfoOffset - 0x14);
}
public override ulong? Secure2
public ulong TimeStampPrevious
{
get => BitConverter.ToUInt64(Data, BlockInfoOffset - 0xC);
set => BitConverter.GetBytes(value ?? 0).CopyTo(Data, BlockInfoOffset - 0xC);
set => BitConverter.GetBytes(value).CopyTo(Data, BlockInfoOffset - 0xC);
}
private void GetSAVOffsets()

View File

@@ -5,7 +5,7 @@
namespace PKHeX.Core
{
public sealed class SAV7b : SaveFile
public sealed class SAV7b : SaveFile, ISecureValueStorage
{
protected override string BAKText => $"{OT} ({Version}) - {Played.LastSavedTime}";
public override string Filter => "savedata|*.bin";
@@ -195,16 +195,16 @@ public override byte[] SetString(string value, int maxLength, int PadToSize = 0,
return StringConverter.SetString7b(value, maxLength, Language, PadToSize, PadWith);
}
public override ulong? Secure1
public ulong TimeStampCurrent
{
get => BitConverter.ToUInt64(Data, BlockInfoOffset - 0x14);
set => BitConverter.GetBytes(value ?? 0).CopyTo(Data, BlockInfoOffset - 0x14);
set => BitConverter.GetBytes(value).CopyTo(Data, BlockInfoOffset - 0x14);
}
public override ulong? Secure2
public ulong TimeStampPrevious
{
get => BitConverter.ToUInt64(Data, BlockInfoOffset - 0xC);
set => BitConverter.GetBytes(value ?? 0).CopyTo(Data, BlockInfoOffset - 0xC);
set => BitConverter.GetBytes(value).CopyTo(Data, BlockInfoOffset - 0xC);
}
public override GameVersion Version

View File

@@ -28,7 +28,6 @@ public abstract class SaveFile : ITrainerInfo, IGameValueLimit
public bool Japanese { get; protected set; }
public virtual string PlayTimeString => $"{PlayedHours}ː{PlayedMinutes:00}ː{PlayedSeconds:00}"; // not :
public bool IndeterminateGame => Version == GameVersion.Unknown;
public virtual bool IndeterminateSubVersion => false;
public abstract string Extension { get; }
public virtual string[] PKMExtensions => PKM.Extensions.Where(f =>
@@ -103,7 +102,6 @@ protected virtual byte[] Write(bool DSV)
public bool HasBerryField => BerryField > -1;
public bool HasHoF => HoF > -1;
public bool HasSecretBase => SecretBase > -1;
public bool HasPuff => Puff > -1;
public bool HasPSS => PSS > -1;
public bool HasOPower => OPower > -1;
public bool HasJPEG => JPEGData != null;
@@ -148,7 +146,6 @@ protected virtual byte[] Write(bool DSV)
public int PokeDex { get; protected set; } = int.MinValue;
public int SuperTrain { get; protected set; } = int.MinValue;
public int SecretBase { get; protected set; } = int.MinValue;
public int Puff { get; protected set; } = int.MinValue;
public int PSS { get; protected set; } = int.MinValue;
public int BerryField { get; protected set; } = int.MinValue;
public int OPower { get; protected set; } = int.MinValue;
@@ -430,8 +427,6 @@ public bool IsPartyAllEggs(params int[] except)
public abstract void SetBoxName(int box, string value);
public virtual int GameSyncIDSize { get; } = 8;
public virtual string GameSyncID { get => null; set { } }
public virtual ulong? Secure1 { get => null; set { } }
public virtual ulong? Secure2 { get => null; set { } }
// Daycare
public int DaycareIndex = 0;
@@ -839,9 +834,6 @@ public bool IsRangeAll(int Offset, int Length, int value)
public string GetString(int offset, int length) => GetString(Data, offset, length);
public abstract byte[] SetString(string value, int maxLength, int PadToSize = 0, ushort PadWith = 0);
public virtual string EBerryName => string.Empty;
public virtual bool IsEBerryIsEnigma => true;
/// <summary>
/// Compresses the <see cref="BoxData"/> by pulling out the empty storage slots and putting them at the end, retaining all existing data.
/// </summary>

View File

@@ -0,0 +1,9 @@
namespace PKHeX.Core
{
public interface IPokePuff
{
bool HasPuffData { get; }
byte[] Puffs { get; set; }
int PuffCount { get; set; }
}
}

View File

@@ -0,0 +1,8 @@
namespace PKHeX.Core
{
public interface ISecureValueStorage
{
ulong TimeStampPrevious { get; set; }
ulong TimeStampCurrent { get; set; }
}
}

View File

@@ -500,13 +500,13 @@ private void UpdateStringSeed(object sender, EventArgs e)
SAV.GameSyncID = value;
SAV.Edited = true;
}
else if (SAV.Generation >= 6)
else if (SAV is ISecureValueStorage s)
{
var value = Convert.ToUInt64(filterText, 16);
if (tb == TB_Secure1)
SAV.Secure1 = value;
s.TimeStampCurrent = value;
else if (tb == TB_Secure2)
SAV.Secure2 = value;
s.TimeStampPrevious = value;
SAV.Edited = true;
}
}
@@ -966,7 +966,7 @@ private void ToggleViewSubEditors(SaveFile sav)
PAN_BattleBox.Visible = L_BattleBox.Visible = L_ReadOnlyPBB.Visible = sav.HasBattleBox;
GB_Daycare.Visible = sav.HasDaycare;
B_OpenSecretBase.Enabled = sav.HasSecretBase;
B_OpenPokepuffs.Enabled = sav.HasPuff;
B_OpenPokepuffs.Enabled = sav is IPokePuff p && p.HasPuffData;
B_OUTPasserby.Enabled = sav.HasPSS;
B_OpenBoxLayout.Enabled = sav.HasNamableBoxes;
B_OpenWondercards.Enabled = sav.HasWondercards;
@@ -1020,21 +1020,23 @@ private void ToggleViewMisc(SaveFile sav)
L_SaveSlot.Visible = CB_SaveSlot.Visible = false;
}
if (sav is ISecureValueStorage s)
{
TB_Secure1.Text = s.TimeStampCurrent.ToString("X16");
TB_Secure2.Text = s.TimeStampPrevious.ToString("X16");
}
switch (sav.Generation)
{
case 6:
TB_GameSync.Enabled = sav.GameSyncID != null;
TB_GameSync.MaxLength = sav.GameSyncIDSize;
TB_GameSync.Text = (sav.GameSyncID ?? 0.ToString()).PadLeft(sav.GameSyncIDSize, '0');
TB_Secure1.Text = sav.Secure1?.ToString("X16");
TB_Secure2.Text = sav.Secure2?.ToString("X16");
break;
case 7:
TB_GameSync.Enabled = sav.GameSyncID != null;
TB_GameSync.MaxLength = sav.GameSyncIDSize;
TB_GameSync.Text = (sav.GameSyncID ?? 0.ToString()).PadLeft(sav.GameSyncIDSize, '0');
TB_Secure1.Text = sav.Secure1?.ToString("X16");
TB_Secure2.Text = sav.Secure2?.ToString("X16");
break;
}
}

View File

@@ -674,8 +674,11 @@ private static bool CheckGCMemoryCard(SAV3GCMemoryCard MC, string path)
private static void StoreLegalSaveGameData(SaveFile sav)
{
Legal.EReaderBerryIsEnigma = sav.IsEBerryIsEnigma;
Legal.EReaderBerryName = sav.EBerryName;
if (sav is SAV3 sav3)
{
Legal.EReaderBerryIsEnigma = sav3.IsEBerryIsEnigma;
Legal.EReaderBerryName = sav3.EBerryName;
}
}
private static PKM LoadTemplate(SaveFile sav)
@@ -827,7 +830,7 @@ private static bool SanityCheckSAV(ref SaveFile sav)
if (sav.Version == GameVersion.FRLG)
sav.Personal = dialog.Result == GameVersion.FR ? PersonalTable.FR : PersonalTable.LG;
}
else if (sav.IndeterminateSubVersion && sav.Version == GameVersion.FRLG)
else if (sav.Version == GameVersion.FRLG) // IndeterminateSubVersion
{
string fr = GameInfo.VersionDataSource.First(r => r.Value == (int) GameVersion.FR).Text;
string lg = GameInfo.VersionDataSource.First(l => l.Value == (int) GameVersion.LG).Text;

View File

@@ -35,8 +35,6 @@ private void InitializeComponent()
this.B_All = new System.Windows.Forms.Button();
this.B_Sort = new System.Windows.Forms.Button();
this.B_None = new System.Windows.Forms.Button();
this.L_Count = new System.Windows.Forms.Label();
this.MT_CNT = new System.Windows.Forms.MaskedTextBox();
((System.ComponentModel.ISupportInitialize)(this.dgv)).BeginInit();
this.SuspendLayout();
//
@@ -61,7 +59,7 @@ private void InitializeComponent()
this.dgv.ShowEditingIcon = false;
this.dgv.Size = new System.Drawing.Size(200, 186);
this.dgv.TabIndex = 11;
this.dgv.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(SAV_Pokepuff.DropClick);
this.dgv.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.DropClick);
//
// B_Save
//
@@ -115,34 +113,11 @@ private void InitializeComponent()
this.B_None.UseVisualStyleBackColor = true;
this.B_None.Click += new System.EventHandler(this.B_None_Click);
//
// L_Count
//
this.L_Count.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.L_Count.AutoSize = true;
this.L_Count.Location = new System.Drawing.Point(12, 240);
this.L_Count.Name = "L_Count";
this.L_Count.Size = new System.Drawing.Size(32, 13);
this.L_Count.TabIndex = 17;
this.L_Count.Text = "CNT:";
//
// MT_CNT
//
this.MT_CNT.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.MT_CNT.Location = new System.Drawing.Point(50, 237);
this.MT_CNT.Mask = "00000";
this.MT_CNT.Name = "MT_CNT";
this.MT_CNT.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.MT_CNT.Size = new System.Drawing.Size(39, 20);
this.MT_CNT.TabIndex = 18;
this.MT_CNT.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// SAV_Pokepuff
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(224, 272);
this.Controls.Add(this.MT_CNT);
this.Controls.Add(this.L_Count);
this.Controls.Add(this.B_None);
this.Controls.Add(this.B_Sort);
this.Controls.Add(this.B_All);
@@ -159,7 +134,6 @@ private void InitializeComponent()
this.Text = "Poké Puffs Editor";
((System.ComponentModel.ISupportInitialize)(this.dgv)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
@@ -171,7 +145,5 @@ private void InitializeComponent()
private System.Windows.Forms.Button B_All;
private System.Windows.Forms.Button B_Sort;
private System.Windows.Forms.Button B_None;
private System.Windows.Forms.Label L_Count;
private System.Windows.Forms.MaskedTextBox MT_CNT;
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using PKHeX.Core;
@@ -7,16 +8,17 @@ namespace PKHeX.WinForms
{
public partial class SAV_Pokepuff : Form
{
private readonly SaveFile Origin;
private readonly SAV6 SAV;
private readonly IPokePuff SAV;
public SAV_Pokepuff(SaveFile sav)
{
InitializeComponent();
WinFormsUtil.TranslateInterface(this, Main.CurrentLanguage);
SAV = (SAV6)(Origin = sav).Clone();
SAV = (SAV6)sav;
Setup();
var puffs = SAV.Puffs;
Setup(puffs.Length);
LoadPuffs(puffs);
new ToolTip().SetToolTip(B_Sort, "Hold CTRL to reverse sort.");
new ToolTip().SetToolTip(B_All, "Hold CTRL to best instead of varied.");
@@ -25,7 +27,7 @@ public SAV_Pokepuff(SaveFile sav)
private readonly string[] pfa = GameInfo.Strings.puffs;
private int PuffCount { get; set; }
private void Setup()
private void Setup(int rowCount)
{
dgv.Rows.Clear();
dgv.Columns.Clear();
@@ -52,10 +54,12 @@ private void Setup()
}
dgv.Columns.Add(dgvIndex);
dgv.Columns.Add(dgvPuff);
dgv.Rows.Add(rowCount);
}
var Puffs = SAV.Puffs;
private void LoadPuffs(byte[] Puffs)
{
PuffCount = Puffs.Length;
dgv.Rows.Add(Puffs.Length);
for (int i = 0; i < Puffs.Length; i++)
{
dgv.Rows[i].Cells[0].Value = (i + 1).ToString();
@@ -67,12 +71,12 @@ private void Setup()
}
dgv.Rows[i].Cells[1].Value = pfa[puffval];
}
MT_CNT.Text = SAV.PuffCount.ToString();
}
private static void DropClick(object sender, DataGridViewCellEventArgs e)
private void DropClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex != 1) return;
if (e.ColumnIndex != 1)
return;
((ComboBox)((DataGridView) sender).EditingControl).DroppedDown = true;
}
@@ -98,8 +102,7 @@ private void B_All_Click(object sender, EventArgs e)
Util.Shuffle(newpuffs);
}
Array.Copy(newpuffs, 0, SAV.Data, SAV.Puff, PuffCount);
Setup();
LoadPuffs(newpuffs);
}
private void B_None_Click(object sender, EventArgs e)
@@ -110,41 +113,34 @@ private void B_None_Click(object sender, EventArgs e)
newpuffs[2] = 3;
newpuffs[3] = 4;
newpuffs[4] = 5;
Array.Copy(newpuffs, 0, SAV.Data, SAV.Puff, PuffCount);
Setup();
LoadPuffs(newpuffs);
}
private void B_Sort_Click(object sender, EventArgs e)
{
var puffs = GetPuffs(false);
Array.Sort(puffs);
if (ModifierKeys == Keys.Control)
Array.Reverse(puffs);
Array.Copy(puffs, 0, SAV.Data, SAV.Puff, PuffCount);
Setup();
bool reverse = ModifierKeys == Keys.Control;
var puffs = GetPuffs().GroupBy(z => z != 0);
var result = puffs.SelectMany(z => reverse ? z.OrderByDescending(x => x) : z.OrderBy(x => x)).ToArray();
LoadPuffs(result);
}
private byte[] GetPuffs(bool resize = true)
private byte[] GetPuffs()
{
List<byte> puffs = new List<byte>();
var puffs = new List<byte>();
for (int i = 0; i < dgv.Rows.Count; i++)
{
string puff = dgv.Rows[i].Cells[1].Value.ToString();
int index = (byte)Array.IndexOf(pfa, puff);
puffs.Add((byte)index);
}
var arr = puffs.ToArray();
if (resize && arr.Length != PuffCount)
Array.Resize(ref arr, PuffCount);
return arr;
return puffs.ToArray();
}
private void B_Save_Click(object sender, EventArgs e)
{
SAV.Puffs = GetPuffs();
SAV.PuffCount = Util.ToInt32(MT_CNT.Text);
Origin.SetData(SAV.Data, 0);
var puffs = GetPuffs();
SAV.Puffs = puffs;
SAV.PuffCount = puffs.Length;
Close();
}
}