Guest User

Untitled

a guest
Oct 23rd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using Mono.Cecil;
  6. using Mono.Cecil.Rocks;
  7. using Mono.Cecil.Cil;
  8. using Mono.Cecil.Pdb;
  9. using Mono.Cecil.Mdb;
  10.  
  11. namespace uReflector.Reflector.Tools
  12. {
  13. public static class AsmToStructure
  14. {
  15. public static TypeReference GetEnumUnderlyingType(TypeDefinition self)
  16. {
  17. var fields = self.Fields;
  18.  
  19. for (int i = 0; i < fields.Count; i++)
  20. {
  21. var field = fields[i];
  22. if (field.Name == "value__")
  23. return field.FieldType;
  24. }
  25.  
  26. throw new ArgumentException();
  27. }
  28.  
  29. public static FieldDefinition[] GetEnumFields(TypeDefinition enumDef)
  30. {
  31. List<FieldDefinition> fieldsOut = new List<FieldDefinition>();
  32.  
  33. var fields = enumDef.Fields;
  34.  
  35. for (int i = 0; i < fields.Count; i++)
  36. if(!(fields[i].Name == "value__"))
  37. fieldsOut.Add(fields[i]);
  38.  
  39. return fieldsOut.ToArray();
  40. }
  41.  
  42. public static TreeNode ConvertToTreeNode(string asmPath)
  43. {
  44.  
  45. dynamic readerParameters = new ReaderParameters { ReadSymbols = true };
  46. AssemblyDefinition asmDef = AssemblyDefinition.ReadAssembly(asmPath, readerParameters);
  47.  
  48. TreeNode mainNode = new TreeNode(asmDef.Name.Name);
  49.  
  50. SortedList<string, TreeNode> namespaces = new SortedList<string, TreeNode>(),
  51. modules = new SortedList<string, TreeNode>(),
  52. types = new SortedList<string, TreeNode>();
  53.  
  54. foreach (ModuleDefinition modDef in asmDef.Modules)
  55. {
  56. modules.Add(modDef.Name, new TreeNode(modDef.Name));
  57.  
  58. foreach (TypeDefinition tDef in modDef.GetTypes())
  59. if(!namespaces.Keys.Contains(tDef.Namespace))
  60. namespaces.Add(tDef.Namespace, new TreeNode((tDef.Namespace == "" ? "-" : tDef.Namespace)));
  61. }
  62.  
  63. foreach (ModuleDefinition modDef in asmDef.Modules)
  64. {
  65.  
  66.  
  67. foreach (TypeDefinition tDef in modDef.GetAllTypes())
  68. {
  69. for (int i = 0; i < namespaces.Keys.Count; i++)
  70. {
  71. if (namespaces.Values[namespaces.IndexOfKey(tDef.Namespace)].Text == (tDef.Namespace == "" ? "-" : tDef.Namespace))
  72. {
  73.  
  74. List<MethodDefinition> exceptionDefs = new List<MethodDefinition>();
  75.  
  76. TreeNode tmpTypeNode = new TreeNode(tDef.Name);
  77.  
  78. //foreach (TypeDefinition _tDef in tDef.NestedTypes)
  79. //{
  80. // TreeNode tmpEnumNode = new TreeNode(string.Concat(_tDef.Name, "(", _tDef.BaseType, ") : ", _tDef.GetEnumUnderlyingType().Name));
  81.  
  82. // foreach (FieldDefinition fDef in GetEnumFields(_tDef))
  83. // tmpEnumNode.Nodes.Add(new TreeNode(fDef.Name));
  84.  
  85. // tmpTypeNode.Nodes.Add(tmpEnumNode);
  86.  
  87. //}
  88.  
  89.  
  90. foreach (PropertyDefinition pDef in tDef.Properties)
  91. {
  92. TreeNode tmpPropNode = new TreeNode(string.Concat(pDef.Name, " : ", pDef.PropertyType.Name));
  93.  
  94. if(pDef.GetMethod != null){
  95. tmpPropNode.Nodes.Add(pDef.GetMethod.Name);
  96. exceptionDefs.Add(pDef.GetMethod);
  97. } if (pDef.SetMethod != null){
  98. tmpPropNode.Nodes.Add(pDef.SetMethod.Name);
  99. exceptionDefs.Add(pDef.SetMethod);
  100. }
  101.  
  102. tmpTypeNode.Nodes.Add(tmpPropNode);
  103. }
  104.  
  105. foreach(FieldDefinition fDef in tDef.Fields)
  106. tmpTypeNode.Nodes.Add(string.Concat(fDef.Name, " : ", fDef.FieldType.Name));
  107.  
  108. foreach (MethodDefinition mDef in tDef.Methods)
  109. {
  110. if (!exceptionDefs.Contains(mDef))
  111. {
  112.  
  113. StringBuilder tmpStr = new StringBuilder();
  114. StringBuilder _tmpStr = new StringBuilder();
  115. StringBuilder __tmpStr = new StringBuilder();
  116.  
  117. _tmpStr.Append(" Method Information:" + Environment.NewLine);
  118. _tmpStr.Append(string.Concat('\t', "Max Stack Size: ", mDef.Body.MaxStackSize.ToString(), Environment.NewLine));
  119. _tmpStr.Append(string.Concat('\t', "Method Size: ", mDef.Body.CodeSize.ToString("x4"), Environment.NewLine));
  120. _tmpStr.Append(string.Concat('\t', "Instruction Count: ", mDef.Body.Instructions.Count, Environment.NewLine));
  121. _tmpStr.Append(Environment.NewLine);
  122.  
  123. if (mDef.Body.HasVariables)
  124. {
  125. _tmpStr.Append(" Local method variables: " + Environment.NewLine);
  126.  
  127. foreach (VariableDefinition vDef in mDef.Body.Variables)
  128. _tmpStr.Append(string.Concat('\t', vDef.Name + " (", vDef.VariableType.Name, ")", Environment.NewLine));
  129. }
  130.  
  131. foreach (ParameterDefinition paramDef in mDef.Parameters)
  132. __tmpStr.Append(string.Concat(paramDef.ParameterType.Name, " ", paramDef.Name, ", "));
  133.  
  134.  
  135. tmpStr.Append(string.Concat("Method: ", mDef.Attributes.ToString(), " ", mDef.ReturnType.Name, " ", mDef.Name, "(", __tmpStr.ToString().TrimEnd(", ".ToCharArray()), ")", Environment.NewLine, "{", Environment.NewLine + Environment.NewLine));
  136. tmpStr.Append(_tmpStr + (_tmpStr.ToString() == "" ? null : Environment.NewLine));
  137.  
  138. TreeNode tmpMethodNode = new TreeNode(string.Concat(mDef.Name, "(", __tmpStr.ToString().TrimEnd(", ".ToCharArray()), ")"));
  139.  
  140. if (mDef == asmDef.EntryPoint)
  141. tmpMethodNode.Text += " [Entry Point]";
  142.  
  143. foreach (Instruction instr in mDef.Body.Instructions)
  144. try
  145. {
  146. tmpStr.Append(string.Concat("\tL_", instr.Offset.ToString("x4"), '\t', instr.OpCode.ToString(), '\t', (instr.OpCode == OpCodes.Ldstr ? @"""" + instr.Operand.ToString() + @"""" : instr.Operand), Environment.NewLine));
  147. }
  148. catch { /* Happens for some instructions */ }
  149.  
  150.  
  151. tmpMethodNode.Tag = string.Concat(tmpStr.ToString(), Environment.NewLine + Environment.NewLine, "}");
  152.  
  153. tmpTypeNode.Nodes.Add(tmpMethodNode);
  154. }
  155.  
  156.  
  157. }
  158.  
  159.  
  160.  
  161. namespaces.Values[namespaces.IndexOfKey(tDef.Namespace)].Nodes.Add(tmpTypeNode);
  162.  
  163. break;
  164. }
  165. }
  166. }
  167. }
  168.  
  169. foreach(TreeNode tn in namespaces.Values)
  170. mainNode.Nodes.Add(tn);
  171.  
  172.  
  173. return mainNode;
  174. }
  175. }
  176. }
Add Comment
Please, Sign In to add comment