diff options
| author | gdkchan <gab.dark.100@gmail.com> | 2023-09-27 14:21:26 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-27 19:21:26 +0200 |
| commit | 01c2b8097c2d66839105470d82405a12d57d196f (patch) | |
| tree | 466e1a04138bd14ba31a6a0738a46065b6033129 /src/Ryujinx.Horizon/Sdk/Ngc/Detail/BinaryReader.cs | |
| parent | 4bd2ca3f0de37c53b3ecc78789a0a8296668235a (diff) | |
Implement NGC service (#5681)
* Implement NGC service
* Use raw byte arrays instead of string for _wordSeparators
* Silence IDE0230 for _wordSeparators
* Try to silence warning about _rangeValuesCount not being read on SparseSet
* Make AcType enum private
* Add abstract methods and one TODO that I forgot
* PR feedback
* More PR feedback
* More PR feedback
Diffstat (limited to 'src/Ryujinx.Horizon/Sdk/Ngc/Detail/BinaryReader.cs')
| -rw-r--r-- | src/Ryujinx.Horizon/Sdk/Ngc/Detail/BinaryReader.cs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/Ryujinx.Horizon/Sdk/Ngc/Detail/BinaryReader.cs b/src/Ryujinx.Horizon/Sdk/Ngc/Detail/BinaryReader.cs new file mode 100644 index 00000000..5bad376a --- /dev/null +++ b/src/Ryujinx.Horizon/Sdk/Ngc/Detail/BinaryReader.cs @@ -0,0 +1,63 @@ +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +namespace Ryujinx.Horizon.Sdk.Ngc.Detail +{ + ref struct BinaryReader + { + private readonly ReadOnlySpan<byte> _data; + private int _offset; + + public BinaryReader(ReadOnlySpan<byte> data) + { + _data = data; + } + + public bool Read<T>(out T value) where T : unmanaged + { + int byteLength = Unsafe.SizeOf<T>(); + + if ((uint)(_offset + byteLength) <= (uint)_data.Length) + { + value = MemoryMarshal.Cast<byte, T>(_data[_offset..])[0]; + _offset += byteLength; + + return true; + } + + value = default; + + return false; + } + + public int AllocateAndReadArray<T>(ref T[] array, int length, int maxLengthExclusive) where T : unmanaged + { + return AllocateAndReadArray(ref array, Math.Min(length, maxLengthExclusive)); + } + + public int AllocateAndReadArray<T>(ref T[] array, int length) where T : unmanaged + { + array = new T[length]; + + return ReadArray(array); + } + + public int ReadArray<T>(T[] array) where T : unmanaged + { + if (array != null) + { + int byteLength = array.Length * Unsafe.SizeOf<T>(); + byteLength = Math.Min(byteLength, _data.Length - _offset); + + MemoryMarshal.Cast<byte, T>(_data.Slice(_offset, byteLength)).CopyTo(array); + + _offset += byteLength; + + return byteLength / Unsafe.SizeOf<T>(); + } + + return 0; + } + } +} |
