/// <summary>
        ///     Syncronizes all the data shown on the window with that of the script. 
        /// </summary>
        private void SyncronizeWindow()
        {
            // Clear everything.
            Text = "Script Debugger - " + _scriptThread.Process.Url;
            variablesPropertyListView.Clear();
            callStackListView.Items.Clear();
            stackTreeView.Nodes.Clear();
            heapTreeView.Nodes.Clear();
            objectsTreeView.Nodes.Clear();
            sourceCodeScriptTextBox.Enabled = true;
            disassemblyScriptTextBox.Enabled = true;
            variablesPropertyListView.Enabled = true;
            callStackListView.Enabled = true;
            startToolStripButton.Enabled = true;
            stopToolStripButton.Enabled = false;
            stepToolStripButton.Enabled = true;
            stackTreeView.Enabled = true;
            stackPropertyGrid.Enabled = true;
            heapTreeView.Enabled = true;
            heapPropertyGrid.Enabled = true;
            objectsTreeView.Enabled = true;
            objectsPropertyGrid.Enabled = true;

            // If we are running then show nothing.
            if (_runScript == true)
            {
                startToolStripButton.Enabled = false;
                stopToolStripButton.Enabled = true;
                stepToolStripButton.Enabled = false;
                sourceCodeScriptTextBox.Enabled = false;
                disassemblyScriptTextBox.Enabled = false;
                variablesPropertyListView.Enabled = false;
                callStackListView.Enabled = false;
                stackTreeView.Enabled = false;
                stackPropertyGrid.Enabled = false;
                heapTreeView.Enabled = false;
                heapPropertyGrid.Enabled = false;
                objectsTreeView.Enabled = false;
                objectsPropertyGrid.Enabled = false;

                return;
            }

            // Grab the current instruction.
            RuntimeInstruction currentInstruction = _scriptThread.Process.Instructions[_scriptThread.InstructionPointer];

            // See if we can find the source code for this instruction.
            if (currentInstruction.File != "")
            {
                if (File.Exists(currentInstruction.File))
                {
                    // Load in source code.
                    if (_sourceURL == "" || _sourceURL != currentInstruction.File)
                    {
                        string text = File.ReadAllText(currentInstruction.File);
                        sourceCodeScriptTextBox.Text = text;
                        _sourceURL = currentInstruction.File;

                        // Highlight.
                        sourceCodeScriptTextBox.Highlight(true);
                    }

                    // Remove any background colors.
                    sourceCodeScriptTextBox.RichTextBox.SelectionStart = 0;
                    sourceCodeScriptTextBox.RichTextBox.SelectionLength = sourceCodeScriptTextBox.RichTextBox.Text.Length;
                    sourceCodeScriptTextBox.RichTextBox.SelectionBackColor = Color.White;

                    // Scroll to the correct line.
                    sourceCodeScriptTextBox.Focus();
                    sourceCodeScriptTextBox.RichTextBox.SelectionStart = sourceCodeScriptTextBox.RichTextBox.GetFirstCharIndexFromLine(currentInstruction.Line - 1);// +currentInstruction.Offset;
                    int nextLineIndex = sourceCodeScriptTextBox.RichTextBox.Text.IndexOf('\n', sourceCodeScriptTextBox.RichTextBox.SelectionStart);
                    sourceCodeScriptTextBox.RichTextBox.SelectionLength = nextLineIndex == -1 ? sourceCodeScriptTextBox.Text.Length - sourceCodeScriptTextBox.RichTextBox.SelectionStart : nextLineIndex - sourceCodeScriptTextBox.RichTextBox.SelectionStart;
                    sourceCodeScriptTextBox.RichTextBox.SelectionBackColor = Color.Red;
                    sourceCodeScriptTextBox.RichTextBox.ScrollToCaret();
                    sourceCodeScriptTextBox.RichTextBox.SelectionLength = 0;
                }
                else
                    sourceCodeScriptTextBox.Text = "";
            }

            // Build the disassembly text.
            if (disassemblyScriptTextBox.Text == "")
            {
                StringBuilder builder = new StringBuilder();
                foreach (RuntimeInstruction instruction in _scriptThread.Process.Instructions)
                    builder.Append(instruction.Decompile() + "\n");
                disassemblyScriptTextBox.Text = builder.ToString();
            }

            // Remove any background colors.
            disassemblyScriptTextBox.RichTextBox.SelectionStart = 0;
            disassemblyScriptTextBox.RichTextBox.SelectionLength = disassemblyScriptTextBox.RichTextBox.Text.Length;
            disassemblyScriptTextBox.RichTextBox.SelectionBackColor = Color.Transparent;

            // Scroll to the correct line.
            disassemblyScriptTextBox.Focus();
            disassemblyScriptTextBox.RichTextBox.SelectionStart = disassemblyScriptTextBox.RichTextBox.GetFirstCharIndexFromLine(_scriptThread.InstructionPointer);
            int disassemblyNextLineIndex = disassemblyScriptTextBox.RichTextBox.Text.IndexOf('\n', disassemblyScriptTextBox.RichTextBox.SelectionStart);
            disassemblyScriptTextBox.RichTextBox.SelectionLength = disassemblyNextLineIndex == -1 ? disassemblyScriptTextBox.Text.Length - disassemblyScriptTextBox.RichTextBox.SelectionStart : disassemblyNextLineIndex - disassemblyScriptTextBox.RichTextBox.SelectionStart;
            disassemblyScriptTextBox.RichTextBox.SelectionBackColor = Color.Red;
            disassemblyScriptTextBox.RichTextBox.ScrollToCaret();
            disassemblyScriptTextBox.RichTextBox.SelectionLength = 0;

            // Find the last last scope we were in thats not native :P.
            _lastScope = null;
            object[] callStack = _scriptThread.CallStack.ToArray();
            for (int i = 0; i < callStack.Length; i++)
            {
                callStackListView.Items.Add(new ListViewItem(new string[] { ((FunctionSymbol)callStack[i]).Identifier})); // FIX ENTRY POINT
                if (_lastScope == null && ((FunctionSymbol)callStack[i]).IsImport == false)
                {
                    _lastScope = ((FunctionSymbol)callStack[i]);
                    callStackListView.SelectedIndices.Add(callStackListView.Items.Count - 1);
                    callStackListView.Items[callStackListView.Items.Count - 1].BackColor = Color.Red;
                }
            }

            // If we have a valid scope then starting filling the locals list :P.
            #region Variables
            _localCategory = new PropertyListViewCategory("Locals");
            variablesPropertyListView.AddCategory(_localCategory);
            if (_lastScope != null)
            {
                foreach (Symbol symbol in _lastScope.Symbols)
                {
                    if (symbol.Type == SymbolType.Variable)
                    {
                        VariableSymbol variable = symbol as VariableSymbol;
                        if (variable.IsArray == true)
                        {
                            int arrayIndex = _scriptThread.GetArrayLocal(variable.Identifier); // Can be optimized.
                            if (arrayIndex == -1 || _scriptThread.Process.MemoryHeap[arrayIndex - 1] == null)
                            {
                                _localCategory.AddProperty(new PropertyListViewItem(variable.Identifier, null, null, "", typeof(string)));
                                continue;
                            }

                            int arrayLength = _scriptThread.GetArrayLength(arrayIndex);
                            object value = "";
                            Type valueType = null;

                            switch (variable.DataType.DataType)
                            {
                                case DataType.Bool:
                                    value = new bool[arrayLength];
                                    for (int i = 0; i < arrayLength; i++)
                                        ((bool[])value)[i] = _scriptThread.GetRuntimeValueArrayElement(arrayIndex, i).BooleanLiteral;
                                    valueType = typeof(bool[]);
                                    break;
                                case DataType.Byte:
                                    value = new byte[arrayLength];
                                    for (int i = 0; i < arrayLength; i++)
                                        ((byte[])value)[i] = _scriptThread.GetRuntimeValueArrayElement(arrayIndex, i).ByteLiteral;
                                    valueType = typeof(byte[]);
                                    break;
                                case DataType.Double:
                                    value = new double[arrayLength];
                                    for (int i = 0; i < arrayLength; i++)
                                        ((double[])value)[i] = _scriptThread.GetRuntimeValueArrayElement(arrayIndex, i).DoubleLiteral;
                                    valueType = typeof(double[]);
                                    break;
                                case DataType.Float:
                                    value = new float[arrayLength];
                                    for (int i = 0; i < arrayLength; i++)
                                        ((float[])value)[i] = _scriptThread.GetRuntimeValueArrayElement(arrayIndex, i).FloatLiteral;
                                    valueType = typeof(float[]);
                                    break;
                                case DataType.Int:
                                    value = new int[arrayLength];
                                    for (int i = 0; i < arrayLength; i++)
                                        ((int[])value)[i] = _scriptThread.GetRuntimeValueArrayElement(arrayIndex, i).IntegerLiteral;
                                    valueType = typeof(int[]);
                                    break;
                                case DataType.Long:
                                    value = new long[arrayLength];
                                    for (int i = 0; i < arrayLength; i++)
                                        ((long[])value)[i] = _scriptThread.GetRuntimeValueArrayElement(arrayIndex, i).LongLiteral;
                                    valueType = typeof(long[]);
                                    break;
                                case DataType.Object:
                                    value = new int[arrayLength];
                                    for (int i = 0; i < arrayLength; i++)
                                        ((int[])value)[i] = _scriptThread.GetRuntimeValueArrayElement(arrayIndex, i).ObjectIndex;
                                    valueType = typeof(int[]);
                                    break;
                                case DataType.Short:
                                    value = new short[arrayLength];
                                    for (int i = 0; i < arrayLength; i++)
                                        ((short[])value)[i] = _scriptThread.GetRuntimeValueArrayElement(arrayIndex, i).ShortLiteral;
                                    valueType = typeof(short[]);
                                    break;
                                case DataType.String:
                                    value = new string[arrayLength];
                                    for (int i = 0; i < arrayLength; i++)
                                        ((string[])value)[i] = _scriptThread.GetRuntimeValueArrayElement(arrayIndex, i).StringLiteral;
                                    valueType = typeof(string[]);
                                    break;
                            }

                            _localCategory.AddProperty(new PropertyListViewItem(variable.Identifier, value, value, "", valueType));
                        }
                        else
                        {
                            RuntimeValue variableValue = _scriptThread.GetRuntimeValueLocal(variable.Identifier); // Can be optimized.
                            object value = "";
                            Type valueType = null;
                            switch (variable.DataType.DataType)
                            {
                                case DataType.Bool: value = variableValue.BooleanLiteral; valueType = typeof(bool); break;
                                case DataType.Byte: value = variableValue.ByteLiteral; valueType = typeof(byte); break;
                                case DataType.Double: value = variableValue.DoubleLiteral; valueType = typeof(double); break;
                                case DataType.Float: value = variableValue.FloatLiteral; valueType = typeof(float); break;
                                case DataType.Int: value = variableValue.IntegerLiteral; valueType = typeof(int); break;
                                case DataType.Long: value = variableValue.LongLiteral; valueType = typeof(long); break;
                                case DataType.Object: value = variableValue.ObjectIndex; valueType = typeof(int); break;
                                case DataType.Short: value = variableValue.ShortLiteral; valueType = typeof(short); break;
                                case DataType.String: value = variableValue.StringLiteral; valueType = typeof(string); break;
                            }
                            _localCategory.AddProperty(new PropertyListViewItem(variable.Identifier, value,value, "", valueType));
                        }
                    }
                }
            }

            // Global list FTW!
               _globalCategory = new PropertyListViewCategory("Globals");
            variablesPropertyListView.AddCategory(_globalCategory);
            foreach (Symbol symbol in _scriptThread.Process.GlobalScope.Symbols)
            {
                if (symbol.Type == SymbolType.Variable)
                {
                    VariableSymbol variable = symbol as VariableSymbol;
                    if (variable.IsArray == true)
                    {
                        int arrayIndex = _scriptThread.GetArrayGlobal(variable.Identifier); // Can be optimized.
                        if (arrayIndex == -1 || _scriptThread.Process.MemoryHeap[arrayIndex - 1] == null)
                        {
                            _globalCategory.AddProperty(new PropertyListViewItem(variable.Identifier, null, null, "", typeof(string)));
                            continue;
                        }

                        int arrayLength = _scriptThread.GetArrayLength(arrayIndex);
                        object value = "";
                        Type valueType = null;

                        switch (variable.DataType.DataType)
                        {
                            case DataType.Bool:
                                value = new bool[arrayLength];
                                for (int i = 0; i < arrayLength; i++)
                                    ((bool[])value)[i] = _scriptThread.GetRuntimeValueArrayElement(arrayIndex, i).BooleanLiteral;
                                valueType = typeof(bool[]);
                                break;
                            case DataType.Byte:
                                value = new byte[arrayLength];
                                for (int i = 0; i < arrayLength; i++)
                                    ((byte[])value)[i] = _scriptThread.GetRuntimeValueArrayElement(arrayIndex, i).ByteLiteral;
                                valueType = typeof(byte[]);
                                break;
                            case DataType.Double:
                                value = new double[arrayLength];
                                for (int i = 0; i < arrayLength; i++)
                                    ((double[])value)[i] = _scriptThread.GetRuntimeValueArrayElement(arrayIndex, i).DoubleLiteral;
                                valueType = typeof(double[]);
                                break;
                            case DataType.Float:
                                value = new float[arrayLength];
                                for (int i = 0; i < arrayLength; i++)
                                    ((float[])value)[i] = _scriptThread.GetRuntimeValueArrayElement(arrayIndex, i).FloatLiteral;
                                valueType = typeof(float[]);
                                break;
                            case DataType.Int:
                                value = new int[arrayLength];
                                for (int i = 0; i < arrayLength; i++)
                                    ((int[])value)[i] = _scriptThread.GetRuntimeValueArrayElement(arrayIndex, i).IntegerLiteral;
                                valueType = typeof(int[]);
                                break;
                            case DataType.Long:
                                value = new long[arrayLength];
                                for (int i = 0; i < arrayLength; i++)
                                    ((long[])value)[i] = _scriptThread.GetRuntimeValueArrayElement(arrayIndex, i).LongLiteral;
                                valueType = typeof(long[]);
                                break;
                            case DataType.Object:
                                value = new int[arrayLength];
                                for (int i = 0; i < arrayLength; i++)
                                    ((int[])value)[i] = _scriptThread.GetRuntimeValueArrayElement(arrayIndex, i).ObjectIndex;
                                valueType = typeof(int[]);
                                break;
                            case DataType.Short:
                                value = new short[arrayLength];
                                for (int i = 0; i < arrayLength; i++)
                                    ((short[])value)[i] = _scriptThread.GetRuntimeValueArrayElement(arrayIndex, i).ShortLiteral;
                                valueType = typeof(short[]);
                                break;
                            case DataType.String:
                                value = new string[arrayLength];
                                for (int i = 0; i < arrayLength; i++)
                                    ((string[])value)[i] = _scriptThread.GetRuntimeValueArrayElement(arrayIndex, i).StringLiteral;
                                valueType = typeof(string[]);
                                break;
                        }

                        _globalCategory.AddProperty(new PropertyListViewItem(variable.Identifier, value, value, "", valueType));
                    }
                    else
                    {
                        RuntimeValue variableValue = _scriptThread.GetRuntimeValueGlobal(variable.Identifier); // Can be optimized.
                        object value = "";
                        Type valueType = null;
                        switch (variable.DataType.DataType)
                        {
                            case DataType.Bool: value = variableValue.BooleanLiteral; valueType = typeof(bool); break;
                            case DataType.Byte: value = variableValue.ByteLiteral; valueType = typeof(byte); break;
                            case DataType.Double: value = variableValue.DoubleLiteral; valueType = typeof(double); break;
                            case DataType.Float: value = variableValue.FloatLiteral; valueType = typeof(float); break;
                            case DataType.Int: value = variableValue.IntegerLiteral; valueType = typeof(int); break;
                            case DataType.Long: value = variableValue.LongLiteral; valueType = typeof(long); break;
                            case DataType.Object: value = variableValue.ObjectIndex; valueType = typeof(int); break;
                            case DataType.Short: value = variableValue.ShortLiteral; valueType = typeof(short); break;
                            case DataType.String: value = variableValue.StringLiteral; valueType = typeof(string); break;
                        }
                        _globalCategory.AddProperty(new PropertyListViewItem(variable.Identifier, value, value, "", valueType));
                    }
                }
            }
            #endregion

            // Fill up stack.
            topIndexLabel.Text = "Top Index: " + _scriptThread.Stack.TopIndex;
            frameIndexLabel.Text = "Frame Index: " + _scriptThread.Stack.FrameIndex;
            int top = _scriptThread.Stack.TopIndex > _scriptThread.Stack.FrameIndex ? _scriptThread.Stack.TopIndex : _scriptThread.Stack.FrameIndex;
            for (int i = 0; i < top; i++)
            {
                RuntimeValue runtimeValue = _scriptThread.Stack.RawStack[i];
                string name = runtimeValue.ToString();
                stackTreeView.Nodes.Add(i + ": " + name, i + ": " + name, 0);
            }

            // Fill up the object stack.
            int index = 0;
            foreach (RuntimeObject obj in _scriptThread.Process.ObjectHeap)
            {
                if (obj != null)
                {
                    string name = obj.ToString();
                    if (name.LastIndexOf('.') > -1)
                        name = name.Substring(name.LastIndexOf('.') + 1);
                    objectsTreeView.Nodes.Add(index + ": " + name, index + ": " + name, 0);
                }
                index++;
            }

            // Fill up the heap.
            index = 0;
            foreach (RuntimeValue obj in _scriptThread.Process.MemoryHeap)
            {
                if (obj != null)
                {
                    string name = obj.ToString();
                    heapTreeView.Nodes.Add(index + ": " + name, index + ": " + name, 0);
                }
                index++;
            }

            // Refresh the variables list!
            variablesPropertyListView.Refresh();
        }
 /// <summary>
 ///		Removes a category from this control.
 /// </summary>
 /// <param name="category">Category to remove.</param>
 public void RemoveCategory(PropertyListViewCategory category)
 {
     _categories.Remove(category);
     category.Parent = null;;
 }
 /// <summary>
 ///		Removes a category from this control.
 /// </summary>
 /// <param name="category">Category to remove.</param>
 public void RemoveCategory(PropertyListViewCategory category)
 {
     _categories.Remove(category);
     category.Parent = null;;
 }
 /// <summary>	
 ///		Adds a new category to this control.
 /// </summary>
 /// <param name="category">Category to add.</param>
 public void AddCategory(PropertyListViewCategory category)
 {
     _categories.Add(category);
     category.Parent = this;
 }
 /// <summary>
 ///		Adds a new category to this control.
 /// </summary>
 /// <param name="category">Category to add.</param>
 public void AddCategory(PropertyListViewCategory category)
 {
     _categories.Add(category);
     category.Parent = this;
 }