mirror of
https://github.com/kwsch/PKHeX
synced 2025-02-17 05:48:44 +00:00
Fix sav4 active block detect
Closes #2697 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!
This commit is contained in:
parent
8c6d674472
commit
64428539bd
2 changed files with 55 additions and 22 deletions
|
@ -149,30 +149,10 @@ namespace PKHeX.Core
|
|||
|
||||
private static int GetActiveBlock(byte[] data, int begin, int length)
|
||||
{
|
||||
// Check to see if the save is initialized completely
|
||||
// if the block is not initialized, fall back to the other save.
|
||||
var start = begin;
|
||||
if (data.IsRangeAll((byte)0, start, 10) || data.IsRangeAll((byte)0xFF, start, 10))
|
||||
return 1;
|
||||
start += PartitionSize; // check other save
|
||||
if (data.IsRangeAll((byte)0, start, 10) || data.IsRangeAll((byte)0xFF, start, 10))
|
||||
return 0;
|
||||
|
||||
// Fall back to highest value save counter
|
||||
return GetActiveBlockViaCounter(data, begin, length);
|
||||
int offset = begin + length - 0x14;
|
||||
return SAV4BlockDetection.CompareFooters(data, offset, offset + PartitionSize);
|
||||
}
|
||||
|
||||
private static int GetActiveBlockViaCounter(byte[] data, int begin, int length)
|
||||
{
|
||||
int ofs = GetBlockSaveCounterOffset(begin, length);
|
||||
var block0 = BitConverter.ToUInt16(data, ofs);
|
||||
var block1 = BitConverter.ToUInt16(data, ofs + PartitionSize);
|
||||
bool first = block0 >= block1;
|
||||
return first ? 0 : 1;
|
||||
}
|
||||
|
||||
private static int GetBlockSaveCounterOffset(int start, int length) => start + length - 0x10;
|
||||
|
||||
protected int WondercardFlags = int.MinValue;
|
||||
protected int AdventureInfo = int.MinValue;
|
||||
protected int Seal = int.MinValue;
|
||||
|
|
53
PKHeX.Core/Saves/SAV4BlockDetection.cs
Normal file
53
PKHeX.Core/Saves/SAV4BlockDetection.cs
Normal file
|
@ -0,0 +1,53 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue