Minor tweaks

File.WriteAllBytes now accepts span on .NET 9 yay
This commit is contained in:
Kurt
2025-01-16 22:16:40 -06:00
parent b9df2d65c6
commit 2976480792
14 changed files with 25 additions and 30 deletions

View File

@@ -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
{

View File

@@ -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<ushort> DiffCountryRegion =>
[

View File

@@ -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<byte> GetJPEGData() => [];
protected internal const int LongStringLength = 0x22; // bytes, not characters
protected internal const int ShortStringLength = 0x1A; // bytes, not characters

View File

@@ -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<byte> 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; }

View File

@@ -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<byte> GetJPEGData() => !HasJPPEGData ? [] : Data.AsSpan(JPEG + 0x54, 0xE004);
private bool HasJPPEGData => Data[JPEG + 0x54] == 0xFF;
public void UnlockAllFriendSafariSlots()

View File

@@ -13,7 +13,7 @@ public class SecretBase6(Memory<byte> raw)
public const int MinLocationID = -1;
public const int MaxLocationID = 85;
protected Span<byte> Data => raw.Span;
public Span<byte> 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();
}

View File

@@ -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);

View File

@@ -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<byte> data, string newfile)
{
File.WriteAllBytes(newfile, data);
if (pb.Image is not Bitmap img)

View File

@@ -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<byte> 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)
{

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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

View File

@@ -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)

View File

@@ -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)