mirror of
https://github.com/SabreTools/SabreTools.IO.git
synced 2026-09-22 14:45:58 +00:00
Handle special struct types
This commit is contained in:
@@ -494,6 +494,20 @@ namespace SabreTools.IO.Extensions
|
||||
/// </remarks>
|
||||
public static object? ReadType(this BinaryReader reader, Type type)
|
||||
{
|
||||
// Handle special struct cases
|
||||
if (type == typeof(Guid))
|
||||
return reader.ReadGuid();
|
||||
#if NET6_0_OR_GREATER
|
||||
else if (type == typeof(Half))
|
||||
return reader.ReadHalf();
|
||||
#endif
|
||||
#if NET7_0_OR_GREATER
|
||||
else if (type == typeof(Int128))
|
||||
return reader.ReadInt128();
|
||||
else if (type == typeof(UInt128))
|
||||
return reader.ReadUInt128();
|
||||
#endif
|
||||
|
||||
if (type.IsClass || (type.IsValueType && !type.IsEnum && !type.IsPrimitive))
|
||||
return ReadComplexType(reader, type);
|
||||
else if (type.IsValueType && type.IsEnum)
|
||||
|
||||
@@ -448,6 +448,24 @@ namespace SabreTools.IO.Extensions
|
||||
/// </remarks>
|
||||
public static bool WriteType(this BinaryWriter writer, object? value, Type type)
|
||||
{
|
||||
// Null values cannot be written
|
||||
if (value == null)
|
||||
return true;
|
||||
|
||||
// Handle special struct cases
|
||||
if (type == typeof(Guid))
|
||||
return writer.Write((Guid)value);
|
||||
#if NET6_0_OR_GREATER
|
||||
else if (type == typeof(Half))
|
||||
return writer.Write((Half)value);
|
||||
#endif
|
||||
#if NET7_0_OR_GREATER
|
||||
else if (type == typeof(Int128))
|
||||
return writer.Write((Int128)value);
|
||||
else if (type == typeof(UInt128))
|
||||
return writer.Write((UInt128)value);
|
||||
#endif
|
||||
|
||||
if (type.IsClass || (type.IsValueType && !type.IsEnum && !type.IsPrimitive))
|
||||
return WriteComplexType(writer, value, type);
|
||||
else if (type.IsValueType && type.IsEnum)
|
||||
|
||||
@@ -622,6 +622,20 @@ namespace SabreTools.IO.Extensions
|
||||
/// </remarks>
|
||||
public static object? ReadType(this byte[] content, ref int offset, Type type)
|
||||
{
|
||||
// Handle special struct cases
|
||||
if (type == typeof(Guid))
|
||||
return content.ReadGuid(ref offset);
|
||||
#if NET6_0_OR_GREATER
|
||||
else if (type == typeof(Half))
|
||||
return content.ReadHalf(ref offset);
|
||||
#endif
|
||||
#if NET7_0_OR_GREATER
|
||||
else if (type == typeof(Int128))
|
||||
return content.ReadInt128(ref offset);
|
||||
else if (type == typeof(UInt128))
|
||||
return content.ReadUInt128(ref offset);
|
||||
#endif
|
||||
|
||||
if (type.IsClass || (type.IsValueType && !type.IsEnum && !type.IsPrimitive))
|
||||
return ReadComplexType(content, ref offset, type);
|
||||
else if (type.IsValueType && type.IsEnum)
|
||||
|
||||
@@ -601,6 +601,24 @@ namespace SabreTools.IO.Extensions
|
||||
/// </remarks>
|
||||
public static bool WriteType(this byte[] content, ref int offset, object? value, Type type)
|
||||
{
|
||||
// Null values cannot be written
|
||||
if (value == null)
|
||||
return true;
|
||||
|
||||
// Handle special struct cases
|
||||
if (type == typeof(Guid))
|
||||
return content.Write(ref offset, (Guid)value);
|
||||
#if NET6_0_OR_GREATER
|
||||
else if (type == typeof(Half))
|
||||
return content.Write(ref offset, (Half)value);
|
||||
#endif
|
||||
#if NET7_0_OR_GREATER
|
||||
else if (type == typeof(Int128))
|
||||
return content.Write(ref offset, (Int128)value);
|
||||
else if (type == typeof(UInt128))
|
||||
return content.Write(ref offset, (UInt128)value);
|
||||
#endif
|
||||
|
||||
if (type.IsClass || (type.IsValueType && !type.IsEnum && !type.IsPrimitive))
|
||||
return WriteComplexType(content, ref offset, value, type);
|
||||
else if (type.IsValueType && type.IsEnum)
|
||||
|
||||
@@ -606,6 +606,20 @@ namespace SabreTools.IO.Extensions
|
||||
/// </remarks>
|
||||
public static object? ReadType(this Stream stream, Type type)
|
||||
{
|
||||
// Handle special struct cases
|
||||
if (type == typeof(Guid))
|
||||
return stream.ReadGuid();
|
||||
#if NET6_0_OR_GREATER
|
||||
else if (type == typeof(Half))
|
||||
return stream.ReadHalf();
|
||||
#endif
|
||||
#if NET7_0_OR_GREATER
|
||||
else if (type == typeof(Int128))
|
||||
return stream.ReadInt128();
|
||||
else if (type == typeof(UInt128))
|
||||
return stream.ReadUInt128();
|
||||
#endif
|
||||
|
||||
if (type.IsClass || (type.IsValueType && !type.IsEnum && !type.IsPrimitive))
|
||||
return ReadComplexType(stream, type);
|
||||
else if (type.IsValueType && type.IsEnum)
|
||||
|
||||
@@ -602,6 +602,24 @@ namespace SabreTools.IO.Extensions
|
||||
/// </remarks>
|
||||
public static bool WriteType(this Stream stream, object? value, Type type)
|
||||
{
|
||||
// Null values cannot be written
|
||||
if (value == null)
|
||||
return true;
|
||||
|
||||
// Handle special struct cases
|
||||
if (type == typeof(Guid))
|
||||
return stream.Write((Guid)value);
|
||||
#if NET6_0_OR_GREATER
|
||||
else if (type == typeof(Half))
|
||||
return stream.Write((Half)value);
|
||||
#endif
|
||||
#if NET7_0_OR_GREATER
|
||||
else if (type == typeof(Int128))
|
||||
return stream.Write((Int128)value);
|
||||
else if (type == typeof(UInt128))
|
||||
return stream.Write((UInt128)value);
|
||||
#endif
|
||||
|
||||
if (type.IsClass || (type.IsValueType && !type.IsEnum && !type.IsPrimitive))
|
||||
return WriteComplexType(stream, value, type);
|
||||
else if (type.IsValueType && type.IsEnum)
|
||||
|
||||
Reference in New Issue
Block a user