From e9ea621b93fbb58d9edfca8375918791637bbd52 Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 30 Dec 2020 20:59:04 +0800 Subject: +init --- .../Extensions/SpanReaderExtensions.cs | 70 ++++++++++++++++++++++ .../Extensions/SystemTypesExtensions.cs | 12 ++++ 2 files changed, 82 insertions(+) create mode 100644 Impostor-dev/src/Impostor.Api/Extensions/SpanReaderExtensions.cs create mode 100644 Impostor-dev/src/Impostor.Api/Extensions/SystemTypesExtensions.cs (limited to 'Impostor-dev/src/Impostor.Api/Extensions') diff --git a/Impostor-dev/src/Impostor.Api/Extensions/SpanReaderExtensions.cs b/Impostor-dev/src/Impostor.Api/Extensions/SpanReaderExtensions.cs new file mode 100644 index 0000000..c103ee7 --- /dev/null +++ b/Impostor-dev/src/Impostor.Api/Extensions/SpanReaderExtensions.cs @@ -0,0 +1,70 @@ +using System; +using System.Buffers.Binary; +using System.Runtime.CompilerServices; + +namespace Impostor.Api +{ + /// + /// Priovides a StreamReader-like api throught extensions + /// + public static class SpanReaderExtensions + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static byte ReadByte(this ref ReadOnlySpan input) + { + var original = Advance(ref input); + return original[0]; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int ReadInt32(this ref ReadOnlySpan input) + { + var original = Advance(ref input); + return BinaryPrimitives.ReadInt32LittleEndian(original); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static uint ReadUInt32(this ref ReadOnlySpan input) + { + var original = Advance(ref input); + return BinaryPrimitives.ReadUInt32LittleEndian(original); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static float ReadSingle(this ref ReadOnlySpan input) + { + var original = Advance(ref input); + + // BitConverter.Int32BitsToSingle + // Doesn't exist in net 2.0 for some reason + return Int32BitsToSingle(BinaryPrimitives.ReadInt32LittleEndian(original)); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool ReadBoolean(this ref ReadOnlySpan input) + { + return input.ReadByte() != 0; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static unsafe float Int32BitsToSingle(int value) + { + return *((float*)&value); + } + + /// + /// Advances the position of by the size of . + /// + /// Type that will be read. + /// input "stream"/span. + /// The original input + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static unsafe ReadOnlySpan Advance(ref ReadOnlySpan input) + where T : unmanaged + { + var original = input; + input = input.Slice(sizeof(T)); + return original; + } + } +} \ No newline at end of file diff --git a/Impostor-dev/src/Impostor.Api/Extensions/SystemTypesExtensions.cs b/Impostor-dev/src/Impostor.Api/Extensions/SystemTypesExtensions.cs new file mode 100644 index 0000000..68f1efd --- /dev/null +++ b/Impostor-dev/src/Impostor.Api/Extensions/SystemTypesExtensions.cs @@ -0,0 +1,12 @@ +using Impostor.Api.Innersloth; + +namespace Impostor.Api +{ + public static class SystemTypesExtensions + { + public static string GetFriendlyName(this SystemTypes type) + { + return SystemTypeHelpers.Names[(int)type]; + } + } +} \ No newline at end of file -- cgit v1.1-26-g67d0