Misc tweaks

SAV3: extract struct slice fetch
Metadata: allow setting `Memory<byte>` instead of `byte[]`
Stats: deduplicate gen1/2 mask set
remove some unnecessary spaces (lol)
This commit is contained in:
Kurt
2024-12-09 01:02:14 -06:00
parent 6397cf7723
commit c6307b40ef
6 changed files with 29 additions and 28 deletions

View File

@@ -192,7 +192,7 @@ public void SetAllLearnMoveShop(Span<bool> result)
{
var moves = MoveShopMoves;
var bits = MoveShopBits;
for (int index = 0 ; index < MoveShopCount; index++)
for (int index = 0; index < MoveShopCount; index++)
{
if ((bits & 1) == 1)
result[moves[index]] = true;

View File

@@ -65,12 +65,9 @@ private void ReadSectors(ReadOnlySpan<byte> data, int group)
{
// Get the sector ID for the serialized savedata block, and copy the chunk into the corresponding object.
var id = ReadInt16LittleEndian(data[(ofs + 0xFF4)..]);
switch (id)
{
case >= 5: data.Slice(ofs, SIZE_SECTOR_USED).CopyTo(Storage.AsSpan((id - 5) * SIZE_SECTOR_USED)); break;
case >= 1: data.Slice(ofs, SIZE_SECTOR_USED).CopyTo(Large.AsSpan((id - 1) * SIZE_SECTOR_USED)); break;
default: data.Slice(ofs, SIZE_SECTOR_USED).CopyTo(Small.AsSpan(0)); break;
}
var src = data.Slice(ofs, SIZE_SECTOR_USED);
var dest = GetStructureChunk(id);
src.CopyTo(dest);
}
}
@@ -82,15 +79,19 @@ private void WriteSectors(Span<byte> data, int group)
{
// Get the sector ID for the serialized savedata block, and copy the corresponding chunk of object data into it.
var id = ReadInt16LittleEndian(data[(ofs + 0xFF4)..]);
switch (id)
{
case >= 5: Storage.AsSpan((id - 5) * SIZE_SECTOR_USED, SIZE_SECTOR_USED).CopyTo(data[ofs..]); break;
case >= 1: Large.AsSpan((id - 1) * SIZE_SECTOR_USED, SIZE_SECTOR_USED).CopyTo(data[ofs..]); break;
default: Small.AsSpan(0, SIZE_SECTOR_USED).CopyTo(data[ofs..]); break;
}
var src = data.Slice(ofs, SIZE_SECTOR_USED);
var dest = GetStructureChunk(id);
dest.CopyTo(src);
}
}
private Span<byte> GetStructureChunk(short id) => id switch
{
>= 5 => Storage.AsSpan((id - 5) * SIZE_SECTOR_USED, SIZE_SECTOR_USED),
>= 1 => Large .AsSpan((id - 1) * SIZE_SECTOR_USED, SIZE_SECTOR_USED),
_ => Small.AsSpan(0, SIZE_SECTOR_USED),
};
/// <summary>
/// Checks the input data to see if all required sectors for the main save data are present for the <see cref="slot"/>.
/// </summary>
@@ -335,7 +336,11 @@ protected override void SetPartyValues(PKM pk, bool isParty)
public sealed override string OT
{
get => GetString(OriginalTrainerTrash);
get
{
int len = Japanese ? 5 : MaxStringLengthTrainer;
return GetString(OriginalTrainerTrash[..len]);
}
set
{
int len = Japanese ? 5 : MaxStringLengthTrainer;

View File

@@ -109,7 +109,7 @@ protected override void SetPKM(PKM pk, bool isParty = false)
public override int PlayedSeconds { get => PlayerData.PlayedSeconds; set => PlayerData.PlayedSeconds = value; }
public override uint Money { get => Misc.Money; set => Misc.Money = value; }
public override uint SecondsToStart { get => AdventureInfo.SecondsToStart; set => AdventureInfo.SecondsToStart = value; }
public override uint SecondsToFame { get => AdventureInfo.SecondsToFame ; set => AdventureInfo.SecondsToFame = value; }
public override uint SecondsToFame { get => AdventureInfo.SecondsToFame; set => AdventureInfo.SecondsToFame = value; }
public override IReadOnlyList<InventoryPouch> Inventory { get => Items.Inventory; set => Items.Inventory = value; }
protected override void SetDex(PKM pk) => Zukan.SetDex(pk);

View File

@@ -28,8 +28,8 @@ public sealed record SaveFileMetadata(SaveFile SAV)
/// </summary>
public string? FileFolder { get; private set; }
private byte[] Footer = []; // .dsv
private byte[] Header = []; // .gci
private Memory<byte> Footer = Memory<byte>.Empty; // .dsv
private Memory<byte> Header = Memory<byte>.Empty; // .gci
private ISaveHandler? Handler;
private string BAKSuffix => $" [{SAV.ShortSummary}].bak";
@@ -56,9 +56,9 @@ public sealed record SaveFileMetadata(SaveFile SAV)
public byte[] Finalize(byte[] data, BinaryExportSetting setting)
{
if (HasFooter && !setting.HasFlag(BinaryExportSetting.ExcludeFooter))
data = [..data, ..Footer];
data = [..data, ..Footer.Span];
if (HasHeader && !setting.HasFlag(BinaryExportSetting.ExcludeHeader))
data = [..Header, ..data];
data = [..Header.Span, ..data];
if (!setting.HasFlag(BinaryExportSetting.ExcludeFinalize))
Handler?.Finalize(data);
return data;
@@ -67,7 +67,7 @@ public byte[] Finalize(byte[] data, BinaryExportSetting setting)
/// <summary>
/// Sets the details of any trimmed header and footer arrays to a <see cref="SaveFile"/> object.
/// </summary>
public void SetExtraInfo(byte[] header, byte[] footer, ISaveHandler handler)
public void SetExtraInfo(Memory<byte> header, Memory<byte> footer, ISaveHandler handler)
{
Header = header;
Footer = footer;
@@ -208,9 +208,9 @@ internal SaveFileMetadata CloneInternal(SaveFile sav)
var clone = this with { SAV = sav };
// Disassociate any mutable references from this object
if (HasFooter)
clone.Footer = [..Footer];
clone.Footer = Footer.ToArray();
if (HasHeader)
clone.Header = [..Header];
clone.Header = Header.ToArray();
return clone;
}
}

View File

@@ -48,7 +48,7 @@ public sealed class FieldObject8b
public byte Count // cnt
{
get => Data[0] ;
get => Data[0];
set => Data[0] = value;
}

View File

@@ -658,11 +658,7 @@ public void ToggleInterface(PKM pk, byte format)
switch (format)
{
case 1:
TB_IVHP.Enabled = false;
SetEVMaskSize(Stat_HP.Size, "00000", MT_EVs);
break;
case 2:
case 1 or 2:
TB_IVHP.Enabled = false;
SetEVMaskSize(Stat_HP.Size, "00000", MT_EVs);
break;