mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-09-01 17:47:24 +02:00
Co-authored-by: Paul <ritter.paul1@gmail.com> Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com> Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com>
52 lines
1.5 KiB
C#
52 lines
1.5 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.Linq.Expressions;
|
|
using Robust.Shared.Serialization.Markdown;
|
|
using Robust.Shared.Serialization.Markdown.Value;
|
|
using Robust.Shared.Utility;
|
|
|
|
namespace Robust.Shared.Serialization.Manager;
|
|
|
|
public sealed partial class SerializationManager
|
|
{
|
|
//null values are the bane of my existence ~<paul
|
|
|
|
public static Expression GetNullExpression(Expression managerConst, Type type)
|
|
{
|
|
return type.IsValueType
|
|
? Expression.Call(managerConst, nameof(GetNullable), new[] { type })
|
|
: Expression.Constant(null, type);
|
|
}
|
|
|
|
private T? GetNullable<T>() where T : struct
|
|
{
|
|
return null;
|
|
}
|
|
|
|
public static Expression WrapNullableIfNeededExpression(Expression expr, bool nullable)
|
|
{
|
|
if (nullable && expr.Type.IsValueType && !expr.Type.IsNullable())
|
|
return Expression.New(expr.Type.EnsureNullableType().GetConstructor(new[] { expr.Type })!, expr);
|
|
return expr;
|
|
}
|
|
|
|
private T GetValueOrDefault<T>(object? value) where T : struct
|
|
{
|
|
return value as T? ?? default;
|
|
}
|
|
|
|
public static bool IsNull(DataNode node)
|
|
{
|
|
return node.IsNull;
|
|
}
|
|
|
|
public ValueDataNode NullNode() => ValueDataNode.Null();
|
|
|
|
public static Expression StructNullHasValue(Expression valueExpression)
|
|
{
|
|
Debug.Assert(valueExpression.Type.IsValueType);
|
|
Debug.Assert(valueExpression.Type.IsNullable());
|
|
return Expression.Property(valueExpression, "HasValue");
|
|
}
|
|
}
|