2022-06-25 11:14:00 -07:00
|
|
|
using System;
|
2021-01-06 15:46:43 -08:00
|
|
|
|
2022-06-18 11:04:24 -07:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logic for recognizing .dsv save files from DeSmuME.
|
|
|
|
/// </summary>
|
|
|
|
public sealed class SaveHandlerDeSmuME : ISaveHandler
|
2021-01-06 15:46:43 -08:00
|
|
|
{
|
2022-06-18 11:04:24 -07:00
|
|
|
private const int sizeFooter = 0x7A;
|
2022-06-25 11:14:00 -07:00
|
|
|
private const int RealSize = SaveUtil.SIZE_G4RAW;
|
|
|
|
private const int ExpectedSize = RealSize + sizeFooter;
|
2021-01-06 15:46:43 -08:00
|
|
|
|
2022-06-25 11:14:00 -07:00
|
|
|
private const string SignatureDSV = "|-DESMUME SAVE-|";
|
2021-01-06 15:46:43 -08:00
|
|
|
|
2022-06-25 11:14:00 -07:00
|
|
|
private static bool GetHasFooter(ReadOnlySpan<byte> input)
|
2022-06-18 11:04:24 -07:00
|
|
|
{
|
2022-06-25 11:14:00 -07:00
|
|
|
var start = input.Length - SignatureDSV.Length;
|
|
|
|
var footer = input[start..];
|
|
|
|
for (int i = SignatureDSV.Length - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
byte c = (byte)SignatureDSV[i];
|
|
|
|
if (footer[i] != c)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2022-06-18 11:04:24 -07:00
|
|
|
}
|
2021-01-06 15:46:43 -08:00
|
|
|
|
2023-05-01 16:51:17 -07:00
|
|
|
public bool IsRecognized(long size) => size is ExpectedSize;
|
2021-01-06 15:46:43 -08:00
|
|
|
|
2022-06-25 11:14:00 -07:00
|
|
|
public SaveHandlerSplitResult? TrySplit(ReadOnlySpan<byte> input)
|
2022-06-18 11:04:24 -07:00
|
|
|
{
|
2022-06-25 11:14:00 -07:00
|
|
|
if (!GetHasFooter(input))
|
2022-06-18 11:04:24 -07:00
|
|
|
return null;
|
2021-01-06 15:46:43 -08:00
|
|
|
|
2022-06-28 16:54:18 -07:00
|
|
|
var footer = input[RealSize..].ToArray();
|
2022-06-25 11:14:00 -07:00
|
|
|
var data = input[..RealSize].ToArray();
|
2021-01-06 15:46:43 -08:00
|
|
|
|
2022-06-25 11:14:00 -07:00
|
|
|
return new SaveHandlerSplitResult(data, Array.Empty<byte>(), footer);
|
2021-01-06 15:46:43 -08:00
|
|
|
}
|
|
|
|
}
|