diff --git a/Ryujinx.HLE/HOS/Services/Mii/Helper.cs b/Ryujinx.HLE/HOS/Services/Mii/Helper.cs index ea469ac9..d5fa98ea 100644 --- a/Ryujinx.HLE/HOS/Services/Mii/Helper.cs +++ b/Ryujinx.HLE/HOS/Services/Mii/Helper.cs @@ -6,7 +6,7 @@ namespace Ryujinx.HLE.HOS.Services.Mii { static class Helper { - public static ushort CalculateCrc16BE(ReadOnlySpan data, int crc = 0) + public static ushort CalculateCrc16(ReadOnlySpan data, int crc, bool reverseEndianess) { 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() diff --git a/Ryujinx.HLE/HOS/Services/Mii/MiiDatabaseManager.cs b/Ryujinx.HLE/HOS/Services/Mii/MiiDatabaseManager.cs index 0fe6220c..4491f2c8 100644 --- a/Ryujinx.HLE/HOS/Services/Mii/MiiDatabaseManager.cs +++ b/Ryujinx.HLE/HOS/Services/Mii/MiiDatabaseManager.cs @@ -416,35 +416,35 @@ namespace Ryujinx.HLE.HOS.Services.Mii return ResultCode.InvalidStoreData; } - if (!metadata.MiiKeyCode.IsEnabledSpecialMii() && !storeData.IsSpecial()) + if (!metadata.MiiKeyCode.IsEnabledSpecialMii() && storeData.IsSpecial()) { - 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; + return ResultCode.InvalidOperationOnSpecialMii; } - 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) diff --git a/Ryujinx.HLE/HOS/Services/Mii/Types/CoreData.cs b/Ryujinx.HLE/HOS/Services/Mii/Types/CoreData.cs index 39a3945b..f3a101d8 100644 --- a/Ryujinx.HLE/HOS/Services/Mii/Types/CoreData.cs +++ b/Ryujinx.HLE/HOS/Services/Mii/Types/CoreData.cs @@ -588,7 +588,7 @@ namespace Ryujinx.HLE.HOS.Services.Mii.Types Nickname = charInfo.Nickname; FontRegion = charInfo.FontRegion; FavoriteColor = charInfo.FavoriteColor; - Gender = (Gender)charInfo.Gender; + Gender = charInfo.Gender; Height = charInfo.Height; Build = charInfo.Build; Type = charInfo.Type; diff --git a/Ryujinx.HLE/HOS/Services/Mii/Types/NintendoFigurineDatabase.cs b/Ryujinx.HLE/HOS/Services/Mii/Types/NintendoFigurineDatabase.cs index 7e6782f0..14eda2ed 100644 --- a/Ryujinx.HLE/HOS/Services/Mii/Types/NintendoFigurineDatabase.cs +++ b/Ryujinx.HLE/HOS/Services/Mii/Types/NintendoFigurineDatabase.cs @@ -233,7 +233,7 @@ namespace Ryujinx.HLE.HOS.Services.Mii.Types private ushort CalculateCrc() { - return Helper.CalculateCrc16BE(AsSpanWithoutCrc()); + return Helper.CalculateCrc16(AsSpanWithoutCrc(), 0, true); } public Span AsSpan() diff --git a/Ryujinx.HLE/HOS/Services/Mii/Types/StoreData.cs b/Ryujinx.HLE/HOS/Services/Mii/Types/StoreData.cs index 7db4ab34..6b7e3e5a 100644 --- a/Ryujinx.HLE/HOS/Services/Mii/Types/StoreData.cs +++ b/Ryujinx.HLE/HOS/Services/Mii/Types/StoreData.cs @@ -49,26 +49,30 @@ namespace Ryujinx.HLE.HOS.Services.Mii.Types public bool IsValidDataCrc() { - return DataCrc == CalculateDataCrc(); + return Helper.CalculateCrc16(AsSpanWithoutDeviceCrc(), 0, false) == 0; } 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() { - return Helper.CalculateCrc16BE(AsSpanWithoutCrc()); + return Helper.CalculateCrc16(AsSpanWithoutDeviceCrc(), 0, true); } private ushort CalculateDeviceCrc() { 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 AsSpan() @@ -76,11 +80,6 @@ namespace Ryujinx.HLE.HOS.Services.Mii.Types return MemoryMarshal.AsBytes(SpanHelpers.CreateReadOnlySpan(in this, 1)); } - private ReadOnlySpan AsSpanWithoutCrc() - { - return AsSpan().Slice(0, Size - 4); - } - private ReadOnlySpan AsSpanWithoutDeviceCrc() { return AsSpan().Slice(0, Size - 2);