2020-05-24 05:17:03 +00:00
|
|
|
|
using System;
|
2022-01-03 05:35:59 +00:00
|
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
2020-05-24 05:17:03 +00:00
|
|
|
|
|
|
|
|
|
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>
|
2022-01-03 05:35:59 +00:00
|
|
|
|
public static int CompareFooters(ReadOnlySpan<byte> data, int offset1, int offset2)
|
2020-05-24 05:17:03 +00:00
|
|
|
|
{
|
|
|
|
|
// Major Counters
|
2022-01-03 05:35:59 +00:00
|
|
|
|
var major1 = ReadUInt32LittleEndian(data[offset1..]);
|
|
|
|
|
var major2 = ReadUInt32LittleEndian(data[offset2..]);
|
2020-05-24 05:17:03 +00:00
|
|
|
|
var result1 = CompareCounters(major1, major2);
|
2021-03-14 23:16:55 +00:00
|
|
|
|
if (result1 != Same)
|
|
|
|
|
return result1;
|
2020-05-24 05:17:03 +00:00
|
|
|
|
|
|
|
|
|
// Minor Counters
|
2022-01-03 05:35:59 +00:00
|
|
|
|
var minor1 = ReadUInt32LittleEndian(data[(offset1 + 4)..]);
|
|
|
|
|
var minor2 = ReadUInt32LittleEndian(data[(offset2 + 4)..]);
|
2020-05-24 05:17:03 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2020-07-02 22:44:03 +00:00
|
|
|
|
// Uninitialized -- only continue if a rollover case (humanly impossible)
|
|
|
|
|
if (counter1 == uint.MaxValue && counter2 != uint.MaxValue - 1)
|
2020-05-24 05:17:03 +00:00
|
|
|
|
return Second;
|
2020-07-02 22:44:03 +00:00
|
|
|
|
if (counter2 == uint.MaxValue && counter1 != uint.MaxValue - 1)
|
2020-05-24 05:17:03 +00:00
|
|
|
|
return First;
|
|
|
|
|
|
|
|
|
|
// Different
|
|
|
|
|
if (counter1 > counter2)
|
|
|
|
|
return First;
|
|
|
|
|
if (counter1 < counter2)
|
|
|
|
|
return Second;
|
|
|
|
|
|
|
|
|
|
return Same;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|