mirror of
https://github.com/kwsch/PKHeX
synced 2024-11-23 12:33:06 +00:00
Split FrameworkUtil into separate extension classes
Adds bigendian float/double logic
This commit is contained in:
parent
240fe8d60f
commit
d13488f517
5 changed files with 125 additions and 58 deletions
|
@ -1,58 +0,0 @@
|
|||
#if !NET6
|
||||
#pragma warning disable
|
||||
// ReSharper disable once UnusedType.Global
|
||||
global using static PKHeX.Core.Buffers.Binary.Extra.BinaryPrimitives;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace System
|
||||
{
|
||||
public static class FutureFeatures
|
||||
{
|
||||
public static bool StartsWith(this string str, char value) => str.Length != 0 && str[0] == value;
|
||||
}
|
||||
}
|
||||
|
||||
namespace PKHeX.Core.Buffers.Binary.Extra
|
||||
{
|
||||
internal static class BinaryPrimitives
|
||||
{
|
||||
public static float ReadSingleLittleEndian(ReadOnlySpan<byte> data) => MemoryMarshal.Read<float>(data);
|
||||
public static void WriteSingleLittleEndian(Span<byte> data, float value) => MemoryMarshal.Write(data, ref value);
|
||||
public static double ReadDoubleLittleEndian(ReadOnlySpan<byte> data) => MemoryMarshal.Read<double>(data);
|
||||
public static void WriteDoubleLittleEndian(Span<byte> data, double value) => MemoryMarshal.Write(data, ref value);
|
||||
}
|
||||
}
|
||||
|
||||
namespace System.Runtime.CompilerServices
|
||||
{
|
||||
using Diagnostics;
|
||||
using Diagnostics.CodeAnalysis;
|
||||
|
||||
/// <summary>
|
||||
/// Reserved to be used by the compiler for tracking metadata.
|
||||
/// This class should not be used by developers in source code.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage, DebuggerNonUserCode]
|
||||
internal static class IsExternalInit
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
namespace System.Diagnostics.CodeAnalysis
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Parameter)]
|
||||
internal sealed class NotNullWhenAttribute : Attribute
|
||||
{
|
||||
/// <summary>Initializes the attribute with the specified return value condition.</summary>
|
||||
/// <param name="returnValue">
|
||||
/// The return value condition. If the method returns this value, the associated parameter will not be null.
|
||||
/// </param>
|
||||
public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
|
||||
|
||||
/// <summary>Gets the return value condition.</summary>
|
||||
public bool ReturnValue { get; }
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
86
PKHeX.Core/Util/NetFramework/BinaryPrimitives.cs
Normal file
86
PKHeX.Core/Util/NetFramework/BinaryPrimitives.cs
Normal file
|
@ -0,0 +1,86 @@
|
|||
#if !NET6
|
||||
global using static PKHeX.Core.Buffers.Binary.Extra.BinaryPrimitives;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace PKHeX.Core.Buffers.Binary.Extra;
|
||||
|
||||
internal static class BinaryPrimitives
|
||||
{
|
||||
public static float ReadSingleLittleEndian(ReadOnlySpan<byte> data)
|
||||
{
|
||||
var value = MemoryMarshal.Read<float>(data);
|
||||
if (BitConverter.IsLittleEndian)
|
||||
return value;
|
||||
return ReverseEndianness(value);
|
||||
}
|
||||
|
||||
public static double ReadDoubleLittleEndian(ReadOnlySpan<byte> data)
|
||||
{
|
||||
var value = MemoryMarshal.Read<double>(data);
|
||||
if (BitConverter.IsLittleEndian)
|
||||
return value;
|
||||
return ReverseEndianness(value);
|
||||
}
|
||||
|
||||
public static float ReadSingleBigEndian(Span<byte> data)
|
||||
{
|
||||
var value = MemoryMarshal.Read<float>(data);
|
||||
if (!BitConverter.IsLittleEndian)
|
||||
return value;
|
||||
return ReverseEndianness(value);
|
||||
}
|
||||
|
||||
public static double ReadDoubleBigEndian(Span<byte> data)
|
||||
{
|
||||
var value = MemoryMarshal.Read<double>(data);
|
||||
if (!BitConverter.IsLittleEndian)
|
||||
return value;
|
||||
return ReverseEndianness(value);
|
||||
}
|
||||
|
||||
public static void WriteSingleLittleEndian(Span<byte> data, float value)
|
||||
{
|
||||
if (!BitConverter.IsLittleEndian)
|
||||
value = ReverseEndianness(value);
|
||||
MemoryMarshal.Write(data, ref value);
|
||||
}
|
||||
|
||||
public static void WriteDoubleLittleEndian(Span<byte> data, double value)
|
||||
{
|
||||
if (!BitConverter.IsLittleEndian)
|
||||
value = ReverseEndianness(value);
|
||||
MemoryMarshal.Write(data, ref value);
|
||||
}
|
||||
|
||||
public static void WriteSingleBigEndian(Span<byte> data, float value)
|
||||
{
|
||||
if (BitConverter.IsLittleEndian)
|
||||
value = ReverseEndianness(value);
|
||||
MemoryMarshal.Write(data, ref value);
|
||||
}
|
||||
|
||||
public static void WriteDoubleBigEndian(Span<byte> data, double value)
|
||||
{
|
||||
if (BitConverter.IsLittleEndian)
|
||||
value = ReverseEndianness(value);
|
||||
MemoryMarshal.Write(data, ref value);
|
||||
}
|
||||
|
||||
public static float ReverseEndianness(float input)
|
||||
{
|
||||
Span<byte> span = stackalloc byte[4];
|
||||
MemoryMarshal.Write(span, ref input);
|
||||
span.Reverse();
|
||||
return MemoryMarshal.Read<float>(span);
|
||||
}
|
||||
|
||||
public static double ReverseEndianness(double input)
|
||||
{
|
||||
Span<byte> span = stackalloc byte[8];
|
||||
MemoryMarshal.Write(span, ref input);
|
||||
span.Reverse();
|
||||
return MemoryMarshal.Read<double>(span);
|
||||
}
|
||||
}
|
||||
#endif
|
16
PKHeX.Core/Util/NetFramework/CodeAnalysis.cs
Normal file
16
PKHeX.Core/Util/NetFramework/CodeAnalysis.cs
Normal file
|
@ -0,0 +1,16 @@
|
|||
#if !NET6
|
||||
namespace System.Diagnostics.CodeAnalysis;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Parameter)]
|
||||
internal sealed class NotNullWhenAttribute : Attribute
|
||||
{
|
||||
/// <summary>Initializes the attribute with the specified return value condition.</summary>
|
||||
/// <param name="returnValue">
|
||||
/// The return value condition. If the method returns this value, the associated parameter will not be null.
|
||||
/// </param>
|
||||
public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
|
||||
|
||||
/// <summary>Gets the return value condition.</summary>
|
||||
public bool ReturnValue { get; }
|
||||
}
|
||||
#endif
|
15
PKHeX.Core/Util/NetFramework/CompilerServices.cs
Normal file
15
PKHeX.Core/Util/NetFramework/CompilerServices.cs
Normal file
|
@ -0,0 +1,15 @@
|
|||
#if !NET6
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace System.Runtime.CompilerServices;
|
||||
|
||||
/// <summary>
|
||||
/// Reserved to be used by the compiler for tracking metadata.
|
||||
/// This class should not be used by developers in source code.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage, DebuggerNonUserCode]
|
||||
internal static class IsExternalInit
|
||||
{
|
||||
}
|
||||
#endif
|
8
PKHeX.Core/Util/NetFramework/System.cs
Normal file
8
PKHeX.Core/Util/NetFramework/System.cs
Normal file
|
@ -0,0 +1,8 @@
|
|||
#if !NET6
|
||||
namespace System;
|
||||
|
||||
public static class FutureFeatures
|
||||
{
|
||||
public static bool StartsWith(this string str, char value) => str.Length != 0 && str[0] == value;
|
||||
}
|
||||
#endif
|
Loading…
Reference in a new issue