From 31cbf4a3eaeef5a144253868f6f0f359f5d82d93 Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Tue, 14 Dec 2021 18:41:21 -0600 Subject: [PATCH] Fix serializing nullable value types --- .../Stores/GeneratedStoreImpl/Serialization.cs | 13 ++++++++++++- .../Config/Stores/GeneratedStoreImpl/Utility.cs | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/IPA.Loader/Config/Stores/GeneratedStoreImpl/Serialization.cs b/IPA.Loader/Config/Stores/GeneratedStoreImpl/Serialization.cs index b1cd6138..9005d4e2 100644 --- a/IPA.Loader/Config/Stores/GeneratedStoreImpl/Serialization.cs +++ b/IPA.Loader/Config/Stores/GeneratedStoreImpl/Serialization.cs @@ -23,6 +23,17 @@ namespace IPA.Config.Stores { EmitLoad(il, member, thisarg); + using var valueTypeLocal = + member.IsNullable + ? GetLocal.Allocate(member.Type) + : default; + + if (member.IsNullable) + { + il.Emit(OpCodes.Stloc, valueTypeLocal.Local); + il.Emit(OpCodes.Ldloca, valueTypeLocal.Local); + } + var endSerialize = il.DefineLabel(); if (member.AllowNull) @@ -48,7 +59,7 @@ namespace IPA.Config.Stores var targetType = GetExpectedValueTypeForType(memberConversionType); if (member.HasConverter) { - using var stlocal = GetLocal.Allocate(member.Type); + using var stlocal = GetLocal.Allocate(memberConversionType); using var valLocal = GetLocal.Allocate(typeof(Value)); il.Emit(OpCodes.Stloc, stlocal); diff --git a/IPA.Loader/Config/Stores/GeneratedStoreImpl/Utility.cs b/IPA.Loader/Config/Stores/GeneratedStoreImpl/Utility.cs index a5d77664..216e0664 100644 --- a/IPA.Loader/Config/Stores/GeneratedStoreImpl/Utility.cs +++ b/IPA.Loader/Config/Stores/GeneratedStoreImpl/Utility.cs @@ -60,7 +60,7 @@ namespace IPA.Config.Stores #endif public static implicit operator LocalBuilder(AllocatedLocal loc) => loc.Local; - public void Dealloc() => allocator.Deallocate(this); + public void Dealloc() => allocator?.Deallocate(this); public void Dispose() => Dealloc(); }