2021-03-28 01:58:51 +00:00
|
|
|
|
using System;
|
2022-01-03 05:35:59 +00:00
|
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
2021-03-28 01:58:51 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
|
|
public abstract class Gen3MysteryData
|
2021-03-28 01:58:51 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
public readonly byte[] Data;
|
2021-03-28 01:58:51 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
protected Gen3MysteryData(byte[] data) => Data = data;
|
2021-03-28 01:58:51 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
public uint Checksum
|
|
|
|
|
{
|
|
|
|
|
get => ReadUInt32LittleEndian(Data.AsSpan(0));
|
|
|
|
|
set => WriteUInt32LittleEndian(Data.AsSpan(0), value);
|
|
|
|
|
}
|
2021-03-28 01:58:51 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
public bool IsChecksumValid() => Checksum == GetChecksum(Data);
|
|
|
|
|
public void FixChecksum() => Checksum = GetChecksum(Data);
|
2021-03-28 01:58:51 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
private static uint GetChecksum(ReadOnlySpan<byte> data)
|
|
|
|
|
{
|
|
|
|
|
uint sum = 0;
|
|
|
|
|
for (var i = 4; i < data.Length; i++)
|
2021-03-28 01:58:51 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
var b = data[i];
|
|
|
|
|
sum += b;
|
2021-03-28 01:58:51 +00:00
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
|
|
return sum;
|
2021-03-28 01:58:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|