From 6aacbe602445109527ef40863d30624b083d3bb8 Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Fri, 28 Feb 2020 17:34:51 -0600 Subject: [PATCH] Fixed custom type field serialization in config system --- .../Config/Providers/JsonConfigProvider.cs | 2 +- .../Config/Stores/GeneratedStoreImpl.cs | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/IPA.Loader/Config/Providers/JsonConfigProvider.cs b/IPA.Loader/Config/Providers/JsonConfigProvider.cs index dedec706..7fb1bfa7 100644 --- a/IPA.Loader/Config/Providers/JsonConfigProvider.cs +++ b/IPA.Loader/Config/Providers/JsonConfigProvider.cs @@ -117,7 +117,7 @@ namespace IPA.Config.Providers public void Store(Value value, FileInfo file) { - if (file.Directory.Exists) + if (!file.Directory.Exists) file.Directory.Create(); try diff --git a/IPA.Loader/Config/Stores/GeneratedStoreImpl.cs b/IPA.Loader/Config/Stores/GeneratedStoreImpl.cs index b43f0aaf..1513a43c 100644 --- a/IPA.Loader/Config/Stores/GeneratedStoreImpl.cs +++ b/IPA.Loader/Config/Stores/GeneratedStoreImpl.cs @@ -1604,6 +1604,28 @@ namespace IPA.Config.Stores // for now, we assume that its a generated type implementing IGeneratedStore var IGeneratedStore_Serialize = typeof(IGeneratedStore).GetMethod(nameof(IGeneratedStore.Serialize)); + var IGeneratedStoreT_CopyFrom = typeof(IGeneratedStore<>).GetMethod(nameof(IGeneratedStore.CopyFrom)); + + if (member.IsField) + { + var noCreate = il.DefineLabel(); + var stlocal = GetLocal(member.Type); + + // first check to make sure that this is an IGeneratedStore, because we don't control assignments to it + il.Emit(OpCodes.Dup); + il.Emit(OpCodes.Isinst, typeof(IGeneratedStore)); + il.Emit(OpCodes.Brtrue_S, noCreate); + il.Emit(OpCodes.Stloc, stlocal); + EmitCreateChildGenerated(il, member.Type); + il.Emit(OpCodes.Dup); + il.Emit(OpCodes.Ldloc, stlocal); + il.Emit(OpCodes.Ldc_I4_0); + il.Emit(OpCodes.Callvirt, IGeneratedStoreT_CopyFrom); + il.Emit(OpCodes.Dup); + il.Emit(OpCodes.Stloc, stlocal); + EmitStore(il, member, il => il.Emit(OpCodes.Ldloc, stlocal)); + il.MarkLabel(noCreate); + } il.Emit(OpCodes.Callvirt, IGeneratedStore_Serialize); }