You've already forked UnrealEngineUWP
mirror of
https://github.com/izzy2lost/UnrealEngineUWP.git
synced 2026-03-26 18:15:20 -07:00
CbObject/CbBinary/CbArray: * Added proxy classes for viewing objects in the debugger. CbArray now displays as an array, CbObject displays as an array of fields, etc... * Removed overloaded "View" methods, which are not required in C# due to GC. CbWriter: * Significantly fewer memory allocations/copying. Output buffer is now allocated in large chunks, with length fields only inserted when copying to the final output buffer. * Methods for writing named/unnamed fields are now more idomatic with other C# serialization classes (ie. WriteXXX(Name, Value) and WriteXXXValue(Value)). Other: * Added serialization tests to Horde projects and fixed NuGet dependencies. #ROBOMERGE-SOURCE: CL 16804192 in //UE5/Main/... #ROBOMERGE-BOT: STARSHIP (Main -> Release-Engine-Test) (v836-16769935) [CL 16804202 by ben marsh in ue5-release-engine-test branch]
117 lines
3.3 KiB
C#
117 lines
3.3 KiB
C#
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Numerics;
|
|
using System.Text;
|
|
|
|
namespace EpicGames.Serialization
|
|
{
|
|
/// <summary>
|
|
/// Methods for reading VarUInt values
|
|
/// </summary>
|
|
public static class VarInt
|
|
{
|
|
/// <summary>
|
|
/// Read a variable-length unsigned integer.
|
|
/// </summary>
|
|
/// <param name="Buffer">A variable-length encoding of an unsigned integer</param>
|
|
/// <returns></returns>
|
|
public static ulong Read(ReadOnlySpan<byte> Buffer)
|
|
{
|
|
return Read(Buffer, out _);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Read a variable-length unsigned integer.
|
|
/// </summary>
|
|
/// <param name="Buffer">A variable-length encoding of an unsigned integer</param>
|
|
/// <param name="BytesRead">The number of bytes consumed from the input</param>
|
|
/// <returns></returns>
|
|
public static ulong Read(ReadOnlySpan<byte> Buffer, out int BytesRead)
|
|
{
|
|
BytesRead = (int)Measure(Buffer);
|
|
|
|
ulong Value = (ulong)(Buffer[0] & (0xff >> BytesRead));
|
|
for (int i = 1; i < BytesRead; i++)
|
|
{
|
|
Value <<= 8;
|
|
Value |= Buffer[i];
|
|
}
|
|
return Value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Measure the length in bytes (1-9) of an encoded variable-length integer.
|
|
/// </summary>
|
|
/// <param name="Buffer">A variable-length encoding of an(signed or unsigned) integer.</param>
|
|
/// <returns>The number of bytes used to encode the integer, in the range 1-9.</returns>
|
|
public static int Measure(ReadOnlySpan<byte> Buffer)
|
|
{
|
|
byte b = Buffer[0];
|
|
b = (byte)~b;
|
|
return BitOperations.LeadingZeroCount(b) - 23;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Measure the number of bytes (1-5) required to encode the 32-bit input.
|
|
/// </summary>
|
|
/// <param name="Value"></param>
|
|
/// <returns></returns>
|
|
public static int Measure(int Value)
|
|
{
|
|
return Measure((uint)Value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Measure the number of bytes (1-5) required to encode the 32-bit input.
|
|
/// </summary>
|
|
/// <param name="Value"></param>
|
|
/// <returns></returns>
|
|
public static int Measure(uint Value)
|
|
{
|
|
return BitOperations.Log2(Value) / 7 + 1;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Measure the number of bytes (1-9) required to encode the 64-bit input.
|
|
/// </summary>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
public static int Measure(ulong value)
|
|
{
|
|
return Math.Min(BitOperations.Log2(value) / 7 + 1, 9);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Write a variable-length unsigned integer.
|
|
/// </summary>
|
|
/// <param name="Value">An unsigned integer to encode</param>
|
|
/// <param name="Buffer">A buffer of at least 9 bytes to write the output to.</param>
|
|
/// <returns>The number of bytes used in the output</returns>
|
|
public static int Write(Span<byte> Buffer, long Value)
|
|
{
|
|
return Write(Buffer, (ulong)Value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Write a variable-length unsigned integer.
|
|
/// </summary>
|
|
/// <param name="Value">An unsigned integer to encode</param>
|
|
/// <param name="Buffer">A buffer of at least 9 bytes to write the output to.</param>
|
|
/// <returns>The number of bytes used in the output</returns>
|
|
public static int Write(Span<byte> Buffer, ulong Value)
|
|
{
|
|
int ByteCount = Measure(Value);
|
|
|
|
for (int Idx = 1; Idx < ByteCount; Idx++)
|
|
{
|
|
Buffer[ByteCount - Idx] = (byte)Value;
|
|
Value >>= 8;
|
|
}
|
|
Buffer[0] = (byte)((0xff << (9 - (int)ByteCount)) | (byte)Value);
|
|
return ByteCount;
|
|
}
|
|
}
|
|
}
|