mirror of
https://github.com/SabreTools/SabreTools.IO.git
synced 2026-02-11 05:35:24 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
96c6bba93e | ||
|
|
b0d81f225b | ||
|
|
ef699ee1fb | ||
|
|
0910b716ba | ||
|
|
584feb33e6 |
@@ -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,27 @@ 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))
|
||||
{
|
||||
writer.Write((Half)value);
|
||||
return true;
|
||||
}
|
||||
#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)
|
||||
@@ -848,7 +862,7 @@ namespace SabreTools.IO.Extensions
|
||||
{
|
||||
// If we have an invalid length
|
||||
if (length < 0)
|
||||
throw new ArgumentOutOfRangeException($"{nameof(length)} must be 0 or a positive value");
|
||||
throw new ArgumentOutOfRangeException($"{nameof(length)} must be 0 or a positive value, {length} requested");
|
||||
|
||||
// Handle the 0-byte case
|
||||
if (length == 0)
|
||||
@@ -856,7 +870,7 @@ namespace SabreTools.IO.Extensions
|
||||
|
||||
// If there are not enough bytes
|
||||
if (offset + length > content.Length)
|
||||
throw new System.IO.EndOfStreamException($"Requested to read {nameof(length)} bytes from {nameof(content)}, {content.Length - offset} returned");
|
||||
throw new System.IO.EndOfStreamException($"Requested to read {length} bytes from {nameof(content)}, {content.Length - offset} returned");
|
||||
|
||||
// Handle the general case, forcing a read of the correct length
|
||||
byte[] buffer = new byte[length];
|
||||
|
||||
@@ -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)
|
||||
@@ -832,7 +846,7 @@ namespace SabreTools.IO.Extensions
|
||||
{
|
||||
// If we have an invalid length
|
||||
if (length < 0)
|
||||
throw new ArgumentOutOfRangeException($"{nameof(length)} must be 0 or a positive value");
|
||||
throw new ArgumentOutOfRangeException($"{nameof(length)} must be 0 or a positive value, {length} requested");
|
||||
|
||||
// Handle the 0-byte case
|
||||
if (length == 0)
|
||||
@@ -842,7 +856,7 @@ namespace SabreTools.IO.Extensions
|
||||
byte[] buffer = new byte[length];
|
||||
int read = stream.Read(buffer, 0, length);
|
||||
if (read < length)
|
||||
throw new EndOfStreamException($"Requested to read {nameof(length)} bytes from {nameof(stream)}, {read} returned");
|
||||
throw new EndOfStreamException($"Requested to read {length} bytes from {nameof(stream)}, {read} returned");
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<LangVersion>latest</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<Version>1.4.9</Version>
|
||||
<Version>1.4.11</Version>
|
||||
<WarningsNotAsErrors>CS0618</WarningsNotAsErrors>
|
||||
|
||||
<!-- Package Properties -->
|
||||
|
||||
Reference in New Issue
Block a user