CodeInstruction.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using Harmony.ILCopying;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection.Emit;
  5. namespace Harmony
  6. {
  7. public class CodeInstruction
  8. {
  9. public OpCode opcode;
  10. public object operand;
  11. public List<Label> labels = new List<Label>();
  12. public List<ExceptionBlock> blocks = new List<ExceptionBlock>();
  13. public CodeInstruction(OpCode opcode, object operand = null)
  14. {
  15. this.opcode = opcode;
  16. this.operand = operand;
  17. }
  18. public CodeInstruction(CodeInstruction instruction)
  19. {
  20. opcode = instruction.opcode;
  21. operand = instruction.operand;
  22. labels = instruction.labels.ToArray().ToList();
  23. }
  24. public CodeInstruction Clone()
  25. {
  26. return new CodeInstruction(this) { labels = new List<Label>() };
  27. }
  28. public CodeInstruction Clone(OpCode opcode)
  29. {
  30. var instruction = new CodeInstruction(this) { labels = new List<Label>() };
  31. instruction.opcode = opcode;
  32. return instruction;
  33. }
  34. public CodeInstruction Clone(OpCode opcode, object operand)
  35. {
  36. var instruction = new CodeInstruction(this) { labels = new List<Label>() };
  37. instruction.opcode = opcode;
  38. instruction.operand = operand;
  39. return instruction;
  40. }
  41. public override string ToString()
  42. {
  43. var list = new List<string>();
  44. foreach (var label in labels)
  45. list.Add("Label" + label.GetHashCode());
  46. foreach (var block in blocks)
  47. list.Add("EX_" + block.blockType.ToString().Replace("Block", ""));
  48. var extras = list.Count > 0 ? " [" + string.Join(", ", list.ToArray()) + "]" : "";
  49. var operandStr = Emitter.FormatArgument(operand);
  50. if (operandStr != "") operandStr = " " + operandStr;
  51. return opcode + operandStr + extras;
  52. }
  53. }
  54. }