mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-14 19:29:36 +01:00
fixes struct instantiation (#3129)
This commit is contained in:
@@ -22,40 +22,26 @@ namespace Robust.Shared.Serialization.Manager
|
||||
|
||||
var generator = method.GetILGenerator();
|
||||
|
||||
if (type.IsValueType)
|
||||
var constructor = type.GetConstructor(Type.EmptyTypes);
|
||||
|
||||
if (constructor == null)
|
||||
{
|
||||
if (!type.IsValueType) return null;
|
||||
generator.DeclareLocal(type);
|
||||
generator.DeclareLocal(typeof(object));
|
||||
|
||||
generator.Emit(OpCodes.Ldloca_S, 0);
|
||||
|
||||
generator.Emit(OpCodes.Initobj, type);
|
||||
|
||||
generator.Emit(OpCodes.Ldloc_0);
|
||||
generator.Emit(OpCodes.Box, type);
|
||||
generator.Emit(OpCodes.Stloc_1);
|
||||
|
||||
generator.Emit(OpCodes.Ldloc_1);
|
||||
generator.Emit(OpCodes.Ret);
|
||||
}
|
||||
else
|
||||
{
|
||||
generator.DeclareLocal(typeof(object));
|
||||
|
||||
var constructor = type.GetConstructor(Type.EmptyTypes);
|
||||
|
||||
if (constructor == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
generator.Emit(OpCodes.Newobj, constructor);
|
||||
generator.Emit(OpCodes.Stloc_0);
|
||||
|
||||
generator.Emit(OpCodes.Ldloc_0);
|
||||
generator.Emit(OpCodes.Ret);
|
||||
}
|
||||
|
||||
if (type.IsValueType)
|
||||
generator.Emit(OpCodes.Box, type);
|
||||
|
||||
generator.Emit(OpCodes.Ret);
|
||||
|
||||
return method.CreateDelegate<InstantiationDelegate<object>>();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Robust.Shared.Serialization.Manager.Definition;
|
||||
@@ -25,7 +26,7 @@ namespace Robust.Shared.Serialization.Manager
|
||||
|
||||
public T Read<T>(DataNode node, ISerializationContext? context = null, bool skipHook = false, T? value = default) //todo paul this default should be null
|
||||
{
|
||||
return (T)Read(typeof(T), node, context, skipHook, value)!;
|
||||
return (T)Read(typeof(T), node, context, skipHook, EqualityComparer<T>.Default.Equals(value, default) ? null : value)!;
|
||||
}
|
||||
|
||||
public object? Read(Type type, DataNode node, ISerializationContext? context = null, bool skipHook = false, object? value = null)
|
||||
|
||||
Reference in New Issue
Block a user