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.

205 lines
9.3 KiB

  1. using IPA.Logging;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Reflection.Emit;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace IPA.Config.Stores
  10. {
  11. internal static partial class GeneratedStoreImpl
  12. {
  13. #region Logs
  14. private static readonly MethodInfo LogErrorMethod = typeof(GeneratedStoreImpl).GetMethod(nameof(LogError), BindingFlags.NonPublic | BindingFlags.Static);
  15. internal static void LogError(Type expected, Type found, string message)
  16. {
  17. Logger.config.Notice($"{message}{(expected == null ? "" : $" (expected {expected}, found {found?.ToString() ?? "null"})")}");
  18. }
  19. private static readonly MethodInfo LogWarningMethod = typeof(GeneratedStoreImpl).GetMethod(nameof(LogWarning), BindingFlags.NonPublic | BindingFlags.Static);
  20. internal static void LogWarning(string message)
  21. {
  22. Logger.config.Warn(message);
  23. }
  24. private static readonly MethodInfo LogWarningExceptionMethod = typeof(GeneratedStoreImpl).GetMethod(nameof(LogWarningException), BindingFlags.NonPublic | BindingFlags.Static);
  25. internal static void LogWarningException(Exception exception)
  26. {
  27. Logger.config.Warn(exception);
  28. }
  29. #endregion
  30. private delegate LocalBuilder GetLocal(Type type, int idx = 0);
  31. private static GetLocal MakeGetLocal(ILGenerator il)
  32. { // TODO: improve this shit a bit so that i can release a hold of a variable and do more auto managing
  33. var locals = new List<LocalBuilder>();
  34. LocalBuilder GetLocal(Type ty, int i = 0)
  35. {
  36. var builder = locals.Where(b => b.LocalType == ty).Skip(i).FirstOrDefault();
  37. if (builder == null)
  38. {
  39. builder = il.DeclareLocal(ty);
  40. locals.Add(builder);
  41. }
  42. return builder;
  43. }
  44. return GetLocal;
  45. }
  46. private static void EmitLoad(ILGenerator il, SerializedMemberInfo member, Action<ILGenerator> thisarg)
  47. {
  48. thisarg ??= il => il.Emit(OpCodes.Ldarg_0);
  49. thisarg(il); // load this
  50. if (member.IsField)
  51. il.Emit(OpCodes.Ldfld, member.Member as FieldInfo);
  52. else
  53. { // member is a property
  54. var prop = member.Member as PropertyInfo;
  55. var getter = prop.GetGetMethod();
  56. if (getter == null) throw new InvalidOperationException($"Property {member.Name} does not have a getter and is not ignored");
  57. il.Emit(OpCodes.Call, getter);
  58. }
  59. }
  60. private static void EmitStore(ILGenerator il, SerializedMemberInfo member, Action<ILGenerator> value, Action<ILGenerator> thisobj)
  61. {
  62. thisobj(il);
  63. value(il);
  64. if (member.IsField)
  65. il.Emit(OpCodes.Stfld, member.Member as FieldInfo);
  66. else
  67. { // member is a property
  68. var prop = member.Member as PropertyInfo;
  69. var setter = prop.GetSetMethod();
  70. if (setter == null) throw new InvalidOperationException($"Property {member.Name} does not have a setter and is not ignored");
  71. il.Emit(OpCodes.Call, setter);
  72. }
  73. }
  74. private static void EmitWarnException(ILGenerator il, string v)
  75. {
  76. il.Emit(OpCodes.Ldstr, v);
  77. il.Emit(OpCodes.Call, LogWarningMethod);
  78. il.Emit(OpCodes.Call, LogWarningExceptionMethod);
  79. }
  80. private static void EmitLogError(ILGenerator il, string message, bool tailcall = false, Action<ILGenerator> expected = null, Action<ILGenerator> found = null)
  81. {
  82. if (expected == null) expected = il => il.Emit(OpCodes.Ldnull);
  83. if (found == null) found = il => il.Emit(OpCodes.Ldnull);
  84. expected(il);
  85. found(il);
  86. il.Emit(OpCodes.Ldstr, message);
  87. if (tailcall) il.Emit(OpCodes.Tailcall);
  88. il.Emit(OpCodes.Call, LogErrorMethod);
  89. }
  90. private static readonly MethodInfo Type_GetTypeFromHandle = typeof(Type).GetMethod(nameof(Type.GetTypeFromHandle));
  91. private static void EmitTypeof(ILGenerator il, Type type)
  92. {
  93. il.Emit(OpCodes.Ldtoken, type);
  94. il.Emit(OpCodes.Call, Type_GetTypeFromHandle);
  95. }
  96. private static readonly Type IDisposable_t = typeof(IDisposable);
  97. private static readonly MethodInfo IDisposable_Dispose = IDisposable_t.GetMethod(nameof(IDisposable.Dispose));
  98. private static readonly Type Decimal_t = typeof(decimal);
  99. private static readonly ConstructorInfo Decimal_FromFloat = Decimal_t.GetConstructor(new[] { typeof(float) });
  100. private static readonly ConstructorInfo Decimal_FromDouble = Decimal_t.GetConstructor(new[] { typeof(double) });
  101. private static readonly ConstructorInfo Decimal_FromInt = Decimal_t.GetConstructor(new[] { typeof(int) });
  102. private static readonly ConstructorInfo Decimal_FromUInt = Decimal_t.GetConstructor(new[] { typeof(uint) });
  103. private static readonly ConstructorInfo Decimal_FromLong = Decimal_t.GetConstructor(new[] { typeof(long) });
  104. private static readonly ConstructorInfo Decimal_FromULong = Decimal_t.GetConstructor(new[] { typeof(ulong) });
  105. private static void EmitNumberConvertTo(ILGenerator il, Type to, Type from)
  106. { // WARNING: THIS USES THE NO-OVERFLOW OPCODES
  107. if (to == from) return;
  108. if (to == Decimal_t)
  109. {
  110. if (from == typeof(float)) il.Emit(OpCodes.Newobj, Decimal_FromFloat);
  111. else if (from == typeof(double)) il.Emit(OpCodes.Newobj, Decimal_FromDouble);
  112. else if (from == typeof(long)) il.Emit(OpCodes.Newobj, Decimal_FromLong);
  113. else if (from == typeof(ulong)) il.Emit(OpCodes.Newobj, Decimal_FromULong);
  114. else if (from == typeof(int)) il.Emit(OpCodes.Newobj, Decimal_FromInt);
  115. else if (from == typeof(uint)) il.Emit(OpCodes.Newobj, Decimal_FromUInt);
  116. else if (from == typeof(IntPtr))
  117. {
  118. EmitNumberConvertTo(il, typeof(long), from);
  119. EmitNumberConvertTo(il, to, typeof(long));
  120. }
  121. else if (from == typeof(UIntPtr))
  122. {
  123. EmitNumberConvertTo(il, typeof(ulong), from);
  124. EmitNumberConvertTo(il, to, typeof(ulong));
  125. }
  126. else
  127. { // if the source is anything else, we first convert to int because that can contain all other values
  128. EmitNumberConvertTo(il, typeof(int), from);
  129. EmitNumberConvertTo(il, to, typeof(int));
  130. };
  131. }
  132. else if (from == Decimal_t)
  133. {
  134. if (to == typeof(IntPtr))
  135. {
  136. EmitNumberConvertTo(il, typeof(long), from);
  137. EmitNumberConvertTo(il, to, typeof(long));
  138. }
  139. else if (to == typeof(UIntPtr))
  140. {
  141. EmitNumberConvertTo(il, typeof(ulong), from);
  142. EmitNumberConvertTo(il, to, typeof(ulong));
  143. }
  144. else
  145. {
  146. var method = Decimal_t.GetMethod($"To{to.Name}"); // conveniently, this is the pattern of the to* names
  147. il.Emit(OpCodes.Call, method);
  148. }
  149. }
  150. else if (to == typeof(IntPtr)) il.Emit(OpCodes.Conv_I);
  151. else if (to == typeof(UIntPtr)) il.Emit(OpCodes.Conv_U);
  152. else if (to == typeof(sbyte)) il.Emit(OpCodes.Conv_I1);
  153. else if (to == typeof(byte)) il.Emit(OpCodes.Conv_U1);
  154. else if (to == typeof(short)) il.Emit(OpCodes.Conv_I2);
  155. else if (to == typeof(ushort)) il.Emit(OpCodes.Conv_U2);
  156. else if (to == typeof(int)) il.Emit(OpCodes.Conv_I4);
  157. else if (to == typeof(uint)) il.Emit(OpCodes.Conv_U4);
  158. else if (to == typeof(long)) il.Emit(OpCodes.Conv_I8);
  159. else if (to == typeof(ulong)) il.Emit(OpCodes.Conv_U8);
  160. else if (to == typeof(float))
  161. {
  162. if (from == typeof(byte)
  163. || from == typeof(ushort)
  164. || from == typeof(uint)
  165. || from == typeof(ulong)
  166. || from == typeof(UIntPtr)) il.Emit(OpCodes.Conv_R_Un);
  167. il.Emit(OpCodes.Conv_R4);
  168. }
  169. else if (to == typeof(double))
  170. {
  171. if (from == typeof(byte)
  172. || from == typeof(ushort)
  173. || from == typeof(uint)
  174. || from == typeof(ulong)
  175. || from == typeof(UIntPtr)) il.Emit(OpCodes.Conv_R_Un);
  176. il.Emit(OpCodes.Conv_R8);
  177. }
  178. }
  179. private static void EmitCreateChildGenerated(ILGenerator il, Type childType, Action<ILGenerator> parentobj)
  180. {
  181. var method = CreateGParent.MakeGenericMethod(childType);
  182. parentobj(il);
  183. il.Emit(OpCodes.Call, method);
  184. }
  185. }
  186. }