mii: Fix multiple inconsistencies (#2392)

I found multiple inconsistencies while diffing with latest sdb, this PR fixes those findings.
This commit is contained in:
Mary 2021-06-23 22:24:16 +02:00 committed by GitHub
parent 0644db02ad
commit e334303559
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 40 deletions

View file

@ -6,7 +6,7 @@ namespace Ryujinx.HLE.HOS.Services.Mii
{ {
static class Helper static class Helper
{ {
public static ushort CalculateCrc16BE(ReadOnlySpan<byte> data, int crc = 0) public static ushort CalculateCrc16(ReadOnlySpan<byte> data, int crc, bool reverseEndianess)
{ {
const ushort poly = 0x1021; const ushort poly = 0x1021;
@ -25,7 +25,12 @@ namespace Ryujinx.HLE.HOS.Services.Mii
} }
} }
return BinaryPrimitives.ReverseEndianness((ushort)crc); if (reverseEndianess)
{
return (ushort)(BinaryPrimitives.ReverseEndianness(crc) >> 16);
}
return (ushort)crc;
} }
public static UInt128 GetDeviceId() public static UInt128 GetDeviceId()

View file

@ -416,35 +416,35 @@ namespace Ryujinx.HLE.HOS.Services.Mii
return ResultCode.InvalidStoreData; return ResultCode.InvalidStoreData;
} }
if (!metadata.MiiKeyCode.IsEnabledSpecialMii() && !storeData.IsSpecial()) if (!metadata.MiiKeyCode.IsEnabledSpecialMii() && storeData.IsSpecial())
{ {
if (_database.GetIndexByCreatorId(out int index, storeData.CreateId)) return ResultCode.InvalidOperationOnSpecialMii;
{
StoreData oldStoreData = _database.Get(index);
if (oldStoreData.IsSpecial())
{
return ResultCode.InvalidOperationOnSpecialMii;
}
_database.Replace(index, storeData);
}
else
{
if (_database.IsFull())
{
return ResultCode.DatabaseFull;
}
_database.Add(storeData);
}
MarkDirty(metadata);
return ResultCode.Success;
} }
return ResultCode.InvalidOperationOnSpecialMii; if (_database.GetIndexByCreatorId(out int index, storeData.CreateId))
{
StoreData oldStoreData = _database.Get(index);
if (oldStoreData.IsSpecial())
{
return ResultCode.InvalidOperationOnSpecialMii;
}
_database.Replace(index, storeData);
}
else
{
if (_database.IsFull())
{
return ResultCode.DatabaseFull;
}
_database.Add(storeData);
}
MarkDirty(metadata);
return ResultCode.Success;
} }
public ResultCode Delete(DatabaseSessionMetadata metadata, CreateId createId) public ResultCode Delete(DatabaseSessionMetadata metadata, CreateId createId)

View file

@ -588,7 +588,7 @@ namespace Ryujinx.HLE.HOS.Services.Mii.Types
Nickname = charInfo.Nickname; Nickname = charInfo.Nickname;
FontRegion = charInfo.FontRegion; FontRegion = charInfo.FontRegion;
FavoriteColor = charInfo.FavoriteColor; FavoriteColor = charInfo.FavoriteColor;
Gender = (Gender)charInfo.Gender; Gender = charInfo.Gender;
Height = charInfo.Height; Height = charInfo.Height;
Build = charInfo.Build; Build = charInfo.Build;
Type = charInfo.Type; Type = charInfo.Type;

View file

@ -233,7 +233,7 @@ namespace Ryujinx.HLE.HOS.Services.Mii.Types
private ushort CalculateCrc() private ushort CalculateCrc()
{ {
return Helper.CalculateCrc16BE(AsSpanWithoutCrc()); return Helper.CalculateCrc16(AsSpanWithoutCrc(), 0, true);
} }
public Span<byte> AsSpan() public Span<byte> AsSpan()

View file

@ -49,26 +49,30 @@ namespace Ryujinx.HLE.HOS.Services.Mii.Types
public bool IsValidDataCrc() public bool IsValidDataCrc()
{ {
return DataCrc == CalculateDataCrc(); return Helper.CalculateCrc16(AsSpanWithoutDeviceCrc(), 0, false) == 0;
} }
public bool IsValidDeviceCrc() public bool IsValidDeviceCrc()
{ {
return DeviceCrc == CalculateDeviceCrc(); UInt128 deviceId = Helper.GetDeviceId();
ushort deviceIdCrc16 = Helper.CalculateCrc16(SpanHelpers.AsByteSpan(ref deviceId), 0, false);
return Helper.CalculateCrc16(AsSpan(), deviceIdCrc16, false) == 0;
} }
private ushort CalculateDataCrc() private ushort CalculateDataCrc()
{ {
return Helper.CalculateCrc16BE(AsSpanWithoutCrc()); return Helper.CalculateCrc16(AsSpanWithoutDeviceCrc(), 0, true);
} }
private ushort CalculateDeviceCrc() private ushort CalculateDeviceCrc()
{ {
UInt128 deviceId = Helper.GetDeviceId(); UInt128 deviceId = Helper.GetDeviceId();
ushort deviceIdCrc16 = Helper.CalculateCrc16BE(SpanHelpers.AsByteSpan(ref deviceId)); ushort deviceIdCrc16 = Helper.CalculateCrc16(SpanHelpers.AsByteSpan(ref deviceId), 0, false);
return Helper.CalculateCrc16BE(AsSpanWithoutDeviceCrc(), deviceIdCrc16); return Helper.CalculateCrc16(AsSpan(), deviceIdCrc16, true);
} }
private ReadOnlySpan<byte> AsSpan() private ReadOnlySpan<byte> AsSpan()
@ -76,11 +80,6 @@ namespace Ryujinx.HLE.HOS.Services.Mii.Types
return MemoryMarshal.AsBytes(SpanHelpers.CreateReadOnlySpan(in this, 1)); return MemoryMarshal.AsBytes(SpanHelpers.CreateReadOnlySpan(in this, 1));
} }
private ReadOnlySpan<byte> AsSpanWithoutCrc()
{
return AsSpan().Slice(0, Size - 4);
}
private ReadOnlySpan<byte> AsSpanWithoutDeviceCrc() private ReadOnlySpan<byte> AsSpanWithoutDeviceCrc()
{ {
return AsSpan().Slice(0, Size - 2); return AsSpan().Slice(0, Size - 2);