private int FindPositionOfILCodeByOffset(int offset, out string line, out NuGenBaseILCode ilCode) { int result = 0; int codeLineIndex = 0; int lineIndex = 0; bool found = false; line = string.Empty; ilCode = null; while (!found && codeLineIndex < CodeObject.CodeLines.Count) { NuGenCodeLine codeLine = CodeObject.CodeLines[codeLineIndex]; ilCode = codeLine as NuGenBaseILCode; if (ilCode != null && ilCode.Offset >= offset) { line = Lines[lineIndex]; found = true; } else { for (int lineNumber = 0; lineNumber < codeLine.TextLineNumber + 1; lineNumber++) { result += Lines[lineIndex].Length + 1; lineIndex++; } } codeLineIndex++; } return(result); }
private NuGenBaseILCode FindILCodeByIndex(int position) { NuGenBaseILCode result = null; int lineIndex = 0; int codeLineIndex = 0; bool found = false; while (!found && codeLineIndex < CodeObject.CodeLines.Count) { NuGenCodeLine codeLine = CodeObject.CodeLines[codeLineIndex]; position -= Lines[lineIndex].Length + 1; for (int lineNumber = 0; lineNumber < codeLine.TextLineNumber; lineNumber++) { lineIndex++; position -= Lines[lineIndex].Length + 1; } if (position < 0) { result = codeLine as NuGenBaseILCode; found = true; } lineIndex++; codeLineIndex++; } return(result); }
private void RefreshCurrentLine(bool isFormattingCleared, bool keepScrollPosition) { int position = 0; string line = string.Empty; NuGenBaseILCode ilCode = null; bool isScrollingNeeded = false; int selectionStart = SelectionStart; int selectionLength = SelectionLength; int firstVisibleCharacter = -1; int lastVisibleCharacter = -1; Point scrollPosition = new Point(); if (CurrentLine != null) { position = FindPositionOfILCodeByOffset(CurrentLine.InstructionOffset, out line, out ilCode); firstVisibleCharacter = GetCharIndexFromPosition(new Point(0, 0)); lastVisibleCharacter = GetCharIndexFromPosition(new Point(ClientSize.Width, ClientSize.Height)); isScrollingNeeded = (position <firstVisibleCharacter || position> lastVisibleCharacter); } if (!isScrollingNeeded && keepScrollPosition) { scrollPosition = GetScrollPosition(); } if (!isFormattingCleared && PreviousCurrentLine != null && CodeObject != null) { string previousLine; NuGenBaseILCode previousILCode; int previousPosition = FindPositionOfILCodeByOffset(PreviousCurrentLine.InstructionOffset, out previousLine, out previousILCode); int indentation = (previousILCode == null ? 0 : previousILCode.Indentation); SetLineColor(previousPosition, previousLine, indentation, DefaultLine); } if (CurrentLine != null && CodeObject != null) { int indentation = (ilCode == null ? 0 : ilCode.Indentation); SetLineColor(position, line, indentation, CurrentLine); } if (keepScrollPosition) { if (isScrollingNeeded) { SelectionLength = 0; ScrollToCaret(); } else { SelectionLength = selectionLength; SelectionStart = selectionStart; SetScrollPosition(scrollPosition); } } }
public void SetRunToCursorAtSelection() { NuGenMethodDefinition methodDefinition = CodeObject as NuGenMethodDefinition; if (methodDefinition != null) { NuGenBaseILCode ilCode = FindILCodeByIndex(SelectionStart); if (ilCode != null) { NuGenBreakpointHandler.Instance.RunToCursor(methodDefinition, ilCode.Offset, false); } } }
protected override void WndProc(ref Message m) { if (!IsRedrawDisabled || (m.Msg != 0x000F && m.Msg != 0x0014 && m.Msg != 0x0085)) { base.WndProc(ref m); if (!DesignMode && m.Msg == 0x000F && CodeObject != null) { Point firstCharPosition = GetPositionFromCharIndex(0); if (firstCharPosition.X >= 0) { using (Graphics graphics = CreateGraphics()) { Font regularFont = Font; Font boldFont = new Font(Font, FontStyle.Bold); Font boldItalicFont = new Font(Font, FontStyle.Bold | FontStyle.Italic); int charIndex = 1; int firstVisibleCharacter = GetCharIndexFromPosition(new Point(0, 0)); int lastVisibleCharacter = GetCharIndexFromPosition(new Point(ClientSize.Width, ClientSize.Height)); if (lastVisibleCharacter == 0) { lastVisibleCharacter = Text.Length; } for (int ilCodeIndex = 0; ilCodeIndex < CodeObject.CodeLines.Count && charIndex < lastVisibleCharacter; ilCodeIndex++) { NuGenCodeLine codeLine = CodeObject.CodeLines[ilCodeIndex]; if (charIndex >= firstVisibleCharacter && charIndex <= lastVisibleCharacter && codeLine is NuGenBaseILCode) { Point position = GetPositionFromCharIndex(charIndex); NuGenBaseILCode ilCode = (NuGenBaseILCode)codeLine; Brush brush = Brushes.Black; Font currentFont = regularFont; graphics.DrawString(ilCode.Address, currentFont, brush, 0 + firstCharPosition.X - SelectionIndent, position.Y); } charIndex += codeLine.Text.Length + 1 + codeLine.Indentation * IndentationSize; } } } } } }
private void setIPToolStripMenuItem_Click(object sender, EventArgs e) { NuGenBaseILCode currentILCode = setIPToolStripMenuItem.Tag as NuGenBaseILCode; if (currentILCode != null) { try { NuGenDebugEventHandler.Instance.EventObjects.Frame.SetIP(Convert.ToUInt32(currentILCode.Offset)); NuGenDebugEventHandler.Instance.DisplayAllInformation(); } catch (Exception exception) { NuGenUIHandler.Instance.ShowException(exception); NuGenUIHandler.Instance.DisplayUserWarning(exception.Message); } } }
public void SetBreakpointAtSelection() { NuGenMethodDefinition methodDefinition = CodeObject as NuGenMethodDefinition; if (methodDefinition != null) { NuGenBaseILCode ilCode = FindILCodeByIndex(SelectionStart); if (ilCode != null) { NuGenFunctionBreakpointInformation breakpointInformation = NuGenBreakpointHandler.Instance.AddRemoveBreakpoint(methodDefinition, ilCode.Offset, false); if (breakpointInformation != null) { UpdateBreakpoint(breakpointInformation); NuGenProject.Instance.IsSaved = false; } } } }
private void contextMenu_Opening(object sender, CancelEventArgs e) { setIPToolStripMenuItem.Visible = false; if (NuGenDebugEventHandler.Instance.EventObjects.Frame != null && NuGenDebugEventHandler.Instance.EventObjects.Frame.IsActiveFrame && CurrentLine != null) { NuGenMethodDefinition displayedMethod = CodeObject as NuGenMethodDefinition; if (displayedMethod != null) { FunctionWrapper currentFunction = NuGenDebugEventHandler.Instance.EventObjects.Frame.GetFunction(); uint currentFunctionToken = currentFunction.GetToken(); if (displayedMethod.Token == currentFunctionToken) { ModuleWrapper currentModule = currentFunction.GetModule(); bool isInMemoryCurrentModule = currentModule.IsInMemory(); string currentModuleName = string.Empty; if (isInMemoryCurrentModule) { currentModuleName = currentModule.GetNameFromMetaData(); } else { currentModuleName = currentModule.GetName(); try { currentModuleName = Path.GetFileNameWithoutExtension(currentModuleName); } catch { } } currentModuleName = currentModuleName.ToLower(); if ((isInMemoryCurrentModule && displayedMethod.BaseTypeDefinition.ModuleScope.Name.ToLower() == currentModuleName) || (!isInMemoryCurrentModule && Path.GetFileNameWithoutExtension(displayedMethod.BaseTypeDefinition.ModuleScope.Assembly.FullPath).ToLower() == currentModuleName)) { NuGenBaseILCode currentILCode = ilEditor.GetILCodeAtMouseCursor(); setIPToolStripMenuItem.Visible = true; if (currentILCode != null) { int hResult = NuGenDebugEventHandler.Instance.EventObjects.Frame.CanSetIP(Convert.ToUInt32(currentILCode.Offset)); if (hResult == 0) { setIPToolStripMenuItem.Enabled = true; setIPToolStripMenuItem.Tag = currentILCode; } else { COMException comException = Marshal.GetExceptionForHR(hResult) as COMException; if (comException != null) { NuGenUIHandler.Instance.DisplayUserWarning(Marshal.GetExceptionForHR(hResult).Message); } setIPToolStripMenuItem.Enabled = false; } } else { setIPToolStripMenuItem.Enabled = false; } } } } } }