From 2976480792eef4bac400ebb4ab664b6efba3cbb2 Mon Sep 17 00:00:00 2001 From: Kurt Date: Thu, 16 Jan 2025 22:16:40 -0600 Subject: [PATCH] Minor tweaks File.WriteAllBytes now accepts span on .NET 9 yay --- .../Gen4/EncounterStatic4Pokewalker.cs | 2 +- .../Legality/Restrictions/Vivillon3DS.cs | 2 +- PKHeX.Core/Saves/SAV6.cs | 2 +- PKHeX.Core/Saves/SAV6AO.cs | 2 +- PKHeX.Core/Saves/SAV6XY.cs | 2 +- .../Gen6/SecretBase/SecretBase6.cs | 6 +---- .../Controls/SAV Editor/SAVEditor.cs | 2 +- .../Controls/SAV Editor/SlotChangeManager.cs | 2 +- .../Subforms/Save Editors/Gen5/SAV_DLC5.cs | 22 +++++++++---------- .../Subforms/Save Editors/Gen5/SAV_Misc5.cs | 2 +- .../Subforms/Save Editors/Gen6/SAV_Link6.cs | 2 +- .../Save Editors/Gen6/SAV_SecretBase.cs | 3 +-- .../Save Editors/Gen7/SAV_Trainer7GG.cs | 4 ++-- .../Subforms/Save Editors/SAV_Chatter.cs | 2 +- 14 files changed, 25 insertions(+), 30 deletions(-) diff --git a/PKHeX.Core/Legality/Encounters/Templates/Gen4/EncounterStatic4Pokewalker.cs b/PKHeX.Core/Legality/Encounters/Templates/Gen4/EncounterStatic4Pokewalker.cs index 712a89d15..cebce5670 100644 --- a/PKHeX.Core/Legality/Encounters/Templates/Gen4/EncounterStatic4Pokewalker.cs +++ b/PKHeX.Core/Legality/Encounters/Templates/Gen4/EncounterStatic4Pokewalker.cs @@ -111,7 +111,7 @@ private void SetPINGA(PK4 pk, EncounterCriteria criteria, PersonalInfo4 pi) pk.IV32 = GetIV32(criteria); } - private uint GetIV32(EncounterCriteria criteria) + private static uint GetIV32(EncounterCriteria criteria) { if (criteria.IsSpecifiedIVsAll()) // Don't trust that the requirements are valid { diff --git a/PKHeX.Core/Legality/Restrictions/Vivillon3DS.cs b/PKHeX.Core/Legality/Restrictions/Vivillon3DS.cs index 07e9b90e4..4b0d468ef 100644 --- a/PKHeX.Core/Legality/Restrictions/Vivillon3DS.cs +++ b/PKHeX.Core/Legality/Restrictions/Vivillon3DS.cs @@ -126,7 +126,7 @@ public static byte GetPattern(in byte country, in byte region) ]; // (country:X2_region:X2) - // do it this way instead of region_country so that the byte[] form is better ordered for better file compression :) + // do it this way instead of region_country so that the buffer is better ordered for better file compression :) // 777 entries, so we can use a binary search to find the form. Worst case is log2(777) = 10 comparisons. private static ReadOnlySpan DiffCountryRegion => [ diff --git a/PKHeX.Core/Saves/SAV6.cs b/PKHeX.Core/Saves/SAV6.cs index 8f673faee..8a0ed80b4 100644 --- a/PKHeX.Core/Saves/SAV6.cs +++ b/PKHeX.Core/Saves/SAV6.cs @@ -41,7 +41,7 @@ public abstract class SAV6 : SAV_BEEF, ITrainerStatRecord, ISaveBlock6Core, IReg public int HoF { get; protected set; } = int.MinValue; public virtual string JPEGTitle => string.Empty; - public virtual byte[] GetJPEGData() => []; + public virtual Span GetJPEGData() => []; protected internal const int LongStringLength = 0x22; // bytes, not characters protected internal const int ShortStringLength = 0x1A; // bytes, not characters diff --git a/PKHeX.Core/Saves/SAV6AO.cs b/PKHeX.Core/Saves/SAV6AO.cs index 047e9c598..49cec12be 100644 --- a/PKHeX.Core/Saves/SAV6AO.cs +++ b/PKHeX.Core/Saves/SAV6AO.cs @@ -100,7 +100,7 @@ public int MultiplayerSpriteID // Daycare public override string JPEGTitle => !HasJPEGData ? string.Empty : StringConverter6.GetString(Data.AsSpan(JPEG, 0x1A)); - public override byte[] GetJPEGData() => !HasJPEGData ? [] : Data.AsSpan(JPEG + 0x54, 0xE004).ToArray(); + public override Span GetJPEGData() => !HasJPEGData ? [] : Data.AsSpan(JPEG + 0x54, 0xE004); private bool HasJPEGData => Data[JPEG + 0x54] == 0xFF; public override int CurrentBox { get => Blocks.BoxLayout.CurrentBox; set => Blocks.BoxLayout.CurrentBox = value; } diff --git a/PKHeX.Core/Saves/SAV6XY.cs b/PKHeX.Core/Saves/SAV6XY.cs index 8ff4973a1..9673a31ea 100644 --- a/PKHeX.Core/Saves/SAV6XY.cs +++ b/PKHeX.Core/Saves/SAV6XY.cs @@ -99,7 +99,7 @@ public bool IsEggAvailable } public override string JPEGTitle => !HasJPPEGData ? string.Empty : StringConverter6.GetString(Data.AsSpan(JPEG, 0x1A)); - public override byte[] GetJPEGData() => !HasJPPEGData ? [] : Data.AsSpan(JPEG + 0x54, 0xE004).ToArray(); + public override Span GetJPEGData() => !HasJPPEGData ? [] : Data.AsSpan(JPEG + 0x54, 0xE004); private bool HasJPPEGData => Data[JPEG + 0x54] == 0xFF; public void UnlockAllFriendSafariSlots() diff --git a/PKHeX.Core/Saves/Substructures/Gen6/SecretBase/SecretBase6.cs b/PKHeX.Core/Saves/Substructures/Gen6/SecretBase/SecretBase6.cs index 95604e4da..d2e2fa16c 100644 --- a/PKHeX.Core/Saves/Substructures/Gen6/SecretBase/SecretBase6.cs +++ b/PKHeX.Core/Saves/Substructures/Gen6/SecretBase/SecretBase6.cs @@ -13,7 +13,7 @@ public class SecretBase6(Memory raw) public const int MinLocationID = -1; public const int MaxLocationID = 85; - protected Span Data => raw.Span; + public Span Data => raw.Span; // structure: (first at 23D24 in sav) // [000-001] u8 IsNew @@ -136,8 +136,6 @@ public void Load(SecretBase6 other) LoadSelf(other); } - public virtual byte[] Write() => Data.ToArray(); - public static SecretBase6? Read(byte[] data) { return data.Length switch @@ -224,6 +222,4 @@ public void SetTeam(SecretBase6PKM[] arr) } protected override void LoadOther(SecretBase6Other other) => other.Data.CopyTo(Data); - - public override byte[] Write() => Data.ToArray(); } diff --git a/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs b/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs index cd7525e1d..7f302a2c8 100644 --- a/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs +++ b/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs @@ -843,7 +843,7 @@ private void B_HallofFame_Click(object sender, EventArgs e) private void B_JPEG_Click(object sender, EventArgs e) { var s6 = (SAV6)SAV; - byte[] jpeg = s6.GetJPEGData(); + var jpeg = s6.GetJPEGData(); if (jpeg.Length == 0) { WinFormsUtil.Alert(MsgSaveJPEGExportFail); diff --git a/PKHeX.WinForms/Controls/SAV Editor/SlotChangeManager.cs b/PKHeX.WinForms/Controls/SAV Editor/SlotChangeManager.cs index 34b7bc954..b47391d90 100644 --- a/PKHeX.WinForms/Controls/SAV Editor/SlotChangeManager.cs +++ b/PKHeX.WinForms/Controls/SAV Editor/SlotChangeManager.cs @@ -204,7 +204,7 @@ private string CreateDragDropPKM(PictureBox pb, bool encrypt, out bool external) return newfile; } - private bool TryMakeDragDropPKM(PictureBox pb, byte[] data, string newfile) + private bool TryMakeDragDropPKM(PictureBox pb, ReadOnlySpan data, string newfile) { File.WriteAllBytes(newfile, data); if (pb.Image is not Bitmap img) diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen5/SAV_DLC5.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen5/SAV_DLC5.cs index 87a206fe6..000eb3df7 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen5/SAV_DLC5.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen5/SAV_DLC5.cs @@ -168,7 +168,7 @@ private bool ImportFile(string extension, string name, int expectSize, out byte[ return true; } - private static void ExportFile(string extension, string name, byte[] data, string? initialName = null) + private static void ExportFile(string extension, string name, ReadOnlySpan data, string? initialName = null) { using var sfd = new SaveFileDialog(); sfd.Filter = $"{name}|*.{extension}"; @@ -307,7 +307,7 @@ private void B_ExportCGB_Click(object sender, EventArgs e) if (sfd.ShowDialog() != DialogResult.OK) return; - File.WriteAllBytes(sfd.FileName, bg.Data.ToArray()); + File.WriteAllBytes(sfd.FileName, bg.Data); } private void B_Save_Click(object sender, EventArgs e) @@ -342,7 +342,7 @@ private void B_PWTExport_Click(object sender, EventArgs e) var name = pwt.Name; if (string.IsNullOrWhiteSpace(name)) name = "Empty"; - ExportFile(WorldTournament5.Extension, PWTFileName, data.ToArray(), name); + ExportFile(WorldTournament5.Extension, PWTFileName, data.Span, name); } private void B_MusicalImport_Click(object sender, EventArgs e) @@ -361,7 +361,7 @@ private void B_MusicalImport_Click(object sender, EventArgs e) private void B_MusicalExport_Click(object sender, EventArgs e) { var data = SAV.MusicalDownloadData; - ExportFile(MusicalShow5.Extension, SAV.Musical.MusicalName, data.ToArray()); + ExportFile(MusicalShow5.Extension, SAV.Musical.MusicalName, data.Span); } private void B_BattleVideoImport_Click(object sender, EventArgs e) @@ -385,7 +385,7 @@ private void B_BattleVideoExport_Click(object sender, EventArgs e) { var index = LB_BattleVideo.SelectedIndex; var data = SAV.GetBattleVideo(index); - ExportFile(BattleVideo5.Extension, BattleVideoFileName, data.ToArray()); + ExportFile(BattleVideo5.Extension, BattleVideoFileName, data.Span); } private void B_BattleVideoExportDecrypted_Click(object sender, EventArgs e) @@ -396,7 +396,7 @@ private void B_BattleVideoExportDecrypted_Click(object sender, EventArgs e) bool actual = !bvid.IsUninitialized; if (actual) bvid.Decrypt(); - ExportFile(BattleVideo5.Extension, BattleVideoFileName, data.ToArray()); + ExportFile(BattleVideo5.Extension, BattleVideoFileName, data.Span); if (actual) bvid.Encrypt(); } @@ -417,7 +417,7 @@ private void B_PokestarExport_Click(object sender, EventArgs e) { var b2w2 = (SAV5B2W2)SAV; var data = b2w2.GetPokestarMovie(LB_Pokestar.SelectedIndex); - ExportFile(PokestarMovie5.Extension, PokeStarMovieFileName, data.ToArray()); + ExportFile(PokestarMovie5.Extension, PokeStarMovieFileName, data.Span); } private void LB_Pokestar_SelectedIndexChanged(object sender, EventArgs e) { } @@ -439,13 +439,13 @@ private void B_Memory2Import_Click(object sender, EventArgs e) } private void B_Memory1Export_Click(object sender, EventArgs e) - => ExportFile(MemoryLinkExtension, MemoryLinkFileName, SAV.Link1Data.ToArray()); + => ExportFile(MemoryLinkExtension, MemoryLinkFileName, SAV.Link1Data.Span); private void B_Memory2Export_Click(object sender, EventArgs e) - => ExportFile(MemoryLinkExtension, MemoryLinkFileName, SAV.Link2Data.ToArray()); + => ExportFile(MemoryLinkExtension, MemoryLinkFileName, SAV.Link2Data.Span); private void B_PokeDexSkinSave_Click(object sender, EventArgs e) - => ExportFile(PokeDexSkin5.Extension, PokeDexFileName, SAV.PokedexSkinData.ToArray()); + => ExportFile(PokeDexSkin5.Extension, PokeDexFileName, SAV.PokedexSkinData.Span); private void B_BattleTestExport_Click(object sender, EventArgs e) - => ExportFile(BattleTest5.Extension, BattleTestFileName, SAV.BattleTest.ToArray()); + => ExportFile(BattleTest5.Extension, BattleTestFileName, SAV.BattleTest.Span); private void B_PokeDexSkinLoad_Click(object sender, EventArgs e) { diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen5/SAV_Misc5.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen5/SAV_Misc5.cs index 0a6b84bae..f2762e48e 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen5/SAV_Misc5.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen5/SAV_Misc5.cs @@ -816,7 +816,7 @@ private void B_DumpFC_Click(object sender, EventArgs e) if (sfd.ShowDialog() != DialogResult.OK) return; - var data = bw.Forest.ForestCity.ToArray(); + var data = bw.Forest.ForestCity.Span; File.WriteAllBytes(sfd.FileName, data); } diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_Link6.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_Link6.cs index c138eed6d..557e1632b 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_Link6.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_Link6.cs @@ -64,7 +64,7 @@ private void B_Export_Click(object sender, EventArgs e) if (sfd.ShowDialog() != DialogResult.OK) return; - File.WriteAllBytes(sfd.FileName, Gifts.Data.ToArray()); + File.WriteAllBytes(sfd.FileName, Gifts.Data); WinFormsUtil.Alert("Pokémon Link data saved to:" + Environment.NewLine + sfd.FileName); } diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_SecretBase.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_SecretBase.cs index 5e6350d41..daffa6c28 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_SecretBase.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_SecretBase.cs @@ -424,8 +424,7 @@ private void B_Export_Click(object sender, EventArgs e) return; var path = sfd.FileName; - var data = sb.Write(); - File.WriteAllBytes(path, data); + File.WriteAllBytes(path, sb.Data); } #endregion diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7GG.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7GG.cs index 29f1edf1b..69fdcbb06 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7GG.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7GG.cs @@ -207,7 +207,7 @@ private void B_ExportGoFiles_Click(object sender, EventArgs e) var folder = fbd.SelectedPath; foreach (var gpk in gofiles) - File.WriteAllBytes(Path.Combine(folder, Util.CleanFileName(gpk.FileName)), gpk.Data.ToArray()); + File.WriteAllBytes(Path.Combine(folder, Util.CleanFileName(gpk.FileName)), gpk.Data); WinFormsUtil.Alert($"Dumped {gofiles.Length} files to {folder}"); } @@ -266,7 +266,7 @@ private void B_Export_Click(object sender, EventArgs e) if (sfd.ShowDialog() != DialogResult.OK) return; - File.WriteAllBytes(sfd.FileName, data.Data.ToArray()); + File.WriteAllBytes(sfd.FileName, data.Data); } private void B_ImportGoFiles_Click(object sender, EventArgs e) diff --git a/PKHeX.WinForms/Subforms/Save Editors/SAV_Chatter.cs b/PKHeX.WinForms/Subforms/Save Editors/SAV_Chatter.cs index 149dbaa94..129b859e0 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/SAV_Chatter.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/SAV_Chatter.cs @@ -61,7 +61,7 @@ private void B_ExportPCM_Click(object sender, EventArgs e) if (sfd.ShowDialog() != DialogResult.OK) return; - File.WriteAllBytes(sfd.FileName, Chatter.Recording.ToArray()); + File.WriteAllBytes(sfd.FileName, Chatter.Recording); } private void B_ExportWAV_Click(object sender, EventArgs e)