mirror of
https://github.com/kwsch/PKHeX
synced 2025-02-16 21:38:40 +00:00
Add counter overflow check
Never will happen, but not worth arguing over because this is essentially what the ROM does. Entry to this method requires both OK.
91c040b081/src/save.c (L587-L605)
Closes #3805
This commit is contained in:
parent
77cac48d34
commit
552676ed3a
2 changed files with 44 additions and 4 deletions
|
@ -123,10 +123,7 @@ public abstract class SAV3 : SaveFile, ILangDeviantSave, IEventFlag37
|
|||
if (!v1)
|
||||
return 0;
|
||||
|
||||
var count0 = ReadUInt32LittleEndian(data[(sectorZero0 + 0x0FFC)..]);
|
||||
var count1 = ReadUInt32LittleEndian(data[(sectorZero1 + 0x0FFC)..]);
|
||||
// don't care about 32bit overflow. a 10 second save would take 1,000 years to overflow!
|
||||
return count1 > count0 ? 1 : 0;
|
||||
return SAV3BlockDetection.CompareFooters(data, sectorZero0, sectorZero1);
|
||||
}
|
||||
|
||||
protected sealed override byte[] GetFinalData()
|
||||
|
|
43
PKHeX.Core/Saves/Substructures/Gen3/SAV3BlockDetection.cs
Normal file
43
PKHeX.Core/Saves/Substructures/Gen3/SAV3BlockDetection.cs
Normal file
|
@ -0,0 +1,43 @@
|
|||
using System;
|
||||
using System.Buffers.Binary;
|
||||
|
||||
namespace PKHeX.Core;
|
||||
|
||||
/// <summary>
|
||||
/// Finds the index of the most recent save block for <see cref="SAV3"/> blocks.
|
||||
/// </summary>
|
||||
public static class SAV3BlockDetection
|
||||
{
|
||||
private const int First = 0;
|
||||
private const int Second = 1;
|
||||
private const int Same = 2;
|
||||
|
||||
/// <summary>
|
||||
/// Compares the footers of the two blocks to determine which is newest.
|
||||
/// </summary>
|
||||
/// <returns>0=Primary, 1=Secondary.</returns>
|
||||
public static int CompareFooters(ReadOnlySpan<byte> data, int offset1, int offset2)
|
||||
{
|
||||
var counter1 = BinaryPrimitives.ReadUInt32LittleEndian(data[(offset1 + 0x0FFC)..]);
|
||||
var counter2 = BinaryPrimitives.ReadUInt32LittleEndian(data[(offset2 + 0x0FFC)..]);
|
||||
var result = CompareCounters(counter1, counter2);
|
||||
return result == Second ? Second : First; // Same -> First, shouldn't happen for valid saves.
|
||||
}
|
||||
|
||||
private static int CompareCounters(uint counter1, uint counter2)
|
||||
{
|
||||
// Uninitialized -- only continue if a rollover case (humanly impossible)
|
||||
if (counter1 == uint.MaxValue && counter2 != uint.MaxValue - 1)
|
||||
return Second;
|
||||
if (counter2 == uint.MaxValue && counter1 != uint.MaxValue - 1)
|
||||
return First;
|
||||
|
||||
// Different
|
||||
if (counter1 > counter2)
|
||||
return First;
|
||||
if (counter1 < counter2)
|
||||
return Second;
|
||||
|
||||
return Same;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue