Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows.Forms;
- using Mono.Cecil;
- using Mono.Cecil.Rocks;
- using Mono.Cecil.Cil;
- using Mono.Cecil.Pdb;
- using Mono.Cecil.Mdb;
- namespace uReflector.Reflector.Tools
- {
- public static class AsmToStructure
- {
- public static TypeReference GetEnumUnderlyingType(TypeDefinition self)
- {
- var fields = self.Fields;
- for (int i = 0; i < fields.Count; i++)
- {
- var field = fields[i];
- if (field.Name == "value__")
- return field.FieldType;
- }
- throw new ArgumentException();
- }
- public static FieldDefinition[] GetEnumFields(TypeDefinition enumDef)
- {
- List<FieldDefinition> fieldsOut = new List<FieldDefinition>();
- var fields = enumDef.Fields;
- for (int i = 0; i < fields.Count; i++)
- if(!(fields[i].Name == "value__"))
- fieldsOut.Add(fields[i]);
- return fieldsOut.ToArray();
- }
- public static TreeNode ConvertToTreeNode(string asmPath)
- {
- dynamic readerParameters = new ReaderParameters { ReadSymbols = true };
- AssemblyDefinition asmDef = AssemblyDefinition.ReadAssembly(asmPath, readerParameters);
- TreeNode mainNode = new TreeNode(asmDef.Name.Name);
- SortedList<string, TreeNode> namespaces = new SortedList<string, TreeNode>(),
- modules = new SortedList<string, TreeNode>(),
- types = new SortedList<string, TreeNode>();
- foreach (ModuleDefinition modDef in asmDef.Modules)
- {
- modules.Add(modDef.Name, new TreeNode(modDef.Name));
- foreach (TypeDefinition tDef in modDef.GetTypes())
- if(!namespaces.Keys.Contains(tDef.Namespace))
- namespaces.Add(tDef.Namespace, new TreeNode((tDef.Namespace == "" ? "-" : tDef.Namespace)));
- }
- foreach (ModuleDefinition modDef in asmDef.Modules)
- {
- foreach (TypeDefinition tDef in modDef.GetAllTypes())
- {
- for (int i = 0; i < namespaces.Keys.Count; i++)
- {
- if (namespaces.Values[namespaces.IndexOfKey(tDef.Namespace)].Text == (tDef.Namespace == "" ? "-" : tDef.Namespace))
- {
- List<MethodDefinition> exceptionDefs = new List<MethodDefinition>();
- TreeNode tmpTypeNode = new TreeNode(tDef.Name);
- //foreach (TypeDefinition _tDef in tDef.NestedTypes)
- //{
- // TreeNode tmpEnumNode = new TreeNode(string.Concat(_tDef.Name, "(", _tDef.BaseType, ") : ", _tDef.GetEnumUnderlyingType().Name));
- // foreach (FieldDefinition fDef in GetEnumFields(_tDef))
- // tmpEnumNode.Nodes.Add(new TreeNode(fDef.Name));
- // tmpTypeNode.Nodes.Add(tmpEnumNode);
- //}
- foreach (PropertyDefinition pDef in tDef.Properties)
- {
- TreeNode tmpPropNode = new TreeNode(string.Concat(pDef.Name, " : ", pDef.PropertyType.Name));
- if(pDef.GetMethod != null){
- tmpPropNode.Nodes.Add(pDef.GetMethod.Name);
- exceptionDefs.Add(pDef.GetMethod);
- } if (pDef.SetMethod != null){
- tmpPropNode.Nodes.Add(pDef.SetMethod.Name);
- exceptionDefs.Add(pDef.SetMethod);
- }
- tmpTypeNode.Nodes.Add(tmpPropNode);
- }
- foreach(FieldDefinition fDef in tDef.Fields)
- tmpTypeNode.Nodes.Add(string.Concat(fDef.Name, " : ", fDef.FieldType.Name));
- foreach (MethodDefinition mDef in tDef.Methods)
- {
- if (!exceptionDefs.Contains(mDef))
- {
- StringBuilder tmpStr = new StringBuilder();
- StringBuilder _tmpStr = new StringBuilder();
- StringBuilder __tmpStr = new StringBuilder();
- _tmpStr.Append(" Method Information:" + Environment.NewLine);
- _tmpStr.Append(string.Concat('\t', "Max Stack Size: ", mDef.Body.MaxStackSize.ToString(), Environment.NewLine));
- _tmpStr.Append(string.Concat('\t', "Method Size: ", mDef.Body.CodeSize.ToString("x4"), Environment.NewLine));
- _tmpStr.Append(string.Concat('\t', "Instruction Count: ", mDef.Body.Instructions.Count, Environment.NewLine));
- _tmpStr.Append(Environment.NewLine);
- if (mDef.Body.HasVariables)
- {
- _tmpStr.Append(" Local method variables: " + Environment.NewLine);
- foreach (VariableDefinition vDef in mDef.Body.Variables)
- _tmpStr.Append(string.Concat('\t', vDef.Name + " (", vDef.VariableType.Name, ")", Environment.NewLine));
- }
- foreach (ParameterDefinition paramDef in mDef.Parameters)
- __tmpStr.Append(string.Concat(paramDef.ParameterType.Name, " ", paramDef.Name, ", "));
- tmpStr.Append(string.Concat("Method: ", mDef.Attributes.ToString(), " ", mDef.ReturnType.Name, " ", mDef.Name, "(", __tmpStr.ToString().TrimEnd(", ".ToCharArray()), ")", Environment.NewLine, "{", Environment.NewLine + Environment.NewLine));
- tmpStr.Append(_tmpStr + (_tmpStr.ToString() == "" ? null : Environment.NewLine));
- TreeNode tmpMethodNode = new TreeNode(string.Concat(mDef.Name, "(", __tmpStr.ToString().TrimEnd(", ".ToCharArray()), ")"));
- if (mDef == asmDef.EntryPoint)
- tmpMethodNode.Text += " [Entry Point]";
- foreach (Instruction instr in mDef.Body.Instructions)
- try
- {
- 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));
- }
- catch { /* Happens for some instructions */ }
- tmpMethodNode.Tag = string.Concat(tmpStr.ToString(), Environment.NewLine + Environment.NewLine, "}");
- tmpTypeNode.Nodes.Add(tmpMethodNode);
- }
- }
- namespaces.Values[namespaces.IndexOfKey(tDef.Namespace)].Nodes.Add(tmpTypeNode);
- break;
- }
- }
- }
- }
- foreach(TreeNode tn in namespaces.Values)
- mainNode.Nodes.Add(tn);
- return mainNode;
- }
- }
- }
Add Comment
Please, Sign In to add comment