private void ReadMethods() { if (FullName.Contains("WithTemplate")) { Console.WriteLine(); } var methodBuilders = new List <StringBuilder>(); // Now start parsing the raw IL, line by line. using (var reader = new StringReader(RawIL)) { // Read each line string line; StringBuilder currentBuilder = null; string currentName = null; while ((line = reader.ReadLine()) != null) { // Is it a class start token? If so, we can start a new builder. if (ILParseHelper.IsLineMethodDeclaration(line)) { currentBuilder = new StringBuilder(); } else if (currentBuilder != null && currentName == null) { // We must now be on the name line. currentName = ILParseHelper.GetMethodNameFromDeclarationLine(line); } // If don't have a current class, skip. if (currentBuilder == null) { continue; } // Add the line to the class builder. currentBuilder.AppendLine(line); // Is it a class end token? If so, clear the current class identifier. if (ILParseHelper.IsLineMethodEndDeclaration(line, ShortName, currentName)) { methodBuilders.Add(currentBuilder); currentName = null; currentBuilder = null; } } } methodBuilders.ForEach(mb => { var method = new DisassembledMethod(); method.Parent = this; method.RawIL = mb.ToString(); method.InitialiseFromIL(); method.FullName = FullName + "." + method.ShortName; methods.Add(method); }); }
private void ReadEvents() { var eventBuilders = new List <StringBuilder>(); // Now start parsing the raw IL, line by line. using (var reader = new StringReader(RawIL)) { // Read each line string line; StringBuilder currentBuilder = null; string currentName = null; while ((line = reader.ReadLine()) != null) { // Is it a class start token? If so, we can start a new builder. if (ILParseHelper.IsLineEventPropertyDeclaration(line)) { currentBuilder = new StringBuilder(); currentName = ILParseHelper.GetEventNameFromDeclarationLine(line); } // If don't have a current class, skip. if (currentBuilder == null) { continue; } // Add the line to the class builder. currentBuilder.AppendLine(line); // Is it a class end token? If so, clear the current class identifier. if (ILParseHelper.IsLineEventEndDeclaration(line, ShortName, currentName)) { eventBuilders.Add(currentBuilder); currentName = null; currentBuilder = null; } } } eventBuilders.ForEach(mb => { var method = new DisassembledEvent(); method.Parent = this; method.RawIL = mb.ToString(); method.InitialiseFromIL(); method.FullName = FullName + "." + method.ShortName; events.Add(method); }); }
public override void InitialiseFromIL() { // Read the first two lines. string firstLine = null; string secondLine = null; using (var reader = new StringReader(RawIL)) { firstLine = reader.ReadLine(); if (firstLine != null) { secondLine = reader.ReadLine(); } } // From the class declaration, read the full name and tokens. try { string className, templateSpecification; List <string> classModifiers; ILParseHelper.GetClassDeclarationParts(firstLine, out classModifiers, out className, out templateSpecification); FullName = className; TemplateSpecification = templateSpecification; ShortName = className.Split('.').Last(); modifiers.Clear(); modifiers.AddRange(classModifiers); } catch (Exception) { // todo } if (secondLine != null) { string baseType; if (ILParseHelper.ReadExtendsLine(secondLine, out baseType)) { BaseType = baseType; } } // Read the fields and methods. ReadFields(); ReadMethods(); ReadProperties(); ReadEvents(); }
public override void InitialiseFromIL() { string line; using (var reader = new StringReader(RawIL)) { while ((line = reader.ReadLine()) != null) { var methodName = ILParseHelper.GetMethodNameFromDeclarationLine(line); if (methodName != null) { ShortName = methodName; FullName = methodName; break; } } } }
private Dictionary <string, string> CreateMapOfClassNamesToContents(IdentifyClassType identifyClassType) { // Create a dictionary of file paths to their contents. var pathsToContents = new Dictionary <string, StringBuilder>(); // Now start parsing the raw IL, line by line. using (var reader = new StringReader(rawIl)) { // Read each line string line; string currentClassName = null; while ((line = reader.ReadLine()) != null) { // Is it a class start token? If so, get the class name. string templateSpecification; if (identifyClassType(line)) { ILParseHelper.GetClassNameFromClassDeclarationLine(line, out currentClassName, out templateSpecification); } // If don't have a current class, skip. if (currentClassName == null) { continue; } // Add the line to the class. if (!pathsToContents.ContainsKey(currentClassName)) { pathsToContents[currentClassName] = new StringBuilder(); } pathsToContents[currentClassName].AppendLine(line); // Is it a class end token? If so, clear the current class identifier. if (ILParseHelper.IsLineClassEndDeclaration(line, currentClassName)) { currentClassName = null; } } } // Return the dictionary. return(pathsToContents.ToDictionary(d => d.Key, d => d.Value.ToString())); }
private void ReadFields() { fields.Clear(); // Read all lines. using (var reader = new StringReader(RawIL)) { string line; while ((line = reader.ReadLine()) != null) { if (ILParseHelper.IsLineFieldDeclaration(line)) { var field = new DisassembledField(); field.Parent = this; field.RawIL = line; field.InitialiseFromIL(); field.FullName = FullName + "." + field.ShortName; fields.Add(field); } } } }
private DisassembledIlClass CreateIlClassFromRawIlClass(RawILClass rawIlClass) { // Get the raw IL. var rawIL = rawIlClass.ILBuilder.ToString(); // Get the first and second line. string firstLine = null; string secondLine = null; using (var reader = new StringReader(rawIL)) { firstLine = reader.ReadLine(); if (firstLine != null) { secondLine = reader.ReadLine(); } } DisassembledIlClass ilClass = new DisassembledClass(); // We can immediately identify structures and interfaces. if (ILParseHelper.IsLineStructDeclaration(firstLine)) { ilClass = new DisassembledStructure(); } else if (ILParseHelper.IsLineInterfaceDeclaration(firstLine)) { ilClass = new DisassembledInterface(); } else { // Now check the second line. if (secondLine == null) { ilClass = new DisassembledClass(); } else { string baseType; if (!ILParseHelper.ReadExtendsLine(secondLine, out baseType)) { ilClass = new DisassembledClass(); } if (baseType == @"[mscorlib]System.Enum") { ilClass = new DisassembledEnumeration(); } else if (baseType == @"[mscorlib]System.MulticastDelegate") { ilClass = new DisassembledDelegate(); } } } // Set the IL. ilClass.RawIL = rawIL; ilClass.InitialiseFromIL(); // Add any children. foreach (var rawChild in rawIlClass.Children) { // Create the entity type from the entity IL. var childIlClass = CreateIlClassFromRawIlClass(rawChild); ilClass.AddChild(childIlClass); } return(ilClass); }
private List <RawILClass> ParseDisassembledIlClasses() { // We'll return a list of raw IL classes. var rootRawILClasses = new List <RawILClass>(); // Now start parsing the raw IL, line by line. using (var reader = new StringReader(RawIL)) { // IL classes can be nested, so when parsing them we must maintain // a stack (so we can nest too). var rawIlClassStack = new Stack <RawILClass>(); // Read each line string line; while ((line = reader.ReadLine()) != null) { if (line.Contains("<GetVisualChildren>")) { Console.WriteLine(); } // Is it a class start token? If so, we can start a new builder. if (ILParseHelper.IsLineAnyLevelIlClassDeclaration(line)) { string fullName, templateSpecification; ILParseHelper.GetClassNameFromClassDeclarationLine(line, out fullName, out templateSpecification); // Create a new raw IL class, parse its name, add it to the list if it's top level, add it to the stack. var rawIlClass = new RawILClass { FullName = fullName, TemplateSpecification = templateSpecification }; if (!rawIlClassStack.Any()) { rootRawILClasses.Add(rawIlClass); } else { rawIlClassStack.Peek().Children.Add(rawIlClass); } rawIlClassStack.Push(rawIlClass); } // If don't have a current class, skip. if (!rawIlClassStack.Any()) { continue; } // Add the line to the class builder. rawIlClassStack.Peek().ILBuilder.AppendLine(line); // Is it a class end token? If so, we've finished the class. if (ILParseHelper.IsLineClassEndDeclaration(line, rawIlClassStack.Peek().FullName)) { // Pop the stack. rawIlClassStack.Pop(); } } } // Return the set of class builders. return(rootRawILClasses); }