Emitter.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using System.Reflection.Emit;
  5. using System.Runtime.InteropServices;
  6. namespace Harmony.ILCopying
  7. {
  8. public class LeaveTry
  9. {
  10. public override string ToString()
  11. {
  12. return "(autogenerated)";
  13. }
  14. }
  15. public static class Emitter
  16. {
  17. static readonly GetterHandler codeLenGetter = FastAccess.CreateFieldGetter(typeof(ILGenerator), "code_len", "m_length");
  18. static readonly GetterHandler localsGetter = FastAccess.CreateFieldGetter(typeof(ILGenerator), "locals");
  19. static readonly GetterHandler localCountGetter = FastAccess.CreateFieldGetter(typeof(ILGenerator), "m_localCount");
  20. public static string CodePos(ILGenerator il)
  21. {
  22. var offset = (int)codeLenGetter(il);
  23. return string.Format("L_{0:x4}: ", offset);
  24. }
  25. public static void LogIL(ILGenerator il, OpCode opCode, object argument)
  26. {
  27. if (HarmonyInstance.DEBUG)
  28. {
  29. var argStr = FormatArgument(argument);
  30. var space = argStr.Length > 0 ? " " : "";
  31. FileLog.LogBuffered(string.Format("{0}{1}{2}{3}", CodePos(il), opCode, space, argStr));
  32. }
  33. }
  34. public static void LogLocalVariable(ILGenerator il, LocalBuilder variable)
  35. {
  36. if (HarmonyInstance.DEBUG)
  37. {
  38. var localCount = -1;
  39. var localsArray = localsGetter != null ? (LocalBuilder[])localsGetter(il) : null;
  40. if (localsArray != null && localsArray.Length > 0)
  41. localCount = localsArray.Length;
  42. else
  43. localCount = (int)localCountGetter(il);
  44. var str = string.Format("{0}Local var {1}: {2}{3}", CodePos(il), localCount - 1, variable.LocalType.FullName, variable.IsPinned ? "(pinned)" : "");
  45. FileLog.LogBuffered(str);
  46. }
  47. }
  48. public static string FormatArgument(object argument)
  49. {
  50. if (argument == null) return "NULL";
  51. var type = argument.GetType();
  52. if (type == typeof(string))
  53. return "\"" + argument + "\"";
  54. if (type == typeof(Label))
  55. return "Label" + ((Label)argument).GetHashCode();
  56. if (type == typeof(Label[]))
  57. return "Labels" + string.Join(",", ((Label[])argument).Select(l => l.GetHashCode().ToString()).ToArray());
  58. if (type == typeof(LocalBuilder))
  59. return ((LocalBuilder)argument).LocalIndex + " (" + ((LocalBuilder)argument).LocalType + ")";
  60. return argument.ToString().Trim();
  61. }
  62. public static void MarkLabel(ILGenerator il, Label label)
  63. {
  64. if (HarmonyInstance.DEBUG) FileLog.LogBuffered(CodePos(il) + FormatArgument(label));
  65. il.MarkLabel(label);
  66. }
  67. public static void MarkBlockBefore(ILGenerator il, ExceptionBlock block, out Label? label)
  68. {
  69. label = null;
  70. switch (block.blockType)
  71. {
  72. case ExceptionBlockType.BeginExceptionBlock:
  73. if (HarmonyInstance.DEBUG)
  74. {
  75. FileLog.LogBuffered(".try");
  76. FileLog.LogBuffered("{");
  77. FileLog.ChangeIndent(1);
  78. }
  79. label = il.BeginExceptionBlock();
  80. return;
  81. case ExceptionBlockType.BeginCatchBlock:
  82. if (HarmonyInstance.DEBUG)
  83. {
  84. // fake log a LEAVE code since BeginCatchBlock() does add it
  85. LogIL(il, OpCodes.Leave, new LeaveTry());
  86. FileLog.ChangeIndent(-1);
  87. FileLog.LogBuffered("} // end try");
  88. FileLog.LogBuffered(".catch " + block.catchType);
  89. FileLog.LogBuffered("{");
  90. FileLog.ChangeIndent(1);
  91. }
  92. il.BeginCatchBlock(block.catchType);
  93. return;
  94. case ExceptionBlockType.BeginExceptFilterBlock:
  95. if (HarmonyInstance.DEBUG)
  96. {
  97. // fake log a LEAVE code since BeginCatchBlock() does add it
  98. LogIL(il, OpCodes.Leave, new LeaveTry());
  99. FileLog.ChangeIndent(-1);
  100. FileLog.LogBuffered("} // end try");
  101. FileLog.LogBuffered(".filter");
  102. FileLog.LogBuffered("{");
  103. FileLog.ChangeIndent(1);
  104. }
  105. il.BeginExceptFilterBlock();
  106. return;
  107. case ExceptionBlockType.BeginFaultBlock:
  108. if (HarmonyInstance.DEBUG)
  109. {
  110. // fake log a LEAVE code since BeginCatchBlock() does add it
  111. LogIL(il, OpCodes.Leave, new LeaveTry());
  112. FileLog.ChangeIndent(-1);
  113. FileLog.LogBuffered("} // end try");
  114. FileLog.LogBuffered(".fault");
  115. FileLog.LogBuffered("{");
  116. FileLog.ChangeIndent(1);
  117. }
  118. il.BeginFaultBlock();
  119. return;
  120. case ExceptionBlockType.BeginFinallyBlock:
  121. if (HarmonyInstance.DEBUG)
  122. {
  123. // fake log a LEAVE code since BeginCatchBlock() does add it
  124. LogIL(il, OpCodes.Leave, new LeaveTry());
  125. FileLog.ChangeIndent(-1);
  126. FileLog.LogBuffered("} // end try");
  127. FileLog.LogBuffered(".finally");
  128. FileLog.LogBuffered("{");
  129. FileLog.ChangeIndent(1);
  130. }
  131. il.BeginFinallyBlock();
  132. return;
  133. }
  134. }
  135. public static void MarkBlockAfter(ILGenerator il, ExceptionBlock block)
  136. {
  137. if (block.blockType == ExceptionBlockType.EndExceptionBlock)
  138. {
  139. if (HarmonyInstance.DEBUG)
  140. {
  141. // fake log a LEAVE code since BeginCatchBlock() does add it
  142. LogIL(il, OpCodes.Leave, new LeaveTry());
  143. FileLog.ChangeIndent(-1);
  144. FileLog.LogBuffered("} // end handler");
  145. }
  146. il.EndExceptionBlock();
  147. }
  148. }
  149. // MethodCopier calls when Operand type is InlineNone
  150. public static void Emit(ILGenerator il, OpCode opcode)
  151. {
  152. if (HarmonyInstance.DEBUG) FileLog.LogBuffered(CodePos(il) + opcode);
  153. il.Emit(opcode);
  154. }
  155. // MethodCopier calls by 3rd argument type
  156. public static void Emit(ILGenerator il, OpCode opcode, LocalBuilder local)
  157. {
  158. LogIL(il, opcode, local);
  159. il.Emit(opcode, local);
  160. }
  161. // MethodCopier calls by 3rd argument type
  162. public static void Emit(ILGenerator il, OpCode opcode, FieldInfo field)
  163. {
  164. LogIL(il, opcode, field);
  165. il.Emit(opcode, field);
  166. }
  167. // MethodCopier calls by 3rd argument type
  168. public static void Emit(ILGenerator il, OpCode opcode, Label[] labels)
  169. {
  170. LogIL(il, opcode, labels);
  171. il.Emit(opcode, labels);
  172. }
  173. // MethodCopier calls by 3rd argument type
  174. public static void Emit(ILGenerator il, OpCode opcode, Label label)
  175. {
  176. LogIL(il, opcode, label);
  177. il.Emit(opcode, label);
  178. }
  179. // MethodCopier calls by 3rd argument type
  180. public static void Emit(ILGenerator il, OpCode opcode, string str)
  181. {
  182. LogIL(il, opcode, str);
  183. il.Emit(opcode, str);
  184. }
  185. // MethodCopier calls by 3rd argument type
  186. public static void Emit(ILGenerator il, OpCode opcode, float arg)
  187. {
  188. LogIL(il, opcode, arg);
  189. il.Emit(opcode, arg);
  190. }
  191. // MethodCopier calls by 3rd argument type
  192. public static void Emit(ILGenerator il, OpCode opcode, byte arg)
  193. {
  194. LogIL(il, opcode, arg);
  195. il.Emit(opcode, arg);
  196. }
  197. // MethodCopier calls by 3rd argument type
  198. public static void Emit(ILGenerator il, OpCode opcode, sbyte arg)
  199. {
  200. LogIL(il, opcode, arg);
  201. il.Emit(opcode, arg);
  202. }
  203. // MethodCopier calls by 3rd argument type
  204. public static void Emit(ILGenerator il, OpCode opcode, double arg)
  205. {
  206. LogIL(il, opcode, arg);
  207. il.Emit(opcode, arg);
  208. }
  209. // MethodCopier calls by 3rd argument type
  210. public static void Emit(ILGenerator il, OpCode opcode, int arg)
  211. {
  212. LogIL(il, opcode, arg);
  213. il.Emit(opcode, arg);
  214. }
  215. // MethodCopier calls by 3rd argument type
  216. public static void Emit(ILGenerator il, OpCode opcode, MethodInfo meth)
  217. {
  218. LogIL(il, opcode, meth);
  219. il.Emit(opcode, meth);
  220. }
  221. // MethodCopier calls by 3rd argument type
  222. public static void Emit(ILGenerator il, OpCode opcode, short arg)
  223. {
  224. LogIL(il, opcode, arg);
  225. il.Emit(opcode, arg);
  226. }
  227. // MethodCopier calls by 3rd argument type
  228. public static void Emit(ILGenerator il, OpCode opcode, SignatureHelper signature)
  229. {
  230. LogIL(il, opcode, signature);
  231. il.Emit(opcode, signature);
  232. }
  233. // MethodCopier calls by 3rd argument type
  234. public static void Emit(ILGenerator il, OpCode opcode, ConstructorInfo con)
  235. {
  236. LogIL(il, opcode, con);
  237. il.Emit(opcode, con);
  238. }
  239. // MethodCopier calls by 3rd argument type
  240. public static void Emit(ILGenerator il, OpCode opcode, Type cls)
  241. {
  242. LogIL(il, opcode, cls);
  243. il.Emit(opcode, cls);
  244. }
  245. // MethodCopier calls by 3rd argument type
  246. public static void Emit(ILGenerator il, OpCode opcode, long arg)
  247. {
  248. LogIL(il, opcode, arg);
  249. il.Emit(opcode, arg);
  250. }
  251. // called from MethodInvoker (calls from MethodCopier use the corresponding Emit() call above)
  252. public static void EmitCall(ILGenerator il, OpCode opcode, MethodInfo methodInfo, Type[] optionalParameterTypes)
  253. {
  254. if (HarmonyInstance.DEBUG) FileLog.LogBuffered(string.Format("{0}Call {1} {2} {3}", CodePos(il), opcode, methodInfo, optionalParameterTypes));
  255. il.EmitCall(opcode, methodInfo, optionalParameterTypes);
  256. }
  257. // not called yet
  258. public static void EmitCalli(ILGenerator il, OpCode opcode, CallingConvention unmanagedCallConv, Type returnType, Type[] parameterTypes)
  259. {
  260. if (HarmonyInstance.DEBUG) FileLog.LogBuffered(string.Format("{0}Calli {1} {2} {3} {4}", CodePos(il), opcode, unmanagedCallConv, returnType, parameterTypes));
  261. il.EmitCalli(opcode, unmanagedCallConv, returnType, parameterTypes);
  262. }
  263. // not called yet
  264. public static void EmitCalli(ILGenerator il, OpCode opcode, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Type[] optionalParameterTypes)
  265. {
  266. if (HarmonyInstance.DEBUG) FileLog.LogBuffered(string.Format("{0}Calli {1} {2} {3} {4} {5}", CodePos(il), opcode, callingConvention, returnType, parameterTypes, optionalParameterTypes));
  267. il.EmitCalli(opcode, callingConvention, returnType, parameterTypes, optionalParameterTypes);
  268. }
  269. }
  270. }