AssetRipper_AssetRipper/Source/AssetRipper.Yaml/YamlScalarNode.NumericNode.cs
ds5678 13edc04cd1 Huge yaml export memory improvements
* Export MonoBehaviour byte arrays as scalars rather than sequences
* Do not serialize yaml scalars, especially not for byte arrays
* Eliminate unnecessary state for yaml scalars
* Create a new type hierarchy for yaml scalars
* Implement hexidecimal emission of floating point scalars
* Implement hexidecimal emission of numeric lists
* Resolves #1422
* Resolves #1409
* Resolves #1381
* Related to #927
* Related to #695
2024-08-04 14:32:11 -07:00

36 lines
939 B
C#

using System.Globalization;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace AssetRipper.Yaml;
public abstract partial class YamlScalarNode
{
private sealed class NumericNode<T>(T value) : YamlScalarNode where T : struct, INumber<T>, IConvertible
{
private protected override void EmitCore(Emitter emitter)
{
if (typeof(T) == typeof(float))
{
emitter.Write(Unsafe.As<T, float>(ref value));
}
else if (typeof(T) == typeof(double))
{
emitter.Write(Unsafe.As<T, double>(ref value));
}
else if (IsSigned)
{
emitter.Write(value.ToInt64(CultureInfo.InvariantCulture));
}
else
{
emitter.Write(value.ToUInt64(CultureInfo.InvariantCulture));
}
}
public override string Value => value.ToString() ?? "";
private static bool IsSigned => typeof(T) == typeof(sbyte) || typeof(T) == typeof(short) || typeof(T) == typeof(int) || typeof(T) == typeof(long);
}
}