2
0
Fork 0
mirror of https://github.com/kwsch/PKHeX synced 2025-03-06 16:27:24 +00:00
PKHeX/PKHeX.Core/Saves/SAV4BlockDetection.cs
Kurt 64428539bd Fix sav4 active block detect
Closes  ty @Ammako !

lazy slice check has flaws; just compare the major/minor counters in the save footer and check the uninitialized cases

nobody is gonna save the game 2^32 times, but check for the overflow case too!
first ff'd is preferred when the second is ff'd-1!
2020-05-23 22:17:03 -07:00

53 lines
1.7 KiB
C#

using System;
namespace PKHeX.Core
{
/// <summary>
/// Finds the index of the most recent save block for <see cref="SAV4"/> blocks.
/// </summary>
public static class SAV4BlockDetection
{
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(byte[] data, int offset1, int offset2)
{
// Major Counters
var major1 = BitConverter.ToUInt32(data, offset1);
var major2 = BitConverter.ToUInt32(data, offset2);
var result1 = CompareCounters(major1, major2);
if (result1 == First)
return First;
if (result1 == Second)
return Second;
// Minor Counters
var minor1 = BitConverter.ToUInt32(data, offset1 + 4);
var minor2 = BitConverter.ToUInt32(data, offset2 + 4);
var result2 = CompareCounters(minor1, minor2);
return result2 == Second ? Second : First; // Same -> First, shouldn't happen for valid saves.
}
private static int CompareCounters(uint counter1, uint counter2)
{
// Uninitialized
if (counter1 == uint.MaxValue && counter2 == 0)
return Second;
if (counter1 == 0 && counter2 == uint.MaxValue)
return First;
// Different
if (counter1 > counter2)
return First;
if (counter1 < counter2)
return Second;
return Same;
}
}
}