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