PKHeX/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_BerryFieldXY.cs
Kurt 47071b41f3
Refactoring: Span-based value writes and method signatures (#3361)
Existing `get`/`set` logic is flawed in that it doesn't work on Big Endian operating systems, and it allocates heap objects when it doesn't need to.

`System.Buffers.Binary.BinaryPrimitives` in the `System.Memory` NuGet package provides both Little Endian and Big Endian methods to read and write data; all the `get`/`set` operations have been reworked to use this new API. This removes the need for PKHeX's manual `BigEndian` class, as all functions are already covered by the BinaryPrimitives API.

The `StringConverter` has now been rewritten to accept a Span to read from & write to, no longer requiring a temporary StringBuilder.

Other Fixes included:
- The Super Training UI for Gen6 has been reworked according to the latest block structure additions.
- Cloning a Stadium2 Save File now works correctly (opening from the Folder browser list).
- Checksum & Sanity properties removed from parent PKM class, and is now implemented via interface.
2022-01-02 21:35:59 -08:00

52 lines
1.8 KiB
C#

using System;
using System.Windows.Forms;
using PKHeX.Core;
using static System.Buffers.Binary.BinaryPrimitives;
namespace PKHeX.WinForms
{
public partial class SAV_BerryFieldXY : Form
{
private readonly SAV6XY SAV;
public SAV_BerryFieldXY(SAV6XY sav)
{
InitializeComponent();
WinFormsUtil.TranslateInterface(this, Main.CurrentLanguage);
SAV = sav;
listBox1.SelectedIndex = 0;
}
private void Changefield(object sender, EventArgs e)
{
// Change Berry Field
// Gather Data
int ofs = SAV.BerryField + 0xC + (listBox1.SelectedIndex * 0x18);
int berry = ReadUInt16LittleEndian(SAV.Data.AsSpan(ofs + (2 * 0)));
int u1 = ReadUInt16LittleEndian(SAV.Data.AsSpan(ofs + (2 * 1)));
int u2 = ReadUInt16LittleEndian(SAV.Data.AsSpan(ofs + (2 * 2)));
int u3 = ReadUInt16LittleEndian(SAV.Data.AsSpan(ofs + (2 * 3)));
int u4 = ReadUInt16LittleEndian(SAV.Data.AsSpan(ofs + (2 * 4)));
int u5 = ReadUInt16LittleEndian(SAV.Data.AsSpan(ofs + (2 * 5)));
int u6 = ReadUInt16LittleEndian(SAV.Data.AsSpan(ofs + (2 * 6)));
int u7 = ReadUInt16LittleEndian(SAV.Data.AsSpan(ofs + (2 * 7)));
// Display Data
TB_Berry.Text = berry.ToString();
TB_u1.Text = u1.ToString();
TB_u2.Text = u2.ToString();
TB_u3.Text = u3.ToString();
TB_u4.Text = u4.ToString();
TB_u5.Text = u5.ToString();
TB_u6.Text = u6.ToString();
TB_u7.Text = u7.ToString();
}
private void B_Cancel_Click(object sender, EventArgs e)
{
Close();
}
}
}