You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
9.5 KiB

  1. #nullable enable
  2. using IPA.Config.Data;
  3. using IPA.Logging;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Reflection.Emit;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using Boolean = IPA.Config.Data.Boolean;
  12. #if NET3
  13. using Net3_Proxy;
  14. using Array = Net3_Proxy.Array;
  15. #endif
  16. namespace IPA.Config.Stores
  17. {
  18. internal static partial class GeneratedStoreImpl
  19. {
  20. // emit takes no args, leaves Value at top of stack
  21. private static void EmitSerializeMember(ILGenerator il, SerializedMemberInfo member, LocalAllocator GetLocal, Action<ILGenerator> thisarg, Action<ILGenerator> parentobj)
  22. {
  23. EmitLoad(il, member, thisarg);
  24. using var valueTypeLocal =
  25. member.IsNullable
  26. ? GetLocal.Allocate(member.Type)
  27. : default;
  28. if (member.IsNullable)
  29. {
  30. il.Emit(OpCodes.Stloc, valueTypeLocal.Local);
  31. il.Emit(OpCodes.Ldloca, valueTypeLocal.Local);
  32. }
  33. var endSerialize = il.DefineLabel();
  34. if (member.AllowNull)
  35. {
  36. var passedNull = il.DefineLabel();
  37. il.Emit(OpCodes.Dup);
  38. if (member.IsNullable)
  39. il.Emit(OpCodes.Call, member.Nullable_HasValue.GetGetMethod());
  40. il.Emit(OpCodes.Brtrue, passedNull);
  41. il.Emit(OpCodes.Pop);
  42. il.Emit(OpCodes.Ldnull);
  43. il.Emit(OpCodes.Br, endSerialize);
  44. il.MarkLabel(passedNull);
  45. }
  46. if (member is { IsNullable: true, HasConverter: false })
  47. il.Emit(OpCodes.Call, member.Nullable_Value.GetGetMethod());
  48. var memberConversionType = member.ConversionType;
  49. var targetType = GetExpectedValueTypeForType(memberConversionType);
  50. if (member.HasConverter)
  51. {
  52. using var stlocal = GetLocal.Allocate(member.IsNullable ? member.Type : memberConversionType);
  53. using var valLocal = GetLocal.Allocate(typeof(Value));
  54. if (member.IsNullable)
  55. il.Emit(OpCodes.Ldloc_S, valueTypeLocal.Local);
  56. il.Emit(OpCodes.Stloc, stlocal);
  57. il.BeginExceptionBlock();
  58. il.Emit(OpCodes.Ldsfld, member.ConverterField);
  59. il.Emit(OpCodes.Ldloc, stlocal);
  60. if (member.IsGenericConverter)
  61. {
  62. var toValueBase = member.ConverterBase.GetMethod(nameof(ValueConverter<int>.ToValue),
  63. new[] { member.ConverterTarget, typeof(object) });
  64. var toValue = member.Converter.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
  65. .FirstOrDefault(m => m.GetBaseDefinition() == toValueBase) ?? toValueBase;
  66. il.Emit(OpCodes.Ldarg_0);
  67. il.Emit(OpCodes.Call, toValue);
  68. }
  69. else
  70. {
  71. var toValueBase = typeof(IValueConverter).GetMethod(nameof(IValueConverter.ToValue),
  72. new[] { typeof(object), typeof(object) });
  73. var toValue = member.Converter.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
  74. .FirstOrDefault(m => m.GetBaseDefinition() == toValueBase) ?? toValueBase;
  75. il.Emit(OpCodes.Box);
  76. il.Emit(OpCodes.Ldarg_0);
  77. il.Emit(OpCodes.Call, toValue);
  78. }
  79. il.Emit(OpCodes.Stloc, valLocal);
  80. il.BeginCatchBlock(typeof(Exception));
  81. EmitWarnException(il, "Error serializing member using converter");
  82. il.Emit(OpCodes.Ldnull);
  83. il.Emit(OpCodes.Stloc, valLocal);
  84. il.EndExceptionBlock();
  85. il.Emit(OpCodes.Ldloc, valLocal);
  86. }
  87. else if (targetType == typeof(Text))
  88. { // only happens when arg is a string or char
  89. var TextCreate = typeof(Value).GetMethod(nameof(Value.Text));
  90. if (member.Type == typeof(char))
  91. {
  92. var strFromChar = typeof(char).GetMethod(nameof(char.ToString), new[] { typeof(char) });
  93. il.Emit(OpCodes.Call, strFromChar);
  94. }
  95. il.Emit(OpCodes.Call, TextCreate);
  96. }
  97. else if (targetType == typeof(Boolean))
  98. {
  99. var BoolCreate = typeof(Value).GetMethod(nameof(Value.Bool));
  100. il.Emit(OpCodes.Call, BoolCreate);
  101. }
  102. else if (targetType == typeof(Integer))
  103. {
  104. var IntCreate = typeof(Value).GetMethod(nameof(Value.Integer));
  105. EmitNumberConvertTo(il, IntCreate.GetParameters()[0].ParameterType, member.Type);
  106. il.Emit(OpCodes.Call, IntCreate);
  107. }
  108. else if (targetType == typeof(FloatingPoint))
  109. {
  110. var FloatCreate = typeof(Value).GetMethod(nameof(Value.Float));
  111. EmitNumberConvertTo(il, FloatCreate.GetParameters()[0].ParameterType, member.Type);
  112. il.Emit(OpCodes.Call, FloatCreate);
  113. }
  114. else if (targetType == typeof(List))
  115. {
  116. // TODO: impl this (enumerables)
  117. Logger.Config.Warn($"Implicit conversions to {targetType} are not currently implemented");
  118. il.Emit(OpCodes.Pop);
  119. il.Emit(OpCodes.Ldnull);
  120. }
  121. else if (targetType == typeof(Map))
  122. {
  123. // TODO: support other aggregate types
  124. if (!memberConversionType.IsValueType)
  125. {
  126. // if it is a reference type, we assume that its a generated type implementing IGeneratedStore
  127. var IGeneratedStore_Serialize = typeof(IGeneratedStore).GetMethod(nameof(IGeneratedStore.Serialize));
  128. var IGeneratedStoreT_CopyFrom = typeof(IGeneratedStore<>).MakeGenericType(member.Type)
  129. .GetMethod(nameof(IGeneratedStore<object>.CopyFrom));
  130. if (!member.IsVirtual)
  131. {
  132. var noCreate = il.DefineLabel();
  133. using var stlocal = GetLocal.Allocate(member.Type);
  134. // first check to make sure that this is an IGeneratedStore, because we don't control assignments to it
  135. il.Emit(OpCodes.Dup);
  136. il.Emit(OpCodes.Isinst, typeof(IGeneratedStore));
  137. il.Emit(OpCodes.Brtrue_S, noCreate);
  138. il.Emit(OpCodes.Stloc, stlocal);
  139. EmitCreateChildGenerated(il, member.Type, parentobj);
  140. il.Emit(OpCodes.Dup);
  141. il.Emit(OpCodes.Ldloc, stlocal);
  142. il.Emit(OpCodes.Ldc_I4_0);
  143. il.Emit(OpCodes.Callvirt, IGeneratedStoreT_CopyFrom);
  144. il.Emit(OpCodes.Dup);
  145. il.Emit(OpCodes.Stloc, stlocal);
  146. EmitStore(il, member, il => il.Emit(OpCodes.Ldloc, stlocal), thisarg);
  147. il.MarkLabel(noCreate);
  148. }
  149. il.Emit(OpCodes.Callvirt, IGeneratedStore_Serialize);
  150. }
  151. else
  152. { // generate serialization for value types
  153. using var valueLocal = GetLocal.Allocate(memberConversionType);
  154. var structure = ReadObjectMembers(memberConversionType);
  155. if (!structure.Any())
  156. {
  157. Logger.Config.Warn($"Custom value type {memberConversionType.FullName} (when compiling serialization of" +
  158. $" {member.Name} on {member.Member.DeclaringType.FullName}) has no accessible members");
  159. il.Emit(OpCodes.Pop);
  160. }
  161. else
  162. {
  163. il.Emit(OpCodes.Stloc, valueLocal);
  164. }
  165. EmitSerializeStructure(il, structure, GetLocal, il => il.Emit(OpCodes.Ldloca, valueLocal), parentobj);
  166. }
  167. }
  168. il.MarkLabel(endSerialize);
  169. }
  170. private static void EmitSerializeStructure(ILGenerator il, IEnumerable<SerializedMemberInfo> structure, LocalAllocator GetLocal, Action<ILGenerator> thisarg, Action<ILGenerator> parentobj)
  171. {
  172. var MapCreate = typeof(Value).GetMethod(nameof(Value.Map));
  173. var MapAdd = typeof(Map).GetMethod(nameof(Map.Add));
  174. using var mapLocal = GetLocal.Allocate(typeof(Map));
  175. using var valueLocal = GetLocal.Allocate(typeof(Value));
  176. il.Emit(OpCodes.Call, MapCreate);
  177. il.Emit(OpCodes.Stloc, mapLocal);
  178. foreach (var mem in structure)
  179. {
  180. EmitSerializeMember(il, mem, GetLocal, thisarg, parentobj);
  181. il.Emit(OpCodes.Stloc, valueLocal);
  182. il.Emit(OpCodes.Ldloc, mapLocal);
  183. il.Emit(OpCodes.Ldstr, mem.Name);
  184. il.Emit(OpCodes.Ldloc, valueLocal);
  185. il.Emit(OpCodes.Call, MapAdd);
  186. }
  187. il.Emit(OpCodes.Ldloc, mapLocal);
  188. }
  189. }
  190. }