misc: prevent crashes

This commit is contained in:
Evan Husted 2024-12-29 23:39:40 -06:00
parent eec92c242c
commit 8fd8a776c9
4 changed files with 28 additions and 7 deletions

View file

@ -13,13 +13,13 @@ namespace Ryujinx.Common.Configuration
public record EnabledDirtyHack(DirtyHacks Hack, int Value)
{
private static readonly byte[] _packedFormat = [8, 32];
public static readonly byte[] PackedFormat = [8, 32];
public ulong Pack() => BitTricks.PackBitFields([(uint)Hack, (uint)Value], _packedFormat);
public ulong Pack() => BitTricks.PackBitFields([(uint)Hack, (uint)Value], PackedFormat);
public static EnabledDirtyHack FromPacked(ulong packedHack)
{
var unpackedFields = BitTricks.UnpackBitFields(packedHack, _packedFormat);
var unpackedFields = BitTricks.UnpackBitFields(packedHack, PackedFormat);
if (unpackedFields is not [var hack, var value])
throw new ArgumentException(nameof(packedHack));
@ -45,6 +45,15 @@ namespace Ryujinx.Common.Configuration
}
}
public ulong[] PackEntries() =>
this
.Select(it =>
BitTricks.PackBitFields([(uint)it.Key, (uint)it.Value], EnabledDirtyHack.PackedFormat))
.ToArray();
public static implicit operator DirtyHackCollection(EnabledDirtyHack[] hacks) => new(hacks);
public static implicit operator DirtyHackCollection(ulong[] packedHacks) => new(packedHacks);
public new int this[DirtyHacks hack] => TryGetValue(hack, out var value) ? value : -1;
public bool IsEnabled(DirtyHacks hack) => ContainsKey(hack);